lm_space.py 5.6 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

csongor's avatar
csongor committed
19
20
from __future__ import division
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
from .space import Space
theos's avatar
theos committed
22

csongor's avatar
csongor committed
23

Theo Steininger's avatar
Theo Steininger committed
24
class LMSpace(Space):
csongor's avatar
csongor committed
25
26
27
28
29
30
31
32
33
34
35
    """
        ..       __
        ..     /  /
        ..    /  /    __ ____ ___
        ..   /  /   /   _    _   |
        ..  /  /_  /  / /  / /  /
        ..  \___/ /__/ /__/ /__/  space class

        NIFTY subclass for spherical harmonics components, for representations
        of fields on the two-sphere.

36
37
38
39
40
41
        Parameters
        ----------
        lmax : int
            The maximal :math:`l` value of any spherical harmonics
            :math:`Y_{lm}` that is represented in this Space.

42
        Attributes
csongor's avatar
csongor committed
43
44
        ----------
        lmax : int
45
46
47
48
49
            The maximal :math:`l` value of any spherical harmonics
            :math:`Y_{lm}` that is represented in this Space.
        mmax : int
            The maximal :math:`m` value of any spherical harmonic
            :math:`Y_{lm}` that is represented in this Space.
50
51
52
53
54
55
        dim : np.int
            Total number of dimensionality, i.e. the number of pixels.
        harmonic : bool
            Specifies whether the space is a signal or harmonic space.
        shape : tuple of np.ints
            The shape of the space's data array.
csongor's avatar
csongor committed
56
57
58
59
60
61
62

        See Also
        --------
        hp_space : A class for the HEALPix discretization of the sphere [#]_.
        gl_space : A class for the Gauss-Legendre discretization of the
            sphere [#]_.

Theo Steininger's avatar
Theo Steininger committed
63
64
65
66
67
        Raises
        ------
        ValueError
            If given lmax is negative.

68
69
        Notes
        -----
Theo Steininger's avatar
Theo Steininger committed
70
            This implementation implicitly sets the mmax parameter to lmax.
71

csongor's avatar
csongor committed
72
73
74
75
76
77
78
79
80
81
        References
        ----------
        .. [#] K.M. Gorski et al., 2005, "HEALPix: A Framework for
               High-Resolution Discretization and Fast Analysis of Data
               Distributed on the Sphere", *ApJ* 622..759G.
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
    """

Martin Reinecke's avatar
Martin Reinecke committed
82
83
    def __init__(self, lmax):
        super(LMSpace, self).__init__()
84
        self._needed_for_hash += ["_lmax"]
csongor's avatar
csongor committed
85
        self._lmax = self._parse_lmax(lmax)
csongor's avatar
csongor committed
86

87
88
89
    def __repr__(self):
        return ("LMSpace(lmax=%r)" % self.lmax)

csongor's avatar
csongor committed
90
91
92
    @property
    def harmonic(self):
        return True
csongor's avatar
csongor committed
93
94

    @property
95
96
    def shape(self):
        return (self.dim, )
csongor's avatar
csongor committed
97
98

    @property
99
100
    def dim(self):
        l = self.lmax
101
        # the LMSpace consists of the full triangle (including -m's!),
theos's avatar
theos committed
102
103
        # minus two little triangles if mmax < lmax
        # dim = (((2*(l+1)-1)+1)**2/4 - 2 * (l-m)(l-m+1)/2
104
105
        # dim = np.int((l+1)**2 - (l-m)*(l-m+1.))
        # We fix l == m
106
        return np.int((l+1)*(l+1))
csongor's avatar
csongor committed
107

108
    def scalar_dvol(self):
109
        return 1.
csongor's avatar
csongor committed
110

111
    def get_k_length_array(self):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
112
113
114
115
116
117
118
119
120
121
122
        lmax = self.lmax
        ldist = np.empty((self.dim,), dtype=np.float64)
        ldist[0:lmax+1] = np.arange(lmax+1, dtype=np.float64)
        tmp = np.empty((2*lmax+2), dtype=np.float64)
        tmp[0::2] = np.arange(lmax+1)
        tmp[1::2] = np.arange(lmax+1)
        idx = lmax+1
        for l in range(1, lmax+1):
            ldist[idx:idx+2*(lmax+1-l)] = tmp[2*l:]
            idx += 2*(lmax+1-l)
        return ldist
123

124
    def get_unique_k_lengths(self):
Martin Reinecke's avatar
Martin Reinecke committed
125
126
        return np.arange(self.lmax+1, dtype=np.float64)

Martin Reinecke's avatar
Martin Reinecke committed
127
128
129
130
131
132
133
134
    @staticmethod
    def _kernel(x, sigma):
        res = x+1.
        res *= x
        res *= -0.5*sigma*sigma
        np.exp(res, out=res)
        return res

135
    def get_fft_smoothing_kernel_function(self, sigma):
Theo Steininger's avatar
Theo Steininger committed
136
137
138
        # cf. "All-sky convolution for polarimetry experiments"
        # by Challinor et al.
        # http://arxiv.org/abs/astro-ph/0008228
Martin Reinecke's avatar
Martin Reinecke committed
139
        return lambda x: self._kernel(x, sigma)
140

csongor's avatar
csongor committed
141
142
    @property
    def lmax(self):
Theo Steininger's avatar
Theo Steininger committed
143
144
        """ Returns the maximal :math:`l` value of any spherical harmonics
        :math:`Y_{lm}` that is represented in this Space.
145
        """
csongor's avatar
csongor committed
146
147
148
149
        return self._lmax

    @property
    def mmax(self):
Theo Steininger's avatar
Theo Steininger committed
150
151
152
153
154
        """ Returns the maximal :math:`m` value of any spherical harmonic
        :math:`Y_{lm}` that is represented in this Space. As :math:`m` goes
        from :math:`-l` to :math:`l` for every :math:`l` this just returns the
        same as lmax.

155
156
        See Also
        --------
Theo Steininger's avatar
Theo Steininger committed
157
158
159
        lmax : Returns the maximal :math:`l`-value of the spherical harmonics
            being used.

160
        """
Theo Steininger's avatar
Theo Steininger committed
161

162
        return self._lmax
csongor's avatar
csongor committed
163
164
165

    def _parse_lmax(self, lmax):
        lmax = np.int(lmax)
166
167
        if lmax < 0:
            raise ValueError("lmax must be >=0.")
csongor's avatar
csongor committed
168
        return lmax
Martin Reinecke's avatar
Martin Reinecke committed
169
170
171
172
173
174
175
176
177

    def get_default_codomain(self):
        from .. import GLSpace
        return GLSpace(self.lmax+1, self.mmax*2+1)

    def check_codomain(self, codomain):
        from .. import GLSpace, HPSpace
        if not isinstance(codomain, (GLSpace, HPSpace)):
            raise TypeError("codomain must be a GLSpace.")