-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseidel.py
35 lines (31 loc) · 1.03 KB
/
seidel.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
import utils
import numpy as np
import numpy.linalg as lg
def main():
m, f = [], []
with open('data/input.txt', mode='r') as file:
m = np.matrix(utils.parse(file))
f = np.matrix(utils.parse(file)).T
a, b, eps = np.matrix(np.copy(m)), np.matrix(np.copy(f)), 1.0e-5
x, x_1 = np.array(f), np.array([0. for i in range(len(m))])
norm = lg.norm(m, ord=np.inf) * lg.norm(f, ord=np.inf)
m /= norm
f /= norm
m = np.matrix(np.diag([1 for i in range(len(m))]) - m)
iteration = 0
while lg.norm(x - x_1, ord=np.inf) >= eps:
x_1 = np.copy(x)
for i in range(len(x)):
x[i] = sum([x[j] * m[i, j] for j in range(len(x))]) + f[i]
iteration += 1
r = np.array(np.dot(a, x) - b).T[0]
with open('summary/seidel.txt', mode='w+') as file:
file.write('x:')
[file.write('{:<15.5e}'.format(e)) for e in np.array(x).T[0]]
file.write('\nr:')
[file.write('{:<15.5e}'.format(e)) for e in r]
file.write('\n||r|| = {:e}'.format(lg.norm(r.T, ord=np.inf)))
file.write('\niteration: {:d}'.format(iteration))
pass
if __name__ == '__main__':
main()