Lab 4.3

Gram–Schmidt and Reduced QR

Focus
subtract → normalize → reconstruct A=QRA = QR
Notebook
Ch4-3 QR-Decomposition.ipynb

Lab goal

Explain each entry of Q and R, check orthonormality separately from reconstruction, and connect the QR projection to least squares.

Lecture-note concepts
  • Gram–Schmidt
  • Nested spans
  • Reduced QR
  • Orthogonal matrices
  • Numerical orthogonality

Code extensionTrace the lecture’s 3 × 2 example before inspecting near-dependent columns. Add a scale-aware normalization check including the first column.

Lecture 4 · Reading guide

Read the symbols first

Wooseok Ha, MAS110 Lecture 4 (Fall 2026), lec04.pdf: printed slides 34–37, 47, 49–51 (PDF pages 39–42, 55, 57–59). This is the same A used for projection and least squares in 4.4.

The matrix A has m=3m = 3 rows and n=2n = 2 independent columns. Gram–Schmidt replaces its columns by an orthonormal basis for the same column space; R records the coefficients needed to reconstruct the original columns. The lecture calls the normalized Gram–Schmidt vectors wiw_{i} before naming the QR columns qiq_{i}. We consistently use qiq_{i} for normalized directions and wjw_{j} for an unnormalized remainder.

⟨u,v⟩;∥u∥;u⊥v\langle u,v\rangle;\quad \lVert u\rVert;\quad u\perp v
An inner product gives one real number, ∥u∥=⟨u,u⟩\Vert u\Vert = \sqrt{\langle u,u\rangle} is its induced norm, and u⊥vu \perp v means ⟨u,v⟩=0\langle u,v\rangle = 0. The lecture writes |u| for this norm. In Euclidean space the default is uTvu^{\mathsf T}v and the norm is ∥u∥2\Vert u\Vert _{2}; for polynomials it is the stated integral norm.
φ;θ;θ^\varphi;\quad \theta;\quad \hat\theta
This chapter uses φ\varphi for an angle and θ\theta for model coefficients in Lab 4.4. The lecture sometimes uses θ\theta for the angle too; the distinction here prevents mixing an angle with a coefficient vector. A hat marks a minimizing coefficient, not a unit vector.
A;G;Q;R;PA;\quad G;\quad Q;\quad R;\quad P
A stores input columns in the Euclidean QR and least-squares labs. G denotes a Gram matrix of inner products, including the polynomial metric (which the lecture locally calls A). Q stores orthonormal columns, R stores their reconstruction coefficients, and P projects onto a subspace. These matrices have different jobs and shapes.
A=[a1∣a2]∈R3×2A=[a_1\mid a_2]\in\mathbb R^{3\times2}
aja_{j} here means column j, a vector in R3\mathbb R^{3}. In the statistical model of 4.4, the lecture also names a row feature vector aia_{i} via row aiTa_{i}^{\mathsf T}. Check the index and shape: columns are feature directions in measurement space; rows collect features for one measurement.
wj;qj=wj∥wj∥2w_j;\quad q_j=\frac{w_j}{\lVert w_j\rVert_2}
wjw_{j} is what remains after removing all earlier orthonormal components of aja_{j}. qjq_{j} has unit length; wjw_{j} generally does not. This normalization needs a nonzero remainder. Independent columns guarantee it in exact arithmetic.
rij=qiTaj (i<j);rjj=∥wj∥2r_{ij}=q_i^{\mathsf T}a_j\ (i<j);\quad r_{jj}=\lVert w_j\rVert_2
These scalars are the reconstruction coefficients. With the positive-diagonal Gram–Schmidt convention, rjj>0r_{jj} > 0. A library can use different matching signs; that does not change QR or the column space.
Q∈Rm×n;R∈Rn×n,m≥nQ\in\mathbb R^{m\times n};\quad R\in\mathbb R^{n\times n},\quad m\ge n
For full column rank, reduced Q has n orthonormal columns and reduced R is invertible and upper triangular. QTQ=InQ^{\mathsf T}Q = I_{n}. For m > n, Q is rectangular and QQT=PQQ^{\mathsf T} = P, not ImI_{m}.
Complete QR;Qfull∈Rm×m\text{Complete QR};\quad Q_{\mathrm{full}}\in\mathbb R^{m\times m}
Complete Q extends the basis to all of Rm;Rfull\mathbb R^{m}; R_{\mathrm{full}} has shape m × n. The extra Q columns do not all belong to Col(A). QfullQfullT=ImQ_{\mathrm{full}}Q_{\mathrm{full}}^{\mathsf T} = I_{m}, whereas projection onto Col(A) uses only the relevant column-space basis.
ε\varepsilon; numerical threshold; matrix rank
ε\varepsilon is a small parameter in the near-dependent example, not a residual vector. In floating point a tiny remainder can indicate numerical dependence. The demonstration routine compares against a threshold scaled by the matrix norm; it is a teaching rank policy, not an exact symbolic rank test.
np.linalg.qr(A, mode="reduced")
This returns a reduced factorization using LAPACK routines. Rank-deficient matrices still have QR factorizations, but R is singular and extra Q directions need not lie in Col(A). The full-column-rank solve Rθ=QTbR\theta = Q^{\mathsf T}b then cannot be used as an invertible solve.

