Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


    return seekSubstitutions(equation, substitution, new ConsCell("y", ConsType.IDENTIFIER));
  }
 
  private ConsCell[] seekSubstitutions(ConsCell equation, ConsCell substitution, ConsCell u) throws ParserException {
    int subID = -1;
    ConsCell current = equation;
    if (!substitution.isNull())
      subID = subID(substitution);
    do {
      ConsCell previous = current.getPreviousConsCell();
      if (substitution.isNull()) {
        if (parser.containsVariables(current.singular()) && current.getCarType() == ConsType.CONS_CELL &&
            (current.isFirstConsCell() || !isSubstitution(previous.singular().toString()))) { //If the thing before this isn't a substitution operation
          substitution = ((ConsCell) current.singular().getCar()).clone();
          if (!isOneTerm(substitution))
            substitution = new ConsCell(substitution, ConsType.CONS_CELL);
          subID = subID(substitution);
        }
        else if (parser.containsVariables(current.getNextConsCell().singular()) && (subID = subID(current)) >= 0)
          substitution = new ConsCell(current.getCar(), current.getCarType(), current.getNextConsCell().singular());
      }
      if (!substitution.isNull()) {
        if (subID == substitutions.length - 1 && current.singular().equals(substitution))
          current.replaceCar(u);
        else if (subID != 20 && current.singular().append(current.getNextConsCell().singular()).getFirstConsCell().equals(substitution)) {
View Full Code Here


    }
    return checkSolutions(solutions, equation, accuracy);
  }
 
  private BigDec getY(ConsCell equation, BigDec x) throws ParserException {
    equation.replaceAll(new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER), new ConsCell(x, ConsType.NUMBER));
    ConsCell result = parser.run(equation);
    if (result.getCarType() != ConsType.NUMBER)
      throw new ParserException("Invalid equation", null);
    return (BigDec) result.getCar();
  }
View Full Code Here

   * @return an ArrayList<ConsCell> containing the terms of the equation and the signs between them.
   * @throws ParserException
   */
  public ArrayList<ConsCell> getTerms(ConsCell equation) {
    ArrayList<ConsCell> output = new ArrayList<ConsCell>();
    ConsCell current = equation;
    do {
      if (current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '+' || (Character) current.getCar() == '-')) {
        output.add(current.singular());
        continue;
      }
      ConsCell term = new ConsCell();
      boolean skipSign = false;
      do {
        skipSign = current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/' || (Character) current.getCar() == '^');
        term = term.append(current.singular());
      } while (!((current = current.getNextConsCell()).isNull()) && (skipSign || !(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '+' || (Character) current.getCar() == '-'))));
      output.add(term.getFirstConsCell());
      if (!current.isNull())
        output.add(current.singular());
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    return output;
  }
