Mode of #cas

polynomialQuotientRemainder

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

#cas [polynomialQuotientRemainder] (p, q)
gives the quotient s and 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].

polynomialQuotientRemainder is polynomial long division with a proof attached. The output is the pair [s,r] with p=sq+r and degr<degq, and the theorem is about Mathlib’s / and % on ℚ[X].

For the proof we divide by the monic polynomial q/c, where c is the leading coefficient of q. The identity p=s′(q/c)+r and the degree bound determine the quotient and the remainder by Mathlib’s Polynomial.div_modByMonic_unique, and Polynomial.div_def and Polynomial.mod_def relate them to / and %.

Details and Options

  • The theorem is p / q = s ∧ p % q = r in ℚ[X], with the variable read as Polynomial.X. The two inputs have the same single variable.
  • The remainder has a smaller degree than q. When q has a larger degree than p, the quotient is 0 and the remainder is p.
  • The divisor q is nonzero. Mathlib’s division is total, with p / 0 = 0 and p % 0 = p, and a zero divisor is refused.
  • The tactic cas proves p / q = s and p % q = r goals in ℚ[X]. It proves each part but not the conjunction itself, so you prove the conjunction as ⟨by cas, by cas⟩.
  • polynomialQuotient and polynomialRemainder print one of the two, with the same theorem.
  • There are no options.

Examples (9)#

Basic Examples (1)

A division by 2x−1. The quotient has fractions as coefficients:

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

[quotient, remainder]

[12x2+14x+18, 98]
[1/2*x^2 + 1/4*x + 1/8, 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

Scope (2)

The zero polynomial divided by x+1:

In[2]:=
#cas [polynomialQuotientRemainder] (0, x + 1)
Out[2]=

[quotient, remainder]

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

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

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

    0 / (Polynomial.X + 1) = 0 ∧ 0 % (Polynomial.X + 1) = 0

    Checked by Lean’s kernel


When the divisor has the larger degree, the quotient is 0 and the remainder is the dividend:

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

[quotient, remainder]

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

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

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

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

    Checked by Lean’s kernel

Applications (1)

The first example as a theorem about Mathlib’s / and %, one part at a time. Only the three standard axioms are used:

In[4]:=
open Polynomial in
theorem division_by_linear :
    (X ^ 3 + 1 : ℚ[X]) / (2 * X - 1) = C (1 / 2) * X ^ 2 + C (1 / 4) * X + C (1 / 8) ∧
      (X ^ 3 + 1 : ℚ[X]) % (2 * X - 1) = C (9 / 8) :=
  ⟨by cas, by cas⟩

#print axioms division_by_linear
Out[4]=

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

Properties and Relations (3)

polynomialQuotient prints the quotient alone:

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

quotient

12x2+14x+18
1/2*x^2 + 1/4*x + 1/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


polynomialRemainder prints the remainder alone:

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

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


apart splits off the polynomial part of a rational function. For x3/(x2+1) it is the quotient x of x3 by x2+1, and the remainder −x stays over x2+1:

In[7]:=
#cas [apart] (x^3/(x^2+1))
Out[7]=

partial fractions over the written factors, refined by gcds

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

Checked by Lean’s kernel: Identity with the partial fraction decomposition

∀ (x : ℝ), x ^ 2 + 1 ≠ 0 → x ^ 3 / (x ^ 2 + 1) = x - x / (x ^ 2 + 1)
The certificate: 1 checked step
  1. Identity with the partial fraction decomposition

    ∀ (x : ℝ), x ^ 2 + 1 ≠ 0 → x ^ 3 / (x ^ 2 + 1) = x - x / (x ^ 2 + 1)

    Checked by Lean’s kernel

Possible Issues (1)

The divisor must be nonzero:

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

Error

polynomialQuotientRemainder: division by the zero polynomial

Neat Examples (1)

Dividing x10−1 by x3−1 leaves x10mod3−1=x−1:

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

[quotient, remainder]

[x7+x4+x, x−1]
[x^7 + x^4 + x, 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 · polynomialRemainder · polynomialGCD · apart