Skip to content

Commit

Permalink
ENH(fields): return angular power spectrum by index pair (#120)
Browse files Browse the repository at this point in the history
Add a function `getcl()` that takes a list of angular power spectra in
GLASS order and returns the entry corresponding to a pair of indices `i`
and `j`.

Closes: #112
Added: Function `getcl()` to return angular power spectra by index from
  a list using GLASS ordering.
  • Loading branch information
ntessore authored Jul 31, 2023
1 parent 8dfe40f commit 004b26f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
35 changes: 35 additions & 0 deletions glass/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
.. autofunction:: generate_gaussian
.. autofunction:: generate_lognormal
Utility functions
-----------------
.. autofunction:: getcl
'''

import warnings
Expand Down Expand Up @@ -285,3 +291,32 @@ def generate_lognormal(gls: Cls, nside: int, shift: float = 1., *,

# yield the lognormal map
yield m


def getcl(cls, i, j, lmax=None):
"""Return a specific angular power spectrum from an array.
Return the angular power spectrum for indices *i* and *j* from an
array in *GLASS* ordering.
Parameters
----------
cls : list of array_like
List of angular power spectra in *GLASS* ordering.
i, j : int
Combination of indices to return.
lmax : int, optional
Truncate the returned spectrum at this mode number.
Returns
-------
cl : array_like
The angular power spectrum for indices *i* and *j*.
"""
if j > i:
i, j = j, i
cl = cls[i*(i+1)//2+i-j]
if lmax is not None:
cl = cl[:lmax+1]
return cl
8 changes: 8 additions & 0 deletions glass/test/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def test_getcl():
from glass.fields import getcl
# make a mock Cls array with the index pairs as entries
cls = [{i, j} for i in range(10) for j in range(i, -1, -1)]
# make sure indices are retrieved correctly
for i in range(10):
for j in range(10):
assert getcl(cls, i, j) == {i, j}

0 comments on commit 004b26f

Please sign in to comment.