Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


   * @return true if the equation contains at least one of the variables, false otherwise
   * @see #containsVariables(ConsCell, ArrayList) containsVariable(ConsCell input, ArrayList<String> variables)
   * @see #containsVariables(ConsCell) containsVariable(ConsCell input)
   */
  public boolean containsVariables(ConsCell input, ArrayList<String> variables, ArrayList<String> otherOps) {
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.IDENTIFIER && !allNames.contains(current.getCar()) && !otherOps.contains(current.getCar()) && variables.contains(current.getCar()))
        return true;
      if (current.getCarType() == ConsType.CONS_CELL && containsVariables(((ConsCell) current.getCar()), variables, otherOps))
        return true;
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    return false;
  }
View Full Code Here


   * @param input
   *            the section of the equation to be checked and fixed, if applicable
   * @return the input with the above cases removed
   */
  public ConsCell removeDoubles(ConsCell input) {
    ConsCell current = input, next = current.getNextConsCell();
    if (next.isNull())
      return input;
    boolean step = true;
    do {
      step = true;
      if (current.getCarType() == ConsType.CONS_CELL) {
        current.replaceCar(removeDoubles((ConsCell) current.getCar()));
        continue;
      }
      if (current.getCarType() != ConsType.OPERATOR || next.getCarType() != ConsType.OPERATOR)
        continue;
      char currentCar = (Character) current.getCar(), nextCar = (Character) next.getCar();
      if ((currentCar == '*' && nextCar == '/') || (currentCar == '/' && nextCar == '*')) {
        next.remove();
        current.replaceCar(new ConsCell('/', ConsType.OPERATOR));
        step = false;
      }
      if ((currentCar == '-' && nextCar == '-') || (currentCar == '+' && nextCar == '+')) {
        next.remove();
        current.replaceCar(new ConsCell('+', ConsType.OPERATOR));
        step = false;
      }
      if ((currentCar == '-' && nextCar == '+') || (currentCar == '+' && nextCar == '-')) {
        next.remove();
        current.replaceCar(new ConsCell('-', ConsType.OPERATOR));
        step = false;
      }
      if (nextCar == '^')
        current.insert(new ConsCell(BigDec.ZERO, ConsType.NUMBER));
      next = current.getNextConsCell();
    } while (!step || (step && !((current = current.getNextConsCell()).isNull() || (next = current.getNextConsCell()).isNull()))); //This steps current forward while checking for nulls
   
    return input;
  }
View Full Code Here

  public ArrayList<ParserPlugin> getPlugins() {
    return plugins;
  }
 
  public ConsCell replaceKeywords(ConsCell input) throws SyntaxException, UnbalancedParenthesesException, ParserException {
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.IDENTIFIER) {
        if (keywords.containsKey(current.getCar()))
          current.replaceCar(Tokenizer.tokenizeString(((KeywordPlugin) this.keywords.get(current.getCar()).getPlugin()).getKeywordData((String) current.getCar())));
      }
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(replaceKeywords((ConsCell) current.getCar()));
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    return input;
  }