View Full Code Here

  }
 
  public ConsCell condenseCoefficient(ConsCell term, ArrayList<String> vars) throws ParserException {
    PairedList<ConsCell, ConsCell> orders = orderOfTerm(term, vars);
    ArrayList<ConsCell> segments = getTermSegments(term);
    ConsCell numerator = new ConsCell(), denominator = new ConsCell();
    if (!parser.containsVariables(segments.get(0), vars))
      numerator = numerator.append(new ConsCell('*', ConsType.OPERATOR, segments.get(0)));
    for (int i = 2; i < segments.size(); i += 2) {
      if (!parser.containsVariables(segments.get(i), vars) && segments.get(i - 1).getCarType() == ConsType.OPERATOR) {
        //TODO make this recursive.  Maybe use distributive property?
        if ((Character) segments.get(i - 1).getCar() == '*')
          numerator = numerator.append(new ConsCell('*', ConsType.OPERATOR, segments.get(i)));
        if ((Character) segments.get(i - 1).getCar() == '/')
          denominator = denominator.append(new ConsCell('*', ConsType.OPERATOR, segments.get(i)));
      }
    }
    numerator = numerator.getFirstConsCell();
    denominator = denominator.getFirstConsCell();
    BigDec num = new BigDec(parser.run(numerator.length() > 0 ? numerator.remove() : new ConsCell(BigDec.ONE, ConsType.NUMBER)).toString());
    BigDec den = new BigDec(parser.run(denominator.length() > 0 ? denominator.remove() : new ConsCell(BigDec.ONE, ConsType.NUMBER)).toString()), gcd = num.gcd(den);
    num = num.divide(gcd);
    den = den.divide(gcd);
    ConsCell output = new ConsCell(num, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(den, ConsType.NUMBER))).getLastConsCell();
    for (ConsCell key : orders) {
      ConsCell exp = orders.get(key);
      if (exp.length() == 1 && exp.getCarType() == ConsType.NUMBER) { //If the exponent is a real number
        if (((BigDec) exp.getCar()).eq(BigDec.ZERO))
          continue;
        output = output.append(new ConsCell('*', ConsType.OPERATOR)).append(key.clone());
        if (((BigDec) exp.getCar()).eq(BigDec.ONE))
          continue;
        output = output.append(new ConsCell('^', ConsType.OPERATOR)).append(exp.clone());
        continue;
      }
      //For anything else, stick the exponent in parentheses and append it
      output = output.append(new ConsCell('*', ConsType.OPERATOR)).append(key.clone()).append(new ConsCell('^', ConsType.OPERATOR)).append(new ConsCell(exp.clone(), ConsType.CONS_CELL));
    }
    output = output.getFirstConsCell();
    return output;
  }
View Full Code Here

        return BigDec.MINUSONE;
      return BigDec.ONE;
    }
   
    BigDec coefficient = BigDec.ONE;
    ConsCell current = term;
    boolean negative = false;
    do {
      if (current.getCarType() == ConsType.OPERATOR && (Character) current.getCar() == '-')
        negative = !negative;
      if (!(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/'))) {
        boolean divide = current.getPreviousConsCell().getCarType() == ConsType.OPERATOR && (Character) current.getPreviousConsCell().getCar() == '/';
        ConsCell eqn = current.singular();
        while (!(current = current.getNextConsCell()).isNull() && !(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/')))
          eqn = eqn.append(current.singular());
        eqn = eqn.getFirstConsCell();
        if (parser.containsVariables(eqn, vars))
          continue;
        eqn = parser.run(eqn);
        if (eqn.length() == 1 && eqn.getCarType() == ConsType.NUMBER)
          coefficient = divide ? coefficient.divide((BigDec) eqn.getCar()) : coefficient.multiply((BigDec) eqn.getCar());
      }
    } while (!(current = current.getNextConsCell()).isNull());
    return negative ? coefficient.multiply(BigDec.MINUSONE) : coefficient;
  }
View Full Code Here

  }
 
  @Override
  public ConsCell getValue() {
    try {
      ConsCell result = new ConsCell(), head = result;
      for (T item : guiItem.getSelectedValuesList())
        head = head.append(Tokenizer.tokenizeString(item.toString())).append(new ConsCell("\n", ConsType.SEPARATOR));
      if (head.getCarType() == ConsType.SEPARATOR)
        head.remove();
      return result;
    }
    catch (SyntaxException | UnbalancedParenthesesException e) {
      getPlugin().getParser().getLog().logError("Unable to tokenize the selected items in " + getName() + " in " + getPlugin().getID());
      return new ConsCell();
    }
  }
View Full Code Here

    });
  }
 
  @Override
  public ConsCell getValue() {
    return new ConsCell("{" + guiItem.isSelected() + "}", ConsType.OBJECT);
  }
