fft_operator.py 6.43 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
from ... import Field, nifty_utilities as utilities
Martin Reinecke's avatar
Martin Reinecke committed
20
from ...spaces import RGSpace, GLSpace, HPSpace, LMSpace
21

Martin Reinecke's avatar
Martin Reinecke committed
22
from ..linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
23
24
25
26
27
from .fft_operator_support import RGRGTransformation,\
                                  LMGLTransformation,\
                                  LMHPTransformation,\
                                  GLLMTransformation,\
                                  HPLMTransformation
Jait Dixit's avatar
Jait Dixit committed
28
29


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

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

    Attributes
    ----------
    domain: Tuple of Spaces (with one entry)
        The domain of the data that is input by "times" and output by
        "adjoint_times".
    target: Tuple of Spaces (with one entry)
        The domain of the data that is output by "times" and input by
        "adjoint_times".
    unitary: bool
68
69
        Returns True if the operator is unitary (currently only the case if
        the domain and codomain are RGSpaces), else False.
70
71
72
73
74

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

76
    """
77

78
    # ---Class attributes---
79

80
81
82
    default_codomain_dictionary = {RGSpace: RGSpace,
                                   HPSpace: LMSpace,
                                   GLSpace: LMSpace,
83
                                   LMSpace: GLSpace,
84
85
86
87
88
89
90
91
92
                                   }

    transformation_dictionary = {(RGSpace, RGSpace): RGRGTransformation,
                                 (HPSpace, LMSpace): HPLMTransformation,
                                 (GLSpace, LMSpace): GLLMTransformation,
                                 (LMSpace, HPSpace): LMHPTransformation,
                                 (LMSpace, GLSpace): LMGLTransformation
                                 }

Jait Dixit's avatar
Jait Dixit committed
93
94
    # ---Overwritten properties and methods---

Martin Reinecke's avatar
more    
Martin Reinecke committed
95
    def __init__(self, domain, target=None, default_spaces=None):
96
        super(FFTOperator, self).__init__(default_spaces)
97
98

        # Initialize domain and target
99
        self._domain = self._parse_domain(domain)
100
        if len(self.domain) != 1:
101
102
            raise ValueError("TransformationOperator accepts only exactly one "
                             "space as input domain.")
Jait Dixit's avatar
Jait Dixit committed
103

104
        if target is None:
Martin Reinecke's avatar
Martin Reinecke committed
105
            target = (self.domain[0].get_default_codomain(), )
Jait Dixit's avatar
Jait Dixit committed
106
        self._target = self._parse_domain(target)
107
108
109
        if len(self.target) != 1:
            raise ValueError("TransformationOperator accepts only exactly one "
                             "space as output target.")
Martin Reinecke's avatar
Martin Reinecke committed
110
111
        self.domain[0].check_codomain(self.target[0])
        self.target[0].check_codomain(self.domain[0])
Jait Dixit's avatar
Jait Dixit committed
112

113
        # Create transformation instances
114
        forward_class = self.transformation_dictionary[
115
                (self.domain[0].__class__, self.target[0].__class__)]
116
        backward_class = self.transformation_dictionary[
117
118
                (self.target[0].__class__, self.domain[0].__class__)]

119
        self._forward_transformation = forward_class(
Martin Reinecke's avatar
Martin Reinecke committed
120
            self.domain[0], self.target[0])
121

122
        self._backward_transformation = backward_class(
Martin Reinecke's avatar
Martin Reinecke committed
123
            self.target[0], self.domain[0])
Jait Dixit's avatar
Jait Dixit committed
124

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
125
    def _times_helper(self, x, spaces, other, trafo):
126
        if spaces is None:
127
128
129
130
            # this case means that x lives on only one space, which is
            # identical to the space in the domain of `self`. Otherwise the
            # input check of LinearOperator would have failed.
            axes = x.domain_axes[0]
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
131
            result_domain = other
132
        else:
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
133
            spaces = utilities.cast_axis_to_tuple(spaces, len(x.domain))
134
            result_domain = list(x.domain)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
135
136
            result_domain[spaces[0]] = other[0]
            axes = x.domain_axes[spaces[0]]
137

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
138
139
140
141
142
        new_val, fct = trafo.transform(x.val, axes=axes)
        res = Field(result_domain, new_val, copy=False)
        if fct != 1.:
            res *= fct
        return res
Jait Dixit's avatar
Jait Dixit committed
143

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
144
145
146
    def _times(self, x, spaces):
        return self._times_helper(x, spaces, self.target,
                                  self._forward_transformation)
147

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
148
149
150
    def _adjoint_times(self, x, spaces):
        return self._times_helper(x, spaces, self.domain,
                                  self._backward_transformation)
Jait Dixit's avatar
Jait Dixit committed
151
152
153

    # ---Mandatory properties and methods---

154
155
156
157
    @property
    def domain(self):
        return self._domain

Jait Dixit's avatar
Jait Dixit committed
158
159
160
161
    @property
    def target(self):
        return self._target

162
163
    @property
    def unitary(self):
164
165
        return (self._forward_transformation.unitary and
                self._backward_transformation.unitary)