Shared helpers#

Helpers shared by the Price-McEwen and Turok-Bucher recursions and by the latitudinal steps of the transforms: the starting values of the half recursions, their steps and the loop that runs them, with the iterates rescaled by exact powers of two.

s2fft.recursions.common.fori_half_recursion(body, carry, L, mm, extension, skip_at_zero_spin=False)#

Run the steps of a half recursion: \(L\) steps take every \(\ell\) to \(m = 0\) and extension more run towards the meeting point, in one loop of static length. With extension None, the \(|m^\prime|\) extra steps instead run in a second loop with a traced trip count. That saves steps when the spin is traced, but cannot be differentiated in reverse mode, so it is only for callers with their own derivative rules.

With skip_at_zero_spin, nothing runs when \(m^\prime = 0\), as for half recursion 0 of a real signal at spin 0. Since extension bounds \(|m^\prime|\), the spin is zero when it is, so that test is static; with extension None it is a lax.cond on the traced spin. A traced spin with a static extension (\(L - 1\)) always runs, also at spin 0: that only costs time, and fills the rows \(m < 0\) that would otherwise stay zero.

s2fft.recursions.common.polar_terms(beta, mm, xp)#

Functions of \(\beta\) adopted by the recursion: the hemisphere (\(\pm 1\)), \(2\sin^2(\beta/2)\) in the north or \(2\cos^2(\beta/2)\) in the south, \(\sin\beta\) (one at the poles), and the meeting point of the two half recursions, \(m^\star = \mathrm{round}(m^\prime\cos\beta)\), the centre of the oscillatory region of the slice.

s2fft.recursions.common.recursion_step(i, j, state, start, el, L, mm, polar, xp)#

Step \(j\) of half recursion \(i\) of the Price & McEwen slice recursion.

Half recursion 0 runs upwards in \(m\) from the edge \(m = -\ell\) and half recursion 1 downwards from \(m = \ell\). Degree \(\ell\) starts at step \(L - 1 - \ell\), so step \(j\) evaluates the row \(m = j - (L - 1)\) (\(i = 0\)) or \(m = L - 1 - j\) (\(i = 1\)) for all \(\beta\) and \(\ell\) at once. The iterates are rescaled by exact powers of two, with the scale kept as an integer index. Each half recursion stops at the meeting point \(m^\star\) from polar_terms(): half recursion 0 gives the rows \(m < m^\star\) and half recursion 1 the rows \(m \geq m^\star\), so that both only run in the direction in which the slice grows.

Parameters:
  • i (int) – Half recursion, 0 or 1.

  • j (int) – Step.

  • state (Tuple) – Previous and current iterates and their scale index, each of shape (ntheta, nel).

  • start (Tuple) – Starting values and their scale indices from scaled_start_values().

  • el (np.ndarray | jnp.ndarray) – Harmonic degrees, shape (nel,).

  • L (int) – Harmonic band-limit.

  • mm (int) – Harmonic order \(m^\prime\) at which to slice the matrix.

  • polar (Tuple) – Functions of \(\beta\) and the meeting point from polar_terms().

  • xp (module) – Array namespace, numpy or jax.numpy.

Returns:

\(d^\ell_{m, m^\prime}(\beta)\) on row \(m\) with shape (ntheta, nel), zero for \(|m| > \ell\) and where the row belongs to the other half recursion, and the updated state.

Return type:

Tuple

s2fft.recursions.common.scaled_start_values(beta, el, mm, xp)#

Starting values from start_values() as iterates within \([2^{-400}, 2^{401})\) and scale indices \(k\), the value being \(d\,2^{800 k}\); each of shape (2, ntheta, nel).

s2fft.recursions.common.start_values(beta, el, mm, xp)#

Starting values of the two half recursions over \(m\) for all \(\beta\) and \(\ell\), as exact base-2 exponents and mantissas.

The half recursions start at the edges of the slice, from

\[|d^\ell_{\mp\ell, m^\prime}(\beta)| = \sqrt{\binom{2\ell}{\ell + |m^\prime|}} \left(\frac{\sin\beta}{2}\right)^{\ell - |m^\prime|} S^{|m^\prime|},\]

with \(S = \sin^2(\beta/2)\) or \(\cos^2(\beta/2)\). These values underflow near the poles, so they are evaluated as \(M 2^E\) with \(E\) an integer: the exponents of \(\sin\beta/2\) and \(S\) are split off exactly (frexp) and only logarithms of \(\mathcal{O}(1)\) mantissas are taken, giving a relative error of a few \(\ell\) units of round-off rather than one proportional to \(\ell \log(1/\beta)\).

Parameters:
  • beta (np.ndarray | jnp.ndarray) – Polar angles in radians, shape (ntheta,).

  • el (np.ndarray | jnp.ndarray) – Harmonic degrees, shape (nel,).

  • mm (int) – Harmonic order \(m^\prime\) at which to slice the matrix.

  • xp (module) – Array namespace, numpy or jax.numpy.

Returns:

Mantissas and (integer-valued) exponents, each of shape (2, ntheta, nel), for the half recursions starting at \(m = -\ell\) (index 0) and \(m = \ell\) (index 1); zero for \(\ell < |m^\prime|\).

Return type:

Tuple[np.ndarray | jnp.ndarray]

Raises:

RuntimeError – For JAX arrays when 64-bit floats are disabled: the iterates of the recursion lie far outside the range of single precision, so it would return zeros. The error is raised before any computation, while tracing (or, through the transforms, while their primitive is lowered), so no single-precision trace is kept for later calls with 64-bit floats.

s2fft.recursions.common.start_values_scalar(beta, el, mm)#

Scalar version of start_values() for a single \(\beta\) and \(\ell\), for the scalar Turok & Bucher recursion.

Returns:

Mantissas and exponents of the starting values of the half recursions starting at \(m = -\ell\) and \(m = \ell\), as ((mantissa_0, exponent_0), (mantissa_1, exponent_1)).

Return type:

Tuple

s2fft.recursions.common.static_extension(spin, L)#

Number of steps that the half recursions run past \(m = 0\), as a static bound: \(|m^\prime|\) when the spin is known, or its largest value \(L - 1\) when it is a JAX tracer (under jit or vmap). Steps past the meeting point change nothing, and a loop of static length can be differentiated in reverse mode. A traced spin is supported because s2fft’s Wigner transforms vmap the spherical ones over it. The bound has to be taken before jit traces the spin, so the JAX recursions and latitudinal steps are thin wrappers that pass it to their jitted implementations as a static argument.