Householder QR produces an orthogonal Q and an upper triangular R with A = Q R full
This proof shows: correctness, termination.
A∈Rm×n with m≥n≥1. Each loop has a fixed length, so the algorithm stops after n steps.
The reflection
Lemma 1. For v=0 and β=2/(vTv), the matrix H=I−βvvT is symmetric and orthogonal.
Proof. HT=H is clear. HTH=H2=I−2βvvT+β2v(vTv)vT=I−2βvvT+2βvvT=I, using βvTv=2. □
Lemma 2. Let a∈Rp be nonzero, α=−sign(a1)∥a∥2 with sign(0)=1, and v=a−αe1. Then v=0 and Ha=αe1.
Proof. v1=a1−α=a1+sign(a1)∥a∥2, and both terms have the same sign with ∥a∥2>0, so ∣v1∣≥∥a∥2>0. Next, vTv=∥a∥22−2αa1+α2=2(∥a∥22−αa1) because α2=∥a∥22, and vTa=∥a∥22−αa1. So βvTa=1 and Ha=a−v=αe1. □
The algorithm
Invariant. Before step k the array holds A(k)=Hk−1⋯H1A, where Hj=diag(Ij−1,H~j) and H~j is the reflection of step j (the identity if the active column was zero). The first k−1 columns of A(k) are zero below the diagonal.
Proof by induction on k. For k=1 there is nothing to show. At step k, if ak:m,k(k)=0, then Hk=I and the claim for k+1 holds since column k is already zero below the diagonal. Otherwise lines L2 and L3 build v and β as in Lemma 2 for a=ak:m,k(k), and line L5 records the result αe1 of Lemma 2 in column k. Line L4 applies H~k to rows k,…,m of the columns j>k: ak:m,j−βv(vTak:m,j)=H~kak:m,j. Rows 1,…,k−1 are not touched, which is the action of Hk=diag(Ik−1,H~k). Columns 1,…,k−1 are zero in rows k,…,m, so Hk leaves them unchanged and they stay zero below the diagonal. So the array is HkA(k)=A(k+1), with columns 1,…,k zero below the diagonal. □
Theorem. After step n, R=Hn⋯H1A is upper triangular and A=QR with Q=H1H2⋯Hn orthogonal.
Proof. By the invariant with k=n+1, all n columns are zero below the diagonal. Each Hk is orthogonal and symmetric by Lemma 1, so Q=H1⋯Hn is orthogonal and QT=Hn⋯H1. Then QTA=R, and A=QR. ■
Least squares. If A has full column rank, then R1 (the top n×n block) is nonsingular, because R1TR1=RTR=ATQQTA=ATA is positive definite. For each x, ∥b−Ax∥2=∥QTb−Rx∥2 because QT preserves the 2-norm, and with QTb=(c1,c2) this equals (∥c1−R1x∥22+∥c2∥22)1/2. It is minimized exactly when R1x=c1. If some rkk=0 then det(ATA)=∏rkk2=0, so A is rank deficient; the implementation reports this case.
Floating point
Higham (Section 19.3) proves that the computed R^ satisfies A+ΔA=QR^ for the exactly orthogonal Q=H1⋯Hn built from the exact reflectors of the computed columns, with ∥Δaj∥2≤γ~mn∥aj∥2. The idea is that each computed application fl(H~ky) equals H~k(y+Δy) with ∥Δy∥2≤γ~m∥y∥2, because the computed v^ is close to the exact v relative to its norm (thanks to the sign choice of line L2), and an orthogonal map does not change the size of a perturbation. Accumulating n such steps, each of relative size γ~m in the 2-norm of the column, gives γ~mn. The tests measure ∥A−Q^R^∥F/∥A∥F and ∥Q^TQ^−I∥F exactly and check them against 4mnu.