Documentation

CompPoly.Univariate.EuclideanAlgorithm

Extended Euclidean Algorithm for CPolynomial #

xgcd p q returns a triple (r, s, t) with r = s * p + t * q. Early stop when r.natDegree < threshold. The default threshold = 0 makes r the Greatest Common Divisor of p and q.

For positive thresholds, xgcd_stopSpec characterizes the output: BezoutStopSpec packages the Bézout identity together with the residue/cofactor degree bounds at the stopping point, via the loop invariant BezoutDegreeInvariant.

def CompPoly.CPolynomial.xgcdAux {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (threshold n : ) (r s t r' s' t' : CPolynomial R) :

Extended euclidean algorithm on p, q.

Stops when r.natDegree < threshold, r = 0, or fuel n exhausted.

If threshold > 0 then xgcdAux need not compute the greatest common divisor.

Potential optimization: the raw long division behind r' / r already computes r' - (r' / r) * r but it's not exposed through CPolynomial R.

Instances For
    def CompPoly.CPolynomial.xgcd {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) (threshold : := 0) :

    Extended euclidean algorithm for (p, q).

    Returns (r, s, t) with r = s * p + t * q.

    With the default threshold = 0 returns the gcd of p and q. With threshold > 0 stops when r.natDegree < threshold.

    If threshold > 0 then xgcdAux need not compute the greatest common divisor.

    Instances For

      Bezout-identity correctness #

      Bezout predicate on a triple (r, s, t): r = s * p + t * q.

      Instances For
        theorem CompPoly.CPolynomial.xgcdAux_bezout {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) (threshold n : ) {r r' s t s' t' : CPolynomial R} (h : p.Bezout q (r, s, t)) (h' : p.Bezout q (r', s', t')) :
        p.Bezout q (xgcdAux threshold n r s t r' s' t')

        xgcdAux preserves the Bezout identity.

        theorem CompPoly.CPolynomial.xgcd_bezout {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) (threshold : ) :
        p.Bezout q (p.xgcd q threshold)

        The output of xgcd is a Bezout triple for (p, q).

        xgcd correctness (threshold 0) #

        Correctness theorems for xgcd with the default threshold 0.

        theorem CompPoly.CPolynomial.xgcdAux_toPoly_eq_gcd {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] [DecidableEq R] (n : ) (r r' s t s' t' : CPolynomial R) (hn : r.toPoly.degree < n) :
        (xgcdAux 0 n r s t r' s' t').1.toPoly = EuclideanDomain.gcd r.toPoly r'.toPoly

        The gcd component of CompPoly's xgcdAux at threshold 0 coincides with Mathlib's EuclideanDomain.gcd.

        The gcd component of CompPoly's xgcd at threshold 0 coincides with Mathlib's EuclideanDomain.gcd.

        theorem CompPoly.CPolynomial.xgcdAux_toPoly_eq_xgcdAux {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] [DecidableEq R] (n : ) (r s t r' s' t' : CPolynomial R) (hn : r.toPoly.degree < n) :
        Prod.map (fun (x : CPolynomial R) => x.toPoly) (fun (x : CPolynomial R × CPolynomial R) => Prod.map (fun (x : CPolynomial R) => x.toPoly) (fun (x : CPolynomial R) => x.toPoly) x) (xgcdAux 0 n r s t r' s' t') = EuclideanDomain.xgcdAux r.toPoly s.toPoly t.toPoly r'.toPoly s'.toPoly t'.toPoly

        CompPoly's xgcdAux at threshold 0 coincides with Mathlib's EuclideanDomain.xgcdAux.

        theorem CompPoly.CPolynomial.xgcd_toPoly_eq_xgcd {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] [DecidableEq R] (p q : CPolynomial R) :
        Prod.map (fun (x : CPolynomial R) => x.toPoly) (fun (x : CPolynomial R) => x.toPoly) (p.xgcd q).2 = EuclideanDomain.xgcd p.toPoly q.toPoly

        The Bezout component of CompPoly's xgcd coincides with Mathlib's EuclideanDomain.xgcd.

        Normalized xgcd #

        def CompPoly.CPolynomial.normXgcd {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) (threshold : := 0) :

        CompPoly's xgcd p q scaled by the inverse leading coefficient of the gcd component, normalizing it to monic.

        Instances For
          theorem CompPoly.CPolynomial.normXgcd_bezout {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) (threshold : ) :
          p.Bezout q (p.normXgcd q threshold)

          The normalized extended-gcd output satisfies the Bezout identity.

          The gcd component of CompPoly's normXgcd is the normalization of Mathlib's EuclideanDomain.gcd

          Monic normalization of computable polynomials agrees with Mathlib normalization.

          The specialized monic gcd has the normalized Mathlib gcd as its toPoly image.

          The specialized monic gcd agrees with the gcd component of normalized extended gcd.

          The Bezout component of normXgcd under toPoly is Mathlib's EuclideanDomain.xgcd scaled by the inverse leading coefficient of the gcd

          theorem CompPoly.CPolynomial.normXgcd_fst_comm {R : Type u_1} [Field R] [BEq R] [LawfulBEq R] [DecidableEq R] (p q : CPolynomial R) :
          (p.normXgcd q).1 = (q.normXgcd p).1

          normXgcd is commutative for the gcd component

          Stop specification for positive thresholds #

          BezoutDegreeInvariant is the invariant of the partial-EEA (xgcd) loop: each non-stop step preserves it (BezoutDegreeInvariant.step), and at the stopping threshold it yields the output specification BezoutStopSpec (xgcd_stopSpec).

          This is the classical degree theory of the extended Euclidean algorithm — the row identity deg t + deg r' = deg g₀ and the complementary cofactor bound at the stop — i.e. the contract behind rational-function reconstruction and Reed-Solomon decoding.

          structure CompPoly.BezoutDegreeInvariant {F : Type u_1} [Ring F] (g₀ g₁ : Polynomial F) (threshold : ) (r s t r' s' t' : Polynomial F) :

          Loop invariant of one partial-EEA (xgcd) step.

          Instances For
            @[reducible, inline]
            abbrev CompPoly.BezoutDegreeInvariantOf {F : Type u_1} [Ring F] (g₀ g₁ : CPolynomial F) (threshold : ) (r s t r' s' t' : CPolynomial F) :

            BezoutDegreeInvariant on the toPoly images of a CPolynomial reference pair g₀, g₁ and the two state triples (r, s, t), (r', s', t').

            Instances For
              theorem CompPoly.BezoutDegreeInvariant.degree_complement {F : Type u_1} [CommRing F] [NoZeroDivisors F] [Nontrivial F] {g₀ g₁ : Polynomial F} {threshold : } {r s t r' s' t' : Polynomial F} (h : BezoutDegreeInvariant g₀ g₁ threshold r s t r' s' t') :
              t.degree + r'.degree = g₀.degree

              BezoutDegreeInvariant implies deg t + deg r' = deg g₀.

              theorem CompPoly.BezoutDegreeInvariant.step {F : Type u_1} [Field F] {g₀ g₁ : Polynomial F} {threshold : } {r s t r' s' t' : Polynomial F} (h : BezoutDegreeInvariant g₀ g₁ threshold r s t r' s' t') (hr : r 0) (hstop : ¬r.natDegree < threshold) :
              BezoutDegreeInvariant g₀ g₁ threshold (r' - r' / r * r) (s' - r' / r * s) (t' - r' / r * t) r s t

              A non-stop Euclidean step preserves the invariant.

              structure CompPoly.BezoutStopSpec {F : Type u_1} [Semiring F] (g₀ g₁ : Polynomial F) (threshold : ) (R S T : Polynomial F) :

              Stop spec of the partial EEA: R = S g₀ + T g₁, residue degree below threshold, and the cofactor T nonzero with complementary degree deg T + threshold ≤ deg g₀.

              Instances For
                @[reducible, inline]
                abbrev CompPoly.BezoutStopSpecOf {F : Type u_1} [Semiring F] (g₀ g₁ : CPolynomial F) (threshold : ) (result : CPolynomial F × CPolynomial F × CPolynomial F) :

                BezoutStopSpec on the toPoly images of a CPolynomial reference pair g₀, g₁ and result triple.

                Instances For
                  theorem CompPoly.xgcdAux_stopSpec_of_invariant {F : Type u_1} [Field F] [BEq F] [LawfulBEq F] (g₀ g₁ : CPolynomial F) (threshold : ) (hthr : 1 threshold) (hg₀ : g₀.toPoly 0) (n : ) (r s t r' s' t' : CPolynomial F) :
                  BezoutDegreeInvariantOf g₀ g₁ threshold r s t r' s' t'r.natDegree < nBezoutStopSpecOf g₀ g₁ threshold (CPolynomial.xgcdAux threshold n r s t r' s' t')

                  BezoutDegreeInvariant implies xgcdAux's output satisfies the stop spec.

                  theorem CompPoly.xgcd_stopSpec {F : Type u_1} [Field F] [BEq F] [LawfulBEq F] (g₀ g₁ : CPolynomial F) (threshold : ) (hdeg : g₁.degree < g₀.degree) (hthr : 1 threshold) (hthr' : threshold g₀.degree) :
                  BezoutStopSpecOf g₀ g₁ threshold (g₀.xgcd g₁ threshold)

                  xgcd's output satisfies the stop spec.