1/cosh的Sympy符号FFT(x)

发布于 2025-01-24 07:10:09 字数 1133 浏览 0 评论 0原文

我正在尝试通过通过错误地计算出$ 1/cosh(x)$的符号FFT,

# %%
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)

但请继续遇到

> --------------------------------------------------------------------------- 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)

任何想法,这意味着什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-01-31 07:10:09

isympy会话中:

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

我要抱怨不完整的追溯,但很长。您仍然离开了最后一个块,一个带有错误的错误

 Cannot convert expression to float

> code> print(g);你为什么不展示呢?请显示尽可能多的信息。它可以帮助我们中那些无法复制您的代码的人(目前)。


我以前从未使用过这个问题,但是即使我使用subs用数字替换变量,它仍然不会产生数字值。所以没有什么可绘的。

In [52]: g.subs({x:2,k:3}).evalf()
Out[52]: 
                ⎛   1         ⎞
FourierTransform⎜───────, 2, 3⎟
                ⎝cosh(2)      ⎠

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)      ⎠
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文