Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


  }
 
  private ConsCell derivative(ConsCell input) throws ParserException {
    BigDec slope = BigDec.ZERO, slopePos = BigDec.ZERO, slopeNeg = BigDec.ZERO, x = BigDec.ZERO;
    ArrayList<ConsCell> parts = input.splitOnSeparator();
    ConsCell temp = parser.run(parts.get(1));
    if (temp.getCarType() != ConsType.NUMBER) {
      return symbolicDerivative(input);
    }
    x = (BigDec) temp.getCar();
    slopePos = pointDerivative(parts.get(0).clone(), x, 1.0);
    slopeNeg = pointDerivative(parts.get(0), x, -1.0);
    slope = slopePos.add(slopeNeg).divide(new BigDec(2));
    return new ConsCell(slope, ConsType.NUMBER);
  }
View Full Code Here


    return new ConsCell(slope, ConsType.NUMBER);
  }
 
  private BigDec pointDerivative(ConsCell equation, BigDec x, double direction) throws ParserException {
    var = parser.getVariables(equation).get(0);
    ArrayList<ConsCell> varCells = equation.allInstancesOf(new ConsCell(var, ConsType.IDENTIFIER));
    for (ConsCell varCell : varCells)
      varCell.replaceCar(new ConsCell(x, ConsType.NUMBER));
    ConsCell temp = parser.run(equation);
    if (temp.getCarType() != ConsType.NUMBER)
      throw new UndefinedResultException("The second argument to the derivative operation must cause the first to evaluate to a number.", this);
    BigDec slope = BigDec.ZERO, prevSlope = BigDec.ONE, deltaX = new BigDec(0.0001), y1 = (BigDec) temp.getCar();
   
    BigDec x2 = x.add(deltaX.multiply(new BigDec(direction)));
    for (ConsCell varCell : varCells)
      varCell.replaceCar(new ConsCell(x2, ConsType.NUMBER));
    temp = parser.run(equation);
    if (temp.getCarType() != ConsType.NUMBER)
      throw new UndefinedResultException("The second argument to the derivative operation must cause the first to evaluate to a number.", this);
    //In basic types:  slope = (y1 - Double.parseDouble(parser.run(equation.replaceAll(var, new Double(x+deltaX*direction).toString())))/(x-(x+deltaX*direction)));
    slope = y1.subtract((BigDec) temp.getCar()).divide(deltaX.multiply(new BigDec(direction)));
   
    while (slope.subtract(prevSlope).abs().doubleValue() > accuracy) {
      prevSlope = slope;
      deltaX = deltaX.divide(new BigDec(10.0));
      x2 = x.add(deltaX.multiply(new BigDec(direction)));
      for (ConsCell varCell : varCells)
        varCell.replaceCar(new ConsCell(x2, ConsType.NUMBER));
      temp = parser.run(equation);
      if (temp.getCarType() != ConsType.NUMBER)
        throw new UndefinedResultException("The second argument to the derivative operation must cause the first to evaluate to a number.", this);
      //In basic types:  slope = (y1 - Double.parseDouble(parser.run(equation.replaceAll(var, new Double(x+deltaX*direction).toString())))/(x-(x+deltaX*direction)));
      slope = y1.subtract((BigDec) temp.getCar()).divide(deltaX.multiply(new BigDec(direction)));
    }
    if ((slope.abs().doubleValue() < accuracy) && equation.length() < 4)
      slope = BigDec.ZERO;
    var = "[xyz]";
    return slope.multiply(BigDec.MINUSONE);
View Full Code Here

    parser.setVars(vars);
    ArrayList<ConsCell> equation = parser.getCAS().getTerms(section);
    equation = parser.getCAS().foldSigns(equation);
   
    for (int i = 0; i < equation.size(); i++) {
      ConsCell term = equation.get(i);
      term = stopCases(term, vars);
      boolean stopped = ((String) term.getLastConsCell().getCar()).equals("{true}"); //Guaranteed by stopCases that the last ConsCell in term will be an OBJECT cell,
      term.getLastConsCell().remove();
      if (parser.containsVariables(term, vars) && !stopped)
        term = rules(term, vars);
      equation.set(i, term);
    }
    section = new ConsCell();
    for (ConsCell term : equation)
      if (term.getCarType() != ConsType.OPERATOR)
        section = section.append(new ConsCell('+', ConsType.OPERATOR, term, ConsType.CONS_CELL));
      else
        section = section.append(term);
    section = parser.getCAS().simplifyTerms(parser.removeDoubles(section.getFirstConsCell()));
    if (section.getCarType() == ConsType.OPERATOR && ((Character) section.getCar()) == '+')
      section = section.remove();
    if (section == null)
      return new ConsCell();
    if (section.getLastConsCell().getCarType() == ConsType.OPERATOR && ((Character) section.getLastConsCell().getCar()) == '+')
      section.getLastConsCell().remove();
    return section;
  }
