Linear Algebra — Lecture 9

Lecture 9: Orthogonal Projection and the Least Squares Method

Shuhong Gao  ·  Clemson University

These notes are interactive: click a section title to open it, click any dotted term to unfold its exact definition or statement in place, click ▸ Proof to expand a proof, and try the exercises — answers are checked instantly.

9.1  When A๐ฑ=๐›A\mathbf{x} = \mathbf{b} has no solution

💡 Intuition

So far an inconsistent system has been a dead end: row reduction produces a row (0โ‹ฏ0โˆฃc)(0\ \cdots\ 0 \mid c) with cโ‰ 0c \neq 0, and we stop. In applications this is where the work begins. Measurements carry noise, a model is only approximately right, and numbers stored in a computer are rounded. A system built from such data typically has more equations than unknowns, and then it almost never has an exact solution. Yet the data still contain information, and we want the ๐ฑ\mathbf{x} that fits them best. This lecture makes โ€œbestโ€ precise: we measure the error vector ๐›โˆ’A๐ฑ\mathbf{b} - A\mathbf{x} by its length and make it as short as possible. The answer is geometric, the closest point of a subspace, and it leads to three practical methods of computing it.

Example 9.1 (Measuring one quantity three times)

A length xx is measured three times, with results 1.101.10, 0.990.99 and 0.980.98. The โ€œsystemโ€ is x=1.10,x=0.99,x=0.98,that is,(111)x=(1.100.990.98),x = 1.10, \qquad x = 0.99, \qquad x = 0.98, \qquad\text{that is,}\qquad \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix} x = \begin{pmatrix} 1.10 \\ 0.99 \\ 0.98 \end{pmatrix}, and it is plainly inconsistent. Nobody would answer ``there is no length''; the natural answer is a compromise, such as the average (1.10+0.99+0.98)/3=3.07/3โ‰ˆ1.023(1.10 + 0.99 + 0.98)/3 = 3.07/3 \approx 1.023. We will see that the average is exactly what the least-squares method produces (Section 9.6).

Example 9.2 (Fitting a line to data)

Suppose yy is a quantity that changes over time tt, and we want to know whether there is a linear trend in yy in terms of tt. An experiment gives four data points (t,y)(t, y): (0,1),(1,1),(2,2),(3,4).(0,\, 1), \qquad (1,\, 1), \qquad (2,\, 2), \qquad (3,\, 4). We want a line y=c0+c1ty = c_0 + c_1 t that fits the data as closely as possible. If the line passed through all four points, each point would give one linear equation in the unknowns c0,c1c_0, c_1: {c0=1c0+c1=1c0+2c1=2c0+3c1=4that is,(10111213)(c0c1)=(1124).\left\{ \begin{aligned} c_0 &= 1 \\ c_0 + c_1 &= 1 \\ c_0 + 2c_1 &= 2 \\ c_0 + 3c_1 &= 4 \end{aligned} \right. \qquad\text{that is,}\qquad \begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{pmatrix} \begin{pmatrix} c_0 \\ c_1 \end{pmatrix} = \begin{pmatrix} 1 \\ 1 \\ 2 \\ 4 \end{pmatrix}. The first two equations force c0=1c_0 = 1 and c1=0c_1 = 0, and then the third equation reads 1=21 = 2. The points are not on one line, so there is no exact solution. We still want the line that comes closest to the data; it is found in Section 9.6.

Remark 9.3 (Why exact solutions are rare)

Let AA be mร—nm \times n with m>nm > n, more equations than unknowns. The system A๐ฑ=๐›A\mathbf{x} = \mathbf{b} is consistent exactly when ๐›โˆˆCol(A)\mathbf{b} \in \operatorname{Col}(A), by Theorem 6.25, and dimCol(A)=rankAโ‰คn<m\dim \operatorname{Col}(A) = \operatorname{rank} A \le n < m, by the rank theorem (Theorem 7.9). So Col(A)\operatorname{Col}(A) is a proper subspace of โ„m\mathbb{R}^m, a line or a plane or a thinner set inside a bigger space. Even if the true data ๐›\mathbf{b} lie in Col(A)\operatorname{Col}(A), a small error in the measurements, or the rounding of each entry to the digits a computer keeps, moves ๐›\mathbf{b} off this thin subspace in almost every case. Rounding the entries of AA has the same effect, since it changes the subspace itself. So with real data we must expect A๐ฑ=๐›A\mathbf{x} = \mathbf{b} to have no solution.

Remark 9.4 (The plan)

For each ๐ฑ\mathbf{x}, the vector ๐›โˆ’A๐ฑ\mathbf{b} - A\mathbf{x} is the error, also called the residual. We will choose ๐ฑ\mathbf{x} to make the residual as short as possible. As ๐ฑ\mathbf{x} varies, A๐ฑA\mathbf{x} runs through all of Col(A)\operatorname{Col}(A), so the task is: find the vector of the subspace Col(A)\operatorname{Col}(A) that is closest to ๐›\mathbf{b}. This needs a notion of length (Section 9.2), of perpendicularity and the closest point of a subspace (Sections 9.3 and 9.4), and good bases for computing it (Section 9.5). Section 9.6 returns to A๐ฑ=๐›A\mathbf{x} = \mathbf{b}.

9.2  Inner product, length, and orthogonality

💡 Intuition

Lecture 1 defined the rowโ€“column product, which multiplies a row by a column and gives a number. Pairing a column vector with another column vector in the same way, by turning the first one into a row, gives the inner product. In the plane and in space it is the familiar dot product, and it carries all the geometry we need: the length of a vector comes from Pythagoras, the distance between two points is the length of their difference, and two vectors are perpendicular when their inner product is 00. All of this makes sense in โ„n\mathbb{R}^n for every nn, where we can no longer draw, and the algebra lets us reason there as if we could.

Definition 9.5 (Inner product)

The inner product (or dot product) of ๐ฎ,๐ฏโˆˆโ„n\mathbf{u}, \mathbf{v} \in \mathbb{R}^n is the number ๐ฎโ‹…๐ฏ=๐ฎT๐ฏ=u1v1+u2v2+โ‹ฏ+unvn,\mathbf{u} \cdot \mathbf{v} = \mathbf{u}^{T}\mathbf{v} = u_1 v_1 + u_2 v_2 + \cdots + u_n v_n , the rowโ€“column product (Definition 1.8) of the row vector ๐ฎT\mathbf{u}^{T} with the column vector ๐ฏ\mathbf{v}. (For n=3n = 3 it appeared in Remark 5.15, on the cross product.)

Proposition 9.6 (Properties of the inner product)

For all ๐ฎ,๐ฏ,๐ฐโˆˆโ„n\mathbf{u}, \mathbf{v}, \mathbf{w} \in \mathbb{R}^n and every scalar cc:

  1. ๐ฎโ‹…๐ฏ=๐ฏโ‹…๐ฎ\mathbf{u} \cdot \mathbf{v} = \mathbf{v} \cdot \mathbf{u};
  2. (๐ฎ+๐ฏ)โ‹…๐ฐ=๐ฎโ‹…๐ฐ+๐ฏโ‹…๐ฐ(\mathbf{u} + \mathbf{v}) \cdot \mathbf{w} = \mathbf{u} \cdot \mathbf{w} + \mathbf{v} \cdot \mathbf{w} and (c๐ฎ)โ‹…๐ฏ=c(๐ฎโ‹…๐ฏ)(c\,\mathbf{u}) \cdot \mathbf{v} = c\,(\mathbf{u} \cdot \mathbf{v}); by (a) the same holds in the second factor;
  3. ๐ฎโ‹…๐ฎโ‰ฅ0\mathbf{u} \cdot \mathbf{u} \ge 0, and ๐ฎโ‹…๐ฎ=0\mathbf{u} \cdot \mathbf{u} = 0 if and only if ๐ฎ=0\mathbf{u} = \mathbf{0}.

Definition 9.7 (Length, distance, unit vector)

The length (or Euclidean norm) of ๐ฎโˆˆโ„n\mathbf{u} \in \mathbb{R}^n is โˆฅ๐ฎโˆฅ=๐ฎโ‹…๐ฎ=u12+u22+โ‹ฏ+un2,\|\mathbf{u}\| = \sqrt{\mathbf{u} \cdot \mathbf{u}} = \sqrt{u_1^2 + u_2^2 + \cdots + u_n^2} , which makes sense by part (c) of Proposition 9.6. The distance between ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v} is dist(๐ฎ,๐ฏ)=โˆฅ๐ฎโˆ’๐ฏโˆฅ\operatorname{dist}(\mathbf{u}, \mathbf{v}) = \|\mathbf{u} - \mathbf{v}\|. A vector of length 11 is a unit vector. For ๐ฎโ‰ 0\mathbf{u} \neq \mathbf{0}, the vector ๐ฎ/โˆฅ๐ฎโˆฅ\mathbf{u} / \|\mathbf{u}\| is a unit vector in the direction of ๐ฎ\mathbf{u}; replacing ๐ฎ\mathbf{u} by it is called normalizing ๐ฎ\mathbf{u}.

Remark 9.8 (Why this is the right length)

For ๐ฎ=(u1,u2)T\mathbf{u} = (u_1,\, u_2)^{T} in the plane, the arrow from 0\mathbf{0} to ๐ฎ\mathbf{u} is the hypotenuse of a right triangle with legs |u1||u_1| and |u2||u_2|, so its length is u12+u22\sqrt{u_1^2 + u_2^2} by Pythagoras; in space, Pythagoras applied twice gives u12+u22+u32\sqrt{u_1^2 + u_2^2 + u_3^2}. Definition 9.7 uses the same formula in every dimension. Since the ii-th entry of c๐ฎc\,\mathbf{u} is cuic u_i, โˆฅc๐ฎโˆฅ=c2u12+โ‹ฏ+c2un2=|c|โˆฅ๐ฎโˆฅ:\|c\,\mathbf{u}\| = \sqrt{c^2 u_1^2 + \cdots + c^2 u_n^2} = |c|\,\|\mathbf{u}\| : scaling a vector by cc scales its length by |c||c|. In particular โˆฅ๐ฎ/โˆฅ๐ฎโˆฅโˆฅ=1\|\mathbf{u}/\|\mathbf{u}\|\| = 1, as claimed in Definition 9.7.

Example 9.9 (Computing lengths)

Let ๐ฎ=(1,โˆ’2,2)T\mathbf{u} = (1,\, -2,\, 2)^{T} and ๐ฏ=(3,0,4)T\mathbf{v} = (3,\, 0,\, 4)^{T}. Then โˆฅ๐ฎโˆฅ=1+4+4=3,โˆฅ๐ฏโˆฅ=9+0+16=5,\|\mathbf{u}\| = \sqrt{1 + 4 + 4} = 3, \qquad \|\mathbf{v}\| = \sqrt{9 + 0 + 16} = 5, ๐ฎโ‹…๐ฏ=3+0+8=11\mathbf{u} \cdot \mathbf{v} = 3 + 0 + 8 = 11, and since ๐ฎโˆ’๐ฏ=(โˆ’2,โˆ’2,โˆ’2)T\mathbf{u} - \mathbf{v} = (-2,\, -2,\, -2)^{T}, dist(๐ฎ,๐ฏ)=12=23\operatorname{dist}(\mathbf{u}, \mathbf{v}) = \sqrt{12} = 2\sqrt{3}. The unit vector in the direction of ๐ฎ\mathbf{u} is 13(1,โˆ’2,2)T\frac{1}{3}(1,\, -2,\, 2)^{T}.

Definition 9.10 (Orthogonal vectors)

Two vectors ๐ฎ,๐ฏโˆˆโ„n\mathbf{u}, \mathbf{v} \in \mathbb{R}^n are orthogonal (or perpendicular), written ๐ฎโŠฅ๐ฏ\mathbf{u} \perp \mathbf{v}, if ๐ฎโ‹…๐ฏ=0\mathbf{u} \cdot \mathbf{v} = 0. The zero vector is orthogonal to every vector.

Theorem 9.11 (Pythagoras)

For ๐ฎ,๐ฏโˆˆโ„n\mathbf{u}, \mathbf{v} \in \mathbb{R}^n, โˆฅ๐ฏโˆ’๐ฎโˆฅ2=โˆฅ๐ฎโˆฅ2+โˆฅ๐ฏโˆฅ2โˆ’2๐ฎโ‹…๐ฏ.\|\mathbf{v} - \mathbf{u}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 - 2\,\mathbf{u} \cdot \mathbf{v} . Hence โˆฅ๐ฏโˆ’๐ฎโˆฅ2=โˆฅ๐ฎโˆฅ2+โˆฅ๐ฏโˆฅ2\|\mathbf{v} - \mathbf{u}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 if and only if ๐ฎโŠฅ๐ฏ\mathbf{u} \perp \mathbf{v}. The same holds for the sum: โˆฅ๐ฎ+๐ฏโˆฅ2=โˆฅ๐ฎโˆฅ2+โˆฅ๐ฏโˆฅ2\|\mathbf{u} + \mathbf{v}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 if and only if ๐ฎโŠฅ๐ฏ\mathbf{u} \perp \mathbf{v}.

The vectors ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v}, drawn from 0\mathbf{0}, and the vector ๐ฏโˆ’๐ฎ\mathbf{v} - \mathbf{u}, drawn from the tip of ๐ฎ\mathbf{u} to the tip of ๐ฏ\mathbf{v}, form a triangle; let ฮธ\theta be its angle at 0\mathbf{0}. By Theorem 9.11, the squared length of the side ๐ฏโˆ’๐ฎ\mathbf{v} - \mathbf{u} differs from the sum of the squared lengths of the other two sides by โˆ’2๐ฎโ‹…๐ฏ-2\,\mathbf{u} \cdot \mathbf{v}, and this term is 00 exactly when ฮธ\theta is a right angle. This is the theorem of Pythagoras from plane geometry, now valid in โ„n\mathbb{R}^n for every nn. (The remark on angles below shows that ๐ฎโ‹…๐ฏ=โˆฅ๐ฎโˆฅโˆฅ๐ฏโˆฅcosฮธ\mathbf{u} \cdot \mathbf{v} = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta, so the identity in the theorem is the law of cosines.)

2026-11-09T22:32:43.685702 image/svg+xml Matplotlib v3.10.9, https://matplotlib.org/
The triangle with sides ๐ฎ\mathbf{u}, ๐ฏ\mathbf{v} and ๐ฏโˆ’๐ฎ\mathbf{v} - \mathbf{u} (from the tip of ๐ฎ\mathbf{u} to the tip of ๐ฏ\mathbf{v}) and the angle ฮธ\theta at 0\mathbf{0}, drawn for ๐ฎ=(3,1)T\mathbf{u} = (3,\, 1)^{T} and ๐ฏ=(1,3)T\mathbf{v} = (1,\, 3)^{T}. Here โˆฅ๐ฎโˆฅ2=โˆฅ๐ฏโˆฅ2=10\|\mathbf{u}\|^2 = \|\mathbf{v}\|^2 = 10 and ๐ฎโ‹…๐ฏ=6\mathbf{u} \cdot \mathbf{v} = 6, so โˆฅ๐ฏโˆ’๐ฎโˆฅ2=10+10โˆ’12=8\|\mathbf{v} - \mathbf{u}\|^2 = 10 + 10 - 12 = 8, less than โˆฅ๐ฎโˆฅ2+โˆฅ๐ฏโˆฅ2\|\mathbf{u}\|^2 + \|\mathbf{v}\|^2 because ฮธ\theta is smaller than a right angle. When ๐ฎโ‹…๐ฏ=0\mathbf{u} \cdot \mathbf{v} = 0, the angle is a right angle and โˆฅ๐ฏโˆ’๐ฎโˆฅ2=โˆฅ๐ฎโˆฅ2+โˆฅ๐ฏโˆฅ2\|\mathbf{v} - \mathbf{u}\|^2 = \|\mathbf{u}\|^2 + \|\mathbf{v}\|^2.
Theorem 9.12 (Cauchyโ€“Schwarz and triangle inequalities)

For all ๐ฎ,๐ฏโˆˆโ„n\mathbf{u}, \mathbf{v} \in \mathbb{R}^n, |๐ฎโ‹…๐ฏ|โ‰คโˆฅ๐ฎโˆฅโˆฅ๐ฏโˆฅandโˆฅ๐ฎ+๐ฏโˆฅโ‰คโˆฅ๐ฎโˆฅ+โˆฅ๐ฏโˆฅ.|\mathbf{u} \cdot \mathbf{v}| \le \|\mathbf{u}\|\,\|\mathbf{v}\| \qquad\text{and}\qquad \|\mathbf{u} + \mathbf{v}\| \le \|\mathbf{u}\| + \|\mathbf{v}\| .

