Skip to content

Commit dca13f6

Browse files
rajsrivpre-commit-ci[bot]cclauss
authored
Implement Gauss-Seidel method for solving linear systems. (#13535)
* Add Gauss-Seidel method implementation Implement Gauss-Seidel method for solving linear systems. * Update gauss-seidel-method.py * Rename gauss-seidel-method.py to gauss_seidel_method.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename parameters in Gauss-Seidel method * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enhance gauss_seidel function documentation and code style Refactor gauss_seidel method for improved readability and clarity. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enhance Gauss-Seidel method with result rounding Added rounding to results for stable test comparison. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Clean up comments in Gauss-Seidel method Removed comments related to convergence check and result rounding. * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 9523b68 commit dca13f6

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@
822822
* [Bisection](maths/numerical_analysis/bisection.py)
823823
* [Bisection 2](maths/numerical_analysis/bisection_2.py)
824824
* [Brent Method](maths/numerical_analysis/brent_method.py)
825+
* [Gauss Seidel Method](maths/numerical_analysis/gauss_seidel_method.py)
825826
* [Integration By Simpson Approx](maths/numerical_analysis/integration_by_simpson_approx.py)
826827
* [Intersection](maths/numerical_analysis/intersection.py)
827828
* [Nevilles Method](maths/numerical_analysis/nevilles_method.py)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def gauss_seidel(
2+
coefficients: list[list[float]],
3+
rhs: list[float],
4+
tol: float = 1e-10,
5+
max_iter: int = 1000,
6+
) -> list[float]:
7+
"""
8+
Solve the linear system Ax = b using the Gauss-Seidel iterative method.
9+
10+
Args:
11+
coefficients (list[list[float]]): Coefficient matrix (n x n)
12+
rhs (list[float]): Right-hand side vector (n)
13+
tol (float): Convergence tolerance
14+
max_iter (int): Maximum number of iterations
15+
16+
Returns:
17+
list[float]: Approximate solution vector
18+
19+
Example:
20+
>>> A = [[4, 1, 2], [3, 5, 1], [1, 1, 3]]
21+
>>> b = [4, 7, 3]
22+
>>> gauss_seidel(A, b)
23+
[0.5, 1.0, 0.5]
24+
25+
Wikipedia:
26+
https://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method
27+
"""
28+
n = len(coefficients)
29+
x = [0.0 for _ in range(n)]
30+
31+
for _ in range(max_iter):
32+
x_new = x.copy()
33+
for i in range(n):
34+
sum_before = sum(coefficients[i][j] * x_new[j] for j in range(i))
35+
sum_after = sum(coefficients[i][j] * x[j] for j in range(i + 1, n))
36+
x_new[i] = (rhs[i] - sum_before - sum_after) / coefficients[i][i]
37+
38+
if all(abs(x_new[i] - x[i]) < tol for i in range(n)):
39+
return [round(val, 10) for val in x_new]
40+
41+
x = x_new
42+
43+
return [round(val, 10) for val in x]

0 commit comments

Comments
 (0)