Решение систем линейных уравнений методом Жордана-Гаусса

 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
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
from copy import copy, deepcopy
def make_one(a, b, i):
d = 1. / a[i][i]
for k in range(len(a[i])):
a[i][k] *= d
b[i] *= d
return a, b
def make_zero(a, b, i, j):
n = len(a)
m = len(a[0])
for k in range(n):
if k != i and a[k][j] == 1:
break
d = a[i][j]
for l in range(m):
a[i][l] -= a[k][l]*d
b[i] -= b[k]*d
return a, b
def solve(a, b):
n = len(a)
m = len(a[0])
a = deepcopy(a); b = copy(b)
for j in range(m):
a, b = make_one(a, b, j)
r = range(n); r.remove(j)
for i in r:
a, b = make_zero(a, b, i, j)
return a, b
def input_data(argv):
a = []; b = []
b = argv[2].split(' ')
m = len(b)
b = [float(b[j]) for j in range(m)]
t = argv[1].split(' ')
n = len(t) / m
k = 0
for i in range(n):
a.append([])
for j in range(m):
a[i].append(float(t[k]))
k += 1
return a, b
if __name__ == "__main__":
if len(sys.argv) < 3:
print("usage: %s '1 1 1 4 2 1 9 3 1' '0 1 3'" % sys.argv[0])
sys.exit(1)
a, b = input_data(sys.argv)
a, b = solve(a, b)
print(b)