Skip to content

Chapter 78. QR Decomposition

QR decomposition is a factorization of a matrix into an orthogonal factor and an upper triangular factor. It is one of the central decompositions in numerical linear algebra, especially for least squares problems, orthogonalization, eigenvalue algorithms, and stable solution methods.

For a real matrix AA, a QR decomposition has the form

A=QR, A = QR,

where QQ has orthonormal columns and RR is upper triangular. If AA is a square n×nn \times n matrix and has full rank, then QQ is an orthogonal matrix and RR is an n×nn \times n upper triangular matrix. The factorization is commonly computed by Gram-Schmidt orthogonalization, Householder reflections, or Givens rotations.

78.1 Orthogonal Matrices

A real square matrix QQ is orthogonal if

QTQ=I. Q^TQ = I.

This condition means that the columns of QQ form an orthonormal set. Each column has length 11, and distinct columns are perpendicular.

If

Q=[q1q2qn], Q = \begin{bmatrix} q_1 & q_2 & \cdots & q_n \end{bmatrix},

then orthogonality means

qiTqj={1,i=j,0,ij. q_i^T q_j = \begin{cases} 1, & i=j, \\ 0, & i \ne j. \end{cases}

An orthogonal matrix preserves lengths and angles. For any vector xx,

Qx=x. \|Qx\| = \|x\|.

This follows from

Qx2=(Qx)T(Qx)=xTQTQx=xTx=x2. \|Qx\|^2 = (Qx)^T(Qx) = x^TQ^TQx = x^Tx = \|x\|^2.

This property makes orthogonal transformations numerically stable. They do not magnify errors merely by changing coordinates.

78.2 Upper Triangular Matrices

An upper triangular matrix has zero entries below the diagonal:

R=[r11r12r13r1n0r22r23r2n00r33r3n000rnn]. R = \begin{bmatrix} r_{11} & r_{12} & r_{13} & \cdots & r_{1n} \\ 0 & r_{22} & r_{23} & \cdots & r_{2n} \\ 0 & 0 & r_{33} & \cdots & r_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & r_{nn} \end{bmatrix}.

Triangular systems are easy to solve by substitution. This is one reason QR decomposition is useful. It converts many problems involving AA into problems involving RR, while QQ handles an orthogonal change of coordinates.

78.3 Full QR Decomposition

Let AA be an m×nm \times n real matrix with mnm \ge n. A full QR decomposition writes

A=QR, A = QR,

where QQ is an m×mm \times m orthogonal matrix and RR is an m×nm \times n upper trapezoidal matrix.

The matrix RR has the form

R=[R10], R = \begin{bmatrix} R_1 \\ 0 \end{bmatrix},

where R1R_1 is an n×nn \times n upper triangular matrix.

Thus

A=Q[R10]. A = Q \begin{bmatrix} R_1 \\ 0 \end{bmatrix}.

The full form is useful theoretically, but it stores more information than is often needed.

78.4 Reduced QR Decomposition

For many applications, the reduced QR decomposition is preferred.

If AA is an m×nm \times n matrix with mnm \ge n and linearly independent columns, then

A=Q^R, A = \widehat{Q}R,

where Q^\widehat{Q} is m×nm \times n with orthonormal columns and RR is n×nn \times n upper triangular.

The matrix Q^\widehat{Q} satisfies

Q^TQ^=In. \widehat{Q}^T\widehat{Q} = I_n.

However,

Q^Q^T \widehat{Q}\widehat{Q}^T

is generally not the m×mm \times m identity matrix. Instead, it is the orthogonal projection onto the column space of AA.

The reduced decomposition stores only the orthonormal basis needed for the column space of AA.

78.5 Geometric Meaning

The QR decomposition separates a matrix into two parts:

A=QR. A = QR.

The factor QQ gives an orthonormal coordinate system for the column space of AA. The factor RR gives the coordinates of the columns of AA in that orthonormal system.

Let

A=[a1a2an] A = \begin{bmatrix} a_1 & a_2 & \cdots & a_n \end{bmatrix}

and

Q=[q1q2qn]. Q = \begin{bmatrix} q_1 & q_2 & \cdots & q_n \end{bmatrix}.

The columns q1,,qnq_1,\ldots,q_n form an orthonormal basis for the same column space as a1,,ana_1,\ldots,a_n. Each original column aja_j can be written as a combination of the qiq_i:

aj=r1jq1+r2jq2++rjjqj. a_j = r_{1j}q_1 + r_{2j}q_2 + \cdots + r_{jj}q_j.

Only the first jj vectors appear because RR is upper triangular.

78.6 QR from Gram-Schmidt

QR decomposition is closely related to Gram-Schmidt orthogonalization.

Let the columns of AA be

a1,a2,,an. a_1, a_2, \ldots, a_n.

The Gram-Schmidt process constructs orthonormal vectors

q1,q2,,qn q_1, q_2, \ldots, q_n

from these columns.

First,

r11=a1,q1=a1r11. r_{11} = \|a_1\|, \qquad q_1 = \frac{a_1}{r_{11}}.

For the second column, subtract its projection onto q1q_1:

r12=q1Ta2, r_{12} = q_1^Ta_2, v2=a2r12q1. v_2 = a_2 - r_{12}q_1.

Then

r22=v2,q2=v2r22. r_{22} = \|v_2\|, \qquad q_2 = \frac{v_2}{r_{22}}.

In general, for column aja_j,

rij=qiTaj1i<j, r_{ij} = q_i^Ta_j \qquad 1 \le i < j, vj=aji=1j1rijqi, v_j = a_j - \sum_{i=1}^{j-1} r_{ij}q_i, rjj=vj,qj=vjrjj. r_{jj} = \|v_j\|, \qquad q_j = \frac{v_j}{r_{jj}}.

These formulas produce

A=QR. A = QR.

The entries of RR are the projection coefficients and normalizing constants from the orthogonalization process.

78.7 A Two Column Example

Let

A=[111001]. A = \begin{bmatrix} 1 & 1 \\ 1 & 0 \\ 0 & 1 \end{bmatrix}.

Its columns are

a1=[110],a2=[101]. a_1 = \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix}, \qquad a_2 = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix}.

