Dear all, I would really appreciate if somebody can help me to understand what does the phrase "Vectorize your function" mean? And what is the job of Vectorize() function in doing that? I have read many threads where experts suggest to Vectorize the function, which will speed up entire calculation (and more elegant ofcourse.) I used to think that vectorizing function means, to create a function such a way so that if input(s) are vector then this function will also return a vector output (without having any extra effort like use of 'for' loop etc.) Is this understanding correct? Here I have tried 2 essentially similar functions:> fn1 <- function(x,y,z) return(x+y+z) > fn2 <- Vectorize(function(x,y,z) return(x+y+z), SIMPLIFY=TRUE) > fn1(1:3, 4:6,5)[1] 10 12 14> fn2(1:3, 4:6,5)[1] 10 12 14 You see that, fn1() and fn2() is giving same answer (vectorized?) despite of the fact that fn1() is not wrapped within Vectorize() function. Therefore I want to know: what additional thing this Vectorize() function brings on the table? Thanks for your time.
On May 9, 2011, at 11:57 AM, Ron Michael wrote:> Dear all, I would really appreciate if somebody can help me to > understand what does the phrase "Vectorize your function" mean? And > what is the job of Vectorize() function in doing that? I have read > many threads where experts suggest to Vectorize the function, which > will speed up entire calculation (and more elegant ofcourse.) > > I used to think that vectorizing function means, to create a > function such a way so that if input(s) are vector then this > function will also return a vector output (without having any extra > effort like use of 'for' loop etc.) Is this understanding correct? > > Here I have tried 2 essentially similar functions: > >> fn1 <- function(x,y,z) return(x+y+z) >> fn2 <- Vectorize(function(x,y,z) return(x+y+z), SIMPLIFY=TRUE) >> fn1(1:3, 4:6,5) > [1] 10 12 14 >> fn2(1:3, 4:6,5) > [1] 10 12 14 > > > You see that, fn1() and fn2() is giving same answer (vectorized?) > despite of the fact that fn1() is not wrapped within Vectorize() > function. Therefore I want to know: what additional thing this > Vectorize() function brings on the table?That is because you used "+" which is "vectorized" to start with. Try is with "sum" and you will not be as "happy" > sum(a=1:3, b=4:6, cc=7:9) [1] 45 It removed all your structure. > Vsum <-function(...) {what <- list(...); lenlist <- length(what); y<-matrix(unlist(what), ncol=lenlist); apply(y,1,sum)} > Vsum(a=1:3, b=4:6, cc=7:9) [1] 12 15 18 # Does not recycle as I might have expected, although it does recycle > (Vsum( a=1:3, b=4:6, cc=7)) [1] 12 8 11 > (mapply(sum, a=1:3, b=4:6, cc=7:9)) # also works and recycles as expected: [1] 12 15 18 > (mapply(sum, a=1:3, b=4:6, cc=7)) [1] 12 14 16 # but _not_ VSum <- Vectorize(sum) for reasons not clear to me behaves the same as sum()> > Thanks for your time. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
On 09/05/2011 11:57 AM, Ron Michael wrote:> Dear all, I would really appreciate if somebody can help me to understand what does the phrase "Vectorize your function" mean? And what is the job of Vectorize() function in doing that? I have read many threads where experts suggest to Vectorize the function, which will speed up entire calculation (and more elegant ofcourse.) > > I used to think that vectorizing function means, to create a function such a way so that if input(s) are vector then this function will also return a vector output (without having any extra effort like use of 'for' loop etc.) Is this understanding correct?That's correct.> Here I have tried 2 essentially similar functions: > > > fn1<- function(x,y,z) return(x+y+z) > > fn2<- Vectorize(function(x,y,z) return(x+y+z), SIMPLIFY=TRUE) > > fn1(1:3, 4:6,5) > [1] 10 12 14 > > fn2(1:3, 4:6,5) > [1] 10 12 14 > > > You see that, fn1() and fn2() is giving same answer (vectorized?) despite of the fact that fn1() is not wrapped within Vectorize() function. Therefore I want to know: what additional thing this Vectorize() function brings on the table?Addition is already vectorized, so Vectorize does nothing but slow down your function. In other cases it is more helpful. For example, if you wanted to compute the residual sum of squares from fitting a mean to a vector of data, you might write y <- rnorm(100) # some fake data RSS <- function(mu) sum( (y-mu)^2 ) That works only if mu is a scalar, but Vectorize(RSS) would work for a vector of mu values. Duncan Murdoch
Seemingly Similar Threads
- Behavior or as.environment in function arguments/call (and force() behaviors...)
- weights in lm, glm (PR#9023)
- data.frame: adding a column that is based on ranges of values in another column
- distinct DISubprograms hindering sharing inlined subprogram descriptions
- distinct DISubprograms hindering sharing inlined subprogram descriptions