Skip to content

Commit

Permalink
add minus, unary minus and tests for both.
Browse files Browse the repository at this point in the history
changed version to 0.2
  • Loading branch information
maekke97 committed Apr 18, 2017
1 parent 8b1da51 commit 1410b5d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
28 changes: 25 additions & 3 deletions HierMat/hmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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
Expand All @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions tests/test_hmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<HMat with {content}>'.format(content=self.hmat_lvl2.blocks)
self.assertEqual(self.hmat_lvl2.__repr__(), check)
Expand Down

0 comments on commit 1410b5d

Please sign in to comment.