View Full Code Here

  }
 
  private ConsCell stopCases(ConsCell term, ArrayList<String> vars) throws ParserException {
    BigDec coefficient = parser.getCAS().getCoefficient(term, vars);
    if (!parser.containsVariables(term, vars) || coefficient.eq(BigDec.ZERO))
      return new ConsCell(BigDec.ZERO, ConsType.NUMBER, new ConsCell("{true}", ConsType.OBJECT), ConsType.CONS_CELL);
    PairedList<ConsCell, ConsCell> orders = parser.getCAS().orderOfTerm(term, vars);
    int remaining = orders.size();
    for (String str : vars) { //Basically, if a variable's power is 1, taking its derivative gets rid of it.
      if (!orders.containsKey(new ConsCell(str, ConsType.IDENTIFIER)))
        continue;
      ConsCell result = parser.run(orders.get(new ConsCell(str, ConsType.IDENTIFIER)));
      if (orders.containsKey(new ConsCell(str, ConsType.IDENTIFIER)) && result.getCarType() == ConsType.NUMBER && result.length() == 1 && ((BigDec) result.getCar()).eq(BigDec.ONE))
        remaining--;
    }
    if (remaining == 0)
      return new ConsCell(coefficient, ConsType.NUMBER, new ConsCell("{true}", ConsType.OBJECT), ConsType.CONS_CELL);
    if (orders.size() == 1)
      return chainFunctions(parser.getCAS().makeTerm(coefficient, orders, false), vars);
    return term.getLastConsCell().append(new ConsCell("{false}", ConsType.OBJECT)).getFirstConsCell();
  }
View Full Code Here

      if (check.equalsIgnoreCase(functionDerivatives[i])) {
        type = i;
        break;
      }
    BigDec coefficient = parser.getCAS().getCoefficient(term, vars);
    ConsCell result = coefficient.neq(BigDec.ONE) ? new ConsCell(coefficient, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR), ConsType.CONS_CELL).getLastConsCell() : new ConsCell();
    if (type > -1 && term.getNextConsCell(offset).length() == 1) {
      ConsCell inner = term.getNextConsCell(offset).getCarType() == ConsType.CONS_CELL ? (ConsCell) term.getNextConsCell(offset).getCar() : term.getNextConsCell(offset);
      ConsCell functionOutput = Tokenizer.tokenizeString(functionDerivatives[type + 1]);
      ArrayList<ConsCell> cells = functionOutput.allInstancesOf(new ConsCell("n", ConsType.IDENTIFIER));
      for (ConsCell cell : cells)
        cell.replaceCar(inner.clone()); //Switch out any instances of n with the inner part of the chain function.
      result = result.append(functionOutput);
      ConsCell chain = symbolicDerivative(inner, vars);
      if (chain.length() == 1 && chain.getCarType() == ConsType.NUMBER && ((BigDec) chain.getCar()).eq(BigDec.ONE))
        return result.append(new ConsCell("{true}", ConsType.OBJECT)).getFirstConsCell();
      return result.append(new ConsCell('*', ConsType.OPERATOR, chain, ConsType.CONS_CELL)).append(new ConsCell("{true}", ConsType.OBJECT)).getFirstConsCell();
    }
    term.getLastConsCell().append(new ConsCell("{false}", ConsType.OBJECT));
    return term;
  }
View Full Code Here

    return term;
  }
 
  private ConsCell rules(ConsCell term, ArrayList<String> vars) throws ParserException {
    for (int priority = 0; priority < 2; priority++) {
      ConsCell current = term;
      while (!(current = current.getNextConsCell()).isNull() && !current.getNextConsCell().isNull()) {
        if (current.getCarType() != ConsType.OPERATOR || !parser.containsVariables(current.getPreviousConsCell(), vars) && !parser.containsVariables(current.getNextConsCell(), vars))
          continue;
        if ((Character) current.getCar() == '*' && priority == 0)
          return multiplicationRule(current, vars);
        else if ((Character) current.getCar() == '/' && priority == 0)
          return divisionRule(current, vars);
        else if ((Character) current.getCar() == '^' && priority == 1) {
          current.replaceCar(powerRule(current, vars));
          term = current.getFirstConsCell();
        }
      }
    }
    return term;
  }
