I have a Mathematica code where I have to evaluate numerically thousands of integrals similar to this one
NIntegrate[
(Pi*Cos[(Pi*(-2*x + y))/(1 + y)] + (1 +
For this particular integral, the main culprit seems to be the integration over x
, probably due to the presence of both fast-decaying and highly-oscillating terms. Also, for this case, one can do the integration over x
analytically:
In[92]:=
-Integrate[(Pi*Cos[(Pi*(-2*x+y))/(1+y)]+(1+y)*(-Sin[(2*Pi*x)/(1+y)]+Sin[(Pi*(-2*x+y))/(1+y)]))/
(E^x* (1+y)),x]/.x->0//FullSimplify
Out[92]= (-\[Pi] (1+y) (2+Cos[(\[Pi] y)/(1+y)])+(2 \[Pi]^2+(1+y)^2) Sin[(\[Pi] y)/(1+y)])/
(4 \[Pi]^2+(1+y)^2)
(I discarded the value on the upper limit, since it is uniformly very small for y
). One can then integrate over y
numerically to get the right result:
In[94]:= NIntegrate[%,{y,0,100}]
Out[94]= 1.17525
A more general workaround would be to split the x
and y
integration like so:
ClearAll[f];
f[y_?NumericQ, xrange : {_?NumericQ, _?NumericQ}] :=
NIntegrate[(Pi*
Cos[(Pi*(-2*x + y))/(1 + y)] + (1 +
y)*(-Sin[(2*Pi*x)/(1 + y)] +
Sin[(Pi*(-2*x + y))/(1 + y)]))/(E^x*(1 + y)), {x, First@xrange, Last@xrange}];
and then we have:
In[105]:= NIntegrate[f[y,{0,100}],{y,0,100}]//Timing
Out[105]= {2.578,1.17525}
which is not blazing fast, but reasonably fast. This procedure won't always work so well (since the 2D integration grid resulting from this procedure won't always be optimal), but should work well enough when the integrand is such that integrations over x
and y
are sufficiently "decoupled".