Theory HOL-Computational_Algebra.Fundamental_Theorem_Algebra

(*  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"
  by (metis add_diff_cancel norm_triangle_ineq4)


subsection ‹Basic lemmas about polynomials›

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 "  ?k"
      using mult_mono[OF H th rp norm_ge_zero[of "poly cs z"]]
      by (simp add: norm_mult)
    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"
  by (simp add: offset_poly_def)

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 [simp]: "offset_poly [:a:] h = [:a:]"
  by (simp add: offset_poly_pCons offset_poly_0)

lemma poly_offset_poly: "poly (offset_poly p h) x = poly p (h + x)"
  by (induct p) (auto simp add: offset_poly_0 offset_poly_pCons algebra_simps)

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 [simp]: "offset_poly p h = 0  p = 0"
proof
  show "offset_poly p h = 0  p = 0"
  proof(induction p)
    case 0
    then show ?case by blast
  next
    case (pCons a p)
    then show ?case   
      by (metis offset_poly_eq_0_lemma offset_poly_pCons offset_poly_single)
  qed
qed (simp add: offset_poly_0)

lemma degree_offset_poly [simp]: "degree (offset_poly p h) = degree p"
proof(induction p)
  case 0
  then show ?case
    by (simp add: offset_poly_0)
next
  case (pCons a p)
  have "p  0  degree (offset_poly (pCons a p) h) = Suc (degree p)"
    by (metis degree_add_eq_right degree_pCons_eq degree_smult_le le_imp_less_Suc offset_poly_eq_0_iff offset_poly_pCons pCons.IH)
  then show ?case
    by simp
qed

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))"
  by (metis degree_offset_poly offset_poly_eq_0_iff poly_offset_poly psize_def)

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: "x2 + y2 = 1"
    by (simp add: cmod_def)
  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  12" "¦2 * y¦2  12"
      by (metis abs_square_le_1 one_power2 power2_abs)+
    with xy * show ?thesis
      by (smt (verit, best) four_x_squared square_le_1)
  qed
  then show ?thesis
    by force
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 obtain m where m: "n = 2 * m" and "m  0" "m < n"
      using n by auto
    with IH obtain z where z: "?P z m"
      by blast
    from z have "?P (csqrt z) n"
      by (simp add: m power_mult)
    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 0: "cmod (complex_of_real (cmod b) / b) = 1"
      using b by (simp add: norm_divide)
    have "v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
    proof (cases "cmod (complex_of_real (cmod b) / b + 1) < 1")
      case True
      then show ?thesis
        by (metis power_one)
    next
      case F1: False
      show ?thesis
      proof (cases "cmod (complex_of_real (cmod b) / b - 1) < 1")
        case True
        with odd n show ?thesis
          by (metis add_uminus_conv_diff neg_one_odd_power)
      next
        case F2: False
        show ?thesis
        proof (cases "cmod (complex_of_real (cmod b) / b + 𝗂) < 1")
          case T1: True
          show ?thesis
          proof (cases "even m")
            case True
            with T1 show ?thesis
              by (rule_tac x="𝗂" in exI) (simp add: m power_mult)
          next
            case False
            with T1 show ?thesis 
              by (rule_tac x="- 𝗂" in exI) (simp add: m power_mult)
          qed
        next
          case False
          then have lt1: "cmod (of_real (cmod b) / b - 𝗂) < 1"
            using "0" F1 F2 unimodular_reduce_norm by blast
          show ?thesis
          proof (cases "even m")
            case True
            with m lt1 show ?thesis 
              by (rule_tac x="- 𝗂" in exI) (simp add: power_mult)
          next
            case False
            with m lt1 show ?thesis 
              by (rule_tac x="𝗂" in exI) (simp add: power_mult)
          qed
        qed
      qed
    qed
    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 1: "?w ^ n = v^n / complex_of_real (cmod b)"
      by (simp add: power_divide of_real_power[symmetric])
    have 2:"cmod (complex_of_real (cmod b) / b) = 1"
      using b by (simp add: norm_divide)
    then have 3: "cmod (complex_of_real (cmod b) / b)  0"
      by simp
    have 4: "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
      apply (simp add: 2)
      done
    show ?thesis
      by (metis 1 mult_left_less_imp_less[OF 4 3])
  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"
  have "r  0"
    by (meson norm_ge_zero order_trans r)
  have "n. r + 1  ¦Re (s n)¦"
    by (smt (verit, ccfv_threshold) abs_Re_le_cmod r)
  then have conv1: "convergent (λn. Re (s (f n)))"
    by (metis Bseq_monoseq_convergent f(2) BseqI' real_norm_def)
  have "n. r + 1  ¦Im (s n)¦"
    by (smt (verit) abs_Im_le_cmod r)
  then have conv2: "convergent (λn. Im (s (f (g n))))"
    by (metis Bseq_monoseq_convergent g(2) BseqI' real_norm_def)

  obtain x where  x: "r>0. n0. nn0. ¦Re (s (f n)) - x¦ < r"
    using conv1[unfolded convergent_def] LIMSEQ_iff real_norm_def by metis 
  obtain y where  y: "r>0. n0. nn0. ¦Im (s (f (g n))) - y¦ < r"
    using conv2[unfolded convergent_def] LIMSEQ_iff real_norm_def by metis
  let ?w = "Complex x y"
  from f(1) g(1) have hs: "strict_mono ?h"
    unfolding strict_mono_def by auto
  have "N. nN. cmod (s (?h n) - ?w) < e" if "e > 0" for e
  proof -
    from that have e2: "e/2 > 0"
      by simp
    from x y e2
    obtain N1 N2 where N1: "nN1. ¦Re (s (f n)) - x¦ < e / 2"
      and N2: "nN2. ¦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+
      show ?thesis
        using metric_bound_lemma[of "s (f (g n))" ?w] N1 N2 nN1 nN2 by fastforce
    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 "degree q = degree p" and q: "w. poly p w = poly q (w - z)"
    by (metis add.commute degree_offset_poly diff_add_cancel poly_offset_poly)
  show ?thesis unfolding q
  proof (induct q)
    case 0
    then show ?case
      using ep by auto
  next
    case (pCons c cs)
    obtain m where m: "m > 0" "norm z  1  norm (poly cs z)  m" for z
      using poly_bound_exists[of 1 "cs"] by blast
    with ep have em0: "e/m > 0"
      by (simp add: field_simps)
    obtain d where d: "d > 0" "d < 1" "d < e / m"
      by (meson em0 field_lbound_gt_zero zero_less_one)
    then have "w. norm (w - z) < d  norm (w - z) * norm (poly cs (w - z)) < e"
      by (smt (verit, del_insts) m mult_left_mono norm_ge_zero pos_less_divide_eq)
    with d show ?case
      by (force simp add: norm_mult)
  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 mth1: "x z. cmod z  r  cmod (poly p z) = - x"
      by (metis add.inverse_inverse norm_zero)
    obtain s where s: "y. (x. (z. cmod z  r  cmod (poly p z) = - x)  y < x)  y < s"
      by (smt (verit, del_insts) real_sup_exists[OF mth1] norm_zero zero_less_norm_iff)

    let ?m = "- s"
    have s1: "(z. cmod z  r  - (- cmod (poly p z)) < y)  ?m < y" for y
      by (metis add.inverse_inverse minus_less_iff s)
    then have s1m: "z. cmod z  r  cmod (poly p z)  ?m"
      by force
    have "z. cmod z  r  cmod (poly p z) < - s + 1 / real (Suc n)" for n
      using s1[of "?m + 1/real (Suc n)"] by simp
    then obtain g where g: "n. cmod (g n)  r" "n. cmod (poly p (g n)) <?m + 1 /real(Suc n)"
      by metis
    from Bolzano_Weierstrass_complex_disc[OF g(1)]
    obtain f::"nat  nat" and z where fz: "strict_mono f" "e>0. N. nN. 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
        with poly_cont obtain d 
          where "d > 0" and d: "w. 0<cmod (w - z) cmod(w - z) < d  cmod(poly p w - poly p z) < ?e/2"
          by blast
        have 1: "cmod(poly p w - poly p z) < ?e / 2" if w: "cmod (w - z) < d" for w
          using d[of w] w e by (cases "w = z") simp_all
        from fz(2) d > 0 obtain N1 where N1: "nN1. cmod (g (f n) - z) < d"
          by blast
        from reals_Archimedean2 obtain N2 :: nat where N2: "2/?e < real N2"
          by blast
        have 2: "cmod (poly p (g (f (N1 + N2))) - poly p z) < ?e/2"
          using N1 1 by auto
        have 0: "a < e2  ¦b - m¦ < e2  2 * e2  ¦b - m¦ + a  False"
          for a b e2 m :: real
          by arith
        from seq_suble[OF fz(1), of "N1 + N2"]
        have 00: "?m + 1 / real (Suc (f (N1 + N2)))  ?m + 1 / real (Suc (N1 + N2))"
          by (simp add: frac_le)
        from N2 e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
        have "?e/2 > 1/ real (Suc (N1 + N2))"
          by (simp add: inverse_eq_divide)
        with  order_less_le_trans[OF _ 00]
        have 1: "¦cmod (poly p (g (f (N1 + N2)))) - ?m¦ < ?e/2"
          using g s1 by (smt (verit))
        with 0[OF 2] have False
          by (smt (verit) field_sum_of_halves norm_triangle_ineq3)
      }
      then have "?e = 0"
        by auto
      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 "d  norm(z * poly (pCons c cs) z) - norm a"
        by (smt (verit, best) norm_ge_zero mult_less_cancel_right2 norm_mult r that)
      with norm_diff_ineq add.commute
      show ?thesis
        by (metis order.trans poly_pCons)
    qed
    then show ?thesis by blast
  next
    case True
    have "d  norm (poly (pCons a (pCons c cs)) z)"
      if "(¦d¦ + norm a) / norm c  norm z" for z :: 'a
    proof -
      have "¦d¦ + norm a  norm (z * c)"
        by (metis that True norm_mult pCons.hyps(1) pos_divide_le_eq zero_less_norm_iff)
      also have "  norm (a + z * c) + norm a"
        by (simp add: add.commute norm_add_leD)
      finally show ?thesis
        using True by auto
    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
    from poly_minimum_modulus_disc[of "¦r¦" "pCons c cs"] show ?thesis
      by (smt (verit, del_insts) order.trans linorder_linear r)
  qed (use pCons.hyps in auto)
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"
  by (simp add: poly_monom)

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
  qed force
qed

lemma poly_decompose:
  fixes p :: "'a::idom poly"
  assumes nc: "¬ constant (poly p)"
  shows "k a q. a  0  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
    by (simp add: constant_def)
next
  case (pCons c cs)
  have "¬ (z. z  0  poly cs z = 0)"
    by (smt (verit) constant_def mult_eq_0_iff pCons.prems poly_pCons)
  from poly_decompose_lemma[OF this]
  obtain k a q where *: "a  0 
     Suc (psize q + k) = psize cs  (z. poly cs z = z ^ k * poly (pCons a q) z)"
    by blast
  then have "psize q + k + 2 = psize (pCons c cs)"
    by (auto simp add: psize_def split: if_splits)
  then show ?case
    using "*" by force
qed

text ‹Fundamental theorem of algebra›

theorem 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
    obtain q where q: "psize q = psize p" "x. poly q x = ?p (c + x)"
      using poly_offset[of p c] by blast
    then have qnc: "¬ constant (poly q)"
      by (metis (no_types, opaque_lifting) add.commute constant_def diff_add_cancel less.prems)
    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"
      by (simp add: a00 psize_def)
    have rnc: "¬ constant (poly ?r)"
      using constant_def qnc qr by fastforce 
    have r01: "poly ?r 0 = 1"
      by (simp add: a00)
    have mrmq_eq: "cmod (poly ?r w) < 1  cmod (poly q w) < cmod ?a0" for w
      by (smt (verit, del_insts) a00 mult_less_cancel_right2 norm_mult qr zero_less_norm_iff)
    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 q have s0: "s = 0"
        by (simp add: lgqr)
      with reduce_poly_simple kas show ?thesis
        by (metis mult.commute mult.right_neutral poly_1 poly_smult r01 smult_one)
    next
      case False note kn = this
      from kn kas(3) q(1) lgqr have k1n: "k + 1 < psize p"
        by simp
      have 01: "¬ constant (poly (pCons 1 (monom a (k - 1))))"
        unfolding constant_def poly_pCons poly_monom
        by (metis add_cancel_left_right kas(1) mult.commute mult_cancel_right2 power_one)
      have 02: "k + 1 = psize (pCons 1 (monom a (k - 1)))"
        using kas by (simp add: psize_def degree_monom_eq)
      from less(1) [OF _ 01] k1n 02
      obtain w where w: "1 + w^k * a = 0"
        by (metis kas(2) mult.commute mult.left_commute poly_monom poly_pCons power_eq_if)
      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 "w  0"
        using kas(2) w by (auto simp add: power_0_left)
      from w have wm1: "w^k * a = - 1"
        by (simp add: add_eq_0_iff)
      have inv0: "0 < inverse (cmod w ^ (k + 1) * m)"
        by (simp add: w  0 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 11: "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 tw: "cmod ?w  cmod w"
        by (smt (verit) mult_le_cancel_right2 norm_ge_zero norm_mult norm_of_real t)
      have "t * (cmod w ^ (k + 1) * m) < 1"
        by (smt (verit, best) inv0 inverse_positive_iff_positive left_inverse mult_strict_right_mono t(3))
      with zero_less_power[OF t(1), of k] have 30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k"
        by simp
      have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k + 1) * cmod (poly s ?w)))"
        using w  0 t(1) by (simp add: algebra_simps norm_power norm_mult)
      with 30 have 120: "cmod (?w^k * ?w * poly s ?w) < t^k"
        by (smt (verit, ccfv_SIG) m(2) mult_left_mono norm_ge_zero t(1) tw zero_le_power)
      from power_strict_mono[OF t(2), of k] t(1) kas(2) have 121: "t^k  1"
        by auto
      from ath[OF norm_ge_zero[of "?w^k * ?w * poly s ?w"] 120 121]
      show ?thesis
        by (smt (verit) "11" kas(4) poly_pCons r01)
    qed
    with cq0 q(2) show ?thesis
      by (smt (verit) mrmq_eq)
  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)"
