Learn R Programming

fastQR (version 1.1.4)

qr_Q: Reconstruct the Q, matrix from a QR object.

Description

returns the \(Q\) matrix of the full QR decomposition. If \(r = \mathrm{rank}(X) < p\), then only the reduced \(Q \in \mathbb{R}^{n \times r}\) matrix is returned.

Usage

qr_Q(qr, tau, rank = NULL, complete = NULL)

Value

returns part or all of \(Q\), the order-\(n\) orthogonal (unitary) transformation represented by qr. If complete is TRUE, \(Q\) has \(n\) columns. If complete is FALSE, \(Q\) has \(p\) columns.

Arguments

qr

object representing a QR decomposition. This will typically have come from a previous call to qr.

tau

a vector of length \(ncol(X)\) which contains additional information on \(Q\). It corresponds to qraux from a previous call to qr.

rank

the rank of x as computed by the decomposition.

complete

logical flag (length 1). Indicates whether to compute the full \(Q \in \bold{R}^{n \times n}\) or the thin \(Q \in \bold{R}^{n \times p}\). If \(r = \mathrm{rank}(X) < p\), then only the reduced \(Q \in \mathbb{R}^{n \times r}\) matrix is returned.

References

golub_van_loan.2013fastQR

bjorck.2015fastQR

bjorck.2024fastQR

bernardi_etal.2024fastQR

Examples

Run this code
## generate sample data
set.seed(1234)
n <- 12
p <- 5
X <- matrix(rnorm(n * p), n, p)

## get the full QR decomposition with pivot
qr_res <- fastQR::qr_fast(X = X,
                          tol = sqrt(.Machine$double.eps),
                          pivot = TRUE)

## get the full Q matrix
Q1 <- qr_Q(qr_res$qr, qr_res$qraux, complete = TRUE)

## check the Q matrix (orthogonality)
max(abs(crossprod(Q1)-diag(1, n)))

## get the reduced Q matrix
Q2 <- qr_Q(qr_res$qr, qr_res$qraux, qr_res$rank, complete = FALSE)

## check the Q matrix (orthogonality)
max(abs(crossprod(Q2)-diag(1, p)))

Run the code above in your browser using DataLab