Python/linear_algebra/src/gaussian_elimination_pivoting/gaussian_elimination_pivoting.py
Mohammad Esfandiyar ad9948d5d4
implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm (#10457)
* Adding new implementation

Adding my python implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm

* Delete linear_algebra/src/GaussianEliminationpivoting.py

* Adding new implementation

Adding my python implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm

* Delete linear_algebra/src/gaussianeliminationpivoting.py

* Adding new implementation

Adding my python implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm 
for the third time because the last two times had conflict with the rules in PR

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete linear_algebra/src/gaussianeliminationpivoting.py

* Adding gaussianeliminationpivoting.py

Adding my python implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm 
for the fourth time because the last three times had conflict with the rules in PR and bots

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

I changed a to matrix and coeff_matrix for better clarity

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussianeliminationpivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update and rename gaussianeliminationpivoting.py to gaussian_elimination_pivoting.py

renamed the file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Delete linear_algebra/src/gaussian_elimination_pivoting.py

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete linear_algebra/src/gaussian_elimination_pivoting/text.py

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update linear_algebra/src/gaussian_elimination_pivoting/gaussian_elimination_pivoting.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* Update gaussian_elimination_pivoting.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-30 14:20:47 +01:00

102 lines
3.2 KiB
Python

import numpy as np
matrix = np.array(
[
[5.0, -5.0, -3.0, 4.0, -11.0],
[1.0, -4.0, 6.0, -4.0, -10.0],
[-2.0, -5.0, 4.0, -5.0, -12.0],
[-3.0, -3.0, 5.0, -5.0, 8.0],
],
dtype=float,
)
def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
"""
Solve a linear system of equations using Gaussian elimination with partial pivoting
Args:
- matrix: Coefficient matrix with the last column representing the constants.
Returns:
- Solution vector.
Raises:
- ValueError: If the matrix is not correct (i.e., singular).
https://courses.engr.illinois.edu/cs357/su2013/lect.htm Lecture 7
Example:
>>> A = np.array([[2, 1, -1], [-3, -1, 2], [-2, 1, 2]], dtype=float)
>>> B = np.array([8, -11, -3], dtype=float)
>>> solution = solve_linear_system(np.column_stack((A, B)))
>>> np.allclose(solution, np.array([2., 3., -1.]))
True
>>> solve_linear_system(np.array([[0, 0], [0, 0]], dtype=float))
array([nan, nan])
"""
ab = np.copy(matrix)
num_of_rows = ab.shape[0]
num_of_columns = ab.shape[1] - 1
x_lst: list[float] = []
# Lead element search
for column_num in range(num_of_rows):
for i in range(column_num, num_of_columns):
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
ab[[column_num, i]] = ab[[i, column_num]]
if ab[column_num, column_num] == 0.0:
raise ValueError("Matrix is not correct")
else:
pass
if column_num != 0:
for i in range(column_num, num_of_rows):
ab[i, :] -= (
ab[i, column_num - 1]
/ ab[column_num - 1, column_num - 1]
* ab[column_num - 1, :]
)
# Upper triangular matrix
for column_num in range(num_of_rows):
for i in range(column_num, num_of_columns):
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
ab[[column_num, i]] = ab[[i, column_num]]
if ab[column_num, column_num] == 0.0:
raise ValueError("Matrix is not correct")
else:
pass
if column_num != 0:
for i in range(column_num, num_of_rows):
ab[i, :] -= (
ab[i, column_num - 1]
/ ab[column_num - 1, column_num - 1]
* ab[column_num - 1, :]
)
# Find x vector (Back Substitution)
for column_num in range(num_of_rows - 1, -1, -1):
x = ab[column_num, -1] / ab[column_num, column_num]
x_lst.insert(0, x)
for i in range(column_num - 1, -1, -1):
ab[i, -1] -= ab[i, column_num] * x
# Return the solution vector
return np.asarray(x_lst)
if __name__ == "__main__":
from doctest import testmod
from pathlib import Path
testmod()
file_path = Path(__file__).parent / "matrix.txt"
try:
matrix = np.loadtxt(file_path)
except FileNotFoundError:
print(f"Error: {file_path} not found. Using default matrix instead.")
# Example usage:
print(f"Matrix:\n{matrix}")
print(f"{solve_linear_system(matrix) = }")