Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


   *            the equation to simplify
   * @return the equation in a simplified form.
   * @throws ParserException
   */
  public ConsCell simplifyTerms(ConsCell input) throws ParserException {
    ConsCell output = new ConsCell();
    input = removeExcessParentheses(input);
    ConsCell current = input;
    do { //Recursion
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(simplifyTerms((ConsCell) current.getCar()));
    } while (!((current = current.getNextConsCell()).isNull()));
   
    ArrayList<ConsCell> terms = foldSigns(getTerms(input));
    ArrayList<PairedList<ConsCell, ConsCell>> orders = new ArrayList<PairedList<ConsCell, ConsCell>>();
    ArrayList<String> vars = parser.getVars();
    for (ConsCell term : terms)
      orders.add(orderOfTerm(term, vars));
   
    while (orders.size() > 0) {
      ConsCell term = terms.remove(0);
      if (term.getCarType() == ConsType.OPERATOR && term.length() == 1) {
        output = output.append(term);
        orders.remove(0);
        continue;
      }
      PairedList<ConsCell, ConsCell> order = orders.remove(0);
      BigDec coefficient = getCoefficient(term);
      for (int i = 0; i < terms.size(); i++) {
        if (equalOrders(order, orders.get(i))) {
          coefficient = coefficient.add(getCoefficient(terms.remove(i)));
          orders.remove(i--);
        }
      }
      if (coefficient.eq(BigDec.ZERO))
        continue;
      ConsCell temp = makeTerm(coefficient, order, false);
      output = output.append(temp.getCarType() == ConsType.OPERATOR ? temp : new ConsCell('+', ConsType.OPERATOR, temp, ConsType.CONS_CELL));
    }
   
    output = output.getFirstConsCell();
    if (output.getCarType() == ConsType.OPERATOR && (Character) output.getCar() == '+')
      output = output.remove();
    orders = new ArrayList<PairedList<ConsCell, ConsCell>>();
    terms = getTerms(output);
    for (ConsCell term : terms)
      orders.add(orderOfTerm(term, vars));
    ArrayList<ConsCell> sortedTerms = sortTerms(terms, orders);
    output = new ConsCell();
    for (ConsCell term : sortedTerms)
      output = output.append(term).getLastConsCell();
    output = output.getFirstConsCell();
    if (output.getCarType() == ConsType.OPERATOR && (Character) output.getCar() == '+')
      output = output.remove();
View Full Code Here


    try {
      return Tokenizer.tokenizeString(guiItem.getSelectedItem().toString());
    }
    catch (SyntaxException | UnbalancedParenthesesException e) {
      getPlugin().getParser().getLog().logError("Unable to tokenize the selected item in " + getName() + " in " + getPlugin().getID());
      return new ConsCell();
    }
  }
View Full Code Here

 
  public ConsCell makeTerm(BigDec coefficient, PairedList<ConsCell, ConsCell> orders, boolean compressNegative) throws UndefinedResultException {
    boolean negate = coefficient.lt(BigDec.ZERO);
    if (negate && !compressNegative)
      coefficient = coefficient.multiply(BigDec.MINUSONE);
    ConsCell output = new ConsCell(coefficient, ConsType.NUMBER);
    ConsCell result = output;
    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));
    }
    if (coefficient.eq(BigDec.ONE) && result.length() >= 3 && result.getCarType(1) == ConsType.OPERATOR && (Character) result.getCar(1) == '*')
      result = result.remove().remove();
    return (negate && !compressNegative) ? new ConsCell('-', ConsType.OPERATOR, result, ConsType.CONS_CELL) : result;
  }
