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.
lambdify() method. With the help of sympy. lambdify() method, we can convert a SymPy expression to an expression that can be numerically evaluated. lambdify acts like a lambda function, except it, converts the SymPy names to the names of the given numerical library, usually NumPy or math.
In general, SymPy functions do not work with objects from other libraries, such as NumPy arrays, and functions from numeric libraries like NumPy or mpmath do not work on SymPy expressions.
To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .
The subs() function in SymPy replaces all occurrences of the first parameter with the second. Substitution is the basic operations that must be performed in a mathematical expression. In this way, we can use subs() function in sympy.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With