adding a heuristic rewrite rule to Sympy simplification

36 views
Skip to first unread message

dma...@gmail.com

unread,
Jul 1, 2021, 3:24:13 PM7/1/21
to sympy
 
How can I extend Sympy's simplification mechanism with additional rules?
The specific rule I would like to add is:

    Sum((A**_k)*binomial(N, _k), (_k, 0, N)) ==> (1 + A)**N

I would prefer to have it incorporated into general simplification functions like simplify & combsimp since it comes up frequently in my calculations.
For one rule I can do an explicit rewrite call, but if I have a library of rules I would like to have a way to integrate them into the main simplification process

thanks
Daniel

Aaron Meurer

unread,
Jul 5, 2021, 3:57:30 PM7/5/21
to sympy
SymPy can already compute this sum (call doit() on the Sum object).
You should add integer=True and positive=True assumptions on N or else
you will get complicated convergence conditions. The result is a
Piecewise because it needs to have |A| <= 1 to converge. You can
remove it with refine(result, abs(A) <= 1).

>>> N, k = symbols("N k", integer=True, positive=True)
>>> A = symbols("A")
>>> Sum((A**k)*binomial(N, k), (k, 0, N)).doit()
Piecewise(((A + 1)**N, Abs(A) <= 1), (Sum(A**k*binomial(N, k), (k, 0,
N)), True))
>>> refine(Sum((A**k)*binomial(N, k), (k, 0, N)).doit(), abs(A) <= 1)
(A + 1)**N

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ff075049-582e-4397-b10b-fa9ab82cfa10n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages