Rules¶
A rule integrates by weights against samples, \(I \approx \sum_n w_n f(x_n)\). The weights come from functax's rules, from a tuple of 1-D rules (a tensor rule, one axis each), or from a plain array such as a spectral response or cell areas; integrix only sums with them. Every sum is pairwise, so a float32 sum over a long spectrum does not drift.
Both integrate and weighted_mean take where=, a boolean mask that broadcasts against the values (a plume over an image, the valid levels of each column), and nan_policy=: "propagate" (the default, so a NaN among the kept samples gives NaN) or "omit" (NaN samples are left out, like np.nansum). A dropped sample adds nothing to the sum and gets an exactly zero, finite gradient, and weighted_mean renormalises over the samples it keeps.
weighted_mean's weight may also be a batch of weight functions: the rule's shape with extra leading axes, one mean per leading index, with the values' batch axes first. An instrument's \(B\) band responses on \(G\) wavelengths reduce \(P\) spectra in one call, radiance \((P, G)\) and responses \((B, G)\) giving \((P, B)\).
log_integrate is the log-sum-exp twin of integrate: it takes \(\log f\) and returns \(\log I = \log \sum_n w_n f_n\) without leaving log space, so an integrand whose values underflow (a heavy-tailed density far from its mode) still has a finite log-integral. Its weights must be positive, and where= drops nodes by making their terms \(-\infty\).
The Spatial integrals guide works through the IME, the cross-sectional flux in its strip and sampled forms, and a NaN-propagating column. The theory is in the Rules chapter of the mathematical reference.
rule
¶
Rules: integrals as weights against samples, I ≈ ∑ₙ wₙ f(xₙ).
integrate contracts values (or an integrand evaluated at a rule's nodes)
with quadrature weights over one axis or several, batched over the rest;
weighted_mean divides by the integral of a weight function, as a band
radiance does; log_integrate returns log I from log f for integrands that
underflow. The rules themselves (nodes and weights) come from functax, or
are a plain weights array such as a spectral response or cell areas.
Integral
¶
Bases: Module
The result of an integral: its value, an error estimate and diagnostics.
A plain rule has no error estimate, so error is None and
converged is True; adaptive procedures (integrix.adapt) fill both.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Inexact[Array, '*batch']
|
The integral I, one per batch entry. |
error |
Inexact[Array, '*batch'] | None
|
An estimate of |I − Î|, or |
n_evaluations |
Int[Array, '']
|
The number of integrand values summed. |
converged |
Bool[Array, '']
|
Whether the error target was met. |
Examples:
>>> import jax.numpy as jnp
>>> result = integrate(jnp.array([0.5, 0.5]), jnp.array([1.0, 3.0]))
>>> bool(result.value == 2.0), result.error, bool(result.converged)
(True, None, True)
log_integrate
¶
log_integrate(rule: Rule, log_values_or_fn: ArrayLike | Callable[..., Any], *, axis: Axes | None = None, args: Any = None, where: ArrayLike | None = None) -> Float[Array, '*batch']
log I from log f: the log-sum-exp twin of integrate.
log I = log ∑ₙ wₙ fₙ = logsumexp over n of (log wₙ + log fₙ), so an integrand whose values underflow (log fₙ ≈ −800, a heavy-tailed density far from its mode) still gives a finite log-integral. The weights must be positive; a mask leaves nodes out by making their term −∞.
Algorithm
- Read the weights w as
integratedoes, and check that they are positive. - Get log f: the log-values as given, or
log_f(nodes)(withargs,log_f(nodes, args)), node axes first. - Form tₙ = log wₙ + log fₙ, with −∞ for nodes
wheredrops (their log fₙ replaced by 0 first, so neither a NaN nor its gradient leaks). - Return logsumexp over the node axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
Rule
|
A rule with positive |
required |
log_values_or_fn
|
ArrayLike | Callable[..., Any]
|
The log-samples log fₙ, or a callable log f
evaluated at the rule's |
required |
axis
|
Axes | None
|
The axes to integrate over, as in |
None
|
args
|
Any
|
Extra arguments passed to the callable as |
None
|
where
|
ArrayLike | None
|
A boolean mask that broadcasts against the log-samples; nodes where it is false are left out. With no node kept the result is −∞, the log of an empty sum. |
None
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
log I, with the shape of the log-samples without the integrated axes. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
As |
Examples:
>>> import jax.numpy as jnp
>>> weights = jnp.array([0.25, 0.75])
>>> log_total = log_integrate(weights, jnp.log(jnp.array([2.0, 4.0])))
>>> bool(jnp.allclose(log_total, jnp.log(3.5)))
True
Values far below the smallest float still have a log-integral:
>>> tiny = jnp.full(2, -800.0)
>>> bool(jnp.allclose(log_integrate(jnp.array([0.5, 0.5]), tiny), -800.0))
True
A mask drops a node:
>>> keep = jnp.array([True, False])
>>> masked = log_integrate(
... weights, jnp.log(jnp.array([2.0, 4.0])), where=keep
... )
>>> bool(jnp.allclose(masked, jnp.log(0.5)))
True
Source code in src/integrix/_src/rule/_log.py
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
integrate
¶
integrate(rule: Rule, values_or_fn: ArrayLike | Callable[..., Any], *, axis: Axes | None = None, args: Any = None, where: ArrayLike | None = None, nan_policy: NanPolicy = 'propagate') -> Integral
Integrate values, or an integrand, against a rule's weights.
I ≈ ∑ₙ wₙ f(xₙ), contracted over one axis or several and batched over the
rest; with a mask or nan_policy="omit", I ≈ ∑ over kept n of wₙ fₙ.
Algorithm
- Read the weights w: a rule's
weights, the outer product of a tuple of 1-D rules (a tensor rule, one axis each), or a plain array (a spectral response, cell areas). - Get the samples: the values as given, or
f(nodes)(withargs,f(nodes, args)), whose node axes come first. - Keep the samples where
whereholds and, withnan_policy="omit", that are not NaN; put 0 in place of every other sample, so it adds nothing and gets a zero gradient. - Move the node axes first and flatten them into one, (N, …).
- Cast w to the samples' dtype and sum wₙ fₙ pairwise over N.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
Rule
|
A rule with |
required |
values_or_fn
|
ArrayLike | Callable[..., Any]
|
The samples fₙ, or a callable f evaluated at the rule's
|
required |
axis
|
Axes | None
|
The axes of the samples to integrate over, one per weights
axis. |
None
|
args
|
Any
|
Extra arguments passed to the callable as |
None
|
where
|
ArrayLike | None
|
A boolean mask that broadcasts against the samples (a plume
over an image, the valid levels of each column); samples where it
is false are left out. |
None
|
nan_policy
|
NanPolicy
|
|
'propagate'
|
Returns:
| Type | Description |
|---|---|
Integral
|
An |
Integral
|
the integrated axes; |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the rule has no weights, a tensor rule holds a
rule that is not 1-D, a callable meets a rule without |
Examples:
>>> import jax.numpy as jnp
>>> weights = jnp.array([0.25, 0.75])
>>> total = integrate(weights, jnp.array([[1.0, 2.0], [10.0, 20.0]]))
>>> bool(jnp.allclose(total.value, jnp.array([1.75, 17.5])))
True
A callable returns its node axis first:
>>> import equinox as eqx
>>> class Trapezoid(eqx.Module):
... nodes: jnp.ndarray
... weights: jnp.ndarray
>>> rule = Trapezoid(jnp.array([0.0, 1.0]), jnp.array([0.5, 0.5]))
>>> total = integrate(rule, lambda x: jnp.outer(x, jnp.array([1.0, 10.0])))
>>> bool(jnp.allclose(total.value, jnp.array([0.5, 5.0])))
True
A tensor rule integrates two axes, one rule each:
A mask and a NaN, both left out (np.nansum over the mask):
>>> enhancement = jnp.array([1.0, jnp.nan, 3.0, 4.0])
>>> plume = jnp.array([True, True, True, False])
>>> mass = integrate(
... jnp.ones(4), enhancement, where=plume, nan_policy="omit"
... )
>>> bool(mass.value == 4.0)
True
Source code in src/integrix/_src/rule/_rule.py
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 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 | |
weighted_mean
¶
weighted_mean(rule: Rule, values: ArrayLike, *, weight: ArrayLike, axis: Axes | None = None, where: ArrayLike | None = None, nan_policy: NanPolicy = 'propagate') -> Inexact[Array, '*batch']
The mean of the values under a weight function, with one rule for both sums.
L̄ = ∫ L(x) R(x) dx / ∫ R(x) dx ≈ ∑ₙ wₙ Rₙ Lₙ / ∑ₙ wₙ Rₙ, a band radiance
from a spectrum L and a spectral response R, say. With a mask or
nan_policy="omit", both sums run over the kept samples K only, so the
mean renormalises: ∑ over K of wₙ Rₙ Lₙ / ∑ over K of wₙ Rₙ. A batch of
weight functions R_b (an instrument's band responses) gives one mean per
b, L̄_b = ∑ₙ wₙ R_bn Lₙ / ∑ₙ wₙ R_bn.
Algorithm
- Place R along the integrated axes (or use it as given when it has the values' shape); a batch of weight functions keeps its leading axes.
- Keep the samples as
integratedoes, with 0 in place of the others in both sums. - Sum wₙ Rₙ Lₙ and wₙ Rₙ pairwise with the same weights.
- Divide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
Rule
|
A rule with |
required |
values
|
ArrayLike
|
The values Lₙ. |
required |
weight
|
ArrayLike
|
The weight function Rₙ, with the shape of |
required |
axis
|
Axes | None
|
The axes to average over, as in |
None
|
where
|
ArrayLike | None
|
A boolean mask that broadcasts against the values, as in
|
None
|
nan_policy
|
NanPolicy
|
|
'propagate'
|
Returns:
| Type | Description |
|---|---|
Inexact[Array, '*batch']
|
The weighted mean, with the shape of |
Inexact[Array, '*batch']
|
axes, followed by a batch of weights' leading axes: radiance (P, G) |
Inexact[Array, '*batch']
|
and responses (B, G) give (P, B). A weight that sums to zero, or a |
Inexact[Array, '*batch']
|
mean with no kept sample, gives |
Raises:
| Type | Description |
|---|---|
ValidationError
|
As |
Examples:
>>> import jax.numpy as jnp
>>> weights = jnp.array([0.5, 1.0, 0.5])
>>> radiance = jnp.array([[1.0, 2.0, 3.0], [2.0, 2.0, 2.0]])
>>> response = jnp.array([0.0, 1.0, 1.0])
>>> band = weighted_mean(weights, radiance, weight=response)
>>> bool(jnp.allclose(band, jnp.array([7.0 / 3.0, 2.0])))
True
A NaN sample is left out and the mean renormalises over the rest:
>>> cloudy = jnp.array([[1.0, 2.0, jnp.nan]])
>>> band = weighted_mean(
... weights, cloudy, weight=response, nan_policy="omit"
... )
>>> bool(jnp.allclose(band, jnp.array([2.0])))
True
Three band responses at once, one mean per pixel and band:
>>> responses = jnp.array(
... [[0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 1.0, 1.0]]
... )
>>> bands = weighted_mean(weights, radiance, weight=responses)
>>> bands.shape
(2, 3)
>>> expected = jnp.array([[7.0 / 3.0, 1.0, 2.0], [2.0, 2.0, 2.0]])
>>> bool(jnp.allclose(bands, expected))
True
Source code in src/integrix/_src/rule/_rule.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |