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