linear_operator.py 5.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

19
import abc
Martin Reinecke's avatar
Martin Reinecke committed
20
from ..utilities import NiftyMeta
Martin Reinecke's avatar
Martin Reinecke committed
21
from ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
22
from future.utils import with_metaclass
23
import numpy as np
24
25


26
class LinearOperator(with_metaclass(
Martin Reinecke's avatar
Martin Reinecke committed
27
        NiftyMeta, type('NewBase', (object,), {}))):
Theo Steininger's avatar
Theo Steininger committed
28

Martin Reinecke's avatar
Martin Reinecke committed
29
30
31
32
33
34
    _validMode = (False, True, True, False, True, False, False, False, True)
    _inverseMode = (0, 4, 8, 0, 1, 0, 0, 0, 2)
    _inverseCapability = (0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15)
    _adjointMode = (0, 2, 1, 0, 8, 0, 0, 0, 4)
    _adjointCapability = (0, 2, 1, 3, 8, 10, 9, 11, 4, 6, 5, 7, 12, 14, 13, 15)
    _addInverse = (0, 5, 10, 15, 5, 5, 15, 15, 10, 15, 10, 15, 15, 15, 15, 15)
Martin Reinecke's avatar
Martin Reinecke committed
35
36
37
38
39
40
    _backwards = 6
    TIMES = 1
    ADJOINT_TIMES = 2
    INVERSE_TIMES = 4
    ADJOINT_INVERSE_TIMES = 8
    INVERSE_ADJOINT_TIMES = 8
41

Martin Reinecke's avatar
Martin Reinecke committed
42
43
    def _dom(self, mode):
        return self.domain if (mode & 9) else self.target
44

Martin Reinecke's avatar
Martin Reinecke committed
45
46
    def _tgt(self, mode):
        return self.domain if (mode & 6) else self.target
47

48
49
    def __init__(self):
        pass
50

51
    @abc.abstractproperty
52
    def domain(self):
53
        """
Martin Reinecke's avatar
Martin Reinecke committed
54
        domain : DomainTuple
Theo Steininger's avatar
Theo Steininger committed
55
56
            The domain on which the Operator's input Field lives.
            Every Operator which inherits from the abstract LinearOperator
57
58
            base class must have this attribute.
        """
59
        raise NotImplementedError
60
61
62

    @abc.abstractproperty
    def target(self):
63
        """
Martin Reinecke's avatar
Martin Reinecke committed
64
        target : DomainTuple
Theo Steininger's avatar
Theo Steininger committed
65
66
            The domain on which the Operator's output Field lives.
            Every Operator which inherits from the abstract LinearOperator
67
68
            base class must have this attribute.
        """
69
70
        raise NotImplementedError

Martin Reinecke's avatar
Martin Reinecke committed
71
72
73
74
    @property
    def inverse(self):
        from .inverse_operator import InverseOperator
        return InverseOperator(self)
75

Martin Reinecke's avatar
Martin Reinecke committed
76
77
78
79
    @property
    def adjoint(self):
        from .adjoint_operator import AdjointOperator
        return AdjointOperator(self)
80

81
82
83
84
85
86
87
88
89
90
    @staticmethod
    def _toOperator(thing, dom):
        from .diagonal_operator import DiagonalOperator
        from .scaling_operator import ScalingOperator
        if isinstance(thing, LinearOperator):
            return thing
        if isinstance(thing, Field):
            return DiagonalOperator(thing)
        if np.isscalar(thing):
            return ScalingOperator(thing, dom)
Martin Reinecke's avatar
Martin Reinecke committed
91
        return NotImplemented
92

Martin Reinecke's avatar
Martin Reinecke committed
93
94
    def __mul__(self, other):
        from .chain_operator import ChainOperator
95
        other = self._toOperator(other, self.domain)
Martin Reinecke's avatar
Martin Reinecke committed
96
        return ChainOperator(self, other)
97

98
99
100
101
102
    def __rmul__(self, other):
        from .chain_operator import ChainOperator
        other = self._toOperator(other, self.target)
        return ChainOperator(other, self)

Martin Reinecke's avatar
Martin Reinecke committed
103
104
    def __add__(self, other):
        from .sum_operator import SumOperator
105
        other = self._toOperator(other, self.domain)
Martin Reinecke's avatar
Martin Reinecke committed
106
        return SumOperator(self, other)
107

108
109
110
    def __radd__(self, other):
        return self.__add__(other)

Martin Reinecke's avatar
Martin Reinecke committed
111
112
    def __sub__(self, other):
        from .sum_operator import SumOperator
113
        other = self._toOperator(other, self.domain)
Martin Reinecke's avatar
Martin Reinecke committed
114
        return SumOperator(self, other, neg=True)
115

Martin Reinecke's avatar
Martin Reinecke committed
116
    # MR FIXME: this might be more complicated ...
117
118
119
120
121
    def __rsub__(self, other):
        from .sum_operator import SumOperator
        other = self._toOperator(other, self.domain)
        return SumOperator(other, self, neg=True)

Martin Reinecke's avatar
Martin Reinecke committed
122
123
    def supports(self, ops):
        return False
124

Martin Reinecke's avatar
Martin Reinecke committed
125
126
127
    @abc.abstractproperty
    def capability(self):
        raise NotImplementedError
128

Martin Reinecke's avatar
Martin Reinecke committed
129
130
131
132
133
134
    @abc.abstractmethod
    def apply(self, x, mode):
        raise NotImplementedError

    def __call__(self, x):
        return self.apply(x, self.TIMES)
135

Martin Reinecke's avatar
Martin Reinecke committed
136
137
138
139
140
141
142
143
    def times(self, x):
        return self.apply(x, self.TIMES)

    def inverse_times(self, x):
        return self.apply(x, self.INVERSE_TIMES)

    def adjoint_times(self, x):
        return self.apply(x, self.ADJOINT_TIMES)
144

Martin Reinecke's avatar
Martin Reinecke committed
145
146
    def adjoint_inverse_times(self, x):
        return self.apply(x, self.ADJOINT_INVERSE_TIMES)
147

Martin Reinecke's avatar
Martin Reinecke committed
148
149
    def inverse_adjoint_times(self, x):
        return self.apply(x, self.ADJOINT_INVERSE_TIMES)
150

Martin Reinecke's avatar
Martin Reinecke committed
151
152
153
154
155
    def _check_mode(self, mode):
        if not self._validMode[mode]:
            raise ValueError("invalid operator mode specified")
        if mode & self.capability == 0:
            raise ValueError("requested operator mode is not supported")
156

Martin Reinecke's avatar
Martin Reinecke committed
157
    def _check_input(self, x, mode):
158
        if not isinstance(x, Field):
Martin Reinecke's avatar
updates  
Martin Reinecke committed
159
            raise ValueError("supplied object is not a `Field`.")
160

Martin Reinecke's avatar
Martin Reinecke committed
161
162
163
164
        self._check_mode(mode)
        if x.domain != self._dom(mode):
                raise ValueError("The operator's and and field's domains "
                                 "don't match.")