6

I want to achieve this kind of simplification: e+ac+ad+bc+bd = e+(a+b)(c+d) . None of SymPy simplification functions worked this way. Is there any other method in SymPy or somewhere else in python to get this kind of simplification?

ntalbs
  • 28,700
  • 8
  • 66
  • 83

1 Answers1

7

You can use collect(expr, e, func=factor).

In [5]: expr = e + a*c + a*d + b*c + b*d

In [6]: collect(expr, e, func=factor)
Out[6]: e + (a + b)⋅(c + d)
asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • Is it possible to do it for general case. For example if we don't know that name of first variable is "e"? Or if we don't know name of any variable? – Giorgi Bakhtadze Nov 17 '16 at 08:43
  • In general you need to know something about the way that you want to do this "partial" factorization. Such a thing is not well defined as there are generally many ways that you can factor different parts an expression (as opposed to factoring the whole expression, which is always unique). – asmeurer Nov 17 '16 at 19:12
  • Is it possible to make same kind of simplification if instead of "e" we have any symbol from other part of expression? For example a + a*c + a*d + b*c + b*d = a + (a + b)⋅(c + d). Above method doesn't work in this case. – Giorgi Bakhtadze Feb 15 '17 at 01:14
  • @GiorgiBakhtadze I think you'd have to replace `c + d` with a single symbol, like `expr.subs(c, x - d)` and collect on that. – asmeurer Feb 15 '17 at 08:09