proof (rule ccontr)
  assume N: "z. poly p z = 0"
  then have "¬ constant (poly p)"
    unfolding constant_def
    by (metis (no_types, opaque_lifting) nc poly_pcompose pcompose_0' pcompose_const poly_0_coeff_0 
        poly_all_0_iff_0 poly_diff right_minus_eq)
  then show False
    using N fundamental_theorem_of_algebra by blast
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 = [:- a, 1:] ^ n * r ^ n"
            using power_mult_distrib r by blast
          also have "... = [:- a, 1:] ^ order a p * [:k:] * ([:1 / k:] * [:- a, 1:] ^ (n - order a p) * r ^ n)"
            using k oop [of a] by (simp flip: power_add)
          also have "... = p * ?w"
            by (metis s kpn)
          finally show ?thesis
            unfolding dvd_def by blast
        next
          case False
          with sne dpn s oa have dsn: "degree s < n"
            by (metis add_diff_cancel_right' degree_0 degree_linear_power degree_mult_eq gr0I zero_less_diff)
          have "poly r x = 0" if h: "poly s x = 0" for x
          proof -
            have "x  a"
              by (metis ap(2) dvd_refl mult_dvd_mono poly_eq_0_iff_dvd power_Suc power_commutes s that)
            moreover have "poly p x = 0"
              by (metis (no_types) mult_eq_0_iff poly_mult s that)
            ultimately show ?thesis
              using pq0 r by auto
          qed
          with False IH dsn obtain u where u: "r ^ (degree s) = s * u"
            by blast
          then have u': "x. poly s x * poly u x = poly r x ^ degree s"
            by (simp only: poly_mult[symmetric] poly_power[symmetric])
          have "q^n = [:- a, 1:] ^ n * r ^ n"
            using power_mult_distrib r by blast
          also have "... = [:- a, 1:] ^ order a p * (s * u * ([:- a, 1:] ^ (n - order a p) * r ^ (n - degree s)))"
            by (smt (verit, del_insts) s u mult_ac power_add add_diff_cancel_right' degree_linear_power degree_mult_eq dpn mult_zero_left)
          also have "... = p * (u * ([:-a,1:] ^ (n - ?op))) * (r ^ (n - degree s))"
            using s by force
          finally show ?thesis
            unfolding dvd_def by auto
        qed
      qed
    qed
    then show ?thesis
      using a order_root pne by blast
  next
    case False
    then show ?thesis
      using dpn n0 fundamental_theorem_of_algebra_alt[of p]
      by fastforce
  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 "(x. poly p x = (0::complex)  poly q x = 0)  q = 0"
      by (auto simp add: poly_all_0_iff_0)
    with p show ?thesis
      by force
  next
    case dp: 2
    then show ?thesis
      by (meson dvd_trans is_unit_iff_degree poly_eq_0_iff_dvd unit_imp_dvd)
  next
    case dp: 3
    have False if "p dvd (q ^ (Suc n))" "poly p x = 0" "poly q x  0" for x
      by (metis dvd_trans poly_eq_0_iff_dvd poly_power power_eq_0_iff that)
    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 "poly p = poly [:poly p 0:]"
      by auto
    then show ?thesis
      by (metis degree_pCons_0 poly_eq_poly_eq_iff)
  qed
  show ?lhs if ?rhs
    unfolding constant_def
    by (metis degree_eq_zeroE pcompose_const poly_0 poly_pcompose that)
qed

lemma complex_poly_decompose:
  "smult (lead_coeff p) (z|poly p z = 0. [:-z, 1:] ^ order z p) = (p :: complex poly)"
proof (induction p rule: poly_root_order_induct)
  case (no_roots p)
  show ?case
  proof (cases "degree p = 0")
    case False
    hence "¬constant (poly p)" by (subst constant_degree)
    with fundamental_theorem_of_algebra and no_roots show ?thesis by blast
  qed (auto elim!: degree_eq_zeroE)
next
  case (root p x n)
  from root have *: "{z. poly ([:- x, 1:] ^ n * p) z = 0} = insert x {z. poly p z = 0}"
    by auto
  have "smult (lead_coeff ([:-x, 1:] ^ n * p)) 
           (z|poly ([:-x,1:] ^ n * p) z = 0. [:-z, 1:] ^ order z ([:- x, 1:] ^ n * p)) = 
        [:- x, 1:] ^ order x ([:- x, 1:] ^ n * p) * 
          smult (lead_coeff p) (z{z. poly p z = 0}. [:- z, 1:] ^ order z ([:- x, 1:] ^ n * p))"
    by (subst *, subst prod.insert) 
       (insert root, auto intro: poly_roots_finite simp: mult_ac lead_coeff_mult lead_coeff_power)
  also have "order x ([:- x, 1:] ^ n * p) = n"
    using root by (subst order_mult) (auto simp: order_power_n_n order_0I)
  also have "(z{z. poly p z = 0}. [:- z, 1:] ^ order z ([:- x, 1:] ^ n * p)) =
               (z{z. poly p z = 0}. [:- z, 1:] ^ order z p)"
  proof (intro prod.cong refl, goal_cases)
    case (1 y)
    with root have "order y ([:-x,1:] ^ n) = 0" by (intro order_0I) auto 
    thus ?case using root by (subst order_mult) auto
  qed
  also note root.IH
  finally show ?case .
qed simp_all

instance complex :: alg_closed_field
  by standard (use fundamental_theorem_of_algebra constant_degree neq0_conv in blast)

lemma size_proots_complex: "size (proots (p :: complex poly)) = degree p"
proof (cases "p = 0")
  case [simp]: False
  show "size (proots p) = degree p"
    by (subst (1 2) complex_poly_decompose [symmetric])
       (simp add: proots_prod proots_power degree_prod_sum_eq degree_power_eq)
qed auto

lemma complex_poly_decompose_multiset:
  "smult (lead_coeff p) (x∈#proots p. [:-x, 1:]) = (p :: complex poly)"
proof (cases "p = 0")
  case False
  hence "(x∈#proots p. [:-x, 1:]) = (x | poly p x = 0. [:-x, 1:] ^ order x p)"
    by (subst image_prod_mset_multiplicity) simp_all
  also have "smult (lead_coeff p)  = p"
    by (rule complex_poly_decompose)
  finally show ?thesis .
qed auto

lemma complex_poly_decompose':
  obtains root where "smult (lead_coeff p) (i<degree p. [:-root i, 1:]) = (p :: complex poly)"
proof -
  obtain roots where roots: "mset roots = proots p"
    using ex_mset by blast

  have "p = smult (lead_coeff p) (x∈#proots p. [:-x, 1:])"
    by (rule complex_poly_decompose_multiset [symmetric])
  also have "(x∈#proots p. [:-x, 1:]) = (xroots. [:-x, 1:])"
    by (subst prod_mset_prod_list [symmetric]) (simp add: roots)
  also have " = (i<length roots. [:-roots ! i, 1:])"
    by (subst prod.list_conv_set_nth) (auto simp: atLeast0LessThan)
  finally have eq: "p = smult (lead_coeff p) (i<length roots. [:-roots ! i, 1:])" .
  also have [simp]: "degree p = length roots"
    using roots by (subst eq) (auto simp: degree_prod_sum_eq)
  finally show ?thesis by (intro that[of "λi. roots ! i"]) auto
qed

lemma complex_poly_decompose_rsquarefree:
  assumes "rsquarefree p"
  shows   "smult (lead_coeff p) (z|poly p z = 0. [:-z, 1:]) = (p :: complex poly)"
proof (cases "p = 0")
  case False
  have "(z|poly p z = 0. [:-z, 1:]) = (z|poly p z = 0. [:-z, 1:] ^ order z p)"
    using assms False by (intro prod.cong) (auto simp: rsquarefree_root_order)
  also have "smult (lead_coeff p)  = p"
    by (rule complex_poly_decompose)
  finally show ?thesis .
qed auto


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

lemma poly_divides_pad_rule:
  fixes p:: "('a::comm_ring_1) poly"
  assumes pq: "p dvd q"
  shows "p dvd (pCons 0 q)"
  by (metis add_0 dvd_def mult_pCons_right pq smult_0_left)

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"
  using lgpq mod_poly_less by fastforce

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"
  by (metis a0 diff_add_cancel dvd_add_left_iff dvd_smult_iff pp' qrp')

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)"
  by (meson fundamental_theorem_of_algebra_alt l pCons_eq_0_iff pCons_eq_iff)

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)"
  by (metis degree_pCons_eq_if l nullstellensatz_univariate pCons_eq_0_iff psize_def)

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"
  by (metis (no_types) basic_cqe_conv_2b h poly_diff right_minus_eq)

lemma poly_const_conv:
  fixes x :: "'a::comm_ring_1"
  shows "poly [:c:] x = y  c = y"
  by simp

end