-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (77 loc) · 2.31 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
GAMMAS = (10, 2, 4 / 5)
INPUT_SIZE = 20
MAX_ITERATIONS = 10_000
MAX_ERROR = 1e-6
class NotConverging(Exception):
pass
def D(matrix):
return np.diag(np.diag(matrix))
def L(matrix):
return np.tril(matrix, -1)
def U(matrix):
return np.triu(matrix, 1)
def solve(A, b, Q, x0, max_iterations=MAX_ITERATIONS, max_error=MAX_ERROR):
Q_inv = np.linalg.inv(Q)
x = x0
for i in range(max_iterations):
x = Q_inv @ ((Q - A) @ x + b)
err = np.linalg.norm(A @ x - b) / np.linalg.norm(b)
if err < max_error:
return x, i
if err == np.Infinity:
raise NotConverging
raise NotConverging
def create_input(gamma, dim=INPUT_SIZE):
A = np.zeros((dim, dim))
for i in range(dim):
for j in range(dim):
if i == j:
A[i][j] = gamma
elif abs(i - j) == 1:
A[i][j] = -1
b = np.zeros(dim)
for i in range(dim):
if i == 0 or i == dim - 1:
b[i] = gamma - 1
else:
b[i] = gamma - 2
x0 = np.zeros(dim)
return A, b, x0
def spectral_radius(A, Q, dim=INPUT_SIZE):
Q_inv = np.linalg.inv(Q)
E = np.identity(dim)
W = E - (Q_inv @ A)
return np.max(np.abs(np.linalg.eigvals(W)))
def jacobi(input):
A, b, x0 = create_input(input)
Q = D(A)
ro = spectral_radius(A, Q)
should_converge = ro < 1
print(f"ro={ro}")
try:
x, iterations = solve(A, b, Q, x0)
assert should_converge == True
print(f"Jacobi converged in {iterations} iterations for gamma={input}")
except NotConverging:
assert should_converge == False
print(f"Jacobi did not converge for gamma={input}")
def gauss_seidel(input):
A, b, x0 = create_input(input)
Q = D(A) + L(A)
ro = spectral_radius(A, Q)
should_converge = ro < 1
print(f"ro={ro}")
try:
x, iterations = solve(A, b, Q, x0)
assert should_converge == True
print(f"Gauss-Seidel converged in {iterations} iterations for gamma={input}")
except NotConverging:
assert should_converge == False
print(f"Gauss-Seidel did not converge for gamma={input}")
def main():
for input in GAMMAS:
jacobi(input)
gauss_seidel(input)
if __name__ == "__main__":
main()