\Header{QR.Auxiliaries}{Reconstruct the Q, R, or X Matrices from a QR Object}
\alias{qr.X}{QR.Auxiliaries}
\alias{qr.Q}{QR.Auxiliaries}
\alias{qr.R}{QR.Auxiliaries}
\keyword{algebra}{QR.Auxiliaries}
\begin{Usage}
\begin{verbatim}
qr.X(qrstr, complete = FALSE, ncol =)
qr.Q(qrstr, complete = FALSE, Dvec = 1)
qr.R(qrstr, complete = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{qrstr}] object representing a QR decomposition.  This will
typically have come from a previous call to \texttt{\Link{qr}} or
\texttt{\Link{lsfit}}.
\item[\texttt{complete}] logical expression of length 1.  Indicates whether an
arbitrary  orthogonal completion of the \eqn{\bold{Q}}{} or
\eqn{\bold{X}}{} matrices is to be made, or whether the \eqn{\bold{R}}{}
matrix is to be completed  by binding zero-value rows beneath the
square upper triangle.
\item[\texttt{ncol}] integer in the range \texttt{1:nrow(qrstr\$qr)}.  The number
of columns to be in the reconstructed \eqn{\bold{X}}{}.  The default
when \texttt{complete} is \texttt{FALSE} is the original \eqn{\bold{X}}{}
from which the qr object was constructed.  The default when
\texttt{complete} is \texttt{TRUE} is a square matrix with the original
\eqn{\bold{X}}{} in the first \texttt{ncol(X)} columns and an arbitrary
orthogonal completion (unitary completion in the complex case) in
the remaining columns.
\item[\texttt{Dvec}] vector (not matrix) of diagonal values.  Each column of
the returned \eqn{\bold{Q}}{} will be multiplied by the corresponding
diagonal value.
\end{ldescription}
\end{Arguments}
\begin{Description}
Returns the original matrix from which the object was constructed or
the components of the decomposition.\end{Description}
\begin{Value}
\texttt{qr.X} returns \eqn{\bold{X}}{}, the original matrix from
which the qr object was constructed.  If \texttt{complete} is
\texttt{TRUE} or the argument \texttt{ncol} is greater than
\texttt{ncol(X)}, additional columns from an arbitrary orthogonal
(unitary) completion of \texttt{X} are returned.

\texttt{qr.Q} returns \bold{Q}, the order-nrow(X) orthogonal (unitary)
transformation represented by qrstr.  If \texttt{complete} is
\texttt{TRUE}, \bold{Q} has \texttt{nrow(X)} columns.  If \texttt{complete}
is \texttt{FALSE}, \bold{Q} has \texttt{ncol(X)} columns.  When
\texttt{Dvec} is specified, each column of \bold{Q} is multiplied by the
corresponding value in \texttt{Dvec}.

\texttt{qr.R} returns \bold{R}, the upper triangular matrix such that
\texttt{X == Q \%*\% R}.  The number of rows of \bold{R} is
\texttt{nrow(X)} or \texttt{ncol(X)}, depending on whether \texttt{complete}
is \texttt{TRUE} or \texttt{FALSE}.\end{Value}
\begin{Examples}
\begin{ExampleCode}
data(savings)
p <- ncol(x <- savings[,-1]) # not the 'sr'
qrstr <- qr(x)   # dim(x) == c(n,p)
qrstr $ rank # = 4 = p
Q <- qr.Q(qrstr) # dim(Q) == dim(x)
R <- qr.R(qrstr) # dim(R) == ncol(x)
X <- qr.X(qrstr) # X == x
range(X - as.matrix(x))# ~ < 6e-12

## X == Q %*% R :
all((1 - X /( Q %*% R))< 100*.Machine$double.eps)#TRUE

dim(Qc <- qr.Q(qrstr, complete=TRUE)) # Square: dim(Qc) == rep(nrow(x),2)
all((crossprod(Qc) - diag(nrow(x))) < 10*.Machine $double.eps)

QD <- qr.Q(qrstr, D=1:p)      # QD == Q %*% diag(1:p)
all(QD - Q %*% diag(1:p)  < 8* .Machine$double.eps)

dim(Rc <- qr.R(qrstr, complete=TRUE)) # == dim(x)
dim(Xc <- qr.X(qrstr, complete=TRUE)) # square: nrow(x) ^ 2
all(Xc[,1:p] == X)
\end{ExampleCode}
\end{Examples}

