Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to practice the algorithm for the transformation from a matrix to its jordan normal form (with change of basis).

To do so, I wrote this script that generates random $n \times n$ matrices, with $n \in \{2,3,4,5\}$

import random, numpy

n = random.randint(2,5)

matrix = []

for i in xrange(n):
    line = []
    for j in xrange(n):
        line.append(random.randint(-10, 10))

A = numpy.matrix(matrix)

print("Here is your %i x %i Matrix" % (n, n))

This way of generating random matrices isn't very good for the following reasons:

  • The numbers can get really ugly (example)
  • Sometimes it is not possible to calculate the decomposition of the matrix (example)

Do you know either pages with many examples of "good" matrices up to $5 \times 5$ or do you know how to change my script?

share|cite|improve this question
I don't know how you'd do it in numpy, but one approach would be to build up a matrix with known Jordan block structure and then perform a similarity transformation with an orthogonal or unimodular matrix. – J. M. Aug 8 '12 at 12:02
up vote 4 down vote accepted

You could generate a Jordan normal form $J$ randomly, then an invertible matrix $X$ randomly and have the algorithm compute $Y=XJX^{-1}$ for you. $Y$ would be your exercise.

share|cite|improve this answer
This has the added advantage that you know the right answer - it generates problems and an answer sheet. The disadvantage is that $Y$ might not be an integer matrix, even if $X$ and $J$ are. – Thomas Andrews Aug 8 '12 at 13:06
If you manage to ensure $X$ is integral and its inverse is integral, then your $Y$ will be integral as well. – rschwieb Aug 8 '12 at 13:10
Sure, but generating such an $X$ randomly seems non-trivial. – Thomas Andrews Aug 8 '12 at 13:14
@ThomasAndrews I'm not sure how one is supposed to respond to such speculation, but I'll just say the comment was a statement of fact directed at the OP, and not a comment direct at your comment, as you seem to have interpreted it. I'll direct a counterspeculation at your comment though and say that generating an invertible unimodular matrix sounds like something that would be really well-documented online. – rschwieb Aug 8 '12 at 13:30
He is looking for a practical way to hack together some examples, not advanced algorithmic way of selecting unimodular matrices. (It might suffice to take $X$ to be a random upper-triangular matrix with $1$s along the diagonal to give him a diverse enough example set, for example.) – Thomas Andrews Aug 8 '12 at 13:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.