Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


   *            the equation must have had the right half negated (so that it = 0) and had simplifyTerms called on it.
   * @return the fully factored equation if possible.
   * @throws ParserException
   */
  private ConsCell rationalRootsFactoring(ConsCell equation) throws ParserException {
    ConsCell original = equation.clone();
    if (orderOfEquation(equation, parser.getVars()).lt(new BigDec(2)))
      return equation;
    ArrayList<ConsCell> eqn = foldSigns(getTerms(equation));
    BigDec highestCoefficient = getCoefficient(eqn.get(0)), lowestCoefficient = getCoefficient(eqn.get(eqn.size() - 1)), lcm = BigDec.ONE;
    if (!highestCoefficient.isInt() || !lowestCoefficient.isInt()) {
      lcm = ((BigDec) highestCoefficient.toFraction().getCar(2)).lcm((BigDec) lowestCoefficient.toFraction().getCar(2));
      equation = eqn.get(0).getLastConsCell().append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER)));
      for (int i = 0; i < eqn.size(); i++)
        equation = equation.append(new ConsCell('+', ConsType.OPERATOR, eqn.get(i).getLastConsCell().append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER))).getFirstConsCell().clone()));
      equation = equation.getFirstConsCell();
      equation = simplifyTerms(parser.removeDoubles(equation));
    }
    ArrayList<BigDec> roots = getRoots(equation);
    for (BigDec root : roots) {
      ConsCell output = polynomialDivision(equation, new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER, new ConsCell('+', ConsType.OPERATOR, new ConsCell(root, ConsType.NUMBER))),
          new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER), true);
      ConsCell last = output.getLastConsCell();
      if (last.getCarType() == ConsType.OBJECT && last.getCar().equals("{false}")) { //If the polynomial division succeeded, return the factored form
        last.remove();
        ConsCell result = parser.removeDoubles(new ConsCell(new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER, new ConsCell('+', ConsType.OPERATOR, new ConsCell(root, ConsType.NUMBER))),
            ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, new ConsCell(rationalRootsFactoring(output), ConsType.CONS_CELL))));
        if (!highestCoefficient.isInt() && lcm.neq(BigDec.ONE))
          result = new ConsCell(BigDec.ONE, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, result))));
        return result;
      }
    }
    return original;
  }
