diff options
author | Thomas Albers Raviola <thomas@thomaslabs.org> | 2024-05-14 11:13:31 +0200 |
---|---|---|
committer | Thomas Albers Raviola <thomas@thomaslabs.org> | 2024-05-14 11:13:31 +0200 |
commit | 45aaf49cb3fe07b006316bacac1318f07a08cc19 (patch) | |
tree | 97c07e83ff6847eb6a2c93dcf841e2c4c33e8b9f |
* Initial commit
-rw-r--r-- | README.md | 0 | ||||
-rw-r--r-- | solvers.py | 20 | ||||
-rw-r--r-- | test_solvers.py | 54 |
3 files changed, 74 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/solvers.py b/solvers.py new file mode 100644 index 0000000..7aa9568 --- /dev/null +++ b/solvers.py @@ -0,0 +1,20 @@ +"""Routines for solving a linear system of equations.""" + +import numpy as np +from numpy.typing import NDArray + + +def gaussian_eliminate(aa: NDArray[np.float_], bb: NDArray[np.float_]) -> NDArray[np.float_] | None: + """Solves a linear system of equations (Ax = b) by Gauss-elimination + + Args: + aa: Matrix with the coefficients. Shape: (n, n). + bb: Right hand side of the equation. Shape: (n,) + + Returns: + Vector xx with the solution of the linear equation or None + if the equations are linearly dependent. + """ + nn = aa.shape[0] + xx = np.zeros((nn,), dtype=np.float_) + return xx diff --git a/test_solvers.py b/test_solvers.py new file mode 100644 index 0000000..ad37c8c --- /dev/null +++ b/test_solvers.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Contains routines to test the solvers module""" + +import numpy as np +from numpy.typing import NDArray +import solvers + + +def main() -> None: + """Main testing function.""" + + print("\nTest elimination") + test_elimination_3() + print("\nTest pivot") + test_pivot_3() + print("\nTest linear dependency") + test_lindep_3() + + +def test_elimination_3() -> None: + """Tests elimination with 3 variables.""" + aa = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]], dtype=np.float_) + bb = np.array([1.0, 4.0, 2.0], dtype=np.float_) + xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5], dtype=np.float_) + xx_gauss = solvers.gaussian_eliminate(aa, bb) + _check_result(xx_expected, xx_gauss) + + +def test_pivot_3() -> None: + """Tests a solution with 3 variables, where pivot is necessary.""" + aa = np.array([[2.0, 4.0, 4.0], [1.0, 2.0, -1.0], [5.0, 4.0, 2.0]]) + bb = np.array([1.0, 2.0, 4.0]) + xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5]) + xx_gauss = solvers.gaussian_eliminate(aa, bb) + _check_result(xx_expected, xx_gauss) + + +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) + _check_result(xx_expected, xx_gauss) + + +def _check_result(expected: NDArray[np.float_] | None, obtained: NDArray[np.float_] | None) -> None: + """Checks results by printing expected and obtained one.""" + print("Expected:", expected) + print("Obtained:", obtained) + + +if __name__ == "__main__": + main() |