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.
TODO optimizations may be had by trimming only at the end of iterative operations
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
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 polynomial with the constant term removed; coeff (divX p) i = coeff p (i + 1).
Instances For
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.
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
Quotient of p by q (when R is a field).
Instances For
Remainder of p modulo q (when R is a field).
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.
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).
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.