The proof of Theorem 9.12 splits ๐ฏ\mathbf{v} into two perpendicular parts, ๐ฏ=c๐ฎ+๐ณ,c=๐ฎโ‹…๐ฏ๐ฎโ‹…๐ฎ,๐ณ=๐ฏโˆ’c๐ฎโŠฅ๐ฎ,\mathbf{v} = c\,\mathbf{u} + \mathbf{z}, \qquad c = \frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{u} \cdot \mathbf{u}}, \qquad \mathbf{z} = \mathbf{v} - c\,\mathbf{u} \perp \mathbf{u},(9.1) as in the figure that follows. The vector c๐ฎc\,\mathbf{u} lies on the line Span(๐ฎ)\operatorname{Span}(\mathbf{u}) and is the foot of the perpendicular dropped from ๐ฏ\mathbf{v} to that line: it is the projection of ๐ฏ\mathbf{v} onto the line through ๐ฎ\mathbf{u}, and โˆฅ๐ณโˆฅ\|\mathbf{z}\| is the distance from ๐ฏ\mathbf{v} to the line. This is the first instance of the main construction of this lecture.

2026-11-09T22:32:43.959895 image/svg+xml Matplotlib v3.10.9, https://matplotlib.org/
The projection of ๐ฏ\mathbf{v} onto the line through ๐ฎ\mathbf{u}: ๐ฏ=c๐ฎ+๐ณ\mathbf{v} = c\,\mathbf{u} + \mathbf{z} with c๐ฎc\,\mathbf{u} on the line and ๐ณ=๐ฏโˆ’c๐ฎ\mathbf{z} = \mathbf{v} - c\,\mathbf{u} perpendicular to it. Drawn for ๐ฎ=(1.5,0.5)T\mathbf{u} = (1.5,\, 0.5)^{T} and ๐ฏ=(1.6,2.4)T\mathbf{v} = (1.6,\, 2.4)^{T}, where c=3.6/2.5=1.44c = 3.6/2.5 = 1.44, c๐ฎ=(2.16,0.72)Tc\,\mathbf{u} = (2.16,\, 0.72)^{T} and ๐ณ=(โˆ’0.56,1.68)T\mathbf{z} = (-0.56,\, 1.68)^{T}. The angle ฮธ\theta between ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v} is defined in the remark below.

The second inequality is the triangle inequality. Draw ๐ฎ\mathbf{u} from 0\mathbf{0} and then ๐ฏ\mathbf{v} from the tip of ๐ฎ\mathbf{u}; the arrow from 0\mathbf{0} to the tip of ๐ฏ\mathbf{v} is ๐ฎ+๐ฏ\mathbf{u} + \mathbf{v}. The three arrows form a triangle, and the inequality says that the side ๐ฎ+๐ฏ\mathbf{u} + \mathbf{v} is never longer than the other two sides together: going straight is never longer than going around.

2026-11-10T13:02:59.237543 image/svg+xml Matplotlib v3.10.9, https://matplotlib.org/
The triangle inequality: the side ๐ฎ+๐ฏ\mathbf{u} + \mathbf{v} is at most as long as the path along ๐ฎ\mathbf{u} followed by ๐ฏ\mathbf{v}. Drawn for ๐ฎ=(3,1)T\mathbf{u} = (3,\, 1)^{T} and ๐ฏ=(1,2)T\mathbf{v} = (1,\, 2)^{T}, where โˆฅ๐ฎ+๐ฏโˆฅ=โˆฅ(4,3)Tโˆฅ=5\|\mathbf{u} + \mathbf{v}\| = \|(4,\, 3)^{T}\| = 5 and โˆฅ๐ฎโˆฅ+โˆฅ๐ฏโˆฅ=10+5โ‰ˆ5.40\|\mathbf{u}\| + \|\mathbf{v}\| = \sqrt{10} + \sqrt{5} \approx 5.40.
Remark 9.13 (Correlation and angles)

By Cauchyโ€“Schwarz (Theorem 9.12), for nonzero ๐ฎ,๐ฏโˆˆโ„n\mathbf{u}, \mathbf{v} \in \mathbb{R}^n we have the inequality of the ratio: โˆ’1โ‰คฯ=๐ฎโ‹…๐ฏโˆฅ๐ฎโˆฅโˆฅ๐ฏโˆฅโ‰ค1.-1 \;\le\; \rho = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|} \;\le\; 1 . The number ฯ\rho measures how strongly ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v} are correlated. It equals 11 when ๐ฏ\mathbf{v} is a positive multiple of ๐ฎ\mathbf{u} and โˆ’1-1 when ๐ฏ\mathbf{v} is a negative multiple of ๐ฎ\mathbf{u} (in the proof of Theorem 9.12, equality holds exactly when ๐ณ=0\mathbf{z} = \mathbf{0}, that is, when ๐ฏ=c๐ฎ\mathbf{v} = c\,\mathbf{u}), and it is 00 when ๐ฎโŠฅ๐ฏ\mathbf{u} \perp \mathbf{v}. If the entries of ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v} are two series of measurements, each with its mean subtracted, then ฯ\rho is the correlation coefficient of statistics. In machine learning ฯ\rho is called the cosine similarity and is used everywhere: words, documents, images or users are represented by vectors, and two of them are considered similar when the cosine similarity of their vectors is close to 11; search engines and recommendation systems rank their results this way.

Since โˆ’1โ‰คฯโ‰ค1-1 \le \rho \le 1, there is exactly one angle ฮธโˆˆ[0,ฯ€]\theta \in [0, \pi] with cosฮธ=ฯ\cos\theta = \rho; it is called the angle between ๐ฎ\mathbf{u} and ๐ฏ\mathbf{v}. In the plane this agrees with the angle of elementary geometry, by the law of cosines. For the vectors of Example 9.9, ฯ=cosฮธ=11/15\rho = \cos\theta = 11/15. Orthogonal means ฯ=0\rho = 0, that is, ฮธ=ฯ€/2\theta = \pi/2. For the decomposition ๐ฏ=c๐ฎ+๐ณ\mathbf{v} = c\,\mathbf{u} + \mathbf{z} in (9.1): when ฮธ\theta is acute, c>0c > 0 and โˆฅc๐ฎโˆฅ=โˆฅ๐ฏโˆฅcosฮธ\|c\,\mathbf{u}\| = \|\mathbf{v}\|\cos\theta.

Definition 9.14 (Orthogonal sets, matrices and bases)

Let ๐ฎ1,โ€ฆ,๐ฎk\mathbf{u}_1, \dots, \mathbf{u}_k be vectors in โ„n\mathbb{R}^n.

  1. The vectors ๐ฎ1,โ€ฆ,๐ฎk\mathbf{u}_1, \dots, \mathbf{u}_k form an orthogonal set if ๐ฎiโ‹…๐ฎj=0\mathbf{u}_i \cdot \mathbf{u}_j = 0 for all iโ‰ ji \neq j, and an orthonormal set if moreover each ๐ฎi\mathbf{u}_i is a unit vector.
  2. An nร—kn \times k matrix is column orthogonal if its columns form an orthogonal set, and column orthonormal if its columns form an orthonormal set. A square column orthonormal matrix is called an orthogonal matrix.
  3. A basis of a vector space (in this lecture, of โ„n\mathbb{R}^n or of a subspace of โ„n\mathbb{R}^n) is an orthogonal basis if it is an orthogonal set, and an orthonormal basis if it is an orthonormal set.

Lemma 9.15 (The matrix VTHV^{T}H records inner products)

Let V=(๐ฏ1โ‹ฏ๐ฏk)โˆˆโ„nร—kV = (\mathbf{v}_1\ \cdots\ \mathbf{v}_k) \in \mathbb{R}^{n \times k} and H=(๐ก1โ‹ฏ๐กr)โˆˆโ„nร—rH = (\mathbf{h}_1\ \cdots\ \mathbf{h}_r) \in \mathbb{R}^{n \times r}. Then VTHV^{T}H is the kร—rk \times r matrix whose (i,j)(i, j) entry is ๐ฏiโ‹…๐กj\mathbf{v}_i \cdot \mathbf{h}_j. In particular, HTHH^{T}H is the rร—rr \times r matrix whose (i,j)(i, j) entry is ๐กiโ‹…๐กj\mathbf{h}_i \cdot \mathbf{h}_j; consequently HH is column orthogonal if and only if HTHH^{T}H is diagonal, and column orthonormal if and only if HTH=IrH^{T}H = I_r.

Remark 9.16 (Correlation tables and attention in transformers)

Suppose that all columns of VV and of HH are unit vectors. Then the (i,j)(i, j) entry of VTHV^{T}H is the correlation ฯ=๐ฏiโ‹…๐กj\rho = \mathbf{v}_i \cdot \mathbf{h}_j of Remark 9.13, so VTHV^{T}H is a table showing how strongly each column of VV is correlated with each column of HH: an entry near 11 means that the two vectors point in nearly the same direction, an entry near 00 that they are unrelated, and an entry near โˆ’1-1 that they point in nearly opposite directions. All of these inner products are computed at once, by a single matrix product, which computers do very fast.

