Skip to content

e2

This module contains functions to calculate the coefficients which are multipled with the orthonormal basis to make up the $e_2$ vector.

isotropic.e2

This module contains functions for generating the vector $e_2$.

F_j(theta_j, j, d)

Calculate the function $F_j$ for the given angle $\theta_j$ and index $j$ in dimension $d$.

Parameters:

Name Type Description Default
theta_j float

The angle at which to evaluate the function.

required
j int

The index corresponding to the angle.

required
d int

The dimension of the space.

required

Returns:

Type Description
Array

The value of the function $F_j$ evaluated at $\theta_j$.

Source code in src/isotropic/e2.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def F_j(theta_j: float, j: int, d: int) -> Array:
    """
    Calculate the function $F_j$ for the given angle $\\theta_j$ and index $j$ in dimension $d$.

    Parameters
    ----------
    theta_j : float
        The angle at which to evaluate the function.
    j : int
        The index corresponding to the angle.
    d : int
        The dimension of the space.

    Returns
    -------
    Array
        The value of the function $F_j$ evaluated at $\\theta_j$.
    """
    dj = d - j

    if j % 2 == 1:
        C_j = (1 / 2) * double_factorial_ratio(dj - 1, dj - 2)
        k_max = (dj - 2) // 2
        # Precompute fractions (static values depending only on dj and k)
        fractions = jnp.array(
            [_compute_fraction(dj, k, odd=True) for k in range(k_max + 1)]
        )
        k_vals = jnp.arange(k_max + 1)
        sin_powers = jnp.power(jnp.sin(theta_j), 2 * k_vals)
        sum_terms = jnp.sum(fractions * sin_powers)
        return 0.5 - C_j * jnp.cos(theta_j) * sum_terms
    else:
        C_j = (1 / jnp.pi) * double_factorial_ratio(dj - 1, dj - 2)
        k_max = (dj - 1) // 2
        fractions = jnp.array(
            [_compute_fraction(dj, k, odd=False) for k in range(1, k_max + 1)]
        )
        k_vals = jnp.arange(1, k_max + 1)
        sin_powers = jnp.power(jnp.sin(theta_j), 2 * k_vals - 1)
        sum_terms = jnp.sum(fractions * sin_powers)
        return theta_j / jnp.pi - C_j * jnp.cos(theta_j) * sum_terms

get_e2_coeffs(d, key=random.PRNGKey(0))

Generate the coefficients of the vector $e_2$.

Parameters:

Name Type Description Default
d int

Dimension of the space.

required
key ArrayLike

Random key for reproducibility, by default random.PRNGKey(0).

PRNGKey(0)

Returns:

Type Description
Tuple[Array, Array]

A tuple containing:

  • theta: Array of angles used to construct $e_2$.
  • e2: Array representing the coefficients of the vector $e_2$.
Notes

PRNG stream: keys are derived via a single random.split(key, d-1) call, producing d-1 independent subkeys upfront. This differs from the previous sequential key, subkey = random.split(key) chain and will yield numerically different draws from the same seed, while remaining statistically equivalent.

