from sympy import *
from IPython.display import display
init_printing(use_latex='mathjax')
J=Matrix([[-3,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,2,0,0,0,0,0],
[0,0,0,4,1,0,0,0],
[0,0,0,0,4,0,0,0],
[0,0,0,0,0,4,1,0],
[0,0,0,0,0,0,4,1],
[0,0,0,0,0,0,0,4]])
H=Matrix([[1,2,3,4,5,6,7,8],
[0,1,2,3,4,5,6,7],
[0,0,1,2,3,4,5,6],
[0,0,0,1,2,3,4,5],
[0,0,0,0,1,2,3,4],
[0,0,0,0,0,1,2,3],
[0,0,0,0,0,0,1,2],
[1,0,0,0,0,0,0,1]])
Hi=H.inv()
A=H*J*Hi
display(A)
lmbda=var('lambda')
I=Matrix.eye(8)
B=(A-lmbda*I)
display(B)
d=B.det()
display(d)
fd=factor(d)
display(fd)
roots=solve(fd,lmbda)
display(roots)
#eigenvalues -3, 0, 2 each have algebraic multiplicity 1
#eigenvalues 4 has algebraic multiplicity 5
height = lambda M: len(M[:,0])
width = lambda M: len(M[0,:])
def kernel_basis(A):
'''Returns matrix whose column are a basis for the null space of A.'''
#1. n x n matrix identity matrix B.
#2. m x n matrix R = rref of A.
#3. Subtract each row i of R from row i of B.
#4. For each pivot column j of R, delete column j of B.
m=width(A)
B=eye(m)
tmp=Matrix(A)
R,pivots=tmp.rref()
cols=range(m)
for (p,e) in zip(pivots,range(len(pivots))):
for c in cols:
B[p,c]=B[p,c]-R[e,c]
for p in reversed(pivots):
B.col_del(p)
return(B)
V={}
for r in roots:
#find basis of kernel of A-r*I
V[r]=(A-r*I).nullspace()
display(V[r])
#eigenvalues -3, 0, 2 each have geometric multiplicity 1
#eigenvalues 4 has geometric multiplicity 2
(h,j)=A.jordan_form()
display([h,j])
#Since eigenvalue 4 has geometric multiplicity 2,
#the Jordan form will have 2 Jordan blocks
#for that eigenvector and 2 associated
#Jordan chains.
#A Jordan chain of length 3:
#A-4I transform the 6th column of h to the 5th column
#and transforms the 5th column to the 4th column.
#The associated Jordan block is 3x3
display((A-4*I)*h[:,6-1]==h[:,5-1])
display((A-4*I)*h[:,5-1]==h[:,4-1])
display(j[4-1:6,4-1:6])
#A Jordan chain of length 2:
#A-4I transform the 8th column of h to the 7th column
#The associated Jordan block is 2x2
display((A-4*I)*h[:,8-1]==h[:,7-1])
display(j[7-1:8,7-1:8])
#These two chain lengths sum to 5,
#the algebraic multiplicity of eigenvalue 4.
#The eigenvalues -3, 0, 2 have trivial
#1x1 Jordan blocks, [-3], [0], [2], respectively,
#and Jordan chains of length one, namely,
#columns 1, 2, and 3 of h, respectively.
Matrix([[1,5,0],[0,1,0],[0,0,1]]).jordan_form()