I would like to lamdify an array input with sympy. Here was my first attempt:
我想用sympy来表示数组输入。这是我的第一次尝试:
import sympy as sym
import numpy as np
# Load Data
data = np.loadtxt( "D:\data.r2023.c87.dat", skiprows=1)
# Access to columns
vza = data [:,2]
sza = data [:,4]
# var_psi is the array input
psi = (1/(np.cos(sza))) + (1/(np.cos(vza)))
var_tau, var_omega, var_psi = sym.symbols('var_tau var_omega var_psi', real = True)
sBetaFunc = sym.exp(-var_tau * var_psi)
sBeta = sym.lambdify(var_psi, sBetaFunc, modules=[“numpy”, "sympy"])
If I now try to call the function the following error appears:
如果我现在尝试调用该函数,则会出现以下错误:
>>> sBeta(psi)
>>> AttributeError: 'Mul' object has no attribute 'exp'
If I try it this way the following error appears:
如果我这样尝试,会出现以下错误:
>>> sBeta(*psi)
>>> TypeError: <lambda>() takes exactly 1 argument (79 given)
I read a lot about this problem. However, nothing seems suitable to my problem or my case.
我读了很多关于这个问题的文章。但是,似乎没有什么比我的问题或我的情况更合适。
I need this in a sympy function because I would like to use the diff function from sympy to differentiate some very complex functions.
我需要在sympy函数中使用它,因为我想使用sympy中的diff函数来区分一些非常复杂的函数。
Thank you in advanced.
先谢谢你。
Edit:
Now I tried this:
现在我尝试了这个:
import sympy as sym
import numpy as np
from sympy.abc import w, x, y, z
sBetaFunc = sym.exp(-var_tau * x)
sBeta = sym.lambdify(x, sBetaFunc, modules=["sympy"])
Now a different error appears:
现在出现一个不同的错误:
>>> sBeta(psi)
>>> ValueError: sequence too large; cannot be greater than 32
2 个解决方案
#1
1
I am not fully sure about all the error messages you receive; one thing I found was that it can be caused by a * in name space. As you import the functions explicitly, this is probably not the issue here. I think it is caused by the fact that you do not provide a value for var_tau
.
我不完全确定您收到的所有错误消息;我发现的一件事是它可能是由名称空间的冲突引起的。当您明确导入函数时,这可能不是问题所在。我认为这是因为你没有为var_tau提供一个值。
The following should do what you try to accomplish:
以下应该做你想要完成的事情:
import sympy as sym
import numpy as np
var_tau, var_omega, var_psi = sym.symbols('var_tau var_omega var_psi', real=True)
sBetaFunc = sym.exp(-var_tau * var_psi)
# also take your tau into account
sBeta = sym.lambdify((var_tau, var_psi), sBetaFunc, modules=np)
# your data; replace with actual values
psi = np.array([1, 2, 3])
# your value for tau
my_tau = 1.
# evaluate your function
result = sBeta(my_tau, psi)
Then result
looks like this:
然后结果如下:
array([ 0.36787944, 0.13533528, 0.04978707])
#2
1
If there is someone who is faced with the same problem I´ll have a solution for you: According to @Cleb answer I solved that problem this was:
如果有人遇到同样的问题,我会为你找到一个解决方案:根据@Cleb的回答我解决了这个问题:
psi = np.array([1, 2, 3])
var_tau = sym.symbols('var_tau', real = True)
sBeta = sym.lambdify((x, y), np.e**(-x*y), ["numpy", "sympy"])
result = sBeta(var_tau, psi)
Then result
looks like this:
然后结果如下:
array([2.71828182845905**(-var_tau), 2.71828182845905**(-2*var_tau),
2.71828182845905**(-3*var_tau)], dtype=object)
Now I am able to use the sym.diff function like this:
现在我可以像这样使用sym.diff函数:
In [1]: sym.diff(result[1], var_tau)
Out[1]: -2.0*2.71828182845905**(-2*var_tau)
However, if I deal with var_tau like a variable it works very well.
但是,如果我像变量一样处理var_tau,它的效果非常好。
#1
1
I am not fully sure about all the error messages you receive; one thing I found was that it can be caused by a * in name space. As you import the functions explicitly, this is probably not the issue here. I think it is caused by the fact that you do not provide a value for var_tau
.
我不完全确定您收到的所有错误消息;我发现的一件事是它可能是由名称空间的冲突引起的。当您明确导入函数时,这可能不是问题所在。我认为这是因为你没有为var_tau提供一个值。
The following should do what you try to accomplish:
以下应该做你想要完成的事情:
import sympy as sym
import numpy as np
var_tau, var_omega, var_psi = sym.symbols('var_tau var_omega var_psi', real=True)
sBetaFunc = sym.exp(-var_tau * var_psi)
# also take your tau into account
sBeta = sym.lambdify((var_tau, var_psi), sBetaFunc, modules=np)
# your data; replace with actual values
psi = np.array([1, 2, 3])
# your value for tau
my_tau = 1.
# evaluate your function
result = sBeta(my_tau, psi)
Then result
looks like this:
然后结果如下:
array([ 0.36787944, 0.13533528, 0.04978707])
#2
1
If there is someone who is faced with the same problem I´ll have a solution for you: According to @Cleb answer I solved that problem this was:
如果有人遇到同样的问题,我会为你找到一个解决方案:根据@Cleb的回答我解决了这个问题:
psi = np.array([1, 2, 3])
var_tau = sym.symbols('var_tau', real = True)
sBeta = sym.lambdify((x, y), np.e**(-x*y), ["numpy", "sympy"])
result = sBeta(var_tau, psi)
Then result
looks like this:
然后结果如下:
array([2.71828182845905**(-var_tau), 2.71828182845905**(-2*var_tau),
2.71828182845905**(-3*var_tau)], dtype=object)
Now I am able to use the sym.diff function like this:
现在我可以像这样使用sym.diff函数:
In [1]: sym.diff(result[1], var_tau)
Out[1]: -2.0*2.71828182845905**(-2*var_tau)
However, if I deal with var_tau like a variable it works very well.
但是,如果我像变量一样处理var_tau,它的效果非常好。