• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Dec 3rd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import random
3. from scipy import linalg
4.
5. n = 4
6. A = np.array([[3,2,9,5],
7.               [3,1,3,11],
8.               [3,7,4,8],
9.               [5,2,4,9]])
10. x1 = np.zeros(n)
11. x2 = np.zeros(n)
12. l1 = np.zeros(n)
13. l2 = np.zeros(n)
14. y = np.zeros(n)
15.
16. def otn(y, x):
17.     result = np.zeros(x.size)
18.     for i in range(x.size):
19.         if x[i] != 0:
20.             result[i] = y[i]/x[i]
21.     return result
22.
23. def usl(y, x):
24.     eps = 0.00001
25.     result = False
26.     for i in range(x.size):
27.         if abs(y[i]-x[i])<=eps:
28.             result = True
29.     return result
30.
31. for i in range(n):
32.     y[i] = 1
33.     l2[i] = 1
34. k = 1
35. x2 = y/np.linalg.norm(y)
36.
37. while True:
38.     x1 = x2
39.     l1 = l2
40.     y = A.dot(x1)
41.     x2 = y/np.linalg.norm(y)
42.     l2 = otn(y, x1)
43.     k += 1
44.     if usl(l1,l2):
45.         break
46. print("k =",k)
47. s= 0
48. for i in range(n):
49.     s += l2[i]
50. L = s / n
51. print(L)
52. print(linalg.eigvals(A))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top