I have functions similar to this:
f1[x_] = Sin[x^2] + I*Sqrt[x];
f2[x_] = Integrate[Exp[-r^2 + 2*I*r*x], {r, 0, Infinity}]
f3[x_, y_] = f2[f1[x]]^2*f2[f1[y]]^2 + Sqrt[f1[x]] // Simplify
I Sqrt[x] + Sin[x^2]
1/2 E^-x^2 Sqrt[π] + I DawsonF[x]
1/16 (E^(Sqrt[x] - I Sin[x^2])^2 Sqrt[π] + 2 I DawsonF[ I Sqrt[x] + Sin[x^2]])^2 (E^(Sqrt[y] - I Sin[y^2])^2 Sqrt[π] + 2 I DawsonF[I Sqrt[y] + Sin[y^2]])^2 + Sqrt[I Sqrt[x] + Sin[x^2]]
I need only to calculate it numerically, just to build some Plots, and use it to solve differential equations with NDSolve and algebraic equations with NSolve. if I try to run it 10000 times like this.
AbsoluteTiming[Table[f3[x, y], {x, 0, 100}, {y, 0, 100}];]
it takes around 3 seconds.
if I exclude nested functions like this
f4[x_, y_] =
Integrate[
Exp[-r^2 + 2*I*r*(Sin[x^2] + I*Sqrt[x])], {r, 0, Infinity}]^2*
Integrate[
Exp[-r^2 + 2*I*r*(Sin[x^2] + I*Sqrt[x])], {r, 0, Infinity}]^2 +
Sqrt[Sin[x^2] + I*Sqrt[x]]
it takes around 2 seconds. But I do not want to do it, because f1 and f2 are used in many other places as independent functions.
I tried to replace Integrate with NIntegrate and with NIntegrate it becomes much slower.
I tried to compile them
f3c = Compile[{{x, _Complex}, {y, _Complex}}, f3[x, y],
CompilationTarget -> "C"]
f4c = Compile[{{x, _Complex}, {y, _Complex}}, f4[x, y],
CompilationTarget -> "C"]
it becomes slower (2 times slower).
My question: Is there anyway, how to make it faster (f3 calculation)? Ideally I would like to increase performance 10-100 times, so it will take 0.03-0.3 seconds to calculate f3 10000 times.
Later I use this f3 function in other complicated functions and when I use NDSolve and NSolve it takes several days of calculations. My program is in the development stage and I need to change it frequently, so every time I need to wait several days to get the results, which is very uncomfortable and in total takes a lot of time.
f2is essentially theDawsonFfunction. J.M. posted recently very efficient code for it in this post. – Henrik Schumacher Sep 27 '18 at 21:57