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