When I evaluate the following expression in Mathematica, it takes so much time that I don't want to wait for the evaluation to complete. So I think that there must be a better approach.

wewint[lx_?NumericQ, ly_?NumericQ] := NIntegrate[
    Cos[lx]*Cos[qx] + Sin[lx]*Sin[qx], {qx, -Pi, Pi}, {qy, -Pi, Pi}, 
    Method -> Automatic];
NIntegrate[wewint[lx, ly], {lx, -Pi, Pi}, {ly, -Pi, Pi}, Method -> Automatic]

If I use Cos[lx-qx] instead of (Cos[lx]*Cos[qx] + Sin[lx]*Sin[qx]), the effect is the same. In reality, the expression I am working with is much more complicated and it couldn't be calculated analytically.

  • 2
    $\begingroup$ Should the integrand be Cos[lx]*Cos[qx] + Sin[lx]*Sin[qx] or Cos[lx]*Cos[qx] + Sin[ly]*Sin[qy]? If the former, than you are performing two unnecessary integrations. Also, as each point evaluated in the second integral requires a full evaluation of the first integral, it will take longer as there is a full init and tear down stage per evaluation. Can they be combined into one? This will eliminate the extra overhead. $\endgroup$ – rcollyer Dec 10 '12 at 16:48

You may have some luck playing with various methods. It is also important to decrease the number of integrations - one 4-dimensional integral can often compute much faster than 2 two-dimensional ones (meaning 2d integral of 2d integrals). So, we have:


(* {0.619141,0.}  *)

while for example


(* {1.188476,389.636}  *)



(*  {4 \[Pi]^4,389.636}  *)

just to confirm.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.