Mathwords logoMathwords

QR Decomposition — Definition, Formula & Example

QR decomposition is a way to write a matrix as a product A=QRA = QR. The columns of QQ are perpendicular unit vectors, and RR is upper triangular. You can think of it as the Gram-Schmidt process applied to the columns of AA, with the bookkeeping saved in RR.

Let AA be a real m×nm \times n matrix with m≥nm \ge n. A (reduced) QR decomposition is a factorization A=QRA = QR. Here QQ is m×nm \times n with orthonormal columns, so QTQ=InQ^TQ = I_n, and RR is an n×nn \times n upper triangular matrix. Such a factorization always exists. If AA has full column rank, RR is invertible, and the factorization is unique once you require the diagonal entries of RR to be positive. In the full QR decomposition, QQ is an m×mm \times m orthogonal matrix and RR is an m×nm \times n upper triangular matrix.

Key Formula

A=QR,QTQ=I,rij=qi⋅aj  (i≤j)A = QR, \qquad Q^TQ = I, \qquad r_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j \;(i \le j)
Where:
  • AA = The matrix being factored, with columns a1,…,an\mathbf{a}_1, \dots, \mathbf{a}_n
  • QQ = Matrix whose columns q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n are orthonormal
  • RR = Upper triangular matrix; entry rijr_{ij} records how much of qi\mathbf{q}_i is in column aj\mathbf{a}_j
  • II = The identity matrix

Worked Example

Problem: Find the QR decomposition of A=[32411]A = \begin{bmatrix} 3 & 2 \\ 4 & 11 \end{bmatrix} using Gram-Schmidt on its columns a1=(3,4)\mathbf{a}_1 = (3, 4) and a2=(2,11)\mathbf{a}_2 = (2, 11).
Step 1: Normalize the first column. Its length becomes r11r_{11}.
r11=∥a1∥=32+42=5,q1=(35,45)r_{11} = \|\mathbf{a}_1\| = \sqrt{3^2 + 4^2} = 5, \qquad \mathbf{q}_1 = \left(\tfrac{3}{5}, \tfrac{4}{5}\right)
Step 2: Project a2\mathbf{a}_2 onto q1\mathbf{q}_1 to get r12r_{12}. Then subtract that part to leave a vector perpendicular to q1\mathbf{q}_1.
r12=q1⋅a2=6+445=10,a2−10 q1=(2−6,  11−8)=(−4,3)r_{12} = \mathbf{q}_1 \cdot \mathbf{a}_2 = \frac{6 + 44}{5} = 10, \qquad \mathbf{a}_2 - 10\,\mathbf{q}_1 = (2 - 6,\; 11 - 8) = (-4, 3)
Step 3: Normalize the leftover vector. Its length becomes r22r_{22}.
r22=(−4)2+32=5,q2=(−45,35)r_{22} = \sqrt{(-4)^2 + 3^2} = 5, \qquad \mathbf{q}_2 = \left(-\tfrac{4}{5}, \tfrac{3}{5}\right)
Answer: Q=[3/5−4/54/53/5]Q = \begin{bmatrix} 3/5 & -4/5 \\ 4/5 & 3/5 \end{bmatrix} and R=[51005]R = \begin{bmatrix} 5 & 10 \\ 0 & 5 \end{bmatrix}. Check: q1⋅q2=−1225+1225=0\mathbf{q}_1 \cdot \mathbf{q}_2 = -\tfrac{12}{25} + \tfrac{12}{25} = 0. Multiplying out QRQR gives back [32411]=A\begin{bmatrix} 3 & 2 \\ 4 & 11 \end{bmatrix} = A.

Why It Matters

In a numerical linear algebra or data science course, QR is the standard way to solve least-squares problems. You solve Rx=QTbR\mathbf{x} = Q^T\mathbf{b} by back substitution instead of forming ATAA^TA, which squares the condition number and loses accuracy. Repeated QR factorizations are also the core of the QR algorithm, which software uses to compute eigenvalues.

Common Mistakes

Mistake: Making the columns of Q perpendicular but forgetting to divide by their lengths.
Correction: The columns of QQ must be orthonormal: each one has length 1. Without normalizing, QTQQ^TQ is diagonal but not II, and QRQR no longer equals AA unless you rescale RR. Store each length on the diagonal of RR.

Related Terms