Piecewise polynomial certificates #
This module implements a Lean checker for one-dimensional piecewise-polynomial certificates.
Why this exists:
- In TorchLean’s “external producer, Lean checker” workflow, an external tool (Julia, Python, ...) can fit or search for a piecewise-polynomial surrogate and export a compact JSON artifact.
- Lean then checks basic algebraic consistency conditions on that artifact.
Our checker is conservative: it checks exact equalities over Rat with no floating-point
tolerance.
Optionally, you can also run an executable float32 cross-check via checkJsonIEEE32ExecExact:
if every rational in the certificate is exactly representable as a finite IEEE-754 binary32
(IEEE32Exec), we re-run the endpoint equalities under IEEE32Exec arithmetic. This is useful
when the producer is actually operating in float32 and you want to confirm the certificate’s
equalities hold under the same semantics.
What is checked in format = "piecewise_poly_v0":
xsis strictly increasing,yshas the same length asxs,- there is one
pieceper interval[xs[i], xs[i+1]], - each piece’s
(lo, hi)matches the corresponding interval endpoints, - the local-coordinate polynomial interpolates the endpoints exactly:
p_i(xs[i]) = ys[i]andp_i(xs[i+1]) = ys[i+1].
This certificate format focuses on endpoint and piece consistency. It does not claim global range bounds for higher-degree polynomials; those require additional machinery such as Bernstein bounds or interval arithmetic with derivative-based splitting.
References (background):
- de Boor, A Practical Guide to Splines (1978).
- Certificate checking pattern: export an artifact, then check it in a small Lean checker core.
Utilities: exact rationals in JSON #
Parse a rational in the format emitted by TorchLean’s Arb helpers:
- integer:
"5","-3" - fraction:
"5/2","-7/10"
We avoid JSON numbers here because they are stored as Scientific and are not guaranteed to
round-trip exactly for large integers.
Instances For
Certificate schema #
Parsed certificate payload for piecewise_poly_v0.
degree is redundant (it should equal coeffs.size - 1), but it is useful for fast validation.
- degree : ℕ
Polynomial degree
d(so each piece hasd+1coefficients). - n : ℕ
Number of knot points.
- xs : TensorArray.Tensor ℚ [self.n]
Knot x-coordinates as a length-
nvector. - ys : TensorArray.Tensor ℚ [self.n]
Knot y-values as a length-
nvector. - pieces : Array PolynomialPiece
One piece per interval
[xs[i], xs[i+1]].
Instances For
Polynomial evaluation #
Evaluate a polynomial in local coordinate t using Horner’s rule.
Given coefficients [a₀, a₁, …, a_d] representing:
a₀ + a₁ t + a₂ t^2 + … + a_d t^d.
This helper is generic so we can reuse it for Rat (exact checking) and for IEEE32Exec
(executable float32 semantics).
Instances For
Parsing #
Parse one polynomial segment from the certificate JSON object.
Instances For
Parse the piecewise_poly_v0 JSON payload into a structured certificate.
Instances For
Checking #
Check a piecewise-polynomial certificate exactly over Rat.
This is the smallest checker core for this format: it uses exact rational arithmetic (no tolerances), so a passing check means the certificate’s equalities are literally true as stated.
Instances For
Check a piecewise_poly_v0 certificate payload.
Raises IO.userError on the first mismatch; prints a short success message otherwise.
Instances For
Attempt to convert a rational to an exact finite IEEE32Exec value.
This is used for optional “float32 semantics” checking: we only accept values that are exactly representable on the binary32 grid, so equality checks are meaningful at the executable IEEE level.
Implementation note:
- We reuse TorchLean’s existing
Rat → IEEE32Execoutward-rounding helpers (roundRatQDown/roundRatQUp), and we accept only when the lower/upper rounding coincide.
Instances For
Check the same endpoint equalities as checkCertificateRat, but under IEEE32Exec arithmetic.
This is an additional (optional) check that answers: “if we interpret the certificate data as binary32 values and run the polynomial evaluation with executable IEEE-754 ops, do we still hit the claimed endpoints?”
The check is deliberately strict:
- every rational in the cert must be exactly representable as a finite
IEEE32Exec, - comparisons use IEEE-style
BEq(so+0 == -0, and NaNs never compare equal).
Instances For
Check a piecewise-polynomial certificate stored as JSON, including IEEE32Exec semantics.
Instances For
Check a piecewise-polynomial certificate stored as a JSON file.