-
Notifications
You must be signed in to change notification settings - Fork 5
/
gauss.py
53 lines (39 loc) · 1.65 KB
/
gauss.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
# To obtain the inverse matrix:
def inv(matrix):
matrix_size = len(matrix)
result = [[0 for i in range(0, matrix_size)] for j in range(0, matrix_size)]
for i in range(0, matrix_size):
column = for_column(matrix, i)
for j in range(0, matrix_size):
result[j][i] = column[j]
return result
# To convert a matrix into an inverse matrix:
def for_column(our_matrix, column):
matrix_size = len(our_matrix)
mega_matrix = [[our_matrix[i][j] for j in range(matrix_size)] for i in range(matrix_size)]
new_column = [0 for i in range(matrix_size)]
for i in range(matrix_size):
mega_matrix[i].append(float(i == column))
for i in range(0, matrix_size):
if mega_matrix[i][i] == 0:
for j in range(i + 1, matrix_size):
if mega_matrix[j][j] != 0:
mega_matrix[i], mega_matrix[j] = mega_matrix[j], mega_matrix[i]
for j in range(i + 1, matrix_size):
d = - mega_matrix[j][i] / mega_matrix[i][i]
for k in range(0, matrix_size + 1):
mega_matrix[j][k] += d * mega_matrix[i][k]
for i in range(matrix_size - 1, -1, -1):
for_result = 0
for j in range(matrix_size):
for_result += mega_matrix[i][j] * new_column[j]
new_column[i] = (mega_matrix[i][matrix_size] - for_result) / mega_matrix[i][i]
return new_column
# Multiplication of SLAU to column:
def multi(SLAU, column):
column_size = len(column)
result = [0 for j in range(column_size)]
for j in range(column_size):
for k in range(column_size):
result[j] += column[k] * SLAU[j][k]
return result