lm_space.py 6.14 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
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2020 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
17

csongor's avatar
csongor committed
18
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19

20
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
21
from .structured_domain import StructuredDomain
Theo Steininger's avatar
Theo Steininger committed
22

csongor's avatar
csongor committed
23

Martin Reinecke's avatar
Martin Reinecke committed
24
class LMSpace(StructuredDomain):
Martin Reinecke's avatar
Martin Reinecke committed
25
    """Represents a set of spherical harmonic coefficients.
Martin Reinecke's avatar
Martin Reinecke committed
26

Martin Reinecke's avatar
Martin Reinecke committed
27
28
    Its harmonic partner spaces are :class:`~nifty7.domains.hp_space.HPSpace`
    and :class:`~nifty7.domains.gl_space.GLSpace`.
Martin Reinecke's avatar
Martin Reinecke committed
29
30
31
32

    Parameters
    ----------
    lmax : int
Martin Reinecke's avatar
Martin Reinecke committed
33
34
        The maximum :math:`l` value of any spherical harmonic coefficient
        :math:`a_{lm}` that is represented by this object.
Martin Reinecke's avatar
Martin Reinecke committed
35
        Must be :math:`\\ge 0`.
Martin Reinecke's avatar
Martin Reinecke committed
36
37
38
39
40

    mmax : int, optional
        The maximum :math:`m` value of any spherical harmonic coefficient
        :math:`a_{lm}` that is represented by this object.
        If not supplied, it is set to `lmax`.
Martin Reinecke's avatar
Martin Reinecke committed
41
        Must be :math:`\\ge 0` and :math:`\\le` `lmax`.
csongor's avatar
csongor committed
42
43
    """

Martin Reinecke's avatar
Martin Reinecke committed
44
45
    _needed_for_hash = ["_lmax", "_mmax"]

46
    def __init__(self, lmax, mmax=None):
Martin Reinecke's avatar
Martin Reinecke committed
47
        self._lmax = int(lmax)
48
49
50
51
        if self._lmax < 0:
            raise ValueError("lmax must be >=0.")
        if mmax is None:
            mmax = self._lmax
Martin Reinecke's avatar
Martin Reinecke committed
52
        self._mmax = int(mmax)
53
54
        if self._mmax < 0 or self._mmax > self._lmax:
            raise ValueError("mmax must be >=0 and <=lmax.")
csongor's avatar
csongor committed
55

56
    def __repr__(self):
Martin Reinecke's avatar
Martin Reinecke committed
57
        return "LMSpace(lmax={}, mmax={})".format(self.lmax, self.mmax)
58

csongor's avatar
csongor committed
59
60
61
    @property
    def harmonic(self):
        return True
csongor's avatar
csongor committed
62
63

    @property
64
    def shape(self):
Martin Reinecke's avatar
Martin Reinecke committed
65
        return (self.size,)
csongor's avatar
csongor committed
66
67

    @property
Martin Reinecke's avatar
Martin Reinecke committed
68
    def size(self):
Martin Reinecke's avatar
Martin Reinecke committed
69
        l, m = self._lmax, self._mmax
70
        # the LMSpace consists of the full triangle (including -m's!),
Theo Steininger's avatar
Theo Steininger committed
71
        # minus two little triangles if mmax < lmax
72
        return (l+1)**2 - (l-m)*(l-m+1)
csongor's avatar
csongor committed
73

Martin Reinecke's avatar
Martin Reinecke committed
74
    @property
75
    def scalar_dvol(self):
76
        return 1.
csongor's avatar
csongor committed
77

78
    def get_k_length_array(self):
79
80
        lmax = self._lmax
        mmax = self._mmax
Martin Reinecke's avatar
Martin Reinecke committed
81
        ldist = np.empty((self.size,), dtype=np.float64)
Martin Reinecke's avatar
stage1  
Martin Reinecke committed
82
83
84
85
86
        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
87
88
89
        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
Martin Reinecke committed
90
        return Field.from_raw(self, ldist)
91

92
    def get_unique_k_lengths(self):
Martin Reinecke's avatar
Martin Reinecke committed
93
94
        return np.arange(self.lmax+1, dtype=np.float64)

Martin Reinecke's avatar
Martin Reinecke committed
95
96
    @staticmethod
    def _kernel(x, sigma):
97
98
        # cf. "All-sky convolution for polarimetry experiments"
        # by Challinor et al.
Martin Reinecke's avatar
Martin Reinecke committed
99
        # https://arxiv.org/abs/astro-ph/0008228
100
        from ..sugar import exp
