From af85032142d8280cafdd2e92389c9fcef5d27f0f Mon Sep 17 00:00:00 2001 From: epapoutsellis Date: Wed, 1 May 2019 16:36:14 +0100 Subject: tests with dot method --- Wrappers/Python/ccpi/framework/framework.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Wrappers/Python/ccpi/framework/framework.py b/Wrappers/Python/ccpi/framework/framework.py index 7516447..a8a0ab4 100755 --- a/Wrappers/Python/ccpi/framework/framework.py +++ b/Wrappers/Python/ccpi/framework/framework.py @@ -758,12 +758,33 @@ class DataContainer(object): def norm(self): '''return the euclidean norm of the DataContainer viewed as a vector''' return numpy.sqrt(self.squared_norm()) + + +# def dot(self, other, *args, **kwargs): +# '''return the inner product of 2 DataContainers viewed as vectors''' +# if self.shape == other.shape: +# return numpy.dot(self.as_array().ravel(), other.as_array().ravel()) +# else: +# raise ValueError('Shapes are not aligned: {} != {}'.format(self.shape, other.shape)) + def dot(self, other, *args, **kwargs): '''return the inner product of 2 DataContainers viewed as vectors''' + method = kwargs.get('method', 'reduce') if self.shape == other.shape: - return numpy.dot(self.as_array().ravel(), other.as_array().ravel()) + # return (self*other).sum() + if method == 'numpy': + return numpy.dot(self.as_array().ravel(), other.as_array()) + elif method == 'reduce': + # see https://github.com/vais-ral/CCPi-Framework/pull/273 + # notice that Python seems to be smart enough to use + # the appropriate type to hold the result of the reduction + sf = reduce(lambda x,y: x + y[0]*y[1], + zip(self.as_array().ravel(), + other.as_array().ravel()), + 0) + return sf else: - raise ValueError('Shapes are not aligned: {} != {}'.format(self.shape, other.shape)) + raise ValueError('Shapes are not aligned: {} != {}'.format(self.shape, other.shape)) -- cgit v1.2.3