'sympy symbolic fft of 1/cosh(x)

I'm trying to compute the symbolic fft of $1/cosh(x)$ via

# %%
import sympy as sp 
import numpy as np 
import sympy.abc as spa

g = sp.fourier_transform(1/sp.cosh(spa.x), spa.x, spa.k)
print(g)
sp.plot(g)

but keep getting the error

> --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
> last)
> ~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\plotting\experimental_lambdify.py
> in __call__(self, args)
>     175             #The result can be sympy.Float. Hence wrap it with complex type.
> --> 176             result = complex(self.lambda_func(args))
>     177             if abs(result.imag) > 1e-7 * abs(result):
> 
> ~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\plotting\experimental_lambdify.py
> in __call__(self, *args, **kwargs)
>     271     def __call__(self, *args, **kwargs):
> --> 272         return self.lambda_func(*args, **kwargs)
>     273 
> 
> <string> in <lambda>(x0)

any ideas what this means?



Solution 1:[1]

In a isympy session:

In [40]: g = fourier_transform(1/cosh(x),x,k)

In [41]: g
Out[41]: 
                ?   1         ?
FourierTransform????????, x, k?
                ?cosh(x)      ?

In [42]: plot(g)
/usr/local/lib/python3.8/dist-packages/sympy/plotting/experimental_lambdify.py:188: UserWarning: The evaluation of the expression is problematic. We are trying a failback method that may still work. Please report this as a bug.
  return self.__call__(args)
-----------------------------------------------------------------------
TypeError                             Traceback (most recent call last)
File /usr/local/lib/python3.8/dist-packages/sympy/plotting/experimental_lambdify.py:176, in lambdify.__call__(self, args)
    174 try:
    175     #The result can be sympy.Float. Hence wrap it with complex type.
--> 176     result = complex(self.lambda_func(args))
    177     if abs(result.imag) > 1e-7 * abs(result):

File /usr/local/lib/python3.8/dist-packages/sympy/plotting/experimental_lambdify.py:272, in Lambdifier.__call__(self, *args, **kwargs)
    271 def __call__(self, *args, **kwargs):
--> 272     return self.lambda_func(*args, **kwargs)

File <string>:1, in <lambda>(x0)
....
File /usr/local/lib/python3.8/dist-packages/sympy/core/expr.py:345, in Expr.__float__(self)
    343 if result.is_number and result.as_real_imag()[1]:
    344     raise TypeError("Cannot convert complex to float")
--> 345 raise TypeError("Cannot convert expression to float")

TypeError: Cannot convert expression to float

I was going to complain about an incomplete traceback, but it is very long. Still you left off the final block, the one with the error

 Cannot convert expression to float

You print(g); why didn't you show that? Please show as much information as you can. It helps those of us who can't replicated your code (at the moment).


I haven't worked with this before, but even if I use subs to replace the variables with numbers, it still doesn't yield a numeric value. So there's nothing to plot.

In [52]: g.subs({x:2,k:3}).evalf()
Out[52]: 
                ?   1         ?
FourierTransform????????, 2, 3?
                ?cosh(2)      ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1