Linear Algebra I · Orthonormal bases and QR factorization
Prerequisites: dot products and norms, projection, independent columns, transpose, triangular systems, and the preceding least-squares lesson. All quantities here are real. Goals: find orthonormal coordinates, perform Gram–Schmidt, state thin-QR dimensions and rank conditions, solve least squares through R, and check results without confusing a rectangular Q with an invertible square matrix. Reproduce the examples, run the downloadable or browser lab, and attempt the practice and open-book assessment before opening solutions.
Orthonormal coordinates and a rectangular Q
A list q₁,…,qₙ is orthonormal when qᵢ·qⱼ is 1 for i=j and 0 otherwise. Put these vectors in the columns of an m×n matrix Q. Then QᵀQ=Iₙ. For y in their span, y=Qc with coordinates c=Qᵀy. For a general y in ℝᵐ, Qᵀy instead gives coordinates of its projection p=QQᵀy. Proof: each qⱼ·(y−p)=0, using QᵀQ=I. Also ||Qc||²=cᵀQᵀQc=||c||². This preserves the length of coefficient vectors mapped by Q; it does not say that projecting an arbitrary y preserves its length.
Only when m=n does an orthonormal-column Q become a square orthogonal matrix, with Q⁻¹=Qᵀ and QQᵀ=Iₘ. A tall Q with m>n has Qᵀ as a left inverse, while QQᵀ is a projection of rank n rather than the identity on all ℝᵐ. Keep the input and output spaces explicit.
Gram–Schmidt: remove projections, then normalize
Start with independent columns a₁,…,aₙ in ℝᵐ, so n≤m. Set w₁=a₁ and q₁=w₁/||w₁||. At step j, compute rᵢⱼ=qᵢᵀaⱼ for i
If an exact wⱼ is zero, that column is already in the span of the previous ones. Do not divide by zero. The original list is dependent, so this full-column-rank construction cannot produce an invertible n×n R. A factorization with a singular R or fewer independent Q columns can still exist. Very small floating-point residuals require numerical care; a rounded display of zero alone is not a proof of exact dependence.
Thin QR and least squares
The column identities aⱼ=∑ᵢ≤ⱼqᵢrᵢⱼ combine into A=QR. For full-column-rank A of size m×n, thin Q is m×n, R is n×n upper triangular, QᵀQ=Iₙ, and the chosen diagonal rⱼⱼ is positive. Nonzero diagonal entries make R invertible. Multiplying QᵀA gives R. To fit y, compute z=Qᵀy and solve Rβ̂=z by back-substitution, starting at the last row. No explicit inverse of R or AᵀA is required.
Why this solves least squares: decompose y=QQᵀy+(I−QQᵀ)y. The two parts are orthogonal, so ||y−QRβ||²=||Qᵀy−Rβ||²+||(I−QQᵀ)y||². The second term does not depend on β; the first becomes zero when Rβ=Qᵀy. Since R is invertible, β̂ is unique, the fitted vector is QQᵀy, and Aᵀ(y−Aβ̂)=0. Check these identities as well as A≈QR and QᵀQ≈I in numerical work.
Worked examples
- Orthonormal coordinates: q₁=(1,1)/√2 and q₂=(1,−1)/√2 form an orthonormal basis of ℝ². For y=(3,1), coordinates Qᵀy=(2√2,√2). Reconstructing gives 2√2q₁+√2q₂=(3,1). Both squared lengths are 10. This Q is square, so Qᵀ really is its two-sided inverse.
- Gram–Schmidt in ℝ³: a₁=(1,1,0), a₂=(1,0,1). Then r₁₁=√2, q₁=(1,1,0)/√2, r₁₂=1/√2, and w₂=(1/2,−1/2,1). Its norm is √(3/2), so q₂=(1,−1,2)/√6. Thus R=[√2 1/√2; 0 √(3/2)]. Check q₁·q₂=0, ||q₂||=1, and q₁r₁₂+q₂r₂₂=(1,0,1).
- Least squares through R: use columns a₁=(1,0,1), a₂=(1,1,1), and y=(2,3,4). Here q₁=(1,0,1)/√2, q₂=(0,1,0), R=[√2 √2;0 1], and z=Qᵀy=(3√2,3). Back-substitution gives β₂=3 and β₁=0. Fit Aβ̂=(3,3,3), residual (−1,0,1), and SSE=2. Both Qᵀr and Aᵀr vanish.
- Dependent columns: replace a₂ in example 3 by a₁. Then w₂=0. Do not manufacture q₂ by dividing by its norm. A rank-one representation is A=q₁[√2 √2]. The unique fit to y is (3,0,3), with SSE=11, but every coefficient pair satisfying β₁+β₂=3 gives it. Dependence prevents a unique triangular solve, not the existence of a best fitted vector.
Common mistakes and numerical checks
Orthogonal vectors need not have unit length; divide by the norm, not its square. Recompute the residual after removing all previous components. Keep the columns in the stated order and the diagonal of R positive in this convention. QᵀQ=I does not imply QQᵀ=I for a tall matrix. Do not infer rank from a six-decimal display. Floating-point square roots and dot products are approximate; check reconstruction, orthonormality, and residual orthogonality with a stated tolerance. The small lab is a teaching implementation, not a general-purpose numerical QR library.
Practice
- Normalize u=(3,4). Project y=(5,0) onto the resulting unit direction.
- Compute QR for columns a₁=(2,0), a₂=(1,2), with a positive diagonal.
- Let Q have columns (1,0,0) and (0,1,0). Calculate QᵀQ, QQᵀ, and the projection of (1,2,3). Compare squared lengths.
- Challenge: use a₁=(1,0,1), a₂=(1,−2,1), y=(2,3,4). Find q₂, R, and the least-squares coefficients. Why does q₂ point in a negative coordinate direction?
Fully worked solutions
- ||u||=5, so q=(3/5,4/5). Then qᵀy=3 and the projection is 3q=(9/5,12/5). The coefficient formula is this simple because q has unit length.
- q₁=(1,0), r₁₁=2, r₁₂=1. Residual w₂=(0,2), so q₂=(0,1), r₂₂=2. Therefore Q=I₂ and R=[2 1;0 2]=A.
- QᵀQ=I₂, but QQᵀ=diag(1,1,0). Projection (1,2,0) has squared length 5; the original vector has squared length 14. The removed component (0,0,3) has squared length 9.
- w₂=(0,−2,0), hence r₂₂=2 and q₂=(0,−1,0). R=[√2 √2;0 2], z=(3√2,−3), so β₂=−3/2 and β₁=9/2. The positive diagonal requires the sign to stay in q₂. Fit remains (3,3,3), residual (−1,0,1), SSE=2.
Assessment · Open book · 18 points
Open book: use books, lesson notes, cited readings, calculators, and relevant free software. Explain your own reasoning, assumptions, units, and checks; identify references or software you use. Attempt the questions before opening the answer key. Suggested study time: 40–60 minutes; this is a self-study guide, not a timed restriction.
Attempt independently before opening the rubric. Keep square roots exact in the hand calculations.
- Find the positive-diagonal QR for a₁=(1,0), a₂=(1,1), showing the projection and residual step. (4)
- Given q₁=(1,0,1)/√2, q₂=(0,1,0), R=[√2 √2;0 2], and y=(0,2,2), solve least squares using R. Give coefficients, fit, residual, and SSE. (5)
- What does an exact zero Gram–Schmidt residual mean, and what must you avoid? For a tall Q, is Qᵀ a two-sided inverse? (3)
Application and reasoning · 6 points
For A with columns (1,0,1),(1,1,1), a report gives Q columns (1,0,1)/√2,(0,1,0) and R=[√2 1;0 1]. Locate the reconstruction failure (2), repair R (2), then solve least squares for y=(0,1,2) and verify its residual (2).
Complete answer key and scoring rubric
- q₁=(1,0), r₁₁=1 (1). r₁₂=1 and projected component (1,0) (1). w₂=(0,1), q₂=(0,1), r₂₂=1 (1). Thus Q=I₂ and R=[1 1;0 1], which reconstructs A (1).
- z=Qᵀy=(√2,2) (1). Back-substitution gives β₂=1 and β₁=0 (2). Fit Qz=(1,2,1), residual (−1,0,1) (1). SSE=2 (1).
- The current column lies in the previous span (1). Do not divide by the zero residual norm or assume an invertible R (1). A tall Q only has QᵀQ=I; QQᵀ is a projection, so Qᵀ is not a two-sided inverse (1).
Application and reasoning · 6 points
The reported second column QR is (1/√2,1,1/√2), not (1,1,1) (2). r₁₂=q₁ᵀa₂=√2, giving R=[√2 √2;0 1] (1 each). z=(√2,1), coefficients (0,1), fit (1,1,1), residual (−1,0,1), SSE=2 and Aᵀr=0 (1 solve, 1 verification).
Scoring: existing questions 12 + application problem 6 = 18 points. Award the stated method and reasoning marks, not only final answers.
Download the QR and least-squares lab
Download the floating-point Python lab
Objective: apply Gram–Schmidt to a₁=(1,0,1), a₂=(1,k,1), reconstruct A, check QᵀQ, and fit y=(a,b,c) through R. Install free Python 3 for Windows, macOS, or Linux from the official link below. Save the file, open a terminal in its folder, and run the commands; on Windows use py -3 if needed. The file uses only standard-library math, needs no account or paid packages, and runs offline after installation. Inputs k,a,b,c are from −4 to 6 in steps of 0.5; fractions such as −1/2 are accepted. Results use floating-point square roots rather than exact rational arithmetic.
python3 ee-qr.py 1 2 3 4
python3 ee-qr.py 2 2 3 4
python3 ee-qr.py -1 2 3 4
python3 ee-qr.py 0 2 3 4
Procedure: predict w₂=(0,k,0), r₂₂=|k|, and the rank before running. Compare each download output with the browser lab. For k=1,2,−1, coefficients are respectively (0,3), (3/2,3/2), (6,−3); every fitted vector is (3,3,3), with SSE=2. At k=0 the rank is 1, no unique pair of coefficients exists, and fit (3,0,3) has SSE=11 with β₁+β₂=3. Both versions check reconstruction and orthonormality; Python also checks residual orthogonality, using tolerance 10⁻⁹ for these small half-unit inputs. Explain why Q can change with k’s sign while the full-rank fitted vector stays fixed: the same plane is spanned, but its orthonormal coordinates and coefficient mapping change.
This fixed-family numerical activity does not establish rank from noisy measurements, benchmark general QR software, or replace physical instrument training. The zero case is known algebraically, not inferred from a rounded numerical display.
Free primary readings and software
Read MIT’s Gram–Schmidt lecture and summary, then Stanford’s QR slides pp. 2–6 for construction and pp. 13–15 for orthogonal decomposition. The freely available notes, video, and exercises do not require a university login or purchase of the suggested textbook.
- MIT 18.06SC · Orthogonal Matrices and Gram-Schmidt: lecture, summary, exercises
- Stanford EE263 · QR factorization slides, pp. 2–6 and 13–15
- Stanford EE263 · Least squares and the orthogonality principle
- Python Software Foundation · Free Python 3 downloads
- Python documentation · math: square roots and floating-point calculations
QR lab · Reconstruct, check, and solve
Apply Gram–Schmidt to columns (1,0,1),(1,k,1) and fit y=(a,b,c).
Free tool built into this page. Use it in your browser without an account or download.
Procedure
Keep y=(2,3,4) and try k=1,2,−1,0. Predict the rank, residual w2, and coefficients. Compare the downloaded Python lab. Why does k=0 require a different branch?
Expected results and answer check
For k=1,2,−1 the coefficients are (0,3),(1.5,1.5),(6,−3), fit (3,3,3), and SSE=2. At k=0 the rank is 1, the coefficient sum is 3, fit (3,0,3), and SSE=11. Exact dependence is known from the family; it is not inferred from rounded zero errors.
Both versions use floating-point arithmetic on a fixed half-unit input family. Python checks residual orthogonality and the same reconstruction/orthonormality conditions with tolerance 1e−9. This is not a general QR solver or a physical measurement experiment.