QR Factorization

Learn how QR factorization of matrices works, using R, Rcpp, Armadillo, and Eigen.

QR decomposition

QR decomposition, or factorization, is a decomposition of a matrix 𝐴𝐴 into the product of an orthogonal matrix 𝑄𝑄 and an upper triangular matrix 𝑅𝑅:

𝐴=𝑄𝑅𝐴 = 𝑄𝑅

Orthogonal matrix

An orthogonal matrix is a square matrix where the columns and rows are orthogonal unit vectors (orthonormal vectors), such that:

𝑄𝑇𝑄=𝐼𝑄^𝑇 𝑄 = 𝐼

In this equation, II is the identity matrix. This leads to the equivalent characterizationβ€”a matrix QQ is orthogonal if its transpose is equal to its inverse.

Note: A vector is said to be normal if it has a length of one. Two vectors are said to be orthogonal if they’re at right angles to each other (in other words, their dot product is equal to zero). A set of vectors is said to be orthonormal if they’re all normal and each pair of vectors in the set is orthogonal.

This factorization is applicable to both square and generic rectangular matrices. In the latter case, matrix 𝐴𝐴 of size (π‘šΓ—π‘›)(π‘š \times 𝑛) with π‘š>π‘›π‘š > 𝑛 is decomposed in the product of 𝑄𝑄, which is size (π‘šΓ—m)(π‘š \times m), and 𝑅𝑅, which is (π‘šΓ—π‘›)(π‘š \times 𝑛), so that:

𝐴=𝑄(𝑅0)𝐴 = 𝑄 \dbinom{𝑅}{0}

QR factorization in R

Base R provides thw qr() function for QR matrix factorization. The qr() function returns a QR object with information on the decomposition. When we use qr.Q() and qr.R(), it is possible to retrieve the actual factorization matrices.

Given a matrix 𝐴𝐴, the QQ and RR matrices are returned.


Matrix A

[124005036]\begin{bmatrix} 1 & 2 & 4 \\ 0 & 0 & 5 \\ 0 & 3 & 6 \end{bmatrix} ...