This is the core computation of the attention mechanism in transformers, the neural networks behind today's large language models. Machine learning stores data in rows: a text of mm words (tokens) is an mร—dm \times d matrix XX whose ii-th row is the vector representing word ii, and the columns of XX are the dd features. From XX the network computes two mร—dโ€ฒm \times d' matrices Q=XWQQ = XW_Q and K=XWKK = XW_K (the weight matrices WQW_Q and WKW_K are learned in training). Row ii of QQ is the query ๐ชiT\mathbf{q}_i^{T} of word ii (โ€œwhat is this word looking for?โ€), and row jj of KK is the key ๐คjT\mathbf{k}_j^{T} of word jj (``what does this word offer?''). The table of all scores is the mร—mm \times m matrix QKT,(QKT)ij=๐ชiโ‹…๐คj,QK^{T}, \qquad (QK^{T})_{ij} = \mathbf{q}_i \cdot \mathbf{k}_j , which is Lemma 9.15 with V=QTV = Q^{T} and H=KTH = K^{T}, whose columns are the queries and the keys. The entry ๐ชiโ‹…๐คj\mathbf{q}_i \cdot \mathbf{k}_j scores how relevant word jj is to word ii, and these scores decide how much each word draws on every other word. In practice the vectors are not normalized, the scores are divided by dโ€ฒ\sqrt{d'}, and each row of the table is turned into weights by the softmax function. (The letters QQ and KK follow the machine learning literature and are used only in this remark; elsewhere in these notes QQ denotes a column orthonormal matrix, as in the QR factorization.)

Example 9.17 (A column orthogonal 4ร—44 \times 4 matrix)

Let H=(111111โˆ’1โˆ’11โˆ’11โˆ’11โˆ’1โˆ’11)=(๐ก1๐ก2๐ก3๐ก4).H = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & 1 & -1 \\ 1 & -1 & -1 & 1 \end{pmatrix} = (\mathbf{h}_1\ \mathbf{h}_2\ \mathbf{h}_3\ \mathbf{h}_4) . Any two different columns agree in exactly two positions and disagree in the other two, so their inner product is 1+1โˆ’1โˆ’1=01 + 1 - 1 - 1 = 0, and each column has โˆฅ๐กiโˆฅ2=4\|\mathbf{h}_i\|^2 = 4. By Lemma 9.15, HTH=(4000040000400004)=4I4.H^{T}H = \begin{pmatrix} 4 & 0 & 0 & 0 \\ 0 & 4 & 0 & 0 \\ 0 & 0 & 4 & 0 \\ 0 & 0 & 0 & 4 \end{pmatrix} = 4 I_4 . So HH is column orthogonal, and U=12HU = \frac{1}{2}H is column orthonormal: UTU=14HTH=I4U^{T}U = \frac{1}{4}H^{T}H = I_4. In particular Hโˆ’1=14HTH^{-1} = \frac{1}{4}H^{T}, without any row reduction. (Matrices of ยฑ1\pm 1 entries with orthogonal columns are called Hadamard matrices; they are used in coding and signal processing.) Two columns alone also give a column orthonormal matrix: K=12(๐ก1๐ก2)=12(11111โˆ’11โˆ’1),KTK=I2,K = \frac{1}{2}(\mathbf{h}_1\ \mathbf{h}_2) = \frac{1}{2}\begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 1 & -1 \\ 1 & -1 \end{pmatrix}, \qquad K^{T}K = I_2 , but KKTโ‰ I4KK^{T} \neq I_4: a non-square column orthonormal matrix has a left inverse KTK^{T} but no inverse.

Theorem 9.18 (Orthogonal sets are independent)

Let ๐ฎ1,โ€ฆ,๐ฎk\mathbf{u}_1, \dots, \mathbf{u}_k be an orthogonal set of nonzero vectors in โ„n\mathbb{R}^n, and let W=Span(๐ฎ1,โ€ฆ,๐ฎk)W = \operatorname{Span}(\mathbf{u}_1, \dots, \mathbf{u}_k). Then:

  1. ๐ฎ1,โ€ฆ,๐ฎk\mathbf{u}_1, \dots, \mathbf{u}_k are linearly independent, so they form an orthogonal basis of WW;
  2. every ๐ฒโˆˆW\mathbf{y} \in W has a unique representation ๐ฒ=c1๐ฎ1+โ‹ฏ+ck๐ฎk,wherecj=๐ฒโ‹…๐ฎj๐ฎjโ‹…๐ฎj,1โ‰คjโ‰คk.\mathbf{y} = c_1\mathbf{u}_1 + \cdots + c_k\mathbf{u}_k , \qquad\text{where}\qquad c_j = \frac{\mathbf{y} \cdot \mathbf{u}_j}{\mathbf{u}_j \cdot \mathbf{u}_j}, \quad 1 \le j \le k .

Example 9.19 (Coordinates without row reduction)

The columns of HH in Example 9.17 are four orthogonal nonzero vectors in โ„4\mathbb{R}^4, hence a basis of โ„4\mathbb{R}^4 (by Theorem 9.18 and the basis test (Theorem 6.48)). To write ๐ฒ=(1,2,3,4)T\mathbf{y} = (1,\, 2,\, 3,\, 4)^{T} in this basis, compute ๐ฒโ‹…๐ก1=10\mathbf{y} \cdot \mathbf{h}_1 = 10, ๐ฒโ‹…๐ก2=โˆ’4\mathbf{y} \cdot \mathbf{h}_2 = -4, ๐ฒโ‹…๐ก3=โˆ’2\mathbf{y} \cdot \mathbf{h}_3 = -2, ๐ฒโ‹…๐ก4=0\mathbf{y} \cdot \mathbf{h}_4 = 0 and divide each by 44: ๐ฒ=52๐ก1โˆ’๐ก2โˆ’12๐ก3+0๐ก4.\mathbf{y} = \tfrac{5}{2}\,\mathbf{h}_1 - \mathbf{h}_2 - \tfrac{1}{2}\,\mathbf{h}_3 + 0\,\mathbf{h}_4 . Check the first entry: 52โˆ’1โˆ’12+0=1\frac{5}{2} - 1 - \frac{1}{2} + 0 = 1.

Proposition 9.20 (Column orthonormal matrices preserve inner products)

Let HH be an nร—kn \times k column orthonormal matrix, that is, HTH=IkH^{T}H = I_k. Then for all ๐ฑ,๐ฒโˆˆโ„k\mathbf{x}, \mathbf{y} \in \mathbb{R}^k, (H๐ฑ)โ‹…(H๐ฒ)=๐ฑโ‹…๐ฒ.(H\mathbf{x}) \cdot (H\mathbf{y}) = \mathbf{x} \cdot \mathbf{y} . Consequently โˆฅH๐ฑโˆฅ=โˆฅ๐ฑโˆฅ\|H\mathbf{x}\| = \|\mathbf{x}\|, dist(H๐ฑ,H๐ฒ)=dist(๐ฑ,๐ฒ)\operatorname{dist}(H\mathbf{x}, H\mathbf{y}) = \operatorname{dist}(\mathbf{x}, \mathbf{y}), and H๐ฑโŠฅH๐ฒH\mathbf{x} \perp H\mathbf{y} if and only if ๐ฑโŠฅ๐ฒ\mathbf{x} \perp \mathbf{y}: the map ๐ฑโ†ฆH๐ฑ\mathbf{x} \mapsto H\mathbf{x} keeps lengths, distances, angles and right angles. Conversely, a matrix HH with (H๐ฑ)โ‹…(H๐ฒ)=๐ฑโ‹…๐ฒ(H\mathbf{x}) \cdot (H\mathbf{y}) = \mathbf{x} \cdot \mathbf{y} for all ๐ฑ,๐ฒ\mathbf{x}, \mathbf{y} is column orthonormal.

Example 9.21 (Checking the proposition)

Let U=12HU = \frac{1}{2}H with HH from Example 9.17, and let ๐ฑ=(1,2,0,1)T\mathbf{x} = (1,\, 2,\, 0,\, 1)^{T}, ๐ฒ=(0,1,3,โˆ’1)T\mathbf{y} = (0,\, 1,\, 3,\, -1)^{T}. Then ๐ฑโ‹…๐ฒ=2โˆ’1=1\mathbf{x} \cdot \mathbf{y} = 2 - 1 = 1, and U๐ฑ=(21โˆ’10),U๐ฒ=12(3โˆ’13โˆ’5),(U๐ฑ)โ‹…(U๐ฒ)=6โˆ’1โˆ’3+02=1.U\mathbf{x} = \begin{pmatrix} 2 \\ 1 \\ -1 \\ 0 \end{pmatrix}, \qquad U\mathbf{y} = \frac{1}{2}\begin{pmatrix} 3 \\ -1 \\ 3 \\ -5 \end{pmatrix}, \qquad (U\mathbf{x}) \cdot (U\mathbf{y}) = \frac{6 - 1 - 3 + 0}{2} = 1 . Also โˆฅU๐ฑโˆฅ2=4+1+1=6=โˆฅ๐ฑโˆฅ2\|U\mathbf{x}\|^2 = 4 + 1 + 1 = 6 = \|\mathbf{x}\|^2. For HH itself, which is column orthogonal but not orthonormal, HTH=4I4H^{T}H = 4I_4 and the same computation gives (H๐ฑ)โ‹…(H๐ฒ)=4(๐ฑโ‹…๐ฒ)(H\mathbf{x}) \cdot (H\mathbf{y}) = 4\,(\mathbf{x} \cdot \mathbf{y}): HH keeps right angles but doubles every length. The rotation matrices RฮธR_\theta of Example 3.17 are orthogonal matrices, which is why rotations keep lengths.

Exercise 9.1 auto-check

What is the length of (2,โˆ’1,2,0)T(2,\, -1,\, 2,\, 0)^{T}?

Answer:
Exercise 9.2 auto-check

For which number aa are (1,a,2)T(1,\, a,\, 2)^{T} and (3,1,โˆ’1)T(3,\, 1,\, -1)^{T} orthogonal?

Answer:
Exercise 9.3 auto-check

Let KK be a 5ร—35 \times 3 matrix with KTK=I3K^{T}K = I_3. Which statement is true?

9.3  The orthogonal complement and the projection theorem

💡 Intuition

Here is the problem left open in Section 9.1. We are given a subspace WW of โ„n\mathbb{R}^n (for least squares, W=Col(A)W = \operatorname{Col}(A)) and a vector ๐›\mathbf{b}, usually not in WW, and we want the vector ๐›ฬ‚\hat{\mathbf{b}} of WW that is closest to ๐›\mathbf{b}. The figure below shows the answer when WW is a plane in โ„3\mathbb{R}^3: drop the perpendicular from ๐›\mathbf{b} to the plane. Its foot is ๐›ฬ‚\hat{\mathbf{b}}, and ๐›\mathbf{b} splits into two pieces, ๐›=๐›ฬ‚+๐ณ,๐›ฬ‚โˆˆW,๐ณโŠฅevery vector of W.\mathbf{b} = \hat{\mathbf{b}} + \mathbf{z}, \qquad \hat{\mathbf{b}} \in W, \qquad \mathbf{z} \perp \text{every vector of } W . Why is ๐›ฬ‚\hat{\mathbf{b}} the closest point? For any other point ๐ฐ\mathbf{w} of WW, the points ๐›\mathbf{b}, ๐›ฬ‚\hat{\mathbf{b}}, ๐ฐ\mathbf{w} form a right triangle with the right angle at ๐›ฬ‚\hat{\mathbf{b}}, so the segment from ๐›\mathbf{b} to ๐ฐ\mathbf{w} is a hypotenuse and is longer than ๐ณ\mathbf{z}. In โ„n\mathbb{R}^n we cannot draw, so we prove both facts. In this section we name the set WโŠฅW^{\perp} of all vectors perpendicular to WW, describe it as a null space, and prove that every ๐›\mathbf{b} splits uniquely as ๐›ฬ‚+๐ณ\hat{\mathbf{b}} + \mathbf{z} with ๐›ฬ‚โˆˆW\hat{\mathbf{b}} \in W and ๐ณโˆˆWโŠฅ\mathbf{z} \in W^{\perp}; the proof also computes ๐›ฬ‚\hat{\mathbf{b}} by solving a small square system. Section 9.4 then shows, with Pythagoras, that ๐›ฬ‚\hat{\mathbf{b}} is the closest point.

2026-11-09T22:32:44.068572 image/svg+xml Matplotlib v3.10.9, https://matplotlib.org/
The orthogonal projection of ๐›=(3,1,5)T\mathbf{b} = (3,\,1,\,5)^{T} onto the plane WW spanned by ๐ฎ1=(1,1,0)T\mathbf{u}_1 = (1,\,1,\,0)^{T} and ๐ฎ2=(โˆ’1,1,1)T\mathbf{u}_2 = (-1,\,1,\,1)^{T}, computed in the examples of this section: ๐›=๐›ฬ‚+๐ณ\mathbf{b} = \hat{\mathbf{b}} + \mathbf{z} with ๐›ฬ‚=(1,3,1)T\hat{\mathbf{b}} = (1,\,3,\,1)^{T} in WW and ๐ณ=(2,โˆ’2,4)T\mathbf{z} = (2,\,-2,\,4)^{T} perpendicular to WW. For any other point ๐ฐ\mathbf{w} of WW the segment from ๐›\mathbf{b} to ๐ฐ\mathbf{w} is the hypotenuse of a right triangle, hence longer than ๐ณ\mathbf{z}.
Definition 9.22 (Orthogonal complement)

Let WW be a subspace of โ„n\mathbb{R}^n. A vector ๐ณ\mathbf{z} is orthogonal to WW if ๐ณโ‹…๐ฐ=0\mathbf{z} \cdot \mathbf{w} = 0 for every ๐ฐโˆˆW\mathbf{w} \in W. The set of all such vectors is the orthogonal complement of WW, WโŠฅ={๐ณโˆˆโ„n:๐ณโ‹…๐ฐ=0 for all ๐ฐโˆˆW}W^{\perp} = \{\, \mathbf{z} \in \mathbb{R}^n : \mathbf{z} \cdot \mathbf{w} = 0 \text{ for all } \mathbf{w} \in W \,\} (read โ€œWW perpโ€).

Proposition 9.23 (Computing WโŠฅW^{\perp})

Let W=Span(๐ฐ1,โ€ฆ,๐ฐk)W = \operatorname{Span}(\mathbf{w}_1, \dots, \mathbf{w}_k) be a subspace of โ„n\mathbb{R}^n, and let B=(๐ฐ1โ‹ฏ๐ฐk)B = (\mathbf{w}_1\ \cdots\ \mathbf{w}_k) be the nร—kn \times k matrix with these columns. Then:

  1. ๐ณโˆˆWโŠฅ\mathbf{z} \in W^{\perp} if and only if ๐ณโ‹…๐ฐi=0\mathbf{z} \cdot \mathbf{w}_i = 0 for i=1,โ€ฆ,ki = 1, \dots, k;
  2. WโŠฅ=Nul(BT)W^{\perp} = \operatorname{Nul}(B^{T}); in particular WโŠฅW^{\perp} is a subspace of โ„n\mathbb{R}^n;
  3. WโˆฉWโŠฅ={0}W \cap W^{\perp} = \{\mathbf{0}\}.

Proposition 9.24 (The dimension of WโŠฅW^{\perp})

For every subspace WW of โ„n\mathbb{R}^n, dimW+dimWโŠฅ=nand(WโŠฅ)โŠฅ=W.\dim W + \dim W^{\perp} = n \qquad\text{and}\qquad (W^{\perp})^{\perp} = W .

Remark 9.25 (The four subspaces of a matrix)

For an mร—nm \times n matrix AA, Proposition 9.23 applied to the columns of ATA^{T} (the rows of AA, transposed) gives Row(A)โŠฅ=Nul(A) in โ„n,Col(A)โŠฅ=Nul(AT) in โ„m.\operatorname{Row}(A)^{\perp} = \operatorname{Nul}(A) \ \text{ in } \mathbb{R}^n, \qquad \operatorname{Col}(A)^{\perp} = \operatorname{Nul}(A^{T}) \ \text{ in } \mathbb{R}^m . The first identity says something we have used since Lecture 1 without the words: ๐ฑ\mathbf{x} solves A๐ฑ=0A\mathbf{x} = \mathbf{0} exactly when ๐ฑ\mathbf{x} is orthogonal to every row of AA. The dimension count dimRow(A)+dimNul(A)=n\dim \operatorname{Row}(A) + \dim \operatorname{Nul}(A) = n is the rank theorem (Theorem 7.9) again.

Example 9.26 (A plane and its normal line)

Let W=Span(๐ฎ1,๐ฎ2)W = \operatorname{Span}(\mathbf{u}_1, \mathbf{u}_2) in โ„3\mathbb{R}^3 with ๐ฎ1=(1,1,0)T\mathbf{u}_1 = (1,\, 1,\, 0)^{T} and ๐ฎ2=(โˆ’1,1,1)T\mathbf{u}_2 = (-1,\, 1,\, 1)^{T}, a plane through 0\mathbf{0}. By Proposition 9.23, WโŠฅW^{\perp} is the solution set of {x1+x2=0โˆ’x1+x2+x3=0\left\{ \begin{aligned} x_1 + x_2 &= 0 \\ -x_1 + x_2 + x_3 &= 0 \end{aligned} \right. The first equation gives x2=โˆ’x1x_2 = -x_1, and then the second gives x3=x1โˆ’x2=2x1x_3 = x_1 - x_2 = 2x_1. So WโŠฅ=Span(๐ง)W^{\perp} = \operatorname{Span}(\mathbf{n}) with ๐ง=(1,โˆ’1,2)T\mathbf{n} = (1,\, -1,\, 2)^{T}, a line, and indeed dimW+dimWโŠฅ=2+1=3\dim W + \dim W^{\perp} = 2 + 1 = 3. The vector ๐ง\mathbf{n} is a normal vector of the plane, which is the solution set of the single equation x1โˆ’x2+2x3=0x_1 - x_2 + 2x_3 = 0, that is, W=Nul(๐งT)W = \operatorname{Nul}(\mathbf{n}^{T}), in agreement with W=(WโŠฅ)โŠฅW = (W^{\perp})^{\perp}.

Example 9.27 (Complements in โ„4\mathbb{R}^4)

With HH from Example 9.17, let W=Span(๐ก1,๐ก2)W = \operatorname{Span}(\mathbf{h}_1, \mathbf{h}_2). The vectors ๐ก3,๐ก4\mathbf{h}_3, \mathbf{h}_4 are orthogonal to ๐ก1\mathbf{h}_1 and ๐ก2\mathbf{h}_2, so they lie in WโŠฅW^{\perp} by part (a) of Proposition 9.23; they are independent, and dimWโŠฅ=4โˆ’2=2\dim W^{\perp} = 4 - 2 = 2, so WโŠฅ=Span(๐ก3,๐ก4)W^{\perp} = \operatorname{Span}(\mathbf{h}_3, \mathbf{h}_4) by the basis test (Theorem 6.48).

Lemma 9.28 (BTBB^{T}B is invertible)

Let BB be an nร—kn \times k matrix whose columns are linearly independent. Then the kร—kk \times k matrix BTBB^{T}B is invertible.

Theorem 9.29 (The orthogonal projection theorem)

Let WW be a subspace of โ„n\mathbb{R}^n and ๐›โˆˆโ„n\mathbf{b} \in \mathbb{R}^n. Then ๐›\mathbf{b} can be written uniquely as ๐›=๐›ฬ‚+๐ณ,๐›ฬ‚โˆˆW,๐ณโˆˆWโŠฅ.\mathbf{b} = \hat{\mathbf{b}} + \mathbf{z}, \qquad \hat{\mathbf{b}} \in W, \quad \mathbf{z} \in W^{\perp} .(9.2) Moreover, for any basis B=(๐ฐ1โ‹ฏ๐ฐk)B = (\mathbf{w}_1\ \cdots\ \mathbf{w}_k) of WW, we have ๐›ฬ‚=B๐œ,where๐œ=(BTB)โˆ’1BT๐›,\hat{\mathbf{b}} = B\mathbf{c}, \qquad\text{where}\qquad \mathbf{c} = (B^{T}B)^{-1}B^{T}\mathbf{b} , that is, ๐œ\mathbf{c} is the unique solution of the kร—kk \times k system BTB๐œ=BT๐›.B^{T}B\,\mathbf{c} = B^{T}\mathbf{b} .(9.3)

Definition 9.30 (Orthogonal projection)

In Theorem 9.29, ๐›ฬ‚\hat{\mathbf{b}} is called the orthogonal projection of ๐›\mathbf{b} onto WW and is denoted projW๐›\operatorname{proj}_W \mathbf{b}; the vector ๐ณ=๐›โˆ’๐›ฬ‚\mathbf{z} = \mathbf{b} - \hat{\mathbf{b}} is the component of ๐›\mathbf{b} orthogonal to WW. By the uniqueness in Theorem 9.29, ๐›ฬ‚\hat{\mathbf{b}} depends only on WW and ๐›\mathbf{b}, not on the basis used to compute it.

Example 9.31 (Projecting onto a plane)

Let WW be the plane of Example 9.26, and use the basis ๐ฐ1=(1,1,0)T\mathbf{w}_1 = (1,\, 1,\, 0)^{T}, ๐ฐ2=(0,2,1)T\mathbf{w}_2 = (0,\, 2,\, 1)^{T} of WW (note ๐ฐ2=๐ฎ1+๐ฎ2\mathbf{w}_2 = \mathbf{u}_1 + \mathbf{u}_2, and ๐ฐ1โ‹…๐ฐ2=2โ‰ 0\mathbf{w}_1 \cdot \mathbf{w}_2 = 2 \neq 0). Project ๐›=(3,1,5)T\mathbf{b} = (3,\, 1,\, 5)^{T} by solving (9.3). With B=(๐ฐ1๐ฐ2)B = (\mathbf{w}_1\ \mathbf{w}_2), BTB=(๐ฐ1โ‹…๐ฐ1๐ฐ1โ‹…๐ฐ2๐ฐ2โ‹…๐ฐ1๐ฐ2โ‹…๐ฐ2)=(2225),BT๐›=(๐ฐ1โ‹…๐›๐ฐ2โ‹…๐›)=(47).B^{T}B = \begin{pmatrix} \mathbf{w}_1 \cdot \mathbf{w}_1 & \mathbf{w}_1 \cdot \mathbf{w}_2 \\ \mathbf{w}_2 \cdot \mathbf{w}_1 & \mathbf{w}_2 \cdot \mathbf{w}_2 \end{pmatrix} = \begin{pmatrix} 2 & 2 \\ 2 & 5 \end{pmatrix}, \qquad B^{T}\mathbf{b} = \begin{pmatrix} \mathbf{w}_1 \cdot \mathbf{b} \\ \mathbf{w}_2 \cdot \mathbf{b} \end{pmatrix} = \begin{pmatrix} 4 \\ 7 \end{pmatrix}. The system 2c1+2c2=42c_1 + 2c_2 = 4, 2c1+5c2=72c_1 + 5c_2 = 7 gives 3c2=33c_2 = 3, so c2=1c_2 = 1 and c1=1c_1 = 1. Hence ๐›ฬ‚=๐ฐ1+๐ฐ2=(131),๐ณ=๐›โˆ’๐›ฬ‚=(2โˆ’24)=2๐ง.\hat{\mathbf{b}} = \mathbf{w}_1 + \mathbf{w}_2 = \begin{pmatrix} 1 \\ 3 \\ 1 \end{pmatrix}, \qquad \mathbf{z} = \mathbf{b} - \hat{\mathbf{b}} = \begin{pmatrix} 2 \\ -2 \\ 4 \end{pmatrix} = 2\,\mathbf{n} . As a check, ๐ณโ‹…๐ฐ1=2โˆ’2+0=0\mathbf{z} \cdot \mathbf{w}_1 = 2 - 2 + 0 = 0 and ๐ณโ‹…๐ฐ2=0โˆ’4+4=0\mathbf{z} \cdot \mathbf{w}_2 = 0 - 4 + 4 = 0, and ๐ณ\mathbf{z} is a multiple of the normal vector ๐ง=(1,โˆ’1,2)T\mathbf{n} = (1,\, -1,\, 2)^{T}. This is the situation drawn in the figure at the beginning of this section.

Exercise 9.4 auto-check

WW is a 22-dimensional subspace of โ„5\mathbb{R}^5. What is dimWโŠฅ\dim W^{\perp}?

Answer:
Exercise 9.5 auto-check

Let WW be the plane x1+2x2โˆ’x3=0x_1 + 2x_2 - x_3 = 0 in โ„3\mathbb{R}^3. Which vector spans WโŠฅW^{\perp}?

9.4  Distance to a subspace and the projection formula

💡 Intuition

The picture of the projection theorem promised more than a decomposition: the foot of the perpendicular is the point of WW closest to ๐›\mathbf{b}. This is Pythagoras once more, and it is the fact that will solve the least-squares problem. The section then collects the algebraic properties of ๐›โ†ฆ๐›ฬ‚\mathbf{b} \mapsto \hat{\mathbf{b}} and gives the formula that makes projections easy to compute: when the basis of WW is orthogonal, the system (9.3) is diagonal and can be solved by inspection.

Theorem 9.32 (Best approximation)

Let WW be a subspace of โ„n\mathbb{R}^n, ๐›โˆˆโ„n\mathbf{b} \in \mathbb{R}^n and ๐›ฬ‚=projW๐›\hat{\mathbf{b}} = \operatorname{proj}_W \mathbf{b}. Then for every ๐ฐโˆˆW\mathbf{w} \in W, โˆฅ๐›โˆ’๐ฐโˆฅ2=โˆฅ๐›โˆ’๐›ฬ‚โˆฅ2+โˆฅ๐›ฬ‚โˆ’๐ฐโˆฅ2,\|\mathbf{b} - \mathbf{w}\|^2 = \|\mathbf{b} - \hat{\mathbf{b}}\|^2 + \|\hat{\mathbf{b}} - \mathbf{w}\|^2 , so โˆฅ๐›โˆ’๐ฐโˆฅ>โˆฅ๐›โˆ’๐›ฬ‚โˆฅ\|\mathbf{b} - \mathbf{w}\| > \|\mathbf{b} - \hat{\mathbf{b}}\| whenever ๐ฐโ‰ ๐›ฬ‚\mathbf{w} \neq \hat{\mathbf{b}}. In words, ๐›ฬ‚\hat{\mathbf{b}} is the unique point of WW closest to ๐›\mathbf{b}.

Definition 9.33 (Distance to a subspace)

The distance from ๐›\mathbf{b} to the subspace WW is dist(๐›,W)=min๐ฐโˆˆWโˆฅ๐›โˆ’๐ฐโˆฅ=โˆฅ๐›โˆ’projW๐›โˆฅ;\operatorname{dist}(\mathbf{b}, W) = \min_{\mathbf{w} \in W} \|\mathbf{b} - \mathbf{w}\| = \|\mathbf{b} - \operatorname{proj}_W \mathbf{b}\| ; the minimum exists and is attained only at projW๐›\operatorname{proj}_W \mathbf{b}, by Theorem 9.32. It is the length of the component of ๐›\mathbf{b} orthogonal to WW, and it is 00 exactly when ๐›โˆˆW\mathbf{b} \in W.

Theorem 9.34 (Properties of the projection)

Let WW be a subspace of โ„n\mathbb{R}^n and write p(๐›)=projW๐›p(\mathbf{b}) = \operatorname{proj}_W \mathbf{b}.

  1. p:โ„nโ†’โ„np\colon \mathbb{R}^n \to \mathbb{R}^n is a linear map.
  2. p(๐›)=๐›p(\mathbf{b}) = \mathbf{b} if and only if ๐›โˆˆW\mathbf{b} \in W, and p(๐›)=0p(\mathbf{b}) = \mathbf{0} if and only if ๐›โˆˆWโŠฅ\mathbf{b} \in W^{\perp}. So the image of pp is WW and its kernel is WโŠฅW^{\perp}.
  3. p(p(๐›))=p(๐›)p(p(\mathbf{b})) = p(\mathbf{b}): projecting twice is the same as projecting once.
  4. projWโŠฅ๐›=๐›โˆ’projW๐›\operatorname{proj}_{W^{\perp}} \mathbf{b} = \mathbf{b} - \operatorname{proj}_W \mathbf{b}.
  5. โˆฅ๐›โˆฅ2=โˆฅp(๐›)โˆฅ2+โˆฅ๐›โˆ’p(๐›)โˆฅ2\|\mathbf{b}\|^2 = \|p(\mathbf{b})\|^2 + \|\mathbf{b} - p(\mathbf{b})\|^2, so โˆฅp(๐›)โˆฅโ‰คโˆฅ๐›โˆฅ\|p(\mathbf{b})\| \le \|\mathbf{b}\|.
  6. If BB is a matrix whose columns form a basis of WW, the standard matrix of pp is P=B(BTB)โˆ’1BT,P = B\,(B^{T}B)^{-1}B^{T} , and it satisfies P2=PP^2 = P and PT=PP^{T} = P.

Remark 9.35 (The projection as a diagonalizable map)

In the language of Lecture 8 (Theorem 8.30), part (b) of Theorem 9.34 says that every nonzero vector of WW is an eigenvector of PP with eigenvalue 11 and every nonzero vector of WโŠฅW^{\perp} is an eigenvector with eigenvalue 00. Since dimW+dimWโŠฅ=n\dim W + \dim W^{\perp} = n, a basis of WW together with a basis of WโŠฅW^{\perp} is a basis of โ„n\mathbb{R}^n consisting of eigenvectors. (The combined list is independent: a relation among its vectors has the form ๐ฐ+๐ณ=0\mathbf{w} + \mathbf{z} = \mathbf{0} with ๐ฐโˆˆW\mathbf{w} \in W, ๐ณโˆˆWโŠฅ\mathbf{z} \in W^{\perp}, so ๐ฐ=โˆ’๐ณโˆˆWโˆฉWโŠฅ={0}\mathbf{w} = -\mathbf{z} \in W \cap W^{\perp} = \{\mathbf{0}\}, and since each part is a combination of a basis equal to 0\mathbf{0}, all its coefficients are 00. There are nn vectors, so the basis test (Theorem 6.48) applies.) Hence PP is diagonalizable with D=diag(1,โ€ฆ,1,0,โ€ฆ,0)D = \operatorname{diag}(1, \dots, 1, 0, \dots, 0). The map (x1,x2)Tโ†ฆ(x1,0)T(x_1,\, x_2)^{T} \mapsto (x_1,\, 0)^{T} of Lecture 8 is the projection onto the x1x_1-axis, and the reflection across a line LL in Lecture 7 is 2Pโˆ’I22P - I_2, with PP the projection onto LL: it keeps the part of a vector in LL and reverses the part orthogonal to LL.

Theorem 9.36 (The projection formula for an orthogonal basis)

Let ๐ฎ1,โ€ฆ,๐ฎk\mathbf{u}_1, \dots, \mathbf{u}_k be an orthogonal basis of the subspace WW of โ„n\mathbb{R}^n. Then for every ๐›โˆˆโ„n\mathbf{b} \in \mathbb{R}^n, projW๐›=๐›โ‹…๐ฎ1๐ฎ1โ‹…๐ฎ1๐ฎ1+๐›โ‹…๐ฎ2๐ฎ2โ‹…๐ฎ2๐ฎ2+โ‹ฏ+๐›โ‹…๐ฎk๐ฎkโ‹…๐ฎk๐ฎk.\operatorname{proj}_W \mathbf{b} = \frac{\mathbf{b} \cdot \mathbf{u}_1}{\mathbf{u}_1 \cdot \mathbf{u}_1}\,\mathbf{u}_1 + \frac{\mathbf{b} \cdot \mathbf{u}_2}{\mathbf{u}_2 \cdot \mathbf{u}_2}\,\mathbf{u}_2 + \cdots + \frac{\mathbf{b} \cdot \mathbf{u}_k}{\mathbf{u}_k \cdot \mathbf{u}_k}\,\mathbf{u}_k .(9.4) If the basis ๐ช1,โ€ฆ,๐ชk\mathbf{q}_1, \dots, \mathbf{q}_k is orthonormal and Q=(๐ช1โ‹ฏ๐ชk)Q = (\mathbf{q}_1\ \cdots\ \mathbf{q}_k), this reads projW๐›=(๐›โ‹…๐ช1)๐ช1+โ‹ฏ+(๐›โ‹…๐ชk)๐ชk=QQT๐›,\operatorname{proj}_W \mathbf{b} = (\mathbf{b} \cdot \mathbf{q}_1)\,\mathbf{q}_1 + \cdots + (\mathbf{b} \cdot \mathbf{q}_k)\,\mathbf{q}_k = QQ^{T}\mathbf{b} ,(9.5) so the standard matrix of projW\operatorname{proj}_W is P=QQTP = QQ^{T}.

Remark 9.37 (Projection onto a line; a warning)

For k=1k = 1, formula (9.4) is the projection onto the line through ๐ฎโ‰ 0\mathbf{u} \neq \mathbf{0}, proj๐ฎ๐›=๐›โ‹…๐ฎ๐ฎโ‹…๐ฎ๐ฎ,\operatorname{proj}_{\mathbf{u}} \mathbf{b} = \frac{\mathbf{b} \cdot \mathbf{u}}{\mathbf{u} \cdot \mathbf{u}}\,\mathbf{u} , which is the vector c๐ฎc\,\mathbf{u} of (9.1) in Remark 9.13. Theorem 9.36 says that projecting onto WW is the sum of the projections onto the lines Span(๐ฎj)\operatorname{Span}(\mathbf{u}_j), provided the ๐ฎj\mathbf{u}_j are orthogonal. For a basis that is not orthogonal the sum is wrong: with ๐ฐ1,๐ฐ2\mathbf{w}_1, \mathbf{w}_2 and ๐›\mathbf{b} of Example 9.31, ๐›โ‹…๐ฐ1๐ฐ1โ‹…๐ฐ1๐ฐ1+๐›โ‹…๐ฐ2๐ฐ2โ‹…๐ฐ2๐ฐ2=2(110)+75(021)=(224/57/5)โ‰ (131)=๐›ฬ‚.\frac{\mathbf{b} \cdot \mathbf{w}_1}{\mathbf{w}_1 \cdot \mathbf{w}_1}\,\mathbf{w}_1 + \frac{\mathbf{b} \cdot \mathbf{w}_2}{\mathbf{w}_2 \cdot \mathbf{w}_2}\,\mathbf{w}_2 = 2\begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix} + \frac{7}{5}\begin{pmatrix} 0 \\ 2 \\ 1 \end{pmatrix} = \begin{pmatrix} 2 \\ 24/5 \\ 7/5 \end{pmatrix} \neq \begin{pmatrix} 1 \\ 3 \\ 1 \end{pmatrix} = \hat{\mathbf{b}} .

Example 9.38 (The plane again, with an orthogonal basis)

The basis ๐ฎ1=(1,1,0)T\mathbf{u}_1 = (1,\, 1,\, 0)^{T}, ๐ฎ2=(โˆ’1,1,1)T\mathbf{u}_2 = (-1,\, 1,\, 1)^{T} of WW in Example 9.26 is orthogonal: ๐ฎ1โ‹…๐ฎ2=โˆ’1+1+0=0\mathbf{u}_1 \cdot \mathbf{u}_2 = -1 + 1 + 0 = 0. For ๐›=(3,1,5)T\mathbf{b} = (3,\, 1,\, 5)^{T}, ๐›โ‹…๐ฎ1๐ฎ1โ‹…๐ฎ1=42=2,๐›โ‹…๐ฎ2๐ฎ2โ‹…๐ฎ2=33=1,๐›ฬ‚=2๐ฎ1+๐ฎ2=(131),\frac{\mathbf{b} \cdot \mathbf{u}_1}{\mathbf{u}_1 \cdot \mathbf{u}_1} = \frac{4}{2} = 2, \qquad \frac{\mathbf{b} \cdot \mathbf{u}_2}{\mathbf{u}_2 \cdot \mathbf{u}_2} = \frac{3}{3} = 1, \qquad \hat{\mathbf{b}} = 2\,\mathbf{u}_1 + \mathbf{u}_2 = \begin{pmatrix} 1 \\ 3 \\ 1 \end{pmatrix}, the same vector as in Example 9.31, with no system to solve. The distance from ๐›\mathbf{b} to WW is dist(๐›,W)=โˆฅ๐›โˆ’๐›ฬ‚โˆฅ=โˆฅ(2,โˆ’2,4)Tโˆฅ=24=26.\operatorname{dist}(\mathbf{b}, W) = \|\mathbf{b} - \hat{\mathbf{b}}\| = \|(2,\, -2,\, 4)^{T}\| = \sqrt{24} = 2\sqrt{6} . By part (d) of Theorem 9.34, the same number comes from projecting onto the line WโŠฅ=Span(๐ง)W^{\perp} = \operatorname{Span}(\mathbf{n}): proj๐ง๐›=126๐ง=(2,โˆ’2,4)T\operatorname{proj}_{\mathbf{n}}\mathbf{b} = \frac{12}{6}\,\mathbf{n} = (2,\, -2,\, 4)^{T}. This also gives the matrix of projW\operatorname{proj}_W quickly: P๐›=๐›โˆ’16๐ง๐งT๐›P\mathbf{b} = \mathbf{b} - \frac{1}{6}\mathbf{n}\,\mathbf{n}^{T}\mathbf{b} for all ๐›\mathbf{b}, so P=I3โˆ’16๐ง๐งT=16(51โˆ’2152โˆ’222),P = I_3 - \frac{1}{6}\,\mathbf{n}\,\mathbf{n}^{T} = \frac{1}{6}\begin{pmatrix} 5 & 1 & -2 \\ 1 & 5 & 2 \\ -2 & 2 & 2 \end{pmatrix}, and indeed P๐›=16(6,18,6)T=(1,3,1)TP\mathbf{b} = \frac{1}{6}(6,\, 18,\, 6)^{T} = (1,\, 3,\, 1)^{T}.

Example 9.39 (A projection in โ„4\mathbb{R}^4)

Let W=Span(๐ก1,๐ก2)W = \operatorname{Span}(\mathbf{h}_1, \mathbf{h}_2) with the columns of HH from Example 9.17, and ๐›=(1,2,3,4)T\mathbf{b} = (1,\, 2,\, 3,\, 4)^{T}. From Example 9.19, ๐›โ‹…๐ก1=10\mathbf{b} \cdot \mathbf{h}_1 = 10 and ๐›โ‹…๐ก2=โˆ’4\mathbf{b} \cdot \mathbf{h}_2 = -4, so ๐›ฬ‚=104๐ก1โˆ’44๐ก2=(3/23/27/27/2),๐›โˆ’๐›ฬ‚=(โˆ’1/21/2โˆ’1/21/2)=โˆ’12๐ก3,\hat{\mathbf{b}} = \frac{10}{4}\,\mathbf{h}_1 - \frac{4}{4}\,\mathbf{h}_2 = \begin{pmatrix} 3/2 \\ 3/2 \\ 7/2 \\ 7/2 \end{pmatrix}, \qquad \mathbf{b} - \hat{\mathbf{b}} = \begin{pmatrix} -1/2 \\ 1/2 \\ -1/2 \\ 1/2 \end{pmatrix} = -\frac{1}{2}\,\mathbf{h}_3 , and dist(๐›,W)=1\operatorname{dist}(\mathbf{b}, W) = 1. The orthogonal component is the part โˆ’12๐ก3+0๐ก4-\frac{1}{2}\mathbf{h}_3 + 0\,\mathbf{h}_4 of the expansion of ๐›\mathbf{b} that lies in WโŠฅ=Span(๐ก3,๐ก4)W^{\perp} = \operatorname{Span}(\mathbf{h}_3, \mathbf{h}_4): with an orthogonal basis of all of โ„n\mathbb{R}^n whose first kk vectors span WW, projecting means keeping the first kk terms of the expansion. In matrix form, with K=12(๐ก1๐ก2)K = \frac{1}{2}(\mathbf{h}_1\ \mathbf{h}_2), P=KKT=12(1100110000110011):P = KK^{T} = \frac{1}{2}\begin{pmatrix} 1 & 1 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & 1 \end{pmatrix}: this projection replaces the first two entries of ๐›\mathbf{b} by their average, and likewise the last two.

Exercise 9.6 auto-check

The projection of ๐›=(3,0,3)T\mathbf{b} = (3,\, 0,\, 3)^{T} onto the line through ๐ฎ=(1,2,2)T\mathbf{u} = (1,\, 2,\, 2)^{T} is c๐ฎc\,\mathbf{u}. What is cc?

Answer:
Exercise 9.7 auto-check

What is the distance from ๐›=(3,0,3)T\mathbf{b} = (3,\, 0,\, 3)^{T} to the line through ๐ฎ=(1,2,2)T\mathbf{u} = (1,\, 2,\, 2)^{T}?

Answer:

9.5  The Gramโ€“Schmidt process and the QR factorization

💡 Intuition

The projection formula needs an orthogonal basis, and the bases we meet in practice, such as the columns of a matrix, are seldom orthogonal. The Gramโ€“Schmidt process repairs this one vector at a time: keep the first vector; from the second subtract its projection onto the first; from the third subtract its projection onto the span of the first two, which we can now compute because those two are already orthogonal; and so on. What is subtracted at each step is a combination of earlier vectors, so the spans do not change. Recording the subtracted coefficients turns the process into a factorization A=QRA = QR, which is to orthogonality what row reduction is to solving equations.

Theorem 9.40 (The Gramโ€“Schmidt process)

Let ๐ฎ1,โ€ฆ,๐ฎp\mathbf{u}_1, \dots, \mathbf{u}_p be linearly independent vectors in โ„n\mathbb{R}^n. Define ๐ฏ1=๐ฎ1\mathbf{v}_1 = \mathbf{u}_1 and, for k=2,โ€ฆ,pk = 2, \dots, p, ๐ฏk=๐ฎkโˆ’โˆ‘j=1kโˆ’1๐ฎkโ‹…๐ฏj๐ฏjโ‹…๐ฏj๐ฏj.\mathbf{v}_k = \mathbf{u}_k - \sum_{j=1}^{k-1} \frac{\mathbf{u}_k \cdot \mathbf{v}_j}{\mathbf{v}_j \cdot \mathbf{v}_j}\,\mathbf{v}_j .(9.6) Then, for each 1โ‰คkโ‰คp1 \le k \le p, the vectors ๐ฏ1,โ€ฆ,๐ฏk\mathbf{v}_1, \dots, \mathbf{v}_k are nonzero and pairwise orthogonal, and Span(๐ฏ1,โ€ฆ,๐ฏk)=Span(๐ฎ1,โ€ฆ,๐ฎk).\operatorname{Span}(\mathbf{v}_1, \dots, \mathbf{v}_k) = \operatorname{Span}(\mathbf{u}_1, \dots, \mathbf{u}_k) .

Remark 9.41 (The Gramโ€“Schmidt relations)
  1. Written out, the first steps of (9.6) are ๐ฏ2=๐ฎ2โˆ’๐ฎ2โ‹…๐ฏ1๐ฏ1โ‹…๐ฏ1๐ฏ1,๐ฏ3=๐ฎ3โˆ’๐ฎ3โ‹…๐ฏ1๐ฏ1โ‹…๐ฏ1๐ฏ1โˆ’๐ฎ3โ‹…๐ฏ2๐ฏ2โ‹…๐ฏ2๐ฏ2,\begin{aligned} \mathbf{v}_2 &= \mathbf{u}_2 - \frac{\mathbf{u}_2 \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\,\mathbf{v}_1, \\ \mathbf{v}_3 &= \mathbf{u}_3 - \frac{\mathbf{u}_3 \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\,\mathbf{v}_1 - \frac{\mathbf{u}_3 \cdot \mathbf{v}_2}{\mathbf{v}_2 \cdot \mathbf{v}_2}\,\mathbf{v}_2, \end{aligned} and so on: by (9.7) in the proof, each ๐ฏk\mathbf{v}_k is ๐ฎk\mathbf{u}_k minus its projection onto Span(๐ฎ1,โ€ฆ,๐ฎkโˆ’1)\operatorname{Span}(\mathbf{u}_1, \dots, \mathbf{u}_{k-1}).
  2. Taking k=pk = p in Theorem 9.40, the vectors ๐ฏ1,โ€ฆ,๐ฏp\mathbf{v}_1, \dots, \mathbf{v}_p form an orthogonal basis of the span of ๐ฎ1,โ€ฆ,๐ฎp\mathbf{u}_1, \dots, \mathbf{u}_p, and the unit vectors ๐ชk=๐ฏk/โˆฅ๐ฏkโˆฅ\mathbf{q}_k = \mathbf{v}_k / \|\mathbf{v}_k\| form an orthonormal basis.
  3. The new vectors in terms of the old. Each ๐ฏj\mathbf{v}_j with j<kj < k lies in Span(๐ฎ1,โ€ฆ,๐ฎkโˆ’1)\operatorname{Span}(\mathbf{u}_1, \dots, \mathbf{u}_{k-1}), so (9.6) shows that ๐ฏk\mathbf{v}_k is ๐ฎk\mathbf{u}_k plus a linear combination of ๐ฎ1,โ€ฆ,๐ฎkโˆ’1\mathbf{u}_1, \dots, \mathbf{u}_{k-1}. In the examples we first write ๐ฏk\mathbf{v}_k in this form and only then substitute the vectors.
  4. The old vectors in terms of the new. Moving the sum in (9.6) to the other side gives ๐ฎk=t1k๐ฏ1+โ‹ฏ+tkโˆ’1,k๐ฏkโˆ’1+๐ฏk,tjk=๐ฎkโ‹…๐ฏj๐ฏjโ‹…๐ฏj(j<k),\mathbf{u}_k = t_{1k}\mathbf{v}_1 + \cdots + t_{k-1,k}\mathbf{v}_{k-1} + \mathbf{v}_k , \qquad t_{jk} = \frac{\mathbf{u}_k \cdot \mathbf{v}_j}{\mathbf{v}_j \cdot \mathbf{v}_j} \quad (j < k),(9.8) for instance ๐ฎ1=๐ฏ1\mathbf{u}_1 = \mathbf{v}_1, ๐ฎ2=t12๐ฏ1+๐ฏ2\mathbf{u}_2 = t_{12}\mathbf{v}_1 + \mathbf{v}_2 and ๐ฎ3=t13๐ฏ1+t23๐ฏ2+๐ฏ3\mathbf{u}_3 = t_{13}\mathbf{v}_1 + t_{23}\mathbf{v}_2 + \mathbf{v}_3. The numbers tjkt_{jk} are computed anyway during the process. In matrix form these relations become the QR factorization later in this section.
We call (9.6) and (9.8) the Gramโ€“Schmidt relations.
Remark 9.42 (Practical points)

(a) Each ๐ฏj\mathbf{v}_j may be replaced by any nonzero multiple c๐ฏjc\,\mathbf{v}_j before it is used in later steps: the term ๐ฎkโ‹…c๐ฏjc๐ฏjโ‹…c๐ฏjc๐ฏj\frac{\mathbf{u}_k \cdot c\mathbf{v}_j}{c\mathbf{v}_j \cdot c\mathbf{v}_j}\,c\,\mathbf{v}_j equals ๐ฎkโ‹…๐ฏj๐ฏjโ‹…๐ฏj๐ฏj\frac{\mathbf{u}_k \cdot \mathbf{v}_j}{\mathbf{v}_j \cdot \mathbf{v}_j}\,\mathbf{v}_j. Clearing denominators this way keeps hand computations in integers. The relations (9.8) then hold with the rescaled vectors and correspondingly rescaled coefficients. (b) Check each new ๐ฏk\mathbf{v}_k by computing its inner products with the earlier ones; they must be 00. (c) If the ๐ฎk\mathbf{u}_k are dependent, some ๐ฏk\mathbf{v}_k comes out 0\mathbf{0}, exactly when ๐ฎk\mathbf{u}_k is a combination of the earlier vectors; dropping it and continuing produces an orthogonal basis of the span. So every subspace of โ„n\mathbb{R}^n has an orthogonal, and hence an orthonormal, basis.

Example 9.43 (Gramโ€“Schmidt in โ„3\mathbb{R}^3)

Let ๐ฎ1=(1,1,0)T\mathbf{u}_1 = (1,\, 1,\, 0)^{T}, ๐ฎ2=(1,0,1)T\mathbf{u}_2 = (1,\, 0,\, 1)^{T}, ๐ฎ3=(0,1,1)T\mathbf{u}_3 = (0,\, 1,\, 1)^{T}.

Step 1. ๐ฏ1=๐ฎ1=(1,1,0)T\mathbf{v}_1 = \mathbf{u}_1 = (1,\, 1,\, 0)^{T}, with ๐ฏ1โ‹…๐ฏ1=2\mathbf{v}_1 \cdot \mathbf{v}_1 = 2.

Step 2. Since ๐ฎ2โ‹…๐ฏ1=1\mathbf{u}_2 \cdot \mathbf{v}_1 = 1, the relation (9.6) is ๐ฏ2=๐ฎ2โˆ’12๐ฏ1\mathbf{v}_2 = \mathbf{u}_2 - \frac{1}{2}\,\mathbf{v}_1. Plugging in the vectors, ๐ฏ2=(101)โˆ’12(110)=(1/2โˆ’1/21).\mathbf{v}_2 = \begin{pmatrix} 1 \\ 0 \\ 1 \end{pmatrix} - \frac{1}{2}\begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix} = \begin{pmatrix} 1/2 \\ -1/2 \\ 1 \end{pmatrix}. To avoid fractions we use ๐ฏ2โ€ฒ=2๐ฏ2=(1,โˆ’1,2)T\mathbf{v}_2' = 2\,\mathbf{v}_2 = (1,\, -1,\, 2)^{T} instead (point (a) of Remark 9.42), with ๐ฏ2โ€ฒโ‹…๐ฏ2โ€ฒ=6\mathbf{v}_2' \cdot \mathbf{v}_2' = 6. In terms of ๐ฏ2โ€ฒ\mathbf{v}_2' the relation reads ๐ฏ2โ€ฒ=2๐ฎ2โˆ’๐ฏ1\mathbf{v}_2' = 2\,\mathbf{u}_2 - \mathbf{v}_1, that is, ๐ฎ2=12๐ฏ1+12๐ฏ2โ€ฒ\mathbf{u}_2 = \frac{1}{2}\,\mathbf{v}_1 + \frac{1}{2}\,\mathbf{v}_2'.

Step 3. Since ๐ฎ3โ‹…๐ฏ1=1\mathbf{u}_3 \cdot \mathbf{v}_1 = 1 and ๐ฎ3โ‹…๐ฏ2โ€ฒ=โˆ’1+2=1\mathbf{u}_3 \cdot \mathbf{v}_2' = -1 + 2 = 1, the relation is ๐ฏ3=๐ฎ3โˆ’12๐ฏ1โˆ’16๐ฏ2โ€ฒ\mathbf{v}_3 = \mathbf{u}_3 - \frac{1}{2}\,\mathbf{v}_1 - \frac{1}{6}\,\mathbf{v}_2'. Plugging in, ๐ฏ3=(011)โˆ’12(110)โˆ’16(1โˆ’12)=(โˆ’2/32/32/3),\mathbf{v}_3 = \begin{pmatrix} 0 \\ 1 \\ 1 \end{pmatrix} - \frac{1}{2}\begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix} - \frac{1}{6}\begin{pmatrix} 1 \\ -1 \\ 2 \end{pmatrix} = \begin{pmatrix} -2/3 \\ 2/3 \\ 2/3 \end{pmatrix}, and we use ๐ฏ3โ€ฒ=32๐ฏ3=(โˆ’1,1,1)T\mathbf{v}_3' = \frac{3}{2}\,\mathbf{v}_3 = (-1,\, 1,\, 1)^{T}, with ๐ฏ3โ€ฒโ‹…๐ฏ3โ€ฒ=3\mathbf{v}_3' \cdot \mathbf{v}_3' = 3; then ๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+23๐ฏ3โ€ฒ\mathbf{u}_3 = \frac{1}{2}\,\mathbf{v}_1 + \frac{1}{6}\,\mathbf{v}_2' + \frac{2}{3}\,\mathbf{v}_3'.

Check: ๐ฏ1โ‹…๐ฏ2โ€ฒ=1โˆ’1+0=0\mathbf{v}_1 \cdot \mathbf{v}_2' = 1 - 1 + 0 = 0, ๐ฏ1โ‹…๐ฏ3โ€ฒ=โˆ’1+1+0=0\mathbf{v}_1 \cdot \mathbf{v}_3' = -1 + 1 + 0 = 0, ๐ฏ2โ€ฒโ‹…๐ฏ3โ€ฒ=โˆ’1โˆ’1+2=0\mathbf{v}_2' \cdot \mathbf{v}_3' = -1 - 1 + 2 = 0. Normalizing, ๐ช1=12(110),๐ช2=16(1โˆ’12),๐ช3=13(โˆ’111)\mathbf{q}_1 = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix}, \qquad \mathbf{q}_2 = \frac{1}{\sqrt{6}}\begin{pmatrix} 1 \\ -1 \\ 2 \end{pmatrix}, \qquad \mathbf{q}_3 = \frac{1}{\sqrt{3}}\begin{pmatrix} -1 \\ 1 \\ 1 \end{pmatrix} is an orthonormal basis of โ„3\mathbb{R}^3. The relations found in the three steps, collected, ๐ฎ1=๐ฏ1,๐ฎ2=12๐ฏ1+12๐ฏ2โ€ฒ,๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+23๐ฏ3โ€ฒ,\mathbf{u}_1 = \mathbf{v}_1, \qquad \mathbf{u}_2 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{2}\,\mathbf{v}_2', \qquad \mathbf{u}_3 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{6}\,\mathbf{v}_2' + \tfrac{2}{3}\,\mathbf{v}_3' , are all that is needed for the QR factorization of (๐ฎ1๐ฎ2๐ฎ3)(\mathbf{u}_1\ \mathbf{u}_2\ \mathbf{u}_3), computed after the QR theorem below.

Example 9.44 (Gramโ€“Schmidt on the columns of a 4ร—34 \times 3 matrix)

Let A=(100110111111)=(๐ฎ1๐ฎ2๐ฎ3).A = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{pmatrix} = (\mathbf{u}_1\ \mathbf{u}_2\ \mathbf{u}_3) . Step 1. ๐ฏ1=๐ฎ1=(1,1,1,1)T\mathbf{v}_1 = \mathbf{u}_1 = (1,\, 1,\, 1,\, 1)^{T}, with ๐ฏ1โ‹…๐ฏ1=4\mathbf{v}_1 \cdot \mathbf{v}_1 = 4.

Step 2. Since ๐ฎ2โ‹…๐ฏ1=3\mathbf{u}_2 \cdot \mathbf{v}_1 = 3, the relation (9.6) is ๐ฏ2=๐ฎ2โˆ’34๐ฏ1\mathbf{v}_2 = \mathbf{u}_2 - \frac{3}{4}\,\mathbf{v}_1. Plugging in the vectors, ๐ฏ2=(0111)โˆ’34(1111)=14(โˆ’3111),\mathbf{v}_2 = \begin{pmatrix} 0 \\ 1 \\ 1 \\ 1 \end{pmatrix} - \frac{3}{4}\begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \frac{1}{4}\begin{pmatrix} -3 \\ 1 \\ 1 \\ 1 \end{pmatrix}, and we use ๐ฏ2โ€ฒ=4๐ฏ2=(โˆ’3,1,1,1)T\mathbf{v}_2' = 4\,\mathbf{v}_2 = (-3,\, 1,\, 1,\, 1)^{T}, with ๐ฏ2โ€ฒโ‹…๐ฏ2โ€ฒ=12\mathbf{v}_2' \cdot \mathbf{v}_2' = 12. In terms of ๐ฏ2โ€ฒ\mathbf{v}_2' the relation reads ๐ฏ2โ€ฒ=4๐ฎ2โˆ’3๐ฏ1\mathbf{v}_2' = 4\,\mathbf{u}_2 - 3\,\mathbf{v}_1, that is, ๐ฎ2=34๐ฏ1+14๐ฏ2โ€ฒ\mathbf{u}_2 = \frac{3}{4}\,\mathbf{v}_1 + \frac{1}{4}\,\mathbf{v}_2'.

Step 3. Since ๐ฎ3โ‹…๐ฏ1=2\mathbf{u}_3 \cdot \mathbf{v}_1 = 2 and ๐ฎ3โ‹…๐ฏ2โ€ฒ=2\mathbf{u}_3 \cdot \mathbf{v}_2' = 2, the relation is ๐ฏ3=๐ฎ3โˆ’24๐ฏ1โˆ’212๐ฏ2โ€ฒ\mathbf{v}_3 = \mathbf{u}_3 - \frac{2}{4}\,\mathbf{v}_1 - \frac{2}{12}\,\mathbf{v}_2'. Plugging in, ๐ฏ3=(0011)โˆ’24(1111)โˆ’212(โˆ’3111)=13(0โˆ’211),\mathbf{v}_3 = \begin{pmatrix} 0 \\ 0 \\ 1 \\ 1 \end{pmatrix} - \frac{2}{4}\begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} - \frac{2}{12}\begin{pmatrix} -3 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \frac{1}{3}\begin{pmatrix} 0 \\ -2 \\ 1 \\ 1 \end{pmatrix}, and we use ๐ฏ3โ€ฒ=3๐ฏ3=(0,โˆ’2,1,1)T\mathbf{v}_3' = 3\,\mathbf{v}_3 = (0,\, -2,\, 1,\, 1)^{T}, with ๐ฏ3โ€ฒโ‹…๐ฏ3โ€ฒ=6\mathbf{v}_3' \cdot \mathbf{v}_3' = 6; then ๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+13๐ฏ3โ€ฒ\mathbf{u}_3 = \frac{1}{2}\,\mathbf{v}_1 + \frac{1}{6}\,\mathbf{v}_2' + \frac{1}{3}\,\mathbf{v}_3'.

