The Matrix class is a class contained by all actual classes in the Matrix package. It is a virtual class.
class
Slots
Dim: an integer vector of length 2 giving the dimensions of the matrix.
Dimnames: a list of length 2. Each element must be NULL or a character vector of length equal to the corresponding element of Dim.
Methods
determinant: signature(x = "Matrix", logarithm = "missing"): and
determinant: signature(x = "Matrix", logarithm = "logical"): compute the (log) determinant of x. The method chosen depends on the actual Matrix class of x. Note that det also works for all our matrices, calling the appropriate determinant() method. The Matrix::det
is an exact copy of `base::det`, but in the correct namespace, and hence calling the S4-aware version of `determinant()`.).
diff: signature(x = "Matrix"): As diff()
for traditional matrices, i.e., applying `diff()` to each column.
dimnames<-: signature(x = "Matrix", value = "list"): set the dimnames to a list of length 2, see dimnames<-.
length: signature(x = "Matrix"): simply defined as prod(dim(x)) (and hence of mode "double").
show: signature(object = "Matrix"): show
method for `print`ing. For printing **sparse**
matrices, see `printSpMatrix`.
zapsmall: signature(x = "Matrix"): typically used for "dMatrix": round() matrix entries such that (relatively) very small entries become zero exactly.
image: signature(object = "Matrix"): draws an image of the matrix entries, using levelplot() from package lattice.
head: signature(object = "Matrix"): return only the head, i.e., the first few rows.
tail: signature(object = "Matrix"): return only the tail, i.e., the last few rows of the respective matrix.
as.matrix, as.array: signature(x = "Matrix"): the same as as(x, "matrix"); see also the note below.
as.vector: signature(x = "Matrix", mode = "missing"): as.vector(m) should be identical to as.vector(as(m, "matrix")), implemented more efficiently for some subclasses.
coerce: signature(from = "ANY", to = "Matrix"): This relies on a correct as.matrix() method for from.
There are many more methods that (conceptually should) work for all "Matrix" objects, e.g., colSums, rowMeans. Even base functions may work automagically (if they first call as.matrix() on their principal argument), e.g., apply, eigen, svd or kappa all do work via coercion to a traditional (dense) matrix.
Note
Loading the Matrix namespace overloads
as.matrix and as.array in the base
namespace by the equivalent of function(x) as(x, "matrix"). Consequently, as.matrix(m) or as.array(m) will properly work when m inherits from the "Matrix" class --- also for functions in package base and other packages. E.g., apply or outer can therefore be applied to "Matrix" matrices.
the classes dgeMatrix, dgCMatrix, and function Matrix for construction (and examples).
Methods, e.g., for kronecker.
Examples
slotNames("Matrix")cl <- getClass("Matrix")names(cl@subclasses)# more than 40 ..showClass("Matrix")#> output with slots and all subclasses(M <- Matrix(c(0,1,0,0),6,4))dim(M)diag(M)cm <- M[1:4,]+10*Diagonal(4)diff(M)## can reshape it even :dim(M)<- c(2,12)M
stopifnot(identical(M, Matrix(c(0,1,0,0),2,12)), all.equal(det(cm), determinant(as(cm,"matrix"), log=FALSE)$modulus, check.attributes=FALSE))