Documentation

LeanCert.Core.Dyadic

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 #

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.

A Dyadic rational number: mantissa * 2^exponent

This representation allows fast arithmetic without GCD normalization.

  • mantissa :

    The significand/mantissa

  • exponent :

    The exponent (power of 2)

Instances For
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      def LeanCert.Core.instDecidableEqDyadic.decEq (x✝ x✝¹ : Dyadic) :
      Decidable (x✝ = x✝¹)
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        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
              Instances For

                Conversion to Rat #

                Convert Dyadic to standard Rat.

                For non-negative exponents: mantissa * 2^exponent For negative exponents: mantissa / 2^(-exponent)

                Equations
                Instances For

                  Cast a Dyadic to ℝ by going through ℚ

                  Equations
                  Instances For
                    theorem LeanCert.Core.Dyadic.toRat_injective_of_normalized {d₁ d₂ : Dyadic} (h : d₁.toRat = d₂.toRat) :
                    d₁.toRat = d₂.toRat

                    Two Dyadics are equal as rationals iff their toRat values are equal

                    Construction #

                    Create a dyadic from an integer (exponent = 0)

                    Equations
                    Instances For

                      Create a dyadic for 2^n

                      Equations
                      Instances For

                        Zero as a Dyadic

                        Equations
                        Instances For

                          One as a Dyadic

                          Equations
                          Instances For

                            Arithmetic Operations #

                            Negation of a Dyadic

                            Equations
                            Instances For

                              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

                                Subtraction of Dyadics

                                Equations
                                Instances For

                                  Multiplication of Dyadics. Simply multiplies mantissas and adds exponents.

                                  Equations
                                  Instances For

                                    Absolute value of a Dyadic

                                    Equations
                                    Instances For

                                      Scale by a power of 2 (efficient: just adjusts exponent)

                                      Equations
                                      Instances For

                                        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

                                            Equations
                                            Instances For
                                              def LeanCert.Core.Dyadic.normalize (d : Dyadic) (maxBits : := 256) (roundUp : Bool := false) :

                                              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
                                                def LeanCert.Core.Dyadic.normalizeDown (d : Dyadic) (maxBits : := 256) :

                                                Normalize for lower bounds (round down)

                                                Equations
                                                Instances For
                                                  def LeanCert.Core.Dyadic.normalizeUp (d : Dyadic) (maxBits : := 256) :

                                                  Normalize for upper bounds (round up)

                                                  Equations
                                                  Instances For

                                                    Comparison #

                                                    Compare two Dyadics (decidable)

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      Less-than-or-equal for Dyadics

                                                      Equations
                                                      Instances For

                                                        Less-than for Dyadics

                                                        Equations
                                                        Instances For
                                                          Equations
                                                          Equations

                                                          Minimum of two Dyadics

                                                          Equations
                                                          Instances For

                                                            Maximum of two Dyadics

                                                            Equations
                                                            Instances For

                                                              Minimum of four Dyadics

                                                              Equations
                                                              Instances For

                                                                Maximum of four Dyadics

                                                                Equations
                                                                Instances For

                                                                  Correctness Theorems #

                                                                  Helper lemmas for shift proofs #

                                                                  Arithmetic Homomorphisms #

                                                                  Unification lemma: toRat d = d.mantissa * 2^d.exponent

                                                                  theorem LeanCert.Core.Dyadic.toRat_add (d₁ d₂ : Dyadic) :
                                                                  (d₁.add d₂).toRat = d₁.toRat + d₂.toRat
                                                                  theorem LeanCert.Core.Dyadic.toRat_mul (d₁ d₂ : Dyadic) :
                                                                  (d₁.mul d₂).toRat = d₁.toRat * d₂.toRat

                                                                  Rounding Properties #

                                                                  shiftDown produces a value ≤ the original

                                                                  theorem LeanCert.Core.Dyadic.toRat_shiftUp_ge (d : Dyadic) (newExp : ) :
                                                                  d.toRat (d.shiftUp newExp).toRat

                                                                  shiftUp produces a value ≥ the original

                                                                  Comparison Helper Lemmas #

                                                                  Comparison Theorems #

                                                                  theorem LeanCert.Core.Dyadic.le_iff_toRat_le (d₁ d₂ : Dyadic) :
                                                                  d₁.le d₂ = true d₁.toRat d₂.toRat

                                                                  le is correct with respect to toRat ordering

                                                                  theorem LeanCert.Core.Dyadic.compare_lt_iff (d₁ d₂ : Dyadic) :
                                                                  d₁.compare d₂ = Ordering.lt d₁.toRat < d₂.toRat

                                                                  compare reflects toRat ordering: lt case

                                                                  theorem LeanCert.Core.Dyadic.compare_gt_iff (d₁ d₂ : Dyadic) :
                                                                  d₁.compare d₂ = Ordering.gt d₁.toRat > d₂.toRat

                                                                  compare reflects toRat ordering: gt case

                                                                  theorem LeanCert.Core.Dyadic.compare_eq_iff (d₁ d₂ : Dyadic) :
                                                                  d₁.compare d₂ = Ordering.eq d₁.toRat = d₂.toRat

                                                                  compare reflects toRat ordering: eq case

                                                                  Min/Max Lemmas #

                                                                  theorem LeanCert.Core.Dyadic.min_toRat_le_left (d₁ d₂ : Dyadic) :
                                                                  (d₁.min d₂).toRat d₁.toRat

                                                                  min produces value ≤ first argument

                                                                  theorem LeanCert.Core.Dyadic.min_toRat_le_right (d₁ d₂ : Dyadic) :
                                                                  (d₁.min d₂).toRat d₂.toRat

                                                                  min produces value ≤ second argument

                                                                  theorem LeanCert.Core.Dyadic.le_max_toRat_left (d₁ d₂ : Dyadic) :
                                                                  d₁.toRat (d₁.max d₂).toRat

                                                                  first argument ≤ max

                                                                  theorem LeanCert.Core.Dyadic.le_max_toRat_right (d₁ d₂ : Dyadic) :
                                                                  d₂.toRat (d₁.max d₂).toRat

                                                                  second argument ≤ max

                                                                  theorem LeanCert.Core.Dyadic.min_toRat (d₁ d₂ : Dyadic) :
                                                                  (d₁.min d₂).toRat = Min.min d₁.toRat d₂.toRat

                                                                  Dyadic.min commutes with toRat

                                                                  theorem LeanCert.Core.Dyadic.max_toRat (d₁ d₂ : Dyadic) :
                                                                  (d₁.max d₂).toRat = Max.max d₁.toRat d₂.toRat

                                                                  Dyadic.max commutes with toRat

                                                                  theorem LeanCert.Core.Dyadic.min4_le_max4 (a b c d : Dyadic) :
                                                                  (a.min4 b c d).toRat (a.max4 b c d).toRat

                                                                  min4 ≤ max4

                                                                  Normalize Lemmas #

                                                                  normalizeDown produces value ≤ original

                                                                  normalizeUp produces value ≥ original

                                                                  Scale2 Lemmas #

                                                                  theorem LeanCert.Core.Dyadic.toRat_scale2 (d : Dyadic) (n : ) :
                                                                  (d.scale2 n).toRat = d.toRat * 2 ^ n

                                                                  scale2 multiplies by 2^n

                                                                  theorem LeanCert.Core.Dyadic.toRat_scale2_le_scale2 (d₁ d₂ : Dyadic) (n : ) (h : d₁.toRat d₂.toRat) :
                                                                  (d₁.scale2 n).toRat (d₂.scale2 n).toRat

                                                                  scale2 preserves order

                                                                  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
                                                                      theorem LeanCert.Core.Dyadic.intSqrt_sq_le {n : } (hn : 0 n) :
                                                                      intSqrt n ^ 2 n

                                                                      intSqrt n ^ 2 ≤ n for n ≥ 0

                                                                      theorem LeanCert.Core.Dyadic.int_lt_succ_sqrt_sq {n : } (hn : 0 n) :
                                                                      n < (intSqrt n + 1) ^ 2

                                                                      n < (intSqrt n + 1) ^ 2 for n ≥ 0

                                                                      theorem LeanCert.Core.Dyadic.intSqrt_nonneg {n : } (hn : 0 n) :

                                                                      intSqrt is nonnegative for nonnegative inputs

                                                                      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.
                                                                        Instances For

                                                                          Sqrt Correctness Theorems #

                                                                          theorem LeanCert.Core.Dyadic.sqrtDown_nonneg (d : Dyadic) (prec : ) (hd : 0 d.mantissa) :
                                                                          0 (d.sqrtDown prec).mantissa

                                                                          sqrtDown is non-negative for non-negative inputs

                                                                          theorem LeanCert.Core.Dyadic.sqrtUp_nonneg (d : Dyadic) (prec : ) (hd : 0 d.mantissa) :
                                                                          0 (d.sqrtUp prec).mantissa

                                                                          sqrtUp is non-negative for non-negative inputs

                                                                          theorem LeanCert.Core.Dyadic.sqrtDown_le_sqrtUp (d : Dyadic) (prec : ) (hd : 0 d.mantissa) :
                                                                          (d.sqrtDown prec).toRat (d.sqrtUp prec).toRat

                                                                          sqrtDown.toRat ≤ sqrtUp.toRat for non-negative inputs