forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian_elimination_pivoting.py
94 lines (74 loc) · 2.6 KB
/
gaussian_elimination_pivoting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import sys
import time
import numpy as np
matrixab = np.loadtxt("matrix.txt")
B = np.copy(matrixab[:, matrixab.shape[1] - 1])
def foo(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<<<<<#