spam-operations.Rd
Basic linear algebra operations for sparse matrices
of class spam
.
Linear algebra operations for matrices of class
spam
are designed to behave exactly as for
regular matrices. In particular, matrix multiplication, transpose, 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 (see for example diag
).
Further functions with identical behavior are dim
and thus nrow
, ncol
.
The function norm
calculates the (matrix-)norm of the argument.
The argument type
specifies the l1
norm, sup
or max
norm (default), or the Frobenius or Hilbert-Schmidt
(frobenius/hs
) norm. Partial matching can be used. For example,
norm
is used to check for symmetry in the function chol
by
computing the norm of the difference between the matrix and its
transpose
The operator %d*%
efficiently multiplies a diagonal matrix (in
vector form) and a sparse matrix and is used for compatibility with the
package fields. More specifically, this method is used in the internal
functions of Krig
to make the code more readable. It avoids
having a branch in the source code to handle the diagonal or nondiagonal
cases. Note that this operator is not symmetric: a vector in
the left argument is interpreted as a diagonal matrix and a vector in
the right argument is kept as a column vector.
The operator %d+%
efficiently adds a diagonal matrix (in vector
form) and a sparse matrix, similarly to the operator %d+%
.
Some Fortran functions are based on https://github.com/johannesgerer/jburkardt-f/blob/master/sparsekit/sparsekit.html
spam
for coercion and other class relations involving the
sparse matrix classes.
# create a weight matrix and scale it:
if (FALSE) {
wij <- distmat
# with distmat from a nearest.dist(..., upper=TRUE) call
n <- dim(wij)[1]
wij@entries <- kernel( wij@entries, h) # for some function kernel
wij <- wij + t(wij) + diag.spam(n) # adjust from diag=FALSE, upper=TRUE
sumwij <- wij %*% rep(1,n)
# row scaling:
# wij@entries <- wij@entries/sumwij[ wij@colindices]
# col scaling:
wij@entries <- wij@entries/sumwij[ rep(1:n, diff(wij@rowpointers))]
}