current.replace(derivative);
return current.getFirstConsCell();
}
private ConsCell powerRule(ConsCell current, ArrayList<String> vars) throws ParserException {
ConsCell derivative = new ConsCell();
ConsCell left = current.getPreviousConsCell().singular(), right = current.getNextConsCell().singular();
current.getPreviousConsCell().remove();
current.getNextConsCell().remove();
ConsCell temp = current;
while (!(temp = temp.getPreviousConsCell()).isNull() && temp.getCarType() != ConsType.OPERATOR) {
left = temp.singular().append(left).getFirstConsCell();
temp = temp.remove();
}
temp = current;
while (!(temp = temp.getNextConsCell()).isNull() && temp.getCarType() != ConsType.OPERATOR) {
right = right.append(temp.singular());
temp = temp.remove();
}
right = right.getFirstConsCell();
if (parser.containsVariables(right, vars)) {
derivative = derivative.append(new ConsCell(symbolicDerivative(right, vars), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR), ConsType.CONS_CELL));
derivative = derivative.append(new ConsCell(left.clone(), ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(right.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
derivative = derivative.append(new ConsCell('*', ConsType.OPERATOR, new ConsCell("ln", ConsType.IDENTIFIER, new ConsCell(left.clone(), ConsType.CONS_CELL),
ConsType.CONS_CELL), ConsType.CONS_CELL));
}
if (parser.containsVariables(left, vars)) {
if (!derivative.isNull())
derivative = derivative.append(new ConsCell('+', ConsType.OPERATOR));
derivative = derivative.append(new ConsCell(right.clone(), ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, left.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL));
derivative = derivative.append(new ConsCell('^', ConsType.OPERATOR, new ConsCell(parser.run(right.clone().append(
new ConsCell('-', ConsType.OPERATOR, new ConsCell(BigDec.ONE, ConsType.NUMBER), ConsType.CONS_CELL)).getFirstConsCell()), ConsType.CONS_CELL), ConsType.CONS_CELL));
derivative = derivative.append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(symbolicDerivative(left, vars), ConsType.CONS_CELL), ConsType.CONS_CELL));
}
derivative = parser.getCAS().simplifyTerm(parser.getCAS().removeExcessParentheses(derivative.getFirstConsCell()));
return derivative;
}