[R] efficiently finding the integrals of a sequence of functions
Hans W Borchers
hwborchers at googlemail.com
Sat Dec 10 16:21:09 CET 2011
JeffND <Zuofeng.Shang.5 <at> nd.edu> writes:
>
> Hi folks,
>
> I am having a question about efficiently finding the integrals of a list of
> functions.
We had the same discussion last month under the heading "performance of
adaptIntegrate vs. integrate", see
https://stat.ethz.ch/pipermail/r-help/2011-November/295260.html
It also depends on the accuracy you want to achieve. Adaptive methods will
be slower as the adaptation will be different in each single step for each
function, i.e. no vectorization here.
Non-adaptive Gaussian quadrature appears to be a good candidate. Assume you
have found grid points x_i and weights w_i for your interval [a, b], then if
F is the matrix with F_ij = f_j(x_i) amd the integrals will be computed all
at once with w %*% F .
Example: A function that returns x, x^2, ..., x^5 in columns
f <- function(x) cbind(x, x^2, x^3, x^4, x^5)
The grid points and weights for the interval [0, 1] are:
x <- c(0.025446, 0.129234, 0.297077, 0.500000, 0.702923, 0.870766, 0.974554)
w <- c(0.064742, 0.139853, 0.190915, 0.208980, 0.190915, 0.139853, 0.064742)
and the integrals for these five functions are
w %*% f(x) # 0.5 0.3333334 0.25 0.2 0.1666667
Functions to calculate Gaussian points and weights are mentioned in the thread
above.
Hans Werner
> To be specific,
> here is a simple example showing my question.
>
> Suppose we have a function f defined by
>
> f<-function(x,y,z) c(x,y^2,z^3)
>
> Thus, f is actually corresponding to three uni-dimensional functions
> f_1(x)=x, f_2(y)=y^2 and f_3(z)=z^3.
> What I am looking for are the integrals of these three functions f_1,f_2,f_3
> over some interval, say, (0,1).
> More specifically, the integrals \int_0^1 f_1(x) dx, \int_0^1 f_2(y) dy and
> \int_0^1 f_3(z) dz.
>
> For this simple example, of course we can do these three integrals one by
> one using
>
> integrate (f_1, lower=0, upper=1)
>
> However, in practice, I have a sequence of 5000 uni-dimensional functions
> and hope to find all of their
> integrals (over some regions), so using loops to do this one by one is not
> efficient.
>
> A possible idea is to convert the sequence of functions to a list of
> objects, and use sapply()
> which allow us to find the integrals in a fashion of vectorizing. But I
> don't know how to convert
> the above example function f to a list. In my research problem, I can only
> define the 5000 functions
> in a vectorizing way like
>
> f<-function(x1,x2,...,x5000) c(f1(x1),f2(x2),...,f5000(x5000))
>
> So how to convert it to a list will be a crucial step to efficiently get the
> integrals.
>
> Thanks for all the suggestions!
> Jeff
>
> --
> View this message in context: http://r.789695.n4.nabble.com/efficiently-finding-the-integrals-of-a-
sequence-of-functions-tp4179452p4179452.html
> Sent from the R help mailing list archive at Nabble.com.
>
>
More information about the R-help
mailing list