Sympy Lambdify with array inputs

I am trying to give an array as input and expect the array to be output for the following code.

from sympy import symbols from sympy.utilities.lambdify import lambdify import os from sympy import * import numpy as np text=open('expr.txt','r') expr=text.read() x,param1,param2=symbols('x param1 param2') params=np.array([param1,param2]) T=lambdify((x,params),expr,modules='numpy') data=np.genfromtxt('datafile.csv',delimiter=',') print T(data[0],[0.29,4.5]) text.close() 

But get the following error.

 TypeError: <lambda>() takes exactly 3 arguments (13 given) 

How do I tell sympy that its only array? Thanks in advance.

+7
python sympy
source share
1 answer

1. Solution: Your problem is that the T function expects a value, but you are passing a list. Instead of print T(data[0],[0.29,4.5]) to get a list of results:

 print [T(val,[0.29,4.5]) for val in data[0]] 

Or use the wrapper function:

 def arrayT(array, params): return [T(val, params) for val in array] print arrayT(data[0], [0.29, 4.5]) 

2. The decision. You must change your mathematical expression. Somehow the sims don't work with a list of lists, so try the following:

 expr = "2*y/z*(x**(z-1)-x**(-1-z/2))" T=lambdify((x,y,z),expr,'numpy') print T(data[0], 0.29, 4.5) 
+5
source share

All Articles