Re: [R] vectorizing the integrate function

2019-08-12 Thread ravi via R-help
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 fi

Re: [R] vectorizing the integrate function

2019-08-11 Thread Linus Chen
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 wrote: > > Hi all,I am having some difficulties in v

Re: [R] vectorizing the integrate function

2019-08-10 Thread Bert Gunter
Ravi: I believe you are under a misconception. The Vectorize() function vectorizes the function call, **but it does not vectorize the computation by moving loops down to the C level**, which is typically what is meant when it is recommended that R users use inbuilt vectorized functions when possib

Re: [R] vectorizing the integrate function

2019-08-10 Thread ravi via R-help
Bert,Thanks a lot. This meets all my expectations for the moment. Thanks.Ravi On Sunday, 11 August 2019, 00:52:02 CEST, Bert Gunter wrote: Cleaner, I think, is to use ?expand.grid and ?do.call, noting that any n-column data frame is also an n-component list. Using your example as befo

Re: [R] vectorizing the integrate function

2019-08-10 Thread ravi via R-help
Rui,Thanks for your help in getting the result with for loops. That is useful, but I have a complicated integral and I am interested in improving the performance with the benefit of vectorization. In the solution from Bert, I would like to know how I can extract the numerical vector from the fu

Re: [R] vectorizing the integrate function

2019-08-10 Thread Bert Gunter
Cleaner, I think, is to use ?expand.grid and ?do.call, noting that any n-column data frame is also an n-component list. Using your example as before: > 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"),SIMPLI

Re: [R] vectorizing the integrate function

2019-08-10 Thread Rui Barradas
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) }

Re: [R] vectorizing the integrate function

2019-08-10 Thread ravi via R-help
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^

Re: [R] vectorizing the integrate function

2019-08-10 Thread Bert Gunter
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)},low

[R] vectorizing the integrate function

2019-08-10 Thread ravi via R-help
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=