zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【线代&NumPy】 特征值分解

ampnumpy 分解 特征值
2023-09-14 09:15:59 时间

import numpy as np
 
# 打印A的函数
def pprint(msg, A):
    print("---", msg, "---")
    (n,m) = A.shape
    for i in range(0, n):
        line = ""
        for j in range(0, m):
            line += "{0:.2f}".format(A[i,j]) + "\t"
        print(line)
    print("")
    
print("特征值分解\n")
A = np.array([[3.0, 1.0], [2.0, 2.0]])
w, S = np.linalg.eig(A)
L = np.diag(w)
pprint("矩阵B ", A)
pprint("特征矩阵 L", L)
pprint("特征向量矩阵 S", S)
pprint("S*L*S^{-1}", np.matmul(np.matmul(S,L), np.linalg.inv(S)))
 
B = np.array([[1.0, 1.0, 0.0], [1.0, 3.0, 1.0], [2.0, -1.0, 1.0]])
w, S = np.linalg.eig(B)
L = np.diag(w)
pprint("矩阵B", B)
pprint("特征矩阵 L", L)
pprint("特征向量矩阵 S", S)
pprint("S*L*S^{-1}", np.matmul(np.matmul(S,L), np.linalg.inv(S)))

 🚩 运行结果:


特征值分解

--- 矩阵B  ---  
3.00    1.00    
2.00    2.00

--- 特征矩阵 L ---
4.00    0.00
0.00    1.00

--- 特征向量矩阵 S ---
0.71    -0.45
0.71    0.89

--- S*L*S^{-1} ---
3.00    1.00
2.00    2.00

--- 矩阵B ---
1.00    1.00    0.00
1.00    3.00    1.00
2.00    -1.00   1.00

--- 特征矩阵 L ---
3.36+0.00j      0.00+0.00j      0.00+0.00j
0.00+0.00j      0.82+0.90j      0.00+0.00j
0.00+0.00j      0.00+0.00j      0.82-0.90j

--- 特征向量矩阵 S ---
0.39+0.00j      -0.19+0.29j     -0.19-0.29j
0.92+0.00j      -0.22-0.22j     -0.22+0.22j
-0.06+0.00j     0.88+0.00j      0.88-0.00j      

--- S*L*S^{-1} ---
1.00-0.00j      1.00+0.00j      -0.00-0.00j
1.00+0.00j      3.00-0.00j      1.00-0.00j
2.00+0.00j      -1.00-0.00j     1.00-0.00j