Solution (source code)

= Solution

The <spectral theorem> for a real <symmetric matrix> gives real <eigenvalues> and an <orthonormal eigenbasis>. Compute the roots of $\det(A-\lambda I)=0$, find a basis for each <eigenspace>, and orthonormalize within any repeated-eigenvalue <eigenspace>. <Eigenvectors> belonging to distinct <eigenvalues> are already orthogonal: if $Av=\lambda v$ and $Aw=\mu w$, symmetry gives $(\lambda-\mu)v^Tw=0$.

Putting the normalized basis <vectors> in the columns of an <orthogonal matrix> $Q$ yields
$$
\boxed{Q^TAQ=\operatorname{diag}(\lambda_1,\ldots,\lambda_n),\qquad A=Q\operatorname{diag}(\lambda_1,\ldots,\lambda_n)Q^T.}
$$
For repeated <eigenvalues>, the choice of axes inside their <eigenspace> is not unique.