View Full Code Here

    }
    return term;
  }
 
  private ConsCell multiplicationRule(ConsCell current, ArrayList<String> vars) throws ParserException {
    ConsCell head = current.getPreviousConsCell(), left = head.singular(), right = current.getNextConsCell().clone();
    while (!(head = head.getPreviousConsCell()).isNull() && !(head.getCarType() == ConsType.OPERATOR && ((Character) head.getCar() == '*' || (Character) head.getCar() == '/')))
      left = head.singular().append(left.getFirstConsCell());
    left = left.getFirstConsCell();
    for (int i = 0; i < left.length(); i++)
      current.getPreviousConsCell().remove();
   
    ConsCell derivative = new ConsCell();
    if (parser.containsVariables(left, vars))
      derivative = new ConsCell(symbolicDerivative(left, vars), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, new ConsCell(right.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL).getLastConsCell();
    if (parser.containsVariables(right, vars))
      derivative = derivative.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(left.clone(), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, new ConsCell(symbolicDerivative(right, vars),
          ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
    current.getNextConsCell().removeAll();
    current = current.replace(derivative.getFirstConsCell()).getFirstConsCell();
    return current;
  }
View Full Code Here

    current = current.replace(derivative.getFirstConsCell()).getFirstConsCell();
    return current;
  }
 
  private ConsCell divisionRule(ConsCell current, ArrayList<String> vars) throws ParserException {
    ConsCell head = current.getPreviousConsCell(), left = head.singular(), right = current.getNextConsCell().clone();
    while (!(head = head.getPreviousConsCell()).isNull() && !(head.getCarType() == ConsType.OPERATOR && ((Character) head.getCar() == '*' || (Character) head.getCar() == '/')))
      left = head.singular().append(left.getFirstConsCell());
    left = left.getFirstConsCell();
    for (int i = 0; i < left.length(); i++)
      current.getPreviousConsCell().remove();
   
    ConsCell derivative = new ConsCell();
    if (parser.containsVariables(left, vars))
      derivative = new ConsCell(symbolicDerivative(left, vars), ConsType.CONS_CELL, new ConsCell('*', ConsType.CONS_CELL, new ConsCell(right.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL);
    if (parser.containsVariables(right, vars))
      derivative = derivative.append(new ConsCell('-', ConsType.OPERATOR, new ConsCell(left.clone(), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, new ConsCell(symbolicDerivative(right, vars),
          ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
    derivative = new ConsCell(derivative.getFirstConsCell(), ConsType.CONS_CELL);
    derivative.append(new ConsCell('/', ConsType.OPERATOR, new ConsCell(right.clone(), ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(2),
        ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
    current.getNextConsCell().removeAll();
    current.replace(derivative);
    return current.getFirstConsCell();
  }
View Full Code Here

    current.replace(derivative);
    return current.getFirstConsCell();
  }
 
  private ConsCell powerRule(ConsCell current, ArrayList<String> vars) throws ParserException {
    ConsCell derivative = new ConsCell();
    ConsCell left = current.getPreviousConsCell().singular(), right = current.getNextConsCell().singular();
    current.getPreviousConsCell().remove();
    current.getNextConsCell().remove();
    ConsCell temp = current;
    while (!(temp = temp.getPreviousConsCell()).isNull() && temp.getCarType() != ConsType.OPERATOR) {
      left = temp.singular().append(left).getFirstConsCell();
      temp = temp.remove();
    }
    temp = current;
    while (!(temp = temp.getNextConsCell()).isNull() && temp.getCarType() != ConsType.OPERATOR) {
      right = right.append(temp.singular());
      temp = temp.remove();
    }
    right = right.getFirstConsCell();
    if (parser.containsVariables(right, vars)) {
      derivative = derivative.append(new ConsCell(symbolicDerivative(right, vars), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR), ConsType.CONS_CELL));
      derivative = derivative.append(new ConsCell(left.clone(), ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(right.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
      derivative = derivative.append(new ConsCell('*', ConsType.OPERATOR, new ConsCell("ln", ConsType.IDENTIFIER, new ConsCell(left.clone(), ConsType.CONS_CELL),
          ConsType.CONS_CELL), ConsType.CONS_CELL));
    }
    if (parser.containsVariables(left, vars)) {
      if (!derivative.isNull())
        derivative = derivative.append(new ConsCell('+', ConsType.OPERATOR));
      derivative = derivative.append(new ConsCell(right.clone(), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, left.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL));
      derivative = derivative.append(new ConsCell('^', ConsType.OPERATOR, new ConsCell(parser.run(right.clone().append(
          new ConsCell('-', ConsType.OPERATOR, new ConsCell(BigDec.ONE, ConsType.NUMBER), ConsType.CONS_CELL)).getFirstConsCell()), ConsType.CONS_CELL), ConsType.CONS_CELL));
      derivative = derivative.append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(symbolicDerivative(left, vars), ConsType.CONS_CELL), ConsType.CONS_CELL));
    }
    derivative = parser.getCAS().simplifyTerm(parser.getCAS().removeExcessParentheses(derivative.getFirstConsCell()));
    return derivative;
  }
View Full Code Here

    }
    else
      vars = parser.getVariables(input);
    parser.setVars(vars);
    parts.set(0, parser.preProcess(parts.get(0)));
    ConsCell output = symbolicDerivative(parts.get(0), vars);
    if (output.toString().equalsIgnoreCase(""))
      return new ConsCell(BigDec.ZERO, ConsType.NUMBER);
    return output;
  }
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.