# Is there any way to speed up this Mathematica evaluation?

I am interested in evaluation of the following integral

NIntegrate[Exp[-Cos[x1]-Cos[x2]-Cos[x1-x3]-Cos[x1-x4]-Cos[x2-x3]-Cos[x2-x4]-Cos[x3-x4]+2(Cos[2x1]+Cos[2x2]+Cos[2x3]+Cos[2x4])],{x1,0,2Pi},{x2,0,2Pi},{x3,0,2Pi},{x4,0,2Pi}]


This integral takes few seconds to evaluate and since I need to perform such integrals multiple times, I would like to speed up this if possible. Does any one have any idea how this would be achieved?

• Is there a pattern that relates all of the integrals you need to perform?
– JimB
Jun 30, 2021 at 23:16
• All the integrals I need to perform are the same as the one above, except that the cosines have prefactors of different floats other than -1 and +2. Jun 30, 2021 at 23:24
• By "prefactors" do you mean the general integrand is -a1 Cos[x1] - a2 Cos[x2] - a13 Cos[x1 - x3] - a14 Cos[x1 - x4] - a23 Cos[x2 - x3] - a24 Cos[x2 - x4] - a34 Cos[x3 - x4] + 2 (b1 Cos[2 x1] + b2 Cos[2 x2] + b3 Cos[2 x3] + b4 Cos[2 x4]) ? If so, then for any set of those constants, the result is 0.
– JimB
Jun 30, 2021 at 23:28
• Sorry, my bad. The integral is Exponential of the cosine functions. I missed the exponential part in the original question. So, yes in general I am interested in the integral of Exp[-a1 Cos[x1]- a2 Cos[x2]...] Jun 30, 2021 at 23:31

Simplify the integrand and perform symbolic (not numerical) integration:

Integrate[-Cos[x1] - Cos[x2] - Cos[x1 - x3] - Cos[x2 - x3] -
Cos[x1 - x4] - Cos[x2 - x4] - Cos[x3 - x4] +
2 (Cos[2 x1] + Cos[2 x2] + Cos[2 x3] + Cos[2 x4]),
{x1, 0, 2 Pi}, {x2, 0, 2 Pi}, {x3, 0, 2 Pi}, {x4, 0, 2 Pi}]


(* 0 *)

1.99 seconds on a Mac laptop.

You could also separate out terms that depend only upon x1 and integrate them with respect to x1, and likewise x2, and x3, and x4. (All trivial.) Then perform the integration of mixed-term portions.

• My bad. This integral is Exponential of the cosine factors. I have edited the question to make this clear. Just the cosine integrals would have been something rather easier and analytical. Jun 30, 2021 at 23:32

The Method->"LocalAdaptive" option does the job:

NIntegrate[Exp[-Cos[x1] - Cos[x2] - Cos[x1 - x3] - Cos[x1 - x4] -
Cos[x2 - x3] - Cos[x2 - x4] - Cos[x3 - x4] +
2 (Cos[2 x1] + Cos[2 x2] + Cos[2 x3] + Cos[2 x4])], {x1, 0,2 Pi}, {x2, 0, 2 Pi},
{x3, 0, 2 Pi}, {x4, 0, 2 Pi}, Method -> "LocalAdaptive"] // AbsoluteTiming


{1.93531, 353854.}

The Method->"GlobalAdaptive" option performs a close result plus a warning "NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained 353819.0213129086 and 52.68992886447849 for the integral and error estimates".

• NIntegrate[ Exp[-Cos[x1] - Cos[x2] - Cos[x1 - x3] - Cos[x1 - x4] - Cos[x2 - x3] - Cos[x2 - x4] - Cos[x3 - x4] + 2 (Cos[2 x1] + Cos[2 x2] + Cos[2 x3] + Cos[2 x4])], {x1, 0, 2 Pi}, {x2, 0, 2 Pi}, {x3, 0, 2 Pi}, {x4, 0, 2 Pi}, Method -> "AdaptiveMonteCarlo", PrecisionGoal -> 3, AccuracyGoal -> 3] // AbsoluteTiming performs {3.61452,347847.}. Jul 1, 2021 at 5:12
• NIntegrate[ Exp[-Cos[x1] - Cos[x2] - Cos[x1 - x3] - Cos[x1 - x4] - Cos[x2 - x3] - Cos[x2 - x4] - Cos[x3 - x4] + 2 (Cos[2 x1] + Cos[2 x2] + Cos[2 x3] + Cos[2 x4])], {x1, 0, 2 Pi}, {x2, 0, 2 Pi}, {x3, 0, 2 Pi}, {x4, 0, 2 Pi}, Method -> "LocalAdaptive", AccuracyGoal -> 4, PrecisionGoal -> 4] // AbsoluteTiming results in {0.397121, 363926.}. Jul 1, 2021 at 7:16