I want to numerically compute following integral (as fast as possible):
\begin{equation} f(x) = \left| \int e^{ \mathrm{i} p x / 2} f(p) \ \mathrm{d}p\right|^2 , \end{equation}
where $f(p)$ is given as a table on a discrete grid in p.
Example function:
f[p_] := Exp[-p^2] Exp[I p] Cos[p]^2 p^2
Generate the table on a discrete grid in p:
fp = Transpose[{Range[-5, 5, 0.01],
f[p] /. p -> Range[-5, 5, 0.01]}]
How I do it:
fx[fp_] :=
ParallelTable[{x, ((fp[[All, 1]][[-1]] - fp[[All, 1]][[1]])/
Length[fp[[All, 1]]])^2 Abs[
Sum[fp[[All, 2]][[n]] Exp[I fp[[All, 1]][[n]] x/2], {n, 1,
Length[fp[[All, 1]]]}]]^2}, {x, -20, 20, 0.1}];
fx[fp] works fine, but takes 16 seconds on my machine.
Is there a way to do this much faster?
Edit: I have an answer code to my own question. I think it might be helpful for others.
Fourierperforms a fast Fourier transform, perhaps that's what you are looking for. – Ulrich Neumann Jun 22 '20 at 11:38Fouriershould work. Of course, you have to adjust some constants. – Henrik Schumacher Jun 22 '20 at 12:05