Compute Jordan normal form of matrix in Python / NumPy

sighol picture sighol · Dec 1, 2013 · Viewed 9.2k times · Source

In MATLAB you can compute the Jordan normal form of a matrix by using the the function jordan.

It there an equivalent function available in NumPy and SciPy?

Answer

Warren Weckesser picture Warren Weckesser · Dec 4, 2013

The MATLAB jordan function is from the Symbolic Math Toolbox, so it does not seem unreasonable to get its Python replacement from the SymPy library. Specifically, the Matrix class has the method jordan_form. You can pass a numpy array as an argument when you create a sympy Matrix. For example, the following is from the wikipedia article on the Jordan normal form:

In [1]: import numpy as np

In [2]: from sympy import Matrix

In [3]: a = np.array([[5, 4, 2, 1], [0, 1, -1, -1], [-1, -1, 3, 0], [1, 1, -1, 2]])

In [4]: m = Matrix(a)

In [5]: m
Out[5]: 
Matrix([
[ 5,  4,  2,  1],
[ 0,  1, -1, -1],
[-1, -1,  3,  0],
[ 1,  1, -1,  2]])

In [6]: P, J = m.jordan_form()

In [7]: J
Out[7]: 
Matrix([
[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 4, 1],
[0, 0, 0, 4]])