-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Changes from 69 commits
579e99f
ad230e5
dc9fb8a
44ca32d
9c48e4b
d9db297
c5cae5f
135405a
0eb31fc
56ceb6a
2e2e767
f270415
3d1e8aa
6a0b6dd
8753484
87c2925
0f62cf6
579468b
ec2b578
3c5344c
4a85130
19f0edf
b1bc7ff
f7900b9
d519383
aac96dd
eafd037
769126f
6f00d68
45aefe8
3865376
33b85e6
d713c70
cf01b7e
0b19e0e
b9e172f
c588c9d
3966efc
07a8109
7d90336
2492d60
757c23e
301daa0
8c19a51
5d7dc32
fe4352b
a16c48e
58ad12e
ded4ad0
6772aeb
aeab790
f4296b0
88af17d
94acda9
619cc00
a726733
5e6b9fa
0a529e8
7e66a8e
2815287
6d648ec
9f6326e
07f5e9b
3b2ae9b
a566734
348c630
4b824d7
13fff37
df34643
f55e7ac
28a5410
30933f4
ea6ad3b
d5f04f6
caf3a97
6096e35
606667c
8594cc0
ed58bab
29f3dff
c5981ee
bc53093
074737b
5148009
52d7cfa
1d628ae
65a782b
dd16955
b81729f
8cc0a36
2b8c8b0
ec6aedd
a5b345f
20462d1
7ff1a46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,94 @@ | ||||||
import sys | ||||||
import time | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
matrixab = np.loadtxt("matrix.txt") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
B = np.copy(matrixab[:, matrixab.shape[1] - 1]) | ||||||
|
||||||
|
||||||
def foo(matrix): | ||||||
TheKidPadra marked this conversation as resolved.
Show resolved
Hide resolved
TheKidPadra marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide type hint for the parameter: |
||||||
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<<<<<# |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.