Check: ๐ฏ1โ‹…๐ฏ2โ€ฒ=โˆ’3+1+1+1=0\mathbf{v}_1 \cdot \mathbf{v}_2' = -3 + 1 + 1 + 1 = 0, ๐ฏ1โ‹…๐ฏ3โ€ฒ=0โˆ’2+1+1=0\mathbf{v}_1 \cdot \mathbf{v}_3' = 0 - 2 + 1 + 1 = 0, ๐ฏ2โ€ฒโ‹…๐ฏ3โ€ฒ=0โˆ’2+1+1=0\mathbf{v}_2' \cdot \mathbf{v}_3' = 0 - 2 + 1 + 1 = 0. So (1,1,1,1)T(1,\, 1,\, 1,\, 1)^{T}, (โˆ’3,1,1,1)T(-3,\, 1,\, 1,\, 1)^{T}, (0,โˆ’2,1,1)T(0,\, -2,\, 1,\, 1)^{T} is an orthogonal basis of Col(A)\operatorname{Col}(A), and ๐ช1=12(1111),๐ช2=123(โˆ’3111),๐ช3=16(0โˆ’211)\mathbf{q}_1 = \frac{1}{2}\begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix}, \qquad \mathbf{q}_2 = \frac{1}{2\sqrt{3}}\begin{pmatrix} -3 \\ 1 \\ 1 \\ 1 \end{pmatrix}, \qquad \mathbf{q}_3 = \frac{1}{\sqrt{6}}\begin{pmatrix} 0 \\ -2 \\ 1 \\ 1 \end{pmatrix} is an orthonormal basis of it. The relations of the three steps, collected, express each column of AA through the new vectors: ๐ฎ1=๐ฏ1,๐ฎ2=34๐ฏ1+14๐ฏ2โ€ฒ,๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+13๐ฏ3โ€ฒ.\mathbf{u}_1 = \mathbf{v}_1, \qquad \mathbf{u}_2 = \tfrac{3}{4}\,\mathbf{v}_1 + \tfrac{1}{4}\,\mathbf{v}_2', \qquad \mathbf{u}_3 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{6}\,\mathbf{v}_2' + \tfrac{1}{3}\,\mathbf{v}_3' .

