src/HOL/Library/Fundamental_Theorem_Algebra.thy
author wenzelm
Sat Jun 13 19:53:53 2015 +0200 (2015-06-13)
changeset 60457 f31f7599ef55
parent 60449 229bad93377e
child 60557 5854821993d2
permissions -rw-r--r--
tuned proofs;
     1 (* Author: Amine Chaieb, TU Muenchen *)
     2 
     3 section \<open>Fundamental Theorem of Algebra\<close>
     4 
     5 theory Fundamental_Theorem_Algebra
     6 imports Polynomial Complex_Main
     7 begin
     8 
     9 subsection \<open>More lemmas about module of complex numbers\<close>
    10 
    11 text \<open>The triangle inequality for cmod\<close>
    12 
    13 lemma complex_mod_triangle_sub: "cmod w \<le> cmod (w + z) + norm z"
    14   using complex_mod_triangle_ineq2[of "w + z" "-z"] by auto
    15 
    16 
    17 subsection \<open>Basic lemmas about polynomials\<close>
    18 
    19 lemma poly_bound_exists:
    20   fixes p :: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
    21   shows "\<exists>m. m > 0 \<and> (\<forall>z. norm z \<le> r \<longrightarrow> norm (poly p z) \<le> m)"
    22 proof (induct p)
    23   case 0
    24   then show ?case by (rule exI[where x=1]) simp
    25 next
    26   case (pCons c cs)
    27   from pCons.hyps obtain m where m: "\<forall>z. norm z \<le> r \<longrightarrow> norm (poly cs z) \<le> m"
    28     by blast
    29   let ?k = " 1 + norm c + \<bar>r * m\<bar>"
    30   have kp: "?k > 0"
    31     using abs_ge_zero[of "r*m"] norm_ge_zero[of c] by arith
    32   have "norm (poly (pCons c cs) z) \<le> ?k" if H: "norm z \<le> r" for z
    33   proof -
    34     from m H have th: "norm (poly cs z) \<le> m"
    35       by blast
    36     from H have rp: "r \<ge> 0"
    37       using norm_ge_zero[of z] by arith
    38     have "norm (poly (pCons c cs) z) \<le> norm c + norm (z * poly cs z)"
    39       using norm_triangle_ineq[of c "z* poly cs z"] by simp
    40     also have "\<dots> \<le> norm c + r * m"
    41       using mult_mono[OF H th rp norm_ge_zero[of "poly cs z"]]
    42       by (simp add: norm_mult)
    43     also have "\<dots> \<le> ?k"
    44       by simp
    45     finally show ?thesis .
    46   qed
    47   with kp show ?case by blast
    48 qed
    49 
    50 
    51 text \<open>Offsetting the variable in a polynomial gives another of same degree\<close>
    52 
    53 definition offset_poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
    54   where "offset_poly p h = fold_coeffs (\<lambda>a q. smult h q + pCons a q) p 0"
    55 
    56 lemma offset_poly_0: "offset_poly 0 h = 0"
    57   by (simp add: offset_poly_def)
    58 
    59 lemma offset_poly_pCons:
    60   "offset_poly (pCons a p) h =
    61     smult h (offset_poly p h) + pCons a (offset_poly p h)"
    62   by (cases "p = 0 \<and> a = 0") (auto simp add: offset_poly_def)
    63 
    64 lemma offset_poly_single: "offset_poly [:a:] h = [:a:]"
    65   by (simp add: offset_poly_pCons offset_poly_0)
    66 
    67 lemma poly_offset_poly: "poly (offset_poly p h) x = poly p (h + x)"
    68   apply (induct p)
    69   apply (simp add: offset_poly_0)
    70   apply (simp add: offset_poly_pCons algebra_simps)
    71   done
    72 
    73 lemma offset_poly_eq_0_lemma: "smult c p + pCons a p = 0 \<Longrightarrow> p = 0"
    74   by (induct p arbitrary: a) (simp, force)
    75 
    76 lemma offset_poly_eq_0_iff: "offset_poly p h = 0 \<longleftrightarrow> p = 0"
    77   apply (safe intro!: offset_poly_0)
    78   apply (induct p)
    79   apply simp
    80   apply (simp add: offset_poly_pCons)
    81   apply (frule offset_poly_eq_0_lemma, simp)
    82   done
    83 
    84 lemma degree_offset_poly: "degree (offset_poly p h) = degree p"
    85   apply (induct p)
    86   apply (simp add: offset_poly_0)
    87   apply (case_tac "p = 0")
    88   apply (simp add: offset_poly_0 offset_poly_pCons)
    89   apply (simp add: offset_poly_pCons)
    90   apply (subst degree_add_eq_right)
    91   apply (rule le_less_trans [OF degree_smult_le])
    92   apply (simp add: offset_poly_eq_0_iff)
    93   apply (simp add: offset_poly_eq_0_iff)
    94   done
    95 
    96 definition "psize p = (if p = 0 then 0 else Suc (degree p))"
    97 
    98 lemma psize_eq_0_iff [simp]: "psize p = 0 \<longleftrightarrow> p = 0"
    99   unfolding psize_def by simp
   100 
   101 lemma poly_offset:
   102   fixes p :: "'a::comm_ring_1 poly"
   103   shows "\<exists>q. psize q = psize p \<and> (\<forall>x. poly q x = poly p (a + x))"
   104 proof (intro exI conjI)
   105   show "psize (offset_poly p a) = psize p"
   106     unfolding psize_def
   107     by (simp add: offset_poly_eq_0_iff degree_offset_poly)
   108   show "\<forall>x. poly (offset_poly p a) x = poly p (a + x)"
   109     by (simp add: poly_offset_poly)
   110 qed
   111 
   112 text \<open>An alternative useful formulation of completeness of the reals\<close>
   113 lemma real_sup_exists:
   114   assumes ex: "\<exists>x. P x"
   115     and bz: "\<exists>z. \<forall>x. P x \<longrightarrow> x < z"
   116   shows "\<exists>s::real. \<forall>y. (\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < s"
   117 proof
   118   from bz have "bdd_above (Collect P)"
   119     by (force intro: less_imp_le)
   120   then show "\<forall>y. (\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < Sup (Collect P)"
   121     using ex bz by (subst less_cSup_iff) auto
   122 qed
   123 
   124 
   125 subsection \<open>Fundamental theorem of algebra\<close>
   126 
   127 lemma unimodular_reduce_norm:
   128   assumes md: "cmod z = 1"
   129   shows "cmod (z + 1) < 1 \<or> cmod (z - 1) < 1 \<or> cmod (z + ii) < 1 \<or> cmod (z - ii) < 1"
   130 proof -
   131   obtain x y where z: "z = Complex x y "
   132     by (cases z) auto
   133   from md z have xy: "x\<^sup>2 + y\<^sup>2 = 1"
   134     by (simp add: cmod_def)
   135   {
   136     assume C: "cmod (z + 1) \<ge> 1" "cmod (z - 1) \<ge> 1" "cmod (z + ii) \<ge> 1" "cmod (z - ii) \<ge> 1"
   137     from C z xy have "2 * x \<le> 1" "2 * x \<ge> -1" "2 * y \<le> 1" "2 * y \<ge> -1"
   138       by (simp_all add: cmod_def power2_eq_square algebra_simps)
   139     then have "abs (2 * x) \<le> 1" "abs (2 * y) \<le> 1"
   140       by simp_all
   141     then have "(abs (2 * x))\<^sup>2 \<le> 1\<^sup>2" "(abs (2 * y))\<^sup>2 \<le> 1\<^sup>2"
   142       by - (rule power_mono, simp, simp)+
   143     then have th0: "4 * x\<^sup>2 \<le> 1" "4 * y\<^sup>2 \<le> 1"
   144       by (simp_all add: power_mult_distrib)
   145     from add_mono[OF th0] xy have False by simp
   146   }
   147   then show ?thesis
   148     unfolding linorder_not_le[symmetric] by blast
   149 qed
   150 
   151 text \<open>Hence we can always reduce modulus of @{text "1 + b z^n"} if nonzero\<close>
   152 lemma reduce_poly_simple:
   153   assumes b: "b \<noteq> 0"
   154     and n: "n \<noteq> 0"
   155   shows "\<exists>z. cmod (1 + b * z^n) < 1"
   156   using n
   157 proof (induct n rule: nat_less_induct)
   158   fix n
   159   assume IH: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>z. cmod (1 + b * z ^ m) < 1)"
   160   assume n: "n \<noteq> 0"
   161   let ?P = "\<lambda>z n. cmod (1 + b * z ^ n) < 1"
   162   show "\<exists>z. ?P z n"
   163   proof cases
   164     assume "even n"
   165     then have "\<exists>m. n = 2 * m"
   166       by presburger
   167     then obtain m where m: "n = 2 * m"
   168       by blast
   169     from n m have "m \<noteq> 0" "m < n"
   170       by presburger+
   171     with IH[rule_format, of m] obtain z where z: "?P z m"
   172       by blast
   173     from z have "?P (csqrt z) n"
   174       by (simp add: m power_mult)
   175     then show ?thesis ..
   176   next
   177     assume "odd n"
   178     then have "\<exists>m. n = Suc (2 * m)"
   179       by presburger+
   180     then obtain m where m: "n = Suc (2 * m)"
   181       by blast
   182     have th0: "cmod (complex_of_real (cmod b) / b) = 1"
   183       using b by (simp add: norm_divide)
   184     from unimodular_reduce_norm[OF th0] \<open>odd n\<close>
   185     have "\<exists>v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
   186       apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1")
   187       apply (rule_tac x="1" in exI)
   188       apply simp
   189       apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1")
   190       apply (rule_tac x="-1" in exI)
   191       apply simp
   192       apply (cases "cmod (complex_of_real (cmod b) / b + ii) < 1")
   193       apply (cases "even m")
   194       apply (rule_tac x="ii" in exI)
   195       apply (simp add: m power_mult)
   196       apply (rule_tac x="- ii" in exI)
   197       apply (simp add: m power_mult)
   198       apply (cases "even m")
   199       apply (rule_tac x="- ii" in exI)
   200       apply (simp add: m power_mult)
   201       apply (auto simp add: m power_mult)
   202       apply (rule_tac x="ii" in exI)
   203       apply (auto simp add: m power_mult)
   204       done
   205     then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1"
   206       by blast
   207     let ?w = "v / complex_of_real (root n (cmod b))"
   208     from odd_real_root_pow[OF \<open>odd n\<close>, of "cmod b"]
   209     have th1: "?w ^ n = v^n / complex_of_real (cmod b)"
   210       by (simp add: power_divide of_real_power[symmetric])
   211     have th2:"cmod (complex_of_real (cmod b) / b) = 1"
   212       using b by (simp add: norm_divide)
   213     then have th3: "cmod (complex_of_real (cmod b) / b) \<ge> 0"
   214       by simp
   215     have th4: "cmod (complex_of_real (cmod b) / b) *
   216         cmod (1 + b * (v ^ n / complex_of_real (cmod b))) <
   217         cmod (complex_of_real (cmod b) / b) * 1"
   218       apply (simp only: norm_mult[symmetric] distrib_left)
   219       using b v
   220       apply (simp add: th2)
   221       done
   222     from mult_left_less_imp_less[OF th4 th3]
   223     have "?P ?w n" unfolding th1 .
   224     then show ?thesis ..
   225   qed
   226 qed
   227 
   228 text \<open>Bolzano-Weierstrass type property for closed disc in complex plane.\<close>
   229 
   230 lemma metric_bound_lemma: "cmod (x - y) \<le> \<bar>Re x - Re y\<bar> + \<bar>Im x - Im y\<bar>"
   231   using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y"]
   232   unfolding cmod_def by simp
   233 
   234 lemma bolzano_weierstrass_complex_disc:
   235   assumes r: "\<forall>n. cmod (s n) \<le> r"
   236   shows "\<exists>f z. subseq f \<and> (\<forall>e >0. \<exists>N. \<forall>n \<ge> N. cmod (s (f n) - z) < e)"
   237 proof -
   238   from seq_monosub[of "Re \<circ> s"]
   239   obtain f where f: "subseq f" "monoseq (\<lambda>n. Re (s (f n)))"
   240     unfolding o_def by blast
   241   from seq_monosub[of "Im \<circ> s \<circ> f"]
   242   obtain g where g: "subseq g" "monoseq (\<lambda>n. Im (s (f (g n))))"
   243     unfolding o_def by blast
   244   let ?h = "f \<circ> g"
   245   from r[rule_format, of 0] have rp: "r \<ge> 0"
   246     using norm_ge_zero[of "s 0"] by arith
   247   have th: "\<forall>n. r + 1 \<ge> \<bar>Re (s n)\<bar>"
   248   proof
   249     fix n
   250     from abs_Re_le_cmod[of "s n"] r[rule_format, of n]
   251     show "\<bar>Re (s n)\<bar> \<le> r + 1" by arith
   252   qed
   253   have conv1: "convergent (\<lambda>n. Re (s (f n)))"
   254     apply (rule Bseq_monoseq_convergent)
   255     apply (simp add: Bseq_def)
   256     apply (metis gt_ex le_less_linear less_trans order.trans th)
   257     apply (rule f(2))
   258     done
   259   have th: "\<forall>n. r + 1 \<ge> \<bar>Im (s n)\<bar>"
   260   proof
   261     fix n
   262     from abs_Im_le_cmod[of "s n"] r[rule_format, of n]
   263     show "\<bar>Im (s n)\<bar> \<le> r + 1"
   264       by arith
   265   qed
   266 
   267   have conv2: "convergent (\<lambda>n. Im (s (f (g n))))"
   268     apply (rule Bseq_monoseq_convergent)
   269     apply (simp add: Bseq_def)
   270     apply (metis gt_ex le_less_linear less_trans order.trans th)
   271     apply (rule g(2))
   272     done
   273 
   274   from conv1[unfolded convergent_def] obtain x where "LIMSEQ (\<lambda>n. Re (s (f n))) x"
   275     by blast
   276   then have x: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar>Re (s (f n)) - x\<bar> < r"
   277     unfolding LIMSEQ_iff real_norm_def .
   278 
   279   from conv2[unfolded convergent_def] obtain y where "LIMSEQ (\<lambda>n. Im (s (f (g n)))) y"
   280     by blast
   281   then have y: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar>Im (s (f (g n))) - y\<bar> < r"
   282     unfolding LIMSEQ_iff real_norm_def .
   283   let ?w = "Complex x y"
   284   from f(1) g(1) have hs: "subseq ?h"
   285     unfolding subseq_def by auto
   286   {
   287     fix e :: real
   288     assume ep: "e > 0"
   289     then have e2: "e/2 > 0"
   290       by simp
   291     from x[rule_format, OF e2] y[rule_format, OF e2]
   292     obtain N1 N2 where N1: "\<forall>n\<ge>N1. \<bar>Re (s (f n)) - x\<bar> < e / 2"
   293       and N2: "\<forall>n\<ge>N2. \<bar>Im (s (f (g n))) - y\<bar> < e / 2"
   294       by blast
   295     {
   296       fix n
   297       assume nN12: "n \<ge> N1 + N2"
   298       then have nN1: "g n \<ge> N1" and nN2: "n \<ge> N2"
   299         using seq_suble[OF g(1), of n] by arith+
   300       from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
   301       have "cmod (s (?h n) - ?w) < e"
   302         using metric_bound_lemma[of "s (f (g n))" ?w] by simp
   303     }
   304     then have "\<exists>N. \<forall>n\<ge>N. cmod (s (?h n) - ?w) < e"
   305       by blast
   306   }
   307   with hs show ?thesis by blast
   308 qed
   309 
   310 text \<open>Polynomial is continuous.\<close>
   311 
   312 lemma poly_cont:
   313   fixes p :: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
   314   assumes ep: "e > 0"
   315   shows "\<exists>d >0. \<forall>w. 0 < norm (w - z) \<and> norm (w - z) < d \<longrightarrow> norm (poly p w - poly p z) < e"
   316 proof -
   317   obtain q where q: "degree q = degree p" "\<And>x. poly q x = poly p (z + x)"
   318   proof
   319     show "degree (offset_poly p z) = degree p"
   320       by (rule degree_offset_poly)
   321     show "\<And>x. poly (offset_poly p z) x = poly p (z + x)"
   322       by (rule poly_offset_poly)
   323   qed
   324   have th: "\<And>w. poly q (w - z) = poly p w"
   325     using q(2)[of "w - z" for w] by simp
   326   show ?thesis unfolding th[symmetric]
   327   proof (induct q)
   328     case 0
   329     then show ?case
   330       using ep by auto
   331   next
   332     case (pCons c cs)
   333     from poly_bound_exists[of 1 "cs"]
   334     obtain m where m: "m > 0" "\<And>z. norm z \<le> 1 \<Longrightarrow> norm (poly cs z) \<le> m"
   335       by blast
   336     from ep m(1) have em0: "e/m > 0"
   337       by (simp add: field_simps)
   338     have one0: "1 > (0::real)"
   339       by arith
   340     from real_lbound_gt_zero[OF one0 em0]
   341     obtain d where d: "d > 0" "d < 1" "d < e / m"
   342       by blast
   343     from d(1,3) m(1) have dm: "d * m > 0" "d * m < e"
   344       by (simp_all add: field_simps)
   345     show ?case
   346     proof (rule ex_forward[OF real_lbound_gt_zero[OF one0 em0]], clarsimp simp add: norm_mult)
   347       fix d w
   348       assume H: "d > 0" "d < 1" "d < e/m" "w \<noteq> z" "norm (w - z) < d"
   349       then have d1: "norm (w-z) \<le> 1" "d \<ge> 0"
   350         by simp_all
   351       from H(3) m(1) have dme: "d*m < e"
   352         by (simp add: field_simps)
   353       from H have th: "norm (w - z) \<le> d"
   354         by simp
   355       from mult_mono[OF th m(2)[OF d1(1)] d1(2) norm_ge_zero] dme
   356       show "norm (w - z) * norm (poly cs (w - z)) < e"
   357         by simp
   358     qed
   359   qed
   360 qed
   361 
   362 text \<open>Hence a polynomial attains minimum on a closed disc
   363   in the complex plane.\<close>
   364 lemma poly_minimum_modulus_disc: "\<exists>z. \<forall>w. cmod w \<le> r \<longrightarrow> cmod (poly p z) \<le> cmod (poly p w)"
   365 proof -
   366   show ?thesis
   367   proof (cases "r \<ge> 0")
   368     case False
   369     then show ?thesis
   370       by (metis norm_ge_zero order.trans)
   371   next
   372     case True
   373     then have "cmod 0 \<le> r \<and> cmod (poly p 0) = - (- cmod (poly p 0))"
   374       by simp
   375     then have mth1: "\<exists>x z. cmod z \<le> r \<and> cmod (poly p z) = - x"
   376       by blast
   377     {
   378       fix x z
   379       assume H: "cmod z \<le> r" "cmod (poly p z) = - x" "\<not> x < 1"
   380       then have "- x < 0 "
   381         by arith
   382       with H(2) norm_ge_zero[of "poly p z"] have False
   383         by simp
   384     }
   385     then have mth2: "\<exists>z. \<forall>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<longrightarrow> x < z"
   386       by blast
   387     from real_sup_exists[OF mth1 mth2] obtain s where
   388       s: "\<forall>y. (\<exists>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<and> y < x) \<longleftrightarrow> y < s" by blast
   389     let ?m = "- s"
   390     {
   391       fix y
   392       from s[rule_format, of "-y"]
   393       have "(\<exists>z x. cmod z \<le> r \<and> - (- cmod (poly p z)) < y) \<longleftrightarrow> ?m < y"
   394         unfolding minus_less_iff[of y ] equation_minus_iff by blast
   395     }
   396     note s1 = this[unfolded minus_minus]
   397     from s1[of ?m] have s1m: "\<And>z x. cmod z \<le> r \<Longrightarrow> cmod (poly p z) \<ge> ?m"
   398       by auto
   399     {
   400       fix n :: nat
   401       from s1[rule_format, of "?m + 1/real (Suc n)"]
   402       have "\<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)"
   403         by simp
   404     }
   405     then have th: "\<forall>n. \<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)" ..
   406     from choice[OF th] obtain g where
   407         g: "\<forall>n. cmod (g n) \<le> r" "\<forall>n. cmod (poly p (g n)) <?m + 1 /real(Suc n)"
   408       by blast
   409     from bolzano_weierstrass_complex_disc[OF g(1)]
   410     obtain f z where fz: "subseq f" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. cmod (g (f n) - z) < e"
   411       by blast
   412     {
   413       fix w
   414       assume wr: "cmod w \<le> r"
   415       let ?e = "\<bar>cmod (poly p z) - ?m\<bar>"
   416       {
   417         assume e: "?e > 0"
   418         then have e2: "?e/2 > 0"
   419           by simp
   420         from poly_cont[OF e2, of z p] obtain d where
   421             d: "d > 0" "\<forall>w. 0<cmod (w - z)\<and> cmod(w - z) < d \<longrightarrow> cmod(poly p w - poly p z) < ?e/2"
   422           by blast
   423         {
   424           fix w
   425           assume w: "cmod (w - z) < d"
   426           have "cmod(poly p w - poly p z) < ?e / 2"
   427             using d(2)[rule_format, of w] w e by (cases "w = z") simp_all
   428         }
   429         note th1 = this
   430 
   431         from fz(2) d(1) obtain N1 where N1: "\<forall>n\<ge>N1. cmod (g (f n) - z) < d"
   432           by blast
   433         from reals_Archimedean2[of "2/?e"] obtain N2 :: nat where N2: "2/?e < real N2"
   434           by blast
   435         have th2: "cmod (poly p (g (f (N1 + N2))) - poly p z) < ?e/2"
   436           using N1[rule_format, of "N1 + N2"] th1 by simp
   437         have th0: "a < e2 \<Longrightarrow> \<bar>b - m\<bar> < e2 \<Longrightarrow> 2 * e2 \<le> \<bar>b - m\<bar> + a \<Longrightarrow> False"
   438           for a b e2 m :: real
   439           by arith
   440         have ath: "m \<le> x \<Longrightarrow> x < m + e \<Longrightarrow> \<bar>x - m\<bar> < e" for m x e :: real
   441           by arith
   442         from s1m[OF g(1)[rule_format]] have th31: "?m \<le> cmod(poly p (g (f (N1 + N2))))" .
   443         from seq_suble[OF fz(1), of "N1 + N2"]
   444         have th00: "real (Suc (N1 + N2)) \<le> real (Suc (f (N1 + N2)))"
   445           by simp
   446         have th000: "0 \<le> (1::real)" "(1::real) \<le> 1" "real (Suc (N1 + N2)) > 0"
   447           using N2 by auto
   448         from frac_le[OF th000 th00]
   449         have th00: "?m + 1 / real (Suc (f (N1 + N2))) \<le> ?m + 1 / real (Suc (N1 + N2))"
   450           by simp
   451         from g(2)[rule_format, of "f (N1 + N2)"]
   452         have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
   453         from order_less_le_trans[OF th01 th00]
   454         have th32: "cmod (poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
   455         from N2 have "2/?e < real (Suc (N1 + N2))"
   456           by arith
   457         with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
   458         have "?e/2 > 1/ real (Suc (N1 + N2))"
   459           by (simp add: inverse_eq_divide)
   460         with ath[OF th31 th32] have thc1: "\<bar>cmod (poly p (g (f (N1 + N2)))) - ?m\<bar> < ?e/2"
   461           by arith
   462         have ath2: "\<bar>a - b\<bar> \<le> c \<Longrightarrow> \<bar>b - m\<bar> \<le> \<bar>a - m\<bar> + c" for a b c m :: real
   463           by arith
   464         have th22: "\<bar>cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)\<bar> \<le>
   465             cmod (poly p (g (f (N1 + N2))) - poly p z)"
   466           by (simp add: norm_triangle_ineq3)
   467         from ath2[OF th22, of ?m]
   468         have thc2: "2 * (?e/2) \<le>
   469             \<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar> + cmod (poly p (g (f (N1 + N2))) - poly p z)"
   470           by simp
   471         from th0[OF th2 thc1 thc2] have False .
   472       }
   473       then have "?e = 0"
   474         by auto
   475       then have "cmod (poly p z) = ?m"
   476         by simp
   477       with s1m[OF wr] have "cmod (poly p z) \<le> cmod (poly p w)"
   478         by simp
   479     }
   480     then show ?thesis by blast
   481   qed
   482 qed
   483 
   484 text \<open>Nonzero polynomial in z goes to infinity as z does.\<close>
   485 
   486 lemma poly_infinity:
   487   fixes p:: "'a::{comm_semiring_0,real_normed_div_algebra} poly"
   488   assumes ex: "p \<noteq> 0"
   489   shows "\<exists>r. \<forall>z. r \<le> norm z \<longrightarrow> d \<le> norm (poly (pCons a p) z)"
   490   using ex
   491 proof (induct p arbitrary: a d)
   492   case 0
   493   then show ?case by simp
   494 next
   495   case (pCons c cs a d)
   496   show ?case
   497   proof (cases "cs = 0")
   498     case False
   499     with pCons.hyps obtain r where r: "\<forall>z. r \<le> norm z \<longrightarrow> d + norm a \<le> norm (poly (pCons c cs) z)"
   500       by blast
   501     let ?r = "1 + \<bar>r\<bar>"
   502     {
   503       fix z :: 'a
   504       assume h: "1 + \<bar>r\<bar> \<le> norm z"
   505       have r0: "r \<le> norm z"
   506         using h by arith
   507       from r[rule_format, OF r0] have th0: "d + norm a \<le> 1 * norm(poly (pCons c cs) z)"
   508         by arith
   509       from h have z1: "norm z \<ge> 1"
   510         by arith
   511       from order_trans[OF th0 mult_right_mono[OF z1 norm_ge_zero[of "poly (pCons c cs) z"]]]
   512       have th1: "d \<le> norm(z * poly (pCons c cs) z) - norm a"
   513         unfolding norm_mult by (simp add: algebra_simps)
   514       from norm_diff_ineq[of "z * poly (pCons c cs) z" a]
   515       have th2: "norm (z * poly (pCons c cs) z) - norm a \<le> norm (poly (pCons a (pCons c cs)) z)"
   516         by (simp add: algebra_simps)
   517       from th1 th2 have "d \<le> norm (poly (pCons a (pCons c cs)) z)"
   518         by arith
   519     }
   520     then show ?thesis by blast
   521   next
   522     case True
   523     with pCons.prems have c0: "c \<noteq> 0"
   524       by simp
   525     have "d \<le> norm (poly (pCons a (pCons c cs)) z)"
   526       if h: "(\<bar>d\<bar> + norm a) / norm c \<le> norm z" for z :: 'a
   527     proof -
   528       from c0 have "norm c > 0"
   529         by simp
   530       from h c0 have th0: "\<bar>d\<bar> + norm a \<le> norm (z * c)"
   531         by (simp add: field_simps norm_mult)
   532       have ath: "\<And>mzh mazh ma. mzh \<le> mazh + ma \<Longrightarrow> \<bar>d\<bar> + ma \<le> mzh \<Longrightarrow> d \<le> mazh"
   533         by arith
   534       from norm_diff_ineq[of "z * c" a] have th1: "norm (z * c) \<le> norm (a + z * c) + norm a"
   535         by (simp add: algebra_simps)
   536       from ath[OF th1 th0] show ?thesis
   537         using True by simp
   538     qed
   539     then show ?thesis by blast
   540   qed
   541 qed
   542 
   543 text \<open>Hence polynomial's modulus attains its minimum somewhere.\<close>
   544 lemma poly_minimum_modulus: "\<exists>z.\<forall>w. cmod (poly p z) \<le> cmod (poly p w)"
   545 proof (induct p)
   546   case 0
   547   then show ?case by simp
   548 next
   549   case (pCons c cs)
   550   show ?case
   551   proof (cases "cs = 0")
   552     case False
   553     from poly_infinity[OF False, of "cmod (poly (pCons c cs) 0)" c]
   554     obtain r where r: "\<And>z. r \<le> cmod z \<Longrightarrow> cmod (poly (pCons c cs) 0) \<le> cmod (poly (pCons c cs) z)"
   555       by blast
   556     have ath: "\<And>z r. r \<le> cmod z \<or> cmod z \<le> \<bar>r\<bar>"
   557       by arith
   558     from poly_minimum_modulus_disc[of "\<bar>r\<bar>" "pCons c cs"]
   559     obtain v where v: "\<And>w. cmod w \<le> \<bar>r\<bar> \<Longrightarrow> cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) w)"
   560       by blast
   561     have "cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) z)" if z: "r \<le> cmod z" for z
   562       using v[of 0] r[OF z] by simp
   563     with v ath[of r] show ?thesis
   564       by blast
   565   next
   566     case True
   567     with pCons.hyps show ?thesis
   568       by simp
   569   qed
   570 qed
   571 
   572 text \<open>Constant function (non-syntactic characterization).\<close>
   573 definition "constant f \<longleftrightarrow> (\<forall>x y. f x = f y)"
   574 
   575 lemma nonconstant_length: "\<not> constant (poly p) \<Longrightarrow> psize p \<ge> 2"
   576   by (induct p) (auto simp: constant_def psize_def)
   577 
   578 lemma poly_replicate_append: "poly (monom 1 n * p) (x::'a::comm_ring_1) = x^n * poly p x"
   579   by (simp add: poly_monom)
   580 
   581 text \<open>Decomposition of polynomial, skipping zero coefficients after the first.\<close>
   582 
   583 lemma poly_decompose_lemma:
   584   assumes nz: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly p z = (0::'a::idom))"
   585   shows "\<exists>k a q. a \<noteq> 0 \<and> Suc (psize q + k) = psize p \<and> (\<forall>z. poly p z = z^k * poly (pCons a q) z)"
   586   unfolding psize_def
   587   using nz
   588 proof (induct p)
   589   case 0
   590   then show ?case by simp
   591 next
   592   case (pCons c cs)
   593   show ?case
   594   proof (cases "c = 0")
   595     case True
   596     from pCons.hyps pCons.prems True show ?thesis
   597       apply auto
   598       apply (rule_tac x="k+1" in exI)
   599       apply (rule_tac x="a" in exI, clarsimp)
   600       apply (rule_tac x="q" in exI)
   601       apply auto
   602       done
   603   next
   604     case False
   605     show ?thesis
   606       apply (rule exI[where x=0])
   607       apply (rule exI[where x=c])
   608       apply (auto simp: False)
   609       done
   610   qed
   611 qed
   612 
   613 lemma poly_decompose:
   614   assumes nc: "\<not> constant (poly p)"
   615   shows "\<exists>k a q. a \<noteq> (0::'a::idom) \<and> k \<noteq> 0 \<and>
   616                psize q + k + 1 = psize p \<and>
   617               (\<forall>z. poly p z = poly p 0 + z^k * poly (pCons a q) z)"
   618   using nc
   619 proof (induct p)
   620   case 0
   621   then show ?case
   622     by (simp add: constant_def)
   623 next
   624   case (pCons c cs)
   625   {
   626     assume "\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0"
   627     then have "poly (pCons c cs) x = poly (pCons c cs) y" for x y
   628       by (cases "x = 0") auto
   629     with pCons.prems have False
   630       by (auto simp add: constant_def)
   631   }
   632   then have th: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0)" ..
   633   from poly_decompose_lemma[OF th]
   634   show ?case
   635     apply clarsimp
   636     apply (rule_tac x="k+1" in exI)
   637     apply (rule_tac x="a" in exI)
   638     apply simp
   639     apply (rule_tac x="q" in exI)
   640     apply (auto simp add: psize_def split: if_splits)
   641     done
   642 qed
   643 
   644 text \<open>Fundamental theorem of algebra\<close>
   645 
   646 lemma fundamental_theorem_of_algebra:
   647   assumes nc: "\<not> constant (poly p)"
   648   shows "\<exists>z::complex. poly p z = 0"
   649   using nc
   650 proof (induct "psize p" arbitrary: p rule: less_induct)
   651   case less
   652   let ?p = "poly p"
   653   let ?ths = "\<exists>z. ?p z = 0"
   654 
   655   from nonconstant_length[OF less(2)] have n2: "psize p \<ge> 2" .
   656   from poly_minimum_modulus obtain c where c: "\<forall>w. cmod (?p c) \<le> cmod (?p w)"
   657     by blast
   658 
   659   show ?ths
   660   proof (cases "?p c = 0")
   661     case True
   662     then show ?thesis by blast
   663   next
   664     case False
   665     from poly_offset[of p c] obtain q where q: "psize q = psize p" "\<forall>x. poly q x = ?p (c + x)"
   666       by blast
   667     have False if h: "constant (poly q)"
   668     proof -
   669       from q(2) have th: "\<forall>x. poly q (x - c) = ?p x"
   670         by auto
   671       have "?p x = ?p y" for x y
   672       proof -
   673         from th have "?p x = poly q (x - c)"
   674           by auto
   675         also have "\<dots> = poly q (y - c)"
   676           using h unfolding constant_def by blast
   677         also have "\<dots> = ?p y"
   678           using th by auto
   679         finally show ?thesis .
   680       qed
   681       with less(2) show ?thesis
   682         unfolding constant_def by blast
   683     qed
   684     then have qnc: "\<not> constant (poly q)"
   685       by blast
   686     from q(2) have pqc0: "?p c = poly q 0"
   687       by simp
   688     from c pqc0 have cq0: "\<forall>w. cmod (poly q 0) \<le> cmod (?p w)"
   689       by simp
   690     let ?a0 = "poly q 0"
   691     from False pqc0 have a00: "?a0 \<noteq> 0"
   692       by simp
   693     from a00 have qr: "\<forall>z. poly q z = poly (smult (inverse ?a0) q) z * ?a0"
   694       by simp
   695     let ?r = "smult (inverse ?a0) q"
   696     have lgqr: "psize q = psize ?r"
   697       using a00
   698       unfolding psize_def degree_def
   699       by (simp add: poly_eq_iff)
   700     have False if h: "\<And>x y. poly ?r x = poly ?r y"
   701     proof -
   702       {
   703         fix x y
   704         from qr[rule_format, of x] have "poly q x = poly ?r x * ?a0"
   705           by auto
   706         also have "\<dots> = poly ?r y * ?a0"
   707           using h by simp
   708         also have "\<dots> = poly q y"
   709           using qr[rule_format, of y] by simp
   710         finally have "poly q x = poly q y" .
   711       }
   712       with qnc show ?thesis
   713         unfolding constant_def by blast
   714     qed
   715     then have rnc: "\<not> constant (poly ?r)"
   716       unfolding constant_def by blast
   717     from qr[rule_format, of 0] a00 have r01: "poly ?r 0 = 1"
   718       by auto
   719     have mrmq_eq: "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w) < cmod ?a0" for w
   720     proof -
   721       have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w / ?a0) < 1"
   722         using qr[rule_format, of w] a00 by (simp add: divide_inverse ac_simps)
   723       also have "\<dots> \<longleftrightarrow> cmod (poly q w) < cmod ?a0"
   724         using a00 unfolding norm_divide by (simp add: field_simps)
   725       finally show ?thesis .
   726     qed
   727     from poly_decompose[OF rnc] obtain k a s where
   728       kas: "a \<noteq> 0" "k \<noteq> 0" "psize s + k + 1 = psize ?r"
   729         "\<forall>z. poly ?r z = poly ?r 0 + z^k* poly (pCons a s) z" by blast
   730     have "\<exists>w. cmod (poly ?r w) < 1"
   731     proof (cases "psize p = k + 1")
   732       case True
   733       with kas(3) lgqr[symmetric] q(1) have s0: "s = 0"
   734         by auto
   735       have hth[symmetric]: "cmod (poly ?r w) = cmod (1 + a * w ^ k)" for w
   736         using kas(4)[rule_format, of w] s0 r01 by (simp add: algebra_simps)
   737       from reduce_poly_simple[OF kas(1,2)] show ?thesis
   738         unfolding hth by blast
   739     next
   740       case False note kn = this
   741       from kn kas(3) q(1) lgqr have k1n: "k + 1 < psize p"
   742         by simp
   743       have th01: "\<not> constant (poly (pCons 1 (monom a (k - 1))))"
   744         unfolding constant_def poly_pCons poly_monom
   745         using kas(1)
   746         apply simp
   747         apply (rule exI[where x=0])
   748         apply (rule exI[where x=1])
   749         apply simp
   750         done
   751       from kas(1) kas(2) have th02: "k + 1 = psize (pCons 1 (monom a (k - 1)))"
   752         by (simp add: psize_def degree_monom_eq)
   753       from less(1) [OF k1n [simplified th02] th01]
   754       obtain w where w: "1 + w^k * a = 0"
   755         unfolding poly_pCons poly_monom
   756         using kas(2) by (cases k) (auto simp add: algebra_simps)
   757       from poly_bound_exists[of "cmod w" s] obtain m where
   758         m: "m > 0" "\<forall>z. cmod z \<le> cmod w \<longrightarrow> cmod (poly s z) \<le> m" by blast
   759       have w0: "w \<noteq> 0"
   760         using kas(2) w by (auto simp add: power_0_left)
   761       from w have "(1 + w ^ k * a) - 1 = 0 - 1"
   762         by simp
   763       then have wm1: "w^k * a = - 1"
   764         by simp
   765       have inv0: "0 < inverse (cmod w ^ (k + 1) * m)"
   766         using norm_ge_zero[of w] w0 m(1)
   767         by (simp add: inverse_eq_divide zero_less_mult_iff)
   768       with real_lbound_gt_zero[OF zero_less_one] obtain t where
   769         t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
   770       let ?ct = "complex_of_real t"
   771       let ?w = "?ct * w"
   772       have "1 + ?w^k * (a + ?w * poly s ?w) = 1 + ?ct^k * (w^k * a) + ?w^k * ?w * poly s ?w"
   773         using kas(1) by (simp add: algebra_simps power_mult_distrib)
   774       also have "\<dots> = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
   775         unfolding wm1 by simp
   776       finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) =
   777         cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)"
   778         by metis
   779       with norm_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"]
   780       have th11: "cmod (1 + ?w^k * (a + ?w * poly s ?w)) \<le> \<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w)"
   781         unfolding norm_of_real by simp
   782       have ath: "\<And>x t::real. 0 \<le> x \<Longrightarrow> x < t \<Longrightarrow> t \<le> 1 \<Longrightarrow> \<bar>1 - t\<bar> + x < 1"
   783         by arith
   784       have "t * cmod w \<le> 1 * cmod w"
   785         apply (rule mult_mono)
   786         using t(1,2)
   787         apply auto
   788         done
   789       then have tw: "cmod ?w \<le> cmod w"
   790         using t(1) by (simp add: norm_mult)
   791       from t inv0 have "t * (cmod w ^ (k + 1) * m) < 1"
   792         by (simp add: field_simps)
   793       with zero_less_power[OF t(1), of k] have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1"
   794         by simp
   795       have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k + 1) * cmod (poly s ?w)))"
   796         using w0 t(1)
   797         by (simp add: algebra_simps power_mult_distrib norm_power norm_mult)
   798       then have "cmod (?w^k * ?w * poly s ?w) \<le> t^k * (t* (cmod w ^ (k + 1) * m))"
   799         using t(1,2) m(2)[rule_format, OF tw] w0
   800         by auto
   801       with th30 have th120: "cmod (?w^k * ?w * poly s ?w) < t^k"
   802         by simp
   803       from power_strict_mono[OF t(2), of k] t(1) kas(2) have th121: "t^k \<le> 1"
   804         by auto
   805       from ath[OF norm_ge_zero[of "?w^k * ?w * poly s ?w"] th120 th121]
   806       have th12: "\<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w) < 1" .
   807       from th11 th12 have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) < 1"
   808         by arith
   809       then have "cmod (poly ?r ?w) < 1"
   810         unfolding kas(4)[rule_format, of ?w] r01 by simp
   811       then show ?thesis
   812         by blast
   813     qed
   814     with cq0 q(2) show ?thesis
   815       unfolding mrmq_eq not_less[symmetric] by auto
   816   qed
   817 qed
   818 
   819 text \<open>Alternative version with a syntactic notion of constant polynomial.\<close>
   820 
   821 lemma fundamental_theorem_of_algebra_alt:
   822   assumes nc: "\<not> (\<exists>a l. a \<noteq> 0 \<and> l = 0 \<and> p = pCons a l)"
   823   shows "\<exists>z. poly p z = (0::complex)"
   824   using nc
   825 proof (induct p)
   826   case 0
   827   then show ?case by simp
   828 next
   829   case (pCons c cs)
   830   show ?case
   831   proof (cases "c = 0")
   832     case True
   833     then show ?thesis by auto
   834   next
   835     case False
   836     {
   837       assume nc: "constant (poly (pCons c cs))"
   838       from nc[unfolded constant_def, rule_format, of 0]
   839       have "\<forall>w. w \<noteq> 0 \<longrightarrow> poly cs w = 0" by auto
   840       then have "cs = 0"
   841       proof (induct cs)
   842         case 0
   843         then show ?case by simp
   844       next
   845         case (pCons d ds)
   846         show ?case
   847         proof (cases "d = 0")
   848           case True
   849           then show ?thesis
   850             using pCons.prems pCons.hyps by simp
   851         next
   852           case False
   853           from poly_bound_exists[of 1 ds] obtain m where
   854             m: "m > 0" "\<forall>z. \<forall>z. cmod z \<le> 1 \<longrightarrow> cmod (poly ds z) \<le> m" by blast
   855           have dm: "cmod d / m > 0"
   856             using False m(1) by (simp add: field_simps)
   857           from real_lbound_gt_zero[OF dm zero_less_one]
   858           obtain x where x: "x > 0" "x < cmod d / m" "x < 1"
   859             by blast
   860           let ?x = "complex_of_real x"
   861           from x have cx: "?x \<noteq> 0" "cmod ?x \<le> 1"
   862             by simp_all
   863           from pCons.prems[rule_format, OF cx(1)]
   864           have cth: "cmod (?x*poly ds ?x) = cmod d"
   865             by (simp add: eq_diff_eq[symmetric])
   866           from m(2)[rule_format, OF cx(2)] x(1)
   867           have th0: "cmod (?x*poly ds ?x) \<le> x*m"
   868             by (simp add: norm_mult)
   869           from x(2) m(1) have "x * m < cmod d"
   870             by (simp add: field_simps)
   871           with th0 have "cmod (?x*poly ds ?x) \<noteq> cmod d"
   872             by auto
   873           with cth show ?thesis
   874             by blast
   875         qed
   876       qed
   877     }
   878     then have nc: "\<not> constant (poly (pCons c cs))"
   879       using pCons.prems False by blast
   880     from fundamental_theorem_of_algebra[OF nc] show ?thesis .
   881   qed
   882 qed
   883 
   884 
   885 subsection \<open>Nullstellensatz, degrees and divisibility of polynomials\<close>
   886 
   887 lemma nullstellensatz_lemma:
   888   fixes p :: "complex poly"
   889   assumes "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0"
   890     and "degree p = n"
   891     and "n \<noteq> 0"
   892   shows "p dvd (q ^ n)"
   893   using assms
   894 proof (induct n arbitrary: p q rule: nat_less_induct)
   895   fix n :: nat
   896   fix p q :: "complex poly"
   897   assume IH: "\<forall>m<n. \<forall>p q.
   898                  (\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longrightarrow>
   899                  degree p = m \<longrightarrow> m \<noteq> 0 \<longrightarrow> p dvd (q ^ m)"
   900     and pq0: "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0"
   901     and dpn: "degree p = n"
   902     and n0: "n \<noteq> 0"
   903   from dpn n0 have pne: "p \<noteq> 0" by auto
   904   let ?ths = "p dvd (q ^ n)"
   905   {
   906     fix a
   907     assume a: "poly p a = 0"
   908     have ?ths if oa: "order a p \<noteq> 0"
   909     proof -
   910       let ?op = "order a p"
   911       from pne have ap: "([:- a, 1:] ^ ?op) dvd p" "\<not> [:- a, 1:] ^ (Suc ?op) dvd p"
   912         using order by blast+
   913       note oop = order_degree[OF pne, unfolded dpn]
   914       show ?thesis
   915       proof (cases "q = 0")
   916         case True
   917         with n0 show ?thesis by (simp add: power_0_left)
   918       next
   919         case False
   920         from pq0[rule_format, OF a, unfolded poly_eq_0_iff_dvd]
   921         obtain r where r: "q = [:- a, 1:] * r" by (rule dvdE)
   922         from ap(1) obtain s where s: "p = [:- a, 1:] ^ ?op * s"
   923           by (rule dvdE)
   924         have sne: "s \<noteq> 0"
   925           using s pne by auto
   926         show ?thesis
   927         proof (cases "degree s = 0")
   928           case True
   929           then obtain k where kpn: "s = [:k:]"
   930             by (cases s) (auto split: if_splits)
   931           from sne kpn have k: "k \<noteq> 0" by simp
   932           let ?w = "([:1/k:] * ([:-a,1:] ^ (n - ?op))) * (r ^ n)"
   933           have "q ^ n = p * ?w"
   934             apply (subst r)
   935             apply (subst s)
   936             apply (subst kpn)
   937             using k oop [of a]
   938             apply (subst power_mult_distrib)
   939             apply simp
   940             apply (subst power_add [symmetric])
   941             apply simp
   942             done
   943           then show ?thesis
   944             unfolding dvd_def by blast
   945         next
   946           case False
   947           with sne dpn s oa have dsn: "degree s < n"
   948               apply auto
   949               apply (erule ssubst)
   950               apply (simp add: degree_mult_eq degree_linear_power)
   951               done
   952             {
   953               fix x assume h: "poly s x = 0"
   954               {
   955                 assume xa: "x = a"
   956                 from h[unfolded xa poly_eq_0_iff_dvd] obtain u where u: "s = [:- a, 1:] * u"
   957                   by (rule dvdE)
   958                 have "p = [:- a, 1:] ^ (Suc ?op) * u"
   959                   apply (subst s)
   960                   apply (subst u)
   961                   apply (simp only: power_Suc ac_simps)
   962                   done
   963                 with ap(2)[unfolded dvd_def] have False
   964                   by blast
   965               }
   966               note xa = this
   967               from h have "poly p x = 0"
   968                 by (subst s) simp
   969               with pq0 have "poly q x = 0"
   970                 by blast
   971               with r xa have "poly r x = 0"
   972                 by auto
   973             }
   974             note impth = this
   975             from IH[rule_format, OF dsn, of s r] impth False
   976             have "s dvd (r ^ (degree s))"
   977               by blast
   978             then obtain u where u: "r ^ (degree s) = s * u" ..
   979             then have u': "\<And>x. poly s x * poly u x = poly r x ^ degree s"
   980               by (simp only: poly_mult[symmetric] poly_power[symmetric])
   981             let ?w = "(u * ([:-a,1:] ^ (n - ?op))) * (r ^ (n - degree s))"
   982             from oop[of a] dsn have "q ^ n = p * ?w"
   983               apply -
   984               apply (subst s)
   985               apply (subst r)
   986               apply (simp only: power_mult_distrib)
   987               apply (subst mult.assoc [where b=s])
   988               apply (subst mult.assoc [where a=u])
   989               apply (subst mult.assoc [where b=u, symmetric])
   990               apply (subst u [symmetric])
   991               apply (simp add: ac_simps power_add [symmetric])
   992               done
   993             then show ?thesis
   994               unfolding dvd_def by blast
   995         qed
   996       qed
   997     qed
   998     then have ?ths using a order_root pne by blast
   999   }
  1000   moreover
  1001   {
  1002     assume exa: "\<not> (\<exists>a. poly p a = 0)"
  1003     from fundamental_theorem_of_algebra_alt[of p] exa
  1004     obtain c where ccs: "c \<noteq> 0" "p = pCons c 0"
  1005       by blast
  1006     then have pp: "\<And>x. poly p x = c"
  1007       by simp
  1008     let ?w = "[:1/c:] * (q ^ n)"
  1009     from ccs have "(q ^ n) = (p * ?w)"
  1010       by simp
  1011     then have ?ths
  1012       unfolding dvd_def by blast
  1013   }
  1014   ultimately show ?ths by blast
  1015 qed
  1016 
  1017 lemma nullstellensatz_univariate:
  1018   "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow>
  1019     p dvd (q ^ (degree p)) \<or> (p = 0 \<and> q = 0)"
  1020 proof -
  1021   consider "p = 0" | "p \<noteq> 0" "degree p = 0" | n where "p \<noteq> 0" "degree p = Suc n"
  1022     by (cases "degree p") auto
  1023   then show ?thesis
  1024   proof cases
  1025     case 1
  1026     then have eq: "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> q = 0"
  1027       by (auto simp add: poly_all_0_iff_0)
  1028     {
  1029       assume "p dvd (q ^ (degree p))"
  1030       then obtain r where r: "q ^ (degree p) = p * r" ..
  1031       from r 1 have False by simp
  1032     }
  1033     with eq 1 show ?thesis by blast
  1034   next
  1035     case 2
  1036     then obtain k where k: "p = [:k:]" "k \<noteq> 0"
  1037       by (cases p) (simp split: if_splits)
  1038     then have th1: "\<forall>x. poly p x \<noteq> 0"
  1039       by simp
  1040     from k 2(2) have "q ^ (degree p) = p * [:1/k:]"
  1041       by (simp add: one_poly_def)
  1042     then have th2: "p dvd (q ^ (degree p))" ..
  1043     from 2(1) th1 th2 show ?thesis
  1044       by blast
  1045   next
  1046     case 3
  1047     {
  1048       assume "p dvd (q ^ (Suc n))"
  1049       then obtain u where u: "q ^ (Suc n) = p * u" ..
  1050       fix x
  1051       assume h: "poly p x = 0" "poly q x \<noteq> 0"
  1052       then have "poly (q ^ (Suc n)) x \<noteq> 0"
  1053         by simp
  1054       then have False using u h(1)
  1055         by (simp only: poly_mult) simp
  1056     }
  1057     with 3 nullstellensatz_lemma[of p q "degree p"]
  1058     show ?thesis by auto
  1059   qed
  1060 qed
  1061 
  1062 text \<open>Useful lemma\<close>
  1063 
  1064 lemma constant_degree:
  1065   fixes p :: "'a::{idom,ring_char_0} poly"
  1066   shows "constant (poly p) \<longleftrightarrow> degree p = 0" (is "?lhs = ?rhs")
  1067 proof
  1068   assume l: ?lhs
  1069   from l[unfolded constant_def, rule_format, of _ "0"]
  1070   have th: "poly p = poly [:poly p 0:]"
  1071     by auto
  1072   then have "p = [:poly p 0:]"
  1073     by (simp add: poly_eq_poly_eq_iff)
  1074   then have "degree p = degree [:poly p 0:]"
  1075     by simp
  1076   then show ?rhs
  1077     by simp
  1078 next
  1079   assume r: ?rhs
  1080   then obtain k where "p = [:k:]"
  1081     by (cases p) (simp split: if_splits)
  1082   then show ?lhs
  1083     unfolding constant_def by auto
  1084 qed
  1085 
  1086 lemma divides_degree:
  1087   assumes pq: "p dvd (q:: complex poly)"
  1088   shows "degree p \<le> degree q \<or> q = 0"
  1089   by (metis dvd_imp_degree_le pq)
  1090 
  1091 text \<open>Arithmetic operations on multivariate polynomials.\<close>
  1092 
  1093 lemma mpoly_base_conv:
  1094   fixes x :: "'a::comm_ring_1"
  1095   shows "0 = poly 0 x" "c = poly [:c:] x" "x = poly [:0,1:] x"
  1096   by simp_all
  1097 
  1098 lemma mpoly_norm_conv:
  1099   fixes x :: "'a::comm_ring_1"
  1100   shows "poly [:0:] x = poly 0 x" "poly [:poly 0 y:] x = poly 0 x"
  1101   by simp_all
  1102 
  1103 lemma mpoly_sub_conv:
  1104   fixes x :: "'a::comm_ring_1"
  1105   shows "poly p x - poly q x = poly p x + -1 * poly q x"
  1106   by simp
  1107 
  1108 lemma poly_pad_rule: "poly p x = 0 \<Longrightarrow> poly (pCons 0 p) x = 0"
  1109   by simp
  1110 
  1111 lemma poly_cancel_eq_conv:
  1112   fixes x :: "'a::field"
  1113   shows "x = 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> y = 0 \<longleftrightarrow> a * y - b * x = 0"
  1114   by auto
  1115 
  1116 lemma poly_divides_pad_rule:
  1117   fixes p:: "('a::comm_ring_1) poly"
  1118   assumes pq: "p dvd q"
  1119   shows "p dvd (pCons 0 q)"
  1120 proof -
  1121   have "pCons 0 q = q * [:0,1:]" by simp
  1122   then have "q dvd (pCons 0 q)" ..
  1123   with pq show ?thesis by (rule dvd_trans)
  1124 qed
  1125 
  1126 lemma poly_divides_conv0:
  1127   fixes p:: "'a::field poly"
  1128   assumes lgpq: "degree q < degree p"
  1129     and lq: "p \<noteq> 0"
  1130   shows "p dvd q \<longleftrightarrow> q = 0" (is "?lhs \<longleftrightarrow> ?rhs")
  1131 proof
  1132   assume r: ?rhs
  1133   then have "q = p * 0" by simp
  1134   then show ?lhs ..
  1135 next
  1136   assume l: ?lhs
  1137   show ?rhs
  1138   proof (cases "q = 0")
  1139     case True
  1140     then show ?thesis by simp
  1141   next
  1142     assume q0: "q \<noteq> 0"
  1143     from l q0 have "degree p \<le> degree q"
  1144       by (rule dvd_imp_degree_le)
  1145     with lgpq show ?thesis by simp
  1146   qed
  1147 qed
  1148 
  1149 lemma poly_divides_conv1:
  1150   fixes p :: "'a::field poly"
  1151   assumes a0: "a \<noteq> 0"
  1152     and pp': "p dvd p'"
  1153     and qrp': "smult a q - p' = r"
  1154   shows "p dvd q \<longleftrightarrow> p dvd r" (is "?lhs \<longleftrightarrow> ?rhs")
  1155 proof
  1156   from pp' obtain t where t: "p' = p * t" ..
  1157   {
  1158     assume l: ?lhs
  1159     then obtain u where u: "q = p * u" ..
  1160     have "r = p * (smult a u - t)"
  1161       using u qrp' [symmetric] t by (simp add: algebra_simps)
  1162     then show ?rhs ..
  1163   next
  1164     assume r: ?rhs
  1165     then obtain u where u: "r = p * u" ..
  1166     from u [symmetric] t qrp' [symmetric] a0
  1167     have "q = p * smult (1/a) (u + t)" by (simp add: algebra_simps)
  1168     then show ?lhs ..
  1169   }
  1170 qed
  1171 
  1172 lemma basic_cqe_conv1:
  1173   "(\<exists>x. poly p x = 0 \<and> poly 0 x \<noteq> 0) \<longleftrightarrow> False"
  1174   "(\<exists>x. poly 0 x \<noteq> 0) \<longleftrightarrow> False"
  1175   "(\<exists>x. poly [:c:] x \<noteq> 0) \<longleftrightarrow> c \<noteq> 0"
  1176   "(\<exists>x. poly 0 x = 0) \<longleftrightarrow> True"
  1177   "(\<exists>x. poly [:c:] x = 0) \<longleftrightarrow> c = 0"
  1178   by simp_all
  1179 
  1180 lemma basic_cqe_conv2:
  1181   assumes l: "p \<noteq> 0"
  1182   shows "\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)"
  1183 proof -
  1184   have False if "h \<noteq> 0" "t = 0" and "pCons a (pCons b p) = pCons h t" for h t
  1185     using l that by simp
  1186   then have th: "\<not> (\<exists> h t. h \<noteq> 0 \<and> t = 0 \<and> pCons a (pCons b p) = pCons h t)"
  1187     by blast
  1188   from fundamental_theorem_of_algebra_alt[OF th] show ?thesis
  1189     by auto
  1190 qed
  1191 
  1192 lemma  basic_cqe_conv_2b: "(\<exists>x. poly p x \<noteq> (0::complex)) \<longleftrightarrow> p \<noteq> 0"
  1193   by (metis poly_all_0_iff_0)
  1194 
  1195 lemma basic_cqe_conv3:
  1196   fixes p q :: "complex poly"
  1197   assumes l: "p \<noteq> 0"
  1198   shows "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<longleftrightarrow> \<not> (pCons a p) dvd (q ^ psize p)"
  1199 proof -
  1200   from l have dp: "degree (pCons a p) = psize p"
  1201     by (simp add: psize_def)
  1202   from nullstellensatz_univariate[of "pCons a p" q] l
  1203   show ?thesis
  1204     by (metis dp pCons_eq_0_iff)
  1205 qed
  1206 
  1207 lemma basic_cqe_conv4:
  1208   fixes p q :: "complex poly"
  1209   assumes h: "\<And>x. poly (q ^ n) x = poly r x"
  1210   shows "p dvd (q ^ n) \<longleftrightarrow> p dvd r"
  1211 proof -
  1212   from h have "poly (q ^ n) = poly r"
  1213     by auto
  1214   then have "(q ^ n) = r"
  1215     by (simp add: poly_eq_poly_eq_iff)
  1216   then show "p dvd (q ^ n) \<longleftrightarrow> p dvd r"
  1217     by simp
  1218 qed
  1219 
  1220 lemma poly_const_conv:
  1221   fixes x :: "'a::comm_ring_1"
  1222   shows "poly [:c:] x = y \<longleftrightarrow> c = y"
  1223   by simp
  1224 
  1225 end