8.2 Orthogonal Bases and the Gram–Schmidt Process
Any basis can be straightened into an orthonormal one: subtract from each vector its projections onto the ones before, then normalise. Recorded as a matrix factorisation, that is $A=QR$.
Section 8.1 showed that orthogonal bases are a pleasure to work with: coordinates come from single dot products, and orthogonal matrices are inverted by transposing. But the bases that problems hand us are rarely orthogonal. The null space of a matrix comes out of row reduction with a basis that points in awkward directions; the columns of a data matrix are whatever the measurements happened to be; the natural basis $1,t,t^2$ of $P_2$ is not orthogonal for most inner products.
The Gram–Schmidt process repairs this. It takes any basis of a subspace and, one vector at a time, removes the part that points along the directions already chosen — what is left is perpendicular to all of them. The same calculation, written as matrices, factorises a matrix $A$ as $A=QR$ with $Q$ having orthonormal columns and $R$ upper triangular. This QR factorisation is one of the workhorses of scientific computing: it is how computers solve least-squares problems reliably (Section 8.3), and the starting point of the standard algorithm for computing eigenvalues.
The Idea: Subtract the Projection
Recall from Section 4.2 the projection of $\mathbf{x}$ onto the line through a nonzero vector $\mathbf{v}$: $$\operatorname{proj}_{\mathbf{v}}\mathbf{x}=\frac{\mathbf{x}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\,\mathbf{v}.$$ What is left over, $\mathbf{x}-\operatorname{proj}_{\mathbf{v}}\mathbf{x}$, is orthogonal to $\mathbf{v}$: $$\left(\mathbf{x}-\frac{\mathbf{x}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\mathbf{v}\right)\cdot\mathbf{v}=\mathbf{x}\cdot\mathbf{v}-\frac{\mathbf{x}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\,(\mathbf{v}\cdot\mathbf{v})=0.$$ So from two independent vectors $\mathbf{x}_1,\mathbf{x}_2$ we get two orthogonal ones: keep $\mathbf{v}_1=\mathbf{x}_1$ and replace $\mathbf{x}_2$ by $\mathbf{v}_2=\mathbf{x}_2-\operatorname{proj}_{\mathbf{v}_1}\mathbf{x}_2$. Since $\mathbf{v}_2$ is $\mathbf{x}_2$ minus a multiple of $\mathbf{x}_1$, the new pair spans the same plane as the old one.
The Gram–Schmidt Process
With more vectors we repeat the idea: each new vector has its projections onto all the earlier $\mathbf{v}$'s removed.
Why it works: suppose $\mathbf{v}_1,\dots,\mathbf{v}_{k-1}$ are already orthogonal. Dotting the formula for $\mathbf{v}_k$ with $\mathbf{v}_j$ ($j\lt k$) kills every subtracted term except the $j$-th, and that one cancels $\mathbf{x}_k\cdot\mathbf{v}_j$ exactly, as in the two-vector calculation above. And $\mathbf{v}_k\ne\mathbf{0}$, because otherwise $\mathbf{x}_k$ would lie in $\operatorname{span}\{\mathbf{v}_1,\dots,\mathbf{v}_{k-1}\}=\operatorname{span}\{\mathbf{x}_1,\dots,\mathbf{x}_{k-1}\}$, contradicting independence.
- Start with a basis $\mathbf{x}_1,\dots,\mathbf{x}_p$ of $W$. (If you are given a spanning set that might be dependent, just run the process: a $\mathbf{v}_k$ that comes out as $\mathbf{0}$ tells you $\mathbf{x}_k$ was redundant — drop it and carry on.)
- Set $\mathbf{v}_1=\mathbf{x}_1$. For each later $\mathbf{x}_k$, compute the coefficients $\dfrac{\mathbf{x}_k\cdot\mathbf{v}_j}{\mathbf{v}_j\cdot\mathbf{v}_j}$ for every earlier $\mathbf{v}_j$ and subtract.
- Clear fractions as you go. You may replace any $\mathbf{v}_k$ by a nonzero multiple of itself (e.g. $(\tfrac12,-\tfrac12,1)\to(1,-1,2)$) before using it in later steps: the projection onto a line does not depend on which vector along the line you use.
- Check that every pair $\mathbf{v}_i\cdot\mathbf{v}_j$ is $0$ — it catches almost every arithmetic slip.
- Normalise last, if an orthonormal basis is wanted: $\mathbf{q}_k=\mathbf{v}_k/\|\mathbf{v}_k\|$.
Orthogonal polynomials (uses calculus). With the integral inner product $\langle f,g\rangle=\int_{-1}^1f(t)g(t)\,dt$ on $C[-1,1]$, Gram–Schmidt turns $1,t,t^2,t^3$ into $1,\ t,\ t^2-\tfrac13,\ t^3-\tfrac35t$ (Exercise III.5). Up to scaling these are the Legendre polynomials, used by engineers to approximate functions and by computers to evaluate integrals very accurately (Gaussian quadrature).
1. Projecting onto the old vectors. Subtract projections onto the new orthogonal vectors $\mathbf{v}_1,\dots,\mathbf{v}_{k-1}$, never onto $\mathbf{x}_1,\dots,\mathbf{x}_{k-1}$. The $\mathbf{x}$'s are not orthogonal, so subtracting their projections does not produce a vector perpendicular to them.
2. Forgetting a term. $\mathbf{v}_3$ needs two subtractions, $\mathbf{v}_4$ three, and so on.
3. Mixing up normalised and unnormalised formulas. If you normalise as you go, the projection onto $\mathbf{q}_j$ is $(\mathbf{x}_k\cdot\mathbf{q}_j)\,\mathbf{q}_j$ — no denominator, because $\mathbf{q}_j\cdot\mathbf{q}_j=1$. Using $\dfrac{\mathbf{x}_k\cdot\mathbf{v}_j}{\mathbf{v}_j\cdot\mathbf{v}_j}$ with the unnormalised $\mathbf{v}_j$ is equally correct; just do not combine a $\mathbf{q}$ in the numerator with a $\mathbf{v}$ elsewhere.
4. The order matters. Gram–Schmidt on $(1,1),(1,0)$ gives $(1,1),\left(\tfrac12,-\tfrac12\right)$; on $(1,0),(1,1)$ it gives $(1,0),(0,1)$. Both answers are correct orthogonal bases of $\mathbb{R}^2$ — a subspace has many.
The QR Factorisation
Run Gram–Schmidt on the columns $\mathbf{a}_1,\dots,\mathbf{a}_n$ of a matrix $A$ and normalise. Because $\operatorname{span}\{\mathbf{q}_1,\dots,\mathbf{q}_k\}=\operatorname{span}\{\mathbf{a}_1,\dots,\mathbf{a}_k\}$, each column $\mathbf{a}_k$ is a combination of $\mathbf{q}_1,\dots,\mathbf{q}_k$ only: $$\mathbf{a}_k=r_{1k}\mathbf{q}_1+r_{2k}\mathbf{q}_2+\dots+r_{kk}\mathbf{q}_k,\qquad r_{jk}=\mathbf{q}_j\cdot\mathbf{a}_k$$ (the coefficients come from Theorem 8.5, since the $\mathbf{q}$'s are orthonormal). Collecting these equations column by column gives a matrix identity.
The diagonal entries are $r_{kk}=\|\mathbf{v}_k\|\gt0$, the lengths of the Gram–Schmidt vectors before normalising; the entries below the diagonal are $0$ because $\mathbf{a}_k$ needs no $\mathbf{q}_j$ with $j\gt k$.
Using the QR Factorisation
Suppose $A=QR$ and we want to solve $A\mathbf{x}=\mathbf{b}$. Multiply $QR\mathbf{x}=\mathbf{b}$ on the left by $Q^{T}$ and use $Q^{T}Q=I$: $$R\mathbf{x}=Q^{T}\mathbf{b}.$$ Since $R$ is upper triangular, this is solved by back substitution — no row reduction and no inverse. If $A$ is square (and invertible), the solution of $R\mathbf{x}=Q^{T}\mathbf{b}$ is the solution of $A\mathbf{x}=\mathbf{b}$. If $A$ has more rows than columns, the same $\mathbf{x}$ solves $A\mathbf{x}=\mathbf{b}$ whenever $\mathbf{b}$ is in $\operatorname{Col}A$ — and when $\mathbf{b}$ is not in $\operatorname{Col}A$, it is the best approximate (least-squares) solution, as Section 8.3 will show.
Exercises
- 1 $\mathbf{x}_1=(3,4)$, $\ \mathbf{x}_2=(1,0)$
- 2 $\mathbf{x}_1=(1,2)$, $\ \mathbf{x}_2=(4,3)$
- 3 $\mathbf{x}_1=(1,1,1)$, $\ \mathbf{x}_2=(0,1,2)$
- 4 $\mathbf{x}_1=(1,2,2)$, $\ \mathbf{x}_2=(3,3,0)$
- 5 $\mathbf{x}_1=(1,1,1)$, $\ \mathbf{x}_2=(1,1,0)$, $\ \mathbf{x}_3=(1,0,0)$
- 6 The plane $x-y+2z=0$ in $\mathbb{R}^3$ (find a basis first).
- 7 $\operatorname{Col}A$ for $A=\begin{bmatrix}1&3\\1&1\\1&1\end{bmatrix}$.
- 8 $\mathbf{x}_1=(1,2,3)$, $\ \mathbf{x}_2=(2,4,6)$. What goes wrong, and why?
- 1 $\mathbf{x}_1=(1,1,0,0)$, $\ \mathbf{x}_2=(1,0,1,0)$
- 2 $\mathbf{x}_1=(-1,0,1,1)$, $\ \mathbf{x}_2=(0,1,1,2)$, $\ \mathbf{x}_3=(0,0,2,2)$
- 3 Find an orthonormal basis for the subspace $x_1+x_2+x_3+x_4=0$ of $\mathbb{R}^4$.
- 4 $\mathbf{x}_1=(1,-1,1,-1)$, $\ \mathbf{x}_2=(2,0,2,0)$, $\ \mathbf{x}_3=(2,1,1,2)$
- 5 $\mathbf{x}_1=(1,1,1,1)$, $\ \mathbf{x}_2=(1,2,3,4)$, $\ \mathbf{x}_3=(2,3,4,5)$. What does Gram–Schmidt reveal about these vectors?
- 6 Extend the orthogonal set $\{(1,1,1,1),\ (-3,-1,1,3)\}$ to an orthogonal basis of $\mathbb{R}^4$.
- 1 On $P_2$ with $\langle p,q\rangle=p(-1)q(-1)+p(0)q(0)+p(1)q(1)$, apply Gram–Schmidt to $1,t,t^2$.
- 2 With the inner product of part 1, apply Gram–Schmidt to $1+t$, $t^2$ (in that order).
- 3 On $\mathbb{R}^2$ with $\langle\mathbf{u},\mathbf{v}\rangle=2u_1v_1+u_2v_2$, apply Gram–Schmidt to $(1,1)$, $(1,0)$ and normalise.
- 4 (uses calculus) On $C[0,1]$ with $\langle f,g\rangle=\displaystyle\int_0^1f(t)g(t)\,dt$, apply Gram–Schmidt to $1,t,t^2$.
- 5 (uses calculus) On $C[-1,1]$ with $\langle f,g\rangle=\displaystyle\int_{-1}^1f(t)g(t)\,dt$, apply Gram–Schmidt to $1,t,t^2,t^3$.
- 1 Find a QR factorisation of $A=\begin{bmatrix}1&2\\0&1\\1&0\end{bmatrix}$ (see Example 1).
- 2 Find a QR factorisation of $A=\begin{bmatrix}3&1\\4&0\end{bmatrix}$.
- 3 Find a QR factorisation of $A=\begin{bmatrix}1&3\\2&3\\2&0\end{bmatrix}$.
- 4 Find a QR factorisation of $A=\begin{bmatrix}1&2&2\\-1&0&1\\1&2&1\\-1&0&2\end{bmatrix}$ (see Exercise II.4).
- 5 Use your answer to part 4 to solve $A\mathbf{x}=\mathbf{b}$ with $\mathbf{b}=(3,1,1,3)$, and check the solution.
- 6 If $A=QR$ where $Q$ has orthonormal columns, show that $R=Q^{T}A$. Why must $R$ be invertible when the columns of $A$ are independent?
- 7 What is the QR factorisation of an orthogonal matrix $U$? Of a matrix whose columns are orthogonal but not unit vectors?
- 1 Explain why the Gram–Schmidt formula for $\mathbf{v}_k$ uses $\mathbf{v}_1,\dots,\mathbf{v}_{k-1}$ and not $\mathbf{x}_1,\dots,\mathbf{x}_{k-1}$. Illustrate with $\mathbf{x}_1=(1,0,0)$, $\mathbf{x}_2=(1,1,0)$, $\mathbf{x}_3=(1,1,1)$.
- 2 Apply Gram–Schmidt to $(1,1),(1,0)$ and then to $(1,0),(1,1)$. Are the answers the same? Are both correct?
- 3 Show that $\operatorname{span}\{\mathbf{v}_1,\mathbf{v}_2\}=\operatorname{span}\{\mathbf{x}_1,\mathbf{x}_2\}$ in Theorem 8.7.
- 4 Surveying. A drone's camera records two direction vectors $\mathbf{x}_1=(2,1,2)$ and $\mathbf{x}_2=(1,1,1)$ (in metres) along the edges of a sloping roof. Find an orthonormal pair of directions in the roof plane, with the first along $\mathbf{x}_1$, so that the roof can be mapped with a right-angled grid of $1$ m squares.
Research question. Gram–Schmidt is exact on paper. What happens to it inside a computer, which rounds every number — and why do numerical libraries use a "modified" version?
Why it matters. Weather centres, GPS receivers and engineering simulation codes compute QR factorisations of huge matrices millions of times a day. If rounding destroys orthogonality, the computed $Q$ no longer satisfies $Q^{T}Q=I$ and the answers of least-squares problems drift. Understanding this is a first step into numerical analysis.
What to do.
- Take $\varepsilon=0.01$ and $\mathbf{x}_1=(1,\varepsilon,0,0)$, $\mathbf{x}_2=(1,0,\varepsilon,0)$, $\mathbf{x}_3=(1,0,0,\varepsilon)$. These are independent but nearly parallel.
- Run classical Gram–Schmidt (the formulas of Theorem 8.7, normalising each $\mathbf{v}_k$), rounding every intermediate result to 4 significant digits — as a cheap computer would. Note that $1+\varepsilon^2=1.0001$ rounds to $1.000$.
- Run modified Gram–Schmidt: compute $\mathbf{w}=\mathbf{x}_3-(\mathbf{x}_3\cdot\mathbf{q}_1)\mathbf{q}_1$ first, then subtract $(\mathbf{w}\cdot\mathbf{q}_2)\mathbf{q}_2$ using the updated vector $\mathbf{w}$. Round in the same way.
- For each method compute $\mathbf{q}_2\cdot\mathbf{q}_3$. Which method keeps the vectors orthogonal?
Tools: Spreadsheet with the ROUND function, or Python
Hand in: A half-page note with the two computed $\mathbf{q}_3$ vectors, the values of $\mathbf{q}_2\cdot\mathbf{q}_3$, and a sentence explaining why the modified version helps.
Going further. Look up Householder reflections, the method most numerical libraries actually use for QR. A Householder matrix $H=I-2\mathbf{u}\mathbf{u}^{T}$ ($\|\mathbf{u}\|=1$) is orthogonal and symmetric — verify both facts.
🧪 Self-check quiz
Q1. Gram–Schmidt on $\mathbf{x}_1=(1,0)$, $\mathbf{x}_2=(1,1)$ gives $\mathbf{v}_2=$
Q2. In a QR factorisation of a matrix with independent columns, $R$ is
Q3. If $A=QR$ and $Q$ has orthonormal columns, then $R$ equals
Q4. Running Gram–Schmidt on $\mathbf{x}_1,\mathbf{x}_2,\mathbf{x}_3$ produces $\mathbf{v}_3=\mathbf{0}$. This means
Q5. Normalising $\mathbf{v}=(2,-1,2)$ gives