Theorem 9.45 (The QR factorization)

Let AA be an mร—nm \times n matrix with linearly independent columns. Then A=QR,A = QR , where QQ is an mร—nm \times n column orthonormal matrix whose columns form an orthonormal basis of Col(A)\operatorname{Col}(A), and RR is an nร—nn \times n upper triangular matrix with positive diagonal entries; in particular RR is invertible. Explicitly, let ๐ฏ1,โ€ฆ,๐ฏn\mathbf{v}_1, \dots, \mathbf{v}_n be the Gramโ€“Schmidt vectors of the columns ๐ฎ1,โ€ฆ,๐ฎn\mathbf{u}_1, \dots, \mathbf{u}_n of AA, and write the relations (9.8) as A=VTA = VT, where V=(๐ฏ1โ‹ฏ๐ฏn)V = (\mathbf{v}_1\ \cdots\ \mathbf{v}_n) and TT is the upper triangular matrix with entries tjkt_{jk} above the diagonal and 11 on it. With D=diag(โˆฅ๐ฏ1โˆฅ,โ€ฆ,โˆฅ๐ฏnโˆฅ)D = \operatorname{diag}(\|\mathbf{v}_1\|, \dots, \|\mathbf{v}_n\|), Q=VDโˆ’1,R=DT,Q = VD^{-1}, \qquad R = DT,(9.9) that is, rjk=tjkโˆฅ๐ฏjโˆฅr_{jk} = t_{jk}\,\|\mathbf{v}_j\| for j<kj < k and rkk=โˆฅ๐ฏkโˆฅr_{kk} = \|\mathbf{v}_k\|. Moreover R=QTAR = Q^{T}A.