Key ideas

Terms you will use

Gram–Schmidtwj=aj−∑i<j(qiTaj)qiw_j=a_j-\sum_{i<j}(q_i^{\mathsf T}a_j)q_i

Remove each old orthonormal component, then normalize the new information.

Keep in mind A zero remainder cannot be normalized.
Reduced QRA=QR;QTQ=InA = QR;\quad Q^{\mathsf T}Q = I_n

An orthonormal column basis and the triangular coefficients reconstruct the input.

Keep in mind Reduced Q is rectangular when m > n.
Two numerical checks∥A−QR∥;∥QTQ−I∥\lVert A-QR\rVert;\quad \lVert Q^{\mathsf T}Q-I\rVert

Measure reconstruction separately from orthonormality.

Keep in mind One small error does not establish the other.

Change the input

See the geometry respond

This is the plane of the lecture’s columns, shown in its orthonormal q1,q2q_{1},q_{2} coordinates. The initial angle 120° gives the lecture’s exact components. Change the angle to see when the second column adds a new direction.

Step 1 of 3 · original columns
r12r_{12}
-0.70711
r22=∥w2∥2r_{22}=\lVert w_2\rVert_2
1.22474
Column rank
2 · independent
Gram–Schmidt in the plane spanned by the lecture columnsa1 · first column; a2 · second column. Both axes use the same scale.-0.80.41.500.61.2coordinate along lecture q₁coordinate along lecture q₂
  • a1a_1 · first column
  • a2a_2 · second column

a2=−0.70711q1+1.22474q2a_2=-0.70711q_1+1.22474q_2. The normalization changes the remainder’s length to 1, while R keeps the original scale.

Lab 4.3 · Lecture QR example

Build the two columns of Q and read R

Keep the lecture’s A=[[−1,1],[0,1],[1,0]]A = [[-1,1],[0,1],[1,0]] throughout. The same column-space basis leads directly to Lab 4.4.

Select a line to see its action, array shape, and concrete operation.

Maintained notebookCh4-3 QR-Decomposition.ipynb ↗Contains guarded Gram–Schmidt routines, rank-deficient cases, and the near-dependent numerical example.

Line 1: import numpy as np
L01 / 24Name the input columns
Predict

Why do a1 and a2 each have three entries?

Before

Two independent directions in R3\mathbb R^{3}

A has two columns with three entries each. Reduced Q will also have shape (3,2), while R has shape (2,2).

A

After
?

Reveal to compare.

Connect the steps

Why Gram–Schmidt produces QR

01 Subtracting old components creates a new perpendicular direction

Set wj=aj−∑i<j(qiTaj)qiw_{j} = a_{j} - \sum_{i<j}(q_{i}^{\mathsf T}a_{j})q_{i}. Pair it with any earlier qkq_{k}. Orthonormality cancels exactly the matching term, leaving qkTwj=0q_{k}^{\mathsf T}w_{j} = 0. If wjw_{j} were zero, aja_{j} would already be in the preceding span, contradicting independence.

Dividing by ∥wj∥2\Vert w_{j}\Vert _{2} gives a unit vector. The same argument applies to the first column with an empty sum: a zero first column must be caught before normalization.

aj=∑i<jrijqi+rjjqja_j=\sum_{i<j}r_{ij}q_i+r_{jj}q_j

02 The nested spans make R upper triangular

Each qjq_{j} is formed using a1,…,aja_{1},\ldots ,a_{j}, and each aja_{j} can be reconstructed using q1,…,qjq_{1},\ldots ,q_{j}. Therefore the two prefixes have the same span. No later direction is needed to reconstruct an earlier column.

Collect these reconstruction coefficients as column j of R. All entries below the diagonal are zero. The nonzero diagonal makes R invertible under the independent-column assumption.

span⁡{a1,…,aj}=span⁡{q1,…,qj};A=QR\operatorname{span}\{a_1,\ldots,a_j\}=\operatorname{span}\{q_1,\ldots,q_j\};\quad A=QR

03 Follow the lecture’s second column exactly

