Expand description
Scalar special-function primitives shared across the workspace.
These are pure (std/libm-only) numeric kernels with no upward crate
dependencies, so they live in the lowest crate (gam-math) and can be
consumed by any term/basis/inference code without inducing an SCC edge.
Functions§
- bd0
- The Bregman divergence
bd0(x, m) = x·ln(x/m) + m − xof the Poisson deviance (Loader’sbd0), summed as a series in(x − m)/(x + m)when the two arguments are within 20% of each other so the two ~equal logarithms never cancel.bd0(0, m) = mexactly. - bernoulli_
kl_ from_ logits - Bernoulli KL divergence in natural coordinates,
KL(σ(a) ‖ σ(b)), without subtracting an entropy from a cross entropy. For|b − a| ≤ 1/2only second-order remainders are evaluated; the tail branches orient the event so the reference probability never rounds to one. - bessel_
i0_ centered_ second_ log_ derivative_ from_ log_ abs - Second log-scale derivative of the centered Bessel primitive:
d²/d(log η)²[log I0(η) − η], i.e. the derivative of the third termd1returned bybessel_i0_centered_terms(d1 = η d/dη[log I0(η) − η]). - bessel_
i0_ centered_ terms - Overflow-free centered Bessel value, ratio, and log-scale derivative.
- bessel_
i0_ centered_ terms_ from_ log_ abs - Stable centered Bessel terms when only
log(|eta|)is representable. - bessel_
i0_ log_ and_ ratio - Overflow-free
(log I0(eta), I1(|eta|) / I0(|eta|)). - bessel_
i0_ log_ minus_ abs_ and_ ratio - Overflow-free
(log I0(eta) - |eta|, I1(|eta|) / I0(|eta|)). - binomial_
coefficient_ f64 - Numerically stable
C(n,k) = n! / (k!·(n−k)!)asf64. Uses the symmetryC(n,k) = C(n, n−k)to keep the loop countmin(k, n−k)and the multiplicative recurrenceC(n,j+1) = C(n,j)·(n−j)/(j+1), avoiding the overflow of separate factorial evaluations. Returns0.0fork > nand exact integer results within2^53. - digamma
- Digamma
ψ(x) = d/dx ln Γ(x), forx > 0;NaNotherwise. - expm1_
minus_ x e^x − 1 − x, the second-order remainder of the exponential. For|x| ≤ 1/2the Taylor tail is summed directly so the result does not cancel againstx; beyond thatexp_m1is accurate on its own.- exprel
- The relative exponential
exprel(x) = (e^x − 1) / x, equal to1atx = 0and summed as a series for|x| ≤ 1/2. - gauss_
legendre - Gauss-Legendre nodes and weights on
[-1, 1]fornpoints, computed via Newton iteration on the Legendre-polynomial roots (Bonnet’s three-term recurrence, cosine initial guess). Returns(nodes, weights)with nodes ascending; for oddnthe central node is exactly0.0. - gauss_
lobatto - Gauss-Lobatto nodes and weights on
[-1, 1], ascending, forn >= 2. - log1p_
minus_ x ln(1 + x) − x, the second-order remainder of the logarithm, with the same small-argument series treatment asexpm1_minus_x.- log_
abs_ one_ minus_ exp ln|1 − e^x|forx ≠ 0, routed throughcrate::probability::log1mexp_positiveon both sides of the origin.- log_
exprel ln(exprel(x)), with the large-|x|branches written so that no exponential of a positive argument is ever formed.- logaddexp
ln(e^a + e^b)without forming either exponential at full scale; returns-∞when both arguments are-∞.- logistic
- The logistic function
σ(x) = 1 / (1 + e^{-x}), oriented so the exponential is always of a non-positive argument. - pentagamma
- Pentagamma
ψ₃(x) = d⁴/dx⁴ ln Γ(x), forx > 0;NaNotherwise. - positive_
frexp - Exact power-of-two decomposition
x = mantissa · 2^exponentfor a positive finitef64, including subnormals. The mantissa lies in[1, 2). - scale_
normalized_ power_ of_ two mantissa · 2^exponentfor a positive mantissa, renormalising the mantissa into[1, 2)first so the result overflows or underflows only when the finalf64itself is unrepresentable. Subnormal results are formed by scaling in units of the least positive subnormal, so IEEE rounds the final value once instead of underflowing an intermediate.- scaled_
positive_ product_ quotient a·b·c/dfor positive finite inputs, carrying the binary exponent separately so an intermediate overflow or underflow cannot change a representable final result.- softplus
softplus(x) = ln(1 + e^x), evaluated asmax(x, 0) + ln(1 + e^{-|x|})so that neither tail overflows and the small-xresult keeps full relative accuracy.- stable_
polynomial_ times_ exp_ neg - Evaluate
(Σ_k coeffs[k]·x^k) · exp(−x)without overflow. For moderatex ≤ 600uses Horner +exp(−x)directly; for very largexrewritesxᵈ · exp(−x) = exp(d·ln x − x)and runs Horner in1/x, which keeps both the polynomial sum and its multiplier inside double range. Returns0.0for non-finitexor emptycoeffs. - tetragamma
- Tetragamma
ψ₂(x) = d³/dx³ ln Γ(x), forx > 0;NaNotherwise. - trigamma
- Trigamma
ψ₁(x) = d²/dx² ln Γ(x), forx > 0;NaNotherwise. - xlogy
x·ln(y)with the convention0·ln(0) = 0used by every deviance kernel.