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:
collect
? 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With