slope_operator.py 2.47 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
17

18
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19

20
from ..domain_tuple import DomainTuple
Philipp Arras's avatar
Philipp Arras committed
21 22
from ..domains.log_rg_space import LogRGSpace
from ..domains.unstructured_domain import UnstructuredDomain
23 24 25 26 27
from ..field import Field
from .linear_operator import LinearOperator


class SlopeOperator(LinearOperator):
Philipp Arras's avatar
Philipp Arras committed
28
    """Evaluates a line on a LogRGSpace given slope and y-intercept
29

Philipp Arras's avatar
Philipp Arras committed
30 31
    Slope and y-intercept of this line are the two parameters which are
    defined on an UnstructeredDomain (in this order) which is the domain of
Philipp Arras's avatar
Docs  
Philipp Arras committed
32 33
    the operator. Being a LogRGSpace instance each pixel has a well-defined
    coordinate value.
Philipp Arras's avatar
Philipp Arras committed
34

35
    The y-intercept is defined to be the value at t_0 of the target.
36 37 38

    Parameters
    ----------
Philipp Arras's avatar
Philipp Arras committed
39 40
    target : LogRGSpace
        The target of the operator which needs to be one-dimensional.
41
    """
Philipp Arras's avatar
Philipp Arras committed
42

43
    def __init__(self, target):
44
        self._target = DomainTuple.make(target)
Philipp Arras's avatar
Philipp Arras committed
45 46 47 48 49 50 51
        if len(self._target) > 1:
            raise TypeError
        if len(self._target[0].shape) > 1:
            raise TypeError
        if not isinstance(self._target[0], LogRGSpace):
            raise TypeError
        self._domain = DomainTuple.make(UnstructuredDomain((2,)))
52
        self._capability = self.TIMES | self.ADJOINT_TIMES
Philipp Arras's avatar
Philipp Arras committed
53 54
        pos = self.target[0].get_k_array() - self.target[0].t_0[0]
        self._pos = pos[0, 1:]
55 56 57

    def apply(self, x, mode):
        self._check_input(x, mode)
Philipp Arras's avatar
Philipp Arras committed
58
        inp = x.to_global_data()
59
        if mode == self.TIMES:
Philipp Arras's avatar
Philipp Arras committed
60 61 62 63 64 65 66 67
            res = np.empty(self.target.shape, dtype=x.dtype)
            res[0] = 0
            res[1:] = inp[1] + inp[0]*self._pos
        else:
            res = np.array(
                [np.sum(self._pos*inp[1:]),
                 np.sum(inp[1:])], dtype=x.dtype)
        return Field.from_global_data(self._tgt(mode), res)