Skip to content

QR Decomposition

QR decomposition is an alternative to the normal equations for solving overdetermined systems. It factors the coefficient matrix AA into an orthonormal matrix QQ and an upper triangular matrix RR, then reduces the least squares problem to a simple triangular system. QR decomposition tends to be numerically more stable than forming ATAA^T A explicitly.


The Factorization A=QRA = QR

Any m×nm \times n matrix AA with m≥nm \geq n and linearly independent columns can be written as:

A=QRA = QR

where:

  • QQ is an m×nm \times n matrix whose columns q1,q2,…,qnq_1, q_2, \dots, q_n form an orthonormal set.
  • RR is an n×nn \times n upper triangular matrix with positive diagonal entries.

The columns of QQ are computed from the columns of AA using the Gram-Schmidt process.


The Gram-Schmidt Process

Let u1,u2,…,unu_1, u_2, \dots, u_n be the columns of AA. The Gram-Schmidt process converts them into orthonormal vectors q1,q2,…,qnq_1, q_2, \dots, q_n using two formulas applied for k=1,2,…,nk = 1, 2, \dots, n:

pk=uk−∑i=1k−1(ukTqi) qip_k = u_k - \sum_{i=1}^{k-1} (u_k^T q_i)\,q_i qk=pk∣pk∣q_k = \frac{p_k}{|p_k|}

At each step, pkp_k removes from uku_k all components in the directions already spanned by q1,…,qk−1q_1, \dots, q_{k-1}. Dividing by ∣pk∣|p_k| then normalizes the result to unit length.

For k=1k=1: The sum is empty (lower limit exceeds upper limit), so p1=u1p_1 = u_1. We just normalize:

p1=u1,q1=u1∣u1∣p_1 = u_1, \qquad q_1 = \frac{u_1}{|u_1|}

For k=2k=2: Subtract the projection onto q1q_1:

p2=u2−(u2Tq1) q1,q2=p2∣p2∣p_2 = u_2 - (u_2^T q_1)\,q_1, \qquad q_2 = \frac{p_2}{|p_2|}

For k=3k=3: Subtract projections onto both q1q_1 and q2q_2:

p3=u3−(u3Tq1) q1−(u3Tq2) q2,q3=p3∣p3∣p_3 = u_3 - (u_3^T q_1)\,q_1 - (u_3^T q_2)\,q_2, \qquad q_3 = \frac{p_3}{|p_3|}

Assembling RR

Once QQ is known, the entries of RR are the dot products of the original columns of AA with the orthonormal columns of QQ. Because of the upper triangular structure of the Gram-Schmidt construction:

R=[u1Tq1u2Tq1u3Tq1⋯0u2Tq2u3Tq2⋯00u3Tq3⋯⋮⋮⋮⋱]R = \begin{bmatrix} u_1^T q_1 & u_2^T q_1 & u_3^T q_1 & \cdots \\ 0 & u_2^T q_2 & u_3^T q_2 & \cdots \\ 0 & 0 & u_3^T q_3 & \cdots \\ \vdots & \vdots & \vdots & \ddots \end{bmatrix}

In general, Rij=ujTqiR_{ij} = u_j^T q_i for i≤ji \leq j, and Rij=0R_{ij} = 0 for i>ji > j.


Solving the Least Squares Problem via QR

Starting from Ax=bAx = b, substitute A=QRA = QR:

QR x=bQR\, x = b

Multiply both sides on the left by QTQ^T:

QTQR x=QTbQ^T Q R\, x = Q^T b

Because QQ has orthonormal columns, QTQ=IQ^T Q = I (proved below). Therefore:

R x=QTbR\, x = Q^T b

This is an n×nn \times n upper triangular system, which is solved by back substitution. No matrix inversion or formation of ATAA^T A is needed.


Proof: QTQ=IQ^T Q = I

Let Q=[q1q2⋯qn]Q = \begin{bmatrix} q_1 & q_2 & \cdots & q_n \end{bmatrix}. Then:

