diff options
author | epapoutsellis <epapoutsellis@gmail.com> | 2019-05-01 16:36:14 +0100 |
---|---|---|
committer | epapoutsellis <epapoutsellis@gmail.com> | 2019-05-01 16:36:14 +0100 |
commit | af85032142d8280cafdd2e92389c9fcef5d27f0f (patch) | |
tree | ba6ccff9471cf94976c9cda7ae0c048e1c2016f8 /Wrappers | |
parent | 525e6bbcd28cb8c04a41463bf7e9806dbd9b747e (diff) | |
download | framework-af85032142d8280cafdd2e92389c9fcef5d27f0f.tar.gz framework-af85032142d8280cafdd2e92389c9fcef5d27f0f.tar.bz2 framework-af85032142d8280cafdd2e92389c9fcef5d27f0f.tar.xz framework-af85032142d8280cafdd2e92389c9fcef5d27f0f.zip |
tests with dot method
Diffstat (limited to 'Wrappers')
-rwxr-xr-x | Wrappers/Python/ccpi/framework/framework.py | 25 |
1 files 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)) |