Post · 7 min read· pp. 656–659

What Is a QR Factorization?

A QR factorization of a rectangular matrix A∈Rm×nA\in\mathbb{R}^{m\times n} with m≥nm\ge n is a factorization A=QRA = QR with Q∈Rm×mQ\in\mathbb{R}^{m\times m} orthonormal and R∈Rm×nR\in\mathbb{R}^{m\times n} upper trapezoidal. The RR factor has the form R=[R10]R = \left[\begin{smallmatrix}R_1\\ 0\end{smallmatrix}\right], where R1R_1 is n×nn\times n and upper triangular. Partitioning QQ conformably with RR we have

\notag A = QR = \begin{array}[b]{@{\mskip-20mu}c@{\mskip0mu}c@{\mskip-1mu}c@{}} & \mskip10mu\scriptstyle n & \scriptstyle m-n \\ \mskip15mu \begin{array}{r} \scriptstyle m \end{array}~ & \multicolumn{2}{c}{\mskip-15mu \left[\begin{array}{c@{~}c@{~}} Q_1 & Q_2 \end{array}\right] } \end{array} \mskip-10mu \begin{array}[b]{@{\mskip-25mu}c@{\mskip-20mu}c@{}} \scriptstyle n \\ \multicolumn{1}{c}{ \left[\begin{array}{@{}c@{}} R_1\\ 0 \end{array}\right]} & \mskip-12mu\ \begin{array}{l} \scriptstyle n \\ \scriptstyle m-n \end{array} \end{array} = Q_1 R_1.

There are therefore two forms of QR factorization:

  • A=QRA = QR is the full QR factorization,
  • A=Q1R1A = Q_1R_1 is the reduced (also called economy-sized, or thin) QR factorization.

To prove the existence of a QR factorization note that if AA has full rank then AT ⁣AA^T\!A is symmetric positive definite. Since A=Q1R1A = Q_1R_1 implies AT ⁣A=R1TQ1TQ1R1=R1TR1A^T\!A = R_1^TQ_1^TQ_1R_1 = R_1^TR_1, we can take R1R_1 to be the Cholesky factor of AT ⁣AA^T\!A and then define Q1=AR1−1Q_1 = AR_1^{-1}. The resulting Q1Q_1 has orthonormal columns because

Q1TQ1=R1−TATAR1−1=R1−TR1TR1R1−1=I.\notag Q_1^TQ_1 = R_1^{-T} A^T A R_1^{-1} = R_1^{-T} R_1^T R_1 R_1^{-1} = I.

Therefore when AA has full rank there is a unique reduced QR factorization if we require R1R_1 to have positive diagonal elements. (Without this requirement we can multiply the iith column of QQ and the iith row of RR by −1-1 and obtain another QR factorization.)

When AA has full rank the columns of Q1Q_1 span the column space (or range) of AA. Indeed Ax=Q1R1x=Q1(R1x)Ax = Q_1R_1x = Q_1(R_1x) implies range(A)⊆range(Q1)\mathrm{range}(A) \subseteq \mathrm{range}(Q_1) while Q1x=Q1R1⋅R1−1x=:AyQ_1x = Q_1R_1\cdot R_1^{-1}x =: Ay implies range(Q1)⊆range(A)\mathrm{range}(Q_1) \subseteq \mathrm{range}(A), so range(Q1)=range(A)\mathrm{range}(Q_1) = \mathrm{range}(A). Furthermore, QTA=RQ^TA = R gives Q2TA=0Q_2^TA = 0, so the columns of Q2Q_2 span the null space of ATA^T.

The QR factorization provides a way of orthonormalizing the columns of a matrix. An alternative is provided by the polar decomposition A=UHA = UH, where UU has orthonormal columns and HH is positive semidefinite. The orthogonal polar factor UU is the closest matrix with orthonormal columns to AA in any unitarily invariant norm, but it is more expensive to compute than the QQ factor.

There are three standard ways of computing a QR factorization.

Gram–Schmidt orthogonalization computes the reduced factorization. It has the disadvantage that in floating-point arithmetic the computed Q^\widehat{Q} is not guaranteed to be orthonormal to the working precision. The modified Gram–Schmidt method (a variation of the classical method) is better behaved numerically in that ∥Q^TQ^−I∥F≤c1(m,n)κ2(A)u\|\widehat{Q}^T\widehat{Q} - I\|_F \le c_1(m,n)\kappa_2(A)u for some constant c1(m,n)c_1(m,n), where uu is the unit roundoff, so the loss of orthogonality is bounded.

Householder QR factorization and Givens QR factorization both construct QTQ^T as a product of orthogonal matrices that are chosen to reduce AA to upper trapezoidal form. In both methods, at the start of the kkth stage we have

\notag \qquad\qquad\qquad\qquad A^{(k)} = Q_{k-1}^T A = \begin{array}[b]{@{\mskip35mu}c@{\mskip20mu}c@{\mskip-5mu}c@{}c} \scriptstyle k-1 & \scriptstyle 1 & \scriptstyle n-k & \\ \multicolumn{3}{c}{ \left[\begin{array}{c@{\mskip10mu}cc} R_{k-1} & y_k & B_k \\ 0 & z_k & C_k \end{array}\right]} & \mskip-12mu \begin{array}{c} \scriptstyle k-1 \\ \scriptstyle m-k+1 \end{array} \end{array}, \qquad\qquad\qquad\qquad (*)

