aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Albers Raviola <thomas@thomaslabs.org>2024-06-04 11:26:32 +0200
committerThomas Albers Raviola <thomas@thomaslabs.org>2024-06-04 11:26:32 +0200
commitfc10b363b3c35313ac2f1bf14f83daec2bf22c41 (patch)
tree310335d19fc0623222ff2aca138947e29bc00c1e
parentfe852453c0cb80e05a0adc4a95ceb52873461851 (diff)
Throw exception on linear dependenceHEADmaster
-rw-r--r--solvers.py4
-rw-r--r--test/test_solvers.py12
2 files changed, 7 insertions, 9 deletions
diff --git a/solvers.py b/solvers.py
index 72f1b1d..38ac247 100644
--- a/solvers.py
+++ b/solvers.py
@@ -92,7 +92,7 @@ def gaussian_eliminate(
aa: NDArray[np.float_],
bb: NDArray[np.float_],
tolerance: float = 1e-6
-) -> NDArray[np.float_] | None:
+) -> NDArray[np.float_]:
'''Solves a linear system of equations (A x = b) by LUP factorization.
Args:
@@ -108,7 +108,7 @@ def gaussian_eliminate(
nn = uu.shape[0]
# Check if rank of matrix is lower than nn
if np.abs(uu[nn - 1, nn - 1]) < tolerance:
- return None
+ raise ValueError
# L y = P @ b
y = forward_substitution(ll, pp @ bb)
diff --git a/test/test_solvers.py b/test/test_solvers.py
index a7c175f..b049cca 100644
--- a/test/test_solvers.py
+++ b/test/test_solvers.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Contains routines to test the solvers module"""
+import pytest
import numpy as np
from numpy.typing import NDArray
@@ -27,11 +28,10 @@ def test_pivot_3() -> None:
def test_lindep_3() -> None:
"""Tests a linearly dependent system wit three variables."""
- aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
- bb = np.array([1.0, 2.0, 3.0])
- xx_expected = None
- xx_gauss = solvers.gaussian_eliminate(aa, bb)
- assert xx_expected == xx_gauss
+ with pytest.raises(ValueError):
+ aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
+ bb = np.array([1.0, 2.0, 3.0])
+ xx_gauss = solvers.gaussian_eliminate(aa, bb)
def test_lu_factorization() -> None:
@@ -45,7 +45,6 @@ def test_forward_substitution() -> None:
bb = np.array([3.0, 2.0, 1.0])
xx_result = solvers.forward_substitution(ll, bb)
xx_expected = np.array([3 / 2, 7.0 / 4.0, -6])
- print(xx_result)
assert np.allclose(xx_expected, xx_result)
def test_back_substitution() -> None:
@@ -53,5 +52,4 @@ def test_back_substitution() -> None:
bb = np.array([1.0, 2.0, 3.0])
xx_result = solvers.back_substitution(uu, bb)
xx_expected = np.array([-6, 7.0 / 4.0, 3.0 / 2.0])
- print(xx_result)
assert np.allclose(xx_expected, xx_result)