4

I have a lot of expressions which sympy default writes as sums of products of variables. These expressions can get quite long. I would like to simplify these expressions to have as few as possible multiplications. A reduced example:

from sympy import symbols, cse, factor, simplify, count_ops, collect
a,b,c=symbols("a b c", integer=True, positive=True)
e = a*a*b + a*a + a*b*b + a*b*c + 4*a*b + a*c + 3*a + b*b*c + 4*b*c + 3*c + 1

What I would like to get is something like:

(a + b + 3) * (a + c) * (b + 1) + 1

which in this case has only 3 multiplications and 5 additions.

Sympy functions such as factor don't work, because of the extra terms.

simplify keeps insisting on creating sums of simple factors, even when I experiment with the measure argument to penalize powers and multiplications.

cse only separates products of simple terms.

Is there a way to generate these kind of simplifications with sympy?

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • For this example you can use `factor(e-1)+1`. I don't know of a general way. – Oscar Benjamin Oct 18 '19 at 22:35
  • @OscarBenjamin: my real expressions involve multiple factorized terms – JohanC Oct 18 '19 at 22:51
  • Unless the symbols in those terms are independent of each other it is going to be hard to identify which combination of terms are going to lead to a favorable factoring. – smichr Oct 19 '19 at 12:17
  • @smichr: So maybe trying out random combinations and then save the "best" would be a valid approach? The documentation of ```simplify``` suggests that this function would do just that, but I only get the long form back. It's typical a function I would assume a strong optimizing compiler would be doing. – JohanC Oct 19 '19 at 13:12
  • 1
    Although you can write an algorithm to solve this problem, there's no guarantee that it's going to work for similar problem. e.g. you could notice that c appears in fewest terms so collect those terms and attempt factoring. In the result notice that one factor has a and the other does not, so try factoring terms that have a and not c. The c-terms and a-terms share common factors so those can be factored out. etc.. `horner` leads to some improvement. heuristics like the above might lead to improvements in some cases. I wouldn't hope for much more. – smichr Oct 19 '19 at 15:57
  • @smichr: many thanks for pointing out ```horner``` which goes quite more into the needed direction than the other functions I tried. – JohanC Oct 19 '19 at 20:13

0 Answers0