From 1410b5d85a34986522ef09bd41c78a166da3143f Mon Sep 17 00:00:00 2001 From: Markus Neumann Date: Tue, 18 Apr 2017 11:49:58 +0200 Subject: [PATCH] add minus, unary minus and tests for both. changed version to 0.2 --- HierMat/hmat.py | 28 +++++++++++++++++++++++++--- setup.py | 2 +- tests/test_hmat.py | 24 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/HierMat/hmat.py b/HierMat/hmat.py index 5ab4cc8..b32213b 100644 --- a/HierMat/hmat.py +++ b/HierMat/hmat.py @@ -26,14 +26,16 @@ class HMat(object): * ``*`` (formatted multiplication) + * ``-a`` (unary minus) + + * ``-`` (formatted subtraction) + * ``==`` (equal) * ``!=`` (not equal) .. todo:: - * implement ``-`` - * implement ``norm`` and ``__abs__`` * implement ``inv`` @@ -182,7 +184,9 @@ def _add_matrix(self, other): for block in self.blocks: start_x = block.root_index[0] - self.root_index[0] start_y = block.root_index[1] - self.root_index[1] - out.blocks.append(block + other[start_x: start_x + block.shape[0], start_y: start_y + block.shape[1]]) + end_x = start_x + block.shape[0] + end_y = start_y + block.shape[1] + out.blocks.append(block + other[start_x: end_x, start_y: end_y]) else: out.content = self.content + other return out @@ -191,6 +195,24 @@ def __radd__(self, other): """Should be commutative so just switch""" return self + other + def __neg__(self): + """Unary minus""" + out = HMat(shape=self.shape, root_index=self.root_index) + if self.content is not None: + out.content = -self.content + else: + out.blocks = [] + for block in self.blocks: + out.blocks.append(-block) + return out + + def __sub__(self, other): + """Subtract other + + :type other: HMat + """ + return self + (-other) + def __mul__(self, other): """multiplication with several types""" if isinstance(other, numpy.matrix): # order is important here! diff --git a/setup.py b/setup.py index b17b259..0711969 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ setup( name='HierMat', - version='0.1', + version='0.2', packages=['HierMat'], url='http://hierarchicalmatrices.readthedocs.io/en/latest/index.html', download_url='https://github.com/maekke97/HierarchicalMatrices', diff --git a/tests/test_hmat.py b/tests/test_hmat.py index 487ef25..ca91599 100644 --- a/tests/test_hmat.py +++ b/tests/test_hmat.py @@ -163,6 +163,30 @@ def test_radd(self): res = addend_hmat.__radd__(mat) self.assertEqual(addend_hmat, res) + def test_neg(self): + negcmat1 = HMat(content=-self.cblock1, shape=(3, 2), root_index=(0, 0)) + negcmat2 = HMat(content=-self.cblock2, shape=(3, 1), root_index=(0, 2)) + negcmat3 = HMat(content=-self.cblock3, shape=(3, 3), root_index=(0, 3)) + negcmat4 = HMat(content=-self.cblock4, shape=(2, 2), root_index=(3, 0)) + negcmat5 = HMat(content=-self.cblock5, shape=(2, 1), root_index=(3, 2)) + negcmat6 = HMat(content=-self.cblock6, shape=(2, 3), root_index=(3, 3)) + negconsistent1 = HMat(blocks=[negcmat1, negcmat2, negcmat3, negcmat4, negcmat5, negcmat6], + shape=(5, 6), root_index=(0, 0)) + self.assertEqual(-self.cmat1, negcmat1) + self.assertEqual(-self.consistent1, negconsistent1) + + def test_minus(self): + zercmat1 = HMat(content=numpy.matrix(numpy.zeros((3, 2))), shape=(3, 2), root_index=(0, 0)) + zercmat2 = HMat(content=numpy.matrix(numpy.zeros((3, 1))), shape=(3, 1), root_index=(0, 2)) + zercmat3 = HMat(content=numpy.matrix(numpy.zeros((3, 3))), shape=(3, 3), root_index=(0, 3)) + zercmat4 = HMat(content=numpy.matrix(numpy.zeros((2, 2))), shape=(2, 2), root_index=(3, 0)) + zercmat5 = HMat(content=numpy.matrix(numpy.zeros((2, 1))), shape=(2, 1), root_index=(3, 2)) + zercmat6 = HMat(content=numpy.matrix(numpy.zeros((2, 3))), shape=(2, 3), root_index=(3, 3)) + zerconsistent1 = HMat(blocks=[zercmat1, zercmat2, zercmat3, zercmat4, zercmat5, zercmat6], + shape=(5, 6), root_index=(0, 0)) + self.assertEqual(self.cmat1 - self.cmat1, zercmat1) + self.assertEqual(self.consistent1 - self.consistent1, zerconsistent1) + def test_repr(self): check = ''.format(content=self.hmat_lvl2.blocks) self.assertEqual(self.hmat_lvl2.__repr__(), check)