fft_operator.py 4.03 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

Martin Reinecke's avatar
Martin Reinecke committed
19 20 21
import numpy as np
from .. import DomainTuple
from ..spaces import RGSpace
22
from ..utilities import infer_space
Martin Reinecke's avatar
Martin Reinecke committed
23
from .linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
24
from .fft_operator_support import RGRGTransformation, SphericalTransformation
Jait Dixit's avatar
Jait Dixit committed
25 26


Jait Dixit's avatar
Jait Dixit committed
27
class FFTOperator(LinearOperator):
28 29 30
    """Transforms between a pair of position and harmonic domains.

    Built-in domain pairs are
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      - a harmonic and a non-harmonic RGSpace (with matching distances)
      - a HPSpace and a LMSpace
      - a GLSpace and a LMSpace
    Within a domain pair, both orderings are possible.

    The operator provides a "times" and an "adjoint_times" operation.
    For a pair of RGSpaces, the "adjoint_times" operation is equivalent to
    "inverse_times"; for the sphere-related domains this is not the case, since
    the operator matrix is not square.

    Parameters
    ----------
    domain: Space or single-element tuple of Spaces
        The domain of the data that is input by "times" and output by
        "adjoint_times".
46 47
    space: the index of the space on which the operator should act
        If None, it is set to 0 if domain contains exactly one space
48
    target: Space or single-element tuple of Spaces (optional)
49 50 51 52
        The domain of the data that is output by "times" and input by
        "adjoint_times".
        If omitted, a co-domain will be chosen automatically.
        Whenever "domain" is an RGSpace, the codomain (and its parameters) are
Martin Reinecke's avatar
Martin Reinecke committed
53
        uniquely determined.
54 55
        For GLSpace, HPSpace, and LMSpace, a sensible (but not unique)
        co-domain is chosen that should work satisfactorily in most situations,
Martin Reinecke's avatar
Martin Reinecke committed
56
        but for full control, the user should explicitly specify a codomain.
57
    """
58

59 60
    def __init__(self, domain, target=None, space=None):
        super(FFTOperator, self).__init__()
61 62

        # Initialize domain and target
Martin Reinecke's avatar
Martin Reinecke committed
63
        self._domain = DomainTuple.make(domain)
64
        self._space = infer_space(self._domain, space)
65 66

        adom = self.domain[self._space]
67
        if target is None:
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
68
            target = adom.get_default_codomain()
69

Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
70 71 72 73 74
        self._target = [dom for dom in self.domain]
        self._target[self._space] = target
        self._target = DomainTuple.make(self._target)
        adom.check_codomain(target)
        target.check_codomain(adom)
Jait Dixit's avatar
Jait Dixit committed
75

76
        if self._target[self._space].harmonic:
Martin Reinecke's avatar
Martin Reinecke committed
77 78 79
            pdom, hdom = (self._domain, self._target)
        else:
            pdom, hdom = (self._target, self._domain)
80 81
        if isinstance(pdom[self._space], RGSpace):
            self._trafo = RGRGTransformation(pdom, hdom, self._space)
Martin Reinecke's avatar
Martin Reinecke committed
82
        else:
83
            self._trafo = SphericalTransformation(pdom, hdom, self._space)
Martin Reinecke's avatar
Martin Reinecke committed
84

Martin Reinecke's avatar
Martin Reinecke committed
85 86
    def apply(self, x, mode):
        self._check_input(x, mode)
87
        if np.issubdtype(x.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
88 89 90 91
            res = (self._trafo.transform(x.real) +
                   1j * self._trafo.transform(x.imag))
        else:
            res = self._trafo.transform(x)
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
92
        return res
Jait Dixit's avatar
Jait Dixit committed
93

94 95 96 97
    @property
    def domain(self):
        return self._domain

Jait Dixit's avatar
Jait Dixit committed
98 99 100 101
    @property
    def target(self):
        return self._target

102
    @property
Martin Reinecke's avatar
Martin Reinecke committed
103 104 105 106 107
    def capability(self):
        res = self.TIMES | self.ADJOINT_TIMES
        if self._trafo.unitary:
            res |= self.INVERSE_TIMES | self.ADJOINT_INVERSE_TIMES
        return res