-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformcl.py
198 lines (143 loc) · 4.3 KB
/
transformcl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Transform angular power spectra."""
__all__ = [
"backend",
"cl",
"corr",
"theta",
"use",
"var",
]
from contextlib import contextmanager
from typing import Literal
from array_api_compat import array_namespace
BackendStr = Literal[
"flt-ii",
"flt-i",
]
backend: BackendStr = "flt-ii"
"""Backend for transforms. See :ref:`backends`."""
@contextmanager
def use(choice: BackendStr) -> None:
"""Context manager to change backend. See :ref:`backends`."""
global backend
restore = backend
backend = choice
try:
yield
finally:
backend = restore
def corr(cl):
r"""
Transform angular power spectrum to angular correlation function.
Takes an angular power spectrum with :math:`\mathtt{n} =
\mathtt{lmax}+1` coefficients and returns the corresponding angular
correlation function in :math:`\mathtt{n}` points.
The correlation function is computed at the angles returned by
:func:`transformcl.theta`.
Parameters
----------
cl : (n,) array_like
Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
Returns
-------
corr : (n,) array_like
Angular correlation function.
See Also
--------
transformcl.cl :
the inverse operation
transformcl.theta :
angles at which the correlation function is evaluated
"""
# length n of the transform
if cl.ndim != 1:
raise TypeError("cl must be 1d array")
n = cl.shape[-1]
if backend in ["flt-ii", "flt-i"]:
xp = array_namespace(cl)
# DLT coefficients = (2l+1)/(4pi) * Cl
a = (2 * xp.arange(n) + 1) / (4 * xp.pi) * cl
if backend == "flt-ii":
import flt
return flt.idlt(a)
if backend == "flt-i":
import flt
return flt.idlt(a, True)
raise NotImplementedError(f"unknown backend {backend!r}")
def cl(corr):
r"""
Transform angular correlation function to angular power spectrum.
Takes an angular function in :math:`\mathtt{n}` points and returns
the corresponding angular power spectrum from :math:`0` to
:math:`\mathtt{lmax} = \mathtt{n}-1`.
The correlation function must be given at the angles returned by
:func:`transformcl.theta`.
Parameters
----------
corr : (n,) array_like
Angular correlation function.
Returns
-------
cl : (n,) array_like
Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
See Also
--------
transformcl.corr :
the inverse operation
transformcl.theta :
angles at which the correlation function is evaluated
"""
# length n of the transform
if corr.ndim != 1:
raise TypeError("corr must be 1d array")
n = corr.shape[-1]
if backend in ["flt-ii", "flt-i"]:
xp = array_namespace(corr)
# DLT coefficients = (2l+1)/(4pi) * Cl
fl = (2 * xp.arange(n) + 1) / (4 * xp.pi)
if backend == "flt-ii":
import flt
return flt.dlt(corr) / fl
if backend == "flt-i":
import flt
return flt.dlt(corr, True) / fl
raise NotImplementedError(f"unknown backend {backend!r}")
def var(cl):
r"""
Compute variance from angular power spectrum.
Given the angular power spectrum, compute the variance of the
spherical random field in a point.
Parameters
----------
cl : array_like
Angular power spectrum. Can be multidimensional, with the last
axis representing the modes.
Returns
-------
var: float
The variance of the given power spectrum.
Notes
-----
The variance :math:`\sigma^2` of the field with power spectrum
:math:`C_l` is
.. math::
\sigma^2 = \sum_{l} \frac{2l + 1}{4\pi} \, C_l \;.
"""
xp = array_namespace(cl)
ell = xp.arange(cl.shape[-1])
return xp.sum((2 * ell + 1) / (4 * xp.pi) * cl, axis=-1)
def theta(n):
r"""
Return the angles :math:`\theta_1, \ldots, \theta_n` of the
correlation function with *n* points.
"""
if backend == "flt-ii":
import flt
return flt.theta(n)
if backend == "flt-i":
import flt
return flt.theta(n, True)
raise NotImplementedError(f"unknown backend {backend!r}")
cltocorr = corr
corrtocl = cl
cltovar = var