Dyadic Rationals (n * 2^e) #
This file defines Dyadic rationals, which are the backbone of high-performance
verified numerics. Unlike arbitrary Rat, Dyadics do not require GCD
normalization, making them significantly faster for kernel evaluation.
Main definitions #
Dyadic- A dyadic rational number:mantissa * 2^exponentDyadic.toRat- Convert to standard rationalDyadic.add,Dyadic.mul,Dyadic.neg- Arithmetic operationsDyadic.shiftDown,Dyadic.shiftUp- Directed rounding for interval bounds
Design notes #
Dyadics use power-of-2 multiplications instead of GCD-based normalization.
This makes them orders of magnitude faster for kernel evaluation via
native_decide, since 2^n can be computed by simple bit operations.
For interval arithmetic, we use directed rounding:
This ensures mathematical soundness even when truncating precision.
Equations
- LeanCert.Core.instReprDyadic = { reprPrec := LeanCert.Core.instReprDyadic.repr }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Equations
Helper: Power of 2 #
Compute 2^n for natural n
Equations
Instances For
Shift an integer left by n bits (multiply by 2^n)
Equations
Instances For
Shift an integer right by n bits (divide by 2^n, floor toward -∞)
Equations
Instances For
Check if any of the low n bits are set
Equations
- LeanCert.Core.Dyadic.hasLowBits m n = decide (m % ↑(LeanCert.Core.Dyadic.pow2Nat n) ≠ 0)
Instances For
Conversion to Rat #
Equations
Equations
Construction #
Create a dyadic from an integer (exponent = 0)
Equations
- LeanCert.Core.Dyadic.ofInt i = { mantissa := i, exponent := 0 }
Instances For
Create a dyadic for 2^n
Equations
- LeanCert.Core.Dyadic.pow2 n = { mantissa := 1, exponent := n }
Instances For
Equations
Equations
Arithmetic Operations #
Equations
Addition of Dyadics. Aligns exponents by shifting the mantissa with larger exponent to match the smaller one.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
Equations
Directed Rounding for Interval Arithmetic #
Shift a dyadic to a new (larger) exponent with "Down" rounding (for lower bounds).
If newExp > d.exponent, we lose precision by right-shifting the mantissa.
Bits are discarded (floor division toward -∞).
Equations
Instances For
Shift a dyadic to a new (larger) exponent with "Up" rounding (for upper bounds).
If newExp > d.exponent, we lose precision by right-shifting the mantissa.
If any bits are lost, we add 1 to ensure upward rounding (ceiling toward +∞).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Normalization (Mantissa Control) #
Get the bit length of the absolute value of an integer
Instances For
Normalize a Dyadic to keep the mantissa within a reasonable bit-limit.
This prevents mantissas from growing without bound during repeated multiplications. Similar to how hardware floats work, but with directed rounding.
maxBits: Maximum allowed bits for mantissa (default 256)roundUp: If true, round toward +∞; if false, round toward -∞
Equations
- One or more equations did not get rendered due to their size.
Instances For
Normalize for lower bounds (round down)
Equations
- d.normalizeDown maxBits = d.normalize maxBits
Instances For
Normalize for upper bounds (round up)
Equations
- d.normalizeUp maxBits = d.normalize maxBits true
Instances For
Comparison #
Compare two Dyadics (decidable)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- LeanCert.Core.Dyadic.instOrd = { compare := LeanCert.Core.Dyadic.compare }
Equations
- LeanCert.Core.Dyadic.instLE = { le := fun (d₁ d₂ : LeanCert.Core.Dyadic) => d₁.le d₂ = true }
Equations
- LeanCert.Core.Dyadic.instLT = { lt := fun (d₁ d₂ : LeanCert.Core.Dyadic) => d₁.lt d₂ = true }
Correctness Theorems #
Helper lemmas for shift proofs #
Arithmetic Homomorphisms #
Rounding Properties #
Comparison Helper Lemmas #
Comparison Theorems #
compare reflects toRat ordering: lt case
compare reflects toRat ordering: gt case
compare reflects toRat ordering: eq case
Min/Max Lemmas #
Normalize Lemmas #
normalizeDown produces value ≤ original
normalizeUp produces value ≥ original
Scale2 Lemmas #
Square Root Operations #
Integer square root of a natural number.
Satisfies: (intSqrtNat n)^2 ≤ n < (intSqrtNat n + 1)^2
Equations
Instances For
Integer square root of a non-negative integer. Returns 0 for negative inputs.
Equations
Instances For
Compute sqrt(d) with a target exponent prec.
Returns a Dyadic with exponent prec such that result ≤ sqrt(d).
For d = m * 2^e, we compute:
- shift = e - 2*prec (to align for sqrt)
- m' = m * 2^shift (or m / 2^(-shift) if shift < 0)
- result = floor(sqrt(m')) * 2^prec
This ensures: result.toRat ≤ sqrt(d.toRat)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Compute sqrt(d) rounded up with target exponent prec.
Returns a Dyadic with exponent prec such that result ≥ sqrt(d).
Uses the same computation as sqrtDown, but if the result is not a perfect square, adds 1 to round up.
Equations
- One or more equations did not get rendered due to their size.