简体   繁体   中英

Turning a list of sympy expressions into a list of lambdified expressions

n=input()

w=sym.symarray(' ', n) 

w[0]=sym.exp(-x**2)

for i in range(1,n):

    w[i]=sym.factor(sym.diff(w[i-1], x))

the lines above result in a list of symbolic sympy functions, which i can access and use, but i wanted to turn it into an array of numeric functions, i have tried through lambdify but it didnt work. while

f=np.arange(0,n)

f[0]=sym.lambdify(x, w[0], modules='numpy')

works just fine, the loop bellow doesnt:

f=np.arange(0,n)

for i in range(0,n):

    f[i]=sym.lambdify(x, w[i], modules='numpy') 

and returns the following error:

Traceback (most recent call last):

  File "/Users/Yuri/newwaves.py", line 16, in <module>

    f[i]=sym.lambdify(x, w[i], np)

 TypeError: int() argument must be a string, a bytes-like object or a number, not 'function'

arange returns a numpy array with dtype=int. To store lambdified functions, you need to use dtype=object. You can create an empty array with np.empty(n, dtype=object) .

Alternately, you could just use a Python list , which is much simpler to work with, since you don't really gain any of the advantages of numpy arrays when your dtype is object .

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