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)
gives the monic greatest common divisor g of the polynomials p and q and proves 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 g divides p and q with explicit cofactors, and the Bézout identity sp+tq=g shows that every common divisor of p and q divides g. So the kernel checks three polynomial identities and a monicity, however long the Euclidean algorithm ran.

Details and Options

  • The theorem is gcd p q = g in ℚ[X], with the variable read as Polynomial.X. The two inputs have the same single variable, which need not be x.
  • The gcd is monic, also for inputs with integer coefficients: where the common factor is 2x+1, the gcd is x+12.
  • The proof checks g ∣ p and g ∣ q with explicit cofactors, the Bézout identity s * p + t * q = g with the coefficients s and t of the extended Euclidean algorithm, and the monicity of g.
  • The inputs are polynomials with rational coefficients in one variable, not both zero.
  • The tactic cas proves gcd p q = g goals in ℚ[X], written either way round, and it refuses a wrong gcd with the right one in its message.
  • polynomialLCM gives the lcm, and extendedGCD the gcd of natural numbers with its Bézout coefficients.
  • There are no options.

Examples (12)#

Basic Examples (2)

The common factor of x2−1 and (x−1)2:

In[1]:=
#cas [polynomialGCD] (x^2 - 1, x^2 - 2*x + 1)
Out[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
  1. Statement in ℚ[X]

    gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1

    Checked by Lean’s kernel


Coprime polynomials have gcd 1:

In[2]:=
#cas [polynomialGCD] (x^3 - 1, x^2 + 1)
Out[2]=

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
  1. Statement in ℚ[X]

    gcd (Polynomial.X ^ 3 - 1) (Polynomial.X ^ 2 + 1) = 1

    Checked by Lean’s kernel

Scope (2)

The gcd is monic, also when the inputs have integer coefficients. Here the common factor is 2x+1:

In[3]:=
#cas [polynomialGCD] (2*x^2 + 3*x + 1, 4*x^2 - 1)
Out[3]=

monic gcd

x+12
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
  1. Statement in ℚ[X]

    gcd (2 * Polynomial.X ^ 2 + 3 * Polynomial.X + 1) (4 * Polynomial.X ^ 2 - 1) =
      Polynomial.X + Polynomial.C (1 / 2)

    Checked by Lean’s kernel


The gcd of 0 and q is the monic scalar multiple of q:

In[4]:=
#cas [polynomialGCD] (0, x^2 - 1)
Out[4]=

monic gcd

x2−1
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
  1. Statement in ℚ[X]

    gcd 0 (Polynomial.X ^ 2 - 1) = Polynomial.X ^ 2 - 1

    Checked by Lean’s kernel

Applications (2)

The first example as a theorem about Mathlib’s gcd. Only the three standard axioms are used:

In[5]:=
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
Out[5]=

gcd_of_quadratics depends on the axioms propext, Classical.choice, Quot.sound.


The tactic cas takes the equation either way round:

In[6]:=
open Polynomial in
example : X - 1 = gcd (X ^ 2 - 1 : ℚ[X]) (X ^ 2 - 2 * X + 1) := by
  cas
Out[6]=

Lean accepts the proof.

Properties and Relations (2)

polynomialLCM gives the least common multiple. The product of the gcd x−1 and the lcm is the product (x2−1)(x−1)2 of the two monic inputs:

In[7]:=
#cas [polynomialLCM] (x^2 - 1, x^2 - 2*x + 1)
Out[7]=

monic lcm

x3−x2−x+1
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
  1. Statement in ℚ[X]

    lcm (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) =
      Polynomial.X ^ 3 - Polynomial.X ^ 2 - Polynomial.X + 1

    Checked by Lean’s kernel


together finds a common denominator with gcds. Here x2−1 and x−1 share the factor x−1:

In[8]:=
#cas [together] (1/(x^2-1) + 1/(x-1))
Out[8]=

common denominator

x+2(x−1)(x+1)
(x + 2)/((x - 1)*(x + 1))
Input (rational function)
(1/(x^2-1) + 1/(x-1))
Conditions
x2−1≠0, x−1≠0

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
  1. 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))

    Checked by Lean’s kernel

Possible Issues (3)

The mode works in one variable:

In[9]:=
#cas [polynomialGCD] (x^2 - 1, y - 1)
Out[9]=

Error

polynomialGCD: expected polynomials in one variable, found x, y

Mathlib’s gcd 0 0 is 0, which is not monic. Give at least one nonzero polynomial:

In[10]:=
#cas [polynomialGCD] (0, 0)
Out[10]=

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:

In[11]:=
open Polynomial in
example : gcd (X ^ 2 - 1 : ℚ[X]) (X - 1) = X + 1 := by
  cas
Out[11]=

Error

cas: the gcd is X - 1, not X + 1

Neat Examples (1)

The gcd of xm−1 and xn−1 is xgcd(m,n)−1:

In[12]:=
#cas [polynomialGCD] (x^12 - 1, x^18 - 1)
Out[12]=

monic gcd

x6−1
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
  1. Statement in ℚ[X]

    gcd (Polynomial.X ^ 12 - 1) (Polynomial.X ^ 18 - 1) = Polynomial.X ^ 6 - 1

    Checked by Lean’s kernel

See Also

polynomialLCM · polynomialQuotient · polynomialRemainder · factor · extendedGCD