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