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)*(-x - z + 1) + x*y*z*(-z
+ 1)*(-y - z + 1)
>>> factor(eq)
2*x*y*z*(x**2 + x*y + x*z - 3*x + y**2 + y*z - 3*y + z**2 - 3*z + 3)
>>> horner(_)
x*(x*(2*x*y*z + y*(2*y*z + z*(2*z - 6))) + y*(y*(2*y*z + z*(2*z - 6)) + z*(z*(2*
z - 6) + 6)))
>>> factor_terms(_)
2*x*y*(x*(x*z + y*z + z*(z - 3)) + y*(y*z + z*(z - 3)) + z*(z*(z - 3) + 3))
>>> r, e = cse(_)
>>> e[0]
2*x*y*z*(x*(x + x0 + y) + x0*z + y*(x0 + y) + 3)
>>> var('x0')
x0
>>> e[0]/2/x/y/z
x*(x + x0 + y) + x0*z + y*(x0 + y) + 3
>>> expand(_)
x**2 + x*x0 + x*y + x0*y + x0*z + y**2 + 3
>>> collect(_,x0)
x**2 + x*y + x0*(x + y + z) + y**2 + 3
>>> factor(_.subs(x0,0)-3)+3+x0*(x+y+z)
x**2 + x*y + x0*(x + y + z) + y**2 + 3
>>> _.subs(r)
x**2 + x*y + y**2 + (z - 3)*(x + y + z) + 3
>>> _*x*y*z*2
2*x*y*z*(x**2 + x*y + y**2 + (z - 3)*(x + y + z) + 3)
>>> feq = _
>>> feq.count_ops()
15
>>> feq.equals(eq)
True

Is the alternate shorter than the original factored form?

>>> x * y * z * (Symbol('6') * (1 - x - y - z) + (x + y) ** 2 + (y + z) ** 2 + (
x + z) ** 2)
x*y*z*(6*(-x - y - z + 1) + (x + y)**2 + (x + z)**2 + (y + z)**2)
>>> feq1=_
>>> feq1.count_ops()
16

So it's shorter by 1 op count. And visually...

>>> feq1
x*y*z*(6*(-x - y - z + 1) + (x + y)**2 + (x + z)**2 + (y + z)**2)
>>> feq
2*x*y*z*(x**2 + x*y + y**2 + (z - 3)*(x + y + z) + 3)

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to