View Full Code Here

   *            the term to get the segments of
   * @return the segments of the term and the signs between them, in order, in an ArrrayList
   */
  public ArrayList<ConsCell> getTermSegments(ConsCell term) {
    ArrayList<ConsCell> output = new ArrayList<ConsCell>();
    ConsCell segment = new ConsCell();
    do {
      if (term.getCarType() == ConsType.OPERATOR && ((Character) term.getCar() == '*' || (Character) term.getCar() == '/')) {
        output.add(segment.getFirstConsCell());
        output.add(term.singular());
        segment = new ConsCell();
      }
      else
        segment = segment.append(term.singular());
    } while (!((term = term.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    if (!segment.isNull() && segment.length() > 0)
      output.add(segment);
    return output;
  }
View Full Code Here

   */
  public PairedList<ConsCell, ConsCell> orderOfTerm(ConsCell term, ArrayList<String> variables) throws ParserException {
    PairedList<ConsCell, ConsCell> orders = new PairedList<ConsCell, ConsCell>();
    if (term.length() < 1)
      return orders;
    ConsCell current = term;
    do {
      if (current.getCarType() != ConsType.OPERATOR && !(current.getNextConsCell().getCarType() == ConsType.OPERATOR && (Character) current.getNextConsCell().getCar() == '^')) {
        ConsCell eqn = current.singular();
        boolean subtract = current.getPreviousConsCell().getCarType() == ConsType.OPERATOR && (Character) current.getPreviousConsCell().getCar() == '/';
        while (!(current = current.getNextConsCell()).isNull() && !(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/'))) {
          if (current.getCarType() == ConsType.OPERATOR) {
            if ((Character) current.getCar() == '^')
              break;
            if ((Character) current.getCar() == '-')
              continue;
          }
          eqn = eqn.append(current.singular());
        }
        if (current.getCarType() == ConsType.OPERATOR && (Character) current.getCar() == '^') {
          current = current.getPreviousConsCell();
          continue;
        }
        eqn = eqn.getFirstConsCell();
        if (!orders.containsKey(eqn))
          orders.put(eqn, new ConsCell());
        orders.put(eqn, orders.get(eqn).getLastConsCell().append(new ConsCell(subtract ? '-' : '+', ConsType.OPERATOR)).append(new ConsCell(BigDec.ONE, ConsType.NUMBER)).getFirstConsCell());
      }
      if (current.getCarType() == ConsType.OPERATOR && (Character) current.getCar() == '^') {
        ConsCell head = current.getPreviousConsCell();
        ConsCell left = new ConsCell();
        do {
          if (!(head.getCarType() == ConsType.OPERATOR && (Character) head.getCar() == '-'))
            left = head.singular().append(left.getFirstConsCell());
        } while (!(head = head.getPreviousConsCell()).isNull() && !(head.getCarType() == ConsType.OPERATOR && ((Character) head.getCar() == '*' || (Character) head.getCar() == '/')));
       
        left = left.getFirstConsCell();
        boolean subtract = left.getCarType() == ConsType.OPERATOR && (Character) left.getCar() == '/';
        if (subtract)
          left = left.remove();
        if (!orders.containsKey(left))
          orders.put(left, new ConsCell());
        head = current.getNextConsCell();
        ConsCell exponent = head.singular();
        while (!(head = head.getNextConsCell()).isNull() && !(head.getCarType() == ConsType.OPERATOR && ((Character) head.getCar() == '*' || (Character) head.getCar() == '/')))
          exponent = exponent.append(head.singular());
       
        exponent = exponent.getFirstConsCell();
        orders.put(left, orders.get(left).getLastConsCell().append(new ConsCell(subtract ? '-' : '+', ConsType.OPERATOR)).append(exponent).getFirstConsCell());
        current = head;
      }
    } while (!(current = current.getNextConsCell()).isNull());
   
    for (int i = 0; i < orders.size(); i++) {
      ConsCell key = orders.getKeys().get(i);
      if (!parser.containsVariables(key, variables) && !parser.containsVariables(orders.get(key), variables)) {
        orders.remove(key);
        i--;
        continue;
      }
View Full Code Here

    return orderOfEquation(orders, vars);
  }
 
  public BigDec orderOfEquation(PairedList<ConsCell, PairedList<ConsCell, ConsCell>> orders, ArrayList<String> vars) throws ParserException {
    BigDec highest = null;
    ConsCell var = new ConsCell(vars.get(0), ConsType.IDENTIFIER);
    for (PairedList<ConsCell, ConsCell> value : orders.getValues()) {
      BigDec order = BigDec.ZERO;
      if (value.containsKey(var))
        order = new BigDec(parser.run(value.get(var)).toString());
      if (highest == null || order.gt(highest))
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.util.ConsCell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.