fft_operator.py 4.8 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
Martin Reinecke's avatar
Martin Reinecke committed
22
from .linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
23
from .fft_operator_support import RGRGTransformation, SphericalTransformation
Jait Dixit's avatar
Jait Dixit committed
24
25


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

    Built-in domain pairs are
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
      - 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".
45
46
    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
47
    target: Space or single-element tuple of Spaces (optional)
48
49
50
51
        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
52
        uniquely determined.
53
54
        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
55
        but for full control, the user should explicitly specify a codomain.
56
57
58

    Attributes
    ----------
59
    domain: Tuple of Spaces
60
61
        The domain of the data that is input by "times" and output by
        "adjoint_times".
62
    target: Tuple of Spaces
63
64
65
        The domain of the data that is output by "times" and input by
        "adjoint_times".
    unitary: bool
66
67
        Returns True if the operator is unitary (currently only the case if
        the domain and codomain are RGSpaces), else False.
68
69
70
71
72
73

    Raises
    ------
    ValueError:
        if "domain" or "target" are not of the proper type.
    """
74

Martin Reinecke's avatar
Martin Reinecke committed
75
    # MR FIXME: target should only be a single DomainObject, not the full tuple
76
77
    def __init__(self, domain, target=None, space=None):
        super(FFTOperator, self).__init__()
78
79

        # Initialize domain and target
Martin Reinecke's avatar
Martin Reinecke committed
80
        self._domain = DomainTuple.make(domain)
81
        if space is None:
Martin Reinecke's avatar
Martin Reinecke committed
82
            if len(self._domain) != 1:
83
84
85
                raise ValueError("need a Field with exactly one domain")
            space = 0
        space = int(space)
Martin Reinecke's avatar
Martin Reinecke committed
86
        if space < 0 or space >= len(self._domain):
87
88
89
90
            raise ValueError("space index out of range")
        self._space = space

        adom = self.domain[self._space]
91
        if target is None:
Martin Reinecke's avatar
Martin Reinecke committed
92
            target = [dom for dom in self.domain]
93
94
            target[self._space] = adom.get_default_codomain()

Martin Reinecke's avatar
Martin Reinecke committed
95
        self._target = DomainTuple.make(target)
Martin Reinecke's avatar
Martin Reinecke committed
96
        atgt = self._target[space]
97
98
        adom.check_codomain(atgt)
        atgt.check_codomain(adom)
Jait Dixit's avatar
Jait Dixit committed
99

Martin Reinecke's avatar
Martin Reinecke committed
100
101
102
103
104
105
106
107
108
109
        if self._target[space].harmonic:
            pdom, hdom = (self._domain, self._target)
        else:
            pdom, hdom = (self._target, self._domain)
        if isinstance(pdom[space], RGSpace):
            self._trafo = RGRGTransformation(pdom, hdom, space)
        else:
            self._trafo = SphericalTransformation(pdom, hdom, space)

    def _times_helper(self, x):
110
        if np.issubdtype(x.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
            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
115
        return res
Jait Dixit's avatar
Jait Dixit committed
116

117
    def _times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
118
        return self._times_helper(x)
119

120
    def _adjoint_times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
121
        return self._times_helper(x)
Jait Dixit's avatar
Jait Dixit committed
122

123
124
125
126
    @property
    def domain(self):
        return self._domain

Jait Dixit's avatar
Jait Dixit committed
127
128
129
130
    @property
    def target(self):
        return self._target

131
132
    @property
    def unitary(self):
Martin Reinecke's avatar
Martin Reinecke committed
133
        return self._trafo.unitary