Mode of #cas

polynomialRemainder

Gives the remainder of a division of polynomials in one variable, with a theorem about Mathlib’s % in ℚ[X].

#cas [polynomialRemainder] (p, q)
gives the remainder r of the division of the polynomial p by the nonzero polynomial q and proves p / q = s ∧ p % q = r in Mathlib’s ℚ[X].

polynomialRemainder gives you the remainder of polynomial long division. It is also a divisibility test, since q divides p exactly when the remainder is 0. The report prints the remainder, and the theorem under it is the one of polynomialQuotientRemainder, which states the quotient and the remainder together, about Mathlib’s / and % on ℚ[X].

Details and Options

  • The theorem is p / q = s ∧ p % q = r in ℚ[X], with the variable read as Polynomial.X, proved as for polynomialQuotientRemainder.
  • The remainder has a smaller degree than q, and it is 0 exactly when q divides p.
  • For a divisor x−a, the remainder is the value of p at a.
  • The divisor q is nonzero. Mathlib’s division is total, with p % 0 = p, and a zero divisor is refused.
  • The tactic cas proves p % q = r goals in ℚ[X].
  • For polynomials in several variables, member reduces modulo a Gröbner basis.
  • There are no options.

Examples (8)#

Basic Examples (2)

The remainder 0 shows that x2+x+1 divides x5+x+1:

In[1]:=
#cas [polynomialRemainder] (x^5 + x + 1, x^2 + x + 1)
Out[1]=

remainder

0
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^5 + x + 1, x^2 + x + 1)

Checked by Lean’s kernel: Statement in ℚ[X]

(Polynomial.X ^ 5 + Polynomial.X + 1) / (Polynomial.X ^ 2 + Polynomial.X + 1) =
    Polynomial.X ^ 3 - Polynomial.X ^ 2 + 1 ∧
  (Polynomial.X ^ 5 + Polynomial.X + 1) % (Polynomial.X ^ 2 + Polynomial.X + 1) = 0
The certificate: 1 checked step
  1. Statement in ℚ[X]

    (Polynomial.X ^ 5 + Polynomial.X + 1) / (Polynomial.X ^ 2 + Polynomial.X + 1) =
        Polynomial.X ^ 3 - Polynomial.X ^ 2 + 1 ∧
      (Polynomial.X ^ 5 + Polynomial.X + 1) % (Polynomial.X ^ 2 + Polynomial.X + 1) = 0

    Checked by Lean’s kernel


A remainder of a division by a linear polynomial is a number:

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

remainder

98
9/8
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^3 + 1, 2*x - 1)

Checked by Lean’s kernel: Statement in ℚ[X]

(Polynomial.X ^ 3 + 1) / (2 * Polynomial.X - 1) =
    Polynomial.C (1 / 2) * Polynomial.X ^ 2 + Polynomial.C (1 / 4) * Polynomial.X + Polynomial.C (1 / 8) ∧
  (Polynomial.X ^ 3 + 1) % (2 * Polynomial.X - 1) = Polynomial.C (9 / 8)
The certificate: 1 checked step
  1. Statement in ℚ[X]

    (Polynomial.X ^ 3 + 1) / (2 * Polynomial.X - 1) =
        Polynomial.C (1 / 2) * Polynomial.X ^ 2 + Polynomial.C (1 / 4) * Polynomial.X + Polynomial.C (1 / 8) ∧
      (Polynomial.X ^ 3 + 1) % (2 * Polynomial.X - 1) = Polynomial.C (9 / 8)

    Checked by Lean’s kernel

Applications (1)

The second example as a theorem about Mathlib’s %. The remainder is the constant polynomial C (9 / 8), and only the three standard axioms are used:

In[3]:=
open Polynomial in
theorem remainder_by_linear : (X ^ 3 + 1 : ℚ[X]) % (2 * X - 1) = C (9 / 8) := by
  cas

#print axioms remainder_by_linear
Out[3]=

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

Properties and Relations (3)

polynomialQuotient gives the quotient x3−x2+1 of the first example:

In[4]:=
#cas [polynomialQuotient] (x^5 + x + 1, x^2 + x + 1)
Out[4]=

quotient

x3−x2+1
x^3 - x^2 + 1
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^5 + x + 1, x^2 + x + 1)

