Sympy summation with indexed variable

六眼飞鱼酱① 提交于 2019-12-01 20:54:04

问题


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?


回答1:


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



回答2:


You can use lambdify. Just make sure the limits of the sum match the iterables of a numpy array.

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)


来源:https://stackoverflow.com/questions/26402387/sympy-summation-with-indexed-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!