Sympy summation with indexed variable

I am trying to create a simplex sum expression with an indexed variable as shown in the previous explanation here However, I cannot lambdify that expression and give an array to calculate the sum. It's impossible?

+3


source to share


2 answers


Maybe so?



>>> 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

      

+2


source


You can use lambdify. Just make sure the sum limits match the numpy array iterations.



from sympy import Sum, symbols, Indexed, lambdify
import numpy as np

x, i = symbols("x i")
s = Sum(Indexed('x',i),(i,0,3))
f = lambdify(x, s)
b = np.array([1, 2, 3, 4])
f(b)

      

0


source







All Articles