View Full Code Here

  }
 
  public ConsCell polynomialDivision(ConsCell dividend, ConsCell divisor, ConsCell var, boolean flagRemainder) throws ParserException {
    BigDec[] dividendCoefficients = getSyntheticCoefficients(dividend, var);
    if (dividendCoefficients == null)
      return new ConsCell("{false}", ConsType.OBJECT);
    BigDec[] divisorCoefficients = getSyntheticCoefficients(divisor, var);
    if (divisorCoefficients == null)
      return new ConsCell("{false}", ConsType.OBJECT);
   
    int resultOrder = dividendCoefficients.length - divisorCoefficients.length;
    ConsCell result = new ConsCell();
    for (int i = 0; i <= resultOrder; i++) {
      BigDec resultCoefficient = dividendCoefficients[i].divide(divisorCoefficients[0]);
      dividendCoefficients[i] = BigDec.ZERO;
      for (int j = 1; j < divisorCoefficients.length; j++)
        dividendCoefficients[i + j] = dividendCoefficients[i + j].subtract(divisorCoefficients[j].multiply(resultCoefficient));
      result = result.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(resultCoefficient, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, new ConsCell(var, ConsType.CONS_CELL,
          new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(resultOrder - i), ConsType.NUMBER)))))));
    }
    ConsCell remainder = new ConsCell();
    for (int i = 0; i < dividendCoefficients.length; i++)
      if (dividendCoefficients[i].neq(BigDec.ZERO))
        remainder = remainder.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(dividendCoefficients[i], ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR,
            new ConsCell(var, ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(dividendCoefficients.length - 1 - i), ConsType.NUMBER)))))));
    remainder = remainder.getFirstConsCell();
    if (!remainder.isNull()) {
      remainder = remainder.remove();
      remainder = new ConsCell(remainder, ConsType.CONS_CELL, new ConsCell('/', ConsType.OPERATOR, new ConsCell(divisor.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL);
      result = result.append(new ConsCell('+', ConsType.OPERATOR, remainder, ConsType.CONS_CELL));
    }
    result = result.getFirstConsCell();
    if (!result.isNull())
      result = simplifyTerms(result.remove());
    if (flagRemainder)
      result.getLastConsCell().append(new ConsCell("{" + !remainder.isNull() + "}", ConsType.OBJECT));
    return result;
  }
View Full Code Here

    ArrayList<ConsCell> terms = foldSigns(getTerms(simplifyTerms(equation)));
    BigDec equationOrder = BigDec.ZERO;
    for (ConsCell term : terms)
      termOrders.add(orderOfTerm(term, vars));
    for (PairedList<ConsCell, ConsCell> order : termOrders) {
      ConsCell temp = order.containsKey(var) ? parser.run(order.get(var)) : new ConsCell(BigDec.ZERO, ConsType.NUMBER);
      if (temp.length() != 1 || temp.getCarType() != ConsType.NUMBER || ((BigDec) temp.getCar()).lt(BigDec.ZERO) || !((BigDec) temp.getCar()).isInt())
        return null;
      if (((BigDec) temp.getCar()).gt(equationOrder))
        equationOrder = (BigDec) temp.getCar();
    }
    int highestOrder = equationOrder.intValue();
    BigDec[] equationCoefficients = new BigDec[highestOrder + 1];
    for (int i = 0; i < equationCoefficients.length; i++)
      equationCoefficients[i] = BigDec.ZERO;
    for (int i = 0; i < terms.size(); i++) {
      ConsCell temp = termOrders.get(i).containsKey(var) ? parser.run(termOrders.get(i).get(var)) : new ConsCell(BigDec.ZERO, ConsType.NUMBER);
      equationCoefficients[highestOrder - ((BigDec) temp.getCar()).intValue()] = equationCoefficients[highestOrder - ((BigDec) temp.getCar()).intValue()].add(getCoefficient(terms.get(i)));
    }
    return equationCoefficients;
  }
View Full Code Here

      while (num.mod(prime).eq(BigDec.ZERO)) {
        factors.add(primes[i]);
        num = num.divide(prime);
      }
    }
    ConsCell output = new ConsCell();
    for (long integer : factors)
      output = output.append(new ConsCell(",", ConsType.SEPARATOR)).append(new ConsCell(new BigDec(integer), ConsType.NUMBER));
    output = output.getFirstConsCell();
    if (output.getCarType() == ConsType.SEPARATOR)
      output = output.remove();
    return output;
  }
View Full Code Here

  }
 
  private ArrayList<BigDec> substitutionSolve(ConsCell equation) throws ParserException {
    ArrayList<String> initialVars = new ArrayList<String>(parser.getVars());
    ArrayList<BigDec> tempSolutions = new ArrayList<BigDec>(), solutions = new ArrayList<BigDec>();
    ConsCell parts[] = seekSubstitutions(equation.clone());
    if (parser.getVariables(parts[0]).size() > 1)
      return solutions;
    tempSolutions = solve(parts[0], false, false);
    if (parts[1].isNull()) {
      for (int i = 0; i < tempSolutions.size(); i++)
        if (!isAnswer(equation, tempSolutions.get(i), BigDec.ZERO, accuracy)) {
          tempSolutions.remove(i);
          i--;
        }
      return tempSolutions;
    }
    else {
      parser.setVars(initialVars);
      for (BigDec d : tempSolutions) {
        try {
          ConsCell deSubEqn = reverseSubstitution(parts[1], d);
          initialInput.push(deSubEqn);
          for (BigDec solutin : solve(deSubEqn, true, false))
            solutions.add(solutin);
        }
        finally {
View Full Code Here

    return checkSolutions(solutions, equation, accuracy);
  }
 
  private ConsCell reverseSubstitution(ConsCell substitution, BigDec z) throws ParserException {
    int subID = subID(substitution);
    ConsCell deSubEqn = parser.run(reverseSubstitutions[subID].replaceAll("z", z.toString()));
    return removeExcessParentheses(subID == substitutions.length - 1 ? substitution.clone() : substitution.getNextConsCell().clone()).getLastConsCell().append(
        parser.removeDoubles(new ConsCell('+', ConsType.OPERATOR, invertSign(deSubEqn)))).getFirstConsCell();
  }
View Full Code Here

        return i;
    return -1;
  }
 
  private ConsCell[] seekSubstitutions(ConsCell equation) throws ParserException {
    return seekSubstitutions(equation, new ConsCell());
  }
View Full Code Here

  private ConsCell[] seekSubstitutions(ConsCell equation) throws ParserException {
    return seekSubstitutions(equation, new ConsCell());
  }
 
  public ConsCell[] seekSubstitutions(ConsCell equation, ConsCell substitution) throws ParserException {
    return seekSubstitutions(equation, substitution, new ConsCell("y", ConsType.IDENTIFIER));
  }
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.