Remark 9.46 (Computing RR)

No extra work is needed to find RR: the Gramโ€“Schmidt process already produces the relations (9.8), that is, column kk of TT, and (9.9) turns them into column kk of RR by multiplying the coefficient of ๐ฏj\mathbf{v}_j by โˆฅ๐ฏjโˆฅ\|\mathbf{v}_j\|. This works just as well with rescaled vectors ๐ฏjโ€ฒ\mathbf{v}_j' (point (a) of Remark 9.42): write each ๐ฎk\mathbf{u}_k in terms of the ๐ฏjโ€ฒ\mathbf{v}_j' and multiply the coefficient of ๐ฏjโ€ฒ\mathbf{v}_j' by โˆฅ๐ฏjโ€ฒโˆฅ\|\mathbf{v}_j'\|, because ๐ฏjโ€ฒ=โˆฅ๐ฏjโ€ฒโˆฅ๐ชj\mathbf{v}_j' = \|\mathbf{v}_j'\|\,\mathbf{q}_j as well. The formula R=QTAR = Q^{T}A, whose (j,k)(j, k) entry is ๐ชjโ‹…๐ฎk\mathbf{q}_j \cdot \mathbf{u}_k, is a good check; its entries below the diagonal come out 00. For a square AA, the matrix QQ is an orthogonal matrix, and A๐ฑ=Q(R๐ฑ)A\mathbf{x} = Q(R\mathbf{x}) writes the map ๐ฑโ†ฆA๐ฑ\mathbf{x} \mapsto A\mathbf{x} as a triangular map followed by a length-preserving one. One can also show that the factorization with positive diagonal in RR is unique.

Example 9.47 (QR of the 3ร—33 \times 3 matrix)

Let A=(๐ฎ1๐ฎ2๐ฎ3)=(110101011)A = (\mathbf{u}_1\ \mathbf{u}_2\ \mathbf{u}_3) = \begin{pmatrix} 1 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{pmatrix}, the vectors of Example 9.43. The relations found there, ๐ฎ1=๐ฏ1,๐ฎ2=12๐ฏ1+12๐ฏ2โ€ฒ,๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+23๐ฏ3โ€ฒ,\mathbf{u}_1 = \mathbf{v}_1, \qquad \mathbf{u}_2 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{2}\,\mathbf{v}_2', \qquad \mathbf{u}_3 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{6}\,\mathbf{v}_2' + \tfrac{2}{3}\,\mathbf{v}_3' , say A=Vโ€ฒTโ€ฒA = V'T' with Vโ€ฒ=(๐ฏ1๐ฏ2โ€ฒ๐ฏ3โ€ฒ)V' = (\mathbf{v}_1\ \mathbf{v}_2'\ \mathbf{v}_3'). The lengths are โˆฅ๐ฏ1โˆฅ=2\|\mathbf{v}_1\| = \sqrt{2}, โˆฅ๐ฏ2โ€ฒโˆฅ=6\|\mathbf{v}_2'\| = \sqrt{6}, โˆฅ๐ฏ3โ€ฒโˆฅ=3\|\mathbf{v}_3'\| = \sqrt{3}, so by (9.9) (in the form of Remark 9.46) we multiply the rows of Tโ€ฒT' by these lengths: Tโ€ฒ=(11212012160023),R=(222220626600233),T' = \begin{pmatrix} 1 & \frac{1}{2} & \frac{1}{2} \\ 0 & \frac{1}{2} & \frac{1}{6} \\ 0 & 0 & \frac{2}{3} \end{pmatrix}, \qquad R = \begin{pmatrix} \sqrt{2} & \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \\[2pt] 0 & \frac{\sqrt{6}}{2} & \frac{\sqrt{6}}{6} \\[2pt] 0 & 0 & \frac{2\sqrt{3}}{3} \end{pmatrix}, and Q=(๐ช1๐ช2๐ช3)Q = (\mathbf{q}_1\ \mathbf{q}_2\ \mathbf{q}_3) with the ๐ชj\mathbf{q}_j of Example 9.43. As a check with R=QTAR = Q^{T}A: the (2,2)(2,2) entry is ๐ช2โ‹…๐ฎ2=(1+0+2)/6=6/2\mathbf{q}_2 \cdot \mathbf{u}_2 = (1 + 0 + 2)/\sqrt{6} = \sqrt{6}/2, and the (3,1)(3,1) entry ๐ช3โ‹…๐ฎ1=(โˆ’1+1+0)/3\mathbf{q}_3 \cdot \mathbf{u}_1 = (-1 + 1 + 0)/\sqrt{3} is indeed 00.

Example 9.48 (QR of the 4ร—34 \times 3 matrix)

For A=(100110111111)A = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{pmatrix} of Example 9.44, the relations found there are ๐ฎ1=๐ฏ1,๐ฎ2=34๐ฏ1+14๐ฏ2โ€ฒ,๐ฎ3=12๐ฏ1+16๐ฏ2โ€ฒ+13๐ฏ3โ€ฒ,\mathbf{u}_1 = \mathbf{v}_1, \qquad \mathbf{u}_2 = \tfrac{3}{4}\,\mathbf{v}_1 + \tfrac{1}{4}\,\mathbf{v}_2', \qquad \mathbf{u}_3 = \tfrac{1}{2}\,\mathbf{v}_1 + \tfrac{1}{6}\,\mathbf{v}_2' + \tfrac{1}{3}\,\mathbf{v}_3' , so A=Vโ€ฒTโ€ฒA = V'T' with Tโ€ฒ=(13/41/201/41/6001/3)T' = \begin{pmatrix} 1 & 3/4 & 1/2 \\ 0 & 1/4 & 1/6 \\ 0 & 0 & 1/3 \end{pmatrix}. The lengths are โˆฅ๐ฏ1โˆฅ=2\|\mathbf{v}_1\| = 2, โˆฅ๐ฏ2โ€ฒโˆฅ=12=23\|\mathbf{v}_2'\| = \sqrt{12} = 2\sqrt{3}, โˆฅ๐ฏ3โ€ฒโˆฅ=6\|\mathbf{v}_3'\| = \sqrt{6}; multiplying the rows of Tโ€ฒT' by them (Remark 9.46) gives RR, and dividing the columns of Vโ€ฒV' by them gives QQ: Q=(12โˆ’323012123โˆ’2612123161212316),R=(2321032330063).Q = \begin{pmatrix} \frac{1}{2} & -\frac{3}{2\sqrt{3}} & 0 \\[2pt] \frac{1}{2} & \frac{1}{2\sqrt{3}} & -\frac{2}{\sqrt{6}} \\[2pt] \frac{1}{2} & \frac{1}{2\sqrt{3}} & \frac{1}{\sqrt{6}} \\[2pt] \frac{1}{2} & \frac{1}{2\sqrt{3}} & \frac{1}{\sqrt{6}} \end{pmatrix}, \qquad R = \begin{pmatrix} 2 & \frac{3}{2} & 1 \\[2pt] 0 & \frac{\sqrt{3}}{2} & \frac{\sqrt{3}}{3} \\[2pt] 0 & 0 & \frac{\sqrt{6}}{3} \end{pmatrix}. For instance, since ๐ฏ1=2๐ช1\mathbf{v}_1 = 2\,\mathbf{q}_1 and ๐ฏ2โ€ฒ=23๐ช2\mathbf{v}_2' = 2\sqrt{3}\,\mathbf{q}_2, the relation for ๐ฎ2\mathbf{u}_2 becomes ๐ฎ2=34๐ฏ1+14๐ฏ2โ€ฒ=32๐ช1+32๐ช2,\mathbf{u}_2 = \frac{3}{4}\mathbf{v}_1 + \frac{1}{4}\mathbf{v}_2' = \frac{3}{2}\mathbf{q}_1 + \frac{\sqrt{3}}{2}\mathbf{q}_2 , which is the second column of RR. As a check with R=QTAR = Q^{T}A: ๐ช1โ‹…๐ฎ2=32\mathbf{q}_1 \cdot \mathbf{u}_2 = \frac{3}{2}, ๐ช2โ‹…๐ฎ3=223=33\mathbf{q}_2 \cdot \mathbf{u}_3 = \frac{2}{2\sqrt{3}} = \frac{\sqrt{3}}{3} and ๐ช3โ‹…๐ฎ3=26=63\mathbf{q}_3 \cdot \mathbf{u}_3 = \frac{2}{\sqrt{6}} = \frac{\sqrt{6}}{3}.

Exercise 9.8 auto-check

Apply Gramโ€“Schmidt to ๐ฎ1=(1,1)T\mathbf{u}_1 = (1,\, 1)^{T}, ๐ฎ2=(2,0)T\mathbf{u}_2 = (2,\, 0)^{T}. What is the second entry of ๐ฏ2\mathbf{v}_2?

Answer:
Exercise 9.9 auto-check

AA has first column (2,1,2,0)T(2,\, 1,\, 2,\, 0)^{T}, and A=QRA = QR is its QR factorization. What is the entry r11r_{11} of RR?

Answer:

9.6  Least-squares solutions

💡 Intuition

We return to an inconsistent system A๐ฑ=๐›A\mathbf{x} = \mathbf{b}. As ๐ฑ\mathbf{x} varies, A๐ฑA\mathbf{x} runs over the subspace Col(A)\operatorname{Col}(A), and the residual ๐›โˆ’A๐ฑ\mathbf{b} - A\mathbf{x} is shortest when A๐ฑA\mathbf{x} is the point of Col(A)\operatorname{Col}(A) closest to ๐›\mathbf{b}, which is the projection ๐›ฬ‚\hat{\mathbf{b}}. So the โ€œbestโ€ ๐ฑ\mathbf{x} solves the consistent system A๐ฑ=๐›ฬ‚A\mathbf{x} = \hat{\mathbf{b}}. The three ways of computing projections in Sections 9.3โ€“9.5 become three methods: the normal equations ATA๐ฑ=AT๐›A^{T}A\,\mathbf{x} = A^{T}\mathbf{b}, the Gramโ€“Schmidt data, and the QR factorization.

Definition 9.49 (Least-squares solution)

Let AA be mร—nm \times n and ๐›โˆˆโ„m\mathbf{b} \in \mathbb{R}^m. A least-squares solution of A๐ฑ=๐›A\mathbf{x} = \mathbf{b} is a vector ๐ฑฬ‚โˆˆโ„n\hat{\mathbf{x}} \in \mathbb{R}^n such that โˆฅ๐›โˆ’A๐ฑฬ‚โˆฅโ‰คโˆฅ๐›โˆ’A๐ฑโˆฅfor every ๐ฑโˆˆโ„n.\|\mathbf{b} - A\hat{\mathbf{x}}\| \le \|\mathbf{b} - A\mathbf{x}\| \qquad\text{for every } \mathbf{x} \in \mathbb{R}^n . The number โˆฅ๐›โˆ’A๐ฑฬ‚โˆฅ\|\mathbf{b} - A\hat{\mathbf{x}}\| is the least-squares error. The name refers to โˆฅ๐›โˆ’A๐ฑโˆฅ2=โˆ‘i(biโˆ’(A๐ฑ)i)2\|\mathbf{b} - A\mathbf{x}\|^2 = \sum_i (b_i - (A\mathbf{x})_i)^2: a least-squares solution makes the sum of the squares of the mm errors as small as possible. If A๐ฑ=๐›A\mathbf{x} = \mathbf{b} is consistent, its least-squares solutions are its ordinary solutions, with error 00.

Theorem 9.50 (Characterization of least-squares solutions)

Let AA be mร—nm \times n, ๐›โˆˆโ„m\mathbf{b} \in \mathbb{R}^m, and ๐›ฬ‚=projCol(A)๐›\hat{\mathbf{b}} = \operatorname{proj}_{\operatorname{Col}(A)}\mathbf{b}. For ๐ฑฬ‚โˆˆโ„n\hat{\mathbf{x}} \in \mathbb{R}^n the following are equivalent:

  1. ๐ฑฬ‚\hat{\mathbf{x}} is a least-squares solution of A๐ฑ=๐›A\mathbf{x} = \mathbf{b};
  2. A๐ฑฬ‚=๐›ฬ‚A\hat{\mathbf{x}} = \hat{\mathbf{b}};
  3. ๐›โˆ’A๐ฑฬ‚\mathbf{b} - A\hat{\mathbf{x}} is orthogonal to every column of AA;
  4. ๐ฑฬ‚\hat{\mathbf{x}} solves the normal equations ATA๐ฑ=AT๐›.A^{T}A\,\mathbf{x} = A^{T}\mathbf{b} .(9.10)
Least-squares solutions always exist, and the least-squares error is dist(๐›,Col(A))=โˆฅ๐›โˆ’๐›ฬ‚โˆฅ\operatorname{dist}(\mathbf{b}, \operatorname{Col}(A)) = \|\mathbf{b} - \hat{\mathbf{b}}\|.

Remark 9.51 (ATAA^{T}A in general)

