Skip to content

implementation of Gaussian Elimination pivoting as a numerical linear algebra algorithm #10457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 95 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
579e99f
Adding new implementation
TheKidPadra Oct 14, 2023
ad230e5
Delete linear_algebra/src/GaussianEliminationpivoting.py
TheKidPadra Oct 14, 2023
dc9fb8a
Adding new implementation
TheKidPadra Oct 14, 2023
44ca32d
Delete linear_algebra/src/gaussianeliminationpivoting.py
TheKidPadra Oct 14, 2023
9c48e4b
Adding new implementation
TheKidPadra Oct 14, 2023
d9db297
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
c5cae5f
Merge branch 'TheAlgorithms:master' into master
TheKidPadra Oct 14, 2023
135405a
Delete linear_algebra/src/gaussianeliminationpivoting.py
TheKidPadra Oct 14, 2023
0eb31fc
Adding gaussianeliminationpivoting.py
TheKidPadra Oct 14, 2023
56ceb6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
2e2e767
Merge branch 'TheAlgorithms:master' into master
TheKidPadra Oct 18, 2023
f270415
Update gaussianeliminationpivoting.py
TheKidPadra Oct 18, 2023
3d1e8aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2023
6a0b6dd
Update gaussianeliminationpivoting.py
TheKidPadra Oct 18, 2023
8753484
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2023
87c2925
Merge branch 'TheAlgorithms:master' into master
TheKidPadra Oct 26, 2023
0f62cf6
Update gaussianeliminationpivoting.py
TheKidPadra Oct 26, 2023
579468b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
ec2b578
Update gaussianeliminationpivoting.py
TheKidPadra Oct 26, 2023
3c5344c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
4a85130
Update gaussianeliminationpivoting.py
TheKidPadra Oct 26, 2023
19f0edf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
b1bc7ff
Update gaussianeliminationpivoting.py
TheKidPadra Oct 26, 2023
f7900b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
d519383
Update and rename gaussianeliminationpivoting.py to gaussian_eliminat…
TheKidPadra Oct 26, 2023
aac96dd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
eafd037
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
769126f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
6f00d68
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
45aefe8
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
3865376
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
33b85e6
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
d713c70
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
cf01b7e
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
0b19e0e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
b9e172f
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
c588c9d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
3966efc
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
07a8109
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
7d90336
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
2492d60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
757c23e
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
301daa0
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
8c19a51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
5d7dc32
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
fe4352b
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
a16c48e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
58ad12e
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
ded4ad0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
6772aeb
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
aeab790
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
f4296b0
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
88af17d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
94acda9
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
619cc00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
a726733
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
5e6b9fa
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
0a529e8
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
7e66a8e
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
2815287
Delete linear_algebra/src/gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
6d648ec
Add files via upload
TheKidPadra Oct 26, 2023
9f6326e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
07f5e9b
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
3b2ae9b
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 26, 2023
a566734
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
348c630
Delete linear_algebra/src/gaussian_elimination_pivoting/text.py
TheKidPadra Oct 26, 2023
4b824d7
Add files via upload
TheKidPadra Oct 26, 2023
13fff37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
df34643
Update linear_algebra/src/gaussian_elimination_pivoting/gaussian_elim…
TheKidPadra Oct 28, 2023
f55e7ac
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
28a5410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
30933f4
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
ea6ad3b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
d5f04f6
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
caf3a97
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
6096e35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
606667c
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
8594cc0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
ed58bab
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
29f3dff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
c5981ee
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
bc53093
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
074737b
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
5148009
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
52d7cfa
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 28, 2023
1d628ae
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
65a782b
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
dd16955
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2023
b81729f
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
8cc0a36
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
2b8c8b0
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
ec6aedd
Update gaussian_elimination_pivoting.py
TheKidPadra Oct 30, 2023
a5b345f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2023
20462d1
Update gaussian_elimination_pivoting.py
cclauss Oct 30, 2023
7ff1a46
Update gaussian_elimination_pivoting.py
cclauss Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import sys
import time
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import time
import time
from pathlib import Path


