Examples of ConsCell


Examples of lipstone.joshua.parser.util.ConsCell

  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)
      if (operation.equals(str)) {
        output = parser.getAngleType().fromRadians(output);
        break;
      }
    return new ConsCell(output, ConsType.NUMBER);
  }
View Full Code Here

Examples of lipstone.joshua.parser.util.ConsCell

    super();
    this.equation = equation.clone();
    this.N = N;
    this.min = min;
    this.max = max;
    varCells = equation.allInstancesOf(new ConsCell(var, ConsType.IDENTIFIER));
  }
View Full Code Here

Examples of lipstone.joshua.parser.util.ConsCell

    this.min = min;
    this.max = max;
    this.maxRange = maxRange;
    this.var = var;
    this.caller = caller;
    varCells = equation.allInstancesOf(new ConsCell(var, ConsType.IDENTIFIER));
  }
View Full Code Here

Examples of lipstone.joshua.parser.util.ConsCell

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

Examples of lipstone.joshua.parser.util.ConsCell

import org.apfloat.Apfloat;

public class Functions extends ParserPlugin implements OperationPlugin, InputFilterPlugin {
 
  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

Examples of lipstone.joshua.parser.util.ConsCell

    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

Examples of lipstone.joshua.parser.util.ConsCell

    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

Examples of lipstone.joshua.parser.util.ConsCell

 
  @Override
  public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
    int numParams = input.splitOnSeparator().size();
    if (operation.equals("round"))
      return new ConsCell(round(input), ConsType.NUMBER);
    if (operation.equals("ceil"))
      return new ConsCell(new BigDec(Math.ceil(new Double(parser.run(input).toString()))), ConsType.NUMBER);
    if (operation.equals("floor"))
      return new ConsCell(new BigDec(Math.floor(new Double(parser.run(input).toString()))), ConsType.NUMBER);
    if (operation.equals("factorial"))
      return new ConsCell(factorial(input), ConsType.NUMBER);
    if (operation.equals("log"))
      return new ConsCell(log(input), ConsType.NUMBER);
    if (operation.equals("ln"))
      return new ConsCell(log(input.append(new ConsCell(",", ConsType.SEPARATOR, new ConsCell("e", ConsType.IDENTIFIER), ConsType.CONS_CELL)).getFirstConsCell()), ConsType.NUMBER);
    if ((operation.equals("sum") || operation.equals("sigma")) && (numParams == 3 || numParams == 4))
      return new ConsCell(sum(input), ConsType.NUMBER);
    if (operation.equals("phi"))
      return new ConsCell(phi(input), ConsType.NUMBER);
    if (operation.equals("isRelativelyPrime") || operation.equals("relativelyPrime") && numParams == 2)
      return new ConsCell(new Boolean(isRelativelyPrime(input)).toString(), ConsType.IDENTIFIER);
    if (operation.equals("sigfigs"))
      return new ConsCell(sigfigs(input), ConsType.NUMBER);
    if (operation.equals("gcd"))
      return new ConsCell(gcd(input), ConsType.NUMBER);
    if (operation.equals("lcm"))
      return new ConsCell(lcm(input), ConsType.NUMBER);
    return null;
  }
View Full Code Here

Examples of lipstone.joshua.parser.util.ConsCell

  public void unloadOperations() {/* Nothing to do here */}
 
  @Override
  public ConsCell preProcess(ConsCell input) throws ParserException {
    Pattern factorial = Pattern.compile("[!]+");
    ConsCell current = input;
    if (current.getCdrType() == ConsType.CONS_CELL)
      current = current.getNextConsCell();
    do {
      if (current.getCarType() != ConsType.IDENTIFIER)
        continue;
      Matcher m = factorial.matcher((String) current.getCar());
      if (m.matches()) {
        //This line produces the following structure: factorial ([data before !'s] , [number of !'s])
        ConsCell replacement = new ConsCell("factorial", ConsType.IDENTIFIER, new ConsCell(new ConsCell(current.getPreviousConsCell().getCar(), current.getPreviousConsCell().getCarType(),
            new ConsCell(",", ConsType.SEPARATOR, new ConsCell(new BigDec(((String) current.getCar()).length()), ConsType.NUMBER))), ConsType.CONS_CELL));
        if (current.getPreviousConsCell() == input) {
          replacement.getLastConsCell().append(input.getNextConsCell(2));
          input = current = replacement;
        }
        else {
          current = current.getPreviousConsCell().remove();
          current.replaceCar(replacement);
View Full Code Here

Examples of lipstone.joshua.parser.util.ConsCell

   *            the parser that precipitated this method call
   * @return the equation with the typed out numbers replaced with the appropriate numerals
   * @throws UndefinedResultException
   */
  public ConsCell convertNumbers(ConsCell input) throws NumberFormatException, UndefinedResultException {
    ConsCell current = input;
    int numeric = -2;
    do {
      if (current.getCarType() == ConsType.IDENTIFIER && (numeric = numberValue((String) current.getCar())) >= -1) {
        ConsCell head = current;
        int number = 0, working = 0, decimal = 0, step = 0;
        boolean dec = false, first = current.isFirstConsCell();
        do {
          if (numeric > 100) {
            number += working * numeric;
            working = 0;
            step = 0;
          }
          else if (numeric == 100) {
            working *= numeric;
            step = 0;
          }
          else if (numeric >= 10) {
            if (step > 0) {
              working *= 100;
              number *= 100;
            }
            working += numeric;
            step = 2;
          }
          else if (numeric >= 0) {
            if (step == 1) {
              working *= 10;
              number *= 10;
            }
            working += numeric;
            step = 1;
          }
          else
            break;
        } while ((current = current.getNextConsCell()).getCarType() == ConsType.IDENTIFIER && (numeric = numberValue((String) current.getCar())) > -1);
        if (numeric == -1)
          dec = true;
        number += working;
        working = 0;
        step = 0;
        if (dec) {
          while ((current = current.getNextConsCell()).getCarType() == ConsType.IDENTIFIER && (numeric = numberValue((String) current.getCar())) > -1) {
            if (numeric > 100) {
              decimal += working * numeric;
              working = 0;
              step = 0;
            }
            else if (numeric == 100) {
              working *= numeric;
              step = 0;
            }
            else if (numeric >= 10) {
              if (step > 0) {
                working *= 100;
                decimal *= 100;
              }
              working += numeric;
              step = 2;
            }
            else if (numeric >= 0) {
              if (step == 1) {
                working *= 10;
                decimal *= 10;
              }
              working += numeric;
              step = 1;
            }
            else
              break;
          }
          decimal += working;
        }
        if (first)
          input = new ConsCell(new BigDec(number + (dec ? ((double) decimal) / Math.pow(10, ((int) Math.log10(decimal) + 1)) : 0)), ConsType.NUMBER, current);
        else {
          head = head.remove().getPreviousConsCell();
          head.append(new ConsCell(new BigDec(number + (dec ? ((double) decimal) / Math.pow(10, ((int) Math.log10(decimal) + 1)) : 0)), ConsType.NUMBER, current));
        }
        if (current.isNull())
          return input;
      }
    } while (!(current = current.getNextConsCell()).isNull());
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.