Repository: tinosulzer/PyBaMM

Issue:
improve string represenation of nodes in the expression tree
for debugging. Improve these things:
- matrices are printed out fully, need some summary (e.g. size)
- parents of the node are printed out, not terribly informative, perhaps use children instead

Discussion:

Patch:
diff --git a/pybamm/expression_tree/array.py b/pybamm/expression_tree/array.py
index 3c247db5..2cc5c5a6 100644
--- a/pybamm/expression_tree/array.py
+++ b/pybamm/expression_tree/array.py
@@ -25,7 +25,7 @@ class Array(pybamm.Symbol):
 
     def __init__(self, entries, name=None, domain=[]):
         if name is None:
-            name = str(entries)
+            name = "Array of shape {!s}".format(entries.shape)
         super().__init__(name, domain=domain)
         self._entries = entries
 
diff --git a/pybamm/expression_tree/matrix.py b/pybamm/expression_tree/matrix.py
index 7df8b706..9cd61e1e 100644
--- a/pybamm/expression_tree/matrix.py
+++ b/pybamm/expression_tree/matrix.py
@@ -22,4 +22,6 @@ class Matrix(pybamm.Array):
     """
 
     def __init__(self, entries, name=None, domain=[]):
+        if name is None:
+            name = "Matrix of shape {!s}".format(entries.shape)
         super().__init__(entries, name=name, domain=domain)
diff --git a/pybamm/expression_tree/symbol.py b/pybamm/expression_tree/symbol.py
index 5b9ae47c..1436246d 100644
--- a/pybamm/expression_tree/symbol.py
+++ b/pybamm/expression_tree/symbol.py
@@ -96,7 +96,9 @@ class Symbol(anytree.NodeMixin):
         which would then mess with loop-checking in the anytree module
         """
         return hash(
-            (self.__class__, self.name) + tuple([child.id for child in self.children])
+            (self.__class__, self.name)
+            + tuple([child.id for child in self.children])
+            + tuple(self.domain)
         )
 
     def render(self):
@@ -181,9 +183,13 @@ class Symbol(anytree.NodeMixin):
         return self._name
 
     def __repr__(self):
