Computable Univariate Polynomials #
This file defines CPolynomial R, the type of canonical univariate polynomials.
Canonicality is tracked semantically by requiring the last stored coefficient, when present,
to be nonzero.
This provides a unique representation for each polynomial, enabling stronger extensionality
properties compared to the raw CPolynomial.Raw type.
Computable univariate polynomials are represented canonically with no trailing zeros.
A polynomial p : CPolynomial.Raw R is canonical if its last stored coefficient is nonzero
whenever the underlying array is nonempty. This gives an instance-stable carrier while keeping
the raw normalization algorithms available separately.
Raw.mul defers trimming to the end of its convolution fold (it accumulates with the
untrimmed Raw.mulRaw and trims once). Raw.powBySq likewise routes through an
untrimmed powBySqUntrimmed and trims once at the top of the repeated-squaring
recursion. CPolynomial.divX skips trimming entirely: the input is canonical and
extract 1 size only strips from the front, so the leading coefficient (when present)
is preserved and the result is already canonical. The division wrappers (divByMonic,
modByMonic, modByMonicRemainderOnly, modByMonicByReversal, div, mod) likewise
skip the redundant post-trim: each underlying Raw algorithm bottoms out in operations
(Raw.add, Raw.sub, subScaledShift) that already trim. CLagrange.basis folds the
s.erase i factors with the untrimmed Raw.mulRaw via Quotient.liftOn on the
underlying multiset and trims once at the end, and CLagrange.interpolate does the
analogous deferred-trim sum via Raw.addRaw/Raw.mulRaw.
Remaining opportunities to trim only at the end of an iterative computation:
the Array.foldl accumulator in Bivariate/ToPoly.lean (toPoly_foldl_zipIdx_eq_sum),
and the two ExtTreeMap.foldl monomial accumulators in Multivariate/Operations.lean.
The NTT butterflyStage in Univariate/NTT/Transform.lean is not on this list: it
operates on a scalar coefficient Array R and never calls trim.
Instances For
Extensionality for canonical polynomials.
Canonical polynomials coerce to raw polynomials.
The zero polynomial is canonical without any normalization assumptions.
The zero polynomial provides the inhabited instance.
CPolynomial R has BEq when R does, comparing the underlying coefficient arrays.
CPolynomial R has LawfulBEq when R does: p == q iff p = q.
CPolynomial R has DecidableEq when R does, via the underlying Array R representation.
Canonical computable polynomials are trim-stable.
Addition of canonical polynomials (result is canonical).
Scalar multiplication by a natural number (result is canonical).
Instances For
Multiplication of canonical polynomials.
The product of two canonical polynomials is canonical because multiplication preserves the "no trailing zeros" property.
The constant polynomial 1 is canonical, and is the Unit for multiplication.
This is #[1], which has no trailing zeros.
This proof does not work without the assumption that R is non-trivial.
The coefficient of X^i in the polynomial. Returns 0 if i is out of bounds.
Instances For
The constant polynomial C r.
Instances For
The variable X.
Instances For
Construct a canonical monomial c * X^n as a CPolynomial R.
The result is canonical (no trailing zeros) when c ≠ 0.
For example, monomial 2 3 represents 3 * X^2.
Note: If c = 0, this returns 0 (the zero polynomial).
Instances For
Return the degree of a CPolynomial.
Instances For
Natural number degree of a canonical polynomial.
Returns the degree as a natural number. For the zero polynomial, returns 0.
This matches Mathlib's Polynomial.natDegree API.
Instances For
Return the leading coefficient of a CPolynomial as the last coefficient
of the trimmed array, or 0 if the trimmed array is empty.
Instances For
Evaluate a polynomial at a point.
Instances For
Evaluate at x : S via a ring hom
f : R →+* S; eval₂ f x p = f(a₀) + f(a₁)*x + f(a₂)*x² + ....
Instances For
Evaluate at x : S via a ring hom using Horner's method.
Instances For
Evaluate a polynomial at a point using Horner's method.
Instances For
Horner evaluation agrees with the sum-of-powers evaluation.
Horner evaluation agrees with the sum-of-powers evaluation.
The support of a polynomial: indices with nonzero coefficients.
Instances For
Number of coefficients (length of the underlying array).
Instances For
Upper bound on degree: size - 1 if non-empty, ⊥ if empty.
Instances For
Convert degreeBound to a natural number by sending ⊥ to 0.
Instances For
Check if a CPolynomial is monic, i.e. its leading coefficient is 1.
Instances For
The support of the sum of two polynomials is a subset of the union of their supports.
Evaluation via a ring hom equals the sum over support of mapped coefficients times powers.
Induction principle for polynomials (mirrors mathlib's Polynomial.induction_on).
Lemma for degree_eq_support_max: degree in terms of lastNonzero.
Degree equals the maximum of the support when the polynomial is non-zero.
Here p.degree = some n where n is the maximum index in p.support.
The natDegree of a sum is at most the max of the natDegrees.
The leading coefficient of a nonzero polynomial is nonzero.
Quotient of p by a monic polynomial q. Matches Mathlib's Polynomial.divByMonic.
Instances For
Remainder of p modulo a monic polynomial q. Matches Mathlib's Polynomial.modByMonic.
Instances For
Remainder of p modulo a monic polynomial q, using a remainder-only implementation.
Instances For
Remainder of p modulo a monic polynomial q, using reversal and low products.
Instances For
The remainder-only monic remainder agrees with the canonical monic remainder.
Quotient of p by q (when R is a field).
Instances For
Any CPolynomial divided by the zero polynomial gives the zero
polynomial.
Remainder of p modulo q (when R is a field).
Instances For
Any CPolynomial modulo the zero polynomial gives the zero
polynomial.
Normalize a nonzero polynomial to monic form. The zero polynomial stays zero.
Instances For
Euclidean gcd with explicit fuel, normalized to a monic result.
Instances For
Monic Euclidean gcd for canonical univariate polynomials.
Instances For
CPolynomial R forms a commutative monoid when R is a semiring.
CPolynomial R forms a semiring when R is a semiring.
The semiring structure extends the AddCommGroup structure with multiplication.
All operations preserve the canonical property (no trailing zeros).
The underlying Raw value of p ^ n equals p.val ^ n
(using the Raw Pow instance). This bridges the optimized
powBySq used in the Semiring instance with the spec
pow.
Distribute CPolynomial.coeff over a Finset.sum.
CPolynomial R forms a commutative semiring when R is a commutative semiring.
Commutativity follows from the commutativity of multiplication in the base ring.
Erase the coefficient at index n (same as p except coeff n = 0, then trimmed).
Uses an in-place Array.setIfInBounds and avoids allocating a length-n monomial
array plus the padding/zip passes of sub.
Instances For
Leading coefficient equals the coefficient at natDegree.
A polynomial equals its leading monomial plus the rest (erase at natDegree).
CPolynomial R forms a ring when R is a ring.
The ring structure extends the semiring structure with negation and subtraction.
Most of the structure is already provided by the Semiring instance.
CPolynomial R forms a commutative ring when R is a commutative ring.
This combines the CommSemiring and Ring structures.
Scalar multiplication for canonical polynomials: multiply each coefficient by r,
then trim to restore canonicity.
Helper lemma: Two CPolynomials are equal if the underlying Raw CPolynomials are trim equivalent.
Scalar multiplication by 0 gives 0.
Scalar multiplication on p by 1 gives p.
CPolynomial forms a module when R is a semiring.
Equality between div and divByMonic for CPolynomial R
Equality between mod and modByMonic for CPolynomial R