[R] vectorizing the integrate function

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Aug 11 00:03:22 CEST 2019


Hello,

The following code might not be the best way of computing the integrals 
for all combinations of the arguments but it gets the job done and I 
believe it's readable code.


f2 <- function(a, b, c){
   integrate(function(x) {exp(-a*x^3 - b*x^2 - c*x)},
             lower = 0, upper = Inf)
}

f2_all_args <- function(a, b, c){
   lapply(a, function(.a)
     lapply(b, function(.b)
       lapply(c, function(.c)
         f2(.a, .b, .c)[1:2]
       )
     )
   )
}

a <- seq(from = 0, to = 1, by = 0.5)
b <- seq(from = 5, to = 10, by = 1)
m <- seq(from = 10, to = 20, by = 5)

res <- f2_all_args(a, b, m)

r <- array(0, c(3, 6, 3))
r.error <- array(0, c(3, 6, 3))

for(i in seq_along(a)){
   res_i <- res[[i]]
   for(j in seq_along(b)){
     res_i_j <- res_i[[j]]
     for(k in seq_along(m)){
       r[i, j, k] <- res_i_j[[k]]$value
       r.error[i, j, k] <- res_i_j[[k]]$abs.error
     }
   }
}


Hope this helps,

Rui Barradas


Às 21:14 de 10/08/19, ravi via R-help escreveu:
>   Bert,Thanks a lot for your help. I have a few follow-up questions (shown in the comment lines).  Numerical values and error for a (i) vector and (ii) array.
> 
> a <- seq(from=0,to=1,by=0.5)
> b <- seq(from=5,to=10,by=1)
> m <- seq(from=10,to=20,by=5)
> 
> f2 <- function(a,b,m)integrate(function(x){exp(-a*x^3-b*x^2-m*x)},lower=0,upper = Inf)
> fv <- Vectorize(f2,vectorize.args=c("a","b","m"),SIMPLIFY=TRUE)
> 
> r1 <- fv(a,b,m)
> # How to get just the numerical results as an array
> # for scalar values of a,b and m, I can get it with
> 
> #s <- integrate(f,0,Inf)$value
> # How do I get this for a vector
> 
> # Finally, if I want an array for all the values of a, b and m, how should I proceed?
> r <- array(0,c(3,6,3))
> r.error <- array(0,c(3,6,3))
> # I want the results of the vectorized integrate function in the above arrays. How do I extract these values?
> 
> Thanks,Ravi
> 
> 
>      On Saturday, 10 August 2019, 20:03:22 CEST, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>   
>   Ravi:
> First of all, you're calling Vectorize incorrectly. The first argument must be a function *name*, not a function call. Here's what you need to do (and thanks for the reprex -- wouldn't have been able to help without it!):
> f2 <- function(a,b,c)integrate(function(x){exp(-a*x^3-b*x^2-c*x)},lower =0,upper = Inf)
> fv <- Vectorize(f2,vectorize.args=c("a","b","c"),SIMPLIFY=TRUE)
> Second of all, as you may realize, the vectorization uses mapply() and so vectorizes the c(a,b,c) triplet "in parallel," which will "recycle" shorter arguments to the length of longer. Hence you will get 6 results from your example:
>> fv(a,b,m)
>               [,1]         [,2]         [,3]        [,4]         [,5]         [,6]
> value        0.09207851   0.0635289    0.04837997  0.08856628   0.06224138   0.04777941
> abs.error    3.365173e-08 3.108388e-06 1.00652e-09 3.284876e-09 1.796619e-08 6.348142e-09
> subdivisions 2            1            2           2            2            2
> message      "OK"         "OK"         "OK"        "OK"         "OK"         "OK"
> call         Expression   Expression   Expression  Expression   Expression   Expression
> 
> Cheers,Bert
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Sat, Aug 10, 2019 at 10:20 AM ravi via R-help <r-help using r-project.org> wrote:
> 
> Hi all,I am having some difficulties in vectorizing the integrate function. Let me explain with an example.
> a <- 10; b <- 3; c <- 4
> f <- function(x) {exp(-a*x^3-b*x^2-c*x)}
> integrate(f,0,Inf) # works fine
> 
> My difficulties start when I want to vectorize.
> 
> # attempts to vectorize fail
> a <- seq(from=0,to=1,by=0.5)
> b <- seq(from=5,to=10,by=1)
> m <- seq(from=10,to=20,by=5)
> f2 <- function(x,a,b,c) {exp(-a*x^3-b*x^2-m*x)}
> fv <- Vectorize(integrate(f2,0,Inf),vectorize.args=c("a","b","m"),SIMPLIFY=TRUE)
> 
> I want the result as a 3-d array with dimensions of the lengths of a, b and c. I have tried several variants but am not having much luck. Will appreciate any help that I can get.
> Thanks,Ravi Sutradhara
> 
> 
> 
> 
>          [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
> 
>    
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



More information about the R-help mailing list