Package lipstone.joshua.parser.exceptions

Examples of lipstone.joshua.parser.exceptions.UndefinedResultException


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


  public BigDec log(ConsCell num) throws ParserException {
    num = parser.run(num);
    ArrayList<ConsCell> sections = num.splitOnSeparator();
    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

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

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

    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);
      min = (BigDec) parts.get(1).getCar();
      max = (BigDec) parts.get(2).getCar();
      if (parts.size() >= 4)
        delta = (BigDec) parts.get(3).getCar();
      output = sum(parts.get(0), min, max, delta);
View Full Code Here

  @Override
  public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
    if (operation.equals("abs")) {
      ConsCell temp = parser.run(input);
      if (temp.getCarType() != ConsType.NUMBER || temp.length() != 1)
        throw new UndefinedResultException("The single argument to the absolute value operation must evaluate to a number.", this);
      return new ConsCell(new BigDec(((BigDec) temp.getCar()).abs()), ConsType.NUMBER);
    }
    return null;
  }
View Full Code Here

  }
 
  private String postProcess(ConsCell output) throws ParserException {
    String result = output.toString();
    if (result.contains("NaN"))
      throw new UndefinedResultException("The result is not a number.", null);
    // Handles the matrix replacement for display purposes
    output = reverseFinalOperationSubstitutions(output);
    ConsCell current = output;
    do {
      if (current.getCarType() == ConsType.OBJECT && ((String) current.getCar()).startsWith("{M")) {
View Full Code Here

                  if ((Character) current.getNextConsCell(2).getCar() == '-')
                    negate = !negate;
                  current.getNextConsCell(2).remove();
                }
                if (current.getNextConsCell(2).getCarType() != ConsType.NUMBER)
                  throw new UndefinedResultException("For stacked exponents, each subsequent exponent must evaluate to a number", null);
                num2 = num2.pow(negate ? ((BigDec) current.getNextConsCell(2).getCar()).multiply(BigDec.MINUSONE) : (BigDec) current.getNextConsCell(2).getCar());
                current.getNextConsCell().remove();
                current.getNextConsCell().remove();
              }
              output = num1.pow(num2);
            }
            else if (operator == '*')
              output = num1.multiply(num2);
            else if (operator == '/')
              output = num1.divide(num2);
            else if (operator == '+')
              output = num1.add(num2);
            else if (operator == '-')
              output = num1.subtract(num2);
            else
              throw new UndefinedResultException(lastPlugin);
            current.replaceCar(new ConsCell(output, ConsType.NUMBER));
            forward = false;
          }
          else if ((current.getCarType() == ConsType.STRING || second.getCarType() == ConsType.STRING) &&
              !(current.getCarType() == ConsType.NUMBER || second.getCarType() == ConsType.NUMBER)) {
            if (operator != '+')
              throw new UndefinedResultException(lastPlugin);
            current.replaceCar(new ConsCell(current.carToString() + second.carToString(), ConsType.STRING));
            second.remove();
            current.getNextConsCell().remove();
            forward = false;
          }
          else if (current.getCarType() == ConsType.NUMBER && second.getCarType() == ConsType.STRING) {
            if (operator == '+')
              current.replaceCar(new ConsCell(current.carToString() + second.carToString(), ConsType.STRING));
            else if (operator == '*')
              current.replaceCar(new ConsCell(stringMultiplier(second.carToString(), ((BigDec) current.getCar()).intValue()), ConsType.STRING));
            else
              throw new UndefinedResultException(lastPlugin);
            second.remove();
            current.getNextConsCell().remove();
            forward = false;
          }
          else if (current.getCarType() == ConsType.STRING && second.getCarType() == ConsType.NUMBER) {
            if (operator == '+')
              current.replaceCar(new ConsCell(current.carToString() + second.carToString(), ConsType.STRING));
            else if (operator == '*')
              current.replaceCar(new ConsCell(stringMultiplier(current.carToString(), ((BigDec) second.getCar()).intValue()), ConsType.STRING));
            else
              throw new UndefinedResultException(lastPlugin);
            second.remove();
            current.getNextConsCell().remove();
            forward = false;
          }
          else if (current.getCarType() == ConsType.OBJECT && second.getCarType() == ConsType.OBJECT) {
View Full Code Here

   
    // Replace keywords with their respective values
    input = parser.replaceKeywords(input);
    parts = parts.get(0).splitOnIdentifier("=");
    if (parts.size() > 2)
      throw new UndefinedResultException("Incorrectly formatted equation.  Please use only 2-sided equalities.", null);
    if (parts.size() > 1) {
      if (parser.getVars().size() == 0 || !parser.containsVariables(input, parser.getVars())) {
        if (new BigDec(parser.run(parts.get(0)).carToString()).eq(new BigDec(parser.run(parts.get(1)).carToString())))
          return new ConsCell("true", ConsType.IDENTIFIER);
        else
View Full Code Here

   * @throws UndefinedResultException
   *             if this number is complex, meaning that this is thrown on imaginary part != 0
   */
  public int intValue() throws UndefinedResultException {
    if (internal.imag().compareTo(Apfloat.ZERO) != 0)
      throw new UndefinedResultException("Cannot get the int value of a complex number.", null);
    return internal.intValue();
  }
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.exceptions.UndefinedResultException

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.