Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing between different expression factorizations in SymPy

Tags:

python

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:

  1. Is there a way to "expand" an expression before calling collect?
  2. 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?
like image 725
Amelio Vazquez-Reina Avatar asked Jul 29 '11 21:07

Amelio Vazquez-Reina


1 Answers

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)
like image 188
HYRY Avatar answered Sep 28 '22 04:09

HYRY