Skip to content

Commit 5675c6d

Browse files
authored
Merge pull request #104 from GiovannaRMendes/feat-gauss
feat: Added Gauss Elimination
2 parents 9c35061 + 2aab78c commit 5675c6d

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Implementation of Gauss Elimination with Partial Pivoting
2+
This implementaion is a Gauss Elimination with Partial Pivoting in Python. You can insert a number of lines of your augmented matrix and then, the numbers that compose it.
3+
4+
# Screenshot
5+
Those are some examples:
6+
7+
![Example 1](gauss_example1.jpg)
8+
![Example 2](gauss_example2.jpg)
9+
10+
# Author
11+
[GiovannaRMendes](https://github.com/GiovannaRMendes)
27.6 KB
Loading
22.6 KB
Loading
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
def gauss_partial(matrix: list, index_column: int):
2+
index_max_val = index_column
3+
4+
for i in range(index_column, len(matrix)):
5+
if matrix[index_column][index_column] < abs(matrix[i][index_column]):
6+
index_max_val = i
7+
8+
if index_column != index_max_val:
9+
matrix[index_column], matrix[index_max_val] = matrix[index_max_val], matrix[index_column]
10+
11+
12+
def verify_triangular(matrix: list) -> bool:
13+
14+
for i in range(len(matrix)):
15+
for j in range(i, len(matrix)):
16+
17+
if (j == i and matrix[j][i] == 0) or (j != i and matrix[j][i] != 0):
18+
return False
19+
20+
return True
21+
22+
23+
def gauss(matrix: list, index: int) -> list:
24+
25+
for j in range(index+1, len(matrix)):
26+
coefficient = round(matrix[j][index] / matrix[index][index], 2)
27+
matrix[j][index] = 0
28+
29+
for k in range(index+1, len(matrix)+1):
30+
matrix[j][k] = round(matrix[j][k] - coefficient * matrix[index][k], 5)
31+
32+
return matrix
33+
34+
35+
def resolve_linear_system(matrix: list) -> list:
36+
sum = 0
37+
n = len(matrix)
38+
results = [1] * n
39+
40+
for i in range(n-1, -1, -1):
41+
42+
for j in range(n-1, i, -1):
43+
sum = sum + matrix[i][j]*results[j]
44+
45+
results[i] = round((matrix[i][n] - sum)/matrix[i][i], 2)
46+
sum = 0
47+
48+
return results
49+
50+
51+
def resolve_matrix(matrix: list):
52+
53+
for i in range(len(matrix)):
54+
gauss_partial(matrix, i)
55+
matrix = gauss(matrix, i)
56+
57+
print('\nThe matrix after Gauss:')
58+
59+
for line in matrix:
60+
print("[", end='')
61+
print(*(f'{val}' for val in line), end='')
62+
print("]")
63+
64+
if verify_triangular(matrix):
65+
66+
print("\nThe result of Gauss Elimination with Partial Pivoting:")
67+
for i, x in enumerate(resolve_linear_system(matrix)):
68+
print(f'x_{i} = {x}')
69+
70+
else:
71+
print("It's impossible to resolve, because it's not a triangular matrix")
72+
return 0
73+
74+
75+
def take_matrix() -> list:
76+
77+
line = 0
78+
matrix = []
79+
max_length = 0
80+
81+
number_lines = int(input("Please, insert the length of the your matrix: "))
82+
print("Please, insert only the numbers of the your augmented matrix (separete with espace):")
83+
84+
for i in range(number_lines):
85+
86+
try:
87+
line = list(input().split())
88+
line = list(int(val) for val in line)
89+
90+
if max_length < len(line):
91+
max_length = len(line)
92+
93+
matrix.append(line)
94+
95+
except:
96+
print("This matrix don't exist!!!")
97+
return 1
98+
99+
if len(matrix) == max_length - 1:
100+
return matrix
101+
102+
return 0
103+
104+
105+
if __name__ == '__main__':
106+
107+
matrix = take_matrix()
108+
109+
if matrix == 0:
110+
print("It's impossible to resolve")
111+
112+
else:
113+
print('The matrix:')
114+
115+
for line in matrix:
116+
print("[", end='')
117+
print(*(f'{val}' for val in line), end='')
118+
print("]")
119+
120+
resolve_matrix(matrix)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-3.12.6

0 commit comments

Comments
 (0)