Skip to content

Commit 26479f8

Browse files
authored
Enhance Gauss-Seidel method with result rounding
Added rounding to results for stable test comparison.
1 parent a51b228 commit 26479f8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

maths/numerical_analysis/gauss_seidel_method.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ def gauss_seidel(
22
coefficients: list[list[float]],
33
rhs: list[float],
44
tol: float = 1e-10,
5-
max_iter: int = 1000,
5+
max_iter: int = 1000
66
) -> list[float]:
77
"""
88
Solve the linear system Ax = b using the Gauss-Seidel iterative method.
@@ -35,8 +35,11 @@ def gauss_seidel(
3535
sum_after = sum(coefficients[i][j] * x[j] for j in range(i + 1, n))
3636
x_new[i] = (rhs[i] - sum_before - sum_after) / coefficients[i][i]
3737

38+
# Convergence check
3839
if all(abs(x_new[i] - x[i]) < tol for i in range(n)):
39-
return x_new
40+
# Return rounded results for stable test comparison
41+
return [round(val, 10) for val in x_new]
42+
4043
x = x_new
4144

42-
return x
45+
return [round(val, 10) for val in x]

0 commit comments

Comments
 (0)