Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96330

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:

  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?

Upvotes: 8

Views: 975

Answers (2)

smichr
smichr

Reputation: 19063

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

Upvotes: 0

HYRY
HYRY

Reputation: 97321

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)

Upvotes: 5

Related Questions