SparseM.ops {SparseM}R Documentation

Basic Linear Algebra for Sparse Matrices

Description

Basic linear algebra operations for sparse matrices, mostly of class matrix.csr.

Arguments

x

matrix of class matrix.csr.

y

matrix of class matrix.csr or a dense matrix or vector.

value

replacement values.

i, j

vectors of elements to extract or replace.

nrow

optional number of rows for the result.

lag

an integer indicating which lag to use.

differences

an integer indicating the order of the difference.

Details

Linear algebra operations for matrices of class matrix.csr are designed to behave exactly as for regular matrices. In particular, matrix multiplication, kronecker product, addition, subtraction and various logical operations should work as with the conventional dense form of matrix storage, as does indexing, rbind, cbind, and diagonal assignment and extraction. The method diag may be used to extract the diagonal of a matrix.csr object, to create a sparse diagonal see SparseM.ontology.

The function determinant computes the (log) determinant, of the argument, returning a "det" object as the base function. This is typically preferred over using the function det() which is a simple wrapper for determinant(), in a way it will work for our sparse matrices, as well. determinant() computes the determinant of the argument matrix. If the matrix is of class matrix.csr then it must be symmetric, or an error will be returned. If the matrix is of class matrix.csr.chol then the (pre-computed) determinant of the Cholesky factor is returned, i.e., the product of the diagonal elements.

The function norm, i.e. norm(x, type), by default computes the “sup” (or "M"aximum norm, i.e., the maximum of the matrix elements. Optionally, this type = "sup" (equivalently, type = "M") norm can be replaced by the Hilbert-Schmidt, type = "HS" or equivalently, type = "F" norm, or the type = "l1", norm. Note that for historical reasons, the default type differs from R's own norm(), see the examples using B, below. The "sup" === "M" and "HS" === "F" equivalences have been introduced in SparseM version 1.84.

References

Koenker, R and Ng, P. (2002). SparseM: A Sparse Matrix Package for R,
http://www.econ.uiuc.edu/~roger/research/home.html

See Also

slm for sparse linear model fitting. SparseM.ontology for coercion and other class relations involving our sparse matrix classes.

Examples

n1 <- 10
n2 <- 10
p <- 6
y <- rnorm(n1)
a <- round(rnorm(n1*p), 2)
a[abs(a) < 0.5] <- 0
A <- matrix(a, n1,p)
A.csr <- as.matrix.csr(A)

B <- matrix(c(1.5, 0,  0,  0, -1.4,    0,    0,   0,   0, -1.4,
              2,   0, -1,  0,  0,      2.1, -1.9, 1.4, 0,  0,
              0,-2.3,  0,  0, -1.9,    0,    0,   0,   0, -1.4,
              0,   0,  0,  0,  0,     -3,    0,   1.3, 0,  1.1,
              0,   0,  0,  0,  2,      0,    0,   0,  -1,  0,
              0,   0, -1.6,0,  0,      0,    0,   0,  -1.7,0),
            10L, 6L)
rcond(B) # 0.21 .. i.e., quite well conditioned
B.csr <- as.matrix.csr(B)
B.csr

## norm() : different 'type' for base R and  SparseM:
(nR <- vapply(c("1", "I", "F", "M", "2"), norm, 0, x = B))
##        1        I        F        M        2
## 8.400000 5.300000 7.372923 3.000000 4.464801
(nSpM <- vapply(c("sup","M",  "HS","F",  "l1"), norm, 0, x = B.csr))
##      sup         M        HS         F        l1
## 3.000000  3.000000  7.372923  7.372923 30.000000
stopifnot(all.equal(unname(nSpM[c("M", "F")]),
                    unname(nR  [c("M", "F" )]), tolerance = 1e-14))

# matrix transposition and multiplication
BtB <- crossprod(B) # == t(B) %*% B  {more efficiently}

A.csr %*% t(B.csr)
BtBs <- t(B.csr) %*% B.csr
BtBs
stopifnot(all.equal(    BtB,  as.matrix(BtBs),  tolerance = 1e-14),
          all.equal(det(BtB), print(det(BtBs)), tolerance = 1e-14))

# matrix  o  vector
stopifnot(all.equal(y %*%  A , y   %*% A.csr) ,
          all.equal(A %*% 1:6, A.csr %*% 1:6)
)

# kronecker product - via kronecker() methods:
A.csr %x% matrix(1:4,2,2)

[Package SparseM version 1.84-2 Index]