Scans¶
A scan builds the running integral of samples along one axis, \(F_k = F_{k-1} + \Delta_k\) with the trapezoid increments \(\Delta_k = \frac{1}{2}(x_k - x_{k-1})(f_{k-1} + f_k)\), by jax.lax.associative_scan in \(O(\log N)\) depth. With reverse=True it integrates from the far end, as optical depth from the top of a column does; the increments are reversed, never the nodes.
The theory is in the Scans chapter of the mathematical reference.
scan
¶
Scans: cumulative integrals, forward and reverse, by associative scan.
cumulative builds the running integral Fₖ of samples along one axis in
O(log N) depth, from the first node or (reverse=True) from the last, as
optical depth down a column needs.
cumulative
¶
cumulative(rule: RuleLike, values: ArrayLike, *, reverse: bool = False, axis: int = -1, initial: float = 0.0) -> Inexact[Array, '*shape']
The running trapezoid integral of samples along one axis.
F₀ = F_init and Fₖ = Fₖ₋₁ + ½ (xₖ − xₖ₋₁)(fₖ₋₁ + fₖ), so Fₖ ≈ F_init +
∫ from x₀ to xₖ of f. With reverse=True the integral runs from the far
end, Fₖ ≈ F_init + ∫ from xₖ to x_{N−1} of f (optical depth from the top
of a column).
Algorithm
- Form the interval increments Δₖ = ½ (xₖ − xₖ₋₁)(fₖ₋₁ + fₖ) for k = 1, …, N − 1, from the rule's nodes.
- Prefix-sum them with
jax.lax.associative_scan(O(log N) depth), from the far end whenreverseis set: a reversed scan reverses the increments, never the nodes, so every width keeps its sign. - Prepend (or, reversed, append) a zero for the starting node and add F_init.
The increments are the trapezoid rule on the rule's nodes (the cumulative
form of functax's trapezoid), so the last entry equals
integrix.rule.integrate with a trapezoid rule on the same nodes; the
rule's own weights are not used. The gradient of a prefix sum is a
reversed prefix sum, which JAX differentiates without a custom rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
RuleLike
|
A rule with |
required |
values
|
ArrayLike
|
The samples f, with N entries along |
required |
reverse
|
bool
|
Integrate from the last node towards the first. |
False
|
axis
|
int
|
The axis of |
-1
|
initial
|
float
|
The value F_init at the starting node. |
0.0
|
Returns:
| Type | Description |
|---|---|
Inexact[Array, '*shape']
|
The running integral F, with the shape of the samples, in their dtype |
Inexact[Array, '*shape']
|
(complex when the nodes are; integer samples take the nodes' dtype). |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the rule has no 1-D |
Examples:
>>> import equinox as eqx
>>> import jax.numpy as jnp
>>> class Trapezoid(eqx.Module):
... nodes: jnp.ndarray
... weights: jnp.ndarray
>>> nodes = jnp.array([0.0, 1.0, 3.0])
>>> rule = Trapezoid(nodes, jnp.array([0.5, 1.5, 1.0]))
>>> values = jnp.array([1.0, 2.0, 3.0])
>>> bool(jnp.allclose(cumulative(rule, values), jnp.array([0.0, 1.5, 6.5])))
True
>>> from_top = cumulative(rule, values, reverse=True)
>>> bool(jnp.allclose(from_top, jnp.array([6.5, 5.0, 0.0])))
True
>>> cumulative(rule, jnp.ones((4, 3))).shape
(4, 3)
Source code in src/integrix/_src/scan/_cumulative.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 91 92 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 | |