# integrate() (PR#1755)

**Duncan Murdoch
**
dmurdoch@pair.com

*Sun, 07 Jul 2002 22:46:51 -0400*

>*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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._