Martin Reinecke's avatar
Martin Reinecke committed
101
        return exp((x+1.) * x * (-0.5*sigma*sigma))
Martin Reinecke's avatar
Martin Reinecke committed
102

103
    def get_fft_smoothing_kernel_function(self, sigma):
Martin Reinecke's avatar
Martin Reinecke committed
104
        return lambda x: self._kernel(x, sigma)
105

106
107
108
    def get_conv_kernel_from_func(self, func):
        """Creates a convolution kernel defined by a function.

Martin Reinecke's avatar
Martin Reinecke committed
109
110
111
112
113
114
115
        Parameters
        ----------
        func: function
            This function needs to take exactly one argument, which is
            colatitude in radians, and return the kernel amplitude at that
            colatitude.

116
117
118
119
        Assumes the function to be radially symmetric,
        e.g. only dependant on theta in radians"""
        from .gl_space import GLSpace
        from ..operators.harmonic_operators import HarmonicTransformOperator
120
        from ducc0.misc import GL_thetas
121
122
123
        # define azimuthally symmetric spaces for kernel transform
        gl = GLSpace(self.lmax + 1, 1)
        lm0 = gl.get_default_codomain()
Martin Reinecke's avatar
switch  
Martin Reinecke committed
124
        theta = GL_thetas(gl.nlat)
125
        # evaluate the kernel function at the required thetas
Martin Reinecke's avatar
Martin Reinecke committed
126
        kernel_sphere = Field.from_raw(gl, func(theta))
127
        # normalize the kernel such that the integral over the sphere is 4pi
Martin Reinecke's avatar
Martin Reinecke committed
128
        kernel_sphere = kernel_sphere * (4 * np.pi / kernel_sphere.s_integrate())
129
130
        # compute the spherical harmonic coefficients of the kernel
        op = HarmonicTransformOperator(lm0, gl)
Martin Reinecke's avatar
stage 3  
Martin Reinecke committed
131
        kernel_lm = op.adjoint_times(kernel_sphere.weight(1)).val
132
        # evaluate the k lengths of the harmonic space
Martin Reinecke's avatar
stage 3  
Martin Reinecke committed
133
        k_lengths = self.get_k_length_array().val.astype(np.int)
Martin Reinecke's avatar
Martin Reinecke committed
134
        return Field.from_raw(self, kernel_lm[k_lengths])
135

csongor's avatar
csongor committed
136
137
    @property
    def lmax(self):
Martin Reinecke's avatar
Martin Reinecke committed
138
139
140
141
        """int : maximum allowed :math:`l`

        The maximum :math:`l` value of any spherical harmonic
        coefficient :math:`a_{lm}` that is represented in this domain.
142
        """
csongor's avatar
csongor committed
143
144
145
146
        return self._lmax

    @property
    def mmax(self):
Martin Reinecke's avatar
Martin Reinecke committed
147
148
149
150
        """int : maximum allowed :math:`m`

        The maximum :math:`m` value of any spherical harmonic
        coefficient :math:`a_{lm}` that is represented in this domain.
151
        """
152
        return self._mmax
Martin Reinecke's avatar
Martin Reinecke committed
153
154

    def get_default_codomain(self):
Martin Reinecke's avatar
Martin Reinecke committed
155
        """Returns a :class:`~nifty7.domains.gl_space.GLSpace` object, which is
Martin Reinecke's avatar
Martin Reinecke committed
156
157
158
159
160
161
        capable of storing an accurate representation of data residing on
        `self`.

        Returns
        -------
        GLSpace
Martin Reinecke's avatar
Martin Reinecke committed
162
            The partner domain
Martin Reinecke's avatar
Martin Reinecke committed
163
        """
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
164
        from ..domains.gl_space import GLSpace
Martin Reinecke's avatar
Martin Reinecke committed
165
166
167
        return GLSpace(self.lmax+1, self.mmax*2+1)

    def check_codomain(self, codomain):
Martin Reinecke's avatar
Martin Reinecke committed
168
169
170
171
172
173
174
175
        """Raises `TypeError` if `codomain` is not a matching partner domain
        for `self`.

        Notes
        -----
        This function only checks whether `codomain` is of type
        :class:`GLSpace` or :class:`HPSpace`.
        """
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
176
177
        from ..domains.gl_space import GLSpace
        from ..domains.hp_space import HPSpace
Martin Reinecke's avatar
Martin Reinecke committed
178
        if not isinstance(codomain, (GLSpace, HPSpace)):
179
            raise TypeError("codomain must be a GLSpace or HPSpace.")