The argument in the proof of Lemma 9.28 works for every matrix AA, not only for one with independent columns: ATA๐ฑ=0A^{T}A\,\mathbf{x} = \mathbf{0} implies โˆฅA๐ฑโˆฅ2=๐ฑTATA๐ฑ=0\|A\mathbf{x}\|^2 = \mathbf{x}^{T}A^{T}A\,\mathbf{x} = 0, so A๐ฑ=0A\mathbf{x} = \mathbf{0}; and A๐ฑ=0A\mathbf{x} = \mathbf{0} clearly implies ATA๐ฑ=0A^{T}A\,\mathbf{x} = \mathbf{0}. Hence Nul(ATA)=Nul(A),rank(ATA)=rankA,\operatorname{Nul}(A^{T}A) = \operatorname{Nul}(A), \qquad \operatorname{rank}(A^{T}A) = \operatorname{rank} A , the second by the rank theorem (Theorem 7.9), since AA and ATAA^{T}A have the same number of columns. In particular, ATAA^{T}A is invertible if and only if the columns of AA are independent, which is the converse of Lemma 9.28. This is used in the next theorem.

Theorem 9.52 (When the least-squares solution is unique)

For an mร—nm \times n matrix AA the following are equivalent:

  1. the columns of AA are linearly independent;
  2. ATAA^{T}A is invertible;
  3. for every ๐›โˆˆโ„m\mathbf{b} \in \mathbb{R}^m, the system A๐ฑ=๐›A\mathbf{x} = \mathbf{b} has exactly one least-squares solution.
In that case ๐ฑฬ‚=(ATA)โˆ’1AT๐›.\hat{\mathbf{x}} = (A^{T}A)^{-1}A^{T}\mathbf{b} .(9.11) If A=QRA = QR is a QR factorization, then ๐ฑฬ‚\hat{\mathbf{x}} is the unique solution of the triangular system R๐ฑ=QT๐›,R\,\mathbf{x} = Q^{T}\mathbf{b},(9.12) that is, ๐ฑฬ‚=Rโˆ’1QT๐›\hat{\mathbf{x}} = R^{-1}Q^{T}\mathbf{b}, and ๐›ฬ‚=QQT๐›\hat{\mathbf{b}} = QQ^{T}\mathbf{b}.

Proposition 9.53 (Least squares from the Gramโ€“Schmidt data)

Let A=(๐ฎ1โ‹ฏ๐ฎn)A = (\mathbf{u}_1\ \cdots\ \mathbf{u}_n) have independent columns, and let ๐ฏ1,โ€ฆ,๐ฏn\mathbf{v}_1, \dots, \mathbf{v}_n be the Gramโ€“Schmidt vectors (possibly rescaled), V=(๐ฏ1โ‹ฏ๐ฏn)V = (\mathbf{v}_1\ \cdots\ \mathbf{v}_n). The Gramโ€“Schmidt relations (9.8), which write each column as ๐ฎk=โˆ‘jโ‰คktjk๐ฏj\mathbf{u}_k = \sum_{j \le k} t_{jk}\mathbf{v}_j, give A=VTA = VT with TT upper triangular and invertible, as in Theorem 9.45. Then:

  1. ๐›ฬ‚=c1๐ฏ1+โ‹ฏ+cn๐ฏn\hat{\mathbf{b}} = c_1\mathbf{v}_1 + \cdots + c_n\mathbf{v}_n with cj=๐›โ‹…๐ฏj/๐ฏjโ‹…๐ฏjc_j = \mathbf{b} \cdot \mathbf{v}_j / \mathbf{v}_j \cdot \mathbf{v}_j;
  2. the least-squares solution is the solution of T๐ฑ=๐œT\mathbf{x} = \mathbf{c}, where ๐œ=(c1,โ€ฆ,cn)T\mathbf{c} = (c_1,\, \dots,\, c_n)^{T}.

Remark 9.54 (Three methods)

For a matrix AA with independent columns we now have three ways to the least-squares solution: (a) the Gramโ€“Schmidt data, with Proposition 9.53; (b) the QR factorization, solving (9.12); (c) the normal equations (9.10). By hand, (c) is usually the shortest for small matrices. In floating-point arithmetic, (b) is the standard choice: forming ATAA^{T}A squares the sensitivity of the problem to rounding errors, while QQ preserves lengths (Proposition 9.20) and so does not magnify errors. Library routines for least squares (such as the backslash operator of MATLAB, or numpy.linalg.lstsq) are based on orthogonal factorizations, not on the normal equations.

Example 9.55 (The three measurements)

For the system x=1.10x = 1.10, x=0.99x = 0.99, x=0.98x = 0.98 of Example 9.1, A=(1,1,1)TA = (1,\, 1,\, 1)^{T} and ๐›=(1.10,0.99,0.98)T\mathbf{b} = (1.10,\, 0.99,\, 0.98)^{T}. The normal equation (9.10) is ATAx=AT๐›A^{T}A\,x = A^{T}\mathbf{b}, that is, 3x=3.073x = 3.07, so xฬ‚=3.07/3โ‰ˆ1.0233\hat{x} = 3.07/3 \approx 1.0233: the average of the measurements. The residual is ๐›โˆ’xฬ‚A=13(0.23โˆ’0.10โˆ’0.13)โ‰ˆ(0.0767โˆ’0.0333โˆ’0.0433),\mathbf{b} - \hat{x}A = \frac{1}{3}\begin{pmatrix} 0.23 \\ -0.10 \\ -0.13 \end{pmatrix} \approx \begin{pmatrix} 0.0767 \\ -0.0333 \\ -0.0433 \end{pmatrix}, whose entries sum to 00 (it is orthogonal to the column (1,1,1)T(1,\, 1,\, 1)^{T}), and the least-squares error is 130.0798โ‰ˆ0.094\frac{1}{3}\sqrt{0.0798} \approx 0.094. In general, for measurements b1,โ€ฆ,bmb_1, \dots, b_m of one quantity, the least-squares estimate is their mean (b1+โ‹ฏ+bm)/m(b_1 + \cdots + b_m)/m.

2026-11-09T22:32:44.174887 image/svg+xml Matplotlib v3.10.9, https://matplotlib.org/
The least-squares line y=12+ty = \frac{1}{2} + t for the data (0,1)(0,1), (1,1)(1,1), (2,2)(2,2), (3,4)(3,4). The vertical segments are the entries ยฑ12\pm\frac{1}{2} of the residual ๐›โˆ’A๐ฑฬ‚\mathbf{b} - A\hat{\mathbf{x}}; the line makes the sum of their squares, here 11, as small as possible.
Example 9.56 (The least-squares line)

