Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating double integrals in R quickly

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,y), llim, ulim)$value
   })
 }, llim, ulim)

with eg

myfun <- function(x,y) cos(x+y)
llim <- -0.5
ulim <- 0.5

I found an old paper that referred to a FORTRAN program called quad2d, but I couldn't find anything else but some help pages for matlab for the rest. So I'm looking for a C or FORTRAN library that can do double integrals quick (i.e. without the sapply loop), and that can be called from R. All ideas are very much appreciated, as long as they're all GPL compatible.

If the solution involves calling other functions from the libraries that are already shipped with R, I'd love to hear from them as well.

like image 919
Joris Meys Avatar asked Jan 18 '12 16:01

Joris Meys


People also ask

How will you write double integral over the region R?

As we mentioned before, when we are using rectangular coordinates, the double integral over a region R R denoted by ∬ R f ( x , y ) d A ∬ R f ( x , y ) d A can be written as ∬ R f ( x , y ) d x d y ∬ R f ( x , y ) d x d y or ∬ R f ( x , y ) d y d x .


1 Answers

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

like image 62
Jeffrey Sax Avatar answered Sep 22 '22 12:09

Jeffrey Sax