nx_quadrature.nx
buildroot/runtime/nx_quadrature.nx
about
nx_quadrature.nx -- ADAPTIVE QUADRATURE WITH A MEASURED ERROR BOUND (computational CM4, 2026-09-03).
ni_quad_adaptive integrates f over [a, b] to a requested absolute tolerance by RECURSIVE SUBDIVISION driven by
a MEASURED local error: on each interval it compares Simpson over the whole interval with Simpson over the two
halves; the Richardson estimate |S_left + S_right - S_whole| / 15 is the local error of the refined value
(Simpson's rule is exact through cubics, so its error scales as h^5 and halving the step divides it by 16).
An interval whose estimate meets its share of the tolerance is accepted at the refined value plus the
Richardson correction; otherwise it is split and each half gets half the tolerance.
THE CONTRACT THE MATRIX ROW NAMES: this organ REFUSES rather than silently returning a wrong answer. When the
requested tolerance cannot be met on the interval -- the recursion reaches its depth bound or the evaluation
budget with the estimate still above tolerance, or the integrand returns a non-finite value -- the status
says so and the caller receives the best value AND the achieved error bound, never a bare number that looks
converged. A tolerance below what f64 rounding can resolve on the result's scale is the textbook case: the
estimate floors at rounding noise and the organ reports REFUSED-TOLERANCE instead of recursing to a lie.
Bounds are DERIVED, not tuned (rule 11): NQ_MAX_DEPTH = 50 subdivisions halves an interval 2^50 times, past
which the midpoint is not representable apart from its endpoints in binary64 (53-bit significand); the
evaluation budget follows from it. Every call reports its evaluation count so a caller can price it.
API (one ruler, every caller composes it):
ni_quad_adaptive(f, a, b, tol, out) -> status; out[0] = value, out[1] = error bound, out[2] = evaluations
status NQ_OK 0 | NQ_REFUSED_TOL 1 | NQ_BAD_INPUT 2 (a > b, tol <= 0, or out null) | NQ_NONFINITE 3
Pure f64 arithmetic on the sovereign compiler (LN16). license_tier: ORIGINAL lib (no main)
dependencies 1 imports · 1 importers
imports: nx_syscalls.nx
imported by: nx_quadrature_gate.nx
structs
| none |
consts
| 27 | const NQ_OK: i64 = 0 |
| 28 | const NQ_REFUSED_TOL: i64 = 1 |
| 29 | const NQ_BAD_INPUT: i64 = 2 |
| 30 | const NQ_NONFINITE: i64 = 3 |
| 31 | const NQ_MAX_DEPTH: i64 = 50 // 2^50 halvings: past this a binary64 midpoint collapses onto an endpoint |
| 32 | const NQ_MAX_EVALS: i64 = 4194304 // 2^22 integrand evaluations: the budget a depth-50 pathological tree may not exceed |
| 33 | const NQ_OUT_VALUE: i64 = 0 |
| 34 | const NQ_OUT_ERR: i64 = 1 |
| 35 | const NQ_OUT_EVALS: i64 = 2 |
| 36 | const NQ_OUT_SLOTS: i64 = 4 |
| 37 | const NQ_OUT_BYTES: i64 = 32 // NQ_OUT_SLOTS * 8 |
functions
| 41 | func nq_finite(x: f64) -> i64 |
| 47 | func nq_abs(x: f64) -> f64 { if x < 0.0 { return 0.0 - x } return x } called by 1: nq_node |
| 50 | func nq_simpson(a: f64, b: f64, fa: f64, fm: f64, fb: f64) -> f64 |
| 56 | func nq_node(f: func(f64) -> f64, a: f64, b: f64, fa: f64, fm: f64, fb: f64, whole: f64, tol: f64, depth: i64, st: *f64, cnt: *i64) -> f64 |
| 82 | func ni_quad_adaptive(f: func(f64) -> f64, a: f64, b: f64, tol: f64, out: *f64) -> i64 |