<s>[INST] 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.
 [/INST] >>> 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$.
</s>