Vectorized matrix cross-products t(X) V Y
or t(X) V^{-1} Y
.
crossprodV(X, Y = NULL, V, inverse = FALSE)
An array of size q x r x n
.
# problem dimensions
p <- 4
q <- 2
r <- 3
n <- 5
X <- array(rnorm(p*q*n), dim = c(p, q, n)) # vectorized
Y <- array(rnorm(p*r*n), dim = c(p, r, n)) # vectorized
V <- crossprod(matrix(rnorm(p*p), p, p)) # not vectorized (but positive definite)
crossprodV(X = X, V = V) # self cross-product
#> , , 1
#>
#> [,1] [,2]
#> [1,] 10.367538 8.404092
#> [2,] 8.404092 7.755207
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 9.559384 -4.642917
#> [2,] -4.642917 19.228313
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] 14.316374 7.504644
#> [2,] 7.504644 14.005141
#>
#> , , 4
#>
#> [,1] [,2]
#> [1,] 4.4446073 -0.9362056
#> [2,] -0.9362056 2.2544066
#>
#> , , 5
#>
#> [,1] [,2]
#> [1,] 5.30789 -10.53170
#> [2,] -10.53170 34.21031
#>
# cross-product with inverse matrix weight
crossprodV(X = X, V = V, Y = Y, inverse = TRUE)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] -2.850482 3.046161 -3.75646
#> [2,] -6.183404 6.304156 -8.08335
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 1.8653974 1.3956639 -3.6787381
#> [2,] -0.7525343 -0.1431919 0.1481828
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] -5.603478 -5.130036 -3.263566
#> [2,] 2.328833 1.918772 1.901262
#>
#> , , 4
#>
#> [,1] [,2] [,3]
#> [1,] 5.611043 -6.190782 2.911992
#> [2,] -7.696716 8.856299 -5.216997
#>
#> , , 5
#>
#> [,1] [,2] [,3]
#> [1,] -2.184113 -4.241434 0.04643557
#> [2,] 6.236424 9.067714 0.42464348
#>