Package lipstone.joshua.parser.types

Examples of lipstone.joshua.parser.types.BigDec


      orders.put(term, orderOfTerm(term, vars));
    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))
        highest = order;
    }
    return highest;
  }
View Full Code Here


   * @throws ParserException
   */
  public ConsCell simplifyTerm(ConsCell term) throws ParserException {
    if (!parser.containsVariables(term, parser.getVars()))
      return parser.run(term);
    BigDec coefficient = getCoefficient(term);
    PairedList<ConsCell, ConsCell> orders = orderOfTerm(term, parser.getVars());
    return makeTerm(coefficient, orders);
  }
View Full Code Here

        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));
    }
   
View Full Code Here

          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;
View Full Code Here

      }
     
      //Numbers
      m = numbers.matcher(input);
      if (!found && m.find(current) && m.start() == current) {
        head = head.append(new ConsCell(new BigDec(m.group()), ConsType.NUMBER));
        current += m.group().length();
        found = true;
      }
     
      //Identifiers
View Full Code Here

    } while (!(current = current.getNextConsCell()).isNull());
  }
 
  private ArrayList<BigDec> stepSolveDomain() throws ParserException {
    ArrayList<BigDec> solutions = new ArrayList<BigDec>();
    BigDec accuracy = this.accuracy;
    if ((answer.abs().gteq(new BigDec(10000000)) || answer.abs().lteq(new BigDec(0.00000001))) && answer.neq(BigDec.ZERO))
      accuracy = answer.multiply(accuracy).abs();
    int i = 0;
    if (containsFactorial) {
      delta = BigDec.ONE;
      xMin = BigDec.ZERO;
      xMax = new BigDec(50);
      i = 6;
    }
    while ((solutions.size() == 0 && i < 4) || i == 6) {
      int maxIterations = (int) Math.round(xMax.subtract(xMin).divide(delta).doubleValue());
      for (int sign = 1; sign >= -1; sign -= 2) {
        int iterationsSinceDirectionChange = 0;
        delta = delta.multiply(new BigDec(sign));
        BigDec slope = BigDec.ZERO, prevSlope = BigDec.ZERO;
        BigDec point1[] = {xMin, getY(xMin)}, point2[] = {xMin.add(delta), getY(xMin.add(delta))};
        slope = getSlope(point1, point2);
        int prevDirection = getDirection(prevSlope, point1[1], sign), direction = getDirection(slope, point2[1], sign);
        //Check if either of the starting points is a solution
        if (point1[1].lteq(answer.add(accuracy)) && point1[1].gteq(answer.subtract(accuracy)))
          solutions.add(new BigDec(point1[0]));
        if (point2[1].lteq(answer.add(accuracy)) && point2[1].gteq(answer.subtract(accuracy)))
          solutions.add(new BigDec(point2[0]));
        if (solutions.size() > 0) //If the equation has solutions from the starting points, stop
          return solutions;
       
        while (iterationsSinceDirectionChange < maxIterations) {
          if (direction != prevDirection && (prevDirection != 0 || (point1[0].eq(xMin) && !solutions.contains(xMin))) && (direction != 0 || (point2[0].eq(xMax) && !solutions.contains(xMax)))) {
            BigDec possibleSolution = oscillator(point1, point2, slope, prevDirection, direction, sign, delta, accuracy);
            if (possibleSolution != null) {
              System.out.println("possibleSolution: (" + possibleSolution + ", " + getY(possibleSolution) + ")");
              if (isAnswer(possibleSolution, accuracy))
                solutions.add(possibleSolution);
            }
           
          }
          else if (direction == 2) //Only need to check direction because all prevDirections except for the first one (which is checked elsewhere) were first stored in direction
            solutions.add(point2[0]);
          //Iteration
          point1[0] = point2[0];
          point1[1] = point2[1];
          point2[0] = point2[0].add(delta);
          point2[1] = getY(point2[0]);
          prevSlope = new BigDec(slope);
          slope = getSlope(point1, point2);
          prevDirection = direction;
          direction = getDirection(slope, point2[1], sign);
          iterationsSinceDirectionChange++;
          if (direction != prevDirection)
            iterationsSinceDirectionChange = 0;
          if ((containsFactorial && point2[0].multiply(new BigDec(sign)).gteq(xMax.multiply(new BigDec(sign)).add(xMin))) || solutions.size() >= 10 || point2[0].gt(domainMax.abs().multiply(new BigDec(3))))
            break;
        }
      }
     
      i++;
      if (i < 4)
        delta = delta.divide(new BigDec(2.0));
    }
   
    return solutions;
  }
View Full Code Here

   *
   * @return a possible answer
   * @throws ParserException
   */
  private BigDec oscillator(BigDec point1[], BigDec point2[], BigDec slope, int prevDirection, int direction, int sign, BigDec delta, BigDec accuracy) throws ParserException {
    BigDec solution = null;
    int numSwitches = 0, maxSteps = 20, steps = 0;
    while (numSwitches <= 20 && steps < maxSteps) {
      if (direction == 0 && prevDirection == 0) //If it is a straight line
        break;
      if (direction != prevDirection) {
        delta = delta.multiply(new BigDec(-0.25));
        numSwitches++;
        steps = 0;
        if (isAnswer(point1[0], accuracy))
          solution = point1[0];
        if (isAnswer(point2[0], accuracy))
View Full Code Here

   * @return -1 if it is sloping away from the answer, 0 if it is neutral but not on the answer (slope == 0), 1 if it is
   *         sloping towards the answer, 2 if it is at the answer
   * @throws UndefinedResultException
   */
  private int getDirection(BigDec slope, BigDec y, int sign) throws UndefinedResultException {
    slope = slope.multiply(new BigDec(sign));
    if (y.eq(answer))
      return 2;
    if (slope.eq(BigDec.ZERO))
      return 0;
    if (slope.lt(BigDec.ZERO))
View Full Code Here

    return (BigDec) result.getCar();
  }
 
  private boolean isAnswer(BigDec point, BigDec accuracy) throws ParserException {
    try { //Try-Catch here because an UndefinedResultException means false, not error.
      BigDec ans = getY(point);
      //The slightly awkward comparison set is used here because it is faster than creating a new object for the absolute value method
      return ans.lteq(answer.add(accuracy)) && ans.gteq(answer.subtract(accuracy));
    }
    catch (UndefinedResultException e) {
      return false;
    }
  }
View Full Code Here

 
  @Override
  public ArrayList<BigDec> compute() {
    ArrayList<BigDec> answers = new ArrayList<BigDec>();
    if (equation.length() == 1) {
      answers.add(new BigDec(answer));
      return answers;
    }
    try {
      if (xMax.subtract(xMin).lteq(range)) {
        try {
          answers.addAll(stepSolveDomain());
        }
        catch (ParserException pe) {
          error = pe;
        }
      }
      else {
        StepSolveThread left = new StepSolveThread(equation, delta, xMin, xMin.add(xMax.subtract(xMin).divide(new BigDec(2))));
        StepSolveThread right = new StepSolveThread(equation, delta, xMin.add(xMax.subtract(xMin).divide(new BigDec(2))), xMax);
        left.fork();
        answers.addAll(right.compute());
        if (right.error != null) {
          error = right.error;
          return answers;
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.types.BigDec

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.