# Computation of the minimal polynomial of a matrix in Mathematica

On the wolfram website, the following program is given for computing the minimal polynomial of a square matrix $a$ in the variable $x$:

MatrixMinimalPolynomial[a_List?MatrixQ,x_]:=Module[

{

i,
n=1,
qu={},
mnm={Flatten[IdentityMatrix[Length[a]]]}


},

While[Length[qu]==0,

AppendTo[mnm,Flatten[MatrixPower[a,n]]];
qu=NullSpace[Transpose[mnm]];
n++


];

First[qu].Table[x^i,{i,0,n-1}]

]

(It's given here: http://mathworld.wolfram.com/MatrixMinimalPolynomial.html also)

I'm having trouble following the algrothim it's using. How is Mathematica actually computing the matrix minimal polynomial?

-

While I can't quite grok the code, it looks like M. is building in increasingly long list of powers of $a$, and searching (by NullSpace) for a relation of linear dependency. Once one is found, the coefficients of the dependency relation are turned into a polynomial through the Table method.

-
Ooh ok. That's specific enough for me. Thanks! – Fortunato Jan 31 '13 at 16:55

This guide is for the user who is not familar with mathematica.

Step 1. Implement the following code.

  MatrixMinimalPolynomial[a_List?MatrixQ,x_]:=Module[
{
i,
n=1,
qu={},
mnm={Flatten[IdentityMatrix[Length[a]]]}
},
While[Length[qu]==0,
AppendTo[mnm,Flatten[MatrixPower[a,n]]];
qu=NullSpace[Transpose[mnm]];
n++
];
First[qu].Table[x^i,{i,0,n-1}]
]


Step 2. Declare the matrix $a$. For example, $a=\begin{pmatrix} 3 & -1 & 0\\ 0 & 2 & 0\\ 1 & -1 & 2 \end{pmatrix}$;

Step 3. Then implement

Factor[MatrixMinimalPolynomial[a, x]]

-