Creates a sparse block-diagonal matrix.

bdiag.spam(...)

Arguments

...

Arrays to be binded together

Details

This is a small helper function to create block diagonal sparse matrices. In the two matrix case, bdiag.spam(A,B), this is equivalent to a complicated rbind(cbind(A, null), cbind(B, t(null))), where null is a null matrix of appropriate dimension.

It is recursively defined.

The arrays are coerced to sparse matrices first.

This function is similar to the function bdiag from the package Matrix. It is also similar to the function adiag from the package magic. However, here no padding is done and all the dimnames are stripped.

Value

Returns a spam matrix as described above.

See also

Author

Reinhard Furrer

Examples

A <- diag.spam(2, 4)           # 2*I4
B <- matrix(1,3,3)
AB <- bdiag.spam(A,B)

# equivalent to:
ABalt <- rbind(cbind( A, matrix(0,nrow(A),ncol(B))),
               cbind( matrix(0,nrow(B),ncol(A)), B))
         
norm(AB-ABalt)
#> [1] 0


# Matrices do not need to be square:
bdiag.spam(1,2:5,6)
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    2    0
#> [3,]    0    3    0
#> [4,]    0    4    0
#> [5,]    0    5    0
#> [6,]    0    0    6
#> Class 'spam' (32-bit)