Skip to content

QR factorisation

Record the shadows and lengths of Gram–Schmidt and a matrix splits into perpendicular columns times a triangle; then clear a column with a mirror.

Helps to know first
This topic

First, the picture

A mirror can swing an arrow onto an axis without changing its length. Watch a mirror turn until the arrow lands.

A mirror that clears a column

The arrow (3, 4) and a turning mirror through the origin.

The arrow (3, 4), of length 5, and a mirror along the horizontal axis. Its image is (3, −4).

mirror angle
0°
image
(3, −4)
0.00 / 6.80 s
Describe this picture

The plane with the arrow (3,4)(3,4), of length 5, and a dashed circle of radius 5. A dotted mirror line through the origin starts along the horizontal axis and, after a short rest, turns slowly anticlockwise over 4 s. The arrow’s mirror image is drawn as it turns; its tip runs round the circle. When the mirror lies along (2,1)(2,1), at 26.57° (0.4636 radians), the image lands on the horizontal axis at (5,0)(5,0), and the picture holds there with the mirror’s matrix [0.60.80.8−0.6]\begin{bmatrix}0.6&0.8\\0.8&-0.6\end{bmatrix} shown in the corner. When the clip has finished, the mirror can be turned by a ring on its line, dragged or moved with the arrow keys a degree at a time; it snaps onto 26.57° when it comes within 1.5°. Readouts give the angle and the image’s coordinates.

Gram–Schmidt, written down

Orthonormal bases and Gram–Schmidt turned the columns of

A=[110101011]\mathbf{A}=\begin{bmatrix}1&1&0\\1&0&1\\0&1&1\end{bmatrix}

into three perpendicular unit arrows, q1=(1,1,0)/2\mathbf{q}_1=(1,1,0)/\sqrt2, q2=(1,−1,2)/6\mathbf{q}_2=(1,-1,2)/\sqrt6 and q3=(−1,1,1)/3\mathbf{q}_3=(-1,1,1)/\sqrt3. On the way it worked out some numbers and then threw them away: the shadows it took off, and the lengths it divided by. Keep them.

Run Gram–Schmidt backwards. Each column of A\mathbf{A} is its leftover plus its shadows, and its leftover is a length times a q\mathbf{q}. So

a1=r11q1,a2=r12q1+r22q2,a3=r13q1+r23q2+r33q3,\mathbf{a}_1=r_{11}\mathbf{q}_1,\qquad \mathbf{a}_2=r_{12}\mathbf{q}_1+r_{22}\mathbf{q}_2,\qquad \mathbf{a}_3=r_{13}\mathbf{q}_1+r_{23}\mathbf{q}_2+r_{33}\mathbf{q}_3,

where rpq=qp⋅aqr_{pq}=\mathbf{q}_p\cdot\mathbf{a}_q is a shadow for p<qp<q, and rppr_{pp} is the length of the pp-th leftover. Column qq uses only q1\mathbf{q}_1 to qq\mathbf{q}_q. In matrices,

A=QR,\mathbf{A}=\mathbf{Q}\mathbf{R},

with the q\mathbf{q}‘s as the columns of Q\mathbf{Q} and the numbers rpqr_{pq} in an upper triangle R\mathbf{R}. This is the QR factorisation. On this page Q\mathbf{Q} and R\mathbf{R} mean these two factors; the bold R\mathbf{R} of the signal processing pages, a correlation matrix, is a different thing.

For our A\mathbf{A}:

R=[21/21/201.51/6002/3]=[1.41420.70710.707101.22470.4082001.1547].\mathbf{R}=\begin{bmatrix}\sqrt2&1/\sqrt2&1/\sqrt2\\0&\sqrt{1.5}&1/\sqrt6\\0&0&2/\sqrt3\end{bmatrix} =\begin{bmatrix}1.4142&0.7071&0.7071\\0&1.2247&0.4082\\0&0&1.1547\end{bmatrix}.

Watch the matrices rebuild A\mathbf{A}.

Cells

A = QR: perpendicular unit columns times an upper triangle.

A, with columns (1, 1, 0), (1, 0, 1), (0, 1, 1).

0.00 / 11.20 s
Describe this picture

Grids of cells in a row (two rows of two on a phone), each cell shaded by its number: A\mathbf{A}, then Q\mathbf{Q} with its three unit columns, (0.707,0.707,0)(0.707,0.707,0), (0.408,−0.408,0.816)(0.408,-0.408,0.816) and (−0.577,0.577,0.577)(-0.577,0.577,0.577), then the upper triangle R\mathbf{R} with diagonal 1.4141.414, 1.2251.225, 1.1551.155 and faded zeros below it. A fourth grid then fills cell by cell with the product QR\mathbf{Q}\mathbf{R}, the cell being filled outlined, and when it is full its label reads QR=A\mathbf{Q}\mathbf{R}=\mathbf{A}. The caption ends with a note: the signs on the diagonal of R\mathbf{R} are a convention, and flipping a column of Q\mathbf{Q} and the matching row of R\mathbf{R} gives another correct pair. The picture has no control.

