def eigensystem(matrix):
"""
Given an array-like 'matrix', returns:
- An array of eigenvalues
- An array of eigenvectors
- A rotation matrix that rotates the eigenbasis
into the original basis
Example:
mat = [[1,2,3],[2,4,5],[3,5,6]]
evals, evecs, rot = eigensystem(mat)
evals
array([ 11.34481428+0.j, -0.51572947+0.j, 0.17091519+0.j]
evecs
array([[-0.32798528, -0.59100905, -0.73697623],
[-0.73697623, -0.32798528, 0.59100905],
[ 0.59100905, -0.73697623, 0.32798528]])
rot
array([[-0.32798528, -0.73697623, 0.59100905],
[-0.59100905, -0.32798528, -0.73697623],
[-0.73697623, 0.59100905, 0.32798528]]))
This allows you to translate between original and eigenbases:
If [v1, v2, v3] are the components of a vector in eigenbasis e1, e2, e3
Then:
rot.dot([v1,v2,v3]) = [vx,vy,vz]
Will give the components in the original basis x, y, z
If [wx, wy, wz] are the components of a vector in original basis z, y, z
Then:
inv(rot).dot([wx,wy,wz]) = [w1,w2,w3]
Will give the components in the eigenbasis e1, e2, e3
inv(rot).dot(mat).dot(rot)
array([[evals[0], 0, 0]
[0, evals[1], 0]
[0, 0, evals[2]]])
Note: For symmetric input 'matrix', inv(rot) == evecs
"""
evals, emat = eig(matrix)
return evals, np.transpose(emat), emat
评论列表
文章目录