<s>[INST] Finding an invertible matrix. 
I want to find an invertible matrix $P$ where $P^tAP$ is a diagonal matrix.
$$A=\begin{pmatrix} 1 & 2 & 1 \\ 2 & 0 & 2 \\ 1 & 2 & 1 \end{pmatrix} $$

I have calculated the eigenvalues of $A$: $0,-2,4$ so the diagonal of $A$ should be $$D=\begin{pmatrix} -2 & 0 & 0 \\ 0 & 4 & 0 \\ 0 & 0 & 0 \end{pmatrix} $$
How can I calculate a $P$ which verifies $D=P^tAP$?
I thought I could find $P$ doing
$$P=\begin{pmatrix} a & b & c \\ d & e & f \\ g & h&i \end{pmatrix}$$
and
multiplying $P$ and $P^t$ with $A$ but it is a long process and I am pretty sure there should be a better method to find $P$.
 [/INST] The characteristic polynomial of $A$ is $q(x)=x^3-2x^2-8x$, hence the spectrum is $\{-2,0,4\}$.
The nullspace of $A+2I$ is generated by $(1,-2,1)$, the nullspace of $A$ is generated by $(1,0,-1)$ and the nullspace of $A-4I$ is generated by $(1,1,1)$, hence with your $D$
$$ P = \begin{pmatrix}\frac{1}{\sqrt{6}} & \frac{1}{\sqrt{3}}& \frac{1}{\sqrt{2}} \\ -\frac{2}{\sqrt{6}} & \frac{1}{\sqrt{3}}& 0 \\ \frac{1}{\sqrt{6}} &\frac{1}{\sqrt{3}} &-\frac{1}{\sqrt{2}} \end{pmatrix}$$
and $P^{-1}=P^T$.
</s>