Question

I am trying to give an array as input and expect an array as 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 a single array? Thanks in advance.

Was it helpful?

Solution

1. Solution: Your problem is, that the function T expects a value, but you are handing out a list. Try this 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 a wrapper function:

def arrayT(array, params):
    return [T(val, params) for val in array]

print arrayT(data[0], [0.29, 4.5])

2. Solution: You have to change your mathematical expression. Somehow sympy doesn't work with list of lists, so try this:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top