Mode of #cas

factor

Factors a polynomial with rational coefficients, with a theorem that the product equals the input.

#cas [factor] p
factors the polynomial p with rational coefficients and proves that the product equals p.

factor writes a polynomial as a product, which is usually the quickest way to read off its zeros and their multiplicities. The theorem is the identity p=q for all real values of the variables, proved by Mathlib’s tactic ring. So the product is always right, even when the factors could be split further.

Details and Options

  • The theorem is ∀x1,…,xn,p=q about Mathlib’s real numbers, where q is the printed product, proved by ring.
  • The input is a polynomial with rational coefficients in one or more variables.
  • The factors come from the rational roots with their multiplicities, from perfect powers, and from homogeneous polynomials in two variables. A remaining factor of degree two or more is printed whole, and it is not claimed to be irreducible.
  • With CASOracle.Factor imported, #cas [factor, oracle := sympy] p asks an external system for the factors. sage, flint, maxima and singular work the same way, and pari in one variable. The identity is checked in Lean, and irreducibility is not. See oracle.
  • The tactic cas proves the same identities.

Examples (14)#

Basic Examples (2)

A cubic with a double root:

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

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x - 1)^2*(2*x + 1)  [leafCount 11, conditions 0]
    input: 2*x^3 - 3*x^2 + 1  [leafCount 13, conditions 0]

Two variables:

In[2]:=
#cas [factor] (x^3*y - x*y^3)
Out[2]=
xy(x−y)(x+y)
x*y*(x - y)*(x + y)

Checked by Lean’s kernel: Identity with the factored homogeneous polynomial

∀ (x y : ℝ), x ^ 3 * y - x * y ^ 3 = x * y * (x - y) * (x + y)
The certificate: 1 checked step
  1. Identity with the factored homogeneous polynomial

    ∀ (x y : ℝ), x ^ 3 * y - x * y ^ 3 = x * y * (x - y) * (x + y)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored homogeneous polynomial: x*y*(x - y)*(x + y)  [leafCount 11, conditions 0]
    factored homogeneous polynomial: -(y*x*(y - x)*(y + x))  [leafCount 13, conditions 0]
    input: x^3*y - x*y^3  [leafCount 11, conditions 0]

Scope (3)

Four rational roots:

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

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 4 - 5 * x ^ 2 + 4 = (x - 1) * (x + 1) * (x - 2) * (x + 2)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 4 - 5 * x ^ 2 + 4 = (x - 1) * (x + 1) * (x - 2) * (x + 2)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x - 1)*(x + 1)*(x - 2)*(x + 2)  [leafCount 15, conditions 0]
    input: x^4 - 5*x^2 + 4  [leafCount 11, conditions 0]

The rational roots can be large:

In[4]:=
#cas [factor] (x^3 - 200000001*x^2 + 200000000*x)
Out[4]=
x*(x - 1)*(x - 200000000)

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 3 - 200000001 * x ^ 2 + 200000000 * x = x * (x - 1) * (x - 200000000)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 3 - 200000001 * x ^ 2 + 200000000 * x = x * (x - 1) * (x - 200000000)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: x*(x - 1)*(x - 200000000)  [leafCount 9, conditions 0]
    input: x^3 - 200000001*x^2 + 200000000*x  [leafCount 13, conditions 0]

A factor with no rational root can stay whole. Here it is x4+x2+1=(x2−x+1)(x2+x+1):

In[5]:=
#cas [factor] (x^6 - 1)
Out[5]=
(x−1)(x+1)(x4+x2+1)
(x - 1)*(x + 1)*(x^4 + x^2 + 1)

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 6 - 1 = (x - 1) * (x + 1) * (x ^ 4 + x ^ 2 + 1)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 6 - 1 = (x - 1) * (x + 1) * (x ^ 4 + x ^ 2 + 1)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x - 1)*(x + 1)*(x^4 + x^2 + 1)  [leafCount 17, conditions 0]
    input: x^6 - 1  [leafCount 5, conditions 0]

Options (2)

An external system can propose the factors. Here SymPy finds Sophie Germain’s identity, and Lean checks the product:

In[6]:=
#cas [factor, oracle := sympy] (x ^ 4 + 4)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[6]=

the identity is checked, irreducibility is not

(x2−2x+2)(x2+2x+2)
(x^2 - 2*x + 2)*(x^2 + 2*x + 2)

SageMath with two variables:

In[7]:=
#cas [factor, oracle := sage] (x ^ 3 - y ^ 3)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[7]=

the identity is checked, irreducibility is not

(x−y)(x2+xy+y2)
(x - y)*(x^2 + x*y + y^2)

Applications (1)

The first factorization as a theorem, for every real x. Only the three standard axioms are used:

In[8]:=
theorem cubic_with_double_root (x : ℝ) :
    2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1) := by
  cas

#print axioms cubic_with_double_root
Out[8]=

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

Properties and Relations (3)

expand multiplies the factors out again:

In[9]:=
#cas [expand] ((x - 1)^2*(2*x + 1))
Out[9]=
2x3−3x2+1
2*x^3 - 3*x^2 + 1

Checked by Lean’s kernel: Identity with the expanded polynomial

∀ (x : ℝ), (x - 1) ^ 2 * (2 * x + 1) = 2 * x ^ 3 - 3 * x ^ 2 + 1
The certificate: 1 checked step
  1. Identity with the expanded polynomial

    ∀ (x : ℝ), (x - 1) ^ 2 * (2 * x + 1) = 2 * x ^ 3 - 3 * x ^ 2 + 1

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    expanded polynomial: 2*x^3 - 3*x^2 + 1  [leafCount 13, conditions 0]
    input: (x - 1)^2*(2*x + 1)  [leafCount 11, conditions 0]

fullSimplify chooses between the factored and the expanded form by size. For x3−x it keeps the input, which is smaller than x(x−1)(x+1):

In[10]:=
#cas [factor] (x ^ 3 - x)
Out[10]=
x*(x - 1)*(x + 1)

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 3 - x = x * (x - 1) * (x + 1)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 3 - x = x * (x - 1) * (x + 1)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: x*(x - 1)*(x + 1)  [leafCount 9, conditions 0]
    input: x^3 - x  [leafCount 5, conditions 0]
In[11]:=
#cas [fullSimplify] (x^3 - x)
Out[11]=
x3−x
x^3 - x

Checked by Lean’s kernel: Identity with the input

∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: x^3 - x  [leafCount 5, conditions 0]
    exact passes: x*(x^2 - 1)  [leafCount 7, conditions 0]
    factored polynomial: x*(x - 1)*(x + 1)  [leafCount 9, conditions 0]

polynomialGCD finds the factors two polynomials share:

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

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

Possible Issues (3)

x4+4 stays whole, although it factors over ℚ:

In[13]:=
#cas [factor] (x^4 + 4)
Out[13]=
x4+4
x^4 + 4

Checked by Lean’s kernel: Identity with the input

∀ (x : ℝ), x ^ 4 + 4 = x ^ 4 + 4
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x : ℝ), x ^ 4 + 4 = x ^ 4 + 4

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: x^4 + 4  [leafCount 5, conditions 0]

A polynomial that stays whole is not claimed to be irreducible. Here x2+1 is irreducible, and the report does not say so:

In[14]:=
#cas [factor] (x^2 + 1)
Out[14]=
x2+1
x^2 + 1

Checked by Lean’s kernel: Identity with the input

∀ (x : ℝ), x ^ 2 + 1 = x ^ 2 + 1
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x : ℝ), x ^ 2 + 1 = x ^ 2 + 1

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: x^2 + 1  [leafCount 5, conditions 0]

Mathematica is not one of the oracles:

In[15]:=
#cas [factor, oracle := mathematica] (x ^ 2 - 1)

Quoted from tests/oracle/Refusals.lean, where #guard_msgs pins the output (the part shown).

Out[15]=

Error

unknown oracle mathematica; use sympy, sage, pari, flint, maxima, giac, singular or gappa

See Also

expand · roots · polynomialGCD · apart · together · oracle