lm_space.py 4.61 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 .structured_domain import StructuredDomain
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
22
from ..field import Field, exp
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
23
from .. import dobj
Theo Steininger's avatar
Theo Steininger committed
24

csongor's avatar
csongor committed
25

Martin Reinecke's avatar
Martin Reinecke committed
26
class LMSpace(StructuredDomain):
Martin Reinecke's avatar
Martin Reinecke committed
27
    """NIFTy subclass for spherical harmonics components, for representations
Martin Reinecke's avatar
Martin Reinecke committed
28
29
30
31
32
33
34
    of fields on the two-sphere.

    Parameters
    ----------
    lmax : int
        The maximum :math:`l` value of any spherical harmonics
        :math:`Y_{lm}` that is represented in this Space.
35
36
37
38
39
40
41
        Must be >=0.

    mmax : int *optional*
        The maximum :math:`m` value of any spherical harmonics
        :math:`Y_{lm}` that is represented in this Space.
        If not supplied, it is set to lmax.
        Must be >=0 and <=lmax.
Martin Reinecke's avatar
Martin Reinecke committed
42
43
44

    See Also
    --------
Martin Reinecke's avatar
Martin Reinecke committed
45
    HPSpace, GLSpace
Martin Reinecke's avatar
Martin Reinecke committed
46
47
48
49
50
51
52
53
54

    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>`_
csongor's avatar
csongor committed
55
56
    """

57
    def __init__(self, lmax, mmax=None):
Martin Reinecke's avatar
Martin Reinecke committed
58
        super(LMSpace, self).__init__()
59
60
61
62
63
64
65
66
67
        self._needed_for_hash += ["_lmax", "_mmax"]
        self._lmax = np.int(lmax)
        if self._lmax < 0:
            raise ValueError("lmax must be >=0.")
        if mmax is None:
            mmax = self._lmax
        self._mmax = np.int(mmax)
        if self._mmax < 0 or self._mmax > self._lmax:
            raise ValueError("mmax must be >=0 and <=lmax.")
csongor's avatar
csongor committed
68

69
    def __repr__(self):
70
        return ("LMSpace(lmax=%r, mmax=%r)" % (self.lmax, self.mmax))
71

csongor's avatar
csongor committed
72
73
74
    @property
    def harmonic(self):
        return True
csongor's avatar
csongor committed
75
76

    @property
77
    def shape(self):
Martin Reinecke's avatar
Martin Reinecke committed
78
        return (self.size, )
csongor's avatar
csongor committed
79
80

    @property
Martin Reinecke's avatar
Martin Reinecke committed
81
    def size(self):
82
83
        l = self._lmax
        m = self._mmax
84
        # the LMSpace consists of the full triangle (including -m's!),
Theo Steininger's avatar
Theo Steininger committed
85
        # minus two little triangles if mmax < lmax
86
        return (l+1)**2 - (l-m)*(l-m+1)
csongor's avatar
csongor committed
87

88
    def scalar_dvol(self):
89
        return 1.
csongor's avatar
csongor committed
90

91
    def get_k_length_array(self):
92
93
        lmax = self._lmax
        mmax = self._mmax
Martin Reinecke's avatar
Martin Reinecke committed
94
        ldist = np.empty((self.size,), dtype=np.float64)
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
95
96
97
98
99
        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
100
101
102
        for m in range(1, mmax+1):
            ldist[idx:idx+2*(lmax+1-m)] = tmp[2*m:]
            idx += 2*(lmax+1-m)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
103
        return Field((self,), dobj.from_global_data(ldist))
104

105
    def get_unique_k_lengths(self):
Martin Reinecke's avatar
Martin Reinecke committed
106
107
        return np.arange(self.lmax+1, dtype=np.float64)

Martin Reinecke's avatar
Martin Reinecke committed
108
109
    @staticmethod
    def _kernel(x, sigma):
110
111
112
        # 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
113
114
115
        res = x+1.
        res *= x
        res *= -0.5*sigma*sigma
116
        exp(res, out=res)
Martin Reinecke's avatar
Martin Reinecke committed
117
118
        return res

119
    def get_fft_smoothing_kernel_function(self, sigma):
Martin Reinecke's avatar
Martin Reinecke committed
120
        return lambda x: self._kernel(x, sigma)
121

csongor's avatar
csongor committed
122
123
    @property
    def lmax(self):
Martin Reinecke's avatar
Martin Reinecke committed
124
        """ Returns the maximum :math:`l` value of any spherical harmonic
Theo Steininger's avatar
Theo Steininger committed
125
        :math:`Y_{lm}` that is represented in this Space.
126
        """
csongor's avatar
csongor committed
127
128
129
130
        return self._lmax

    @property
    def mmax(self):
Martin Reinecke's avatar
Martin Reinecke committed
131
132
        """ Returns the maximum :math:`m` value of any spherical harmonic
        :math:`Y_{lm}` that is represented in this Space.
133
        """
134
        return self._mmax
Martin Reinecke's avatar
Martin Reinecke committed
135
136
137
138
139
140
141
142

    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)):
143
            raise TypeError("codomain must be a GLSpace or HPSpace.")