forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussianeliminationpivoting.py
75 lines (60 loc) · 2.23 KB
/
gaussianeliminationpivoting.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
import numpy as np
from typing import List
def custom_pivoting(a: np.ndarray, n: int, i: int) -> int:
"""
Selects the index of the minimum absolute value in the i-th column of a matrix.
Parameters:
- a (np.ndarray): The input matrix.
- n (int): The size of the matrix.
- i (int): The column index.
Returns:
- int: The index of the minimum absolute value in the i-th column.
Example:
>>> a_matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=float)
>>> custom_pivoting(a_matrix, 3, 1)
0
"""
min_index = i
for index in range(i + 1, n):
if abs(a[index][i]) < abs(a[min_index][i]):
min_index = index
return min_index
def custom_gauss_elimination_pivoting(a: List[List[float]], b: List[float], n: int) -> List[float]:
"""
Solves a system of linear equations using Gaussian elimination with partial pivoting.
Parameters:
- a (List[List[float]]): The coefficient matrix.
- b (List[float]): The constant vector.
- n (int): The size of the system.
Returns:
- List[float]: The solution vector.
Example:
>>> a_matrix = [[2, 3, 4], [1, -2, 3], [3, 4, 5]]
>>> b_vector = [20, 9, 11]
>>> custom_gauss_elimination_pivoting(a_matrix, b_vector, 3)
[1.0, 2.0, 3.0]
"""
result = []
for i in range(n - 1):
new_index = custom_pivoting(a, n, i)
a[i], a[new_index] = a[new_index], a[i]
b[i], b[new_index] = b[new_index], b[i]
pivot = a[i][i]
for j in range(i + 1, n):
m = -1 * a[j][i] / pivot
for k in range(0, n):
a[j][k] += m * a[i][k]
b[j] += m * b[i]
for p in range(n - 1, -1, -1):
result.append(b[p] / a[p][p])
for q in range(p - 1, -1, -1):
b[q] = b[q] - result[n - p - 1] * a[q][p]
return result
# 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<<<<<#