R 中包含矩阵时的函数集成

Integration of function when matrix is included in R

我想集成一个看起来像

的功能
f <- function(x) 1.96 * sqrt(t(c(1,x)) %*% m %*% c(1,x))

其中 m 是

m <- matrix(c(3.855, -0.206, -0.206, 0.01), nrow = 2, ncol = 2, byrow = T)

由于我的函数内的矩阵乘法产生一个标量,对于 x 的任何值,这只是 f(x) 的一维积分。我怎样才能顺利解决这个问题?

只需 integrateVectorize:

integrate(Vectorize(f), lower = 0, upper = 1)

这是另一个没有 Vectorize 的选项(但我相信 的方法更 space 有效)

> ff <- function(x) 1.96 * sqrt(diag(t(rbind(1, x)) %*% m %*% rbind(1, x)))

> integrate(ff, lower = 0, upper = 1)
3.745299 with absolute error < 4.2e-14

其中 ff 已经是一个向量化函数,因为它是使用 rbind + diag 构造的以接受向量参数。