Create a diagonal matrix object, i.e., an object inheriting from
diagonalMatrix
.
1 2 3 4 5 6 
n 
integer specifying the dimension of the (square) matrix. If
missing, 
x 
numeric or logical; if missing, a unit diagonal n x n matrix is created. 
uplo 
for 
shape 
string of 1 character, one of 
unitri 
optional logical indicating if a triangular result
should be “unittriangular”, i.e., with 
kind 
string of 1 character, one of 
cols 
integer vector with values from 
Diagonal()
returns an object of class
ddiMatrix
or ldiMatrix
(with “superclass” diagonalMatrix
).
.symDiagonal()
returns an object of class
dsCMatrix
or lsCMatrix
,
i.e., a sparse symmetric matrix. This can be
more efficient than Diagonal(n)
when the result is combined
with further symmetric (sparse) matrices, however not for
matrix multiplications where Diagonal()
is clearly preferred.
.sparseDiagonal()
, the workhorse of .symDiagonal
returns
a CsparseMatrix
(the resulting class depending
on shape
and kind
) representation of Diagonal(n)
,
or, when cols
are specified, of Diagonal(n)[, cols+1]
.
Martin Maechler
the generic function diag
for extraction
of the diagonal from a matrix works for all “Matrices”.
bandSparse
constructs a banded sparse matrix from
its nonzero sub/super  diagonals. band(A)
returns a
band matrix containing some sub/super  diagonals of A
.
Matrix
for general matrix construction;
further, class diagonalMatrix
.
1 2 3 4 5 6 7 8 9 10 11 12 13  Diagonal(3)
Diagonal(x = 10^(3:1))
Diagonal(x = (1:4) >= 2)#> "ldiMatrix"
## Use Diagonal() + kronecker() for "repeatedblock" 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 (201210) unitriangular
stopifnot(I4@diag == "U", all(I4 == diag(4)))

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.
All documentation is copyright its authors; we didn't write any of that.