calculating double integrals in R quickly
Solution 1:
The cubature package does 2D (and N-D) integration using an adaptive algorithm. It should outperform simpler approaches for most integrands.
Solution 2:
The pracma
package that Joshua pointed out contains a version of quad2d
.
quad2d(myfun, llim, ulim, llim, ulim)
This gives the same answer, within numerical tolerance, as your loop, using the example function.
By default, with your example function, quad2d
is slower than the loop. If you drop n
down, you can make it faster, but I guess it depends upon how smooth your function is, and how much accuracy you are willing to sacrifice for speed.