Learn R Programming

qlcMatrix (version 0.9.2)

unfold: Unfolding of block matrices (sparse matricex)

Description

Utility function for some matrix manipulations for sparse block matrices.

Usage

unfold(X, colGroups, rowGroups = NULL)

Arguments

Value

When rowGroups != NULL then the result is a list of three matrices:UThe unfolded block matrixLThe left matrix for refolding the unfolded matrixRThe right matrix for refolding the unfolded matrixWhen rowGroups = NULL then the R matrix is not returned, and the refolding works with only the L matrix: X = L %*% U.

Details

For some sparse manipulation it turns out the profitable to `unfold' sparse block matrices, i.e. to separate the blocks into their own rows and columns. Each block can then separately be manipulated by using matrix products with diagonal matrices (see example below). For convenience, the function also returns two matrices to `refold' the unfolded matrix. Specifically, X = L %*% U %*% R

See Also

This is used in the sparse computation of assocCol to divide each block by a different N.

Examples

Run this code
# specially prepared block matrix. For illustration purpuse, this one is not sparse
( X <- Matrix( c( rep(c(1,1,1,1,2,2,3,3,3,4),3),
		rep(c(5,5,5,5,6,6,7,7,7,8),2)),10,5, sparse = TRUE) )

# this matrix has two column groups, and four row groups
# groups can be specified as sparse matrices, or as grouping vectors
colG <- ttMatrix(c(1,1,1,2,2))$M*1
rowG <- ttMatrix(c(1,1,1,1,2,2,3,3,3,4))$M*1

# unfold the matrix, with the result that each block has it's own rows/columns
# the $L and $R matrices can be used to refold the matrix to it's original state
( M <- unfold(X, colG, rowG) )

# unfold and refold back: result is identical
with(M, all.equal(X, L %*% U %*% R) )

# Unfolded, each block can be easily reached for computations using diagonal matrices
# for example, multiply each block by the sum of its cells, and then fold back again
# this is a trick to apply computations to blocks in sparse block matrices
sums <- drop(crossprod(kronecker(Diagonal(nrow(colG)),rowG)) %*% rowSums(M$U))
S <- Diagonal( x = sums )

with(M, L %*% S %*% U %*% R )

Run the code above in your browser using DataLab