[R] vectorizing the integrate function

ravi rv15| @end|ng |rom y@hoo@@e
Mon Aug 12 13:19:22 CEST 2019

 Hi all,I would like to thank every one who has responded to my question. I have received valuable help.Linus, the function that I gave here is just a toy function. I think that is possible even to find an analytical solution. I chose that just to include many constant parameters. I wanted to find out how I can vectorize. I have obtained nice answers from Bert and I appreciate the help. I knew even previously that the vectorizing primarily improves the readability of the code. Thanks Bert for making it crystal clear that the looping is still at the interpreted level of R.Thanks,Ravi
    On Sunday, 11 August 2019, 12:24:43 CEST, Linus Chen <linus.l.chen using gmail.com> wrote:  
 Dear ravi,

In your example, the function "f" is linear to a, b, and c.
Is this the general case in your task?

If it is, you can save  *lots* of computation taking advantage of the linearity.


On Sat, 10 Aug 2019 at 19:20, 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]]

More information about the R-help mailing list