Generate "nice" random matrix
You can create random matrices until the eigenvalues do not have a Root
form:
While[
A = RandomInteger[{-1, 1}, {4, 4}];
!FreeQ[RootReduce @ Eigenvalues[A], _Root]
];
Eigenvalues[A]
JordanDecomposition[A][[2]]
{-2, I Sqrt[2], -I Sqrt[2], 0}
{{-2, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, -I Sqrt[2], 0}, {0, 0, 0, I Sqrt[2]}}
I'll present another method to generate "nice" random matrices. I make no claims on the distribution followed by the matrices from my procedure.
This method has three steps:
- use
RandomInteger[]
to generate the eigenvalues, and randomly convert clusters of identical eigenvalues into Jordan blocks - reduce a random integer matrix to Hermite normal form and take its unimodular factor
- multiply the generated matrices appropriately
An implementation might look like this:
BlockRandom[SeedRandom["jordan", Method -> "ExtendedCA"]; (* for reproducibility *)
With[{n = 4 (* matrix size *), erange = {-2, 2} (* range of eigenvalues *)},
vm = First[HermiteDecomposition[RandomInteger[{-1, 1}, {n, n}]]];
jm = SparseArray[Band[{1, 1}] ->
(If[Length[#] == 1, {#},
DiagonalMatrix[#] +
DiagonalMatrix[RandomInteger[1, Length[#] - 1],
1]] & /@
Split[Sort[RandomInteger[erange, n]]])];
mat = Inverse[vm].jm.vm]];
MatrixForm[mat]
$$\begin{pmatrix} 0 & 0 & -1 & 0 \\ 0 & 0 & 1 & 0 \\ 1 & 0 & -2 & 0 \\ -2 & 0 & -1 & 2 \\ \end{pmatrix}$$
Check the Jordan form:
MatrixForm /@ JordanDecomposition[mat]
$$\{\begin{pmatrix} 1 & 0 & 0 & 0 \\ -1 & 0 & 1 & 0 \\ 1 & -1 & 0 & 0 \\ 1 & 0 & 0 & 1 \\ \end{pmatrix}, \begin{pmatrix} -1 & 1 & 0 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 2 \\ \end{pmatrix}\}$$