"""Lensed TT, TE, EE and BB by the full-sky correlation-function method."""
from __future__ import annotations
from functools import partial
from typing import NamedTuple
import jax
import jax.numpy as jnp
[docs]
class LensedSpectra(NamedTuple):
"""Lensed spectra from :func:`lensed_spectra`, each ``(n_c, lmax + 1)`` from ``l = 0``."""
#: The lensed temperature spectrum.
tt: jax.Array
#: The lensed temperature and E-mode cross spectrum.
te: jax.Array
#: The lensed E-mode spectrum.
ee: jax.Array
#: The lensed B-mode spectrum.
bb: jax.Array
def _lensed_one(cl_tt, cl_te, cl_ee, cl_pp, tables):
"""Return lensed ``(TT, TE, EE, BB)`` for ``l = 0..lmax`` of one cosmology in float64."""
hi = jax.lax.Precision.HIGHEST
dtype = tables.d00.dtype
ell = jnp.arange(tables.d00.shape[0], dtype=jnp.float64) # 0..lmax
llp1 = ell * (ell + 1.0)
norm = (2.0 * ell + 1.0) / (4.0 * jnp.pi)
# Deflection correlation functions on the quadrature nodes.
a = (norm * llp1 * cl_pp).astype(dtype)
sigma2 = jnp.einsum("l,ln->n", a, tables.one_minus_d11, precision=hi)
c_gl2 = jnp.einsum("l,ln->n", a, tables.d1m1, precision=hi)
# F_l - 1 for the leading term of each difference from the unlensed
# correlation function, F_l for the others, (l, n).
less = jnp.expm1(-0.5 * llp1.astype(dtype)[:, None] * sigma2[None, :])
damping = less + 1.0
# The anisotropic expansion parameter is c_l = h_l C_gl,2 with h_l = l(l+1)/2.
# Each term is F_l times a power of C_gl,2(beta) times a sum over l.
h = 0.5 * llp1
b_t, b_x, b_e = (norm * cl_tt, norm * cl_te, norm * cl_ee)
def term(b, coefficient, d, factor=damping):
return jnp.einsum(
"l,ln,ln->n", (b * coefficient).astype(dtype), factor, d, precision=hi
)
xi = (
term(b_t, 1.0, tables.d00, less)
+ c_gl2 * term(b_t, h, tables.d1m1)
+ c_gl2**2
* (term(b_t, h**2 / 4.0, tables.d00) + term(b_t, h**2 / 4.0, tables.d2m2))
)
xi_plus = (
term(b_e, 1.0, tables.d22, less)
+ c_gl2 * term(b_e, h, tables.d13)
+ c_gl2**2
* (term(b_e, h**2 / 4.0, tables.d22) + term(b_e, h**2 / 4.0, tables.d04))
)
xi_minus = (
term(b_e, 1.0, tables.d2m2, less)
+ c_gl2 * (term(b_e, h / 2.0, tables.d1m1) + term(b_e, h / 2.0, tables.d3m3))
+ c_gl2**2
* (
term(b_e, h**2 / 4.0, tables.d2m2)
+ term(b_e, h**2 / 8.0, tables.d00)
+ term(b_e, h**2 / 8.0, tables.d4m4)
)
)
xi_cross = (
term(b_x, 1.0, tables.d20, less)
+ c_gl2 * (term(b_x, h / 2.0, tables.d11) + term(b_x, h / 2.0, tables.d1m3))
+ c_gl2**2
* (term(b_x, 3.0 * h**2 / 8.0, tables.d20) + term(b_x, h**2 / 8.0, tables.d2m4))
)
def back(xi_n, d):
integrand = (tables.weights * xi_n.astype(jnp.float64)).astype(dtype)
return (
2.0
* jnp.pi
* jnp.einsum("ln,n->l", d, integrand, precision=hi).astype(jnp.float64)
)
# The four xi are the lensed minus the unlensed correlation functions,
# and the unlensed ones transform back to the unlensed spectra.
tt = cl_tt + back(xi, tables.d00)
te = cl_te + back(xi_cross, tables.d20)
plus = back(xi_plus, tables.d22)
minus = back(xi_minus, tables.d2m2)
return tt, te, cl_ee + 0.5 * (plus + minus), 0.5 * (plus - minus)
@partial(jax.jit, static_argnames=("chunk",))
def lens(cl_tt, cl_te, cl_ee, cl_pp, tables, chunk: int = 4) -> LensedSpectra:
"""Return lensed ``TT, TE, EE, BB`` for a batch as a :class:`LensedSpectra` of ``(n_c, lmax + 1)``.
``cl_tt``, ``cl_te``, ``cl_ee`` (unlensed) and ``cl_pp`` (lensing
potential) are ``(n_c, lmax + 1)``, indexed by ``l`` from 0, and
``tables`` are :func:`~discocmb.grids.lensing_tables` of that ``lmax``.
The function ignores the monopole and dipole. It handles ``chunk``
cosmologies at a time, since each needs an ``(lmax + 1, n)`` array of
damping factors on the ``n`` nodes of ``tables``.
"""
spectra = [
jnp.asarray(c, dtype=jnp.float64).at[:, :2].set(0.0)
for c in (cl_tt, cl_te, cl_ee, cl_pp)
]
n_c, n_l = spectra[0].shape
pad = (-n_c) % chunk
spectra = [jnp.pad(c, ((0, pad), (0, 0))).reshape(-1, chunk, n_l) for c in spectra]
one = jax.vmap(_lensed_one, in_axes=(0, 0, 0, 0, None))
out = jax.lax.map(lambda args: one(*args, tables), tuple(spectra))
return LensedSpectra(*(c.reshape(-1, n_l)[:n_c] for c in out))
[docs]
def lensed_spectra(cl, grid, *, chunk: int = 32) -> LensedSpectra:
"""Return the lensed TT, TE, EE and BB of the unlensed :class:`~discocmb.Spectra` ``cl``.
``cl`` comes from :func:`~discocmb.cmb_power_spectrum` or
:func:`~discocmb.solve_cmb` on ``grid``, whose Wigner tables the sums
use, and each lensed spectrum is ``(n_c, grid.lmax + 1)`` in the units
of ``cl``. The lensed spectra are good to a few hundred multipoles below
``grid.lmax``: to ``l ~ 2500`` with the default grid.
The sums run in the precision of the grid, ``chunk`` cosmologies at a
time. Only the lensing correction goes through the quadrature, so double
precision moves the lensed spectra by 2e-6 and costs 55 ms more for a
batch of 128.
"""
n = cl.tt.shape[0]
return lens(cl.tt, cl.te, cl.ee, cl.pp, grid.operators.tables, chunk=min(chunk, n))