# Matrix Decomposition and Collect

I am looking to decompose a matrix in terms of a sum of matrices with the same coefficients, i want something in this form :

$$m1= \begin{pmatrix} a^2 + 2a + 3 & 1 \\ 2a & a^2 \end{pmatrix} =a^2\begin{pmatrix} 1& 0\\ 0& 1 \end{pmatrix} +a\begin{pmatrix} 2& 0\\ 2 & 0 \end{pmatrix}+\begin{pmatrix} 3& 1\\ 0& 0 \end{pmatrix}$$

I tried to use Collect[m1,a] but it collects the terms inside the matrix instead of giving a sum of matrices.

• Please, add always copyable code to your posts. – Henrik Schumacher Apr 24 '19 at 16:26

Here is a possible approach:

decompose[m_,a_]:=With[{max = Max @ Exponent[m, a]},
terms = SeriesCoefficient[m, {a, 0, #}]& /@ Range[0,max];
a^Range[0,max] . MatrixForm/@terms
]


Then:

    decompose[{{3+2 a+a^2,1},{2 a,a^2}}, a] //TeXForm


$$a^2 \left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right)+a \left( \begin{array}{cc} 2 & 0 \\ 2 & 0 \\ \end{array} \right)+\left( \begin{array}{cc} 3 & 1 \\ 0 & 0 \\ \end{array} \right)$$

• Very nice (+1). This could be useful in a number of linear algebra environments. – David G. Stork Apr 24 '19 at 18:32