Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools

How to Find the QR Decomposition

Write A=QRA = QR with QQ having orthonormal columns and RR upper triangular. Gram-Schmidt on the columns of AA gives both: the unit vectors are QQ, and the dot products and lengths it computes along the way are RR.

Recognize the task

A matrix with independent columns, often tall, and the words QR decomposition or QR factorization; a least-squares problem to solve stably; or an orthonormal basis of the column space. Forms: (132320)\begin{pmatrix} 1 & 3 \\ 2 & 3 \\ 2 & 0 \end{pmatrix}, (3142)\begin{pmatrix} 3 & 1 \\ 4 & 2 \end{pmatrix}. Test: the columns must be independent; otherwise a zero vector appears during the process.
The factorization is defined in the QR decomposition lesson; the column process is the Gram-Schmidt method.

Two ways to do it

Gram-Schmidtby hand
rij=qi⋅aj (i<j),rjj=∥uj∥r_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j \ (i < j), \quad r_{jj} = \|\mathbf{u}_j\|
Orthonormalize the columns in order and keep every coefficient (entries of R).
Householder reflectionsby computer
H=I−2wwT,Hn⋯H1A=RH = I - 2\mathbf{w}\mathbf{w}^T, \quad H_n \cdots H_1 A = R
Reflect each column onto a coordinate axis; more stable in floating point (Householder reflections).

Steps

StepOn A=(132320)A = \begin{pmatrix} 1 & 3 \\ 2 & 3 \\ 2 & 0 \end{pmatrix}
1Normalize the first column. r11=∥a1∥r_{11} = \|\mathbf{a}_1\| and q1=a1/r11\mathbf{q}_1 = \mathbf{a}_1 / r_{11}.r11=1+4+4=3r_{11} = \sqrt{1 + 4 + 4} = 3, q1=13(1,2,2)\mathbf{q}_1 = \frac{1}{3}(1, 2, 2)
2Dot the next column with the unit vectors found so far. Each rij=qi⋅ajr_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j goes above the diagonal in column jj of RR.r12=13(3+6+0)=3r_{12} = \frac{1}{3}(3 + 6 + 0) = 3
3Subtract and normalize. uj=aj−∑i<jrijqi\mathbf{u}_j = \mathbf{a}_j - \sum_{i < j} r_{ij}\mathbf{q}_i; its length is rjjr_{jj}, and qj=uj/rjj\mathbf{q}_j = \mathbf{u}_j / r_{jj}.u2=(3,3,0)−(1,2,2)=(2,1,−2)\mathbf{u}_2 = (3, 3, 0) - (1, 2, 2) = (2, 1, -2), r22=3r_{22} = 3, q2=13(2,1,−2)\mathbf{q}_2 = \frac{1}{3}(2, 1, -2)
4Assemble Q and R. The qj\mathbf{q}_j are the columns of QQ; the rijr_{ij} fill RR on and above the diagonal.Q=13(12212−2)Q = \frac{1}{3}\begin{pmatrix} 1 & 2 \\ 2 & 1 \\ 2 & -2 \end{pmatrix}, R=(3303)R = \begin{pmatrix} 3 & 3 \\ 0 & 3 \end{pmatrix}
5Check. QTQ=IQ^TQ = I and QR=AQR = A. The diagonal of RR must be positive.QR=(132320)QR = \begin{pmatrix} 1 & 3 \\ 2 & 3 \\ 2 & 0 \end{pmatrix} ✓

Worked examples

Example 1 · a square matrix

2 by 2
A=(3142)A = \begin{pmatrix} 3 & 1 \\ 4 & 2 \end{pmatrix}: r11=5r_{11} = 5, q1=(35,45)\mathbf{q}_1 = (\frac{3}{5}, \frac{4}{5})
length of column 1
r12=3+85=115r_{12} = \frac{3 + 8}{5} = \frac{11}{5}, u2=(1,2)−115q1=(−825,625)\mathbf{u}_2 = (1, 2) - \frac{11}{5}\mathbf{q}_1 = (-\frac{8}{25}, \frac{6}{25})
remove the q1\mathbf{q}_1 part
r22=25r_{22} = \frac{2}{5}, Q=(35−454535)Q = \begin{pmatrix} \frac{3}{5} & -\frac{4}{5} \\ \frac{4}{5} & \frac{3}{5} \end{pmatrix}, R=(5115025)R = \begin{pmatrix} 5 & \frac{11}{5} \\ 0 & \frac{2}{5} \end{pmatrix}
check: det⁡R=2=det⁡A\det R = 2 = \det A

Example 2 · least squares with QR

triangular solve
the matrix above, b=(0,3,6)\mathbf{b} = (0, 3, 6): QTb=13(0+6+12,  0+3−12)=(6,−3)Q^T\mathbf{b} = \frac{1}{3}(0 + 6 + 12,\; 0 + 3 - 12) = (6, -3)
no normal equations needed
(3303)x^=(6,−3)\begin{pmatrix} 3 & 3 \\ 0 & 3 \end{pmatrix}\hat{\mathbf{x}} = (6, -3)
back-substitute
x^=(3,−1)\hat{\mathbf{x}} = (3, -1)
matches ATAx^=ATbA^TA\hat{\mathbf{x}} = A^T\mathbf{b}
The second example is the least-squares problem solved without forming ATAA^TA. Try other matrices in the QR decomposition visualizer.

See it happen

Column jj of AA uses only the first jj directions q1,…,qj\mathbf{q}_1, \dots, \mathbf{q}_j, which is why RR is upper triangular. QQ holds the perpendicular directions, and RR says how much of each direction every column contains. Watch the columns being straightened in the QR decomposition visualizer.

Common mistakes

Writing 1 on the diagonal of R

✗ R=(3301)R = \begin{pmatrix} 3 & 3 \\ 0 & 1 \end{pmatrix}
✓ r22=∥u2∥=3r_{22} = \|\mathbf{u}_2\| = 3, the length before normalizing

Dotting with the unnormalized vector

✗ r12=a1⋅a2=9r_{12} = \mathbf{a}_1 \cdot \mathbf{a}_2 = 9
✓ r12=q1⋅a2=3r_{12} = \mathbf{q}_1 \cdot \mathbf{a}_2 = 3; use the unit vector

Expecting Q times its transpose to be I

✗ QQT=IQQ^T = I for the 3×23 \times 2 matrix QQ above
✓ QTQ=IQ^TQ = I, but QQTQQ^T is the projection onto the column space of AA