先放代码,有时间补内容
复制代码
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
52import numpy as np def MaxFabs(v): m,n = v.shape assert n == 1 ans = 0 for i in range(m): if ans < np.fabs(v[i]): ans = v[i] maxn = i return ans def PowerMethod(a,v,g): m,n = a.shape assert m == n times = 0 u = v/MaxFabs(v) v = v.astype(float) # 设置x的精度 Lambda = 0 while True: tempv = v.copy() tempu = u.copy() # 记录上一次的迭代答案 tempL = Lambda v=np.dot(a,u) u=v/MaxFabs(v) Lambda=MaxFabs(v) times += 1 # 迭代次数加一 gap = abs(Lambda-tempL) # 与上一次答案模的差 if gap < g: # 精度满足要求,结束 break elif times > 10000: # 如果迭代超过10000次,结束 break print("10000次迭代仍不收敛") print(times) print(Lambda) print(id(Lambda),id(tempL)) if __name__ == '__main__': #当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。 a = np.array([[1,-1,2],[-2,0,5],[6,-3,6]]) v = np.array([[1],[1],[1]]) g=1e-6 PowerMethod(a,v,g)
最后
以上就是执着航空最近收集整理的关于python实现用改进的乘幂法求方阵的按模最大特征值和特征向量的全部内容,更多相关python实现用改进内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复