Reputation: 553
Is sympy able to compute fourier transforms quickly or do I need another software? Because I tried many things . I am pretty inexperienced in python and Sympy though . Is there a quicker way to do these Fourier transforms with python?
from sympy import poly, pi, I, pow
from sympy.abc import a,f, n, k
n = poly(a**2 + t**2)
k = t / n
fourier_transform(k , t, 2*pi*f)
And another example from Python 3.9 shell :
So what about the actual result?
Upvotes: 3
Views: 1481
Reputation: 1370
Currently, if the Fourier transform returns a PieceWise
expression, it throws a fuss and just returns an unevaluated expression instead. So SymPy can do the integral, it just isn't in a very nice form. Here is the result after manual integration.
You can use this function on your own to attain this result if you want this kind of answer.
def my_fourier_transform(f, t, s):
return integrate(f*exp(-2*S.Pi*I*t*s), (t, -oo, oo)).simplify()
One can see that the long condition has arg(a)
and arg(s)
which should be sign(a)
and sign(s)
respectively (assuming they are real-valued). After doing a similar thing in Wolfram Alpha, I think this is a correct result. It is just not a very nice one.
But I found a trick that helps. If SymPy struggles to simplify something, usually giving it stronger assumptions is the way to go if your main goal is just to get an answer. A lot of the time, the answer is still correct even if the assumptions don't hold. So we make the variables positive.
Note that SymPy does the transform differently to Wolfram Alpha as noted in the comment below. This was why my third argument is different.
from sympy import *
a, s, t = symbols('a s t', positive=True)
f = t / (a**2 + t**2)
# Wolfram computes integral(f(t)*exp(I*t*w))
# SymPy computes integral(f(t)*exp(-2*pi*I*s*t))
# So w = -2*pi*s
print(fourier_transform(f, t, -s))
# I*pi*exp(-2*pi*a*s)
Correct me if I'm wrong but according to Wikipedia:
So the Fourier transform of the Dirac Delta is 1.
Using the my_fourier_transfrom
defined above, we get
The first condition is always false. This is probably a failure on SymPy's part since this integral diverges. I assume it's because it can't decide whether it is oo
, -oo
or zoo
.
Upvotes: 1