Sympy summation with indexed variable

Diogo Santos picture Diogo Santos · Oct 16, 2014 · Viewed 8.7k times · Source

I try to create a sympy expression with a Sum with an indexed variable as previous explain here However, I can not do lambdify of this expression and give an array to get the sum calculated. Is this impossible?

Answer

smichr picture smichr · Oct 17, 2014

Perhaps like this?

>>> s=Sum(Indexed('x',i),(i,1,3))
>>> f = lambda x: Subs(s.doit(), [s.function.subs(s.variables[0], j)
... for j in range(s.limits[0][1], s.limits[0][2] + 1)], x).doit()
>>> f((30,10,2))
42