Raw Univariate Polynomial Operations #
Operations and evaluation lemmas for raw computable univariate polynomials.
Evaluates a CPolynomial.Raw at x : S using a ring homomorphism f : R →+* S.
Computes f(a₀) + f(a₁) * x + f(a₂) * x² + ... where aᵢ are the coefficients.
Equations
- CompPoly.CPolynomial.Raw.eval₂ f x p = Array.foldl (fun (acc : S) (x_1 : R × ℕ) => match x_1 with | (a, i) => acc + f a * x ^ i) 0 (Array.zipIdx p)
Instances For
Evaluates a CPolynomial.Raw at x : S using Horner's method.
Computes f(aₙ) + x * (f(aₙ₋₁) + x * (... + x * f(a₀))) via a right fold.
Equations
- CompPoly.CPolynomial.Raw.eval₂Horner f x p = Array.foldr (fun (a : R) (acc : S) => acc * x + f a) 0 p
Instances For
Evaluates a CPolynomial.Raw at a given value
Equations
Instances For
Raw addition: pointwise sum of coefficient arrays (padded to equal length).
The result may have trailing zeros and should be trimmed for canonical form.
Equations
- p.addRaw q = match Array.matchSize p q 0 with | (p', q') => CompPoly.CPolynomial.Raw.mk (Array.zipWith (fun (x1 x2 : R) => x1 + x2) p' q')
Instances For
Scalar multiplication: multiplies each coefficient by r.
Equations
- CompPoly.CPolynomial.Raw.smul r p = CompPoly.CPolynomial.Raw.mk (Array.map (fun (a : R) => r * a) p)
Instances For
Right scalar multiplication: multiplies each coefficient by r on the right.
Corresponds to p.toPoly * Polynomial.C r, in contrast to smul which corresponds
to Polynomial.C r * p.toPoly. The distinction matters in non-commutative semirings.
Equations
- CompPoly.CPolynomial.Raw.smulRight r p = CompPoly.CPolynomial.Raw.mk (Array.map (fun (a : R) => a * r) p)
Instances For
Raw scalar multiplication by a natural number (may have trailing zeros).
Equations
- CompPoly.CPolynomial.Raw.nsmulRaw n p = CompPoly.CPolynomial.Raw.mk (Array.map (fun (a : R) => ↑n * a) p)
Instances For
Scalar multiplication of CPolynomial.Raw by a natural number, with result trimmed.
Equations
Instances For
Multiplication by X^i: shifts coefficients right by i positions (prepends i zeros).
Equations
Instances For
Multiplication of a CPolynomial.Raw by X, reduces to mulPowX 1.
Equations
Instances For
Multiplication accumulator using the naive O(n²) algorithm: Σᵢ (aᵢ * q) * X^i.
The partial sums are combined with the untrimmed addRaw, so the result may carry
trailing zeros and should be trimmed for canonical form. Deferring the trim to the
end (see mul) avoids an O(size) scan after every partial sum.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multiplication using the naive O(n²) algorithm: Σᵢ (aᵢ * q) * X^i.
Trims only once, at the end of the convolution fold, rather than after every
partial sum (mulRaw does the untrimmed accumulation).
Instances For
Exponentiation of a CPolynomial.Raw by a natural number n via repeated multiplication.
This is the specification; powBySq is the efficient O(log n) implementation.
Instances For
Repeated-squaring core that accumulates with the untrimmed mulRaw, deferring
the canonicalization trim to the outer powBySq wrapper. The output may carry
trailing zeros and is not in canonical form.
For $ n > 0 $, computes $ p ^ n $ by recursing on $ n / 2 $:
- If $ n $ is even: $ (p ^ {n/2})^2 $
- If $ n $ is odd: $ p \cdot (p ^ {n/2})^2 $
Equations
- One or more equations did not get rendered due to their size.
- p.powBySqUntrimmed 0 = CompPoly.CPolynomial.Raw.C 1
Instances For
Exponentiation via repeated squaring: $ O(\log n) $ multiplications
instead of $ O(n) $. Delegates to powBySqUntrimmed and trims only once
at the end, rather than after every squaring step.
Equations
- p.powBySq n = (p.powBySqUntrimmed n).trim
Instances For
Equations
Equations
Equations
Equations
Equations
Equations
- CompPoly.CPolynomial.Raw.instNatCast = { natCast := fun (n : ℕ) => CompPoly.CPolynomial.Raw.C ↑n }
Keep only the stored coefficients with index < k.
Equations
- CompPoly.CPolynomial.Raw.truncate k p = Array.extract p 0 k
Instances For
Return exactly the first k coefficients, padding missing coefficients with zero.
Equations
- CompPoly.CPolynomial.Raw.takeLow k p = Array.ofFn fun (i : Fin k) => p.coeff ↑i
Instances For
Reverse the first n coefficients, padding missing coefficients with zero.
Equations
- CompPoly.CPolynomial.Raw.reverse n p = Array.ofFn fun (i : Fin n) => p.coeff (n - 1 - ↑i)
Instances For
Low product computed by the coefficient convolution formula.
Equations
- CompPoly.CPolynomial.Raw.mulLowConvolution k p q = Array.ofFn fun (i : Fin k) => ∑ j ∈ Finset.range (↑i + 1), p.coeff j * q.coeff (↑i - j)
Instances For
Backend for computing the low k coefficients of a raw product.
Return the low
kcoefficients ofp * q.The backend does not return coefficients at degree
>= k.Low coefficients agree with ordinary raw multiplication.
Instances For
Upper bound on degree: size - 1 if non-empty, ⊥ if empty.
Equations
- p.degreeBound = match Array.size p with | 0 => ⊥ | n.succ => ↑n
Instances For
Pseudo-division by X: removes the constant term and shifts remaining coefficients left.
Equations
- p.divX = Array.extract p 1
Instances For
Equations
Equations
- CompPoly.CPolynomial.Raw.instIntCast = { intCast := fun (n : ℤ) => CompPoly.CPolynomial.Raw.C ↑n }