We fit the line y=c0+c1ty = c_0 + c_1 t to the data (ti,yi)(t_i, y_i) of Example 9.2, i=1,โ€ฆ,4i = 1, \dots, 4, by the method of the normal equations (9.10). The system has one equation c0+c1ti=yic_0 + c_1 t_i = y_i for each data point, so A=(1t11t21t31t4)=(10111213),๐ฑ=(c0c1),๐›=(y1y2y3y4)=(1124).A = \begin{pmatrix} 1 & t_1 \\ 1 & t_2 \\ 1 & t_3 \\ 1 & t_4 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{pmatrix}, \qquad \mathbf{x} = \begin{pmatrix} c_0 \\ c_1 \end{pmatrix}, \qquad \mathbf{b} = \begin{pmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \end{pmatrix} = \begin{pmatrix} 1 \\ 1 \\ 2 \\ 4 \end{pmatrix}. The columns of AA are 1=(1,1,1,1)T\mathbf{1} = (1,\, 1,\, 1,\, 1)^{T} and ๐ญ=(t1,t2,t3,t4)T\mathbf{t} = (t_1,\, t_2,\, t_3,\, t_4)^{T}. As in Lemma 9.15, the entries of ATAA^{T}A are the inner products of the columns of AA, and the entries of AT๐›A^{T}\mathbf{b} are the inner products of the columns with ๐›\mathbf{b}. Each inner product is a sum over the data points: ATA=(1โ‹…11โ‹…๐ญ๐ญโ‹…1๐ญโ‹…๐ญ)=(4โˆ‘tiโˆ‘tiโˆ‘ti2)=(46614),A^{T}A = \begin{pmatrix} \mathbf{1} \cdot \mathbf{1} & \mathbf{1} \cdot \mathbf{t} \\ \mathbf{t} \cdot \mathbf{1} & \mathbf{t} \cdot \mathbf{t} \end{pmatrix} = \begin{pmatrix} 4 & \sum t_i \\ \sum t_i & \sum t_i^2 \end{pmatrix} = \begin{pmatrix} 4 & 6 \\ 6 & 14 \end{pmatrix}, AT๐›=(1โ‹…๐›๐ญโ‹…๐›)=(โˆ‘yiโˆ‘tiyi)=(817),A^{T}\mathbf{b} = \begin{pmatrix} \mathbf{1} \cdot \mathbf{b} \\ \mathbf{t} \cdot \mathbf{b} \end{pmatrix} = \begin{pmatrix} \sum y_i \\ \sum t_i y_i \end{pmatrix} = \begin{pmatrix} 8 \\ 17 \end{pmatrix}, since โˆ‘ti=0+1+2+3\sum t_i = 0 + 1 + 2 + 3, โˆ‘ti2=0+1+4+9\sum t_i^2 = 0 + 1 + 4 + 9, โˆ‘yi=1+1+2+4\sum y_i = 1 + 1 + 2 + 4 and โˆ‘tiyi=0+1+4+12\sum t_i y_i = 0 + 1 + 4 + 12. Solving the normal equations (9.10), here by (9.11) with det(ATA)=56โˆ’36=20\det(A^{T}A) = 56 - 36 = 20, we get (cฬ‚0cฬ‚1)=120(14โˆ’6โˆ’64)(817)=120(1020)=(1/21).\begin{pmatrix} \hat{c}_0 \\ \hat{c}_1 \end{pmatrix} = \frac{1}{20}\begin{pmatrix} 14 & -6 \\ -6 & 4 \end{pmatrix}\begin{pmatrix} 8 \\ 17 \end{pmatrix} = \frac{1}{20}\begin{pmatrix} 10 \\ 20 \end{pmatrix} = \begin{pmatrix} 1/2 \\ 1 \end{pmatrix}. The least-squares line is y=12+ty = \frac{1}{2} + t, drawn in the figure. Its values at t=0,1,2,3t = 0, 1, 2, 3 are A๐ฑฬ‚=(12,32,52,72)TA\hat{\mathbf{x}} = (\frac{1}{2},\, \frac{3}{2},\, \frac{5}{2},\, \frac{7}{2})^{T}, where ๐ฑฬ‚=(cฬ‚0,cฬ‚1)T\hat{\mathbf{x}} = (\hat{c}_0,\, \hat{c}_1)^{T}; the residual is (12,โˆ’12,โˆ’12,12)T(\frac{1}{2},\, -\frac{1}{2},\, -\frac{1}{2},\, \frac{1}{2})^{T}, and the least-squares error is 11. No other line has a smaller sum of squared vertical errors.

Example 9.57 (One problem, three methods)

Let A=(135110112133)=(๐ฎ1๐ฎ2๐ฎ3),๐›=(357โˆ’3).A = \begin{pmatrix} 1 & 3 & 5 \\ 1 & 1 & 0 \\ 1 & 1 & 2 \\ 1 & 3 & 3 \end{pmatrix} = (\mathbf{u}_1\ \mathbf{u}_2\ \mathbf{u}_3), \qquad \mathbf{b} = \begin{pmatrix} 3 \\ 5 \\ 7 \\ -3 \end{pmatrix}. The system A๐ฑ=๐›A\mathbf{x} = \mathbf{b} is inconsistent: row reduction of (Aโˆฃ๐›)(A \mid \mathbf{b}) gives four pivots, one of them in the last column. We find its least-squares solution in three ways.

Method 1: Gramโ€“Schmidt (9.6). ๐ฏ1=๐ฎ1=(1,1,1,1)T\mathbf{v}_1 = \mathbf{u}_1 = (1,\, 1,\, 1,\, 1)^{T}. Since ๐ฎ2โ‹…๐ฏ1=8\mathbf{u}_2 \cdot \mathbf{v}_1 = 8 and ๐ฏ1โ‹…๐ฏ1=4\mathbf{v}_1 \cdot \mathbf{v}_1 = 4, the relation is ๐ฏ2=๐ฎ2โˆ’2๐ฏ1\mathbf{v}_2 = \mathbf{u}_2 - 2\,\mathbf{v}_1, that is, ๐ฎ2=2๐ฏ1+๐ฏ2\mathbf{u}_2 = 2\,\mathbf{v}_1 + \mathbf{v}_2. Plugging in, ๐ฏ2=๐ฎ2โˆ’2๐ฏ1=(3113)โˆ’(2222)=(1โˆ’1โˆ’11).\mathbf{v}_2 = \mathbf{u}_2 - 2\,\mathbf{v}_1 = \begin{pmatrix} 3 \\ 1 \\ 1 \\ 3 \end{pmatrix} - \begin{pmatrix} 2 \\ 2 \\ 2 \\ 2 \end{pmatrix} = \begin{pmatrix} 1 \\ -1 \\ -1 \\ 1 \end{pmatrix}. Next ๐ฎ3โ‹…๐ฏ1=10\mathbf{u}_3 \cdot \mathbf{v}_1 = 10 and ๐ฎ3โ‹…๐ฏ2=5โˆ’0โˆ’2+3=6\mathbf{u}_3 \cdot \mathbf{v}_2 = 5 - 0 - 2 + 3 = 6, with ๐ฏ2โ‹…๐ฏ2=4\mathbf{v}_2 \cdot \mathbf{v}_2 = 4, so the relation is ๐ฏ3=๐ฎ3โˆ’52๐ฏ1โˆ’32๐ฏ2\mathbf{v}_3 = \mathbf{u}_3 - \frac{5}{2}\,\mathbf{v}_1 - \frac{3}{2}\,\mathbf{v}_2, that is, ๐ฎ3=52๐ฏ1+32๐ฏ2+๐ฏ3\mathbf{u}_3 = \frac{5}{2}\,\mathbf{v}_1 + \frac{3}{2}\,\mathbf{v}_2 + \mathbf{v}_3. Plugging in, ๐ฏ3=๐ฎ3โˆ’52๐ฏ1โˆ’32๐ฏ2=(5023)โˆ’(5/25/25/25/2)โˆ’(3/2โˆ’3/2โˆ’3/23/2)=(1โˆ’11โˆ’1).\mathbf{v}_3 = \mathbf{u}_3 - \frac{5}{2}\,\mathbf{v}_1 - \frac{3}{2}\,\mathbf{v}_2 = \begin{pmatrix} 5 \\ 0 \\ 2 \\ 3 \end{pmatrix} - \begin{pmatrix} 5/2 \\ 5/2 \\ 5/2 \\ 5/2 \end{pmatrix} - \begin{pmatrix} 3/2 \\ -3/2 \\ -3/2 \\ 3/2 \end{pmatrix} = \begin{pmatrix} 1 \\ -1 \\ 1 \\ -1 \end{pmatrix}. The Gramโ€“Schmidt vectors are columns of the matrix HH of Example 9.17: ๐ฏ1=๐ก1\mathbf{v}_1 = \mathbf{h}_1, ๐ฏ2=๐ก4\mathbf{v}_2 = \mathbf{h}_4, ๐ฏ3=๐ก3\mathbf{v}_3 = \mathbf{h}_3, each with ๐ฏjโ‹…๐ฏj=4\mathbf{v}_j \cdot \mathbf{v}_j = 4. The relations ๐ฎ1=๐ฏ1\mathbf{u}_1 = \mathbf{v}_1, ๐ฎ2=2๐ฏ1+๐ฏ2\mathbf{u}_2 = 2\mathbf{v}_1 + \mathbf{v}_2, ๐ฎ3=52๐ฏ1+32๐ฏ2+๐ฏ3\mathbf{u}_3 = \frac{5}{2}\mathbf{v}_1 + \frac{3}{2}\mathbf{v}_2 + \mathbf{v}_3 say A=VTA = VT with V=(๐ฏ1๐ฏ2๐ฏ3),T=(125/2013/2001).V = (\mathbf{v}_1\ \mathbf{v}_2\ \mathbf{v}_3), \qquad T = \begin{pmatrix} 1 & 2 & 5/2 \\ 0 & 1 & 3/2 \\ 0 & 0 & 1 \end{pmatrix}. Project ๐›\mathbf{b} onto Col(A)\operatorname{Col}(A): ๐›โ‹…๐ฏ1=12\mathbf{b} \cdot \mathbf{v}_1 = 12, ๐›โ‹…๐ฏ2=3โˆ’5โˆ’7โˆ’3=โˆ’12\mathbf{b} \cdot \mathbf{v}_2 = 3 - 5 - 7 - 3 = -12, ๐›โ‹…๐ฏ3=3โˆ’5+7+3=8\mathbf{b} \cdot \mathbf{v}_3 = 3 - 5 + 7 + 3 = 8, so ๐œ=(3,โˆ’3,2)T\mathbf{c} = (3,\, -3,\, 2)^{T} and ๐›ฬ‚=3๐ฏ1โˆ’3๐ฏ2+2๐ฏ3=(248โˆ’2),๐›โˆ’๐›ฬ‚=(11โˆ’1โˆ’1)=๐ก2.\hat{\mathbf{b}} = 3\,\mathbf{v}_1 - 3\,\mathbf{v}_2 + 2\,\mathbf{v}_3 = \begin{pmatrix} 2 \\ 4 \\ 8 \\ -2 \end{pmatrix}, \qquad \mathbf{b} - \hat{\mathbf{b}} = \begin{pmatrix} 1 \\ 1 \\ -1 \\ -1 \end{pmatrix} = \mathbf{h}_2 . By Proposition 9.53, solve T๐ฑ=๐œT\mathbf{x} = \mathbf{c} by back substitution: x3=2,x2=โˆ’3โˆ’32x3=โˆ’6,x1=3โˆ’2x2โˆ’52x3=3+12โˆ’5=10.x_3 = 2, \qquad x_2 = -3 - \tfrac{3}{2}x_3 = -6, \qquad x_1 = 3 - 2x_2 - \tfrac{5}{2}x_3 = 3 + 12 - 5 = 10 .

Method 2: QR. All three lengths are โˆฅ๐ฏjโˆฅ=2\|\mathbf{v}_j\| = 2, so by (9.9), Q=12VQ = \frac{1}{2}V and R=2TR = 2T: R=(245023002),QT๐›=12(12โˆ’128)=(6โˆ’64).R = \begin{pmatrix} 2 & 4 & 5 \\ 0 & 2 & 3 \\ 0 & 0 & 2 \end{pmatrix}, \qquad Q^{T}\mathbf{b} = \frac{1}{2}\begin{pmatrix} 12 \\ -12 \\ 8 \end{pmatrix} = \begin{pmatrix} 6 \\ -6 \\ 4 \end{pmatrix}. (One checks R=QTAR = Q^{T}A; for instance its (1,3)(1,3) entry is ๐ช1โ‹…๐ฎ3=12(5+0+2+3)=5\mathbf{q}_1 \cdot \mathbf{u}_3 = \frac{1}{2}(5 + 0 + 2 + 3) = 5.) Back substitution in (9.12): 2x3=42x_3 = 4, so x3=2x_3 = 2; 2x2+3x3=โˆ’62x_2 + 3x_3 = -6, so x2=โˆ’6x_2 = -6; 2x1+4x2+5x3=62x_1 + 4x_2 + 5x_3 = 6, so x1=(6+24โˆ’10)/2=10x_1 = (6 + 24 - 10)/2 = 10.

Method 3: the normal equations (9.10). ATA=(481082026102638),AT๐›=(121220).A^{T}A = \begin{pmatrix} 4 & 8 & 10 \\ 8 & 20 & 26 \\ 10 & 26 & 38 \end{pmatrix}, \qquad A^{T}\mathbf{b} = \begin{pmatrix} 12 \\ 12 \\ 20 \end{pmatrix}. Row reduction of the augmented matrix gives (481012820261210263820)โ†’12R1(2456820261210263820)โ†’R2โˆ’4R1R3โˆ’5R1(2456046โˆ’120613โˆ’10)โ†’12R2R3โˆ’3R2(2456023โˆ’60048),\begin{aligned} \begin{pmatrix} 4 & 8 & 10 & 12 \\ 8 & 20 & 26 & 12 \\ 10 & 26 & 38 & 20 \end{pmatrix} &\overset{\frac{1}{2}R_1}{\longrightarrow} \begin{pmatrix} 2 & 4 & 5 & 6 \\ 8 & 20 & 26 & 12 \\ 10 & 26 & 38 & 20 \end{pmatrix} \\ &\overset{\substack{R_2 - 4R_1 \\ R_3 - 5R_1}}{\longrightarrow} \begin{pmatrix} 2 & 4 & 5 & 6 \\ 0 & 4 & 6 & -12 \\ 0 & 6 & 13 & -10 \end{pmatrix} \\ &\overset{\substack{\frac{1}{2}R_2 \\ R_3 - 3R_2}}{\longrightarrow} \begin{pmatrix} 2 & 4 & 5 & 6 \\ 0 & 2 & 3 & -6 \\ 0 & 0 & 4 & 8 \end{pmatrix}, \end{aligned} and back substitution gives x3=2x_3 = 2, x2=โˆ’6x_2 = -6, x1=10x_1 = 10 again.

Conclusion. All three methods give ๐ฑฬ‚=(10โˆ’62),A๐ฑฬ‚=(10โˆ’18+1010โˆ’6+010โˆ’6+410โˆ’18+6)=(248โˆ’2)=๐›ฬ‚,\hat{\mathbf{x}} = \begin{pmatrix} 10 \\ -6 \\ 2 \end{pmatrix}, \qquad A\hat{\mathbf{x}} = \begin{pmatrix} 10 - 18 + 10 \\ 10 - 6 + 0 \\ 10 - 6 + 4 \\ 10 - 18 + 6 \end{pmatrix} = \begin{pmatrix} 2 \\ 4 \\ 8 \\ -2 \end{pmatrix} = \hat{\mathbf{b}} , and the least-squares error is โˆฅ๐›โˆ’A๐ฑฬ‚โˆฅ=โˆฅ(1,1,โˆ’1,โˆ’1)Tโˆฅ=2\|\mathbf{b} - A\hat{\mathbf{x}}\| = \|(1,\, 1,\, -1,\, -1)^{T}\| = 2. As the characterization of least-squares solutions (Theorem 9.50) requires, the residual is orthogonal to each column of AA: 1+1โˆ’1โˆ’1=01 + 1 - 1 - 1 = 0, 3+1โˆ’1โˆ’3=03 + 1 - 1 - 3 = 0, 5+0โˆ’2โˆ’3=05 + 0 - 2 - 3 = 0.

Example 9.58 (Two conflicting equations)

Take the matrix AA of Example 9.44 and ๐›=(1,2,3,5)T\mathbf{b} = (1,\, 2,\, 3,\, 5)^{T}. The system is x1=1,x1+x2=2,x1+x2+x3=3,x1+x2+x3=5,x_1 = 1, \qquad x_1 + x_2 = 2, \qquad x_1 + x_2 + x_3 = 3, \qquad x_1 + x_2 + x_3 = 5 , and the last two equations contradict each other. Using Example 9.48, QT๐›=(๐ช1โ‹…๐›๐ช2โ‹…๐›๐ช3โ‹…๐›)=(11/27/(23)4/6),Q^{T}\mathbf{b} = \begin{pmatrix} \mathbf{q}_1 \cdot \mathbf{b} \\ \mathbf{q}_2 \cdot \mathbf{b} \\ \mathbf{q}_3 \cdot \mathbf{b} \end{pmatrix} = \begin{pmatrix} 11/2 \\ 7/(2\sqrt{3}) \\ 4/\sqrt{6} \end{pmatrix}, and back substitution in (9.12) gives x3=4/66/3=2x_3 = \frac{4/\sqrt{6}}{\sqrt{6}/3} = 2, then 32x2=723โˆ’23=323\frac{\sqrt{3}}{2}x_2 = \frac{7}{2\sqrt{3}} - \frac{2}{\sqrt{3}} = \frac{3}{2\sqrt{3}}, so x2=1x_2 = 1, and 2x1=112โˆ’32โˆ’2=22x_1 = \frac{11}{2} - \frac{3}{2} - 2 = 2, so x1=1x_1 = 1. Thus ๐ฑฬ‚=(1,1,2)T\hat{\mathbf{x}} = (1,\, 1,\, 2)^{T}: the first two equations hold exactly, and x1+x2+x3=4x_1 + x_2 + x_3 = 4 splits the difference between 33 and 55. The residual is (0,0,โˆ’1,1)T(0,\, 0,\, -1,\, 1)^{T}, and the error is 2\sqrt{2}.

Remark 9.59 (Dependent columns)

If the columns of AA are dependent, ๐›ฬ‚\hat{\mathbf{b}} is still unique, but A๐ฑ=๐›ฬ‚A\mathbf{x} = \hat{\mathbf{b}} has infinitely many solutions, all of the form ๐ฑฬ‚+๐ง\hat{\mathbf{x}} + \mathbf{n} with ๐งโˆˆNul(A)\mathbf{n} \in \operatorname{Nul}(A) (the structure theorem, Theorem 3.3). For example, for A=(121212)A = \begin{pmatrix} 1 & 2 \\ 1 & 2 \\ 1 & 2 \end{pmatrix} and ๐›=(1,2,6)T\mathbf{b} = (1,\, 2,\, 6)^{T}, the normal equations (9.10) are 3x1+6x2=93x_1 + 6x_2 = 9 and 6x1+12x2=186x_1 + 12x_2 = 18, a single equation x1+2x2=3x_1 + 2x_2 = 3: every point of this line is a least-squares solution, and each gives A๐ฑ=(3,3,3)TA\mathbf{x} = (3,\, 3,\, 3)^{T}, the projection of ๐›\mathbf{b} onto the line Col(A)\operatorname{Col}(A). In such cases one usually asks in addition for the least-squares solution of smallest length. It is the one orthogonal to Nul(A)=Span((โˆ’2,1)T)\operatorname{Nul}(A) = \operatorname{Span}\bigl((-2,\, 1)^{T}\bigr), that is, the one in Row(A)=Span((1,2)T)\operatorname{Row}(A) = \operatorname{Span}\bigl((1,\, 2)^{T}\bigr) (Remark 9.25): (x1,x2)=s(1,2)(x_1,\, x_2) = s\,(1,\, 2) with s+4s=3s + 4s = 3, so ๐ฑ=(3/5,6/5)T\mathbf{x} = (3/5,\, 6/5)^{T}, of length 3/53/\sqrt{5}.

Exercise 9.10 auto-check

Four measurements of a quantity xx give 22, 33, 33, 88. What is the least-squares solution of x=2x = 2, x=3x = 3, x=3x = 3, x=8x = 8?

Answer:
Exercise 9.11 auto-check

Find the least-squares line y=c0+c1ty = c_0 + c_1 t for the data (โˆ’1,1)(-1,\, 1), (0,2)(0,\, 2), (1,5)(1,\, 5). What is the slope c1c_1?

Answer:
Exercise 9.12 auto-check

Let AA be a 6ร—36 \times 3 matrix with independent columns and ๐ฑฬ‚\hat{\mathbf{x}} the least-squares solution of A๐ฑ=๐›A\mathbf{x} = \mathbf{b}. Which statement is always true?

Remark 9.60 (Looking ahead)

Least squares fits not only lines: any model that is linear in its unknown coefficients, such as a parabola c0+c1t+c2t2c_0 + c_1 t + c_2 t^2 or a combination of sines and cosines, leads to an inconsistent system and to the same normal equations.

When the columns of AA are dependent, the least-squares solutions form the set ๐ฑฬ‚+Nul(A)\hat{\mathbf{x}} + \operatorname{Nul}(A) (Remark 9.59), and the one of smallest length is the unique least-squares solution lying in Row(A)\operatorname{Row}(A), the orthogonal complement of Nul(A)\operatorname{Nul}(A). It is written ๐ฑโ€ =Aโ€ ๐›\mathbf{x}^{\dagger} = A^{\dagger}\mathbf{b}, where Aโ€ A^{\dagger} is the pseudoinverse of AA; when the columns are independent, Aโ€ =(ATA)โˆ’1ATA^{\dagger} = (A^{T}A)^{-1}A^{T} and ๐ฑโ€ \mathbf{x}^{\dagger} is the ๐ฑฬ‚\hat{\mathbf{x}} of (9.11). Both Aโ€ A^{\dagger} and the most reliable numerical least-squares methods come from the singular value decomposition (SVD) A=UฮฃVTA = U\Sigma V^{T}, which exists for every mร—nm \times n matrix: UU (mร—mm \times m) and VV (nร—nn \times n) are orthogonal matrices, and ฮฃ\Sigma is an mร—nm \times n matrix whose only nonzero entries are the singular values ฯƒ1โ‰ฅโ‹ฏโ‰ฅฯƒr>0\sigma_1 \ge \cdots \ge \sigma_r > 0 in positions (1,1),โ€ฆ,(r,r)(1,1), \dots, (r,r), with r=rankAr = \operatorname{rank} A. Then Aโ€ =Vฮฃโ€ UTA^{\dagger} = V\Sigma^{\dagger}U^{T}, where the nร—mn \times m matrix ฮฃโ€ \Sigma^{\dagger} has 1/ฯƒ1,โ€ฆ,1/ฯƒr1/\sigma_1, \dots, 1/\sigma_r in the same positions. The SVD also shows how close AA is to having dependent columns, which is what matters when the data are rounded.

The matrices of this lecture also carry a structural message. A projection matrix satisfies PT=PP^{T} = P and is diagonalizable with an orthogonal basis of eigenvectors, and ATAA^{T}A is symmetric as well. The spectral theorem says that this is true of every symmetric matrix: it can be diagonalized as A=QDQTA = QDQ^{T} with QQ orthogonal, which removes both obstacles to diagonalization met in Lecture 8. Applied to the symmetric matrix ATAA^{T}A, whose eigenvalues are ฯƒ12,โ€ฆ,ฯƒr2\sigma_1^2, \dots, \sigma_r^2 and 00, it gives the matrix VV and the singular values of the SVD.

Index of statements

  1. Example 9.1 (Measuring one quantity three times)
  2. Example 9.2 (Fitting a line to data)
  3. Remark 9.3 (Why exact solutions are rare)
  4. Remark 9.4 (The plan)
  5. Definition 9.5 (Inner product)
  6. Proposition 9.6 (Properties of the inner product)
  7. Definition 9.7 (Length, distance, unit vector)
  8. Remark 9.8 (Why this is the right length)
  9. Example 9.9 (Computing lengths)
  10. Definition 9.10 (Orthogonal vectors)
  11. Theorem 9.11 (Pythagoras)
  12. Theorem 9.12 (Cauchyโ€“Schwarz and triangle inequalities)
  13. Remark 9.13 (Correlation and angles)
  14. Definition 9.14 (Orthogonal sets, matrices and bases)
  15. Lemma 9.15 (The matrix VTHV^{T}H records inner products)
  16. Remark 9.16 (Correlation tables and attention in transformers)
  17. Example 9.17 (A column orthogonal 4ร—44 \times 4 matrix)
  18. Theorem 9.18 (Orthogonal sets are independent)
  19. Example 9.19 (Coordinates without row reduction)
  20. Proposition 9.20 (Column orthonormal matrices preserve inner products)
  21. Example 9.21 (Checking the proposition)
  22. Definition 9.22 (Orthogonal complement)
  23. Proposition 9.23 (Computing WโŠฅW^{\perp})
  24. Proposition 9.24 (The dimension of WโŠฅW^{\perp})
  25. Remark 9.25 (The four subspaces of a matrix)
  26. Example 9.26 (A plane and its normal line)
  27. Example 9.27 (Complements in โ„4\mathbb{R}^4)
  28. Lemma 9.28 (BTBB^{T}B is invertible)
  29. Theorem 9.29 (The orthogonal projection theorem)
  30. Definition 9.30 (Orthogonal projection)
  31. Example 9.31 (Projecting onto a plane)
  32. Theorem 9.32 (Best approximation)
  33. Definition 9.33 (Distance to a subspace)
  34. Theorem 9.34 (Properties of the projection)
  35. Remark 9.35 (The projection as a diagonalizable map)
  36. Theorem 9.36 (The projection formula for an orthogonal basis)
  37. Remark 9.37 (Projection onto a line; a warning)
  38. Example 9.38 (The plane again, with an orthogonal basis)
  39. Example 9.39 (A projection in โ„4\mathbb{R}^4)
  40. Theorem 9.40 (The Gramโ€“Schmidt process)
  41. Remark 9.41 (The Gramโ€“Schmidt relations)
  42. Remark 9.42 (Practical points)
  43. Example 9.43 (Gramโ€“Schmidt in โ„3\mathbb{R}^3)
  44. Example 9.44 (Gramโ€“Schmidt on the columns of a 4ร—34 \times 3 matrix)
  45. Theorem 9.45 (The QR factorization)
  46. Remark 9.46 (Computing RR)
  47. Example 9.47 (QR of the 3ร—33 \times 3 matrix)
  48. Example 9.48 (QR of the 4ร—34 \times 3 matrix)
  49. Definition 9.49 (Least-squares solution)
  50. Theorem 9.50 (Characterization of least-squares solutions)
  51. Remark 9.51 (ATAA^{T}A in general)
  52. Theorem 9.52 (When the least-squares solution is unique)
  53. Proposition 9.53 (Least squares from the Gramโ€“Schmidt data)
  54. Remark 9.54 (Three methods)
  55. Example 9.55 (The three measurements)
  56. Example 9.56 (The least-squares line)
  57. Example 9.57 (One problem, three methods)
  58. Example 9.58 (Two conflicting equations)
  59. Remark 9.59 (Dependent columns)
  60. Remark 9.60 (Looking ahead)