-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOR.py
65 lines (53 loc) · 1.52 KB
/
SOR.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
import time
import numpy as np
def SOR(A, b, omg, tol=1e-6):
"""
Implementation of successive over-relaxation method for solve A @ x = b;
A must be:
1) symmetric (i.e. A.T = A)
2) positive-definite (i.e. x.T@A@x > 0 for all non-zero x)
3) real.
Parameters
----------
A : 2darray
matrix of system.
b : 1darray
Ordinate or dependent variable values.
tol : float, optional
required tollerance default 1e-6
Return
------
x : 1darray
solution of system
iter : int
number of iteration
"""
x = np.zeros(len(b))
iter = 0
while True:
x_new = np.zeros(len(x))
for i in range(A.shape[0]):
s1 = np.dot(A[i, :i], x_new[:i])
s2 = np.dot(A[i, i + 1:], x[i + 1:])
x_new[i] = (1 - omg)*x[i] + omg*(b[i] - s1 - s2) / A[i, i]
res = np.sqrt(np.sum((A @ x_new - b)**2))
if res < tol:
break
x = x_new
iter += 1
return x, iter
if __name__ == "__main__":
np.random.seed(69420)
N = 20
A = np.random.normal(size=[N, N])
A = A.T @ A #per garantire la convergenza del metodo
b = np.random.normal(size=[N])
start = time.time()
x1, iter = SOR(A, b, 1.9, 1e-8)
print(f"number of iteration = {iter}")
print(f"Elapsed time = {time.time()-start}")
start = time.time()
x2 = np.linalg.solve(A, b)
print(f"Elapsed time = {time.time()-start}")
d = np.sqrt(np.sum((x1 - x2)**2))
print(f'difference with numpy = {d}')