Skip to content

sigway.utils

Numerical and unit-conversion helpers used across SIGWAY.

Unit & cosmology conversions

Relate comoving wavenumber \(k\), e-folds \(N\), and the Hubble rate \(H\).

sigway.utils.wavenumber_from_efolds_si_units

wavenumber_from_efolds_si_units(N, H, N_CMB, H_CMB)

Convert inflationary e-folds \(N\) and Hubble rate \(H\) to comoving wavenumber \(k\).

Uses the horizon-crossing relation \(k = k_\mathrm{CMB}\,(H/H_\mathrm{CMB})\,e^{N - N_\mathrm{CMB}}\), where \(k_\mathrm{CMB} = 0.05\,\mathrm{Mpc}^{-1}\) converted to SI units (\(\mathrm{s}^{-1}\)).

Parameters:

Name Type Description Default
N array_like

Inflationary e-fold values \(N\).

required
H array_like

Hubble parameter values in s^-1 evaluated at the corresponding \(N\).

required
N_CMB float

E-fold value \(N_\mathrm{CMB}\) at which the CMB pivot scale crosses the horizon.

required
H_CMB float

Hubble parameter \(H_\mathrm{CMB}\) in s^-1 at the CMB pivot scale.

required

Returns:

Name Type Description
k_N ndarray

Comoving wavenumber \(k\) in s^-1, with the same shape as N and H.

sigway.utils.efolds_from_wavenumber_si_units

efolds_from_wavenumber_si_units(k, H, N_CMB, H_CMB)

Convert comoving wavenumber \(k\) and Hubble rate \(H\) to inflationary e-folds \(N\).

Inverts the horizon-crossing relation used in wavenumber_from_efolds_si_units: \(N = N_\mathrm{CMB} + \ln\!\left[k\,/\,(k_\mathrm{CMB}\,H/H_\mathrm{CMB})\right]\).

Parameters:

Name Type Description Default
k array_like

Comoving wavenumber values \(k\) in s^-1.

required
H array_like

Hubble parameter values in s^-1 evaluated at the corresponding \(k\).

required
N_CMB float

E-fold value \(N_\mathrm{CMB}\) at which the CMB pivot scale crosses the horizon.

required
H_CMB float

Hubble parameter \(H_\mathrm{CMB}\) in s^-1 at the CMB pivot scale.

required

Returns:

Name Type Description
N ndarray

Number of e-folds \(N\), with the same shape as k and H.

sigway.utils.H_from_wavenumber

H_from_wavenumber(k, N, H, N_CMB, H_CMB)

Interpolate the Hubble rate \(H\) as a function of comoving wavenumber \(k\).

First maps the inflationary trajectory \((N, H)\) to a wavenumber array via wavenumber_from_efolds_si_units, sorts it in ascending order, then linearly interpolates \(H\) at the requested \(k\) values.

Parameters:

Name Type Description Default
k array_like

Comoving wavenumber values \(k\) in s^-1 at which \(H\) is evaluated.

required
N array_like

Inflationary e-fold array describing the background trajectory.

required
H array_like

Hubble parameter values in s^-1 corresponding to N.

required
N_CMB float

E-fold value \(N_\mathrm{CMB}\) at which the CMB pivot scale crosses the horizon.

required
H_CMB float

Hubble parameter \(H_\mathrm{CMB}\) in s^-1 at the CMB pivot scale.

required

Returns:

Name Type Description
H_k ndarray

Hubble parameter \(H(k)\) in s^-1, with the same shape as k.

Simpson integration

Composite Simpson quadrature on uniform and non-uniform grids.

sigway.utils.simpson_uniform

simpson_uniform(f, x)

Composite Simpson quadrature on a uniform grid.

