return new Exponentiation(base, exp);
}
private Expression simplify() {
Expression base = lhs.evaluate();
Expression exp = rhs.evaluate();
NaturalNumber two = Naturals.getInstance().create(2);
if (exp.equals(two)) {
if (base instanceof Sqrt) {
return ((Sqrt) base).argument();
} else if (base instanceof Addition) {
Expression a = ((Addition) base).lhs();
Expression b = ((Addition) base).rhs();
return new Sum(new Exponentiation(a, two), new Exponentiation(b, two),
new Multiplication(two, new Multiplication(a, b))).evaluate();
} else if (base instanceof Subtraction) {
Expression a = ((Subtraction) base).lhs();
Expression b = ((Subtraction) base).rhs();
return new Sum(new Exponentiation(a, two), new Exponentiation(b, two),
new Negation(new Multiplication(two, new Multiplication(a, b)))).evaluate();
} else if (base instanceof Negation) {
return new Exponentiation(((Negation) base).getChild(), exp).evaluate();
}
}
if (base instanceof Multiplication) {
Expression a = ((Multiplication) base).lhs();
Expression b = ((Multiplication) base).rhs();
return new Multiplication(new Exponentiation(a, exp),
new Exponentiation(b, exp)).evaluate();
} else if (base instanceof Division) {
Expression a = ((Division) base).lhs();
Expression b = ((Division) base).rhs();
return new Division(new Exponentiation(a, exp),
new Exponentiation(b, exp)).evaluate();
}