QTQ=[−q1T−−q2T−⋮⋮][∣∣q1q2⋯∣∣]=[q1Tq1q1Tq2⋯q2Tq1q2Tq2⋯⋮⋮⋱]Q^T Q = \begin{bmatrix} - & q_1^T & - \\ - & q_2^T & - \\ \vdots & & \vdots \end{bmatrix} \begin{bmatrix} | & | & \\ q_1 & q_2 & \cdots \\ | & | & \end{bmatrix} = \begin{bmatrix} q_1^T q_1 & q_1^T q_2 & \cdots \\ q_2^T q_1 & q_2^T q_2 & \cdots \\ \vdots & \vdots & \ddots \end{bmatrix}

Since QQ has orthonormal columns, qiTqj=δijq_i^T q_j = \delta_{ij}. Every diagonal entry equals 1 and every off-diagonal entry equals 0:

QTQ=[10⋯01⋯⋮⋮⋱]=I■Q^T Q = \begin{bmatrix} 1 & 0 & \cdots \\ 0 & 1 & \cdots \\ \vdots & \vdots & \ddots \end{bmatrix} = I \qquad \blacksquare

Proof: A=QRA = QR

We show that the Gram-Schmidt process produces exactly the factorization A=QRA = QR.

From the Gram-Schmidt recurrence qk=pk/∣pk∣q_k = p_k / |p_k|, we have pk=∣pk∣ qkp_k = |p_k|\,q_k. Substituting back into the formula for pkp_k:

∣pk∣ qk=uk−∑i=1k−1(ukTqi) qi|p_k|\,q_k = u_k - \sum_{i=1}^{k-1}(u_k^T q_i)\,q_i

Rearranging to express uku_k:

uk=∣pk∣ qk+∑i=1k−1(ukTqi) qi(1)u_k = |p_k|\,q_k + \sum_{i=1}^{k-1}(u_k^T q_i)\,q_i \tag{1}

Multiply both sides of (1) by qkTq_k^T from the left:

qkTuk=∣pk∣qkTqk⏟1+∑i=1k−1(ukTqi)qkTqi⏟0=∣pk∣q_k^T u_k = |p_k|\underbrace{q_k^T q_k}_{1} + \sum_{i=1}^{k-1}(u_k^T q_i)\underbrace{q_k^T q_i}_{0} = |p_k|

(The sum vanishes because qkTqi=0q_k^T q_i = 0 for i<ki < k, by orthogonality.)

Since the dot product is symmetric, qkTuk=ukTqkq_k^T u_k = u_k^T q_k. Substituting ∣pk∣=ukTqk|p_k| = u_k^T q_k back into (1):

uk=(ukTqk) qk+∑i=1k−1(ukTqi) qi=∑i=1k(ukTqi) qiu_k = (u_k^T q_k)\,q_k + \sum_{i=1}^{k-1}(u_k^T q_i)\,q_i = \sum_{i=1}^{k}(u_k^T q_i)\,q_i

For n=3n = 3 this expands to:

u1=(u1Tq1) q1u_1 = (u_1^T q_1)\,q_1 u2=(u2Tq1) q1+(u2Tq2) q2u_2 = (u_2^T q_1)\,q_1 + (u_2^T q_2)\,q_2 u3=(u3Tq1) q1+(u3Tq2) q2+(u3Tq3) q3u_3 = (u_3^T q_1)\,q_1 + (u_3^T q_2)\,q_2 + (u_3^T q_3)\,q_3

Stacking the columns:

A=[∣∣∣u1u2u3∣∣∣]=[∣∣∣q1q2q3∣∣∣][u1Tq1u2Tq1u3Tq10u2Tq2u3Tq200u3Tq3]=QR■A = \begin{bmatrix} | & | & | \\ u_1 & u_2 & u_3 \\ | & | & | \end{bmatrix} = \begin{bmatrix} | & | & | \\ q_1 & q_2 & q_3 \\ | & | & | \end{bmatrix} \begin{bmatrix} u_1^T q_1 & u_2^T q_1 & u_3^T q_1 \\ 0 & u_2^T q_2 & u_3^T q_2 \\ 0 & 0 & u_3^T q_3 \end{bmatrix} = QR \qquad \blacksquare

The second factor is exactly the upper triangular matrix RR defined earlier, confirming that the Gram-Schmidt construction gives a valid QR factorization.