# Generate Matrices with Whole Eigenvalues

I am trying to write a program to generate 500 distinct matrices. I'd like to figure out a way to generate matrices with whole eigenvalues.

Currently, I'm using rand to compute the values of my matrix. However, this approach does not make for integer eigenvalues. Instead, the matrices' eigenvalues are all complex floats.

Is there a different approach I could use to build matrices that have whole eigenvalues?

• Why not start with the eigenvalues to build the matrices? If you write down the characteristic equation, you can deduce what the matrix might look like – Shuri2060 Jul 11 '17 at 21:16
• I think you mean "distinct" as opposed to "unique" – Will Jagy Jul 11 '17 at 21:22
• If there are no constraints on the matrices generate a diagonal matrix, with integer values and conjugate it by orthogonal matrices. – Jonathan Davidson Jul 11 '17 at 21:24
• No need to use orthogonal matrices to conjugate it by, you can use any nonsingular matrices. – Robert Israel Jul 11 '17 at 21:26

Approach 1, works if the size of the matrix is $\geq 6$. Step 1. Generate a diagonal matrix $\geq 6$ with all eigenvalues distinct. Step 2. Pick 500 distinct permutations on the rows of that matrix. (There are a total 6! = 720 permutations, so you are guaranteed to have 500 distinct ones.)

If the size of the matrix is smaller (say, 4), repeat steps 1-2 above for two or more different sets of 4 distinct eigenvalues.

Approach 2 (less cheap, but more versatile and also generates more interesting matrices):

Do two similar matrices count as distinct? [https://en.wikipedia.org/wiki/Matrix_similarity] If so, you can proceed as follows:

1. Generate a diagonal matrix $D$ with the desired (integer) eigenvalues.
2. Generate an invertible matrix $A$ of the same size as $D$.
3. Record the matrix $A^{-1} D A$.

Repeat steps 1-3 or (if you want to keep the same eigenvalues) just the steps 2-3, making sure no two $A$'s repeat.

• To extend your answer we can use also upper triangular matrices with eigenvalues on the main diagonal. For six eigenvalues we have additional $5 \times 6$ degrees of freedom which can be chosen in absolutely free way. These matrices can be also transformed further by similarity. math.stackexchange.com/questions/803313/… – Widawensen Jul 12 '17 at 10:32
• @Widawensen, yes, absolutely. Thanks (also on behalf of the OP:) ! – avs Jul 12 '17 at 16:58
• @avs, only problem is when the inverse of A has floats :/ have an idea for how I could control that? – maddie Jul 13 '17 at 0:57
• @avs, I guess I could just repeat step 2 until the determinant of A is +/- 1 math.stackexchange.com/questions/19528/… – maddie Jul 13 '17 at 1:02
• @avs To say truth in the example given by me I forget $5 \times 6$ to divide by 2 so the free entries in an upper triangular matrix of dimension $n$ are $n(n-1)/2$ – Widawensen Jul 13 '17 at 16:13