calculating double integrals in R quickly

后端 未结 2 1667
夕颜
夕颜 2020-11-29 06:22

I\'m looking for a solution for a double integral that is faster than

integrate(function(y) { 
   sapply(y, function(y) {
     integrate(function(x) myfun(x         


        
相关标签:
2条回答
  • 2020-11-29 06:33

    The cubature package does 2D (and N-D) integration using an adaptive algorithm. It should outperform simpler approaches for most integrands.

    0 讨论(0)
  • 2020-11-29 06:50

    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.

    0 讨论(0)
提交回复
热议问题