First compute

r11=a1=2. r_{11} = \|a_1\| = \sqrt{2}.

Thus

q1=12[110]. q_1 = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix}.

Next,

r12=q1Ta2=12. r_{12} = q_1^Ta_2 = \frac{1}{\sqrt{2}}.

Subtract the projection:

v2=a2r12q1. v_2 = a_2 - r_{12}q_1.

Since

r12q1=1212[110]=12[110], r_{12}q_1 = \frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix} = \frac{1}{2} \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix},

we get

v2=[101][1/21/20]=[1/21/21]. v_2 = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix} - \begin{bmatrix} 1/2 \\ 1/2 \\ 0 \end{bmatrix} = \begin{bmatrix} 1/2 \\ -1/2 \\ 1 \end{bmatrix}.

Now

r22=v2=14+14+1=32. r_{22} = \|v_2\| = \sqrt{ \frac{1}{4} + \frac{1}{4} + 1 } = \sqrt{\frac{3}{2}}.

Therefore

q2=v2r22=23[1/21/21]. q_2 = \frac{v_2}{r_{22}} = \sqrt{\frac{2}{3}} \begin{bmatrix} 1/2 \\ -1/2 \\ 1 \end{bmatrix}.

Thus the reduced QR decomposition is

A=QR, A = QR,

where

Q=[1/21/61/21/602/6] Q = \begin{bmatrix} 1/\sqrt{2} & 1/\sqrt{6} \\ 1/\sqrt{2} & -1/\sqrt{6} \\ 0 & 2/\sqrt{6} \end{bmatrix}

and

R=[21/203/2]. R = \begin{bmatrix} \sqrt{2} & 1/\sqrt{2} \\ 0 & \sqrt{3/2} \end{bmatrix}.

The columns of QQ are orthonormal, and RR is upper triangular.

78.8 Solving Square Linear Systems

Suppose AA is a square invertible matrix and

A=QR. A = QR.

To solve

Ax=b, Ax = b,

substitute the factorization:

QRx=b. QRx = b.

Multiply both sides by QTQ^T:

Rx=QTb. Rx = Q^Tb.

Since RR is upper triangular, the system

Rx=QTb Rx = Q^Tb

can be solved by backward substitution.

This method is usually more expensive than LU for a general square dense system, but it has strong stability properties because QQ is orthogonal.

78.9 Least Squares Problems

QR decomposition is one of the standard methods for solving least squares problems.

Suppose AA is an m×nm \times n matrix with mnm \ge n, and suppose the system

Axb Ax \approx b

has more equations than unknowns. The least squares problem asks for xx minimizing

Axb2. \|Ax-b\|_2.

Let

A=QR A = QR

be a reduced QR decomposition. Then

