def spectrum_decomposition(A):
'''
compute the spectrum decomposition of matrix A
'''
# check if A is a simple matrix
if isSimple(A) != True:
raise Exception('non-simple matrix cannot be spectrum-decomposed')
eigenValues, eigenVectors = la.eig(A)
invVectors = la.inv(eigenVectors)
eigenVectors_row = eigenVectors.shape[0]
eigenVectors_column = eigenVectors.shape[1]
invVectors_row = invVectors.shape[0]
invVectors_column = invVectors.shape[1]
# an array of all distinct eigen values with their associated algebraic multiplicity
# keys: eigen values
# values: algebraic multiplicity
# {eigenValue: algebraicMulti}
dictValues = {}
while eigenValues.shape[0] != 0:
index = np.argwhere(abs(eigenValues - eigenValues[0]) < 0.00001)
spectrum = eigenVectors[:, index].reshape((eigenVectors_row, len(index))).dot(invVectors[index, :].reshape((len(index), invVectors_column)) )
dictValues.update({eigenValues[0]: spectrum})
eigenValues = np.delete(eigenValues, index)
eigenVectors = np.delete(eigenVectors, index, 1)
invVectors = np.delete(invVectors, index, 0)
return dictValues
评论列表
文章目录