# Theory Fundamental_Theorem_Algebra

theory Fundamental_Theorem_Algebra
imports Polynomial
```(*  Title:      HOL/Computational_Algebra/Fundamental_Theorem_Algebra.thy
Author:     Amine Chaieb, TU Muenchen
*)

section ‹Fundamental Theorem of Algebra›

theory Fundamental_Theorem_Algebra
imports Polynomial Complex_Main
begin

subsection ‹More lemmas about module of complex numbers›

text ‹The triangle inequality for cmod›

lemma complex_mod_triangle_sub: "cmod w ≤ cmod (w + z) + norm z"
using complex_mod_triangle_ineq2[of "w + z" "-z"] by auto

lemma poly_bound_exists:
fixes p :: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
shows "∃m. m > 0 ∧ (∀z. norm z ≤ r ⟶ norm (poly p z) ≤ m)"
proof (induct p)
case 0
then show ?case by (rule exI[where x=1]) simp
next
case (pCons c cs)
from pCons.hyps obtain m where m: "∀z. norm z ≤ r ⟶ norm (poly cs z) ≤ m"
by blast
let ?k = " 1 + norm c + ¦r * m¦"
have kp: "?k > 0"
using abs_ge_zero[of "r*m"] norm_ge_zero[of c] by arith
have "norm (poly (pCons c cs) z) ≤ ?k" if H: "norm z ≤ r" for z
proof -
from m H have th: "norm (poly cs z) ≤ m"
by blast
from H have rp: "r ≥ 0"
using norm_ge_zero[of z] by arith
have "norm (poly (pCons c cs) z) ≤ norm c + norm (z * poly cs z)"
using norm_triangle_ineq[of c "z* poly cs z"] by simp
also have "… ≤ norm c + r * m"
using mult_mono[OF H th rp norm_ge_zero[of "poly cs z"]]
also have "… ≤ ?k"
by simp
finally show ?thesis .
qed
with kp show ?case by blast
qed

text ‹Offsetting the variable in a polynomial gives another of same degree›

definition offset_poly :: "'a::comm_semiring_0 poly ⇒ 'a ⇒ 'a poly"
where "offset_poly p h = fold_coeffs (λa q. smult h q + pCons a q) p 0"

lemma offset_poly_0: "offset_poly 0 h = 0"

lemma offset_poly_pCons:
"offset_poly (pCons a p) h =
smult h (offset_poly p h) + pCons a (offset_poly p h)"
by (cases "p = 0 ∧ a = 0") (auto simp add: offset_poly_def)

lemma offset_poly_single: "offset_poly [:a:] h = [:a:]"

lemma poly_offset_poly: "poly (offset_poly p h) x = poly p (h + x)"
apply (induct p)
done

lemma offset_poly_eq_0_lemma: "smult c p + pCons a p = 0 ⟹ p = 0"
by (induct p arbitrary: a) (simp, force)

lemma offset_poly_eq_0_iff: "offset_poly p h = 0 ⟷ p = 0"
apply (safe intro!: offset_poly_0)
apply (induct p)
apply simp
apply (frule offset_poly_eq_0_lemma, simp)
done

lemma degree_offset_poly: "degree (offset_poly p h) = degree p"
apply (induct p)
apply (case_tac "p = 0")
apply (rule le_less_trans [OF degree_smult_le])
done

definition "psize p = (if p = 0 then 0 else Suc (degree p))"

lemma psize_eq_0_iff [simp]: "psize p = 0 ⟷ p = 0"
unfolding psize_def by simp

lemma poly_offset:
fixes p :: "'a::comm_ring_1 poly"
shows "∃q. psize q = psize p ∧ (∀x. poly q x = poly p (a + x))"
proof (intro exI conjI)
show "psize (offset_poly p a) = psize p"
unfolding psize_def
show "∀x. poly (offset_poly p a) x = poly p (a + x)"
qed

text ‹An alternative useful formulation of completeness of the reals›
lemma real_sup_exists:
assumes ex: "∃x. P x"
and bz: "∃z. ∀x. P x ⟶ x < z"
shows "∃s::real. ∀y. (∃x. P x ∧ y < x) ⟷ y < s"
proof
from bz have "bdd_above (Collect P)"
by (force intro: less_imp_le)
then show "∀y. (∃x. P x ∧ y < x) ⟷ y < Sup (Collect P)"
using ex bz by (subst less_cSup_iff) auto
qed

subsection ‹Fundamental theorem of algebra›

lemma unimodular_reduce_norm:
assumes md: "cmod z = 1"
shows "cmod (z + 1) < 1 ∨ cmod (z - 1) < 1 ∨ cmod (z + 𝗂) < 1 ∨ cmod (z - 𝗂) < 1"
proof -
obtain x y where z: "z = Complex x y "
by (cases z) auto
from md z have xy: "x⇧2 + y⇧2 = 1"
have False if "cmod (z + 1) ≥ 1" "cmod (z - 1) ≥ 1" "cmod (z + 𝗂) ≥ 1" "cmod (z - 𝗂) ≥ 1"
proof -
from that z xy have "2 * x ≤ 1" "2 * x ≥ -1" "2 * y ≤ 1" "2 * y ≥ -1"
by (simp_all add: cmod_def power2_eq_square algebra_simps)
then have "¦2 * x¦ ≤ 1" "¦2 * y¦ ≤ 1"
by simp_all
then have "¦2 * x¦⇧2 ≤ 1⇧2" "¦2 * y¦⇧2 ≤ 1⇧2"
by - (rule power_mono, simp, simp)+
then have th0: "4 * x⇧2 ≤ 1" "4 * y⇧2 ≤ 1"
from add_mono[OF th0] xy show ?thesis
by simp
qed
then show ?thesis
unfolding linorder_not_le[symmetric] by blast
qed

text ‹Hence we can always reduce modulus of ‹1 + b z^n› if nonzero›
lemma reduce_poly_simple:
assumes b: "b ≠ 0"
and n: "n ≠ 0"
shows "∃z. cmod (1 + b * z^n) < 1"
using n
proof (induct n rule: nat_less_induct)
fix n
assume IH: "∀m<n. m ≠ 0 ⟶ (∃z. cmod (1 + b * z ^ m) < 1)"
assume n: "n ≠ 0"
let ?P = "λz n. cmod (1 + b * z ^ n) < 1"
show "∃z. ?P z n"
proof cases
assume "even n"
then have "∃m. n = 2 * m"
by presburger
then obtain m where m: "n = 2 * m"
by blast
from n m have "m ≠ 0" "m < n"
by presburger+
with IH[rule_format, of m] obtain z where z: "?P z m"
by blast
from z have "?P (csqrt z) n"
then show ?thesis ..
next
assume "odd n"
then have "∃m. n = Suc (2 * m)"
by presburger+
then obtain m where m: "n = Suc (2 * m)"
by blast
have th0: "cmod (complex_of_real (cmod b) / b) = 1"
using b by (simp add: norm_divide)
from unimodular_reduce_norm[OF th0] ‹odd n›
have "∃v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1")
apply (rule_tac x="1" in exI)
apply simp
apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1")
apply (rule_tac x="-1" in exI)
apply simp
apply (cases "cmod (complex_of_real (cmod b) / b + 𝗂) < 1")
apply (cases "even m")
apply (rule_tac x="𝗂" in exI)
apply (rule_tac x="- 𝗂" in exI)
apply (cases "even m")
apply (rule_tac x="- 𝗂" in exI)
apply (auto simp add: m power_mult)
apply (rule_tac x="𝗂" in exI)
apply (auto simp add: m power_mult)
done
then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1"
by blast
let ?w = "v / complex_of_real (root n (cmod b))"
from odd_real_root_pow[OF ‹odd n›, of "cmod b"]
have th1: "?w ^ n = v^n / complex_of_real (cmod b)"
have th2:"cmod (complex_of_real (cmod b) / b) = 1"
using b by (simp add: norm_divide)
then have th3: "cmod (complex_of_real (cmod b) / b) ≥ 0"
by simp
have th4: "cmod (complex_of_real (cmod b) / b) *
cmod (1 + b * (v ^ n / complex_of_real (cmod b))) <
cmod (complex_of_real (cmod b) / b) * 1"
apply (simp only: norm_mult[symmetric] distrib_left)
using b v
done
from mult_left_less_imp_less[OF th4 th3]
have "?P ?w n" unfolding th1 .
then show ?thesis ..
qed
qed

text ‹Bolzano-Weierstrass type property for closed disc in complex plane.›

lemma metric_bound_lemma: "cmod (x - y) ≤ ¦Re x - Re y¦ + ¦Im x - Im y¦"
using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y"]
unfolding cmod_def by simp

lemma bolzano_weierstrass_complex_disc:
assumes r: "∀n. cmod (s n) ≤ r"
shows "∃f z. strict_mono (f :: nat ⇒ nat) ∧ (∀e >0. ∃N. ∀n ≥ N. cmod (s (f n) - z) < e)"
proof -
from seq_monosub[of "Re ∘ s"]
obtain f where f: "strict_mono f" "monoseq (λn. Re (s (f n)))"
unfolding o_def by blast
from seq_monosub[of "Im ∘ s ∘ f"]
obtain g where g: "strict_mono g" "monoseq (λn. Im (s (f (g n))))"
unfolding o_def by blast
let ?h = "f ∘ g"
from r[rule_format, of 0] have rp: "r ≥ 0"
using norm_ge_zero[of "s 0"] by arith
have th: "∀n. r + 1 ≥ ¦Re (s n)¦"
proof
fix n
from abs_Re_le_cmod[of "s n"] r[rule_format, of n]
show "¦Re (s n)¦ ≤ r + 1" by arith
qed
have conv1: "convergent (λn. Re (s (f n)))"
apply (rule Bseq_monoseq_convergent)
apply (metis gt_ex le_less_linear less_trans order.trans th)
apply (rule f(2))
done
have th: "∀n. r + 1 ≥ ¦Im (s n)¦"
proof
fix n
from abs_Im_le_cmod[of "s n"] r[rule_format, of n]
show "¦Im (s n)¦ ≤ r + 1"
by arith
qed

have conv2: "convergent (λn. Im (s (f (g n))))"
apply (rule Bseq_monoseq_convergent)
apply (metis gt_ex le_less_linear less_trans order.trans th)
apply (rule g(2))
done

from conv1[unfolded convergent_def] obtain x where "LIMSEQ (λn. Re (s (f n))) x"
by blast
then have x: "∀r>0. ∃n0. ∀n≥n0. ¦Re (s (f n)) - x¦ < r"
unfolding LIMSEQ_iff real_norm_def .

from conv2[unfolded convergent_def] obtain y where "LIMSEQ (λn. Im (s (f (g n)))) y"
by blast
then have y: "∀r>0. ∃n0. ∀n≥n0. ¦Im (s (f (g n))) - y¦ < r"
unfolding LIMSEQ_iff real_norm_def .
let ?w = "Complex x y"
from f(1) g(1) have hs: "strict_mono ?h"
unfolding strict_mono_def by auto
have "∃N. ∀n≥N. cmod (s (?h n) - ?w) < e" if "e > 0" for e
proof -
from that have e2: "e/2 > 0"
by simp
from x[rule_format, OF e2] y[rule_format, OF e2]
obtain N1 N2 where N1: "∀n≥N1. ¦Re (s (f n)) - x¦ < e / 2"
and N2: "∀n≥N2. ¦Im (s (f (g n))) - y¦ < e / 2"
by blast
have "cmod (s (?h n) - ?w) < e" if "n ≥ N1 + N2" for n
proof -
from that have nN1: "g n ≥ N1" and nN2: "n ≥ N2"
using seq_suble[OF g(1), of n] by arith+
from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
show ?thesis
using metric_bound_lemma[of "s (f (g n))" ?w] by simp
qed
then show ?thesis by blast
qed
with hs show ?thesis by blast
qed

text ‹Polynomial is continuous.›

lemma poly_cont:
fixes p :: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
assumes ep: "e > 0"
shows "∃d >0. ∀w. 0 < norm (w - z) ∧ norm (w - z) < d ⟶ norm (poly p w - poly p z) < e"
proof -
obtain q where q: "degree q = degree p" "poly q x = poly p (z + x)" for x
proof
show "degree (offset_poly p z) = degree p"
by (rule degree_offset_poly)
show "⋀x. poly (offset_poly p z) x = poly p (z + x)"
by (rule poly_offset_poly)
qed
have th: "⋀w. poly q (w - z) = poly p w"
using q(2)[of "w - z" for w] by simp
show ?thesis unfolding th[symmetric]
proof (induct q)
case 0
then show ?case
using ep by auto
next
case (pCons c cs)
from poly_bound_exists[of 1 "cs"]
obtain m where m: "m > 0" "norm z ≤ 1 ⟹ norm (poly cs z) ≤ m" for z
by blast
from ep m(1) have em0: "e/m > 0"
have one0: "1 > (0::real)"
by arith
from field_lbound_gt_zero[OF one0 em0]
obtain d where d: "d > 0" "d < 1" "d < e / m"
by blast
from d(1,3) m(1) have dm: "d * m > 0" "d * m < e"
show ?case
proof (rule ex_forward[OF field_lbound_gt_zero[OF one0 em0]], clarsimp simp add: norm_mult)
fix d w
assume H: "d > 0" "d < 1" "d < e/m" "w ≠ z" "norm (w - z) < d"
then have d1: "norm (w-z) ≤ 1" "d ≥ 0"
by simp_all
from H(3) m(1) have dme: "d*m < e"
from H have th: "norm (w - z) ≤ d"
by simp
from mult_mono[OF th m(2)[OF d1(1)] d1(2) norm_ge_zero] dme
show "norm (w - z) * norm (poly cs (w - z)) < e"
by simp
qed
qed
qed

text ‹Hence a polynomial attains minimum on a closed disc
in the complex plane.›
lemma poly_minimum_modulus_disc: "∃z. ∀w. cmod w ≤ r ⟶ cmod (poly p z) ≤ cmod (poly p w)"
proof -
show ?thesis
proof (cases "r ≥ 0")
case False
then show ?thesis
by (metis norm_ge_zero order.trans)
next
case True
then have "cmod 0 ≤ r ∧ cmod (poly p 0) = - (- cmod (poly p 0))"
by simp
then have mth1: "∃x z. cmod z ≤ r ∧ cmod (poly p z) = - x"
by blast
have False if "cmod z ≤ r" "cmod (poly p z) = - x" "¬ x < 1" for x z
proof -
from that have "- x < 0 "
by arith
with that(2) norm_ge_zero[of "poly p z"] show ?thesis
by simp
qed
then have mth2: "∃z. ∀x. (∃z. cmod z ≤ r ∧ cmod (poly p z) = - x) ⟶ x < z"
by blast
from real_sup_exists[OF mth1 mth2] obtain s where
s: "∀y. (∃x. (∃z. cmod z ≤ r ∧ cmod (poly p z) = - x) ∧ y < x) ⟷ y < s"
by blast
let ?m = "- s"
have s1[unfolded minus_minus]:
"(∃z x. cmod z ≤ r ∧ - (- cmod (poly p z)) < y) ⟷ ?m < y" for y
using s[rule_format, of "-y"]
unfolding minus_less_iff[of y] equation_minus_iff by blast
from s1[of ?m] have s1m: "⋀z x. cmod z ≤ r ⟹ cmod (poly p z) ≥ ?m"
by auto
have "∃z. cmod z ≤ r ∧ cmod (poly p z) < - s + 1 / real (Suc n)" for n
using s1[rule_format, of "?m + 1/real (Suc n)"] by simp
then have th: "∀n. ∃z. cmod z ≤ r ∧ cmod (poly p z) < - s + 1 / real (Suc n)" ..
from choice[OF th] obtain g where
g: "∀n. cmod (g n) ≤ r" "∀n. cmod (poly p (g n)) <?m + 1 /real(Suc n)"
by blast
from bolzano_weierstrass_complex_disc[OF g(1)]
obtain f z where fz: "strict_mono (f :: nat ⇒ nat)" "∀e>0. ∃N. ∀n≥N. cmod (g (f n) - z) < e"
by blast
{
fix w
assume wr: "cmod w ≤ r"
let ?e = "¦cmod (poly p z) - ?m¦"
{
assume e: "?e > 0"
then have e2: "?e/2 > 0"
by simp
from poly_cont[OF e2, of z p] obtain d where
d: "d > 0" "∀w. 0<cmod (w - z)∧ cmod(w - z) < d ⟶ cmod(poly p w - poly p z) < ?e/2"
by blast
have th1: "cmod(poly p w - poly p z) < ?e / 2" if w: "cmod (w - z) < d" for w
using d(2)[rule_format, of w] w e by (cases "w = z") simp_all
from fz(2) d(1) obtain N1 where N1: "∀n≥N1. cmod (g (f n) - z) < d"
by blast
from reals_Archimedean2[of "2/?e"] obtain N2 :: nat where N2: "2/?e < real N2"
by blast
have th2: "cmod (poly p (g (f (N1 + N2))) - poly p z) < ?e/2"
using N1[rule_format, of "N1 + N2"] th1 by simp
have th0: "a < e2 ⟹ ¦b - m¦ < e2 ⟹ 2 * e2 ≤ ¦b - m¦ + a ⟹ False"
for a b e2 m :: real
by arith
have ath: "m ≤ x ⟹ x < m + e ⟹ ¦x - m¦ < e" for m x e :: real
by arith
from s1m[OF g(1)[rule_format]] have th31: "?m ≤ cmod(poly p (g (f (N1 + N2))))" .
from seq_suble[OF fz(1), of "N1 + N2"]
have th00: "real (Suc (N1 + N2)) ≤ real (Suc (f (N1 + N2)))"
by simp
have th000: "0 ≤ (1::real)" "(1::real) ≤ 1" "real (Suc (N1 + N2)) > 0"
using N2 by auto
from frac_le[OF th000 th00]
have th00: "?m + 1 / real (Suc (f (N1 + N2))) ≤ ?m + 1 / real (Suc (N1 + N2))"
by simp
from g(2)[rule_format, of "f (N1 + N2)"]
have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
from order_less_le_trans[OF th01 th00]
have th32: "cmod (poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
from N2 have "2/?e < real (Suc (N1 + N2))"
by arith
with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
have "?e/2 > 1/ real (Suc (N1 + N2))"
with ath[OF th31 th32] have thc1: "¦cmod (poly p (g (f (N1 + N2)))) - ?m¦ < ?e/2"
by arith
have ath2: "¦a - b¦ ≤ c ⟹ ¦b - m¦ ≤ ¦a - m¦ + c" for a b c m :: real
by arith
have th22: "¦cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)¦ ≤
cmod (poly p (g (f (N1 + N2))) - poly p z)"
from ath2[OF th22, of ?m]
have thc2: "2 * (?e/2) ≤
¦cmod(poly p (g (f (N1 + N2)))) - ?m¦ + cmod (poly p (g (f (N1 + N2))) - poly p z)"
by simp
from th0[OF th2 thc1 thc2] have False .
}
then have "?e = 0"
by auto
then have "cmod (poly p z) = ?m"
by simp
with s1m[OF wr] have "cmod (poly p z) ≤ cmod (poly p w)"
by simp
}
then show ?thesis by blast
qed
qed

text ‹Nonzero polynomial in z goes to infinity as z does.›

lemma poly_infinity:
fixes p:: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
assumes ex: "p ≠ 0"
shows "∃r. ∀z. r ≤ norm z ⟶ d ≤ norm (poly (pCons a p) z)"
using ex
proof (induct p arbitrary: a d)
case 0
then show ?case by simp
next
case (pCons c cs a d)
show ?case
proof (cases "cs = 0")
case False
with pCons.hyps obtain r where r: "∀z. r ≤ norm z ⟶ d + norm a ≤ norm (poly (pCons c cs) z)"
by blast
let ?r = "1 + ¦r¦"
have "d ≤ norm (poly (pCons a (pCons c cs)) z)" if "1 + ¦r¦ ≤ norm z" for z
proof -
have r0: "r ≤ norm z"
using that by arith
from r[rule_format, OF r0] have th0: "d + norm a ≤ 1 * norm(poly (pCons c cs) z)"
by arith
from that have z1: "norm z ≥ 1"
by arith
from order_trans[OF th0 mult_right_mono[OF z1 norm_ge_zero[of "poly (pCons c cs) z"]]]
have th1: "d ≤ norm(z * poly (pCons c cs) z) - norm a"
unfolding norm_mult by (simp add: algebra_simps)
from norm_diff_ineq[of "z * poly (pCons c cs) z" a]
have th2: "norm (z * poly (pCons c cs) z) - norm a ≤ norm (poly (pCons a (pCons c cs)) z)"
from th1 th2 show ?thesis
by arith
qed
then show ?thesis by blast
next
case True
with pCons.prems have c0: "c ≠ 0"
by simp
have "d ≤ norm (poly (pCons a (pCons c cs)) z)"
if h: "(¦d¦ + norm a) / norm c ≤ norm z" for z :: 'a
proof -
from c0 have "norm c > 0"
by simp
from h c0 have th0: "¦d¦ + norm a ≤ norm (z * c)"
have ath: "⋀mzh mazh ma. mzh ≤ mazh + ma ⟹ ¦d¦ + ma ≤ mzh ⟹ d ≤ mazh"
by arith
from norm_diff_ineq[of "z * c" a] have th1: "norm (z * c) ≤ norm (a + z * c) + norm a"
from ath[OF th1 th0] show ?thesis
using True by simp
qed
then show ?thesis by blast
qed
qed

text ‹Hence polynomial's modulus attains its minimum somewhere.›
lemma poly_minimum_modulus: "∃z.∀w. cmod (poly p z) ≤ cmod (poly p w)"
proof (induct p)
case 0
then show ?case by simp
next
case (pCons c cs)
show ?case
proof (cases "cs = 0")
case False
from poly_infinity[OF False, of "cmod (poly (pCons c cs) 0)" c]
obtain r where r: "cmod (poly (pCons c cs) 0) ≤ cmod (poly (pCons c cs) z)"
if "r ≤ cmod z" for z
by blast
have ath: "⋀z r. r ≤ cmod z ∨ cmod z ≤ ¦r¦"
by arith
from poly_minimum_modulus_disc[of "¦r¦" "pCons c cs"]
obtain v where v: "cmod (poly (pCons c cs) v) ≤ cmod (poly (pCons c cs) w)"
if "cmod w ≤ ¦r¦" for w
by blast
have "cmod (poly (pCons c cs) v) ≤ cmod (poly (pCons c cs) z)" if z: "r ≤ cmod z" for z
using v[of 0] r[OF z] by simp
with v ath[of r] show ?thesis
by blast
next
case True
with pCons.hyps show ?thesis
by simp
qed
qed

text ‹Constant function (non-syntactic characterization).›
definition "constant f ⟷ (∀x y. f x = f y)"

lemma nonconstant_length: "¬ constant (poly p) ⟹ psize p ≥ 2"
by (induct p) (auto simp: constant_def psize_def)

lemma poly_replicate_append: "poly (monom 1 n * p) (x::'a::comm_ring_1) = x^n * poly p x"

text ‹Decomposition of polynomial, skipping zero coefficients after the first.›

lemma poly_decompose_lemma:
assumes nz: "¬ (∀z. z ≠ 0 ⟶ poly p z = (0::'a::idom))"
shows "∃k a q. a ≠ 0 ∧ Suc (psize q + k) = psize p ∧ (∀z. poly p z = z^k * poly (pCons a q) z)"
unfolding psize_def
using nz
proof (induct p)
case 0
then show ?case by simp
next
case (pCons c cs)
show ?case
proof (cases "c = 0")
case True
from pCons.hyps pCons.prems True show ?thesis
apply auto
apply (rule_tac x="k+1" in exI)
apply (rule_tac x="a" in exI)
apply clarsimp
apply (rule_tac x="q" in exI)
apply auto
done
next
case False
show ?thesis
apply (rule exI[where x=0])
apply (rule exI[where x=c])
apply (auto simp: False)
done
qed
qed

lemma poly_decompose:
assumes nc: "¬ constant (poly p)"
shows "∃k a q. a ≠ (0::'a::idom) ∧ k ≠ 0 ∧
psize q + k + 1 = psize p ∧
(∀z. poly p z = poly p 0 + z^k * poly (pCons a q) z)"
using nc
proof (induct p)
case 0
then show ?case
next
case (pCons c cs)
have "¬ (∀z. z ≠ 0 ⟶ poly cs z = 0)"
proof
assume "∀z. z ≠ 0 ⟶ poly cs z = 0"
then have "poly (pCons c cs) x = poly (pCons c cs) y" for x y
by (cases "x = 0") auto
with pCons.prems show False
qed
from poly_decompose_lemma[OF this]
show ?case
apply clarsimp
apply (rule_tac x="k+1" in exI)
apply (rule_tac x="a" in exI)
apply simp
apply (rule_tac x="q" in exI)
apply (auto simp add: psize_def split: if_splits)
done
qed

text ‹Fundamental theorem of algebra›

lemma fundamental_theorem_of_algebra:
assumes nc: "¬ constant (poly p)"
shows "∃z::complex. poly p z = 0"
using nc
proof (induct "psize p" arbitrary: p rule: less_induct)
case less
let ?p = "poly p"
let ?ths = "∃z. ?p z = 0"

from nonconstant_length[OF less(2)] have n2: "psize p ≥ 2" .
from poly_minimum_modulus obtain c where c: "∀w. cmod (?p c) ≤ cmod (?p w)"
by blast

show ?ths
proof (cases "?p c = 0")
case True
then show ?thesis by blast
next
case False
from poly_offset[of p c] obtain q where q: "psize q = psize p" "∀x. poly q x = ?p (c + x)"
by blast
have False if h: "constant (poly q)"
proof -
from q(2) have th: "∀x. poly q (x - c) = ?p x"
by auto
have "?p x = ?p y" for x y
proof -
from th have "?p x = poly q (x - c)"
by auto
also have "… = poly q (y - c)"
using h unfolding constant_def by blast
also have "… = ?p y"
using th by auto
finally show ?thesis .
qed
with less(2) show ?thesis
unfolding constant_def by blast
qed
then have qnc: "¬ constant (poly q)"
by blast
from q(2) have pqc0: "?p c = poly q 0"
by simp
from c pqc0 have cq0: "∀w. cmod (poly q 0) ≤ cmod (?p w)"
by simp
let ?a0 = "poly q 0"
from False pqc0 have a00: "?a0 ≠ 0"
by simp
from a00 have qr: "∀z. poly q z = poly (smult (inverse ?a0) q) z * ?a0"
by simp
let ?r = "smult (inverse ?a0) q"
have lgqr: "psize q = psize ?r"
using a00
unfolding psize_def degree_def
have False if h: "⋀x y. poly ?r x = poly ?r y"
proof -
have "poly q x = poly q y" for x y
proof -
from qr[rule_format, of x] have "poly q x = poly ?r x * ?a0"
by auto
also have "… = poly ?r y * ?a0"
using h by simp
also have "… = poly q y"
using qr[rule_format, of y] by simp
finally show ?thesis .
qed
with qnc show ?thesis
unfolding constant_def by blast
qed
then have rnc: "¬ constant (poly ?r)"
unfolding constant_def by blast
from qr[rule_format, of 0] a00 have r01: "poly ?r 0 = 1"
by auto
have mrmq_eq: "cmod (poly ?r w) < 1 ⟷ cmod (poly q w) < cmod ?a0" for w
proof -
have "cmod (poly ?r w) < 1 ⟷ cmod (poly q w / ?a0) < 1"
using qr[rule_format, of w] a00 by (simp add: divide_inverse ac_simps)
also have "… ⟷ cmod (poly q w) < cmod ?a0"
using a00 unfolding norm_divide by (simp add: field_simps)
finally show ?thesis .
qed
from poly_decompose[OF rnc] obtain k a s where
kas: "a ≠ 0" "k ≠ 0" "psize s + k + 1 = psize ?r"
"∀z. poly ?r z = poly ?r 0 + z^k* poly (pCons a s) z" by blast
have "∃w. cmod (poly ?r w) < 1"
proof (cases "psize p = k + 1")
case True
with kas(3) lgqr[symmetric] q(1) have s0: "s = 0"
by auto
have hth[symmetric]: "cmod (poly ?r w) = cmod (1 + a * w ^ k)" for w
using kas(4)[rule_format, of w] s0 r01 by (simp add: algebra_simps)
from reduce_poly_simple[OF kas(1,2)] show ?thesis
unfolding hth by blast
next
case False note kn = this
from kn kas(3) q(1) lgqr have k1n: "k + 1 < psize p"
by simp
have th01: "¬ constant (poly (pCons 1 (monom a (k - 1))))"
unfolding constant_def poly_pCons poly_monom
using kas(1)
apply simp
apply (rule exI[where x=0])
apply (rule exI[where x=1])
apply simp
done
from kas(1) kas(2) have th02: "k + 1 = psize (pCons 1 (monom a (k - 1)))"
from less(1) [OF k1n [simplified th02] th01]
obtain w where w: "1 + w^k * a = 0"
unfolding poly_pCons poly_monom
using kas(2) by (cases k) (auto simp add: algebra_simps)
from poly_bound_exists[of "cmod w" s] obtain m where
m: "m > 0" "∀z. cmod z ≤ cmod w ⟶ cmod (poly s z) ≤ m" by blast
have w0: "w ≠ 0"
using kas(2) w by (auto simp add: power_0_left)
from w have "(1 + w ^ k * a) - 1 = 0 - 1"
by simp
then have wm1: "w^k * a = - 1"
by simp
have inv0: "0 < inverse (cmod w ^ (k + 1) * m)"
using norm_ge_zero[of w] w0 m(1)
with field_lbound_gt_zero[OF zero_less_one] obtain t where
t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
let ?ct = "complex_of_real t"
let ?w = "?ct * w"
have "1 + ?w^k * (a + ?w * poly s ?w) = 1 + ?ct^k * (w^k * a) + ?w^k * ?w * poly s ?w"
using kas(1) by (simp add: algebra_simps power_mult_distrib)
also have "… = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
unfolding wm1 by simp
finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) =
cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)"
by metis
with norm_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"]
have th11: "cmod (1 + ?w^k * (a + ?w * poly s ?w)) ≤ ¦1 - t^k¦ + cmod (?w^k * ?w * poly s ?w)"
unfolding norm_of_real by simp
have ath: "⋀x t::real. 0 ≤ x ⟹ x < t ⟹ t ≤ 1 ⟹ ¦1 - t¦ + x < 1"
by arith
have "t * cmod w ≤ 1 * cmod w"
apply (rule mult_mono)
using t(1,2)
apply auto
done
then have tw: "cmod ?w ≤ cmod w"
using t(1) by (simp add: norm_mult)
from t inv0 have "t * (cmod w ^ (k + 1) * m) < 1"
with zero_less_power[OF t(1), of k] have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1"
by simp
have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k + 1) * cmod (poly s ?w)))"
using w0 t(1)
by (simp add: algebra_simps power_mult_distrib norm_power norm_mult)
then have "cmod (?w^k * ?w * poly s ?w) ≤ t^k * (t* (cmod w ^ (k + 1) * m))"
using t(1,2) m(2)[rule_format, OF tw] w0
by auto
with th30 have th120: "cmod (?w^k * ?w * poly s ?w) < t^k"
by simp
from power_strict_mono[OF t(2), of k] t(1) kas(2) have th121: "t^k ≤ 1"
by auto
from ath[OF norm_ge_zero[of "?w^k * ?w * poly s ?w"] th120 th121]
have th12: "¦1 - t^k¦ + cmod (?w^k * ?w * poly s ?w) < 1" .
from th11 th12 have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) < 1"
by arith
then have "cmod (poly ?r ?w) < 1"
unfolding kas(4)[rule_format, of ?w] r01 by simp
then show ?thesis
by blast
qed
with cq0 q(2) show ?thesis
unfolding mrmq_eq not_less[symmetric] by auto
qed
qed

text ‹Alternative version with a syntactic notion of constant polynomial.›

lemma fundamental_theorem_of_algebra_alt:
assumes nc: "¬ (∃a l. a ≠ 0 ∧ l = 0 ∧ p = pCons a l)"
shows "∃z. poly p z = (0::complex)"
using nc
proof (induct p)
case 0
then show ?case by simp
next
case (pCons c cs)
show ?case
proof (cases "c = 0")
case True
then show ?thesis by auto
next
case False
have "¬ constant (poly (pCons c cs))"
proof
assume nc: "constant (poly (pCons c cs))"
from nc[unfolded constant_def, rule_format, of 0]
have "∀w. w ≠ 0 ⟶ poly cs w = 0" by auto
then have "cs = 0"
proof (induct cs)
case 0
then show ?case by simp
next
case (pCons d ds)
show ?case
proof (cases "d = 0")
case True
then show ?thesis
using pCons.prems pCons.hyps by simp
next
case False
from poly_bound_exists[of 1 ds] obtain m where
m: "m > 0" "∀z. ∀z. cmod z ≤ 1 ⟶ cmod (poly ds z) ≤ m" by blast
have dm: "cmod d / m > 0"
using False m(1) by (simp add: field_simps)
from field_lbound_gt_zero[OF dm zero_less_one]
obtain x where x: "x > 0" "x < cmod d / m" "x < 1"
by blast
let ?x = "complex_of_real x"
from x have cx: "?x ≠ 0" "cmod ?x ≤ 1"
by simp_all
from pCons.prems[rule_format, OF cx(1)]
have cth: "cmod (?x*poly ds ?x) = cmod d"
from m(2)[rule_format, OF cx(2)] x(1)
have th0: "cmod (?x*poly ds ?x) ≤ x*m"
from x(2) m(1) have "x * m < cmod d"
with th0 have "cmod (?x*poly ds ?x) ≠ cmod d"
by auto
with cth show ?thesis
by blast
qed
qed
then show False
using pCons.prems False by blast
qed
then show ?thesis
by (rule fundamental_theorem_of_algebra)
qed
qed

subsection ‹Nullstellensatz, degrees and divisibility of polynomials›

lemma nullstellensatz_lemma:
fixes p :: "complex poly"
assumes "∀x. poly p x = 0 ⟶ poly q x = 0"
and "degree p = n"
and "n ≠ 0"
shows "p dvd (q ^ n)"
using assms
proof (induct n arbitrary: p q rule: nat_less_induct)
fix n :: nat
fix p q :: "complex poly"
assume IH: "∀m<n. ∀p q.
(∀x. poly p x = (0::complex) ⟶ poly q x = 0) ⟶
degree p = m ⟶ m ≠ 0 ⟶ p dvd (q ^ m)"
and pq0: "∀x. poly p x = 0 ⟶ poly q x = 0"
and dpn: "degree p = n"
and n0: "n ≠ 0"
from dpn n0 have pne: "p ≠ 0" by auto
show "p dvd (q ^ n)"
proof (cases "∃a. poly p a = 0")
case True
then obtain a where a: "poly p a = 0" ..
have ?thesis if oa: "order a p ≠ 0"
proof -
let ?op = "order a p"
from pne have ap: "([:- a, 1:] ^ ?op) dvd p" "¬ [:- a, 1:] ^ (Suc ?op) dvd p"
using order by blast+
note oop = order_degree[OF pne, unfolded dpn]
show ?thesis
proof (cases "q = 0")
case True
with n0 show ?thesis by (simp add: power_0_left)
next
case False
from pq0[rule_format, OF a, unfolded poly_eq_0_iff_dvd]
obtain r where r: "q = [:- a, 1:] * r" by (rule dvdE)
from ap(1) obtain s where s: "p = [:- a, 1:] ^ ?op * s"
by (rule dvdE)
have sne: "s ≠ 0"
using s pne by auto
show ?thesis
proof (cases "degree s = 0")
case True
then obtain k where kpn: "s = [:k:]"
by (cases s) (auto split: if_splits)
from sne kpn have k: "k ≠ 0" by simp
let ?w = "([:1/k:] * ([:-a,1:] ^ (n - ?op))) * (r ^ n)"
have "q ^ n = p * ?w"
apply (subst r)
apply (subst s)
apply (subst kpn)
using k oop [of a]
apply (subst power_mult_distrib)
apply simp
apply simp
done
then show ?thesis
unfolding dvd_def by blast
next
case False
with sne dpn s oa have dsn: "degree s < n"
apply auto
apply (erule ssubst)
done
have "poly r x = 0" if h: "poly s x = 0" for x
proof -
have xa: "x ≠ a"
proof
assume "x = a"
from h[unfolded this poly_eq_0_iff_dvd] obtain u where u: "s = [:- a, 1:] * u"
by (rule dvdE)
have "p = [:- a, 1:] ^ (Suc ?op) * u"
apply (subst s)
apply (subst u)
apply (simp only: power_Suc ac_simps)
done
with ap(2)[unfolded dvd_def] show False
by blast
qed
from h have "poly p x = 0"
by (subst s) simp
with pq0 have "poly q x = 0"
by blast
with r xa show ?thesis
by auto
qed
with IH[rule_format, OF dsn, of s r] False have "s dvd (r ^ (degree s))"
by blast
then obtain u where u: "r ^ (degree s) = s * u" ..
then have u': "⋀x. poly s x * poly u x = poly r x ^ degree s"
by (simp only: poly_mult[symmetric] poly_power[symmetric])
let ?w = "(u * ([:-a,1:] ^ (n - ?op))) * (r ^ (n - degree s))"
from oop[of a] dsn have "q ^ n = p * ?w"
apply -
apply (subst s)
apply (subst r)
apply (simp only: power_mult_distrib)
apply (subst mult.assoc [where b=s])
apply (subst mult.assoc [where a=u])
apply (subst mult.assoc [where b=u, symmetric])
apply (subst u [symmetric])
done
then show ?thesis
unfolding dvd_def by blast
qed
qed
qed
then show ?thesis
using a order_root pne by blast
next
case False
with fundamental_theorem_of_algebra_alt[of p]
obtain c where ccs: "c ≠ 0" "p = pCons c 0"
by blast
then have pp: "poly p x = c" for x
by simp
let ?w = "[:1/c:] * (q ^ n)"
from ccs have "(q ^ n) = (p * ?w)"
by simp
then show ?thesis
unfolding dvd_def by blast
qed
qed

lemma nullstellensatz_univariate:
"(∀x. poly p x = (0::complex) ⟶ poly q x = 0) ⟷
p dvd (q ^ (degree p)) ∨ (p = 0 ∧ q = 0)"
proof -
consider "p = 0" | "p ≠ 0" "degree p = 0" | n where "p ≠ 0" "degree p = Suc n"
by (cases "degree p") auto
then show ?thesis
proof cases
case p: 1
then have eq: "(∀x. poly p x = (0::complex) ⟶ poly q x = 0) ⟷ q = 0"
{
assume "p dvd (q ^ (degree p))"
then obtain r where r: "q ^ (degree p) = p * r" ..
from r p have False by simp
}
with eq p show ?thesis by blast
next
case dp: 2
then obtain k where k: "p = [:k:]" "k ≠ 0"
by (cases p) (simp split: if_splits)
then have th1: "∀x. poly p x ≠ 0"
by simp
from k dp(2) have "q ^ (degree p) = p * [:1/k:]"
by simp
then have th2: "p dvd (q ^ (degree p))" ..
from dp(1) th1 th2 show ?thesis
by blast
next
case dp: 3
have False if dvd: "p dvd (q ^ (Suc n))" and h: "poly p x = 0" "poly q x ≠ 0" for x
proof -
from dvd obtain u where u: "q ^ (Suc n) = p * u" ..
from h have "poly (q ^ (Suc n)) x ≠ 0"
by simp
with u h(1) show ?thesis
by (simp only: poly_mult) simp
qed
with dp nullstellensatz_lemma[of p q "degree p"] show ?thesis
by auto
qed
qed

text ‹Useful lemma›
lemma constant_degree:
fixes p :: "'a::{idom,ring_char_0} poly"
shows "constant (poly p) ⟷ degree p = 0" (is "?lhs = ?rhs")
proof
show ?rhs if ?lhs
proof -
from that[unfolded constant_def, rule_format, of _ "0"]
have th: "poly p = poly [:poly p 0:]"
by auto
then have "p = [:poly p 0:]"
then have "degree p = degree [:poly p 0:]"
by simp
then show ?thesis
by simp
qed
show ?lhs if ?rhs
proof -
from that obtain k where "p = [:k:]"
by (cases p) (simp split: if_splits)
then show ?thesis
unfolding constant_def by auto
qed
qed

text ‹Arithmetic operations on multivariate polynomials.›

lemma mpoly_base_conv:
fixes x :: "'a::comm_ring_1"
shows "0 = poly 0 x" "c = poly [:c:] x" "x = poly [:0,1:] x"
by simp_all

lemma mpoly_norm_conv:
fixes x :: "'a::comm_ring_1"
shows "poly [:0:] x = poly 0 x" "poly [:poly 0 y:] x = poly 0 x"
by simp_all

lemma mpoly_sub_conv:
fixes x :: "'a::comm_ring_1"
shows "poly p x - poly q x = poly p x + -1 * poly q x"
by simp

lemma poly_pad_rule: "poly p x = 0 ⟹ poly (pCons 0 p) x = 0"
by simp

lemma poly_cancel_eq_conv:
fixes x :: "'a::field"
shows "x = 0 ⟹ a ≠ 0 ⟹ y = 0 ⟷ a * y - b * x = 0"
by auto

fixes p:: "('a::comm_ring_1) poly"
assumes pq: "p dvd q"
shows "p dvd (pCons 0 q)"
proof -
have "pCons 0 q = q * [:0,1:]" by simp
then have "q dvd (pCons 0 q)" ..
with pq show ?thesis by (rule dvd_trans)
qed

lemma poly_divides_conv0:
fixes p:: "'a::field poly"
assumes lgpq: "degree q < degree p"
and lq: "p ≠ 0"
shows "p dvd q ⟷ q = 0" (is "?lhs ⟷ ?rhs")
proof
assume ?rhs
then have "q = p * 0" by simp
then show ?lhs ..
next
assume l: ?lhs
show ?rhs
proof (cases "q = 0")
case True
then show ?thesis by simp
next
assume q0: "q ≠ 0"
from l q0 have "degree p ≤ degree q"
by (rule dvd_imp_degree_le)
with lgpq show ?thesis by simp
qed
qed

lemma poly_divides_conv1:
fixes p :: "'a::field poly"
assumes a0: "a ≠ 0"
and pp': "p dvd p'"
and qrp': "smult a q - p' = r"
shows "p dvd q ⟷ p dvd r" (is "?lhs ⟷ ?rhs")
proof
from pp' obtain t where t: "p' = p * t" ..
show ?rhs if ?lhs
proof -
from that obtain u where u: "q = p * u" ..
have "r = p * (smult a u - t)"
using u qrp' [symmetric] t by (simp add: algebra_simps)
then show ?thesis ..
qed
show ?lhs if ?rhs
proof -
from that obtain u where u: "r = p * u" ..
from u [symmetric] t qrp' [symmetric] a0
have "q = p * smult (1/a) (u + t)"
then show ?thesis ..
qed
qed

lemma basic_cqe_conv1:
"(∃x. poly p x = 0 ∧ poly 0 x ≠ 0) ⟷ False"
"(∃x. poly 0 x ≠ 0) ⟷ False"
"(∃x. poly [:c:] x ≠ 0) ⟷ c ≠ 0"
"(∃x. poly 0 x = 0) ⟷ True"
"(∃x. poly [:c:] x = 0) ⟷ c = 0"
by simp_all

lemma basic_cqe_conv2:
assumes l: "p ≠ 0"
shows "∃x. poly (pCons a (pCons b p)) x = (0::complex)"
proof -
have False if "h ≠ 0" "t = 0" and "pCons a (pCons b p) = pCons h t" for h t
using l that by simp
then have th: "¬ (∃ h t. h ≠ 0 ∧ t = 0 ∧ pCons a (pCons b p) = pCons h t)"
by blast
from fundamental_theorem_of_algebra_alt[OF th] show ?thesis
by auto
qed

lemma  basic_cqe_conv_2b: "(∃x. poly p x ≠ (0::complex)) ⟷ p ≠ 0"
by (metis poly_all_0_iff_0)

lemma basic_cqe_conv3:
fixes p q :: "complex poly"
assumes l: "p ≠ 0"
shows "(∃x. poly (pCons a p) x = 0 ∧ poly q x ≠ 0) ⟷ ¬ (pCons a p) dvd (q ^ psize p)"
proof -
from l have dp: "degree (pCons a p) = psize p"
from nullstellensatz_univariate[of "pCons a p" q] l
show ?thesis
by (metis dp pCons_eq_0_iff)
qed

lemma basic_cqe_conv4:
fixes p q :: "complex poly"
assumes h: "⋀x. poly (q ^ n) x = poly r x"
shows "p dvd (q ^ n) ⟷ p dvd r"
proof -
from h have "poly (q ^ n) = poly r"
by auto
then have "(q ^ n) = r"