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