[R] vectorizing the integrate function
Linus Chen
||nu@@|@chen @end|ng |rom gm@||@com
Sun Aug 11 12:24:04 CEST 2019
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.
Lei
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.
More information about the R-help
mailing list