Skip to content

Commit

Permalink
added abs and norm and tests
Browse files Browse the repository at this point in the history
renamed readme.md to readme for pypi to read
  • Loading branch information
maekke97 committed Apr 18, 2017
1 parent 1410b5d commit 985dd69
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
29 changes: 27 additions & 2 deletions HierMat/hmat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""hmat.py: :class:`HMat`, :func:`build_hmatrix`, :func:`recursion_build_hmatrix`, :class:`StructureWarning`
"""
import math
import numbers

import numpy
Expand Down Expand Up @@ -33,10 +34,10 @@ class HMat(object):
* ``==`` (equal)
* ``!=`` (not equal)
* ``abs`` (frobenius norm)
.. todo::
* implement ``norm`` and ``__abs__``
* implement ``inv``
"""
Expand Down Expand Up @@ -107,6 +108,30 @@ def __ne__(self, other):
"""
return not self == other

def __abs__(self):
"""Frobenius norm"""
if isinstance(self.content, RMat):
return self.content.norm()
elif isinstance(self.content, numpy.matrix):
return numpy.linalg.norm(self.content)
else:
total = 0
for block in self.blocks:
total += abs(block)**2
return math.sqrt(total)

def norm(self, order=None):
"""Norm of the matrix
:param order: order of the norm (see in :func:`numpy.linalg.norm`)
:return: norm
:rtype: float
"""
if order is None or order == 'fro':
return abs(self)
else:
raise NotImplementedError('Only Frobenius implemented so far')

def __add__(self, other):
"""addition with several types"""
try:
Expand Down
File renamed without changes.
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.2',
version='0.3',
packages=['HierMat'],
url='http://hierarchicalmatrices.readthedocs.io/en/latest/index.html',
download_url='https://github.com/maekke97/HierarchicalMatrices',
Expand Down
15 changes: 15 additions & 0 deletions tests/test_hmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def setUpClass(cls):
cls.consistent2 = HMat(blocks=[cls.cmat1T, cls.cmat2T, cls.cmat3T, cls.cmat4T, cls.cmat5T, cls.cmat6T],
shape=(6, 5), root_index=(0, 0))

def test_abs_norm(self):
self.assertEqual(abs(self.cmat3), 3.0)
self.assertEqual(abs(self.cmat4), 2.0)
self.assertEqual(abs(self.consistent1), 5.4772255750516612)
rmat1 = RMat(numpy.matrix(numpy.ones((3, 1))), numpy.matrix(numpy.ones((3, 1))), max_rank=1)
hmat1 = HMat(content=rmat1, shape=(3, 3), root_index=(0, 0))
hmat2 = HMat(content=rmat1, shape=(3, 3), root_index=(0, 3))
hmat3 = HMat(content=rmat1, shape=(3, 3), root_index=(3, 0))
hmat4 = HMat(content=rmat1, shape=(3, 3), root_index=(3, 3))
hmat = HMat(blocks=[hmat1, hmat2, hmat3, hmat4], shape=(6, 6), root_index=(0, 0))
self.assertEqual(abs(hmat), 6.0)
self.assertEqual(abs(hmat), hmat.norm())
self.assertEqual(abs(hmat), hmat.norm('fro'))
self.assertRaises(NotImplementedError, hmat.norm, 2)

def test_get_item(self):
self.assertEqual(self.consistent1[0], self.cmat1)
self.assertEqual(self.consistent1[0, 0], self.cmat1)
Expand Down

0 comments on commit 985dd69

Please sign in to comment.