From 365ea7153042687dbd1f10a7e61f74fc2eb29580 Mon Sep 17 00:00:00 2001 From: Edoardo Pasca Date: Thu, 7 Mar 2019 08:18:55 +0000 Subject: add scaled operator and block scaled --- .../optimisation/operators/BlockScaledOperator.py | 26 ++++++++++++++++++++++ .../Python/ccpi/optimisation/operators/Operator.py | 4 ++++ .../ccpi/optimisation/operators/ScaledOperator.py | 21 ++++++++++++++++- .../Python/ccpi/optimisation/operators/__init__.py | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Wrappers/Python/ccpi/optimisation/operators/BlockScaledOperator.py (limited to 'Wrappers') diff --git a/Wrappers/Python/ccpi/optimisation/operators/BlockScaledOperator.py b/Wrappers/Python/ccpi/optimisation/operators/BlockScaledOperator.py new file mode 100644 index 0000000..29dacb8 --- /dev/null +++ b/Wrappers/Python/ccpi/optimisation/operators/BlockScaledOperator.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Feb 14 12:36:40 2019 + +@author: ofn77899 +""" +#from ccpi.optimisation.ops import Operator +import numpy +from numbers import Number +import functools +from ccpi.framework import AcquisitionData, ImageData, BlockDataContainer +from ccpi.optimisation.operators import Operator, LinearOperator + + + +class BlockScaledOperator(BlockOperator): + def __init__(self, *args, **kwargs): + super(BlockScaledOperator, self).__init__(*args, **kwargs) + scalar = kwargs.get('scalar',1) + if isinstance (scalar, list) or isinstance(scalar, tuple) or \ + isinstance(scalar, numpy.ndarray): + if len(scalars) != len(self.operators): + raise ValueError('dimensions of scalars and operators do not match') + else: + scalar = [scalar for _ in self.operators] + self.operators = [v * op for op in self.operators] \ No newline at end of file diff --git a/Wrappers/Python/ccpi/optimisation/operators/Operator.py b/Wrappers/Python/ccpi/optimisation/operators/Operator.py index ea08b30..11b9b87 100755 --- a/Wrappers/Python/ccpi/optimisation/operators/Operator.py +++ b/Wrappers/Python/ccpi/optimisation/operators/Operator.py @@ -4,6 +4,7 @@ Created on Tue Mar 5 15:55:56 2019 @author: ofn77899 """ +from ccpi.framework,operators import ScaledOperator class Operator(object): '''Operator that maps from a space X -> Y''' @@ -23,3 +24,6 @@ class Operator(object): raise NotImplementedError def domain_geometry(self): raise NotImplementedError + def __rmul__(self, scalar): + return ScaledOperator(self, scalar) + diff --git a/Wrappers/Python/ccpi/optimisation/operators/ScaledOperator.py b/Wrappers/Python/ccpi/optimisation/operators/ScaledOperator.py index 56f58cd..1181604 100644 --- a/Wrappers/Python/ccpi/optimisation/operators/ScaledOperator.py +++ b/Wrappers/Python/ccpi/optimisation/operators/ScaledOperator.py @@ -2,6 +2,25 @@ from ccpi.optimisation.operators import LinearOperator from numbers import Number class ScaledOperator(LinearOperator): + '''ScaledOperator + + A class to represent the scalar multiplication of an Operator with a scalar. + It holds an operator and a scalar. Basically it returns the multiplication + of the result of direct and adjoint of the operator with the scalar. + For the rest it behaves like the operator it holds. + + Args: + operator (Operator): a Operator or LinearOperator + scalar (Number): a scalar multiplier + Example: + The scaled operator behaves like the following: + sop = ScaledOperator(operator, scalar) + sop.direct(x) = scalar * operator.direct(x) + sop.adjoint(x) = scalar * operator.adjoint(x) + sop.norm() = operator.norm() + sop.range_geometry() = operator.range_geometry() + sop.domain_geometry() = operator.domain_geometry() + ''' def __init__(self, operator, scalar): if not isinstance (scalar, Number): raise TypeError('expected scalar: got {}'.format(type(scalar)) @@ -20,4 +39,4 @@ class ScaledOperator(LinearOperator): return self.operator.range_geometry() def domain_geometry(self): return self.operator.domain_geometry() - + \ No newline at end of file diff --git a/Wrappers/Python/ccpi/optimisation/operators/__init__.py b/Wrappers/Python/ccpi/optimisation/operators/__init__.py index 088f48c..cc307e0 100755 --- a/Wrappers/Python/ccpi/optimisation/operators/__init__.py +++ b/Wrappers/Python/ccpi/optimisation/operators/__init__.py @@ -7,4 +7,6 @@ Created on Tue Mar 5 15:56:27 2019 from .Operator import Operator from .LinearOperator import LinearOperator +from .ScaledOperator import ScaledOperator from .BlockOperator import BlockOperator +from .BlockScaledOperator import BlockScaledOperator -- cgit v1.2.3