-
Notifications
You must be signed in to change notification settings - Fork 0
/
npeigen.py
66 lines (60 loc) · 2.77 KB
/
npeigen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
# 定义矩阵数据
# h_mat_data = """
# 1 6 3 4 3 0 2 7
# 6 2 7 5 6 5 2 1
# 3 7 3 3 2 2 7 4
# 4 5 3 4 0 3 0 2
# 3 6 2 0 5 7 2 6
# 0 5 2 3 7 6 5 4
# 2 2 7 0 2 5 7 1
# 7 1 4 2 6 4 1 8
# """
h_mat_data = """
1 1 2
1 2 2
2 2 3
"""
# h_mat_data = """
# 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 1 2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 2 2 3 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 3 3 3 4 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 4 4 4 4 5 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 5 5 5 5 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 6 6 6 6 6 6 7 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 7 7 7 7 7 7 7 8 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 8 8 8 8 8 8 8 8 9 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 9 9 9 9 9 9 9 9 9 10 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 10 10 10 10 10 10 10 10 10 10 11 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 11 11 11 11 11 11 11 11 11 11 11 12 12 13 14 15 16 17 18 19 20 21 22 23 24
# 12 12 12 12 12 12 12 12 12 12 12 12 13 13 14 15 16 17 18 19 20 21 22 23 24
# 13 13 13 13 13 13 13 13 13 13 13 13 13 14 14 15 16 17 18 19 20 21 22 23 24
# 14 14 14 14 14 14 14 14 14 14 14 14 14 14 15 15 16 17 18 19 20 21 22 23 24
# 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 16 17 18 19 20 21 22 23 24
# 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 17 18 19 20 21 22 23 24
# 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 18 18 19 20 21 22 23 24
# 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 19 20 21 22 23 24
# 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 20 21 22 23 24
# 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 21 22 23 24
# 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 22 23 24
# 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 23 23 24
# 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 24
# 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 25
# """
# 将矩阵数据字符串分割成行,然后转换成整数,并创建 NumPy 数组
h_mat = np.array([row.split() for row in h_mat_data.strip().split('\n')], dtype=int)
# 输出转换后的 NumPy 矩阵
print("转换后的 NumPy 矩阵:")
print(h_mat)
# 使用 numpy 的 eigh 函数计算特征值和特征向量
eigenvalues, eigenvectors = np.linalg.eig(h_mat) #np.linalg.eigh(h_mat)
# 输出特征值
print("特征值(升序排序):")
for eigenvalue in sorted(eigenvalues):
print(eigenvalue)
# 输出特征向量
print("\n特征向量(对应于升序的特征值):")
for i, eigenvector in enumerate(eigenvectors.T):
print(f"第 {i+1} 个特征向量:")
print(eigenvector)