Choosing between different expression factorizations in SymPy
Say I have an expression as follows:
a*b*c + b*c + a*d
One could factorize it as:
b*(a*c + c) + (a*d)
or as
c*(a*b + b) + (a*d)
or as
a*d + b*c*(a + 1)
among other possibilities.
For other expressions, the # of possibilities can be much larger.
My question is, does SymPy have any utility that allows the user to choose which of them to display? Is there a way to specify the common factor/s to use when factorizing / grouping terms in an expression?
EDIT: As @user772649 points out below, I can use collect
for this. However, collect
seems to give different outputs depending on the initial factorization of the mathematical expression e.g.:
a,b,c,d = symbols("a,b,c,d")
# These two equations are mathematically equivalent:
eq1 = a*b*c + b*c + a*d
eq2 = a*d + b*c*(a + 1)
print collect(eq1, a)
print collect(eq2, a)
prints:
a*(b*c + d) + b*c
a*d + b*c*(a + 1)
The equations eq1
and eq2
are mathematically equivalent, but collect
outputs a different factorization for each of them, despite of the fact that the call to the collect
command was the same for both. This brings me to the following two questions:
- Is there a wa开发者_开发问答y to "expand" an expression before calling
collect
? - Is there a way of "collecting" (factoring an expression) in a way that is invariant to the initial factorization without having to expand the expression first?
use collect():
from sympy import *
a,b,c,d = symbols("a,b,c,d")
eq = a * b * c + b * c + a * d
print collect(eq, b)
print collect(eq, c)
print collect(eq, b*c)
the output is:
a*d + b*(c + a*c)
a*d + c*(b + a*b)
a*d + b*c*(1 + a)
One thing that might be nice is if collect
would collect on previously grouped sub-expressions if more than one symbol is given. But either giving a product to collect on (as @HYRY showed) or something like the following is possible:
def separatevars_additively(expr, symbols=[]):
from sympy import factor_terms
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = factor_terms(dep)
if expr:
d[0] = expr
return d
var('a:d')
eq = a*b*c + b*c + a*d
def do(i):
return sum(separatevars_additively(eq,[i]).values())
for i in eq.free_symbols:
print('%s: %s' % (i, do(i)))
gives
b: a*d + b*c*(a + 1)
a: a*(b*c + d) + b*c
c: a*d + b*c*(a + 1)
d: a*b*c + a*d + b*c
精彩评论