View Full Code Here

    input = parser.removeDoubles(input);
    return simplifyTerms(distributiveRecursion(distributeExponents(input)));
  }
 
  private ConsCell distributiveRecursion(ConsCell input) throws ParserException {
    ConsCell current = input, result = input;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(distributiveRecursion((ConsCell) current.getCar()));
    } while (!(current = current.getNextConsCell()).isNull());
    current = input;
    do {
      if (current.getCarType() != ConsType.OPERATOR || (Character) current.getCar() == '+' || (Character) current.getCar() == '^' || (Character) current.getCar() == '%')
        continue;
      ConsCell next = new ConsCell(), previous = new ConsCell();
      ConsCell head = current.getNextConsCell();
      do {
        next = next.append(head.singular());
        if ((head = head.remove()).getCarType() == ConsType.OPERATOR && (Character) head.getCar() == '^')
          next = next.append(head.singular());
        else
          break;
      } while (!(head = head.remove()).isNull());
      next = next.getFirstConsCell();
      if ((Character) current.getCar() != '-') {
        head = current.getPreviousConsCell();
        do {
          previous = previous.isNull() ? head.singular() : new ConsCell(head.getCar(), head.getCarType(), previous, ConsType.CONS_CELL);
          if ((head = head.remove().getPreviousConsCell()).getCarType() == ConsType.OPERATOR && (Character) head.getCar() == '^')
            previous = new ConsCell('^', ConsType.OPERATOR, previous, ConsType.CONS_CELL);
          else
            break;
        } while (!(head = head.remove().getPreviousConsCell()).isNull());
        result = current;
      }
      switch ((Character) current.getCar()) {
        case '-': {
          //Invert all signs in the next item
          ConsCell insert;
          if (!parser.containsVariables(next))
            insert = parser.run(new ConsCell(BigDec.MINUSONE, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, new ConsCell(next, ConsType.CONS_CELL))));
          else if (next.getCarType() == ConsType.CONS_CELL)
            insert = new ConsCell(distributiveRecursion(invertSign((ConsCell) next.getCar())), ConsType.CONS_CELL);
          else {
            current.insert(next);
            continue;
          }
          current.replaceCar(new ConsCell('+', ConsType.OPERATOR));
          ConsCell end = insert.getLastConsCell();
          current.insert(insert);
          if (current.getPreviousConsCell().isNull())
            current = current.remove();
          result = current = end;
          continue;
        }
        case '*': {
          ArrayList<ConsCell> left = new ArrayList<>(), right = new ArrayList<>();
         
          if (previous.length() != 1)
            left.add(previous);
          else if (previous.getCarType() == ConsType.CONS_CELL)
            left = foldSigns(getTerms((ConsCell) previous.getCar()));
          else if (previous.getCarType() == ConsType.OPERATOR)
            if ((Character) previous.getCar() == '-')
              left.add(new ConsCell(BigDec.MINUSONE, ConsType.NUMBER));
            else
              left.add(new ConsCell(BigDec.ONE, ConsType.NUMBER));
          else
            left.add(previous);
         
          if (next.length() != 1)
            right.add(next);
          else if (next.getCarType() == ConsType.CONS_CELL)
            right = foldSigns(getTerms((ConsCell) next.getCar()));
          else if (next.getCarType() == ConsType.OPERATOR)
            if ((Character) next.getCar() == '-')
              right.add(new ConsCell(BigDec.MINUSONE, ConsType.NUMBER));
            else
              right.add(new ConsCell(BigDec.ONE, ConsType.NUMBER));
          else
            right.add(next);
         
          ConsCell insert = new ConsCell();
          for (ConsCell lTerm : left)
            for (ConsCell rTerm : right)
              insert = insert.append(lTerm.clone()).append(new ConsCell('*', ConsType.OPERATOR)).append(rTerm.clone()).append(new ConsCell('+', ConsType.OPERATOR));
          if (!insert.isNull()) {
            insert.remove();
            insert = insert.getFirstConsCell();
          }
         
          ConsCell end = insert.getLastConsCell();
          if (isOneTerm(insert) && insert.length() != 1) {
            current.replace(insert);
            result = current = end;
          }
          else
            current.replaceCar(insert);
          continue;
        }
        case '/': {
          ConsCell insert = previous.length() == 1 && next.length() == 1 ? polynomialDivision(previous, next, new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER), true) :
              new ConsCell("{true}", ConsType.OBJECT);
          if (((String) insert.getLastConsCell().getCar()).equals("{false}")) {
            insert.getLastConsCell().remove();
            current.replaceCar(insert);
          }
          else
            current.replaceCar(new ConsCell(previous.getCar(), previous.getCarType(), new ConsCell('/', ConsType.OPERATOR, new ConsCell(next, ConsType.CONS_CELL))));
          continue;
        }
      }
    } while (!(current = current.getNextConsCell()).isNull());
    return result.getFirstConsCell();