Source code in src/isotropic/e2.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def get_e2_coeffs(d: int, key: ArrayLike = random.PRNGKey(0)) -> Tuple[Array, Array]:
    """
    Generate the coefficients of the vector $e_2$.

    Parameters
    ----------
    d : int
        Dimension of the space.
    key : ArrayLike, optional
        Random key for reproducibility, by default random.PRNGKey(0).

    Returns
    -------
    Tuple[Array, Array]
        A tuple containing:

        - theta: Array of angles used to construct $e_2$.
        - e2: Array representing the coefficients of the vector $e_2$.

    Notes
    -----
    PRNG stream: keys are derived via a single ``random.split(key, d-1)`` call,
    producing ``d-1`` independent subkeys upfront. This differs from the
    previous sequential ``key, subkey = random.split(key)`` chain and will
    yield numerically different draws from the same seed, while remaining
    statistically equivalent.
    """
    # Draw keys upfront: all_subkeys[0] for the azimuthal angle,
    # all_subkeys[1:] for the d-2 bisection angles.
    all_subkeys = random.split(key, d - 1)  # shape (d-1, 2)
    theta_last = random.uniform(all_subkeys[0], shape=(), minval=0, maxval=2 * jnp.pi)

    # Early return for d < 3 (no bisection needed)
    if d < 3:
        theta = jnp.array([theta_last])
        e2 = jnp.cumprod(jnp.concatenate([jnp.ones(1), jnp.sin(theta)]))
        e2 = e2 * jnp.cos(jnp.append(theta, 0.0))
        return jnp.append(theta, 0), e2

    # --- Trace-time precomputation of static arrays ---
    # max number of sum terms across all j; tight at j=0 (even) and j=1 (odd)
    max_k = (d - 1) // 2

    C_list: list = []
    fracs_list: list = []
    sinexp_list: list = []
    is_odd_list: list = []

    for j in range(d - 2):
        dj = d - j
        if j % 2 == 1:  # odd j
            C_j = 0.5 * double_factorial_ratio(dj - 1, dj - 2)
            k_max_j = (dj - 2) // 2
            fracs = [_compute_fraction(dj, k, odd=True) for k in range(k_max_j + 1)]
            sin_exps = [2 * k for k in range(k_max_j + 1)]
            is_odd = 1.0
        else:  # even j
            C_j = (1.0 / np.pi) * double_factorial_ratio(dj - 1, dj - 2)
            k_max_j = (dj - 1) // 2
            fracs = [_compute_fraction(dj, k, odd=False) for k in range(1, k_max_j + 1)]
            sin_exps = [2 * k - 1 for k in range(1, k_max_j + 1)]
            is_odd = 0.0

        # Zero-pad fracs; pad sin_exps with int 1 (keeps array integer-typed,
        # ensuring jnp.power uses integer-power dispatch and avoids the
        # exp(0*log(0))=NaN path that float 0.0 exponents can trigger).
        fracs += [0.0] * (max_k - len(fracs))
        sin_exps += [1] * (max_k - len(sin_exps))

        C_list.append(C_j)
        fracs_list.append(fracs)
        sinexp_list.append(sin_exps)
        is_odd_list.append(is_odd)

    C_arr = jnp.array(C_list)  # (d-2,)
    fracs_arr = jnp.array(fracs_list)  # (d-2, max_k)
    sin_exp_arr = jnp.array(sinexp_list)  # (d-2, max_k)
    is_odd_arr = jnp.array(is_odd_list)  # (d-2,)

    # Draw one uniform x per j via vmap over keys
    j_subkeys = all_subkeys[1:]  # (d-2, 2)
    x_vals = jax.vmap(lambda k: random.uniform(k, shape=(), minval=0, maxval=1))(
        j_subkeys
    )  # (d-2,)

    # Unified runtime function: same signature for all j, vmappable
    def compute_theta_j(x, C, fracs, sin_exps, is_odd):  # numpydoc ignore=GL08
        def F(theta):  # numpydoc ignore=GL08
            sin_sum = jnp.dot(fracs, jnp.power(jnp.sin(theta), sin_exps))
            offset = jnp.where(is_odd, 0.5, theta / jnp.pi)
            return offset - C * jnp.cos(theta) * sin_sum

        return get_theta(F, 0.0, jnp.pi, x, 1e-9)

    # vmap over j: all d-2 bisections run in parallel
    theta_vals = jax.vmap(compute_theta_j)(
        x_vals, C_arr, fracs_arr, sin_exp_arr, is_odd_arr
    )  # (d-2,)

    # Assemble full theta vector and compute e2
    theta = jnp.append(theta_vals, theta_last)  # (d-1,)

    # Spherical coordinate conversion: cumulative product of sines times cosine
    e2 = jnp.cumprod(jnp.concatenate([jnp.ones(1), jnp.sin(theta)]))
    e2 = e2 * jnp.cos(jnp.append(theta, 0.0))

    theta = jnp.append(theta, 0)  # Append 0 for cos(0) of last coordinate

    return theta, e2