diff --git a/Gauss Elimination with Partial Pivoting/README.md b/Gauss Elimination with Partial Pivoting/README.md new file mode 100644 index 0000000..093c651 --- /dev/null +++ b/Gauss Elimination with Partial Pivoting/README.md @@ -0,0 +1,11 @@ +# Implementation of Gauss Elimination with Partial Pivoting +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. + +# Screenshot +Those are some examples: + +![Example 1](gauss_example1.jpg) +![Example 2](gauss_example2.jpg) + +# Author +[GiovannaRMendes](https://github.com/GiovannaRMendes) \ No newline at end of file diff --git a/Gauss Elimination with Partial Pivoting/gauss_example1.jpg b/Gauss Elimination with Partial Pivoting/gauss_example1.jpg new file mode 100644 index 0000000..9c3fa5e Binary files /dev/null and b/Gauss Elimination with Partial Pivoting/gauss_example1.jpg differ diff --git a/Gauss Elimination with Partial Pivoting/gauss_example2.jpg b/Gauss Elimination with Partial Pivoting/gauss_example2.jpg new file mode 100644 index 0000000..6f176d1 Binary files /dev/null and b/Gauss Elimination with Partial Pivoting/gauss_example2.jpg differ diff --git a/Gauss Elimination with Partial Pivoting/main.py b/Gauss Elimination with Partial Pivoting/main.py new file mode 100644 index 0000000..49695fb --- /dev/null +++ b/Gauss Elimination with Partial Pivoting/main.py @@ -0,0 +1,120 @@ +def gauss_partial(matrix: list, index_column: int): + index_max_val = index_column + + for i in range(index_column, len(matrix)): + if matrix[index_column][index_column] < abs(matrix[i][index_column]): + index_max_val = i + + if index_column != index_max_val: + matrix[index_column], matrix[index_max_val] = matrix[index_max_val], matrix[index_column] + + +def verify_triangular(matrix: list) -> bool: + + for i in range(len(matrix)): + for j in range(i, len(matrix)): + + if (j == i and matrix[j][i] == 0) or (j != i and matrix[j][i] != 0): + return False + + return True + + +def gauss(matrix: list, index: int) -> list: + + for j in range(index+1, len(matrix)): + coefficient = round(matrix[j][index] / matrix[index][index], 2) + matrix[j][index] = 0 + + for k in range(index+1, len(matrix)+1): + matrix[j][k] = round(matrix[j][k] - coefficient * matrix[index][k], 5) + + return matrix + + +def resolve_linear_system(matrix: list) -> list: + sum = 0 + n = len(matrix) + results = [1] * n + + for i in range(n-1, -1, -1): + + for j in range(n-1, i, -1): + sum = sum + matrix[i][j]*results[j] + + results[i] = round((matrix[i][n] - sum)/matrix[i][i], 2) + sum = 0 + + return results + + +def resolve_matrix(matrix: list): + + for i in range(len(matrix)): + gauss_partial(matrix, i) + matrix = gauss(matrix, i) + + print('\nThe matrix after Gauss:') + + for line in matrix: + print("[", end='') + print(*(f'{val}' for val in line), end='') + print("]") + + if verify_triangular(matrix): + + print("\nThe result of Gauss Elimination with Partial Pivoting:") + for i, x in enumerate(resolve_linear_system(matrix)): + print(f'x_{i} = {x}') + + else: + print("It's impossible to resolve, because it's not a triangular matrix") + return 0 + + +def take_matrix() -> list: + + line = 0 + matrix = [] + max_length = 0 + + number_lines = int(input("Please, insert the length of the your matrix: ")) + print("Please, insert only the numbers of the your augmented matrix (separete with espace):") + + for i in range(number_lines): + + try: + line = list(input().split()) + line = list(int(val) for val in line) + + if max_length < len(line): + max_length = len(line) + + matrix.append(line) + + except: + print("This matrix don't exist!!!") + return 1 + + if len(matrix) == max_length - 1: + return matrix + + return 0 + + +if __name__ == '__main__': + + matrix = take_matrix() + + if matrix == 0: + print("It's impossible to resolve") + + else: + print('The matrix:') + + for line in matrix: + print("[", end='') + print(*(f'{val}' for val in line), end='') + print("]") + + resolve_matrix(matrix) \ No newline at end of file diff --git a/Gauss Elimination with Partial Pivoting/runtime.txt b/Gauss Elimination with Partial Pivoting/runtime.txt new file mode 100644 index 0000000..4937ae1 --- /dev/null +++ b/Gauss Elimination with Partial Pivoting/runtime.txt @@ -0,0 +1 @@ +python-3.12.6 \ No newline at end of file