Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH(fields): return angular power spectrum by index pair #120

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Loading