Learn R Programming

fastQR (version 1.1.4)

qr_X: Reconstruct the original matrix from which the object was constructed \(X\in\mathbb{R}^{n\times p}\) from the Q and R matrices of the QR decomposition.

Description

returns the \(X\in\mathbb{R}^{n\times p}\) matrix.

Usage

qr_X(Q, R, pivot = NULL)

Value

returns the matrix \(X\).

Arguments

Q

either the reduced \(Q\in\mathbb{R}^{n\times p}\) of full \(Q\in\mathbb{R}^{n\times n}\), Q matrix obtained from the QR decomposition.

R

either the reduced \(R\in\mathbb{R}^{p\times p}\) of full \(R\in\mathbb{R}^{n\times p}\), R matrix obtained from the QR decomposition.

pivot

a vector of length \(p\), specifying the permutation of the columns of \(X\) applied during the QR decomposition process. The default is NULL if no pivoting has been applied.

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 QR decomposition with pivot
qr_res <- fastQR::qr_fast(X = X, pivot = TRUE)

## get the Q and R matrices
Q  <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = TRUE)
R  <- qr_R(qr = qr_res$qr, rank = qr_res$rank, complete = TRUE)
X1 <- qr_X(Q = Q, R = R, pivot = qr_res$pivot)
max(abs(X1 - X))

## get the full QR decomposition without pivot
qr_res <- fastQR::qr_fast(X = X, pivot = FALSE)

## get the Q and R matrices
Q  <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = p, complete = FALSE)
R  <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE)
X1 <- qr_X(Q = Q, R = R, pivot = NULL)
max(abs(X1 - X))

Run the code above in your browser using DataLab