В Tue, 11 Jun 2024 18:44:08 +0000
"Levine, Michael" <mlev...@purdue.edu> пишет:

> Let us say we have a function
> 
> F <- function(x){ body of the function}
> 
> Where x is, in general, a d by 1 vector with d>1.  Now I want to
> integrate out some of the coordinates of x, e.g. x[1] or x[2] or both
> of them etc. I'm well aware of how to integrate out e.g. y if a
> function is defined as f <- function (x,y) {body of the function}
> where y is a scalar.

The reason integrate() wants a separate function argument for the
integration coordinate is so that it could give the function a vector
of different values of the variable and receive a vector of the same
length containing the corresponding values of the function.

If the problem is small enough to make performance considerations
irrelevant, you can use Vectorize to make a function compatible with
integrate() from your function F:

x <- x0
z <- z0
Fiy <- Vectorize(function(y) F(c(x, y, z)))
integrate(Fiy, ymin, ymax)

The resulting function Fiy will accept a vector of values for y and
translate it into multiple calls to F with a three-element vector
argument as it expects.

Achieving better performance will require rewriting the function F to
be "vectorised", i.e. to accept vectors for arguments and return a
vector of the same length.

-- 
Best regards,
Ivan

______________________________________________
R-help@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.

Reply via email to