I'm trying to integrate numerically in 6 dimensions a very long expression and I read about the option to NIntegrate
a compiled function which should be faster. However, this is not the case. I have tried a simple example with just 3 variables.
g[x_, y_, z_] := Sin[x]^2 y Exp[z];
f = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, g[x, y, z]];
f2[x_?NumericQ, y_?NumericQ, z_?NumericQ] := f[x, y, z];
And now the integrals and the output:
NIntegrate[f2[x, y, z], {x, 0, 100}, {y, 0, 10}, {z, 0, 9}]//Timing
{2.335434,2.03437*10^7}
NIntegrate[g[x, y, z], {x, 0, 100}, {y, 0, 10}, {z, 0, 9}]//Timing
{0.316207,2.03437*10^7}
Can I do anything to improve the speed of the integral of the compiled function?
f2 = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, Sin[x]^2*y*Exp[z] 1, RuntimeOptions -> "EvaluateSymbolically" -> False]
and it helps a bit. But I suspect the rest of it comes fromNIntegrate
's symbolic preprocessing, which cannot be done for a purely numeric function. $\endgroup$NIntegrate
that would use a symbolic function for preprocessing, and its compiled version to calculate numerical values of the integrand. I tried to build something like this using constructs likebva2D2sub2ci[k_, bv_, p_, \[Psi]_] := If[NumericQ[bv] && NumericQ[p] && NumericQ[\[Psi]],...
but at best achieved the same speed as when using an uncompiled integrand. $\endgroup$