Axb2=QRxb2. \|Ax-b\|_2 = \|QRx-b\|_2.

Since multiplication by an orthogonal matrix preserves length, this is equivalent to

RxQTb2. \|Rx-Q^Tb\|_2.

Thus the least squares solution satisfies the triangular system

Rx=QTb. Rx = Q^Tb.

This gives a stable method that avoids forming the normal equations

ATAx=ATb. A^TAx = A^Tb.

Avoiding ATAA^TA is important because forming it can worsen conditioning.

78.10 Projection Interpretation

The least squares problem seeks the vector in the column space of AA closest to bb.

If

A=Q^R A = \widehat{Q}R

is the reduced QR decomposition, then the columns of Q^\widehat{Q} form an orthonormal basis for the column space of AA.

The orthogonal projection of bb onto this column space is

Q^Q^Tb. \widehat{Q}\widehat{Q}^Tb.

The least squares fitted vector is therefore

Ax=Q^Q^Tb. Ax = \widehat{Q}\widehat{Q}^Tb.

Since

Ax=Q^Rx, Ax = \widehat{Q}Rx,

we obtain

Q^Rx=Q^Q^Tb. \widehat{Q}Rx = \widehat{Q}\widehat{Q}^Tb.

Multiplying by Q^T\widehat{Q}^T gives

Rx=Q^Tb. Rx = \widehat{Q}^Tb.

This is the same triangular system obtained by the norm-preservation argument.

78.11 Classical and Modified Gram-Schmidt

There are two common Gram-Schmidt algorithms.

Classical Gram-Schmidt computes all projection coefficients against the original vector and then subtracts the combined projection.

Modified Gram-Schmidt subtracts projections one at a time, updating the vector after each subtraction.

Mathematically, both methods produce the same exact result in exact arithmetic. Numerically, modified Gram-Schmidt is usually more stable because it reduces the loss of orthogonality caused by rounding error.

MethodMain ideaNumerical behavior
Classical Gram-SchmidtSubtract combined projectionLess stable
Modified Gram-SchmidtSubtract projections sequentiallyMore stable
Householder QRUse reflectionsMore stable for dense matrices
Givens QRUse plane rotationsUseful for sparse or structured updates

78.12 Householder Reflections

A Householder reflection is an orthogonal transformation that reflects vectors across a hyperplane.

It has the form

H=I2vvT, H = I - 2vv^T,

where

v=1. \|v\| = 1.

The matrix HH is symmetric and orthogonal:

HT=H,HTH=I. H^T = H, \qquad H^TH = I.

Householder QR uses a sequence of reflections to introduce zeros below the diagonal. At each step, a reflection is chosen to map part of a column to a multiple of a coordinate vector.

After applying these reflections, the matrix becomes upper triangular:

HkH2H1A=R. H_k \cdots H_2H_1A = R.

Since each HiH_i is orthogonal,

A=H1H2HkR. A = H_1H_2\cdots H_kR.

Thus

Q=H1H2Hk. Q = H_1H_2\cdots H_k.

Householder QR is a standard dense QR method because it is stable and efficient.

78.13 Givens Rotations

A Givens rotation is an orthogonal transformation that acts on only two coordinates at a time.

In two dimensions, it has the form

G=[cssc], G = \begin{bmatrix} c & s \\ -s & c \end{bmatrix},

where

c2+s2=1. c^2+s^2=1.

A Givens rotation can be chosen to zero one selected entry of a vector. Applied repeatedly, these rotations can transform a matrix into upper triangular form.

Givens rotations are especially useful when the matrix is sparse. Since each rotation affects only two rows, the method can preserve more structure than a dense Householder transformation.

They are also useful for updating QR decompositions when rows or columns are added or changed.

78.14 Uniqueness

QR decomposition is not completely unique unless a sign convention is imposed.

If

A=QR, A = QR,

then one may multiply a column of QQ by 1-1 and the corresponding row of RR by 1-1. The product remains unchanged.

To make the reduced QR decomposition unique for a full column rank matrix, one usually requires the diagonal entries of RR to be positive:

rii>0. r_{ii} > 0.

Under this convention, the reduced QR decomposition is unique.

78.15 Rank Deficiency

If the columns of AA are linearly dependent, then some diagonal entry of RR becomes zero in exact arithmetic.

For a full column rank matrix, RR is nonsingular. For a rank deficient matrix, RR is singular.

Rank deficiency creates difficulties in solving least squares problems because the minimizer may fail to be unique. In such cases, pivoted QR decomposition is often used.

