-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelaxation.py
34 lines (30 loc) · 1012 Bytes
/
relaxation.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
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))])
m_t = m.T
m = np.dot(m_t, m)
f = np.dot(m_t, f)
iteration, omega = 0, 1.5
while lg.norm(x - x_1, ord=np.inf) >= eps:
x_1 = np.copy(x)
for i in range(len(x)):
x[i] -= omega * float(sum([x[j] * m[i, j] for j in range(len(x))]) - f[i]) / m[i, i]
iteration += 1
r = np.array(np.dot(a, x) - b).T[0]
with open('summary/relaxation.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()