用 sympy 中的值替换列表会产生意想不到的结果
Substituting values in sympy for a list gives the unexpected result
我想执行以下操作,我在其中输入索引,然后它吐出我想要的值。但是,当我为其分配值时,它给了我意想不到的结果。
import sympy as sy
import numpy as np
i = sy.Symbol('i', integer=True)
j = sy.Symbol('j', integer=True)
a = sy.IndexedBase('a')
y=a[i, j]**2+3
y.subs({a:np.array([[1,2,3,4,5,6,7]]),i:0,j:4})
我希望答案是 5**2+3 = 28,但结果是 [0, 4]**2 + 3。
IndexedBase
类型似乎没有为访问数据而实现(而是为了符号目的)。请参阅 http://docs.sympy.org/latest/modules/tensor/indexed.html 中的以下声明:
The main purpose of this class is to allow the convenient creation of objects of the Indexed class. The getitem method of IndexedBase returns an instance of Indexed.
也许您可以使用 Matrix
类型实现您想要的效果?
我想执行以下操作,我在其中输入索引,然后它吐出我想要的值。但是,当我为其分配值时,它给了我意想不到的结果。
import sympy as sy
import numpy as np
i = sy.Symbol('i', integer=True)
j = sy.Symbol('j', integer=True)
a = sy.IndexedBase('a')
y=a[i, j]**2+3
y.subs({a:np.array([[1,2,3,4,5,6,7]]),i:0,j:4})
我希望答案是 5**2+3 = 28,但结果是 [0, 4]**2 + 3。
IndexedBase
类型似乎没有为访问数据而实现(而是为了符号目的)。请参阅 http://docs.sympy.org/latest/modules/tensor/indexed.html 中的以下声明:
The main purpose of this class is to allow the convenient creation of objects of the Indexed class. The getitem method of IndexedBase returns an instance of Indexed.
也许您可以使用 Matrix
类型实现您想要的效果?