-        """returns the string `__class__(id, name, parent expression)`"""
-        return "{!s}({}, {!s}, {!s})".format(
-            self.__class__.__name__, hex(self.id), self._name, self.parent
+        """returns the string `__class__(id, name, children, domain)`"""
+        return "{!s}({}, {!s}, children={!s}, domain={!s})".format(
+            self.__class__.__name__,
+            hex(self.id),
+            self._name,
+            [str(child) for child in self.children],
+            [str(subdomain) for subdomain in self.domain],
         )
 
     def __add__(self, other):
diff --git a/pybamm/expression_tree/vector.py b/pybamm/expression_tree/vector.py
index b2abb123..ad7c2b1e 100644
--- a/pybamm/expression_tree/vector.py
+++ b/pybamm/expression_tree/vector.py
@@ -24,6 +24,8 @@ class Vector(pybamm.Array):
     """
 
     def __init__(self, entries, name=None, domain=[]):
+        if name is None:
+            name = "Vector of shape {!s}".format(entries.shape)
         super().__init__(entries, name=name, domain=domain)
 
 
@@ -46,7 +48,7 @@ class StateVector(pybamm.Symbol):
 
     def __init__(self, y_slice, name=None, domain=[]):
         if name is None:
-            name = str(y_slice)
+            name = "StateVector with slice '{!s}'".format(y_slice)
         super().__init__(name=name, domain=domain)
         self._y_slice = y_slice
 


Test patch:
diff --git a/tests/test_expression_tree/test_matrix.py b/tests/test_expression_tree/test_matrix.py
index 1df04518..65a73f38 100644
--- a/tests/test_expression_tree/test_matrix.py
+++ b/tests/test_expression_tree/test_matrix.py
@@ -22,13 +22,13 @@ class TestMatrix(unittest.TestCase):
         self.assertEqual(self.mat.size, 9)
 
     def test_matrix_evaluate(self):
-        self.assertTrue((self.mat.evaluate() == self.A).all(), self.A)
+        np.testing.assert_array_equal(self.mat.evaluate(), self.A)
 
     def test_matrix_operations(self):
-        self.assertTrue(((self.mat + self.mat).evaluate() == 2 * self.A).all())
-        self.assertTrue(((self.mat - self.mat).evaluate() == 0 * self.A).all())
-        self.assertTrue(
-            ((self.mat * self.vect).evaluate() == np.array([5, 2, 3])).all()
+        np.testing.assert_array_equal((self.mat + self.mat).evaluate(), 2 * self.A)
+        np.testing.assert_array_equal((self.mat - self.mat).evaluate(), 0 * self.A)
+        np.testing.assert_array_equal(
+            (self.mat * self.vect).evaluate(), np.array([5, 2, 3])
         )
 
     def test_matrix_modification(self):
diff --git a/tests/test_expression_tree/test_symbol.py b/tests/test_expression_tree/test_symbol.py
index a7b764d6..2d123a79 100644
--- a/tests/test_expression_tree/test_symbol.py
+++ b/tests/test_expression_tree/test_symbol.py
@@ -141,33 +141,44 @@ class TestSymbol(unittest.TestCase):
         """
         a = pybamm.Symbol("a")
         b = pybamm.Symbol("b")
+        c = pybamm.Symbol("c", domain=["test"])
+        d = pybamm.Symbol("d", domain=["test"])
         hex_regex = r"\-?0x[0-9,a-f]+"
-        self.assertRegex(a.__repr__(), r"Symbol\(" + hex_regex + r", a, None\)")
-        self.assertRegex(b.__repr__(), r"Symbol\(" + hex_regex + r", b, None\)")
         self.assertRegex(
-            (a + b).__repr__(), r"Addition\(" + hex_regex + r", \+, None\)"
+            a.__repr__(),
+            r"Symbol\(" + hex_regex + r", a, children\=\[\], domain\=\[\]\)",
         )
         self.assertRegex(
-            (a + b).children[0].__repr__(), r"Symbol\(" + hex_regex + r", a, a \+ b\)"
+            b.__repr__(),
+            r"Symbol\(" + hex_regex + r", b, children\=\[\], domain\=\[\]\)",
         )
         self.assertRegex(
-            (a + b).children[1].__repr__(), r"Symbol\(" + hex_regex + r", b, a \+ b\)"
+            c.__repr__(),
+            r"Symbol\(" + hex_regex + r", c, children\=\[\], domain\=\['test'\]\)",
         )
         self.assertRegex(
-            (a * b).__repr__(), r"Multiplication\(" + hex_regex + r", \*, None\)"
+            d.__repr__(),
+            r"Symbol\(" + hex_regex + r", d, children\=\[\], domain\=\['test'\]\)",
         )
         self.assertRegex(
-            (a * b).children[0].__repr__(), r"Symbol\(" + hex_regex + r", a, a \* b\)"
+            (a + b).__repr__(),
+            r"Addition\(" + hex_regex + r", \+, children\=\['a', 'b'\], domain=\[\]\)",
         )
         self.assertRegex(
-            (a * b).children[1].__repr__(), r"Symbol\(" + hex_regex + ", b, a \* b\)"
+            (c * d).__repr__(),
+            r"Multiplication\("
+            + hex_regex
+            + r", \*, children\=\['c', 'd'\], domain=\['test'\]\)",
         )
         self.assertRegex(
-            pybamm.grad(a).__repr__(), r"Gradient\(" + hex_regex + ", grad, None\)"
+            pybamm.grad(a).__repr__(),
+            r"Gradient\(" + hex_regex + ", grad, children\=\['a'\], domain=\[\]\)",
         )
         self.assertRegex(
-            pybamm.grad(a).children[0].__repr__(),
-            r"Symbol\(" + hex_regex + ", a, grad\(a\)\)",
+            pybamm.grad(c).__repr__(),
+            r"Gradient\("
+            + hex_regex
+            + ", grad, children\=\['c'\], domain=\['test'\]\)",
         )
 
     def test_symbol_visualise(self):