Decomposing a matrix with polynomial elements into a polynomial with matrix coefficients
Here is a very simple way to do it:
Table[1/i! D[M, {a, i}] /. a -> 0, {i, 0, 3}]
(*
==> {{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}, {{0, 0}, {0, 0}}}
*)
This works even if the entries are not polynomials. If they are, you can replace the arbitrary maximum 3
in the Table
index by the degree of the polynomial:
Max[Exponent[M, a]]
Edit
Looking at the other solutions, this solution (due to its fundamental simplicity) is the only one that works without modification for arbitrary rank tensors and simultaneously for arbitrary functions that can be expanded in the variable a
, be they polynomial or not.
Here's one quick way for polynomial matrices:
polyMat = {{15 + a^2, a + 5 a^2}, {a - 5 a^2, 2}};
Transpose[PadRight[CoefficientList[polyMat, a]], {2, 3, 1}]
{{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}}
Alternatively (as Jens hints), you can do Flatten[PadRight[CoefficientList[polyMat, a]], {3}]
.
You can check that the matrix polynomial is reproduced with Fold[(#1 a + #2) &, 0, Reverse @ %]
.
First, if you don't know the degree you can compute it:
m = {{15 + a^5, a + 5 a^2}, {a - 5 a^2, 2 c}};
degree = Max[Flatten[Exponent[#, {a}] & /@ Flatten[m]]]
(I have increased the degree of m
to verify that intermediate zero matrices are correctly output.)
And now, with degree
in hand, why not use a function intended for this task?
Coefficient[m, a, #] & /@ Range[0, degree]
It works for tensors of any rank, including scalars.