Implements the composite Simpson 1/3 rule (see https://en.wikipedia.org/wiki/Simpson%27s_rule). Integration is always performed over the first axis of f; any remaining axes are treated as independent integrals and are preserved in the output shape.

When the number of intervals \(N = \mathrm{len}(x) - 1\) is odd (i.e. len(x) is even), the last interval is handled with the higher-order correction from the irregularly-spaced variant so that the overall method remains fourth-order accurate. The grid is assumed uniform; no uniformity check is performed for performance reasons.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values at the grid points. The first axis must match len(x).

required
x (ndarray, shape(N + 1))

Uniformly spaced grid points. Only the spacing \(h = x_1 - x_0\) is used internally.

required

Returns:

Name Type Description
result (ndarray, shape(...))

Approximate integral \(\int f(x)\,\mathrm{d}x\). A scalar is returned when f is one-dimensional.

Examples:

Integrate \(\sin(x)\) from \(0\) to \(\pi\) (exact value: \(2\)):

>>> import jax.numpy as jnp
>>> from sigway.utils import simpson_uniform
>>> x = jnp.linspace(0, jnp.pi, 101)
>>> f = jnp.sin(x)
>>> float(simpson_uniform(f, x))
2.0000000108...

sigway.utils.simpson_uniform_even

simpson_uniform_even(f, h)

Composite Simpson 1/3 rule for an even number of intervals.

Applies the standard composite Simpson formula \(\int f\,\mathrm{d}x \approx \tfrac{h}{3}(f_0 + 4f_1 + 2f_2 + \cdots + 4f_{N-1} + f_N)\) assuming \(N\) (number of intervals) is even. Called internally by simpson_uniform; use that function directly unless you need fine-grained control.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values on a uniform grid. Integration is over the first axis.

required
h (ndarray, shape(1))

Uniform step size \(h = x_1 - x_0\).

required

Returns:

Name Type Description
result (ndarray, shape(...))

Approximate integral \(\int f\,\mathrm{d}x\) over the remaining axes.

sigway.utils.simpson_uniform_odd

simpson_uniform_odd(f, h)

Composite Simpson 1/3 rule for an odd number of intervals.

Handles the case where the total number of intervals \(N\) is odd by treating the last (unpaired) interval with a higher-order correction formula and applying the standard composite Simpson rule to the remaining even number of intervals. Called internally by simpson_uniform.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values on a uniform grid. Integration is over the first axis.

required
h (ndarray, shape(1))

Uniform step size \(h = x_1 - x_0\).

required

Returns:

Name Type Description
result (ndarray, shape(...))

Approximate integral \(\int f\,\mathrm{d}x\) over the remaining axes.

sigway.utils.simpson_nonuniform

simpson_nonuniform(f, x)

Composite Simpson quadrature on a non-uniform grid.

Implements the composite irregularly-spaced Simpson 1/3 rule (see https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule_for_irregularly_spaced_data). Integration is always performed over the first axis of f; any remaining axes are treated as independent integrals and are preserved in the output.

When the number of intervals \(N = \mathrm{len}(x) - 1\) is odd, the final unpaired interval is handled by simpson_nonuniform_odd using a three-point correction that maintains fourth-order accuracy.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values at the grid points. The first axis must match len(x).

required
x (ndarray, shape(N + 1) or shape(N + 1, ...))

Grid point coordinates. When x has the same shape as f, step sizes are used without reshaping; when x is one-dimensional, step sizes are broadcast over the trailing axes of f.

required

Returns:

Name Type Description
result (ndarray, shape(...))

Approximate integral \(\int f(x)\,\mathrm{d}x\). A scalar is returned when f is one-dimensional.

sigway.utils.simpson_nonuniform_even

simpson_nonuniform_even(f, h, result)

Pass-through for non-uniform Simpson when the interval count is even.

When the number of intervals \(N\) is even, all intervals are already covered by the main vectorised sum in simpson_nonuniform and no correction is needed. This function simply returns result unchanged, serving as the even-branch of the jax.lax.switch inside simpson_nonuniform.

Parameters:

Name Type Description Default
f ndarray

Function values (unused; present for a uniform call signature).

required
h ndarray

Step-size array (unused; present for a uniform call signature).

required
result ndarray

Partial integral accumulated by simpson_nonuniform.

required

Returns:

Name Type Description
result ndarray

The input result unchanged.

sigway.utils.simpson_nonuniform_odd

simpson_nonuniform_odd(f, h, result)

Correction term for non-uniform Simpson when the interval count is odd.

Adds the contribution of the last unpaired interval using the irregularly-spaced three-point correction formula:

\(\Delta I = f_{N}\,\frac{2h_1^2 + 3h_0 h_1}{6(h_0+h_1)} + f_{N-1}\,\frac{h_1^2 + 3h_1 h_0}{6h_0} - f_{N-2}\,\frac{h_1^3}{6h_0(h_0+h_1)}\)

where \(h_0 = x_{N-1}-x_{N-2}\) and \(h_1 = x_N - x_{N-1}\). Called as the odd-branch of the jax.lax.switch inside simpson_nonuniform.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values on the non-uniform grid.

required
h (ndarray, shape(N))

Consecutive step sizes \(h_i = x_{i+1} - x_i\).

required
result (ndarray, shape(...))

Partial integral from simpson_nonuniform covering all but the last interval.

required

Returns:

Name Type Description
result (ndarray, shape(...))

Corrected integral including the final interval.

Broadcasting

sigway.utils.do_broadcasting

do_broadcasting(f, h)

Return step-size pairs for non-uniform Simpson with broadcasting.

Extracts alternating consecutive step-size pairs \((h_{2i}, h_{2i+1})\) from h and reshapes them so they broadcast over all trailing axes of f. Used when x is one-dimensional but f is multi-dimensional.

Parameters:

Name Type Description Default
f (ndarray, shape(N + 1, ...))

Function values array. Its number of dimensions determines the reshape target.

required
h (ndarray, shape(N))

Array of consecutive step sizes \(h_i = x_{i+1} - x_i\).

required

Returns:

Name Type Description
h0 (ndarray, shape(N // 2, 1, 1, ...))

Even-indexed step sizes reshaped for broadcasting against f.

h1 (ndarray, shape(N // 2, 1, 1, ...))

Odd-indexed step sizes reshaped for broadcasting against f.

sigway.utils.no_broadcasting

no_broadcasting(f, h)

Return step-size pairs for non-uniform Simpson without broadcasting.

Extracts alternating consecutive step-size pairs \((h_{2i}, h_{2i+1})\) from h without reshaping, for use when f and x have the same shape (i.e. x is already broadcast-compatible with f).

Parameters:

Name Type Description Default
f ndarray

Function values array (used only to determine dimensionality; not modified).

required
h (ndarray, shape(N))

Array of consecutive step sizes \(h_i = x_{i+1} - x_i\).

required

Returns:

Name Type Description
h0 ndarray

Even-indexed step sizes \(h_{0}, h_{2}, h_{4}, \ldots\)

h1 ndarray

Odd-indexed step sizes \(h_{1}, h_{3}, h_{5}, \ldots\)