omnibias.core.verified.quadrature
Coefficient recurrences, activation contracts, jet algebra, and verified arithmetic shared by the numerical backends.
import omnibias.core.verified.quadratureModule contract & mathematical conventions
Rigorous quadrature with a *derived* error term.
The certified-evidence stack currently bounds an integral with a user-supplied heuristic
padding (interval_trapezoid_bound). This module replaces that with the
textbook composite-trapezoid error bound
\int_a^b f \;-\; T_n(f) \;=\; -\,\frac{(b-a)\,h^2}{12}\, f''(\xi), \qquad \xi \in [a, b],\; h = (b-a)/n,
so given a rigorous enclosure M2 \supseteq f''([a, b]) the integral is bracketed
*without any tunable fudge factor*. The remainder interval is genuinely derived
from the second-derivative enclosure -- exactly the kind of term a Taylor model
(~omnibias.core.verified.taylor_model.TaylorModel) produces.
A composite midpoint variant is included for completeness.
Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.
DerivOracle
omnibias.core.verified.DerivOracleSource ↗DerivOracle = Callable[[int, Interval], Interval]A derivative-enclosure oracle deriv(k, x) bounding f^{(k)} over x.
Read the implementation
DerivOracle = Callable[[int, Interval], Interval]Other public import paths
omnibias.core.verified.DerivOracleomnibias.core.verified.quadrature.DerivOracle
GAUSS_LEGENDRE_MAX_N
omnibias.core.verified.GAUSS_LEGENDRE_MAX_NSource ↗GAUSS_LEGENDRE_MAX_N: int = max(_GL_RULES)The largest n for which a certified Gauss-Legendre rule is tabulated.
Read the implementation
GAUSS_LEGENDRE_MAX_N: int = max(_GL_RULES)Other public import paths
omnibias.core.verified.GAUSS_LEGENDRE_MAX_Nomnibias.core.verified.quadrature.GAUSS_LEGENDRE_MAX_N
IntervalFn
omnibias.core.verified.IntervalFnSource ↗IntervalFn = Callable[[Interval], Interval]A guaranteed enclosure oracle f_iv(x) bounding f over an interval x.
Read the implementation
IntervalFn = Callable[[Interval], Interval]Other public import paths
omnibias.core.verified.IntervalFnomnibias.core.verified.quadrature.IntervalFn
QuadEstimate
omnibias.core.verified.QuadEstimateSource ↗class QuadEstimateDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
A **numerical** quadrature estimate with an a-posteriori error indicator.
Unlike the certified rules above, this is *not* a guaranteed enclosure: it is a
high-accuracy estimate plus a heuristic error from the last refinement step,
honestly labelled numerical. Use it for integrands where the certified
rules do not apply (endpoint singularities), and prefer a certified rule when
a guaranteed bound is required.
Parameters
| Name | Type & default | Contract |
|---|---|---|
valuedataclass field | floatRequired | See the operation contract and implementation below. |
error_estimatedataclass field | floatRequired | See the operation contract and implementation below. |
labeldataclass field | strDefault: 'numerical' | See the operation contract and implementation below. |
Regression example · handles endpoint singularity
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_handles_endpoint_singularity(self) -> None:
# int_0^1 1/sqrt(x) dx = 2 (an endpoint singularity that defeats derivative bounds).
est = tanh_sinh_estimate(lambda x: 1.0 / math.sqrt(x) if x > 0 else 0.0, 0.0, 1.0, level=8)
assert isinstance(est, QuadEstimate)
assert est.label == "numerical"
assert abs(est.value - 2.0) < 1e-5python -m pytest packages/omnibias-core/tests/verified/test_quadrature_rules.py -k test_handles_endpoint_singularity -qFields & methods
valuefield
value: floatDeclared float field on QuadEstimate. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗error_estimatefield
error_estimate: floatDeclared float field on QuadEstimate. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗labelfield
label: str = 'numerical'Declared str field on QuadEstimate. Default expression: 'numerical'.
Read the implementation
class QuadEstimate:
"""A **numerical** quadrature estimate with an a-posteriori error indicator.
Unlike the certified rules above, this is *not* a guaranteed enclosure: it is a
high-accuracy estimate plus a heuristic error from the last refinement step,
honestly labelled ``numerical``. Use it for integrands where the certified
rules do not apply (endpoint singularities), and prefer a certified rule when
a guaranteed bound is required.
"""
value: float
error_estimate: float
label: str = "numerical"Other public import paths
omnibias.core.verified.QuadEstimateomnibias.core.verified.quadrature.QuadEstimate
clenshaw_curtis_integral
omnibias.core.verified.clenshaw_curtis_integralSource ↗clenshaw_curtis_integral(f: IntervalFn, a: float, b: float, *, n: int, deriv_np1_bound: Interval) -> IntervalRigorous Clenshaw-Curtis enclosure (Chebyshev-Lobatto interpolatory rule).
Integrates the degree-n Chebyshev interpolant of f at the n + 1
Lobatto nodes x_k = cos(k pi / n) (enclosed with cos_iv, weights via
_cc_weights). The remainder is the *interpolation* error
\int_a^b (f - p_n) bounded rigorously (if conservatively) by
|E| \le (b-a) \frac{(b-a)^{n+1}}{(n+1)!}\, \max_{[a,b]} |f^{(n+1)}|,using \max |\omega_{n+1}| \le (b-a)^{n+1}. deriv_np1_bound encloses
f^{(n+1)} over [a, b]. A weaker (looser) bound than Gauss for smooth
f, so it only ever widens the certified gap. Requires a <= b, n>=2.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | IntervalFnRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
nkeyword-only | intRequired | See the operation contract and implementation below. |
deriv_np1_boundkeyword-only | IntervalRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
n < 2, raisesValueError(f'clenshaw_curtis_integral needs n >= 2, got {n}'). - When
a > b, raisesValueError(f'clenshaw_curtis_integral requires a <= b, got a={a}, b={b}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · clenshaw curtis encloses sin
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_clenshaw_curtis_encloses_sin(self) -> None:
enc = clenshaw_curtis_integral(_sin_iv, 0.0, math.pi, n=8, deriv_np1_bound=Interval(-1.0, 1.0))
assert enc.contains(2.0)python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestCertifiedRulesEncloseExact::test_clenshaw_curtis_encloses_sin" -qRead the implementation
def clenshaw_curtis_integral(
f: IntervalFn,
a: float,
b: float,
*,
n: int,
deriv_np1_bound: Interval,
) -> Interval:
r"""Rigorous Clenshaw-Curtis enclosure (Chebyshev-Lobatto interpolatory rule).
Integrates the degree-``n`` Chebyshev interpolant of ``f`` at the ``n + 1``
Lobatto nodes ``x_k = cos(k pi / n)`` (enclosed with :func:`cos_iv`, weights via
:func:`_cc_weights`). The remainder is the *interpolation* error
``\int_a^b (f - p_n)`` bounded rigorously (if conservatively) by
.. math::
|E| \le (b-a) \frac{(b-a)^{n+1}}{(n+1)!}\, \max_{[a,b]} |f^{(n+1)}|,
using ``\max |\omega_{n+1}| \le (b-a)^{n+1}``. ``deriv_np1_bound`` encloses
``f^{(n+1)}`` over ``[a, b]``. A weaker (looser) bound than Gauss for smooth
``f``, so it only ever widens the certified gap. Requires ``a <= b``, ``n>=2``.
"""
if n < 2:
raise ValueError(f"clenshaw_curtis_integral needs n >= 2, got {n}")
if a > b:
raise ValueError(f"clenshaw_curtis_integral requires a <= b, got a={a}, b={b}")
mid = (Interval.point(a) + Interval.point(b)) * Interval.point(0.5)
half = (Interval.point(b) - Interval.point(a)) * Interval.point(0.5)
weights = _cc_weights(n)
acc = Interval.point(0.0)
for k in range(n + 1):
x_k = cos_iv(PI_IV * Interval.from_rational(k) * Interval.from_rational(Fraction(1, n)))
t_k = mid + half * x_k
acc = acc + weights[k] * f(t_k)
main = half * acc
span = Interval.point(b) - Interval.point(a)
bound = (
span
* span.pow_int(n + 1)
* Interval.from_rational(Fraction(1, math.factorial(n + 1)))
* Interval.point(deriv_np1_bound.mag)
).hi
return main + Interval(-bound, bound)Other public import paths
omnibias.core.verified.clenshaw_curtis_integralomnibias.core.verified.quadrature.clenshaw_curtis_integral
euler_maclaurin_quadrature
omnibias.core.verified.euler_maclaurin_quadratureSource ↗euler_maclaurin_quadrature(f: IntervalFn, deriv: DerivOracle, a: float, b: float, *, panels: int, terms: int) -> IntervalRigorous high-order integral enclosure via the Euler-Maclaurin trapezoid.
Corrects the composite trapezoid T_h by its Euler-Maclaurin endpoint terms,
\int_a^b f = T_h
- \sum_{k=1}^{p} \frac{B_{2k}}{(2k)!} h^{2k}
\bigl(f^{(2k-1)}(b) - f^{(2k-1)}(a)\bigr) - R_p,with the classical remainder |R_p| \le |B_{2p}|/(2p)! \, h^{2p} (b-a)
\max_{[a,b]} |f^{(2p)}|. f encloses f at the trapezoid nodes;
deriv(k, x) encloses f^{(k)}. This is the certified Romberg-class rule:
each extra Bernoulli pair raises the order by two. Requires a <= b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | IntervalFnRequired | See the operation contract and implementation below. |
derivpositional-or-keyword | DerivOracleRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
panelskeyword-only | intRequired | See the operation contract and implementation below. |
termskeyword-only | intRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
panels < 1, raisesValueError(f'euler_maclaurin_quadrature needs panels >= 1, got {panels}'). - When
terms < 1, raisesValueError(f'terms (p) must be >= 1, got {terms}'). - When
a > b, raisesValueError(f'euler_maclaurin_quadrature requires a <= b, got a={a}, b={b}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · euler maclaurin and romberg enclose sin
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_euler_maclaurin_and_romberg_enclose_sin(self) -> None:
# int_0^pi sin = 2.
exact = 2.0
for fn in (euler_maclaurin_quadrature, romberg_integral):
enc = fn(_sin_iv, _sin_deriv, 0.0, math.pi, panels=16, terms=3)
assert enc.contains(exact), fn.__name__python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestCertifiedRulesEncloseExact::test_euler_maclaurin_and_romberg_enclose_sin" -qRead the implementation
def euler_maclaurin_quadrature(
f: IntervalFn,
deriv: DerivOracle,
a: float,
b: float,
*,
panels: int,
terms: int,
) -> Interval:
r"""Rigorous high-order integral enclosure via the Euler-Maclaurin trapezoid.
Corrects the composite trapezoid ``T_h`` by its Euler-Maclaurin endpoint terms,
.. math::
\int_a^b f = T_h
- \sum_{k=1}^{p} \frac{B_{2k}}{(2k)!} h^{2k}
\bigl(f^{(2k-1)}(b) - f^{(2k-1)}(a)\bigr) - R_p,
with the classical remainder ``|R_p| \le |B_{2p}|/(2p)! \, h^{2p} (b-a)
\max_{[a,b]} |f^{(2p)}|``. ``f`` encloses ``f`` at the trapezoid nodes;
``deriv(k, x)`` encloses ``f^{(k)}``. This is the certified Romberg-class rule:
each extra Bernoulli pair raises the order by two. Requires ``a <= b``.
"""
if panels < 1:
raise ValueError(f"euler_maclaurin_quadrature needs panels >= 1, got {panels}")
if terms < 1:
raise ValueError(f"terms (p) must be >= 1, got {terms}")
if a > b:
raise ValueError(f"euler_maclaurin_quadrature requires a <= b, got a={a}, b={b}")
nodes = _equispaced_node_intervals(a, b, panels)
vals = [f(node) for node in nodes]
span = Interval.point(b) - Interval.point(a)
h = span * Interval.from_rational(Fraction(1, panels))
trap = (vals[0] + vals[-1]) * Interval.point(0.5)
for k in range(1, panels):
trap = trap + vals[k]
total = h * trap
a_iv = Interval.point(a)
b_iv = Interval.point(b)
for k in range(1, terms + 1):
coeff = Interval.from_rational(bernoulli_number_exact(2 * k) / math.factorial(2 * k))
total = total - coeff * h.pow_int(2 * k) * (deriv(2 * k - 1, b_iv) - deriv(2 * k - 1, a_iv))
box = Interval(a, b)
f2p = deriv(2 * terms, box)
bound = (
Interval.from_rational(abs(bernoulli_number_exact(2 * terms)) / math.factorial(2 * terms))
* h.pow_int(2 * terms)
* span
* Interval.point(f2p.mag)
).hi
return total + Interval(-bound, bound)Other public import paths
omnibias.core.verified.euler_maclaurin_quadratureomnibias.core.verified.quadrature.euler_maclaurin_quadrature
gauss_legendre_integral
omnibias.core.verified.gauss_legendre_integralSource ↗gauss_legendre_integral(f: IntervalFn, a: float, b: float, *, n: int, deriv_2n_bound: Interval) -> IntervalRigorous n-point Gauss-Legendre enclosure with the classical remainder.
Maps the certified [-1, 1] nodes/weights to [a, b], sums
(b-a)/2 \sum_i w_i f(t_i) with f the enclosure oracle, and adds the
guaranteed Gauss error term
E_n = \frac{(b-a)^{2n+1} (n!)^4}{(2n+1)\,[(2n)!]^3}\, f^{(2n)}(\xi),bounded by deriv_2n_bound (a guaranteed enclosure of f^{(2n)} over
[a, b]). Exact for polynomials of degree <= 2n-1. Requires a <= b
and 1 <= n <= GAUSS_LEGENDRE_MAX_N.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | IntervalFnRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
nkeyword-only | intRequired | See the operation contract and implementation below. |
deriv_2n_boundkeyword-only | IntervalRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
n not in _GL_RULES, raisesValueError(f'no tabulated Gauss-Legendre rule for n={n} (have 1..{GAUSS_LEGENDRE_MAX_N})'). - When
a > b, raisesValueError(f'gauss_legendre_integral requires a <= b, got a={a}, b={b}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · reversed limits raise
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_reversed_limits_raise(self) -> None:
with pytest.raises(ValueError):
gauss_legendre_integral(_exp_iv, 1.0, 0.0, n=3, deriv_2n_bound=Interval.point(1.0))python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestInputGuards::test_reversed_limits_raise" -qRegression example · clenshaw curtis encloses sin
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_clenshaw_curtis_encloses_sin(self) -> None:
enc = clenshaw_curtis_integral(_sin_iv, 0.0, math.pi, n=8, deriv_np1_bound=Interval(-1.0, 1.0))
assert enc.contains(2.0)python -m pytest packages/omnibias-core/tests/verified/test_quadrature_rules.py -k test_clenshaw_curtis_encloses_sin -qRead the implementation
def gauss_legendre_integral(
f: IntervalFn,
a: float,
b: float,
*,
n: int,
deriv_2n_bound: Interval,
) -> Interval:
r"""Rigorous ``n``-point Gauss-Legendre enclosure with the classical remainder.
Maps the certified ``[-1, 1]`` nodes/weights to ``[a, b]``, sums
``(b-a)/2 \sum_i w_i f(t_i)`` with ``f`` the enclosure oracle, and adds the
guaranteed Gauss error term
.. math::
E_n = \frac{(b-a)^{2n+1} (n!)^4}{(2n+1)\,[(2n)!]^3}\, f^{(2n)}(\xi),
bounded by ``deriv_2n_bound`` (a guaranteed enclosure of ``f^{(2n)}`` over
``[a, b]``). Exact for polynomials of degree ``<= 2n-1``. Requires ``a <= b``
and ``1 <= n <= GAUSS_LEGENDRE_MAX_N``.
"""
if n not in _GL_RULES:
raise ValueError(f"no tabulated Gauss-Legendre rule for n={n} (have 1..{GAUSS_LEGENDRE_MAX_N})")
if a > b:
raise ValueError(f"gauss_legendre_integral requires a <= b, got a={a}, b={b}")
mid = (Interval.point(a) + Interval.point(b)) * Interval.point(0.5)
half = (Interval.point(b) - Interval.point(a)) * Interval.point(0.5)
acc = Interval.point(0.0)
for (nlo, nhi), (wlo, whi) in _GL_RULES[n]:
t = mid + half * Interval(nlo, nhi)
acc = acc + Interval(wlo, whi) * f(t)
main = half * acc
span = Interval.point(b) - Interval.point(a)
const = Fraction(math.factorial(n) ** 4, (2 * n + 1) * math.factorial(2 * n) ** 3)
err = Interval.from_rational(const) * span.pow_int(2 * n + 1) * deriv_2n_bound
return main + errOther public import paths
omnibias.core.verified.gauss_legendre_integralomnibias.core.verified.quadrature.gauss_legendre_integral
gauss_legendre_nodes
omnibias.core.verified.gauss_legendre_nodesSource ↗gauss_legendre_nodes(n: int, a: float, b: float) -> list[Interval]The n certified Gauss-Legendre node enclosures mapped to [a, b].
Useful when the caller wants to evaluate its own oracle at the exact nodes.
Parameters
| Name | Type & default | Contract |
|---|---|---|
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
list[Interval]Explicit validation & failure cases
- When
n not in _GL_RULES, raisesValueError(f'no tabulated Gauss-Legendre rule for n={n} (have 1..{GAUSS_LEGENDRE_MAX_N})').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · unknown n raises
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_unknown_n_raises(self) -> None:
with pytest.raises(ValueError):
gauss_legendre_nodes(99, 0.0, 1.0)python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestGaussNodes::test_unknown_n_raises" -qRegression example · nodes are mapped and symmetric
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_nodes_are_mapped_and_symmetric(self) -> None:
nodes = gauss_legendre_nodes(4, -1.0, 1.0)
assert len(nodes) == 4
# symmetric about 0
assert nodes[0].mid == pytest.approx(-nodes[-1].mid, abs=1e-12)python -m pytest packages/omnibias-core/tests/verified/test_quadrature_rules.py -k test_nodes_are_mapped_and_symmetric -qRead the implementation
def gauss_legendre_nodes(n: int, a: float, b: float) -> list[Interval]:
"""The ``n`` certified Gauss-Legendre node enclosures mapped to ``[a, b]``.
Useful when the caller wants to evaluate its own oracle at the exact nodes.
"""
if n not in _GL_RULES:
raise ValueError(f"no tabulated Gauss-Legendre rule for n={n} (have 1..{GAUSS_LEGENDRE_MAX_N})")
mid = (Interval.point(a) + Interval.point(b)) * Interval.point(0.5)
half = (Interval.point(b) - Interval.point(a)) * Interval.point(0.5)
return [mid + half * Interval(nlo, nhi) for (nlo, nhi), _ in _GL_RULES[n]]Other public import paths
omnibias.core.verified.gauss_legendre_nodesomnibias.core.verified.quadrature.gauss_legendre_nodes
midpoint_integral
omnibias.core.verified.midpoint_integralSource ↗midpoint_integral(midpoint_values: Sequence[Interval], a: float, b: float, second_deriv_bound: Interval) -> IntervalRigorous composite-midpoint enclosure with the +(b-a)h^2/24 f'' term.
Requires a <= b (reversed limits raise ValueError).
Parameters
| Name | Type & default | Contract |
|---|---|---|
midpoint_valuespositional-or-keyword | Sequence[Interval]Required | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
second_deriv_boundpositional-or-keyword | IntervalRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
n < 1, raisesValueError('need at least one panel'). - When
a > b, raisesValueError(f'midpoint_integral requires a <= b (got a={a}, b={b}); the composite rule and its derived remainder assume nodes ordered left-to-right across [a, b]. Integrate over [b, a] and negate.').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · midpoint rejects reversed limits
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_midpoint_rejects_reversed_limits() -> None:
mids = [Interval.point(0.25), Interval.point(0.75)]
with pytest.raises(ValueError, match="a <= b"):
midpoint_integral(mids, 1.0, 0.0, Interval.point(0.0))python -m pytest "packages/omnibias-core/tests/verified/test_numerics.py::test_midpoint_rejects_reversed_limits" -qRead the implementation
def midpoint_integral(
midpoint_values: Sequence[Interval],
a: float,
b: float,
second_deriv_bound: Interval,
) -> Interval:
"""Rigorous composite-midpoint enclosure with the ``+(b-a)h^2/24 f''`` term.
Requires ``a <= b`` (reversed limits raise :class:`ValueError`).
"""
n = len(midpoint_values)
if n < 1:
raise ValueError("need at least one panel")
if a > b:
raise ValueError(
f"midpoint_integral requires a <= b (got a={a}, b={b}); the "
"composite rule and its derived remainder assume nodes ordered "
"left-to-right across [a, b]. Integrate over [b, a] and negate."
)
span = Interval.point(b) - Interval.point(a)
h = span * Interval.from_rational(Fraction(1, n))
main = sum_intervals(list(midpoint_values)) * h
err = (span * h.pow_int(2) * Interval.from_rational(Fraction(1, 24))) * second_deriv_bound
return main + errOther public import paths
omnibias.core.verified.midpoint_integralomnibias.core.verified.quadrature.midpoint_integral
romberg_integral
omnibias.core.verified.romberg_integralSource ↗romberg_integral(f: IntervalFn, deriv: DerivOracle, a: float, b: float, *, panels: int=8, terms: int=4) -> IntervalRigorous Romberg-class integral enclosure.
Romberg integration is Richardson extrapolation of the composite trapezoid;
because the trapezoid error has the *step-independent-coefficient*
Euler-Maclaurin expansion, eliminating its first terms even powers is the
same asymptotic correction as euler_maclaurin_quadrature. This routine
is that certified correction (a genuine enclosure, unlike a bare Richardson
tableau, which would only enclose the trapezoid *values*, not the integral).
Requires a <= b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | IntervalFnRequired | See the operation contract and implementation below. |
derivpositional-or-keyword | DerivOracleRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
panelskeyword-only | intDefault: 8 | See the operation contract and implementation below. |
termskeyword-only | intDefault: 4 | See the operation contract and implementation below. |
IntervalExecuted regression · euler maclaurin and romberg enclose sin
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_euler_maclaurin_and_romberg_enclose_sin(self) -> None:
# int_0^pi sin = 2.
exact = 2.0
for fn in (euler_maclaurin_quadrature, romberg_integral):
enc = fn(_sin_iv, _sin_deriv, 0.0, math.pi, panels=16, terms=3)
assert enc.contains(exact), fn.__name__python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestCertifiedRulesEncloseExact::test_euler_maclaurin_and_romberg_enclose_sin" -qRead the implementation
def romberg_integral(
f: IntervalFn,
deriv: DerivOracle,
a: float,
b: float,
*,
panels: int = 8,
terms: int = 4,
) -> Interval:
r"""Rigorous Romberg-class integral enclosure.
Romberg integration is Richardson extrapolation of the composite trapezoid;
because the trapezoid error has the *step-independent-coefficient*
Euler-Maclaurin expansion, eliminating its first ``terms`` even powers is the
same asymptotic correction as :func:`euler_maclaurin_quadrature`. This routine
is that certified correction (a genuine enclosure, unlike a bare Richardson
tableau, which would only enclose the trapezoid *values*, not the integral).
Requires ``a <= b``.
"""
return euler_maclaurin_quadrature(f, deriv, a, b, panels=panels, terms=terms)Other public import paths
omnibias.core.verified.romberg_integralomnibias.core.verified.quadrature.romberg_integral
simpson_integral
omnibias.core.verified.simpson_integralSource ↗simpson_integral(f: IntervalFn, a: float, b: float, *, panels: int, fourth_deriv_bound: Interval) -> IntervalRigorous composite Simpson enclosure with the -(b-a)h^4/180 f'''' remainder.
panels must be a positive **even** integer (Simpson pairs the panels). The
quadrature evaluates f at the panels + 1 equispaced node *intervals* via
the enclosure oracle f; the fourth-order Peano remainder
-(b-a) h^4 / 180 \cdot f^{(4)}(\xi) is bounded by fourth_deriv_bound (a
guaranteed enclosure of f^{(4)} over [a, b]). Requires a <= b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | IntervalFnRequired | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
panelskeyword-only | intRequired | See the operation contract and implementation below. |
fourth_deriv_boundkeyword-only | IntervalRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
panels < 2 or panels % 2 != 0, raisesValueError(f'simpson_integral needs a positive even panel count, got {panels}'). - When
a > b, raisesValueError(f'simpson_integral requires a <= b, got a={a}, b={b}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · simpson needs even panels
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_simpson_needs_even_panels(self) -> None:
with pytest.raises(ValueError):
simpson_integral(_exp_iv, 0.0, 1.0, panels=7, fourth_deriv_bound=Interval.point(1.0))python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestInputGuards::test_simpson_needs_even_panels" -qRegression example · simpson encloses exp
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_simpson_encloses_exp(self) -> None:
enc = simpson_integral(_exp_iv, 0.0, 1.0, panels=8, fourth_deriv_bound=exp_iv(Interval(0.0, 1.0)))
assert enc.contains(math.e - 1.0)python -m pytest packages/omnibias-core/tests/verified/test_quadrature_rules.py -k test_simpson_encloses_exp -qRead the implementation
def simpson_integral(
f: IntervalFn,
a: float,
b: float,
*,
panels: int,
fourth_deriv_bound: Interval,
) -> Interval:
r"""Rigorous composite Simpson enclosure with the ``-(b-a)h^4/180 f''''`` remainder.
``panels`` must be a positive **even** integer (Simpson pairs the panels). The
quadrature evaluates ``f`` at the ``panels + 1`` equispaced node *intervals* via
the enclosure oracle ``f``; the fourth-order Peano remainder
``-(b-a) h^4 / 180 \cdot f^{(4)}(\xi)`` is bounded by ``fourth_deriv_bound`` (a
guaranteed enclosure of ``f^{(4)}`` over ``[a, b]``). Requires ``a <= b``.
"""
if panels < 2 or panels % 2 != 0:
raise ValueError(f"simpson_integral needs a positive even panel count, got {panels}")
if a > b:
raise ValueError(f"simpson_integral requires a <= b, got a={a}, b={b}")
nodes = _equispaced_node_intervals(a, b, panels)
vals = [f(node) for node in nodes]
span = Interval.point(b) - Interval.point(a)
h = span * Interval.from_rational(Fraction(1, panels))
weighted = vals[0] + vals[-1]
for k in range(1, panels):
w = 4 if k % 2 == 1 else 2
weighted = weighted + vals[k] * Interval.from_rational(w)
main = h * Interval.from_rational(Fraction(1, 3)) * weighted
err = -(span * h.pow_int(4) * Interval.from_rational(Fraction(1, 180))) * fourth_deriv_bound
return main + errOther public import paths
omnibias.core.verified.simpson_integralomnibias.core.verified.quadrature.simpson_integral
tanh_sinh_estimate
omnibias.core.verified.tanh_sinh_estimateSource ↗tanh_sinh_estimate(f: Callable[[float], float], a: float, b: float, *, level: int=6) -> QuadEstimateDouble-exponential (tanh-sinh) **numerical** estimate for \int_a^b f.
The substitution x = (a+b)/2 + (b-a)/2 \tanh(\tfrac{\pi}{2}\sinh t) clusters
nodes toward the endpoints, so it handles integrable endpoint singularities
that defeat the derivative-bound rules. This returns a QuadEstimate,
**not** a certified interval: a rigorous tanh-sinh error needs a
strip-analyticity bound on f (not a real-derivative bound), which is out of
scope here (honestly labelled numerical). The error indicator is the
change between the last two refinement levels.
Parameters
| Name | Type & default | Contract |
|---|---|---|
fpositional-or-keyword | Callable[[float], float]Required | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
levelkeyword-only | intDefault: 6 | See the operation contract and implementation below. |
QuadEstimateExplicit validation & failure cases
- When
a > b, raisesValueError(f'tanh_sinh_estimate requires a <= b, got a={a}, b={b}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · smooth estimate is accurate
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_smooth_estimate_is_accurate(self) -> None:
est = tanh_sinh_estimate(math.exp, 0.0, 1.0, level=6)
assert abs(est.value - (math.e - 1.0)) < 1e-12python -m pytest "packages/omnibias-core/tests/verified/test_quadrature_rules.py::TestTanhSinhEstimator::test_smooth_estimate_is_accurate" -qRegression example · handles endpoint singularity
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_handles_endpoint_singularity(self) -> None:
# int_0^1 1/sqrt(x) dx = 2 (an endpoint singularity that defeats derivative bounds).
est = tanh_sinh_estimate(lambda x: 1.0 / math.sqrt(x) if x > 0 else 0.0, 0.0, 1.0, level=8)
assert isinstance(est, QuadEstimate)
assert est.label == "numerical"
assert abs(est.value - 2.0) < 1e-5python -m pytest packages/omnibias-core/tests/verified/test_quadrature_rules.py -k test_handles_endpoint_singularity -qRead the implementation
def tanh_sinh_estimate(
f: Callable[[float], float],
a: float,
b: float,
*,
level: int = 6,
) -> QuadEstimate:
r"""Double-exponential (tanh-sinh) **numerical** estimate for ``\int_a^b f``.
The substitution ``x = (a+b)/2 + (b-a)/2 \tanh(\tfrac{\pi}{2}\sinh t)`` clusters
nodes toward the endpoints, so it handles integrable endpoint singularities
that defeat the derivative-bound rules. This returns a :class:`QuadEstimate`,
**not** a certified interval: a rigorous tanh-sinh error needs a
strip-analyticity bound on ``f`` (not a real-derivative bound), which is out of
scope here (honestly labelled ``numerical``). The error indicator is the
change between the last two refinement levels.
"""
if a > b:
raise ValueError(f"tanh_sinh_estimate requires a <= b, got a={a}, b={b}")
mid = 0.5 * (a + b)
half = 0.5 * (b - a)
half_pi = math.pi / 2.0
def _sum_at(h: float) -> float:
total = 0.0
j = 0
while True:
t = j * h
sh = half_pi * math.sinh(t)
x = math.tanh(sh)
w = half_pi * math.cosh(t) / (math.cosh(sh) ** 2)
if 1.0 - abs(x) < 1e-15 and j > 0:
break
contrib = w * (f(mid + half * x) + (f(mid - half * x) if j > 0 else 0.0))
total += contrib
j += 1
if j > 2000:
break
return total * h
prev = half * _sum_at(1.0)
est = prev
err = abs(prev)
for lv in range(1, level + 1):
h = 1.0 / (2**lv)
est = half * _sum_at(h)
err = abs(est - prev)
prev = est
return QuadEstimate(value=est, error_estimate=err)Other public import paths
omnibias.core.verified.tanh_sinh_estimateomnibias.core.verified.quadrature.tanh_sinh_estimate
trapezoid_integral
omnibias.core.verified.trapezoid_integralSource ↗trapezoid_integral(node_values: Sequence[Interval], a: float, b: float, second_deriv_bound: Interval) -> IntervalRigorous enclosure of \int_a^b f from equispaced node enclosures.
node_values are enclosures of f at n+1 equispaced nodes spanning
[a, b]; second_deriv_bound must enclose f'' over all of [a, b].
Requires a <= b (reversed limits raise ValueError).
Parameters
| Name | Type & default | Contract |
|---|---|---|
node_valuespositional-or-keyword | Sequence[Interval]Required | See the operation contract and implementation below. |
apositional-or-keyword | floatRequired | See the operation contract and implementation below. |
bpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
second_deriv_boundpositional-or-keyword | IntervalRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
n < 1, raisesValueError('need at least one panel'). - When
a > b, raisesValueError(f'trapezoid_integral requires a <= b (got a={a}, b={b}); the composite rule and its derived remainder assume nodes ordered left-to-right across [a, b]. Integrate over [b, a] and negate.').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · trapezoid rejects reversed limits
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_trapezoid_rejects_reversed_limits() -> None:
nodes = [Interval.point(0.0), Interval.point(1.0)]
with pytest.raises(ValueError, match="a <= b"):
trapezoid_integral(nodes, 1.0, 0.0, Interval.point(0.0))python -m pytest "packages/omnibias-core/tests/verified/test_numerics.py::test_trapezoid_rejects_reversed_limits" -qRegression example · trapezoid encloses cubic
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_trapezoid_encloses_cubic() -> None:
# f(x)=x^3 on [0,2], exact=4, f''=6x in [0,12].
n = 16
nodes = [Interval.point((2.0 * i / n) ** 3) for i in range(n + 1)]
enc = trapezoid_integral(nodes, 0.0, 2.0, Interval(0.0, 12.0))
assert enc.lo <= 4.0 <= enc.hipython -m pytest packages/omnibias-core/tests/verified/test_numerics.py -k test_trapezoid_encloses_cubic -qRead the implementation
def trapezoid_integral(
node_values: Sequence[Interval],
a: float,
b: float,
second_deriv_bound: Interval,
) -> Interval:
"""Rigorous enclosure of ``\\int_a^b f`` from equispaced node enclosures.
``node_values`` are enclosures of ``f`` at ``n+1`` equispaced nodes spanning
``[a, b]``; ``second_deriv_bound`` must enclose ``f''`` over all of ``[a, b]``.
Requires ``a <= b`` (reversed limits raise :class:`ValueError`).
"""
n = len(node_values) - 1
if n < 1:
raise ValueError("need at least one panel")
if a > b:
raise ValueError(
f"trapezoid_integral requires a <= b (got a={a}, b={b}); the "
"composite rule and its derived remainder assume nodes ordered "
"left-to-right across [a, b]. Integrate over [b, a] and negate."
)
span = Interval.point(b) - Interval.point(a)
h = span * Interval.from_rational(Fraction(1, n))
main = trapezoid_sum(node_values, h)
# error = -(b-a) h^2 / 12 * f''(xi), xi in [a,b]
err = -(span * h.pow_int(2) * Interval.from_rational(Fraction(1, 12))) * second_deriv_bound
return main + errOther public import paths
omnibias.core.verified.trapezoid_integralomnibias.core.verified.quadrature.trapezoid_integral
trapezoid_sum
omnibias.core.verified.trapezoid_sumSource ↗trapezoid_sum(node_values: Sequence[Interval], h: Interval | float) -> IntervalComposite-trapezoid weighted sum h*(f0/2 + f1 + ... + f_{n-1} + fn/2).
Parameters
| Name | Type & default | Contract |
|---|---|---|
node_valuespositional-or-keyword | Sequence[Interval]Required | See the operation contract and implementation below. |
hpositional-or-keyword | Interval | floatRequired | See the operation contract and implementation below. |
IntervalExplicit validation & failure cases
- When
n_nodes < 2, raisesValueError('trapezoid rule needs at least two nodes').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · trapezoid encloses cubic
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_trapezoid_encloses_cubic() -> None:
# f(x)=x^3 on [0,2], exact=4, f''=6x in [0,12].
n = 16
nodes = [Interval.point((2.0 * i / n) ** 3) for i in range(n + 1)]
enc = trapezoid_integral(nodes, 0.0, 2.0, Interval(0.0, 12.0))
assert enc.lo <= 4.0 <= enc.hipython -m pytest "packages/omnibias-core/tests/verified/test_numerics.py::test_trapezoid_encloses_cubic" -qRead the implementation
def trapezoid_sum(node_values: Sequence[Interval], h: Interval | float) -> Interval:
"""Composite-trapezoid weighted sum ``h*(f0/2 + f1 + ... + f_{n-1} + fn/2)``."""
n_nodes = len(node_values)
if n_nodes < 2:
raise ValueError("trapezoid rule needs at least two nodes")
hi = Interval.from_value(h)
half = Interval.from_rational(Fraction(1, 2))
terms = [node_values[0] * half]
terms.extend(node_values[1:-1])
terms.append(node_values[-1] * half)
return sum_intervals(terms) * hiOther public import paths
omnibias.core.verified.trapezoid_sumomnibias.core.verified.quadrature.trapezoid_sum