Householder QR example in two dimensions (source code)

= Householder QR example in two dimensions
{c}
{title2=$A=QR$}

Apply the preceding <Householder reflection> to the columns of
$$
A=\begin{pmatrix}3&1\\4&2\end{pmatrix}.
$$
The first column becomes $(-5,0)^T$ and the second becomes $(-11/5,2/5)^T$:
$$
R=HA=\begin{pmatrix}-5&-11/5\\0&2/5\end{pmatrix},
\qquad
A=HR.
$$
Thus an <orthogonal matrix> multiplying an <upper triangular matrix> reconstructs $A$. The zero below the diagonal came from a geometric operation that preserves <Euclidean norms> and <inner products>.

\Image[/householder-qr-column-elimination.png]
{title=A single Householder reflection makes a two-column matrix upper triangular}
{description=Both columns are reflected by the same <orthogonal transformation>. The first lands on the first coordinate axis; the second retains a nonzero second component. The intermediate triangular factor has a negative first diagonal entry.}

Our positive-diagonal convention for <QR decomposition> is obtained by a sign adjustment. With $D=\operatorname{diag}(-1,1)$, put
$$
Q=HD=\begin{pmatrix}3/5&-4/5\\4/5&3/5\end{pmatrix},
\qquad
R_+=DR=\begin{pmatrix}5&11/5\\0&2/5\end{pmatrix}.
$$
Then $Q^TQ=I$ and $\boxed{A=QR_+}$. The intermediate sign chooses an <orientation> for a <basis vector>; the adjustment makes the diagonal positive.

For a larger <matrix>, the next <Householder reflection> acts only on the lower rows of the remaining columns, retaining the zeros already created. Store its defining <vector> and apply $z\mapsto z-2u(u^Tz)$ instead of forming a dense reflection <matrix>. This links a simple mirror operation to efficient <QR decomposition>, including <least-squares solutions>.