scientific-programming-exer.../ex_42.py

33 lines
574 B
Python
Raw Normal View History

2019-01-23 13:38:36 +00:00
import numpy as np
A = np.array(
[ [0, 1, 1, 0, 0, 0]
, [1, 0, 1, 0, 1, 0]
, [1, 1, 0, 1, 0, 0]
, [0, 0, 1, 0, 1, 0]
2019-01-23 14:37:46 +00:00
, [0, 1, 0, 1, 0, 1]
2019-01-23 13:38:36 +00:00
, [0, 0, 0, 0, 1, 0]
])
2019-01-23 15:21:46 +00:00
eigvalues, eigvectors = np.linalg.eigh(A)
print(eigvalues)
print(eigvectors)
2019-01-23 13:38:36 +00:00
l_max_i = eigvalues.argmax()
l_max = eigvalues[l_max_i]
2019-01-23 15:21:46 +00:00
print(l_max_i, l_max)
2019-01-23 13:38:36 +00:00
v_max = eigvectors[l_max_i]
def some_norm(M):
return np.abs(M).max()
2019-01-23 14:37:46 +00:00
B = A
2019-01-23 13:38:36 +00:00
for k in range(1, 21):
2019-01-23 14:37:46 +00:00
B = B.dot(A)
2019-01-23 15:21:46 +00:00
print("some kind of error for k =", k, ":", some_norm(B - l_max**k * np.outer(v_max, v_max)) / l_max**k)
2019-01-23 13:38:36 +00:00
2019-01-23 15:21:46 +00:00
print(v_max.argmax())