Looking ahead to The determinant as area: the diagonal of R\mathbf{R} multiplies to 1.4142⋅1.2247⋅1.1547=2.0001.4142\cdot1.2247\cdot1.1547=2.000, which is the size of det⁡A=−2\det\mathbf{A}=-2. Q\mathbf{Q} keeps volumes, so the triangle R\mathbf{R} carries all of the volume.

Least squares with QR

In Least squares we solved A⊤Ax^=A⊤y\mathbf{A}^\top\mathbf{A}\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{y}. Put A=QR\mathbf{A}=\mathbf{Q}\mathbf{R} into it. Since Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}, the left side is R⊤Rx^\mathbf{R}^\top\mathbf{R}\hat{\mathbf{x}} and the right side is R⊤Q⊤y\mathbf{R}^\top\mathbf{Q}^\top\mathbf{y}. Cancel R⊤\mathbf{R}^\top:

R x^=Q⊤y.\mathbf{R}\,\hat{\mathbf{x}}=\mathbf{Q}^\top\mathbf{y}.

R\mathbf{R} is a triangle, so this is one back substitution.

Try it on the line fit of that page, with columns (1,1,1)(1,1,1) and (0,1,2)(0,1,2) and heights y=(1,3,2)\mathbf{y}=(1,3,2). Gram–Schmidt gives q1=(1,1,1)/3\mathbf{q}_1=(1,1,1)/\sqrt3 with r11=3r_{11}=\sqrt3. The shadow of (0,1,2)(0,1,2) on q1\mathbf{q}_1 is r12=3/3=3r_{12}=3/\sqrt3=\sqrt3, which leaves (−1,0,1)(-1,0,1), of length r22=2r_{22}=\sqrt2, so q2=(−1,0,1)/2\mathbf{q}_2=(-1,0,1)/\sqrt2. Then Q⊤y=(6/3, 1/2)=(3.4641, 0.7071)\mathbf{Q}^\top\mathbf{y}=(6/\sqrt3,\ 1/\sqrt2)=(3.4641,\ 0.7071), and back substitution gives

c1=0.70711.4142=0.5,c0=3.4641−1.7321⋅0.51.7321=1.5.c_1=\frac{0.7071}{1.4142}=0.5,\qquad c_0=\frac{3.4641-1.7321\cdot0.5}{1.7321}=1.5 .

The same line, y=1.5+0.5ty=1.5+0.5t.

Why bother, when the normal equations gave the same answer? Because A⊤A\mathbf{A}^\top\mathbf{A} squares the difficulty. When two columns are nearly parallel, small rounding errors in A⊤A\mathbf{A}^\top\mathbf{A} become large errors in x^\hat{\mathbf{x}}. Here is an extreme case. Take a tiny number ε=10−8\varepsilon=10^{-8} and

A=[11ε00ε],y=(2, ε, ε),\mathbf{A}=\begin{bmatrix}1&1\\\varepsilon&0\\0&\varepsilon\end{bmatrix},\qquad \mathbf{y}=(2,\ \varepsilon,\ \varepsilon),

whose exact answer is (1,1)(1,1). The matrix A⊤A\mathbf{A}^\top\mathbf{A} has 1+ε21+\varepsilon^2 on its diagonal, and a computer working to about 16 digits rounds 1+10−161+10^{-16} to exactly 1. It stores [1111]\begin{bmatrix}1&1\\1&1\end{bmatrix}, which has no inverse, and the normal equations fail. QR never forms A⊤A\mathbf{A}^\top\mathbf{A}, and it returns (1,1)(1,1).

Mirrors that clear a column

Programs rarely run Gram–Schmidt itself; rounding errors slowly spoil the right angles. They use mirrors. The picture at the top of the page is one step.

A mirror is a reflection across a line, an orthogonal matrix from the Gram–Schmidt page, so it keeps lengths. Choose the mirror that sends the first column onto the first axis. The column (3,4)(3,4) has length 5, so the mirror must send it to (5,0)(5,0). The mirror line lies halfway between the two, along (2,1)(2,1). Its matrix, called a Householder reflection, is

H=I−2 vv⊤v⊤v,v=(3,4)−(5,0)=(−2,4),\mathbf{H}=\mathbf{I}-\frac{2\,\mathbf{v}\mathbf{v}^\top}{\mathbf{v}^\top\mathbf{v}},\qquad \mathbf{v}=(3,4)-(5,0)=(-2,4),

where v\mathbf{v} is at a right angle to the mirror line. Here v⊤v=20\mathbf{v}^\top\mathbf{v}=20, and H=[0.60.80.8−0.6]\mathbf{H}=\begin{bmatrix}0.6&0.8\\0.8&-0.6\end{bmatrix}. Check: H (3,4)=(1.8+3.2, 2.4−2.4)=(5,0)\mathbf{H}\,(3,4)=(1.8+3.2,\ 2.4-2.4)=(5,0). This H\mathbf{H} is local to this page; signal processing pages use bold H\mathbf{H} for a filter’s matrix.

Apply the mirror to a whole matrix whose first column is (3,4)(3,4):

