1

I would like to lamdify an array input with sympy. Here was my first attempt:

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.

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
Petermailpan
  • 103
  • 1
  • 10

2 Answers2

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 clash 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.

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])
Community
  • 1
  • 1
Cleb
  • 25,102
  • 20
  • 116
  • 151
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:

    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:

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.

Petermailpan
  • 103
  • 1
  • 10