# Create a random 2×2 matrix with a repeated eigenvalue and single eigenvector

Does anyone have a good technique for creating a random $2\times 2$ matrix that has one eigenvalue of multiplicity two, but only a single eigenvector?

And more generally, has anyone written a function where one can feed the function the eigenvalues (real or complex) with a given algebraic multiplicity and geometric multiplicity for each eigenvalue to create a $n\times n$ random matrix?

Any square matrix can be put into Jordan normal form via a similarity transformation; and for a matrix in Jordan normal form, the geometric and algebraic multiplicities of the eigenvalues are determined by its block structure. So you could take the following approach: generate a Jordan matrix with the appropriate multiplicities, and then perform a random similarity transformation on it. This will give you a "random" matrix with the appropriate eigenvectors and eigenvalues, in some appropriate sense.

Here's a $2\times2$ example, with a double algebraic eigenvalue and one geometric eigenvector:

eigval = RandomReal[];
Jordanmat = {{eigval, 1}, {0, eigval}}
simmat = {{0}};
While[Det[simmat] == 0,
simmat = RandomReal[1, {2, 2}]
];
randmat = Inverse[simmat].Jordanmat.simmat
Chop[Eigenvalues[randmat], 10^-7]
Chop[Eigenvectors[randmat], 10^-7]

(* {{4.35177, 5.59407}, {-3.36891, -4.33061}} *)
(* {0.0105807, 0.0105806} *)
(* {{0.790019, -0.613082}, {-0.790019, 0.613082}} *)


(ETA: The While loop in there is to avoid the admittedly remote possibility that you randomly choose a similarity matrix with determinant zero. It could probably be omitted and you'd never notice the difference.)

Note, though, that the distribution the entries will have will almost certainly be more complicated than the distribution from which you draw the entries of the eigenvalues and the similarity matrix. This is something to be aware of if your application depends on the distribution of your matrix entries.

• Slightly shorter: eigval = RandomReal[]; Jordanmat = {{eigval, 1}, {0, eigval}}; simmat = NestWhile[RandomReal[1, {2, 2}] &, 0, Det[#] == 0 &, {2, 1}]; randmat = LinearSolve[simmat, Jordanmat.simmat]. To produce block diagonal matrices with appropriate Jordan blocks, something like ArrayFlatten[SparseArray[Band[{1, 1}] -> {({{2, 1}, {0, 2}}), {{3}}, ({{1, 1}, {0, 1}}), {{-1}}}]] is useful. May 2 '15 at 2:44

A $2 \times 2$ matrix has at most two eigenvectors, and just one eigenvector if the rows are related by a factor:

myEigenVec = {1, 3};
myMatrixMaker[vec_List, n_] :=
Times @@ Eigensystem[Transpose[{myEigenVec, n myEigenVec}]]

myMatrixMaker[myEigenVec, 2]


(* {{7, 21}, {0, 0}} *)

where the {0,0} eigenvector is degenerate.