Matrix-class {Matrix} | R Documentation |
Virtual Class "Matrix" of Matrices
Description
The Matrix
class is a class contained by all actual
classes in the Matrix package. It is a “virtual” 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 ofDim
.
Methods
- determinant
signature(x = "Matrix", logarithm = "missing")
: and- determinant
signature(x = "Matrix", logarithm = "logical")
: compute the (\log
) determinant ofx
. The method chosen depends on the actual Matrix class ofx
. Note thatdet
also works for all our matrices, calling the appropriatedeterminant()
method. TheMatrix::det
is an exact copy ofbase::det
, but in the correct namespace, and hence calling the S4-aware version ofdeterminant()
.).- diff
signature(x = "Matrix")
: Asdiff()
for traditional matrices, i.e., applyingdiff()
to each column.- dim
signature(x = "Matrix")
: extract matrix dimensionsdim
.- dim<-
signature(x = "Matrix", value = "ANY")
: wherevalue
is integer of length 2. Allows to reshape Matrix objects, but only whenprod(value) == prod(dim(x))
.- dimnames
signature(x = "Matrix")
: extractdimnames
.- dimnames<-
signature(x = "Matrix", value = "list")
: set thedimnames
to alist
of length 2, seedimnames<-
.- length
signature(x = "Matrix")
: simply defined asprod(dim(x))
(and hence of mode"double"
).- show
signature(object = "Matrix")
:show
method forprint
ing. For printing sparse matrices, seeprintSpMatrix
.- 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 animage
of the matrix entries, usinglevelplot()
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 asas(x, "matrix")
; see also the note below.- as.vector
signature(x = "Matrix", mode = "missing")
:as.vector(m)
should be identical toas.vector(as(m, "matrix"))
, implemented more efficiently for some subclasses.- as(x, "vector"), as(x, "numeric")
etc, similarly.
- coerce
signature(from = "ANY", to = "Matrix")
: This relies on a correctas.matrix()
method forfrom
.
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.
Author(s)
Douglas Bates bates@stat.wisc.edu and Martin Maechler
See Also
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))