Package lipstone.joshua.parser.types

Examples of lipstone.joshua.parser.types.BigDec


public class TrigonometryPlugin extends ParserPlugin implements OperationPlugin {
  String[] angleInput = {"sin", "cos", "tan", "sec", "csc", "cot"}, angleOutput = {"arcsin", "arccos", "arctan", "arcsec", "arccsc", "arccot"};
 
  @Override
  public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
    BigDec output = null;
    ConsCell value = parser.run(input);
    if (value.length() != 1 || value.getCarType() != ConsType.NUMBER)
      throw new UndefinedResultException("The " + operation + " operation takes one decimal number for its arguments.", this);
    BigDec inp = (BigDec) value.getCar();
   
    //Special points (e.g. angles at which sin, cos, etc. have exact answers
    if (operation.equals("cos") && ((parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(180.0)).eq(new BigDec(90.0)))
        || (parser.getAngleType().equals("Radians") && inp.mod(new BigDec(Math.PI)).eq(new BigDec(Math.PI / 2))) || (parser.getAngleType().equals("Grads") && inp.mod(new BigDec(200.0)).eq(new BigDec(100.0))))) {
      return new ConsCell(BigDec.ZERO, ConsType.NUMBER);
    }
    if ((operation.equals("sin") || operation.equals("tan")) && ((parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(180.0)).eq(new BigDec(0.0)))
        || (parser.getAngleType().equals("Radians") && inp.mod(new BigDec(Math.PI)).eq(BigDec.ZERO)) || (parser.getAngleType().equals("Grads") && inp.mod(new BigDec(200.0)).eq(BigDec.ZERO)))) {
      return new ConsCell(BigDec.ZERO, ConsType.NUMBER);
    }
    if (operation.equals("sin") && (parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(360.0)).eq(new BigDec(30.0)) ||
        parser.getAngleType().equals("Radians") && inp.eq(new BigDec(Math.PI / 6)) || parser.getAngleType().equals("Grads") && inp.mod(new BigDec(400.0)).eq(new BigDec(33.333333)))) {
      return new ConsCell(new BigDec(0.5), ConsType.NUMBER);
    }
    if (operation.equals("cos") && (parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(360.0)).eq(new BigDec(60.0)) ||
        parser.getAngleType().equals("Radians") && inp.eq(new BigDec(Math.PI / 3)) || parser.getAngleType().equals("Grads") && inp.mod(new BigDec(400.0)).eq(new BigDec(66.666667)))) {
      return new ConsCell(new BigDec(0.5), ConsType.NUMBER);
    }
    if (operation.equals("sin") && (parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(360.0)).eq(new BigDec(210.0)) ||
        parser.getAngleType().equals("Radians") && inp.eq(new BigDec(2 * Math.PI / 3)) ||
        parser.getAngleType().equals("Grads") && inp.mod(new BigDec(400.0)).eq(new BigDec(233.333333)))) {
      return new ConsCell(new BigDec(-0.5), ConsType.NUMBER);
    }
    if (operation.equals("cos") && (parser.getAngleType().equals("Degrees") && inp.mod(new BigDec(360.0)).eq(new BigDec(240.0)) ||
        parser.getAngleType().equals("Radians") && inp.eq(new BigDec(4 * Math.PI / 3)) ||
        parser.getAngleType().equals("Grads") && inp.mod(new BigDec(400.0)).eq(new BigDec(266.666667)))) {
      return new ConsCell(new BigDec(-0.5), ConsType.NUMBER);
    }
    //End special points
   
    for (String str : angleInput)
      if (operation.equals(str)) {
        inp = parser.getAngleType().toRadians(inp);
        break;
      }
   
    if (((operation.equals("arcsin") || operation.equals("arccos")) && !(inp.gteq(new BigDec(-1.0)) && inp.lteq(new BigDec(1.0))))
        || ((operation.equals("arcsec") || operation.equals("arccsc")) && (inp.gteq(new BigDec(-1.0)) && inp.lteq(new BigDec(1.0))))
        || (operation.equals("arccosh") && (inp.lt(BigDec.ONE))) || (operation.equals("arctanh") && (inp.abs().gteq(BigDec.ONE)))
        || (operation.equals("arccoth") && (inp.abs().lteq(BigDec.ONE)))
        || (operation.equals("arcsech") && !(inp.gt(BigDec.ZERO) && inp.lteq(BigDec.ONE)))
        || (operation.equals("arccsch") && (inp.eq(BigDec.ZERO)))) {
      return new ConsCell(BigDec.ZERO, ConsType.NUMBER);
    }
   
    while ((operation.equals("sec") || operation.equals("csc") || operation.equals("cot")) && inp.lt(BigDec.ZERO))
      inp = inp.add(new BigDec(Math.PI * 2));
    while ((operation.equals("sec") || operation.equals("csc") || operation.equals("cot")) && inp.gt(new BigDec(Math.PI * 2)))
      inp = inp.subtract(new BigDec(Math.PI * 2));
   
    if (operation.equals("sin"))
      output = new BigDec(ApcomplexMath.sin(inp.getInternal()));
    if (operation.equals("cos"))
      output = new BigDec(ApcomplexMath.cos(inp.getInternal()));
    if (operation.equals("tan"))
      output = new BigDec(ApcomplexMath.tan(inp.getInternal()));
    if (operation.equals("sec"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.cos(inp.getInternal())));
    if (operation.equals("csc"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.sin(inp.getInternal())));
    if (operation.equals("cot"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.tan(inp.getInternal())));
    if (operation.equals("arcsin"))
      output = inp.eq(BigDec.ZERO) ? BigDec.ZERO : new BigDec(ApcomplexMath.asin(inp.getInternal()));
    if (operation.equals("arccos"))
      output = inp.eq(BigDec.ZERO) ? BigDec.ONE : new BigDec(ApcomplexMath.acos(inp.getInternal()));
    if (operation.equals("arctan"))
      output = inp.eq(BigDec.ZERO) ? BigDec.ZERO : new BigDec(ApcomplexMath.atan(inp.getInternal()));
    if (operation.equals("arcsec"))
      output = BigDec.ONE.divide(inp.eq(BigDec.ZERO) ? BigDec.ONE : new BigDec(ApcomplexMath.acos(inp.getInternal())));
    if (operation.equals("arccsc"))
      output = BigDec.ONE.divide(inp.eq(BigDec.ZERO) ? BigDec.ZERO : new BigDec(ApcomplexMath.asin(inp.getInternal())));
    if (operation.equals("arccot"))
      output = BigDec.ONE.divide(inp.eq(BigDec.ZERO) ? BigDec.ZERO : new BigDec(ApcomplexMath.atan(inp.getInternal())));
    if (operation.equals("sinh"))
      output = new BigDec(ApcomplexMath.sinh(inp.getInternal()));
    if (operation.equals("cosh"))
      output = new BigDec(ApcomplexMath.cosh(inp.getInternal()));
    if (operation.equals("tanh"))
      output = new BigDec(ApcomplexMath.tanh(inp.getInternal()));
    if (operation.equals("sech"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.cosh(inp.getInternal())));
    if (operation.equals("csch"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.sinh(inp.getInternal())));
    if (operation.equals("coth"))
      output = BigDec.ONE.divide(new BigDec(ApcomplexMath.tanh(inp.getInternal())));
    if (operation.equals("arcsinh"))
      return parser.run(new String("ln(x+(x^2+1)^(1/2))").replaceAll("x", "(" + inp.toString() + ")"));
    if (operation.equals("arccosh"))
      return parser.run(new String("ln(x+(x^2-1)^(1/2))").replaceAll("x", "(" + inp.toString() + ")"));
    if (operation.equals("arctanh"))
      return parser.run(new String("1/2*ln((1+x)/(1-x))").replaceAll("x", "(" + inp.toString() + ")"));
    if (operation.equals("arcsech"))
      return parser.run(new String("ln((1+(1-x^2)^(1/2))/x)").replaceAll("x", "(" + inp.toString() + ")"));
    if (operation.equals("arccsch"))
      return parser.run(new String("ln(1/x+((1+x^2)^(1/2))/(abs(x)))").replaceAll("x", "(" + inp.toString() + ")"));
    if (operation.equals("arccoth"))
      return parser.run(new String("1/2*ln((x+1)/(x-1))").replaceAll("x", "(" + inp.toString() + ")"));
   
    if (output == null//If the operation was not in this plugin
      return null;
   
    for (String str : angleOutput)
View Full Code Here


  public BigDec round(ConsCell input) throws ParserException {
    ConsCell num = parser.run(input);
    if (num.length() != 1 || num.getCarType() != ConsType.NUMBER)
      throw new UndefinedResultException("The argument to the round function must evaluate to a number", this);
    Apfloat real = ((BigDec) num.getCar()).getInternal().real(), complex = ((BigDec) num.getCar()).getInternal().imag(), round = new Apfloat(0.5);
    return new BigDec(real.frac().compareTo(round) == -1 ? real.floor() : real.ceil(), complex.frac().compareTo(round) == -1 ? complex.floor() : complex.ceil());
  }
View Full Code Here

    Apfloat real = ((BigDec) num.getCar()).getInternal().real(), complex = ((BigDec) num.getCar()).getInternal().imag(), round = new Apfloat(0.5);
    return new BigDec(real.frac().compareTo(round) == -1 ? real.floor() : real.ceil(), complex.frac().compareTo(round) == -1 ? complex.floor() : complex.ceil());
  }
 
  public BigDec factorial(ConsCell input) throws ParserException {
    BigDec numFactorial = BigDec.ONE;
    ArrayList<ConsCell> parts = input.splitOnSeparator();
    if (parts.size() >= 2)
      numFactorial = round(parts.get(1));
    BigDec output = BigDec.ONE;
    ConsCell num = parser.run(parts.get(0));
    if (num.getCarType() != ConsType.NUMBER || !((BigDec) num.getCar()).isInt() || !((BigDec) num.getCar()).gteq(BigDec.ZERO))
      throw new ParserException("The factorial operation is only defined for arguments that evaluate to whole numbers", this);
    BigDec cap = (BigDec) num.getCar();
    if (cap.gteq(numFactorial))
      for (BigDec i = new BigDec(cap); i.gteq(BigDec.ONE); i = i.subtract(numFactorial))
        output = output.multiply(i);
    return output;
  }
