* Factorize this sum (if possible).
*
* @return either the factorized form of this sum or this
*/
public Expression factorize() {
IntegerNumber l = null, r = null;
Expression lrest = null, rrest = null;
if (lhs instanceof IntegerNumber) {
l = (IntegerNumber) lhs;
lrest = Naturals.one();
} else if (lhs instanceof Multiplication
&& ((Multiplication) lhs).lhs() instanceof IntegerNumber) {
l = (IntegerNumber) ((Multiplication) lhs).lhs();
lrest = ((Multiplication) lhs).rhs();
} else if (lhs instanceof Division
&& ((Division) lhs).lhs() instanceof IntegerNumber) {
l = (IntegerNumber) ((Division) lhs).lhs();
lrest = new Division(Naturals.one(), ((Division) lhs).rhs());
}
if (rhs instanceof IntegerNumber) {
r = (IntegerNumber) rhs;
rrest = Naturals.one();
} else if (rhs instanceof Multiplication
&& ((Multiplication) rhs).lhs() instanceof IntegerNumber) {
r = (IntegerNumber) ((Multiplication) rhs).lhs();
rrest = ((Multiplication) rhs).rhs();
} else if (rhs instanceof Division
&& ((Division) rhs).lhs() instanceof IntegerNumber) {
r = (IntegerNumber) ((Division) rhs).lhs();
rrest = new Division(Naturals.one(), ((Division) rhs).rhs());
}
if (l != null && r != null) {
IntegerNumber lcm = (IntegerNumber) new Lcm(l, r).evaluate();
IntegerNumber gcd = (IntegerNumber) l.multiply(r).divide(lcm);
if (!gcd.isOne()) {
return new Multiplication(gcd, new Addition(
new Multiplication(l.divide(gcd), lrest),
new Multiplication(r.divide(gcd), rrest)).evaluate());
}
}