H[3147]=[56.20−3.4].\mathbf{H}\begin{bmatrix}3&1\\4&7\end{bmatrix}=\begin{bmatrix}5&6.2\\0&-3.4\end{bmatrix}.

The first column is cleared below the top. The result is upper triangular, so it is R\mathbf{R}. Reflecting twice brings you back, H2=I\mathbf{H}^2=\mathbf{I}, so A=HR\mathbf{A}=\mathbf{H}\mathbf{R} with Q=H\mathbf{Q}=\mathbf{H}. Larger matrices use one mirror per column. The diagonal again carries the area: 5⋅3.4=175\cdot3.4=17, and det⁡[3147]=21−4=17\det\begin{bmatrix}3&1\\4&7\end{bmatrix}=21-4=17.

Here r22=−3.4r_{22}=-3.4 is negative. That is only a convention: a mirror that sends (3,4)(3,4) to (−5,0)(-5,0) works equally well. Programs choose the sign that keeps v\mathbf{v} large, v=a+∥a∥e1\mathbf{v}=\mathbf{a}+\lVert\mathbf{a}\rVert\mathbf{e}_1, here (8,4)(8,4). The other choice subtracts two nearly equal numbers when the column is close to the axis. For a=(1,0.01)\mathbf{a}=(1,0.01), ∥a∥=1.00005\lVert\mathbf{a}\rVert=1.00005, and 1−1.00005=−0.000051-1.00005=-0.00005 keeps only the last few digits of the two numbers.

Worked example

  1. From Gram–Schmidt. The 3×33\times3 matrix above has R\mathbf{R} with diagonal 1.41421.4142, 1.22471.2247, 1.15471.1547, whose product is 2.
  2. A line fit. R=[1.73211.732101.4142]\mathbf{R}=\begin{bmatrix}1.7321&1.7321\\0&1.4142\end{bmatrix}, Q⊤y=(3.4641,0.7071)\mathbf{Q}^\top\mathbf{y}=(3.4641,0.7071), so c1=0.5c_1=0.5 and c0=1.5c_0=1.5.
  3. Already orthonormal. If the columns of A\mathbf{A} are already orthonormal, Q=A\mathbf{Q}=\mathbf{A} and R=I\mathbf{R}=\mathbf{I}.
  4. A mirror. H=[0.60.80.8−0.6]\mathbf{H}=\begin{bmatrix}0.6&0.8\\0.8&-0.6\end{bmatrix} sends (3,4)(3,4) to (5,0)(5,0), and HH=I\mathbf{H}\mathbf{H}=\mathbf{I}.
  5. Determinant from R. The size of det⁡A\det\mathbf{A} is the product of the sizes of the diagonal of R\mathbf{R}: 17 for the mirror example.
  6. Predict. As two columns become nearly parallel, the second leftover of Gram–Schmidt shrinks, so r22r_{22} falls towards 0. That small number is the warning sign that Conditioning measures.

Where you’ll meet this

Library routines for least squares, such as NumPy’s lstsq, factor A\mathbf{A} with orthogonal matrices instead of forming A⊤A\mathbf{A}^\top\mathbf{A}. For a tall matrix, Householder QR costs about twice as much as the normal equations, and buys back the accuracy that squaring would lose. Statistics software fits regression lines this way. Some Kalman filters carry a triangular factor like R\mathbf{R} in place of a covariance matrix, for the same reason.

In signal processing · squared condition numbers

In Adaptive filters: LMS, the spread of the eigenvalues of the correlation matrix is its condition number, and it decides how slowly the filter learns. That correlation matrix is built like A⊤A\mathbf{A}^\top\mathbf{A} from the input samples, so it carries the square of the data’s own difficulty, the effect this page avoids.

Reference card

QuantityFormulaNotes
FactorisationA=QR\mathbf{A}=\mathbf{Q}\mathbf{R}Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}, R\mathbf{R} upper triangular
Entries of R\mathbf{R}rpq=qp⋅aqr_{pq}=\mathbf{q}_p\cdot\mathbf{a}_q for p<qp<q; rppr_{pp} = length of a leftoverGram–Schmidt, written down
Least squaresRx^=Q⊤y\mathbf{R}\hat{\mathbf{x}}=\mathbf{Q}^\top\mathbf{y}back substitution
Normal equationsA⊤A=R⊤R\mathbf{A}^\top\mathbf{A}=\mathbf{R}^\top\mathbf{R}they square the difficulty
HouseholderH=I−2vv⊤/(v⊤v)\mathbf{H}=\mathbf{I}-2\mathbf{v}\mathbf{v}^\top/(\mathbf{v}^\top\mathbf{v})a mirror; H2=I\mathbf{H}^2=\mathbf{I}
Clearing a columnv=a±∥a∥e1\mathbf{v}=\mathbf{a}\pm\lVert\mathbf{a}\rVert\mathbf{e}_1pick the sign that keeps v\mathbf{v} large
Determinant∣det⁡A∣=∏p∣rpp∣\lvert\det\mathbf{A}\rvert=\prod_p\lvert r_{pp}\rvertsquare A\mathbf{A}

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look