简体   繁体   中英

Substituting values in sympy for a list gives the unexpected result

I want to do the following operation in which I feed in the index and it spits out the value that I want. However When I assign it the values it gives me 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})

I want the answer to be 5**2+3 = 28, but it gives me a[0, 4]**2 + 3.

The IndexedBase type doesn't seem to be implemented for accessing data (but rather for notation purpose). See at http://docs.sympy.org/latest/modules/tensor/indexed.html the following statement:

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.

Maybe you could achieve what you want with a Matrix type?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM