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_] = 
     Exp[-r^2 + 2*I*r*(Sin[x^2] + I*Sqrt[x])], {r, 0, Infinity}]^2*
     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.

  • $\begingroup$ f2 is essentially the DawsonF function. J.M. posted recently very efficient code for it in this post. $\endgroup$ – Henrik Schumacher Sep 27 '18 at 21:57
  • $\begingroup$ IRRC, J.M.'s function did not work with complex arguments. But maybe it can be adapted? @J.M. Your advice is needed here. (Well, you would have found this anyway...) $\endgroup$ – Henrik Schumacher Sep 27 '18 at 22:39
  • $\begingroup$ @Henrik, yes, the Dawson routine I posted will not work as is; however, I did write a Faddeeva function routine, and that has a relatively simple relationship with Dawson's integral. $\endgroup$ – J. M.'s technical difficulties Sep 27 '18 at 23:57
  • $\begingroup$ @J.M.issomewhatokay. Thanks for your help. Is it the correct relation between Faddeeva integral and DawsonF? $$\mathtt{DawsonF}(z)=\frac{{i}\sqrt{\pi}}{2}(e^{-z^2}-\mathtt{Faddeeva}(z))$$ $\endgroup$ – Zlelik Sep 28 '18 at 21:39
  • $\begingroup$ Yes, that's the correct identity. $\endgroup$ – J. M.'s technical difficulties Sep 29 '18 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.