aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorThomas Albers Raviola <thomas@thomaslabs.org>2024-06-04 11:08:51 +0200
committerThomas Albers Raviola <thomas@thomaslabs.org>2024-06-04 11:08:51 +0200
commit68870236395435606a767c4c8c1a6b45f5c1ff64 (patch)
tree535d787e504f336ea934137a5bb137cbd5b0b49a /test
parent255c2e7b7bf721296d4d4f22cb106ab3cf197e0a (diff)
Add tests for LU routine
Diffstat (limited to 'test')
-rw-r--r--test/test_solvers.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/test_solvers.py b/test/test_solvers.py
index e5b73d8..5b5da6f 100644
--- a/test/test_solvers.py
+++ b/test/test_solvers.py
@@ -3,8 +3,10 @@
import numpy as np
from numpy.typing import NDArray
+
import solvers
+
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_)
@@ -30,3 +32,26 @@ def test_lindep_3() -> None:
xx_expected = None
xx_gauss = solvers.gaussian_eliminate(aa, bb)
assert xx_expected == xx_gauss
+
+
+def test_lu_factorization() -> None:
+ aa = np.array([[2.0, 4.0, 4.0], [1.0, 2.0, -1.0], [5.0, 4.0, 2.0]])
+ ll, uu, pp = solvers.lu_factorization(aa)
+ assert np.allclose(aa, pp.T @ ll @ uu)
+
+
+def test_forward_substitution() -> None:
+ ll = np.array([[2.0, 0.0, 0.0], [-1.0, 2.0, 0.0], [4.0, 4.0, 2.0]])
+ bb = np.array([3.0, 2.0, 1.0])
+ xx_result = solvers.forward_substitution(ll, bb)[:, 0]
+ 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:
+ uu = np.array([[2.0, 4.0, 4.0], [0.0, 2.0, -1.0], [0.0, 0.0, 2.0]])
+ bb = np.array([1.0, 2.0, 3.0])
+ xx_result = solvers.back_substitution(uu, bb)[:, 0]
+ xx_expected = np.array([-6, 7.0 / 4.0, 3.0 / 2.0])
+ print(xx_result)
+ assert np.allclose(xx_expected, xx_result)