-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab-1-determinant-gaussian-elimination.py
82 lines (71 loc) · 2.22 KB
/
lab-1-determinant-gaussian-elimination.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# #
# Calculate the matrix determinant using Gaussian elimination #
# with the main matrix element selection #
# #
def main(A=None):
if A is None:
A = []
n = int(input('Please enter the matrix dimension: '))
for i in range(0, n):
A.append([])
for i in range(0, n):
for j in range(0, n):
row_element = float(input("A[{}][{}] = ".format(i + 1, j + 1)))
A[i].append(row_element)
else:
n = len(A)
print('Your matrix: ')
for i in range(0, n):
print(A[i])
det = 1
V = []
C = []
for column in range(0, n):
V.append([])
C.append([])
for i in range(0, n):
for j in range(0, n):
V[i].append(A[i][j])
for i in range(0, n):
for j in range(0, n):
if j > i:
C[i].append(0)
else:
C[i].append(A[i][j])
for k in range(0, n):
max = abs(V[k][k])
h = k
w = k
for l in range(k, n):
for f in range(k, n):
if max < V[l][f]:
max = abs(V[l][f])
h = l
w = f
for d in range(0, n):
value = V[k][d]
V[k][d] = V[h][d]
V[h][d] = value
for d in range(0, n):
if d < k:
value = C[d][k]
C[d][k] = C[d][w]
C[d][w] = value
else:
value = V[d][k]
V[d][k] = V[d][w]
V[d][w] = value
det = det * pow((-1), w + h) * V[k][k]
for i in range(k + 1, n):
for j in range(k + 1, n):
C[k][j] = V[k][j] / V[k][k]
V[i][j] = V[i][j] - V[i][k] * C[k][j]
print('Determinant = ' + str(det))
if __name__ == '__main__':
A = [
[8.30, 2.78, 4.10, 1.90],
[3.92, 8.45, 7.62, 2.46],
[3.77, 7.37, 8.04, 2.28],
[2.21, 3.49, 1.69, 6.69]
]
main(A)