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?
 A: 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.
A: 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]]

A: I know this is now five years old, but it still came up when I searched so I'll post incase others see.
One quick workaround is to just use the Jordan normal form. Simply Set your matrix A (using $A:=\{\{\},\ldots \{\}\}$), and then enter JordanDecomposition[A]. This returns two matrices $M,N$. The second is the matrix in Jordan normal form, and the first gives you the similarity $A=MNM^{-1}$.
The minimal polynomial has a factor of $(x-\lambda)^n$ for each $\lambda$ appearing on the diagonal, and $n$ is the size of the largest $\lambda$-Jordan block.
For example, the matrix
$$\begin{bmatrix}
-2 & 1 & 0 & 0 & 0 & 0\\
0 &-2 & 1 & 0 & 0 & 0\\
0 & 0 & -2 & 0 & 0 & 0\\
0 & 0 & 0 & 3 & 1 & 0\\
0 & 0 & 0 & 0 & 3 & 0\\
0 & 0 & 0 & 0 & 0 & 7
\end{bmatrix} $$
was the minimal polyonmial
$$(x+2)^3(x-3)^2(x-7).$$
It also has the characteristic polynomial $$(x+2)^3(x-3)^2(x-7).$$
