linear_operator.py 5.39 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
    _backwards = 6
Martin Reinecke's avatar
Martin Reinecke committed
36
    _all_ops = 15
Martin Reinecke's avatar
Martin Reinecke committed
37
38
39
40
41
    TIMES = 1
    ADJOINT_TIMES = 2
    INVERSE_TIMES = 4
    ADJOINT_INVERSE_TIMES = 8
    INVERSE_ADJOINT_TIMES = 8
42

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

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

49
50
    def __init__(self):
        pass
51

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

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

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

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

82
83
84
85
86
87
88
89
90
91
    @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
92
        return NotImplemented
93

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

99
100
101
    def __rmul__(self, other):
        from .chain_operator import ChainOperator
        other = self._toOperator(other, self.target)
Martin Reinecke's avatar
Martin Reinecke committed
102
        return ChainOperator.make([other, self])
103

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

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

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

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

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

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

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

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

Martin Reinecke's avatar
Martin Reinecke committed
137
138
139
140
141
142
143
144
    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)
145

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

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

Martin Reinecke's avatar
Martin Reinecke committed
152
153
154
155
156
    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")
157

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

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