View Full Code Here


      identifiers = Pattern.compile("(['a-zA-Z_$%#&:;<=>?~\\|][a-zA-Z_\\.$%&:;<=>?~_\\|]*|(!)+)"),
      strings = Pattern.compile("\"([^\"]|\\\\\")*\""),
      separators = Pattern.compile("(,|\n)"), operators = Pattern.compile("[\\Q+-*/%^\\E\\\\]");
 
  public static ConsCell tokenizeString(String input) throws SyntaxException, UnbalancedParenthesesException {
    ConsCell head = new ConsCell(), result = head;
    Stack<ConsCell> parenStack = new Stack<>();
    Stack<Integer> parens = new Stack<>();
    Matcher m;
    int current = 0;
    while (current < input.length()) {
      boolean found = false;
      //Parentheses
      if (!found && input.charAt(current) == '(') {
        parens.push(current);
        ConsCell descent = new ConsCell();
        parenStack.push(head.append(new ConsCell(descent, ConsType.CONS_CELL)));
        head = descent;
        current++;
        found = true;
      }
     
      if (!found && input.charAt(current) == ')') {
        if (parenStack.isEmpty())
          throw new UnbalancedParenthesesException(current, input, null);
        head = parenStack.pop();
        parens.pop();
        current++;
        found = true;
      }
     
      //Object
      if (!found && input.charAt(0) == '{') {
        int end = Parser.getEndIndex(input, current, "{", "}") + 1;
        head = head.append(new ConsCell(input.substring(current, end), ConsType.OBJECT));
        current += end;
        found = true;
      }
     
      //Operators
      m = operators.matcher(input);
      if (!found && m.find(current) && m.start() == current) { //An operator is a special identifier
        head = head.append(new ConsCell((input.charAt(current) == '\\') ? '/' : input.charAt(current), ConsType.OPERATOR));
        current++;
        found = true;
      }
     
      //Separator
      m = separators.matcher(input);
      if (!found && m.find(current) && m.start() == current) {
        head = head.append(new ConsCell(m.group(), ConsType.SEPARATOR));
        current += m.group().length();
        found = true;
      }
     
      //Whitespace
      m = whitespace.matcher(input);
      if (!found && m.find(current) && m.start() == current) {
        current += m.group().length();
        found = true;
      }
     
      //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
      m = identifiers.matcher(input);
      if (!found && m.find(current) && m.start() == current) {
        head = head.append(new ConsCell(m.group(), ConsType.IDENTIFIER));
        current += m.group().length();
        found = true;
      }
     
      //Strings
      m = strings.matcher(input);
      if (!found && m.find(current) && m.start() == current) {
        head = head.append(new ConsCell(m.group().substring(1, m.group().length() - 1), ConsType.STRING));
        current += m.group().length();
        found = true;
      }
     
      //If there aren't any matches
View Full Code Here

        try {
          return Tokenizer.tokenizeString(data);
        }
        catch (SyntaxException | UnbalancedParenthesesException e) {
          parser.getLog().logError("Unable to load the saved data for " + getID());
          return new ConsCell();
        }
      }
     
      @Override
      public String toString(ConsCell data) {
        return data.toString();
      }
    });
    if (this instanceof SettingsPlugin && (System.getProperty("java.awt.headless") == null || (System.getProperty("java.awt.headless") != null && !System.getProperty("java.awt.headless").equals("true")))) {
      settingsPanel.setName(getClass().getSimpleName());
      settingsPanel.setLayout(new GridBagLayout());
      settingsPanel.setOpaque(false);
      configurationData = new SimpleXML<ConsCell>(parser.getBaseLocation() + "configuration/" + getID() + ".settings", new Converter<ConsCell>() {
       
        @Override
        public ConsCell fromString(String data) {
          try {
            return Tokenizer.tokenizeString(data);
          }
          catch (SyntaxException | UnbalancedParenthesesException e) {
            parser.getLog().logError("Unable to load the saved configuration in " + getID());
            return new ConsCell();
          }
        }
       
        @Override
        public String toString(ConsCell data) {
View Full Code Here

   * @param key
   *            the key pointing to the data to be removed
   * @return the value mapped to the key being removed
   */
  public final ConsCell removeData(String key) {
    ConsCell output = data.remove(key, DATA_NODE);
    data.remove(key);
    return output;
  }
View Full Code Here

  private StepSolveThread(ConsCell equation, BigDec delta, BigDec xMin, BigDec xMax) throws ParserException {
    super();
    //Yes, I know that this looks horrible, but it works.
    slopeCells = new ArrayList<ConsCell>();
    //First, create the slope structure.  This creates: (a - b)/(c - d)
    slopeEquation = new ConsCell(new ConsCell("a", ConsType.IDENTIFIER, new ConsCell('-', ConsType.OPERATOR, new ConsCell("b", ConsType.IDENTIFIER), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL,
        new ConsCell('/', ConsType.OPERATOR, new ConsCell(new ConsCell("c", ConsType.IDENTIFIER, new ConsCell('-', ConsType.OPERATOR, new ConsCell("d", ConsType.IDENTIFIER), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL);
    slopeCells.add((ConsCell) slopeEquation.cadr("a").getElementA()); //This saves a's spot.
    slopeCells.add((ConsCell) slopeEquation.cadr("add").getElementA()); //This saves b's spot.
    slopeCells.add((ConsCell) slopeEquation.cadr("dda").getElementA()); //This saves c's spot.
    slopeCells.add((ConsCell) slopeEquation.cadr("ddadd").getElementA()); //This saves d's spot.
    this.delta = delta;
View Full Code Here

      initVariableCells(this.equation);
    }
  }
 
  private void initVariableCells(ConsCell equation) {
    ConsCell current = equation;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        initVariableCells((ConsCell) current.getCar());
      if (current.getCarType() == ConsType.IDENTIFIER && ((String) current.getCar()).equals(vars.get(0)))
        variableCells.add(current);
    } while (!(current = current.getNextConsCell()).isNull());
  }
View Full Code Here

    return y.lt(answer) ? 1 : -1;
  }
 
  private BigDec getY(BigDec x) throws ParserException {
    for (ConsCell c : variableCells)
      c.replaceCar(new ConsCell(x, ConsType.NUMBER));
    ConsCell result = parser.run(equation);
    if (result.getCarType() != ConsType.NUMBER || result.length() != 1)
      throw new UndefinedResultException("The NumericalSolver requires that the equation contain only one variable.", null);
    return (BigDec) result.getCar();
  }
View Full Code Here

    return (BigDec) result.getCar();
  }
 
  private BigDec getSlope(BigDec point1[], BigDec point2[]) throws ParserException {
    //First, replace the needed parts of the slope equation with the new numbers
    slopeCells.get(0).replaceCar(new ConsCell(point1[1], ConsType.NUMBER));
    slopeCells.get(1).replaceCar(new ConsCell(point2[1], ConsType.NUMBER));
    slopeCells.get(2).replaceCar(new ConsCell(point1[0], ConsType.NUMBER));
    slopeCells.get(3).replaceCar(new ConsCell(point2[0], ConsType.NUMBER));
    ConsCell result = parser.run(equation);
    if (result.getCarType() != ConsType.NUMBER || result.length() != 1)
      throw new UndefinedResultException("The NumericalSolver requires that the equation contain only one variable.", null);
    return (BigDec) result.getCar();
  }
View Full Code Here

      denominator = denominator.multiply(ten);
    }
    BigDec gcd = numerator.gcd(denominator);
    numerator = numerator.divide(gcd);
    denominator = denominator.divide(gcd);
    return new ConsCell(numerator, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(denominator, ConsType.NUMBER)));
  }
View Full Code Here

   *            the equation to check
   * @return the variables in input, if there are any, an empty ArrayList<String> otherwise
   */
  public ArrayList<String> getVariables(ConsCell input) {
    ArrayList<String> output = new ArrayList<String>();
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.IDENTIFIER && !allNames.contains(current.getCar()) && isAllLetters((String) current.getCar()) && !output.contains(current.getCar()))
        output.add((String) current.getCar());
      if (current.getCarType() == ConsType.CONS_CELL) {
        ArrayList<String> temp = getVariables((ConsCell) current.getCar());
        for (String var : temp)
          if (!output.contains(var))
            output.add(var);
      }
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    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.