summaryrefslogtreecommitdiffstats
path: root/Wrappers
diff options
context:
space:
mode:
authorepapoutsellis <epapoutsellis@gmail.com>2019-04-14 20:03:01 +0100
committerepapoutsellis <epapoutsellis@gmail.com>2019-04-14 20:03:01 +0100
commitc6b643e939b0c26e41fea4a86d81178af2481387 (patch)
tree6c77e13eccf1bdd64adb0283352db1b92071bf41 /Wrappers
parent76906f3a5941b45441ee0209605d73ea6fb445f2 (diff)
downloadframework-c6b643e939b0c26e41fea4a86d81178af2481387.tar.gz
framework-c6b643e939b0c26e41fea4a86d81178af2481387.tar.bz2
framework-c6b643e939b0c26e41fea4a86d81178af2481387.tar.xz
framework-c6b643e939b0c26e41fea4a86d81178af2481387.zip
add pnorm method
Diffstat (limited to 'Wrappers')
-rwxr-xr-xWrappers/Python/ccpi/framework/BlockDataContainer.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/Wrappers/Python/ccpi/framework/BlockDataContainer.py b/Wrappers/Python/ccpi/framework/BlockDataContainer.py
index 75ee4b2..4655e1b 100755
--- a/Wrappers/Python/ccpi/framework/BlockDataContainer.py
+++ b/Wrappers/Python/ccpi/framework/BlockDataContainer.py
@@ -302,13 +302,26 @@ class BlockDataContainer(object):
return type(self)(*[el.conjugate() for el in self.containers], shape=self.shape)
## reductions
+
def sum(self, *args, **kwargs):
return numpy.sum([ el.sum(*args, **kwargs) for el in self.containers])
+
def squared_norm(self):
y = numpy.asarray([el.squared_norm() for el in self.containers])
return y.sum()
+
def norm(self):
- return numpy.sqrt(self.squared_norm())
+ return numpy.sqrt(self.squared_norm())
+
+ def pnorm(self, p=2):
+
+ if p==1:
+ return sum(self.abs())
+ elif p==2:
+ return sum([el*el for el in self.containers]).sqrt()
+ else:
+ return ValueError('Not implemented')
+
def copy(self):
'''alias of clone'''
return self.clone()
@@ -467,7 +480,38 @@ class BlockDataContainer(object):
def __itruediv__(self, other):
'''Inline truedivision'''
return self.__idiv__(other)
+
+
+
+
+
+if __name__ == '__main__':
+
+ from ccpi.framework import ImageGeometry, BlockGeometry
+ import numpy
+
+ N, M = 2, 3
+ ig = ImageGeometry(N, M)
+ BG = BlockGeometry(ig, ig)
+
+ U = BG.allocate('random_int')
+
+
+ print ("test sum BDC " )
+ w = U[0].as_array() + U[1].as_array()
+ w1 = sum(U).as_array()
+ numpy.testing.assert_array_equal(w, w1)
+
+ print ("test sum BDC " )
+ z = numpy.sqrt(U[0].as_array()**2 + U[1].as_array()**2)
+ z1 = sum(U**2).sqrt().as_array()
+ numpy.testing.assert_array_equal(z, z1)
+
+
+ z2 = U.pnorm(2)
+
+