where Rk−1R_{k-1} is upper triangular and Qk−1Q_{k-1} is a product of Householder transformations or Givens rotations. Working on A(k)(k:m,k:n)A^{(k)}(k:m,k:n) we now apply a Householder transformation or n−kn-k Givens rotations in order to zero out the last n−kn-k elements of zkz_k and thereby take the matrix one step closer to upper trapezoidal form.

Householder QR factorization is the method of choice for general matrices, but Givens QR factorization is preferred for structured matrices with a lot of zeros, such as upper Hessenberg matrices and tridiagonal matrices.

Both these methods produce QQ in factored form and if the product is explicitly formed they yield a computed Q^\widehat{Q} that is orthogonal to the working precision, that is, ∥Q^TQ^−I∥F≤c2(m,n)u\|\widehat{Q}^T\widehat{Q} - I\|_F \le c_2(m,n)u, for some constant c2c_2.

Modified Gram–Schmidt, Householder QR, and Givens QR all have the property that there exists an exactly orthogonal QQ such that the computed R^\widehat{R} satisfies

A+ΔA=QR^,∥ΔA∥F≤c3(m,n)u∥A∥F,\notag A + \Delta A = Q \widehat{R}, \quad \|\Delta A\|_F \le c_3(m,n)u \|A\|_F,

for some constant c3c_3.

Another way of computing a QR factorization is by the technique in the existence proof above, via Cholesky factorization of AT ⁣AA^T\!A. This is known as the Cholesky QR algorithm and it has favorable computational cost when m≫nm \gg n. In its basic form, this method is not recommended unless AA is extremely well conditioned, because the computed Q^\widehat{Q} is far from orthonormal for ill conditioned matrices. The method can be made competitive with the others either by using extra precision or by iterating the process.

Column Pivoting and Rank-Revealing QR Factorization

In practice, we often want to compute a basis for the range of AA when AA is rank deficient. The basic QR factorization may not do so. Householder QR factorization with column pivoting reveals rank deficiency by incorporating column interchanges. At the kkth stage, before applying a Householder transformation to (∗)(*), the column of largest 22-norm of CkC_k, the jjth say, is determined, and if its norm exceeds that of zKz_K then the kkth and (k+j)(k+j)th columns of A(k)A^{(k)} are interchanged. The result is a factorization AΠ=QRA\Pi = QR, where Π\Pi is a permutation matrix and RR satisfies the inequalities

∣rkk∣2≥∑i=kj∣rij∣2,j=k+1 ⁣:n,k=1 ⁣:n.\notag |r_{kk}|^2 \ge \displaystyle\sum_{i=k}^j |r_{ij}|^2, \quad j=k+1\colon n, \quad k=1\colon n.

In particular,

∣r11∣≥∣r22∣≥⋯≥∣rnn∣.(†)\notag \qquad\qquad\qquad\qquad\qquad\qquad |r_{11}| \ge |r_{22}| \ge \cdots \ge |r_{nn}|. \qquad\qquad\qquad\qquad\qquad\qquad (\dagger)

If AA is rank deficient then RR has the form

R=[R11R1200],\notag R = \begin{bmatrix}R_{11} & R_{12} \\ 0 & 0 \end{bmatrix},

with R11R_{11} nonsingular, and the rank of AA is the dimension of R11R_{11}.

Near rank deficiency of AA to tends to be revealed by a small trailing diagonal block of RR, but this is not guaranteed. Indeed for the Kahan matrix

Un(θ)=diag(1,s,…,sn−1)[1−c−c…−c1−c…−c⋱⋱⋮⋱−c1]\notag U_n(\theta) = \mathrm{diag}(1,s,\dots,s^{n-1}) \begin{bmatrix} 1 & -c & -c & \dots & -c \\ & 1 & -c & \dots & -c \\ & & \ddots &\ddots & \vdots \\ & & &\ddots & -c \\ & & & & 1 \end{bmatrix}

where c=cos⁡θc =\cos\theta and s=sin⁡θs = \sin\theta, unnu_{nn} is of order 2n2^n times larger than the smallest singular value for small θ\theta and Un(θ)U_n(\theta) is invariant under QR factorization with column pivoting.

In practice, column pivoting reduces the efficiency of Householder QR factorization because it limits the amount of the computation that can be expressed in terms of matrix multiplication. This has motivated the development of methods that select the pivot columns using randomized projections. These methods gain speed and produce factorizations of similar rank-revealing quality, though they give up the inequalities (†)(\dagger).

It is known that at least one permutation matrix Π\Pi exists such that the QR factorization of AΠA\Pi is rank-revealing. Computing such a permutation is impractical, but heuristic algorithms for producing an approximate rank-revealing factorization are available.

References

This is a minimal set of references, which contain further useful references within.

This article is part of the “What Is” series, available from https://nhigham.com/category/what-is and in PDF form from the GitHub repository https://github.com/higham/what-is.