Marius Hofert
2010-Dec-22 12:43 UTC
[R] How to integrate a function with additional argument being a vector or matrix?
Dear expeRts, I somehow don't see why the following does not work: integrand <- function(x, vec, mat, val) 1 # dummy return value A <- matrix(runif(16), ncol = 4) u <- c(0.4, 0.1, 0.2, 0.3) integrand(0.3, u, A, 4) integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, val = 4) I would like to integrate a function ("integrand") which gets an "x" value (the running variable), a vector ("vec"), a matrix ("mat"), and a value ("val"). This function returns a number (set to 1 here). Of course, pointwise evaluation works without flaws. But why does integration not work? I obtain: Error in integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, : evaluation of function gave a result of wrong length Cheers, Marius
Gerrit Eichner
2010-Dec-22 13:16 UTC
[R] How to integrate a function with additional argument beingavector or matrix?
On Wed, 22 Dec 2010, Marius Hofert wrote:> Dear expeRts, > > I somehow don't see why the following does not work: > > integrand <- function(x, vec, mat, val) 1 # dummy return value > A <- matrix(runif(16), ncol = 4) > u <- c(0.4, 0.1, 0.2, 0.3) > integrand(0.3, u, A, 4) > integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, val = 4) > > I would like to integrate a function ("integrand") which gets an "x" value (the running variable), a vector ("vec"), a matrix ("mat"), and a value ("val"). This function returns a number (set to 1 here). Of course, pointwise evaluation works without flaws. But why does integration not work? I obtain: > > Error in integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, : > evaluation of function gave a result of wrong lengthBecause your function isn't vectorized, i.e., does not *return* a vector. Instead, integrand <- function(x, vec, mat, val) rep( 1, length( x)) # dummy return value should work. Hth -- Gerrit> > Cheers, > > Marius > > ______________________________________________ > R-help at 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.
jim holtman
2010-Dec-22 13:18 UTC
[R] How to integrate a function with additional argument being a vector or matrix?
Change as follows:> integrand <- function(x, vec, mat, val) rep(1, length(x)) # dummy return value > A <- matrix(runif(16), ncol = 4) > u <- c(0.4, 0.1, 0.2, 0.3) > integrand(0.3, u, A, 4)[1] 1> integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, val = 4)1 with absolute error < 0.000000000000011>Notice in the help page for integrate: an R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error Your function has to return a vector of the same length as 'x' On Wed, Dec 22, 2010 at 7:43 AM, Marius Hofert <m_hofert at web.de> wrote:> Dear expeRts, > > I somehow don't see why the following does not work: > > integrand <- function(x, vec, mat, val) 1 # dummy return value > A <- matrix(runif(16), ncol = 4) > u <- c(0.4, 0.1, 0.2, 0.3) > integrand(0.3, u, A, 4) > integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, val = 4) > > I would like to integrate a function ("integrand") which gets an "x" value (the running variable), a vector ("vec"), a matrix ("mat"), and a value ("val"). This function returns a number (set to 1 here). Of course, pointwise evaluation works without flaws. But why does integration not work? I obtain: > > Error in integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, ?: > ?evaluation of function gave a result of wrong length > > Cheers, > > Marius > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Reasonably Related Threads
- latex() etc.: How to nicely format a matrix for a LaTeX document?
- Quiz: Who finds the nicest form of X_1^\prime?
- splom, plotmath: how to add three lines of information with alignment?
- Seed in 'parallel' vignette
- Why is format(10000, big.mark = "\\,") not 10\,000?