View Full Code Here

    this.caller = caller;
    varCells = equation.allInstancesOf(new ConsCell(var, ConsType.IDENTIFIER));
  }
 
  private BigDec trapezoidal(BigDec last, int N) throws ParserException {
    BigDec answer = BigDec.ZERO, deltaX = max.subtract(min).divide(new BigDec(N)).abs(), sigma = BigDec.ZERO;
    for (int n = 0; n < N; n++) {
      //In basic types:  sigma = sigma + Double.parseDouble(parser.run(equation.replaceAll(var, new Double(a + n*deltaX).toString()), false)) +
      //Double.parseDouble(parser.run(equation.replaceAll(var, new Double(a + (n+1)*deltaX).toString()), false));
      BigDec x = min.add(deltaX.multiply(new BigDec(n)));
      for (ConsCell varCell : varCells)
        varCell.replaceCar(new ConsCell(x, ConsType.NUMBER));
      sigma = sigma.add((BigDec) parser.run(equation).getCar());
     
      x = min.add(deltaX.multiply((new BigDec(n).add(new BigDec(1)))));
      for (ConsCell varCell : varCells)
        varCell.replaceCar(new ConsCell(x, ConsType.NUMBER));
      sigma = sigma.add((BigDec) parser.run(equation).getCar());
    }
    //In basic types:  answer = (b-a)/(2*N)*sigma
    answer = (max.subtract(min).divide(new BigDec(N).multiply(new BigDec(2)))).multiply(sigma);
    if (answer.subtract(last).abs().doubleValue() < accuracy)
      return answer;
    else if (N >= maxN)
      throw new ParserException("The integration system ran out of time", caller);
    else
View Full Code Here

      return trapezoidal(answer, N * 2);
  }
 
  @Override
  protected BigDec compute() {
    BigDec answer = BigDec.ZERO;
    try {
      if (max.subtract(min).lteq(maxRange))
        answer = trapezoidal(BigDec.ZERO, N);
      else {
        NIThread left = new NIThread(equation, N / 2, min, min.add(max.subtract(min).divide(new BigDec(2))));
        NIThread right = new NIThread(equation, N / 2, min.add(max.subtract(min).divide(new BigDec(2))), max);
        left.fork();
        answer = right.compute();
        answer = answer.add(left.join());
        if (right.error != null) {
          error = right.error;
          if (error.getThrower() == null)
            error.setThrower(caller);
          return answer;
View Full Code Here

   * @param parser
   *            the parser that called this method
   * @return the sum of this equation from [min, max] with delta defaulting to 1, delta as passed otherwise
   */
  public BigDec sum(ConsCell input) throws ParserException {
    BigDec output = BigDec.ZERO, min = BigDec.ZERO, max = BigDec.ONE, delta = BigDec.ONE;
    input = parser.run(input);
    ArrayList<ConsCell> parts = input.splitOnSeparator();
    if (parts.size() >= 3) {
      if (parts.get(1).getCarType() != ConsType.NUMBER || parts.get(2).getCarType() != ConsType.NUMBER || (parts.size() >= 4 && parts.get(3).getCarType() != ConsType.NUMBER))
        throw new UndefinedResultException("The sum function takes three or four arguments, the second and third and fourth of which must evaluate to numbers.", this);
View Full Code Here

    }
    return output;
  }
 
  private BigDec sum(ConsCell equation, BigDec min, BigDec max, BigDec delta) throws ParserException {
    BigDec output = BigDec.ZERO;
    if (min.gt(max)) {
      BigDec temp = new BigDec(min);
      min = new BigDec(max);
      max = new BigDec(temp);
    }
    ArrayList<ConsCell> vars = equation.allInstancesOf(new ConsCell(parser.getVariables(equation).get(0), ConsType.IDENTIFIER));
    for (BigDec i = new BigDec(min); i.lteq(max); i = i.add(delta)) {
      for (ConsCell var : vars)
        var.replaceCar(new ConsCell(i, ConsType.NUMBER));
      output = output.add((BigDec) parser.run(equation).getCar());
    }
    return output;
View Full Code Here

    return output;
  }
 
  private BigDec gcd(ConsCell input) throws ParserException {
    ArrayList<ConsCell> ints = input.splitOnSeparator();
    BigDec gcd = new BigDec(parser.run(ints.get(0)).toString());
    for (int i = 1; i < ints.size(); i++)
      gcd = gcd.gcd(new BigDec(parser.run(ints.get(i)).toString()));
    return gcd;
  }
View Full Code Here

    return gcd;
  }
 
  private BigDec lcm(ConsCell input) throws ParserException {
    ArrayList<ConsCell> ints = input.splitOnSeparator();
    BigDec lcm = new BigDec(parser.run(ints.get(0)).toString());
    for (int i = 1; i < ints.size(); i++)
      lcm = lcm.lcm(new BigDec(parser.run(ints.get(i)).toString()));
    return lcm;
  }
View Full Code Here

    if ((sections.get(0).getCarType() != ConsType.NUMBER || sections.get(0).length() != 1) && (sections.size() == 1 || (sections.size() > 1 && (sections.get(1).getCarType() != ConsType.NUMBER ||
        sections.get(1).length() != 1))))
      throw new UndefinedResultException("The log function accepts either one or two arguments, both of which must evaluate to numbers.", this);
    if (((BigDec) num.getCar()).lteq(BigDec.ZERO))
      throw new UndefinedResultException("The log function is only defined for values greater than 0.", this);
    return new BigDec(Math.log(((BigDec) (parser.run(sections.get(0)).getCar())).doubleValue()) / Math.log((sections.size() > 1 ?
        (BigDec) parser.run(sections.get(1)).getCar() : new BigDec(parser.getDefaultLogBase())).doubleValue()));
  }
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.