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