| diagonalMatrix-class | R Documentation | 
Class "diagonalMatrix" is the virtual class of all diagonal matrices.
A virtual Class: No objects may be created from it.
diag:character string, either "U" or
"N", where "U" means ‘unit-diagonal’.
Dim:matrix dimension, and
Dimnames:the dimnames, a
list, see the Matrix class
description.  Typically list(NULL,NULL) for diagonal matrices.
Class "sparseMatrix", directly.
These are just a subset of the signature for which defined methods. Currently, there are (too) many explicit methods defined in order to ensure efficient methods for diagonal matrices.
signature(from = "matrix", to = "diagonalMatrix"): ... 
signature(from = "Matrix", to = "diagonalMatrix"): ... 
signature(from = "diagonalMatrix", to = "generalMatrix"): ... 
signature(from = "diagonalMatrix", to = "triangularMatrix"): ... 
signature(from = "diagonalMatrix", to = "nMatrix"): ... 
signature(from = "diagonalMatrix", to = "matrix"): ... 
signature(from = "diagonalMatrix", to = "sparseVector"): ... 
signature(x = "diagonalMatrix"): ... 
 and many more methods
signature(a = "diagonalMatrix", b, ...): is
trivially implemented, of course; see also solve-methods.
signature(x = "nMatrix"), semantically
equivalent to base function which(x, arr.ind).
signature(x = "diagonalMatrix"): all these
group methods return a "diagonalMatrix", apart from
cumsum() etc which return a vector also for
base matrix.
signature(e1 = "ddiMatrix", e2="denseMatrix"):
arithmetic and other operators from the Ops
group have a few dozen explicit method definitions, in order to
keep the results diagonal in many cases, including the following:
signature(e1 = "ddiMatrix", e2="denseMatrix"):
the result is from class ddiMatrix which is
typically very desirable.  Note that when e2 contains
off-diagonal zeros or NAs, we implicitly use 0 / x = 0, hence
differing from traditional R arithmetic (where 0 / 0
	\mapsto \mbox{NaN}), in order to preserve sparsity.
(object = "diagonalMatrix"): Returns
an object of S3 class "diagSummary" which is the summary of
the vector object@x plus a simple heading, and an
appropriate print method.
Diagonal() as constructor of these matrices, and
isDiagonal.
ddiMatrix and ldiMatrix are
“actual” classes extending "diagonalMatrix".
I5 <- Diagonal(5)
D5 <- Diagonal(x = 10*(1:5))
## trivial (but explicitly defined) methods:
stopifnot(identical(crossprod(I5), I5),
          identical(tcrossprod(I5), I5),
          identical(crossprod(I5, D5), D5),
          identical(tcrossprod(D5, I5), D5),
          identical(solve(D5), solve(D5, I5)),
          all.equal(D5, solve(solve(D5)), tolerance = 1e-12)
          )
solve(D5)# efficient as is diagonal
# an unusual way to construct a band matrix:
rbind2(cbind2(I5, D5),
       cbind2(D5, I5))
(lM <- Diagonal(x = c(TRUE,FALSE,FALSE)))
str(lM)#> gory details (slots)
crossprod(lM) # numeric
(nM <- as(lM, "nMatrix"))
crossprod(nM) # pattern sparse
(d2 <- Diagonal(x = c(10,1)))
str(d2)
## slightly larger in internal size:
str(as(d2, "sparseMatrix"))
M <- Matrix(cbind(1,2:4))
M %*% d2 #> `fast' multiplication
chol(d2) # trivial
stopifnot(is(cd2 <- chol(d2), "ddiMatrix"),
          all.equal(cd2@x, c(sqrt(10),1)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.