title: calculating double integrals in R quickly
body: <p>I'm looking for a solution for a double integral that is faster than </p>
  
  <pre><code>integrate(function(y) { 
     sapply(y, function(y) {
       integrate(function(x) myfun(x,y), llim, ulim)$value
     })
   }, llim, ulim)
  </code></pre>
  
  <p>with eg</p>
  
  <pre><code>myfun &lt;- function(x,y) cos(x+y)
  llim &lt;- -0.5
  ulim &lt;- 0.5
  </code></pre>
  
  <p>I found an <a href="http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1907&amp;context=cstech&amp;sei-redir=1&amp;referer=http://www.google.be/url?sa=t&amp;rct=j&amp;q=quad2d&amp;source=web&amp;cd=4&amp;ved=0CFEQFjAD&amp;url=http%253A%252F%252Fdocs.lib.purdue.edu%252Fcgi%252Fviewcontent.cgi%253Farticle%253D1907%2526context%253Dcstech&amp;ei=jvEWT6rcEYiL8gPjlJDzAg&amp;usg=AFQjCNH_F8u0HdaVM0v6WpR0MmKM_sjeAA#search=%22quad2d%22">old paper</a> that referred to a FORTRAN program called <code>quad2d</code>, 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. </p>
  
  <p>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.</p>