View Full Code Here

    } while (!(current = current.getNextConsCell()).isNull());
    return result.getFirstConsCell();
  }
 
  private ConsCell distributeExponents(ConsCell input) throws ParserException {
    ConsCell current = input, result = input;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(distributeExponents((ConsCell) current.getCar()));
    } while (!(current = current.getNextConsCell()).isNull());
    current = input;
    while (!(current = current.getNextConsCell()).isNull()) {
      if (current.getCarType() == ConsType.OPERATOR && (Character) current.getCar() == '^') {
        ConsCell insert = new ConsCell();
        ConsCell base = current.getPreviousConsCell().singular();
        if (base.getCarType() != ConsType.CONS_CELL)
          continue;
        ConsCell power = new ConsCell();
        ConsCell head = current.getNextConsCell();
        if (head.getCarType() == ConsType.OPERATOR && (Character) head.getCar() == '-') {
          power = power.append(head.singular());
          head.remove();
        }
        boolean skipCheck = false;
        while (!(head = current.getNextConsCell()).isNull() && (skipCheck || !(head.getCarType() == ConsType.OPERATOR && (Character) head.getCar() != '^'))) {
          power = power.append(head.singular());
          head.remove();
          skipCheck = (power.getCarType() == ConsType.OPERATOR || (power.getCarType() == ConsType.IDENTIFIER && !parser.containsVariables(power)));
        }
        power = parser.run(removeExcessParentheses(power.getFirstConsCell()));
        if (parser.containsVariables(power)) {
          ArrayList<ConsCell> terms = getTerms(power);
          ArrayList<ConsCell> varTerms = new ArrayList<ConsCell>();
          int i = 1;
          if (terms.get(0).getCarType() != ConsType.OPERATOR && parser.containsVariables(terms.get(0)))
            varTerms.add(terms.remove(0));
          else
            i = 2;
          for (; i < terms.size(); i += 2) {
            if (parser.containsVariables(terms.get(i))) {
              varTerms.add(terms.remove(--i));
              varTerms.add(terms.remove(i));
              i--;
            }
          }
          power = new ConsCell();
          for (ConsCell term : varTerms)
            power = power.append(term);
          power = power.getFirstConsCell();
          if (power.getCarType() == ConsType.OPERATOR && (Character) power.getCar() == '+')
            power = power.remove();
          insert = insert.append(new ConsCell('*', ConsType.OPERATOR)).append(base.clone()).append(new ConsCell('^', ConsType.OPERATOR)).append(new ConsCell(power, ConsType.CONS_CELL));
          power = new ConsCell();
          for (ConsCell term : terms)
            power = power.append(term).getLastConsCell();
          power = power.getFirstConsCell();
          if (power.getCarType() == ConsType.OPERATOR && (Character) power.getCar() == '+')
            power = power.remove();
        }
        //if (power.getCarType() != ConsType.NUMBER)
        //throw new ParserException("Unsupported exponential format in the equation solver", null);
        BigDec pow = new BigDec(power.toString());
        int exp = pow.intValue();
        pow = pow.subtract(new BigDec(exp));
        boolean negative = exp < 0;
        for (int i = 0; i < exp; i++)
          insert = insert.append(new ConsCell(negative ? '/' : '*', ConsType.OPERATOR)).append(base.clone());
        if (!pow.eq(BigDec.ZERO))
          insert = insert.append(new ConsCell(negative ? '/' : '*', ConsType.OPERATOR)).append(base.clone()).append(new ConsCell('^', ConsType.OPERATOR)).append(new ConsCell(pow, ConsType.NUMBER));
       
        insert = insert.getFirstConsCell();
        if (current.getPreviousConsCell() == input) {
          input = current;
          if (negative)
            insert = new ConsCell(BigDec.ONE, ConsType.NUMBER).append(insert).getFirstConsCell();
          else
            insert = insert.remove();
        }
        if (insert.getFirstConsCell().getCarType() == ConsType.OPERATOR && (Character) insert.getCar() == '*')
          insert = insert.remove();
        current.getPreviousConsCell().remove();
        current.insert(insert);
        ConsCell temp = current;
        current = head.isNull() ? insert.getLastConsCell() : head;
        temp.remove();
        result = current;
      }
    }
    return result.getFirstConsCell();
  }
View Full Code Here

      if ((Character) terms.get(i).getCar() == '+')
        terms.remove(i--);
      else if ((Character) terms.get(i).getCar() == '-') {
        if (terms.get(i + 1).getCarType() == ConsType.OPERATOR && terms.get(i + 1).length() == 1) {
          if ((Character) terms.get(i + 1).getCar() == '+') {
            terms.get(i + 1).replaceCar(new ConsCell('-', ConsType.OPERATOR));
            terms.remove(i--);
          }
          else if ((Character) terms.get(i + 1).getCar() == '-') {
            terms.get(i + 1).replaceCar(new ConsCell('+', ConsType.OPERATOR));
            terms.remove(i--);
          }
        }
        else if (terms.get(i + 1).getCarType() == ConsType.NUMBER) {
          terms.get(i + 1).replaceCar(new ConsCell(((BigDec) terms.get(i + 1).getCar()).multiply(BigDec.MINUSONE), ConsType.NUMBER));
          terms.remove(i--);
        }
        else {
          terms.set(i + 1, new ConsCell('-', ConsType.OPERATOR, terms.get(i + 1)));
          terms.remove(i--);
        }
      }
    }
    return terms;