The first normalized column is q1=(−1,0,1)T/2q_{1} = (-1,0,1)^{\mathsf T}/\sqrt{2}. The second coefficient is r12=q1Ta2=−1/2r_{12} = q_{1}^{\mathsf T}a_{2} = -1/\sqrt{2}. Subtracting r12q1r_{12}q_{1} from a2=(1,1,0)Ta_{2} = (1,1,0)^{\mathsf T} leaves w2=(1/2,1,1/2)Tw_{2} = (1/2,1,1/2)^{\mathsf T}.

Its length is 6/2\sqrt{6}/2, so q2=(1,2,1)T/6q_{2} = (1,2,1)^{\mathsf T}/\sqrt{6}. The negative coefficient is allowed: the component of a2a_{2} along q1q_{1} points in the opposite direction.

a2=−12q1+62q2a_2=-\frac1{\sqrt2}q_1+\frac{\sqrt6}2q_2

04 Orthonormal coordinates simplify projection and fitting

Because QTQ=I2Q^{\mathsf T}Q = I_{2}, the coordinates of the projection of b onto Col(A) are QTbQ^{\mathsf T}b. The fitted vector is QQTbQQ^{\mathsf T}b. If Aθ^A\hat{\theta} equals that fitted vector, then QRθ^=QQTbQR\hat{\theta} = QQ^{\mathsf T}b, so multiplying by QTQ^{\mathsf T} gives Rθ^=QTbR\hat{\theta} = Q^{\mathsf T}b.

For b=(1,1,1)Tb = (1,1,1)^{\mathsf T}, this produces θ^=(2/3,4/3)T\hat{\theta} = (2/3,4/3)^{\mathsf T}, exactly the 4.4 example. QTbQ^{\mathsf T}b are basis coordinates in q1,q2;θ^q_{1},q_{2}; \hat{\theta} are coefficients of the original columns a1,a2a_{1},a_{2}. They are not the same vector of numbers.

QTb=(04/6);Rθ^=QTb;b^=QQTbQ^{\mathsf T}b=\begin{pmatrix}0\\4/\sqrt6\end{pmatrix};\quad R\hat\theta=Q^{\mathsf T}b;\quad \hat b=QQ^{\mathsf T}b

05 Factorization error and orthogonality error test different things

A small ∥A−QR∥\Vert A - QR\Vert says the factors reconstruct the input. It does not guarantee a small ∥QTQ−I∥\Vert Q^{\mathsf T}Q - I\Vert. Classical Gram–Schmidt can lose orthogonality for near-dependent columns through cancellation, even while reconstructing A accurately.

Modified Gram–Schmidt computes each projection from the updated remainder. It often improves numerical orthogonality, but severe conditioning can still require reorthogonalization. Use a library QR for practical factorization. The companion notebook compares both errors on the original ε=10−8\varepsilon = 10^{-8} example instead of relying on one allclose check.

06 Dependent columns change the full-column-rank algorithm, not the existence of QR

If a2=2a1a_{2} = 2a_{1}, its remainder after subtracting the q1q_{1} component is zero. The teaching routine stops instead of dividing by zero; there is no second independent direction in the column space.

QR still exists: a library may complete Q with an arbitrary perpendicular direction while making R singular. Then using all columns of that Q in QQTQQ^{\mathsf T} can project onto a larger space than Col(A). A rank-r basis QrQ_{r} gives the correct P=QrQrTP = Q_{r}Q_{r}^{\mathsf T}; lstsq handles the fit without requiring an invertible R.

Check your understanding

Must the lecture’s Q match NumPy’s Q entry for entry?

No. A column sign in Q can be reversed if the corresponding row of R is reversed. Check A≈QR,QTQ≈IA \approx QR, Q^{\mathsf T}Q \approx I, and the projection instead.

Why is R upper triangular?

Column aja_{j} is already in span⁡{q1,…,qj}\operatorname{span}\{q_{1},\ldots ,q_{j}\}. It has no component along any later qiq_{i}, so rij=0r_{ij} = 0 when i > j.

What fails when the first column is zero?

Its norm is zero, so even the first normalization is invalid. A guard only inside the later-column loop misses this case.

Does small A − QR imply QTQQ^{\mathsf T}Q is close to I?

No. Reconstruction and orthogonality are different properties. Near-dependent columns can expose a large orthogonality error even with a small reconstruction error.

Do dependent columns mean there is no QR factorization?

No. They mean R is singular and the full-column-rank algorithm cannot make every input column contribute a new direction. Any extra Q columns need not belong to Col(A).

Code references: NumPy: reduced and complete QR ↗

Continue in Python

Run the complete notebook in Colab

This maintained notebook uses the same notation and worked example as the web lab. Run all cells in order, then change the inputs. NumPy is required; the final plot also uses Matplotlib, available in Colab.