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;
var = "[xyz]";
return slope.multiply(BigDec.MINUSONE);