I am trying to compute double integral for matrix and I want the result to be matrix
fn <- function(u, z){
h <- function(z) exp(sum(z*u))
res <- h(z)
uut <- u %*% t(u)
return(res * uut)
}
I <- cubature::cubintegrate(f = fn, lower = c(-3.5,-3.5), upper = c(4,4), method = "cuhre", z = 0.5)
As you can see the output from the fn is square matrix with u dimension and this what I expect but from some reason I only get a scalar from I using cubature::cubintegrate with argument fDim =1. if I change fDim = 4 I get vector with four values and I am not sure if this is right to create matrix from this vector.
Is there a way to compute the integral and get a square matrix? Your help is much appreciated.
Let's take a simple integrand, for which we know the value of the integral:
fn <- function(u){
u %*% t(u)
}
cubature::cubintegrate(f = fn, lower = c(0,0), upper = c(1,1), method = "cuhre",
fDim = 4)$integral
# [1] 0.3333333 0.2500000 0.2500000 0.3333333
# integral_0^1 integral_0^1 x^2 dxdy = 1/3
# integral_0^1 integral_0^1 x*y dxdy = 1/2*1/2 = 1/4
So it seems that fDim = 4
gives the good result.