import numpy as np

matrixab = np.loadtxt("matrix.txt")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
matrixab = np.loadtxt("matrix.txt")
matrixab = np.loadtxt(Path(__file__).parent / "matrix.txt")

B = np.copy(matrixab[:, matrixab.shape[1] - 1])


def foo(matrix):
Copy link
Member

@cclauss cclauss Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please. Come up with a better name for this function and add type hints and maybe even some doctests.

According to CONTRIBUTING.md algorithmic functions should not print().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is renamed to solve_linear_system, and type hints are added for parameters and return values. The doctest provides an example of usage and asserts the correctness of the solution. I've also removed the print statements and replaced the exit call with a sys.exit exception to allow for better error handling.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file linear_algebra/src/gaussian_elimination_pivoting/gaussian_elimination_pivoting.py, please provide doctest for the function foo

Please provide return type hint for the function: foo. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: matrix

start = time.process_time()
ab = np.copy(matrix)
numofrows = ab.shape[0]
numofcolumns = ab.shape[1] - 1
xlst = []

"""Lead element search"""
print("Matrix before leading coefficient search: ")
print(ab)
print(" ")

"""Upper triangular matrix"""
for columnnum in range(numofrows):
for i in range(columnnum, numofcolumns):
if abs(ab[i][columnnum]) > abs(ab[columnnum][columnnum]):
ab[[columnnum, i]] = ab[[i, columnnum]]
if ab[columnnum, columnnum] == 0.0:
sys.exit("Matrix is not correct")
else:
pass
if columnnum != 0:
for i in range(columnnum, numofrows):
ab[i, :] -= (
ab[i, columnnum - 1]
/ ab[columnnum - 1, columnnum - 1]
* ab[columnnum - 1, :]
)

print("Upper triangular matrix: ")
print(ab.round(3))
print(" ")

"""Find x vector"""
columnnum = numofrows
while columnnum != 0:
columnnum -= 1
lineofx = ab[columnnum, numofrows]
if columnnum + 1 != numofrows:
for y in range(1, numofrows - columnnum):
lineofx += -ab[columnnum, numofrows - y] * xlst[y - 1]
x = lineofx / ab[columnnum, columnnum]
xlst.append(x)

stop = time.process_time()
xlst.reverse()
print("x vector: ")
print(xlst)
print(" ")
print(f"Start time: {start}, End time: {stop}")
print(f"Elapsed time during the whole function in seconds: {stop - start}")

return np.asarray(xlst)


if __name__ == "__main__":
vectorofxalpha = foo(matrixab)

"""Cond(A)"""
modifiedb = np.copy(B)
modifiedb[np.argmax(abs(B))] = B[np.argmax(abs(B))] / 100 * 101

matrixab[:, matrixab.shape[1] - 1] = modifiedb
print()
print("Cond(A) check: ")
vectorofxbeta = foo(matrixab)

deltab = modifiedb - B
deltax = vectorofxalpha - vectorofxbeta
print(" ")
conda = abs(np.sum(deltax) / np.sum(vectorofxalpha)) * (np.sum(B) / np.sum(deltab))
print(f"Cond(A) =< {conda:0.6f}")


# Example usage:
# n_size = 3
# a_matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=float)
# b_vector = np.array([10, 11, 12], dtype=float)

# solution = custom_gauss_elimination_pivoting(a_matrix, b_vector, n_size)
# print("Solution:", solution)


# URL that points to Wikipedia or another similar explanation.
# >>>>>>URL:https://courses.engr.illinois.edu/cs357/su2013/lectures/lecture07.pdf<<<<<#
4 changes: 4 additions & 0 deletions linear_algebra/src/gaussian_elimination_pivoting/matrix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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