\HeaderA{diag}{Matrix Diagonals}{diag}
\aliasA{diag<\Rdash}{diag}{diag<.Rdash.}
\keyword{array}{diag}
\begin{Description}\relax
Extract or replace the diagonal of a matrix,
or construct a diagonal matrix.
\end{Description}
\begin{Usage}
\begin{verbatim}
diag(x = 1, nrow, ncol)
diag(x) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a matrix, vector or 1D array.
\item[\code{nrow, ncol}] Optional dimensions for the result.
\item[\code{value}] either a single value or a vector of length equal to that
of the current diagonal.  Should be of a mode which can be coerced
to that of \code{x}.
\end{ldescription}
\end{Arguments}
\begin{Value}
If \code{x} is a matrix then \code{diag(x)} returns the diagonal of
\code{x}.  The resulting vector will have \code{\LinkA{names}{names}} if the
matrix \code{x} has matching column and row names.

If \code{x} is a vector (or 1D array) of length two or more,
then \code{diag(x)} returns a diagonal matrix whose diagonal is \code{x}.

If \code{x} is a vector of length one then \code{diag(x)} returns an
identity matrix of order the nearest integer to \code{x}.  The
dimension of the returned matrix can be specified by \code{nrow} and
\code{ncol} (the default is square).

The replacement form sets the diagonal of the matrix \code{x} to the
given value(s).
\end{Value}
\begin{Note}\relax
Using \code{diag(x)} can have unexpected effects if \code{x} is a
vector that could be of length one. Use \code{diag(x, nrow =
    length(x))} for consistent behaviour.
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{upper.tri}{upper.tri}}, \code{\LinkA{lower.tri}{lower.tri}}, \code{\LinkA{matrix}{matrix}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(stats)
dim(diag(3))
diag(10,3,4) # guess what?
all(diag(1:3) == {m <- matrix(0,3,3); diag(m) <- 1:3; m})

diag(var(M <- cbind(X = 1:5, Y = stats::rnorm(5))))
#-> vector with names "X" and "Y"

rownames(M) <- c(colnames(M),rep("",3));
M; diag(M) #  named as well
\end{ExampleCode}
\end{Examples}

