Mode of #cas
polynomialGCD
Gives the monic gcd of two polynomials in one variable, with a theorem about Mathlib’s gcd in ℚ[X].
#cas [polynomialGCD] (p, q)gcd p q = g in Mathlib’s ℚ[X].polynomialGCD finds the common factor of two polynomials without factoring either of them. The theorem is about Mathlib’s gcd in ℚ[X], the normalized gcd of its NormalizedGCDMonoid, which is monic.
The proof is a certificate, so the kernel never reruns the Euclidean algorithm. The gcd divides and with explicit cofactors, and the Bézout identity shows that every common divisor of and divides . So the kernel checks three polynomial identities and a monicity, however long the Euclidean algorithm ran.
Examples (12)#
Basic Examples (2)
The common factor of and :
#cas [polynomialGCD] (x^2 - 1, x^2 - 2*x + 1)
monic gcd
x - 1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(x^2 - 1, x^2 - 2*x + 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1
The certificate: 1 checked step
Statement in ℚ[X]
gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1
Coprime polynomials have gcd :
#cas [polynomialGCD] (x^3 - 1, x^2 + 1)
monic gcd
1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(x^3 - 1, x^2 + 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd (Polynomial.X ^ 3 - 1) (Polynomial.X ^ 2 + 1) = 1
The certificate: 1 checked step
Statement in ℚ[X]
gcd (Polynomial.X ^ 3 - 1) (Polynomial.X ^ 2 + 1) = 1
Scope (2)
The gcd is monic, also when the inputs have integer coefficients. Here the common factor is :
#cas [polynomialGCD] (2*x^2 + 3*x + 1, 4*x^2 - 1)
monic gcd
x + 1/2- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(2*x^2 + 3*x + 1, 4*x^2 - 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd (2 * Polynomial.X ^ 2 + 3 * Polynomial.X + 1) (4 * Polynomial.X ^ 2 - 1) = Polynomial.X + Polynomial.C (1 / 2)
The certificate: 1 checked step
Statement in ℚ[X]
gcd (2 * Polynomial.X ^ 2 + 3 * Polynomial.X + 1) (4 * Polynomial.X ^ 2 - 1) = Polynomial.X + Polynomial.C (1 / 2)
The gcd of and is the monic scalar multiple of :
#cas [polynomialGCD] (0, x^2 - 1)
monic gcd
x^2 - 1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(0, x^2 - 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd 0 (Polynomial.X ^ 2 - 1) = Polynomial.X ^ 2 - 1
The certificate: 1 checked step
Statement in ℚ[X]
gcd 0 (Polynomial.X ^ 2 - 1) = Polynomial.X ^ 2 - 1
Applications (2)
The first example as a theorem about Mathlib’s gcd. Only the three standard axioms are used:
open Polynomial in theorem gcd_of_quadratics : gcd (X ^ 2 - 1 : ℚ[X]) (X ^ 2 - 2 * X + 1) = X - 1 := by cas #print axioms gcd_of_quadratics
gcd_of_quadratics depends on the axioms propext, Classical.choice, Quot.sound.
The tactic cas takes the equation either way round:
open Polynomial in example : X - 1 = gcd (X ^ 2 - 1 : ℚ[X]) (X ^ 2 - 2 * X + 1) := by cas
Lean accepts the proof.
Properties and Relations (2)
polynomialLCM gives the least common multiple. The product of the gcd and the lcm is the product of the two monic inputs:
#cas [polynomialLCM] (x^2 - 1, x^2 - 2*x + 1)
monic lcm
x^3 - x^2 - x + 1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(x^2 - 1, x^2 - 2*x + 1)
Checked by Lean’s kernel: Statement in ℚ[X]
lcm (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X ^ 3 - Polynomial.X ^ 2 - Polynomial.X + 1
The certificate: 1 checked step
Statement in ℚ[X]
lcm (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X ^ 3 - Polynomial.X ^ 2 - Polynomial.X + 1
together finds a common denominator with gcds. Here and share the factor :
#cas [together] (1/(x^2-1) + 1/(x-1))
common denominator
(x + 2)/((x - 1)*(x + 1))- Input (rational function)
(1/(x^2-1) + 1/(x-1))- Conditions
- ,
Checked by Lean’s kernel: Identity with the common-denominator form
∀ (x : ℝ), x ^ 2 - 1 ≠ 0 → x - 1 ≠ 0 → 1 / (x ^ 2 - 1) + 1 / (x - 1) = (x + 2) / ((x - 1) * (x + 1))
The certificate: 1 checked step
Identity with the common-denominator form
∀ (x : ℝ), x ^ 2 - 1 ≠ 0 → x - 1 ≠ 0 → 1 / (x ^ 2 - 1) + 1 / (x - 1) = (x + 2) / ((x - 1) * (x + 1))
Possible Issues (3)
The mode works in one variable:
#cas [polynomialGCD] (x^2 - 1, y - 1)
Error
polynomialGCD: expected polynomials in one variable, found x, y
Mathlib’s gcd 0 0 is , which is not monic. Give at least one nonzero polynomial:
#cas [polynomialGCD] (0, 0)
Error
polynomialGCD: gcd 0 0 = 0 is not monic; give a nonzero polynomial
The tactic cas refuses a wrong gcd and gives the right one:
open Polynomial in example : gcd (X ^ 2 - 1 : ℚ[X]) (X - 1) = X + 1 := by cas
Error
cas: the gcd is X - 1, not X + 1
Neat Examples (1)
The gcd of and is :
#cas [polynomialGCD] (x^12 - 1, x^18 - 1)
monic gcd
x^6 - 1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(x^12 - 1, x^18 - 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd (Polynomial.X ^ 12 - 1) (Polynomial.X ^ 18 - 1) = Polynomial.X ^ 6 - 1
The certificate: 1 checked step
Statement in ℚ[X]
gcd (Polynomial.X ^ 12 - 1) (Polynomial.X ^ 18 - 1) = Polynomial.X ^ 6 - 1
See Also
polynomialLCM · polynomialQuotient · polynomialRemainder · factor · extendedGCD
