super("sqrt", arg);
}
@Override
public Expression evaluate() {
Expression x = arg.evaluate();
if (x instanceof ANumber) {
ANumber a = (ANumber) x;
if (a instanceof NaturalNumber) {
return Naturals.sqrt((NaturalNumber) a);
} else if (!a.isNegative()) {
if (a instanceof IntegerNumber) {
return Naturals.sqrt(a.toNatural());
} else if (a instanceof RationalNumber) {
return sqrt((RationalNumber) a);
}
}
} else if (x instanceof Multiplication) {
Expression e = sqrt((Multiplication) x);
if (e instanceof Multiplication) {
return e;
}
} else if (x instanceof Addition) {
Expression factorized = ((Addition) x).factorize();
if (factorized instanceof Multiplication) {
Expression e = sqrt((Multiplication) factorized);
if (e instanceof Multiplication) {
return e;
}
}