View Full Code Here

   * @return the equation with all excess parentheses removed
   */
  public ConsCell removeExcessParentheses(ConsCell input) {
    while (input.length() == 1 && input.getCarType() == ConsType.CONS_CELL)
      input = (ConsCell) input.getCar();
    ConsCell current = input;
    do {
      if (current.getCarType() != ConsType.CONS_CELL)
        continue;
      //We can drop the parentheses if the interior has a length of 1 after the recursive call
      current.replaceCar(removeExcessParentheses((ConsCell) current.getCar()));
      if (current.getCarType() != ConsType.CONS_CELL)
        continue;
      /* We can also drop the parentheses in the following cases:
       * Left: + on any f(x), -,* on one term, ^,/,% on length == 1, not operator on length == 1
       * Right: +,- on any f(x), *,/ on one term, ^,% on length == 1, not operator on length == 1
       */
      Object left = current.getPreviousConsCell().getCar(), right = current.getNextConsCell().getCar();
      boolean isOneTerm = isOneTerm((ConsCell) current.getCar());
      int length = ((ConsCell) current.getCar()).length();
      if (!(left == null || !(left instanceof Character) && length == 1 || left instanceof Character && ((Character) left == '+' || isOneTerm && ((Character) left == '-' || (Character) left == '*') ||
          length == 1 && ((Character) left == '^' || (Character) left == '/' || (Character) left == '%'))))
        continue;
      if (!(right == null || !(right instanceof Character) && length == 1 || right instanceof Character && ((Character) right == '+' || (Character) right == '-' || isOneTerm && ((Character) right == '*' ||
          (Character) right == '/') || length == 1 && ((Character) right == '^' || (Character) right == '%'))))
        continue;
      current = current.replace((ConsCell) current.getCar());
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    return input;
  }
View Full Code Here

 
  public ArrayList<ConsCell> sortTerms(ArrayList<ConsCell> terms, ArrayList<PairedList<ConsCell, ConsCell>> orders) {
    if (terms.size() < 1)
      return terms;
    if (terms.get(0).getCarType() != ConsType.OPERATOR) {
      terms.add(0, new ConsCell('+', ConsType.OPERATOR));
      orders.add(0, new PairedList<ConsCell, ConsCell>());
    }
    for (int i = 1; i < terms.size(); i += 2) {
      if (terms.get(i).getCarType() == ConsType.OPERATOR)
        continue;
      for (int j = i + 2; j < terms.size(); j += 2) {
        if (terms.get(j).getCarType() == ConsType.OPERATOR)
          continue;
        if (orders.get(i).compareTo(orders.get(j)) < 0) {
          ConsCell temp = terms.get(i), tempSign = terms.get(i - 1);
          PairedList<ConsCell, ConsCell> tempOrder = orders.get(i), tempOrderSign = orders.get(i - 1);
          terms.set(i, terms.get(j));
          terms.set(j, temp);
          terms.set(i - 1, terms.get(j - 1));
          terms.set(j - 1, tempSign);
View Full Code Here

    return terms;
  }
 
  public ConsCell invertSign(ConsCell input) {
    if (input.getCarType() != ConsType.OPERATOR)
      input = new ConsCell('-', ConsType.OPERATOR).append(input).getFirstConsCell();
    else if ((Character) input.getCar() == '-')
      input = input.remove();
    else
      input.replaceCar(new ConsCell('-', ConsType.OPERATOR));
   
    ConsCell current = input;
    while (!(current = current.getNextConsCell()).isNull()) {
      if (current.getCarType() == ConsType.OPERATOR) {
        if ((Character) input.getCar() == '^') {
          current = current.getNextConsCell();
          if (current.isNull())
            break;
          continue;
        }
        if ((Character) current.getCar() == '-')
          current.replaceCar(new ConsCell('+', ConsType.OPERATOR));
        else if ((Character) current.getCar() == '+')
          current.replaceCar(new ConsCell('-', ConsType.OPERATOR));
      }
    }
    return input.getFirstConsCell();
  }
View Full Code Here

    for (int i = 0; i < components.size(); i++)
      this.components[i] = parser.run(components.get(i));
  }
 
  public ConsCell dot(Vector b) throws ParserException {
    ConsCell result = new ConsCell();
    for (int i = 0; i < components.length; i++)
      result = result.append(new ConsCell('+', ConsType.OPERATOR, components[i])).append(new ConsCell('*', ConsType.OPERATOR, b.components[i]));
    if (!result.isNull())
      result = result.getFirstConsCell().remove();
    return parser.run(result);
  }
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.