Diagonal {Matrix} | R Documentation |
Construct a Diagonal Matrix
Description
Construct a formally diagonal Matrix
,
i.e., an object inheriting from virtual class
diagonalMatrix
(or, if desired, a mathematically diagonal
CsparseMatrix
).
Usage
Diagonal(n, x = NULL, names = FALSE)
.sparseDiagonal(n, x = NULL, uplo = "U", shape = "t", unitri = TRUE, kind, cols)
.trDiagonal(n, x = NULL, uplo = "U", unitri = TRUE, kind)
.symDiagonal(n, x = NULL, uplo = "U", kind)
Arguments
n |
integer indicating the dimension of the (square) matrix.
If missing, then |
x |
numeric or logical vector listing values for the diagonal
entries, to be recycled as necessary. If |
names |
either |
uplo |
one of |
shape |
one of |
unitri |
logical indicating if a formally triangular result with
ones on the diagonal should be formally unit triangular, i.e.,
with |
kind |
one of |
cols |
optional integer vector with values in |
Value
Diagonal()
returns an object inheriting from virtual class
diagonalMatrix
.
.sparseDiagonal()
returns a CsparseMatrix
representation of Diagonal(n, x)
or, if cols
is given,
of Diagonal(n, x)[, cols+1]
. The precise class of the result
depends on shape
and kind
.
.trDiagonal()
and .symDiagonal()
are simple wrappers,
for .sparseDiagonal(shape = "t")
and
.sparseDiagonal(shape = "s")
, respectively.
.sparseDiagonal()
exists primarily to leverage efficient
C-level methods available for CsparseMatrix
.
Author(s)
Martin Maechler
See Also
the generic function diag
for extraction
of the diagonal from a matrix works for all “Matrices”.
bandSparse
constructs a banded sparse matrix from
its non-zero sub-/super - diagonals. band(A)
returns a
band matrix containing some sub-/super - diagonals of A
.
Matrix
for general matrix construction;
further, class diagonalMatrix
.
Examples
Diagonal(3)
Diagonal(x = 10^(3:1))
Diagonal(x = (1:4) >= 2)#-> "ldiMatrix"
## Use Diagonal() + kronecker() for "repeated-block" matrices:
M1 <- Matrix(0+0:5, 2,3)
(M <- kronecker(Diagonal(3), M1))
(S <- crossprod(Matrix(rbinom(60, size=1, prob=0.1), 10,6)))
(SI <- S + 10*.symDiagonal(6)) # sparse symmetric still
stopifnot(is(SI, "dsCMatrix"))
(I4 <- .sparseDiagonal(4, shape="t"))# now (2012-10) unitriangular
stopifnot(I4@diag == "U", all(I4 == diag(4)))