How do I resolve the Lambdify name error in Sympy?

Asked 2 years ago, Updated 2 years ago, 83 views

If you receive an object that you spun Fourier_transform with Sympy as lambdify, a name error appears that FourierTransform is not defined even though numpy and sympy are applied to the module.

import sympy as sym
t,pw,f=sym.symbols('t,pw,f')
Ori=sym.exp(-(t**2)/(pw**2))
FT=sym.fourier_transform(Ori,t,f)
F=sym.lambdify([pw,f],FT,modules=['numpy', 'sympy'])
print(F(1,2))

In Output, NameError: name 'FourierTransform' is not defined.

Error The whole story is like this. Traceback (most recent call last):

File "", line 1, in runfile('G:/test.py', wdir='G://')

File "C:\Users\76rbn\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace)

File "C:\Users\76rbn\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "G:/test.py", line 6, in print(F(1,2))

File "", line 2, in _lambdifygenerated return (1.73205080756888e-5*sqrt(1/pw)FourierTransform(exp(-4*t2*log(2)/pw*2), t, f))

NameError: name 'FourierTransform' is not defined

Currently, I am using 1.3 for sympy, 3.7.1 for python, and 4.6.1 for conda. How can we solve this?

sympy python lambdify fourier_transform

2022-09-22 20:36

2 Answers

First, we need to look at two things.

FourierTransform is a problem that cannot be found in the module, so you can explicitly declare modules=[numpy,sym].

There will still be an error.

      1 def _lambdifygenerated(pw, f):
----> 2     return (FourierTransform(exp(-t**2/pw**2), t, f))

AttributeError: 'Mul' object has no attribute 'exp'

The above error is an error that occurs because there is no variable t. The function accepts only two items, but you can see that t, pw, and f variables are required within the actual function.

You can modify it in the form of putting three variables explicitly as shown below.

F=sym.lambdify([t, pw, f], FT, modules=[numpy, sym])
In [6]:import numpy 
   ...: ...: import sympy as sym
   ...: ...: t,pw,f=sym.symbols('t,pw,f')
   ...: ...: Ori=sym.exp(-(t**2)/(pw**2))
   ...: ...: FT=sym.fourier_transform(Ori,t,f)
   ...: ...: F=sym.lambdify([t, pw, f], FT, modules=[numpy, sym])

In [7]: print(F(1,2,3))
FourierTransform(0.778800783071405, 1, 3)


2022-09-22 20:36

https://docs.sympy.org/latest/_modules/sympy/integrals/transforms.html

First, look at the code of the link above. F(1,2,3) is a class object called FourierTransform.

For example, you can do the following...ValueError: Invalid limits given: ((1, -oo,oo),) error occurs. Take a look at the Fourier transform formula.

f = F(1,2,3)
f.doit()
ValueError: Invalid limits given: ((1, -oo, oo),)


2022-09-22 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.