From 6bff474a8295f3b46c03622fd8ffa5239dd08803 Mon Sep 17 00:00:00 2001
From: markus holzer <markus.holzer@fau.de>
Date: Sat, 8 Aug 2020 13:58:43 +0200
Subject: [PATCH] Added test case for LinearEquationSystem

---
 pystencils/utils.py            |  6 +-----
 pystencils_tests/test_utils.py | 36 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 5 deletions(-)
 create mode 100644 pystencils_tests/test_utils.py

diff --git a/pystencils/utils.py b/pystencils/utils.py
index f089dad1b..6cbe324e9 100644
--- a/pystencils/utils.py
+++ b/pystencils/utils.py
@@ -206,6 +206,7 @@ class LinearEquationSystem:
         non_zero_rows = self.next_zero_row
         num_unknowns = len(self.unknowns)
         if non_zero_rows == 0:
+            print("test")
             return 'multiple'
 
         *row_begin, left, right = self._matrix.row(non_zero_rows - 1)
@@ -239,8 +240,3 @@ class LinearEquationSystem:
                 break
             result -= 1
         self.next_zero_row = result
-
-
-def find_unique_solutions_with_zeros(system: LinearEquationSystem):
-    if not system.solution_structure() != 'multiple':
-        raise ValueError("Function works only for underdetermined systems")
diff --git a/pystencils_tests/test_utils.py b/pystencils_tests/test_utils.py
new file mode 100644
index 000000000..0cc2895ba
--- /dev/null
+++ b/pystencils_tests/test_utils.py
@@ -0,0 +1,36 @@
+import sympy as sp
+from pystencils.utils import LinearEquationSystem
+
+
+def test_LinearEquationSystem():
+    x, y, z = sp.symbols("x, y, z")
+    les = LinearEquationSystem([x, y, z])
+    les.add_equation(1 * x + 2 * y - 1 * z + 4)
+    les.add_equation(2 * x + 1 * y + 1 * z - 2)
+    les.add_equation(1 * x + 2 * y + 1 * z + 2)
+
+    # usually reduce is not necessary since almost every function of LinearEquationSystem calls reduce beforehand
+    les.reduce()
+
+    expected_matrix = sp.Matrix([[1, 0, 0, sp.Rational(5, 3)],
+                                 [0, 1, 0, sp.Rational(-7, 3)],
+                                 [0, 0, 1, sp.Integer(1)]])
+    assert les.matrix == expected_matrix
+    assert les.rank == 3
+
+    sol = les.solution()
+    assert sol[x] == sp.Rational(5, 3)
+    assert sol[y] == sp.Rational(-7, 3)
+    assert sol[z] == sp.Integer(1)
+
+    les = LinearEquationSystem([x, y])
+    assert les.solution_structure() == 'multiple'
+
+    les.add_equation(x + 1)
+    assert les.solution_structure() == 'multiple'
+
+    les.add_equation(y + 2)
+    assert les.solution_structure() == 'single'
+
+    les.add_equation(x + y + 5)
+    assert les.solution_structure() == 'none'
-- 
GitLab