Matrix-class | R Documentation |

The `Matrix`

class is a class contained by all actual
classes in the Matrix package. It is a “virtual” class.

Common to *all* matrix objects in the package:

`Dim`

:Object of class

`"integer"`

- the dimensions of the matrix - must be an integer vector with exactly two non-negative values.`Dimnames`

:list of length two; each component containing NULL or a

`character`

vector length equal the corresponding`Dim`

element.

- 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.- dim
`signature(x = "Matrix")`

: extract matrix dimensions`dim`

.- dim<-
`signature(x = "Matrix", value = "ANY")`

: where`value`

is integer of length 2. Allows to*reshape*Matrix objects, but only when`prod(value) == prod(dim(x))`

.- dimnames
`signature(x = "Matrix")`

: extract`dimnames`

.- 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`

.- 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.- as(x, "vector"), as(x, "numeric")
etc, similarly.

- 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`

.

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.

Douglas Bates bates@stat.wisc.edu and Martin Maechler

the classes `dgeMatrix`

,
`dgCMatrix`

, and function
`Matrix`

for construction (and examples).

Methods, e.g., for `kronecker`

.

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))

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.