From dmurdoch@pair.com  Mon Jul  8 03:46:51 2002
From: dmurdoch@pair.com (Duncan Murdoch)
Date: Sun, 07 Jul 2002 22:46:51 -0400
Subject: integrate() (PR#1755)
In-Reply-To: <OFE7C3A3BF.88C529CC-ON86256BF0.000954B4@mmm.com>
References: <OFE7C3A3BF.88C529CC-ON86256BF0.000954B4@mmm.com>
Message-ID: <ktuhiuopd85go2tidr0b8hrq1pt0g8hg8a@4ax.com>


>This simply means that the function has to be properly vectorized. 

By the way, sometimes vectorizing is ugly.  Here's a simple function
that should vectorize any function that's designed to take a scalar as
the first argument.

vectorize <- function (f) 
{
  function(x,...)
  { 
     result <- rep(f(x[1],...),length(x))  # this sets type and length
     for (i in 2:length(x)) result[i] <- f(x[i],...)
     result
  }
}

It's slow, and it's ugly, but it lets the original failing example
work:

> integrate(function(x)1,0,1)
Error in integrate(function(x) 1, 0, 1) : evaluation of function gave
a result of wrong length
> integrate(vectorize(function(x)1),0,1)
1 with absolute error < 5.6e-15

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


integrate() (PR#1755)
Duncan Murdoch