11"""Transform angular power spectra."""
22
33__all__ = [
4- "backend" ,
54 "cl" ,
65 "corr" ,
76 "theta" ,
8- "use" ,
97 "var" ,
108]
119
12- from contextlib import contextmanager
13- from typing import Literal
14-
1510from array_api_compat import array_namespace
1611
17- BackendStr = Literal [
18- "flt-ii" ,
19- "flt-i" ,
20- ]
21-
22-
23- backend : BackendStr = "flt-ii"
24- """Backend for transforms. See :ref:`backends`."""
25-
26-
27- @contextmanager
28- def use (choice : BackendStr ) -> None :
29- """Context manager to change backend. See :ref:`backends`."""
30- global backend
31- restore = backend
32- backend = choice
33- try :
34- yield
35- finally :
36- backend = restore
12+ import flt
3713
3814
39- def corr (cl ):
15+ def corr (cl , closed = False ):
4016 r"""
4117 Transform angular power spectrum to angular correlation function.
4218
4319 Takes an angular power spectrum with :math:`\mathtt{n} =
4420 \mathtt{lmax}+1` coefficients and returns the corresponding angular
4521 correlation function in :math:`\mathtt{n}` points.
4622
47- The correlation function is computed at the angles returned by
48- :func:`transformcl.theta`.
23+ The correlation function values can be computed either over the
24+ closed interval :math:`[0, \pi]`, in which case :math:`\theta_0 = 0`
25+ and :math:`\theta_{n-1} = \pi`, or over the open interval :math:`(0,
26+ \pi)`.
4927
5028 Parameters
5129 ----------
5230 cl : (n,) array_like
5331 Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
32+ closed : bool
33+ Compute correlation function over open (``closed=False``) or closed
34+ (``closed=True``) interval.
5435
5536 Returns
5637 -------
@@ -66,30 +47,20 @@ def corr(cl):
6647
6748 """
6849
50+ xp = array_namespace (cl )
51+
6952 # length n of the transform
7053 if cl .ndim != 1 :
7154 raise TypeError ("cl must be 1d array" )
7255 n = cl .shape [- 1 ]
7356
74- if backend in ["flt-ii" , "flt-i" ]:
75- xp = array_namespace (cl )
76- # DLT coefficients = (2l+1)/(4pi) * Cl
77- a = (2 * xp .arange (n ) + 1 ) / (4 * xp .pi ) * cl
78-
79- if backend == "flt-ii" :
80- import flt
81-
82- return flt .idlt (a )
83-
84- if backend == "flt-i" :
85- import flt
57+ # DLT coefficients = (2l+1)/(4pi) * Cl
58+ a = (2 * xp .arange (n ) + 1 ) / (4 * xp .pi ) * cl
8659
87- return flt .idlt (a , True )
60+ return flt .idlt (a , closed )
8861
89- raise NotImplementedError (f"unknown backend { backend !r} " )
9062
91-
92- def cl (corr ):
63+ def cl (corr , closed = False ):
9364 r"""
9465 Transform angular correlation function to angular power spectrum.
9566
@@ -98,7 +69,10 @@ def cl(corr):
9869 :math:`\mathtt{lmax} = \mathtt{n}-1`.
9970
10071 The correlation function must be given at the angles returned by
101- :func:`transformcl.theta`.
72+ :func:`transformcl.theta`. These can be distributed either over the
73+ closed interval :math:`[0, \pi]`, in which case :math:`\theta_0 = 0`
74+ and :math:`\theta_{n-1} = \pi`, or over the open interval :math:`(0,
75+ \pi)`.
10276
10377 Parameters
10478 ----------
@@ -109,6 +83,9 @@ def cl(corr):
10983 -------
11084 cl : (n,) array_like
11185 Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
86+ closed : bool
87+ Compute correlation function over open (``closed=False``) or
88+ closed (``closed=True``) interval.
11289
11390 See Also
11491 --------
@@ -119,27 +96,17 @@ def cl(corr):
11996
12097 """
12198
99+ xp = array_namespace (corr )
100+
122101 # length n of the transform
123102 if corr .ndim != 1 :
124103 raise TypeError ("corr must be 1d array" )
125104 n = corr .shape [- 1 ]
126105
127- if backend in ["flt-ii" , "flt-i" ]:
128- xp = array_namespace (corr )
129- # DLT coefficients = (2l+1)/(4pi) * Cl
130- fl = (2 * xp .arange (n ) + 1 ) / (4 * xp .pi )
131-
132- if backend == "flt-ii" :
133- import flt
134-
135- return flt .dlt (corr ) / fl
136-
137- if backend == "flt-i" :
138- import flt
106+ # DLT coefficients = (2l+1)/(4pi) * Cl
107+ fl = (2 * xp .arange (n ) + 1 ) / (4 * xp .pi )
139108
140- return flt .dlt (corr , True ) / fl
141-
142- raise NotImplementedError (f"unknown backend { backend !r} " )
109+ return flt .dlt (corr , closed ) / fl
143110
144111
145112def var (cl ):
@@ -175,22 +142,12 @@ def var(cl):
175142 return xp .sum ((2 * ell + 1 ) / (4 * xp .pi ) * cl , axis = - 1 )
176143
177144
178- def theta (n ):
145+ def theta (n , closed = False ):
179146 r"""
180147 Return the angles :math:`\theta_1, \ldots, \theta_n` of the
181148 correlation function with *n* points.
182149 """
183-
184- if backend == "flt-ii" :
185- import flt
186-
187- return flt .theta (n )
188- if backend == "flt-i" :
189- import flt
190-
191- return flt .theta (n , True )
192-
193- raise NotImplementedError (f"unknown backend { backend !r} " )
150+ return flt .theta (n , closed )
194151
195152
196153cltocorr = corr
0 commit comments