A column-pivoted QR decomposition has the form

AP=QR, AP = QR,

where PP is a permutation matrix that reorders the columns of AA. The pivoting helps reveal numerical rank.

78.16 Determinants from QR

For a square matrix

A=QR, A = QR,

the determinant satisfies

det(A)=det(Q)det(R). \det(A) = \det(Q)\det(R).

For an orthogonal matrix,

det(Q)=1ordet(Q)=1. \det(Q)=1 \quad \text{or} \quad \det(Q)=-1.

Since RR is triangular,

det(R)=i=1nrii. \det(R)=\prod_{i=1}^{n} r_{ii}.

Therefore

det(A)=det(Q)i=1nrii. \det(A) = \det(Q) \prod_{i=1}^{n} r_{ii}.

If only the absolute value is needed,

det(A)=i=1nrii. |\det(A)| = \prod_{i=1}^{n} |r_{ii}|.

For large matrices, one often computes

logdet(A)=i=1nlogrii. \log |\det(A)| = \sum_{i=1}^{n} \log |r_{ii}|.

78.17 QR Algorithm for Eigenvalues

QR decomposition also appears in the QR algorithm for computing eigenvalues.

The basic iteration starts with

A0=A. A_0 = A.

At step kk, compute

Ak=QkRk. A_k = Q_kR_k.

Then form

Ak+1=RkQk. A_{k+1} = R_kQ_k.

Since

Ak+1=RkQk=QkTAkQk, A_{k+1} = R_kQ_k = Q_k^TA_kQ_k,

the matrices AkA_k are orthogonally similar. Therefore they have the same eigenvalues.

Under suitable conditions and with practical enhancements, the sequence tends toward a form from which eigenvalues can be read. This is one of the major uses of QR decomposition in numerical linear algebra.

78.18 Algorithm: Modified Gram-Schmidt QR

The following algorithm computes a reduced QR decomposition using modified Gram-Schmidt.

modified_gram_schmidt_qr(A):
    m, n = shape of A

    Q = zero matrix of size m by n
    R = zero matrix of size n by n

    for j = 1 to n:
        v = A[:,j]

        for i = 1 to j - 1:
            R[i,j] = Q[:,i]^T v
            v = v - R[i,j] * Q[:,i]

        R[j,j] = norm(v)

        if R[j,j] == 0:
            stop: columns are linearly dependent

        Q[:,j] = v / R[j,j]

    return Q, R

This algorithm is easy to understand and useful for teaching. For high-quality dense numerical software, Householder QR is often preferred.

78.19 Storage in Practice

QR decomposition is often stored implicitly.

A dense implementation using Householder reflections may store the upper triangular part of RR in the upper triangular part of the original matrix. The Householder vectors are stored below the diagonal. The matrix QQ is not always formed explicitly.

ObjectPractical storage
RRUpper triangular part of the array
QQProduct of stored Householder reflectors
Householder vectorsLower triangular part of the array
Reduced QQFormed only when needed

This storage model saves memory and improves performance. Many algorithms only need to multiply by QQ or QTQ^T, so forming QQ explicitly would be unnecessary work.

78.20 Comparison with LU and Cholesky

DecompositionFormApplies toMain advantage
LUPA=LUPA = LUGeneral square matricesFast direct solves
CholeskyA=LLTA = LL^TSymmetric positive definite matricesFaster SPD solves
QRA=QRA = QRSquare or rectangular matricesOrthogonality and least squares
SVDA=UΣVTA = U\Sigma V^TGeneral matricesRank and conditioning information

QR is usually more expensive than LU for a square dense solve, but it is more stable in important settings and naturally handles rectangular matrices. It is the standard decomposition behind many reliable least squares methods.

78.21 Summary

QR decomposition factors a matrix as

A=QR. A = QR.

The factor QQ has orthonormal columns. The factor RR is upper triangular. Geometrically, QQ gives an orthonormal basis for the column space of AA, while RR gives the coordinates of the original columns in that basis.

QR decomposition is used to solve least squares problems, orthogonalize vectors, solve square systems, update factorizations, estimate rank, and compute eigenvalues. Its strength comes from orthogonality. Since orthogonal transformations preserve lengths and angles, QR-based algorithms tend to have good numerical behavior.

The main computational forms are Gram-Schmidt, Householder reflections, and Givens rotations. Gram-Schmidt explains the idea. Householder reflections are standard for dense matrices. Givens rotations are useful when sparsity or incremental updates matter.