Checked by Lean’s kernel: Statement in ℚ[X]

(Polynomial.X ^ 5 + Polynomial.X + 1) / (Polynomial.X ^ 2 + Polynomial.X + 1) =
    Polynomial.X ^ 3 - Polynomial.X ^ 2 + 1 ∧
  (Polynomial.X ^ 5 + Polynomial.X + 1) % (Polynomial.X ^ 2 + Polynomial.X + 1) = 0
The certificate: 1 checked step
  1. Statement in ℚ[X]

    (Polynomial.X ^ 5 + Polynomial.X + 1) / (Polynomial.X ^ 2 + Polynomial.X + 1) =
        Polynomial.X ^ 3 - Polynomial.X ^ 2 + 1 ∧
      (Polynomial.X ^ 5 + Polynomial.X + 1) % (Polynomial.X ^ 2 + Polynomial.X + 1) = 0

    Checked by Lean’s kernel


The remainder of x3+1 by x−2 is its value at 2:

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

remainder

9
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^3 + 1, x - 2)

Checked by Lean’s kernel: Statement in ℚ[X]

(Polynomial.X ^ 3 + 1) / (Polynomial.X - 2) = Polynomial.X ^ 2 + 2 * Polynomial.X + 4 ∧
  (Polynomial.X ^ 3 + 1) % (Polynomial.X - 2) = 9
The certificate: 1 checked step
  1. Statement in ℚ[X]

    (Polynomial.X ^ 3 + 1) / (Polynomial.X - 2) = Polynomial.X ^ 2 + 2 * Polynomial.X + 4 ∧
      (Polynomial.X ^ 3 + 1) % (Polynomial.X - 2) = 9

    Checked by Lean’s kernel

In[6]:=
open Polynomial in
example : (X ^ 3 + 1 : ℚ[X]).eval 2 = 9 := by
  cas
Out[6]=

Lean accepts the proof.


When the remainder is 0, the divisor is a gcd of the two. Here it is monic, and polynomialGCD gives it back:

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

monic gcd

x2+x+1
x^2 + x + 1
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^5 + x + 1, x^2 + x + 1)

Checked by Lean’s kernel: Statement in ℚ[X]

gcd (Polynomial.X ^ 5 + Polynomial.X + 1) (Polynomial.X ^ 2 + Polynomial.X + 1) =
  Polynomial.X ^ 2 + Polynomial.X + 1
The certificate: 1 checked step
  1. Statement in ℚ[X]

    gcd (Polynomial.X ^ 5 + Polynomial.X + 1) (Polynomial.X ^ 2 + Polynomial.X + 1) =
      Polynomial.X ^ 2 + Polynomial.X + 1

    Checked by Lean’s kernel

Possible Issues (1)

Mathlib sets p % 0 = p, and a zero divisor is refused:

In[8]:=
#cas [polynomialRemainder] (x^2, 0)
Out[8]=

Error

polynomialRemainder: division by the zero polynomial

Neat Examples (1)

The remainder of x10−1 by x3−1 is x10mod3−1:

In[9]:=
#cas [polynomialRemainder] (x^10 - 1, x^3 - 1)
Out[9]=

remainder

x - 1
Domain
x ↦ X in ℚ[X]
Input (polynomials)
(x^10 - 1, x^3 - 1)

Checked by Lean’s kernel: Statement in ℚ[X]

(Polynomial.X ^ 10 - 1) / (Polynomial.X ^ 3 - 1) =
    Polynomial.X ^ 7 + Polynomial.X ^ 4 + Polynomial.X ∧
  (Polynomial.X ^ 10 - 1) % (Polynomial.X ^ 3 - 1) = Polynomial.X - 1
The certificate: 1 checked step
  1. Statement in ℚ[X]

    (Polynomial.X ^ 10 - 1) / (Polynomial.X ^ 3 - 1) =
        Polynomial.X ^ 7 + Polynomial.X ^ 4 + Polynomial.X ∧
      (Polynomial.X ^ 10 - 1) % (Polynomial.X ^ 3 - 1) = Polynomial.X - 1

    Checked by Lean’s kernel

See Also

polynomialQuotient · polynomialQuotientRemainder · polynomialGCD · member