Simplify expression

18 views
Skip to first unread message

ecir...@gmail.com

unread,
Mar 14, 2014, 1:30:24 PM3/14/14
to sy...@googlegroups.com
Hi!

I'm trying to simplify expressions in the form of "a*b*c + d*e + f - g*h*h + i*a", basically the terms can be either added or subtracted and each term is a product of some symbols.

For example, I would like to turn the expression:

    a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4

into:

    (a - b)**4 - a

Both "simplify" and "factor" do not work. I tried to ask around about it but so far without luck. And that brings me here...

1. Are there any tricks in SymPy which could simplify the above expression?

2. If no, is it even possible to do such "compressions" in theory? The problem seems to be that sometimes one needs to add an expression only to be able to reduce some other expressions.

3. If I knew that one term is in the form of "(a - b)", would the help?

4. If all the answers turns out to be "no", what can I do? Could someone recommend a strategy on how to tackle such expression?

Thanks!

Sergey Kirpichev

unread,
Mar 14, 2014, 1:48:07 PM3/14/14
to sy...@googlegroups.com


On Friday, March 14, 2014 9:30:24 PM UTC+4, ecir...@gmail.com wrote:
3. If I knew that one term is in the form of "(a - b)", would the help?

Yes, for example:
In [2]: e = a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4

In [3]: e
Out[3]:
 4      3        2  2        3        4
a  - 4⋅a ⋅b + 6⋅a ⋅b  - 4⋅a⋅b  - a + b

In [4]: e2 = e.subs(a, x + b)

In [5]: e2.expand()
Out[5]:
      4   
-b + x  - x

In [6]: e2.expand().subs(x, a - b)
Out[6]:
            4
-a + (a - b)

ecir...@gmail.com

unread,
Mar 15, 2014, 10:48:29 AM3/15/14
to sy...@googlegroups.com
Thank you!
Reply all
Reply to author
Forward
0 new messages