Solution (source code)

= Solution

A <QR factorization> of a real $n\times n$ <matrix> is
$$
\boxed{A=QR,\qquad Q^TQ=I,\qquad R\text{ upper triangular}.}
$$
Thus $Q$ is an <orthogonal matrix> and $R$ is an <upper triangular matrix>. It exists even if $A$ is singular; requiring positive diagonal entries of $R$ is a convenient uniqueness convention for nonsingular $A$, rather than part of the general existence statement. Over the complex numbers, replace <orthogonal> by <unitary> and transpose by <conjugate transpose>.