Solution (source code)

= Solution

For independent columns $a_1,\ldots,a_k$ in a real <inner product space>, the <Gram-Schmidt process> recursively forms
$$
u_j=a_j-\sum_{i<j}\langle q_i,a_j\rangle q_i,\qquad q_j=\frac{u_j}{\|u_j\|}.
$$
Each $u_j$ subtracts the <orthogonal projections> onto earlier normalized vectors. Their independence ensures $u_j\ne0$. The resulting $q_i$ are <orthonormal> and preserve the span of every initial column set. Putting $Q=(q_1\ \cdots\ q_k)$ and $r_{ij}=\langle q_i,a_j\rangle$ for $i\leq j$, with zeros below the diagonal, gives a <QR decomposition> $A=QR$ with $Q^TQ=I$. Dependent input columns require omitting a zero residual or treating rank deficiency separately.