How can I find all the matrices that commute with this matrix? I would like to find all the matrices that commute with the following matrix
$$A = \begin{pmatrix}2&0&0\\ \:0&2&0\\ \:0&0&3\end{pmatrix}$$
I set $AX = XA$, but still can't find the solutions from the equations.
 A: >>> from sympy import *
>>> A = diag(2,2,3)
>>> X = MatrixSymbol('X',3,3)
>>> Matrix(A*X - X*A)
Matrix([
[      0,       0, -X[0, 2]],
[      0,       0, -X[1, 2]],
[X[2, 0], X[2, 1],        0]])

If $\rm A X = X A$, then $x_{13} = x_{23} = x_{31} = x_{32} = 0$. The other five entries are unconstrained.
We can also vectorize $\rm A X = X A$, which yields the following homogeneous linear system
$$\left( (\mathrm I_3 \otimes \mathrm A) - (\mathrm A \otimes \mathrm I_3) \right) \mbox{vec} (\mathrm X) = 0_9$$ 
or,
$$\begin{bmatrix}0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\end{bmatrix} \mbox{vec} (\mathrm X) = \begin{bmatrix} 0\\0\\0\\0\\0\\0\\0\\0\\0\end{bmatrix}$$
Again, we conclude that $x_{13} = x_{23} = x_{31} = x_{32} = 0$.
A: Block matrices provide an immediate insight. Let
$$A = \left[\begin{array}{l}2&0&0\\0&2&0\\0&0&3\end{array}\right] = \left[\begin{array}{l|l}a&0\\\hline0&3\end{array}\right]$$
The submatrix $a = 2I_2$. Now define the block matrix 
$$X = \left[\begin{array}{l|l}b&0\\\hline0&c\end{array}\right]$$ with conformal block sizes. That is, $c=constant$ and 
$$b = \left[\begin{array}{l}b_{11}&b_{12}\\b_{21}&b_{22}\end{array}\right] $$
has arbitrary complex elements.
The equation to solve is
$$[A,X] = AX - XA = \left[\begin{array}{l|l}a&0\\\hline0&3\end{array}\right] \left[\begin{array}{l|l}b&0\\\hline0&c\end{array}\right] - \left[\begin{array}{l|l}b&0\\\hline0&c\end{array}\right] \left[\begin{array}{l|l}a&0\\\hline0&3\end{array}\right] =  \left[\begin{array}{l|l}0&0\\\hline0&0\end{array}\right]$$
We have two equations:
$$ b a =  a b$$
$$ 3c = 3c$$
The second equation is trivial: $c$ is arbitrary. The first equation is just
$$ 2bI_{2} = 2 I_{2} b$$
Since the identity matrix commutes with every matrix, the $b$ matrix is arbitrary.
To conclude, the solution matrix has five arbitrary complex numbers arranged so:
$$X = \left[\begin{array}{ll|l}b_{11}&b_{12}&0\\b_{21}&b_{22}&0\\\hline0&0&c \end{array}\right] $$
One sees the benefit of this form in analyzing matrices of the form
$$A = \left[\begin{array}{l}c_{i} I_{i}&0&0 & 0\\0&c_{j}I_{j}&0 & 0\\0&0&c_{k}I_{k} & 0 \\ 0 & 0 & 0 & \ddots\end{array}\right]$$
A: $AX$ doubles the first two rows of $X$, and triples the third row.
$XA$ doubles the first two columns of $X$, and triples the third column.  These two must agree.
This gives us a matrix $$X=\left(\begin{matrix}*& * & 0\\*&*&0\\0&0&*\end{matrix}\right)$$
Where $*$ can be anything.
A: If
$B:= \left( \begin{matrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right)$, 
then your matrix becomes $A = 2I + B$. Thus a matrix $C$ will commute with $A$ if and only if $C$ commutes with $B$. But note
$BC = \left( \begin{matrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right)\left( \begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix} \right) = \left( \begin{matrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ g & h & i \end{matrix} \right)$
and 
$CB = \left( \begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix} \right) \left( \begin{matrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right) = \left( \begin{matrix} 0 & 0 & c \\ 0 & 0 & f \\ 0 & 0 & i \end{matrix} \right)$.
It follows that $BC = CB$ if and only if $c=f = g = h = 0$
