Spectral Decomposition of a Matrix

To find the spectral decomposition of a matrix, first diagonalize if possible, then write the diagonal form as  
\[D= \sum_i \lambda_i E_i\]
  where  
\[E_i\]
  is the matrix with 1s on the leading diagonal in columns corresponding to the ith eigenvector of the matrix of eigenvectors,  
\[P\]
.
Example:  
\[A= \left( \begin{array}{ccc} 4 & 2 & 2 \\ 2 & 4 & 2 \\ 2 & 2 & 4 \end{array} \right) \]
.
The eigenvalues of  
\[A\]
  are the solutions to  
\[det(A- \lambda I)=0\]
.
\[\begin{equation} \begin{aligned} det(A- \lambda I) &=det(\left( \begin{array}{ccc} 4 & 2 & 2 \\ 2 & 4 & 2 \\ 2 & 2 & 4 \end{array} \right) - \lambda \left( \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right) ) \\ &= \left| \begin{array}{ccc} 4- \lambda & 2 & 2 \\ 2 & 4- \lambda & 2 \\ 2 & 2 & 4- \lambda \end{array} \right| \\ &= (4 -\lambda)((4- \lambda)^2-2^2)-2(2(4-\lambda) -2^2)+2(2(4-\lambda)-2^2) \\ &=(2- \lambda)^2(8- \lambda) = 0 \end{aligned} \end{equation}\]
.
Hence  
\[\lambda_1=2, \: \lambda_2 =8\]
Now find the eigenvectors. Let  
\[\mathbf{v}=\begin{pmatrix}x_1\\x_2\\x_3\end{pmatrix}\]

\[\lambda_1=2\]

\[(A-2I) \mathbf{v}= \left( \begin{array}{ccc} 2 & 2 & 2 \\ 2 & 2 & 2 \\ 2 & 2 & 2 \end{array} \right)\begin{pmatrix}x_1\\x_2\\x_3\end{pmatrix}= \begin{pmatrix}2x_1+2x_2+2x_3\\2x_1+2x_2+2x_3\\2x_1+2x_2+2x_3\end{pmatrix} = \begin{pmatrix}0\\0\\0\end{pmatrix}\]

Set  
\[x_2=1, \: x_3=0\]
  then  
\[x_1=-1\]
. An eigenvector is  
\[\begin{pmatrix}-1\\1\\0\end{pmatrix}\]

Set  
\[x_2=0, \: x_3=1\]
  then  
\[x_1=-1\]
. An eigenvector is  
\[\begin{pmatrix}-1\\0\\1\end{pmatrix}\]

\[\lambda_2=8\]

\[(A-2I) \mathbf{v}= \left( \begin{array}{ccc} -4 & 2 & 2 \\ 2 & -4 & 2 \\ 2 & 2 & -4 \end{array} \right)\begin{pmatrix}x_1\\x_2\\x_3\end{pmatrix}= \begin{pmatrix}-4x_1+2x_2+2x_3\\2x_1-4x_2+2x_3\\2x_1+2x_2-4x_3\end{pmatrix} = \begin{pmatrix}0\\0\\0\end{pmatrix}\]

A solution is  
\[x_1=x_2=x_3=1\]
  then the last eigenvector is  
\[\begin{pmatrix}1\\1\\1\end{pmatrix}\]

The matrix of eigenvectors is  
\[ \left| \begin{array}{ccc} -1 & -1 & 1 \\ 1 & 1 & 1 \\ 0 & w_2 & 1 \end{array} \right| \]

Eigenvectors corresponding to  
\[\lambda_1 =2\]
  occupy the first two columns and the eigenvector corresponding to  
\[\lambda_2 =8\]
  occupies the third column.
Then  
\[D=2E_1 +8E_2\]
  where  
\[E_1 = \left( \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 0 \end{array} \right), \: E_2 = \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{array} \right)\]

Then  
\[A=P(2E_1 +8E_2)P^{-1}=2 PE_1 P^{-1}+8 PE_2P^{-1}\]