Re: [R] efficiently finding the integrals of a sequence of functions

2011-12-11 Thread JeffND
Thanks,Hans!

I agree that this is a good way of solving this problem. 

Here is another way. Instead of defining a vector of uni-dimensional
functions and trying to integrating
each component (a uni-dimensional function), we can do something below

my.integrand-function(x,k)
{
return(f[x,k]) ## use matrix to represent the sequence of uni-dimensional
functions
}

my.integral-function(k) ## k=1,...,5000 denotes the function labels
{
return(integrate(my.integrand,lower=...,upper=...,k)$value)
}

When calculating the integrals, just perform

sapply(1:5000, my.integral)

This is a way of avoiding loops but the computing time needs to be carefully
examined.

Jeff



--
View this message in context: 
http://r.789695.n4.nabble.com/efficiently-finding-the-integrals-of-a-sequence-of-functions-tp4179452p4183323.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


[R] efficiently finding the integrals of a sequence of functions

2011-12-10 Thread JeffND
Hi folks,

I am having a question about efficiently finding the integrals of a list of
functions. 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.

__
R-help@r-project.org mailing list
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.


Re: [R] efficiently finding the integrals of a sequence of functions

2011-12-10 Thread Hans W Borchers
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.50, 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.334 0.25 0.2 0.167

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.
 


__
R-help@r-project.org mailing list
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.