*/
private ArrayList<BigDec> formSolve(ConsCell equation) throws ParserException {
ArrayList<BigDec> solutions = new ArrayList<BigDec>();
ArrayList<ConsCell> terms = foldSigns(getTerms(simplifyTerms(equation)));
PairedList<ConsCell, BigDec> orders = new PairedList<>();
BigDec highestOrder = BigDec.ZERO;
for (ConsCell term : terms) {
PairedList<ConsCell, ConsCell> order = orderOfTerm(term, parser.getVars());
if (order.size() > 1)
return new ArrayList<BigDec>();
BigDec o = BigDec.ZERO;
if (order.size() > 0 && order.get(order.getKeys().get(0)).getCarType() == ConsType.NUMBER)
o = ((BigDec) order.get(order.getKeys().get(0)).getCar());
if (o.gt(highestOrder))
highestOrder = o;
orders.put(term, o);
}
BigDec modifier = null;
BigDec[] form = new BigDec[]{BigDec.ONE, BigDec.ZERO};
if ((modifier = matchesForm(orders, highestOrder, form)) != null) { //Linear
if (linearEquation == null) { //Initialize the linear equation structure. This makes subsequent calls faster.
linearEquationHooks[0] = new ConsCell("a", ConsType.IDENTIFIER);
linearEquationHooks[1] = new ConsCell("b", ConsType.IDENTIFIER, new ConsCell('/', ConsType.OPERATOR, linearEquationHooks[0]));
linearEquation = new ConsCell('-', ConsType.OPERATOR, linearEquationHooks[1]);
}
BigDec a = BigDec.ZERO, b = BigDec.ZERO;
form[0] = form[0].subtract(modifier);
form[1] = form[1].subtract(modifier);
for (ConsCell term : orders) {
BigDec order = orders.get(term);
if (order.eq(form[1]))
b = b.add(getCoefficient(term));
else
a = a.add(getCoefficient(term));
}
linearEquationHooks[0].replaceCar(new ConsCell(a, ConsType.NUMBER));
linearEquationHooks[1].replaceCar(new ConsCell(b, ConsType.NUMBER));
solutions.add(new BigDec(parser.run(linearEquation.clone()).toString()));
}
if (solutions.size() == 0 && (modifier = matchesForm(orders, highestOrder, (form = new BigDec[]{new BigDec(2), BigDec.ONE, BigDec.ZERO}))) != null) { //Quadratic
if (quadraticEquation == null) { //Initialize the quadratic equation structure. This makes subsequent calls faster.
quadraticEquation = Tokenizer.tokenizeString("(-b\"+-\"(b^2-4*a*c)^0.5)/(2*a)");
quadraticEquationHooks[0] = quadraticEquation.allInstancesOf(new ConsCell("a", ConsType.IDENTIFIER));
quadraticEquationHooks[1] = quadraticEquation.allInstancesOf(new ConsCell("b", ConsType.IDENTIFIER));
quadraticEquationHooks[2] = quadraticEquation.allInstancesOf(new ConsCell("c", ConsType.IDENTIFIER));
quadraticEquationHooks[3] = quadraticEquation.allInstancesOf(new ConsCell("+-", ConsType.STRING));
}
BigDec a = BigDec.ZERO, b = BigDec.ZERO, c = BigDec.ZERO;
form[0] = form[0].subtract(modifier);
form[1] = form[1].subtract(modifier);
form[2] = form[2].subtract(modifier);
for (ConsCell term : orders) {
BigDec order = orders.get(term);
if (order.eq(form[2]))
c = c.add(getCoefficient(term));
else if (order.eq(form[1]))
b = b.add(getCoefficient(term));
else
a = a.add(getCoefficient(term));
}
for (ConsCell cell : quadraticEquationHooks[0])
cell.replaceCar(new ConsCell(a, ConsType.NUMBER)); //a
for (ConsCell cell : quadraticEquationHooks[1])
cell.replaceCar(new ConsCell(b, ConsType.NUMBER)); //b
quadraticEquationHooks[2].get(0).replaceCar(new ConsCell(c, ConsType.NUMBER)); //c
quadraticEquationHooks[3].get(0).replaceCar(new ConsCell('+', ConsType.OPERATOR)); //+
solutions.add(new BigDec(parser.run(quadraticEquation.clone()).toString()));
quadraticEquationHooks[3].get(0).replaceCar(new ConsCell('-', ConsType.OPERATOR)); //-
solutions.add(new BigDec(parser.run(quadraticEquation.clone()).toString()));
}
return checkSolutions(solutions, equation, accuracy);
}