Reputation: 24834
I begin with sympy python lib.
If, I have this expression
from sympy.abc import a,b,c,p,q
e = p * ( a + b ) + q * ( a + c )
how I can use a,b,c
as factor ? like
a(p+q) + b*p + c*q
Upvotes: 3
Views: 414
Reputation: 91680
collect
is indeed the function you want. You can pass multiple symbols as the collection variable to collect them all. And as you noticed, collect
will not expand your expression first, so if you want that, you have to do it yourself with expand
.
In [15]: collect(e.expand(), [a, b, c])
Out[15]: a⋅(p + q) + b⋅p + c⋅q
Upvotes: 3
Reputation: 24834
from sympy.abc import a,b,c,p,q
from sympy import collect, expand
e = p * ( a + b ) + q * ( a + c )
print e
print expand(e)
print collect(expand(e),a)
Upvotes: 5