Mode of #cas
factor
Factors a polynomial with rational coefficients, with a theorem that the product equals the input.
#cas [factor] pfactor 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 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.
Examples (14)#
Basic Examples (2)
A cubic with a double root:
#cas [factor] (2*x^3 - 3*x^2 + 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
Identity with the factored polynomial
∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)
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:
#cas [factor] (x^3*y - x*y^3)
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
Identity with the factored homogeneous polynomial
∀ (x y : ℝ), x ^ 3 * y - x * y ^ 3 = x * y * (x - y) * (x + y)
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:
#cas [factor] (x^4 - 5*x^2 + 4)
(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
Identity with the factored polynomial
∀ (x : ℝ), x ^ 4 - 5 * x ^ 2 + 4 = (x - 1) * (x + 1) * (x - 2) * (x + 2)
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:
#cas [factor] (x^3 - 200000001*x^2 + 200000000*x)
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
Identity with the factored polynomial
∀ (x : ℝ), x ^ 3 - 200000001 * x ^ 2 + 200000000 * x = x * (x - 1) * (x - 200000000)
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 :
#cas [factor] (x^6 - 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
Identity with the factored polynomial
∀ (x : ℝ), x ^ 6 - 1 = (x - 1) * (x + 1) * (x ^ 4 + x ^ 2 + 1)
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:
#cas [factor, oracle := sympy] (x ^ 4 + 4)
Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).
the identity is checked, irreducibility is not
(x^2 - 2*x + 2)*(x^2 + 2*x + 2)SageMath with two variables:
#cas [factor, oracle := sage] (x ^ 3 - y ^ 3)
Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).
the identity is checked, irreducibility is not
(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:
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
cubic_with_double_root depends on the axioms propext, Classical.choice, Quot.sound.
Properties and Relations (3)
expand multiplies the factors out again:
#cas [expand] ((x - 1)^2*(2*x + 1))
2*x^3 - 3*x^2 + 1Checked 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
Identity with the expanded polynomial
∀ (x : ℝ), (x - 1) ^ 2 * (2 * x + 1) = 2 * x ^ 3 - 3 * x ^ 2 + 1
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 it keeps the input, which is smaller than :
#cas [factor] (x ^ 3 - x)
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
Identity with the factored polynomial
∀ (x : ℝ), x ^ 3 - x = x * (x - 1) * (x + 1)
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]
#cas [fullSimplify] (x^3 - x)
x^3 - xChecked by Lean’s kernel: Identity with the input
∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x
The certificate: 1 checked step
Identity with the input
∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x
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:
#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
Possible Issues (3)
stays whole, although it factors over :
#cas [factor] (x^4 + 4)
x^4 + 4Checked by Lean’s kernel: Identity with the input
∀ (x : ℝ), x ^ 4 + 4 = x ^ 4 + 4
The certificate: 1 checked step
Identity with the input
∀ (x : ℝ), x ^ 4 + 4 = x ^ 4 + 4
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 is irreducible, and the report does not say so:
#cas [factor] (x^2 + 1)
x^2 + 1Checked by Lean’s kernel: Identity with the input
∀ (x : ℝ), x ^ 2 + 1 = x ^ 2 + 1
The certificate: 1 checked step
Identity with the input
∀ (x : ℝ), x ^ 2 + 1 = x ^ 2 + 1
The computation
Ranked candidates (proposals, not proof)
input: x^2 + 1 [leafCount 5, conditions 0]
Mathematica is not one of the oracles:
#cas [factor, oracle := mathematica] (x ^ 2 - 1)
Quoted from tests/oracle/Refusals.lean, where #guard_msgs pins the output (the part shown).
Error
unknown oracle mathematica; use sympy, sage, pari, flint, maxima, giac, singular or gappa
