operator_adapter.py 2.09 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
16
17
18
19
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

from .linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
20
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
22


Martin Reinecke's avatar
Martin Reinecke committed
23
24
class OperatorAdapter(LinearOperator):
    """Class representing the inverse and/or adjoint of another operator."""
Martin Reinecke's avatar
Martin Reinecke committed
25

Martin Reinecke's avatar
Martin Reinecke committed
26
27
    def __init__(self, op, mode):
        super(OperatorAdapter, self).__init__()
Martin Reinecke's avatar
Martin Reinecke committed
28
        self._op = op
Martin Reinecke's avatar
Martin Reinecke committed
29
30
31
        self._mode = int(mode)
        if self._mode < 1 or self._mode > 3:
            raise ValueError("invalid mode")
Martin Reinecke's avatar
Martin Reinecke committed
32
33
34

    @property
    def domain(self):
Martin Reinecke's avatar
Martin Reinecke committed
35
        return self._op._dom(1 << self._mode)
Martin Reinecke's avatar
Martin Reinecke committed
36
37
38

    @property
    def target(self):
Martin Reinecke's avatar
Martin Reinecke committed
39
        return self._op._tgt(1 << self._mode)
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42

    @property
    def capability(self):
Martin Reinecke's avatar
Martin Reinecke committed
43
        return self._capTable[self._mode][self._op.capability]
Martin Reinecke's avatar
Martin Reinecke committed
44

Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
    def _flip_modes(self, mode):
        newmode = mode ^ self._mode
        return self._op if newmode == 0 else OperatorAdapter(self._op, newmode)
48

Martin Reinecke's avatar
Martin Reinecke committed
49
    def apply(self, x, mode):
Martin Reinecke's avatar
Martin Reinecke committed
50
        return self._op.apply(x, self._modeTable[self._mode][self._ilog[mode]])
Martin Reinecke's avatar
Martin Reinecke committed
51
52

    def draw_sample(self, dtype=np.float64):
Martin Reinecke's avatar
Martin Reinecke committed
53
54
55
        if self._mode & self.INVERSE_BIT:
            return self._op.inverse_draw_sample(dtype)
        return self._op.draw_sample(dtype)
Martin Reinecke's avatar
Martin Reinecke committed
56
57

    def inverse_draw_sample(self, dtype=np.float64):
Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
        if self._mode & self.INVERSE_BIT:
            return self._op.draw_sample(dtype)
        return self._op.inverse_draw_sample(dtype)