This comes from http://stackoverflow.com/q/15180488/161801.
Does anyone know of any functions in SymPy that could convert
expression = (
x * (1 - x) * y * (1 - x - y) * z +
x * (1 - x) * z * (1 - x - z) * y +
y * (1 - y) * x * (1 - y - x) * z +
y * (1 - y) * z * (1 - y - z) * x +
The only tricky factoring that I know of is horner. That can be used
as follows to obtain a shorter expression in terms of op counts and
physical length:
>>> eq
x*y*z*(-x + 1)*(-x - y + 1) + x*y*z*(-x + 1)*(-x - z + 1) + x*y*z*(-y + 1)*(-x -
y + 1) + x*y*z*(-y + 1)*(-y - z + 1) + x*y*z*(-z + 1)*(
I posted the following on the SO page
def iflfactor(eq):
"""Return the "I'm feeling lucky" factored form of eq."""
e = Mul(*[horner(e) if e.is_Add else e for e in
Mul.make_args(factor_terms(expand(eq)))])
r, e = cse(e)
s = [ri[0] for ri in r]
e = Mul(*[collect(ei.expand