src/HOL/Library/Fundamental_Theorem_Algebra.thy
author Andreas Lochbihler
Fri Sep 20 10:09:16 2013 +0200 (2013-09-20)
changeset 53745 788730ab7da4
parent 53077 a1b3784f8129
child 54230 b1d955791529
permissions -rw-r--r--
prefer Code.abort over code_abort
     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 ex bz obtain x Y where x: "P x" and Y: "\<And>x. P x \<Longrightarrow> x < Y"  by blast
   161   from ex have thx:"\<exists>x. x \<in> Collect P" by blast
   162   from bz have thY: "\<exists>Y. isUb UNIV (Collect P) Y"
   163     by(auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def order_le_less)
   164   from reals_complete[OF thx thY] obtain L where L: "isLub UNIV (Collect P) L"
   165     by blast
   166   from Y[OF x] have xY: "x < Y" .
   167   from L have L': "\<forall>x. P x \<longrightarrow> x \<le> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)
   168   from Y have Y': "\<forall>x. P x \<longrightarrow> x \<le> Y"
   169     apply (clarsimp, atomize (full)) by auto
   170   from L Y' have "L \<le> Y" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)
   171   {fix y
   172     {fix z assume z: "P z" "y < z"
   173       from L' z have "y < L" by auto }
   174     moreover
   175     {assume yL: "y < L" "\<forall>z. P z \<longrightarrow> \<not> y < z"
   176       hence nox: "\<forall>z. P z \<longrightarrow> y \<ge> z" by auto
   177       from nox L have "y \<ge> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)
   178       with yL(1) have False  by arith}
   179     ultimately have "(\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < L" by blast}
   180   thus ?thesis by blast
   181 qed
   182 
   183 subsection {* Fundamental theorem of algebra *}
   184 lemma  unimodular_reduce_norm:
   185   assumes md: "cmod z = 1"
   186   shows "cmod (z + 1) < 1 \<or> cmod (z - 1) < 1 \<or> cmod (z + ii) < 1 \<or> cmod (z - ii) < 1"
   187 proof-
   188   obtain x y where z: "z = Complex x y " by (cases z, auto)
   189   from md z have xy: "x\<^sup>2 + y\<^sup>2 = 1" by (simp add: cmod_def)
   190   {assume C: "cmod (z + 1) \<ge> 1" "cmod (z - 1) \<ge> 1" "cmod (z + ii) \<ge> 1" "cmod (z - ii) \<ge> 1"
   191     from C z xy have "2*x \<le> 1" "2*x \<ge> -1" "2*y \<le> 1" "2*y \<ge> -1"
   192       by (simp_all add: cmod_def power2_eq_square algebra_simps)
   193     hence "abs (2*x) \<le> 1" "abs (2*y) \<le> 1" by simp_all
   194     hence "(abs (2 * x))\<^sup>2 <= 1\<^sup>2" "(abs (2 * y))\<^sup>2 <= 1\<^sup>2"
   195       by - (rule power_mono, simp, simp)+
   196     hence th0: "4*x\<^sup>2 \<le> 1" "4*y\<^sup>2 \<le> 1"
   197       by (simp_all add: power_mult_distrib)
   198     from add_mono[OF th0] xy have False by simp }
   199   thus ?thesis unfolding linorder_not_le[symmetric] by blast
   200 qed
   201 
   202 text{* Hence we can always reduce modulus of @{text "1 + b z^n"} if nonzero *}
   203 lemma reduce_poly_simple:
   204  assumes b: "b \<noteq> 0" and n: "n\<noteq>0"
   205   shows "\<exists>z. cmod (1 + b * z^n) < 1"
   206 using n
   207 proof(induct n rule: nat_less_induct)
   208   fix n
   209   assume IH: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>z. cmod (1 + b * z ^ m) < 1)" and n: "n \<noteq> 0"
   210   let ?P = "\<lambda>z n. cmod (1 + b * z ^ n) < 1"
   211   {assume e: "even n"
   212     hence "\<exists>m. n = 2*m" by presburger
   213     then obtain m where m: "n = 2*m" by blast
   214     from n m have "m\<noteq>0" "m < n" by presburger+
   215     with IH[rule_format, of m] obtain z where z: "?P z m" by blast
   216     from z have "?P (csqrt z) n" by (simp add: m power_mult csqrt)
   217     hence "\<exists>z. ?P z n" ..}
   218   moreover
   219   {assume o: "odd n"
   220     have th0: "cmod (complex_of_real (cmod b) / b) = 1"
   221       using b by (simp add: norm_divide)
   222     from o have "\<exists>m. n = Suc (2*m)" by presburger+
   223     then obtain m where m: "n = Suc (2*m)" by blast
   224     from unimodular_reduce_norm[OF th0] o
   225     have "\<exists>v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
   226       apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1", rule_tac x="1" in exI, simp)
   227       apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1", rule_tac x="-1" in exI, simp add: diff_minus)
   228       apply (cases "cmod (complex_of_real (cmod b) / b + ii) < 1")
   229       apply (cases "even m", rule_tac x="ii" in exI, simp add: m power_mult)
   230       apply (rule_tac x="- ii" in exI, simp add: m power_mult)
   231       apply (cases "even m", rule_tac x="- ii" in exI, simp add: m power_mult diff_minus)
   232       apply (rule_tac x="ii" in exI, simp add: m power_mult diff_minus)
   233       done
   234     then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1" by blast
   235     let ?w = "v / complex_of_real (root n (cmod b))"
   236     from odd_real_root_pow[OF o, of "cmod b"]
   237     have th1: "?w ^ n = v^n / complex_of_real (cmod b)"
   238       by (simp add: power_divide complex_of_real_power)
   239     have th2:"cmod (complex_of_real (cmod b) / b) = 1" using b by (simp add: norm_divide)
   240     hence th3: "cmod (complex_of_real (cmod b) / b) \<ge> 0" by simp
   241     have th4: "cmod (complex_of_real (cmod b) / b) *
   242    cmod (1 + b * (v ^ n / complex_of_real (cmod b)))
   243    < cmod (complex_of_real (cmod b) / b) * 1"
   244       apply (simp only: norm_mult[symmetric] distrib_left)
   245       using b v by (simp add: th2)
   246 
   247     from mult_less_imp_less_left[OF th4 th3]
   248     have "?P ?w n" unfolding th1 .
   249     hence "\<exists>z. ?P z n" .. }
   250   ultimately show "\<exists>z. ?P z n" by blast
   251 qed
   252 
   253 text{* Bolzano-Weierstrass type property for closed disc in complex plane. *}
   254 
   255 lemma metric_bound_lemma: "cmod (x - y) <= \<bar>Re x - Re y\<bar> + \<bar>Im x - Im y\<bar>"
   256   using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y" ]
   257   unfolding cmod_def by simp
   258 
   259 lemma bolzano_weierstrass_complex_disc:
   260   assumes r: "\<forall>n. cmod (s n) \<le> r"
   261   shows "\<exists>f z. subseq f \<and> (\<forall>e >0. \<exists>N. \<forall>n \<ge> N. cmod (s (f n) - z) < e)"
   262 proof-
   263   from seq_monosub[of "Re o s"]
   264   obtain f g where f: "subseq f" "monoseq (\<lambda>n. Re (s (f n)))"
   265     unfolding o_def by blast
   266   from seq_monosub[of "Im o s o f"]
   267   obtain g where g: "subseq g" "monoseq (\<lambda>n. Im (s(f(g n))))" unfolding o_def by blast
   268   let ?h = "f o g"
   269   from r[rule_format, of 0] have rp: "r \<ge> 0" using norm_ge_zero[of "s 0"] by arith
   270   have th:"\<forall>n. r + 1 \<ge> \<bar> Re (s n)\<bar>"
   271   proof
   272     fix n
   273     from abs_Re_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Re (s n)\<bar> \<le> r + 1" by arith
   274   qed
   275   have conv1: "convergent (\<lambda>n. Re (s ( f n)))"
   276     apply (rule Bseq_monoseq_convergent)
   277     apply (simp add: Bseq_def)
   278     apply (rule exI[where x= "r + 1"])
   279     using th rp apply simp
   280     using f(2) .
   281   have th:"\<forall>n. r + 1 \<ge> \<bar> Im (s n)\<bar>"
   282   proof
   283     fix n
   284     from abs_Im_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Im (s n)\<bar> \<le> r + 1" by arith
   285   qed
   286 
   287   have conv2: "convergent (\<lambda>n. Im (s (f (g n))))"
   288     apply (rule Bseq_monoseq_convergent)
   289     apply (simp add: Bseq_def)
   290     apply (rule exI[where x= "r + 1"])
   291     using th rp apply simp
   292     using g(2) .
   293 
   294   from conv1[unfolded convergent_def] obtain x where "LIMSEQ (\<lambda>n. Re (s (f n))) x"
   295     by blast
   296   hence  x: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Re (s (f n)) - x \<bar> < r"
   297     unfolding LIMSEQ_iff real_norm_def .
   298 
   299   from conv2[unfolded convergent_def] obtain y where "LIMSEQ (\<lambda>n. Im (s (f (g n)))) y"
   300     by blast
   301   hence  y: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Im (s (f (g n))) - y \<bar> < r"
   302     unfolding LIMSEQ_iff real_norm_def .
   303   let ?w = "Complex x y"
   304   from f(1) g(1) have hs: "subseq ?h" unfolding subseq_def by auto
   305   {fix e assume ep: "e > (0::real)"
   306     hence e2: "e/2 > 0" by simp
   307     from x[rule_format, OF e2] y[rule_format, OF e2]
   308     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
   309     {fix n assume nN12: "n \<ge> N1 + N2"
   310       hence nN1: "g n \<ge> N1" and nN2: "n \<ge> N2" using seq_suble[OF g(1), of n] by arith+
   311       from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
   312       have "cmod (s (?h n) - ?w) < e"
   313         using metric_bound_lemma[of "s (f (g n))" ?w] by simp }
   314     hence "\<exists>N. \<forall>n\<ge>N. cmod (s (?h n) - ?w) < e" by blast }
   315   with hs show ?thesis  by blast
   316 qed
   317 
   318 text{* Polynomial is continuous. *}
   319 
   320 lemma poly_cont:
   321   assumes ep: "e > 0"
   322   shows "\<exists>d >0. \<forall>w. 0 < cmod (w - z) \<and> cmod (w - z) < d \<longrightarrow> cmod (poly p w - poly p z) < e"
   323 proof-
   324   obtain q where q: "degree q = degree p" "\<And>x. poly q x = poly p (z + x)"
   325   proof
   326     show "degree (offset_poly p z) = degree p"
   327       by (rule degree_offset_poly)
   328     show "\<And>x. poly (offset_poly p z) x = poly p (z + x)"
   329       by (rule poly_offset_poly)
   330   qed
   331   {fix w
   332     note q(2)[of "w - z", simplified]}
   333   note th = this
   334   show ?thesis unfolding th[symmetric]
   335   proof(induct q)
   336     case 0 thus ?case  using ep by auto
   337   next
   338     case (pCons c cs)
   339     from poly_bound_exists[of 1 "cs"]
   340     obtain m where m: "m > 0" "\<And>z. cmod z \<le> 1 \<Longrightarrow> cmod (poly cs z) \<le> m" by blast
   341     from ep m(1) have em0: "e/m > 0" by (simp add: field_simps)
   342     have one0: "1 > (0::real)"  by arith
   343     from real_lbound_gt_zero[OF one0 em0]
   344     obtain d where d: "d >0" "d < 1" "d < e / m" by blast
   345     from d(1,3) m(1) have dm: "d*m > 0" "d*m < e"
   346       by (simp_all add: field_simps mult_pos_pos)
   347     show ?case
   348       proof(rule ex_forward[OF real_lbound_gt_zero[OF one0 em0]], clarsimp simp add: norm_mult)
   349         fix d w
   350         assume H: "d > 0" "d < 1" "d < e/m" "w\<noteq>z" "cmod (w-z) < d"
   351         hence d1: "cmod (w-z) \<le> 1" "d \<ge> 0" by simp_all
   352         from H(3) m(1) have dme: "d*m < e" by (simp add: field_simps)
   353         from H have th: "cmod (w-z) \<le> d" by simp
   354         from mult_mono[OF th m(2)[OF d1(1)] d1(2) norm_ge_zero] dme
   355         show "cmod (w - z) * cmod (poly cs (w - z)) < e" by simp
   356       qed
   357     qed
   358 qed
   359 
   360 text{* Hence a polynomial attains minimum on a closed disc
   361   in the complex plane. *}
   362 lemma  poly_minimum_modulus_disc:
   363   "\<exists>z. \<forall>w. cmod w \<le> r \<longrightarrow> cmod (poly p z) \<le> cmod (poly p w)"
   364 proof-
   365   {assume "\<not> r \<ge> 0" hence ?thesis unfolding linorder_not_le
   366       apply -
   367       apply (rule exI[where x=0])
   368       apply auto
   369       apply (subgoal_tac "cmod w < 0")
   370       apply simp
   371       apply arith
   372       done }
   373   moreover
   374   {assume rp: "r \<ge> 0"
   375     from rp have "cmod 0 \<le> r \<and> cmod (poly p 0) = - (- cmod (poly p 0))" by simp
   376     hence mth1: "\<exists>x z. cmod z \<le> r \<and> cmod (poly p z) = - x"  by blast
   377     {fix x z
   378       assume H: "cmod z \<le> r" "cmod (poly p z) = - x" "\<not>x < 1"
   379       hence "- x < 0 " by arith
   380       with H(2) norm_ge_zero[of "poly p z"]  have False by simp }
   381     then have mth2: "\<exists>z. \<forall>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<longrightarrow> x < z" by blast
   382     from real_sup_exists[OF mth1 mth2] obtain s where
   383       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
   384     let ?m = "-s"
   385     {fix y
   386       from s[rule_format, of "-y"] have
   387     "(\<exists>z x. cmod z \<le> r \<and> -(- cmod (poly p z)) < y) \<longleftrightarrow> ?m < y"
   388         unfolding minus_less_iff[of y ] equation_minus_iff by blast }
   389     note s1 = this[unfolded minus_minus]
   390     from s1[of ?m] have s1m: "\<And>z x. cmod z \<le> r \<Longrightarrow> cmod (poly p z) \<ge> ?m"
   391       by auto
   392     {fix n::nat
   393       from s1[rule_format, of "?m + 1/real (Suc n)"]
   394       have "\<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)"
   395         by simp}
   396     hence th: "\<forall>n. \<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)" ..
   397     from choice[OF th] obtain g where
   398       g: "\<forall>n. cmod (g n) \<le> r" "\<forall>n. cmod (poly p (g n)) <?m+1 /real(Suc n)"
   399       by blast
   400     from bolzano_weierstrass_complex_disc[OF g(1)]
   401     obtain f z where fz: "subseq f" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. cmod (g (f n) - z) < e"
   402       by blast
   403     {fix w
   404       assume wr: "cmod w \<le> r"
   405       let ?e = "\<bar>cmod (poly p z) - ?m\<bar>"
   406       {assume e: "?e > 0"
   407         hence e2: "?e/2 > 0" by simp
   408         from poly_cont[OF e2, of z p] obtain d where
   409           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
   410         {fix w assume w: "cmod (w - z) < d"
   411           have "cmod(poly p w - poly p z) < ?e / 2"
   412             using d(2)[rule_format, of w] w e by (cases "w=z", simp_all)}
   413         note th1 = this
   414 
   415         from fz(2)[rule_format, OF d(1)] obtain N1 where
   416           N1: "\<forall>n\<ge>N1. cmod (g (f n) - z) < d" by blast
   417         from reals_Archimedean2[of "2/?e"] obtain N2::nat where
   418           N2: "2/?e < real N2" by blast
   419         have th2: "cmod(poly p (g(f(N1 + N2))) - poly p z) < ?e/2"
   420           using N1[rule_format, of "N1 + N2"] th1 by simp
   421         {fix a b e2 m :: real
   422         have "a < e2 \<Longrightarrow> abs(b - m) < e2 \<Longrightarrow> 2 * e2 <= abs(b - m) + a
   423           ==> False" by arith}
   424       note th0 = this
   425       have ath:
   426         "\<And>m x e. m <= x \<Longrightarrow>  x < m + e ==> abs(x - m::real) < e" by arith
   427       from s1m[OF g(1)[rule_format]]
   428       have th31: "?m \<le> cmod(poly p (g (f (N1 + N2))))" .
   429       from seq_suble[OF fz(1), of "N1+N2"]
   430       have th00: "real (Suc (N1+N2)) \<le> real (Suc (f (N1+N2)))" by simp
   431       have th000: "0 \<le> (1::real)" "(1::real) \<le> 1" "real (Suc (N1+N2)) > 0"
   432         using N2 by auto
   433       from frac_le[OF th000 th00] have th00: "?m +1 / real (Suc (f (N1 + N2))) \<le> ?m + 1 / real (Suc (N1 + N2))" by simp
   434       from g(2)[rule_format, of "f (N1 + N2)"]
   435       have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
   436       from order_less_le_trans[OF th01 th00]
   437       have th32: "cmod(poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
   438       from N2 have "2/?e < real (Suc (N1 + N2))" by arith
   439       with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
   440       have "?e/2 > 1/ real (Suc (N1 + N2))" by (simp add: inverse_eq_divide)
   441       with ath[OF th31 th32]
   442       have thc1:"\<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar>< ?e/2" by arith
   443       have ath2: "\<And>(a::real) b c m. \<bar>a - b\<bar> <= c ==> \<bar>b - m\<bar> <= \<bar>a - m\<bar> + c"
   444         by arith
   445       have th22: "\<bar>cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)\<bar>
   446 \<le> cmod (poly p (g (f (N1 + N2))) - poly p z)"
   447         by (simp add: norm_triangle_ineq3)
   448       from ath2[OF th22, of ?m]
   449       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
   450       from th0[OF th2 thc1 thc2] have False .}
   451       hence "?e = 0" by auto
   452       then have "cmod (poly p z) = ?m" by simp
   453       with s1m[OF wr]
   454       have "cmod (poly p z) \<le> cmod (poly p w)" by simp }
   455     hence ?thesis by blast}
   456   ultimately show ?thesis by blast
   457 qed
   458 
   459 lemma "(rcis (sqrt (abs r)) (a/2))\<^sup>2 = rcis (abs r) a"
   460   unfolding power2_eq_square
   461   apply (simp add: rcis_mult)
   462   apply (simp add: power2_eq_square[symmetric])
   463   done
   464 
   465 lemma cispi: "cis pi = -1"
   466   unfolding cis_def
   467   by simp
   468 
   469 lemma "(rcis (sqrt (abs r)) ((pi + a)/2))\<^sup>2 = rcis (- abs r) a"
   470   unfolding power2_eq_square
   471   apply (simp add: rcis_mult add_divide_distrib)
   472   apply (simp add: power2_eq_square[symmetric] rcis_def cispi cis_mult[symmetric])
   473   done
   474 
   475 text {* Nonzero polynomial in z goes to infinity as z does. *}
   476 
   477 lemma poly_infinity:
   478   assumes ex: "p \<noteq> 0"
   479   shows "\<exists>r. \<forall>z. r \<le> cmod z \<longrightarrow> d \<le> cmod (poly (pCons a p) z)"
   480 using ex
   481 proof(induct p arbitrary: a d)
   482   case (pCons c cs a d)
   483   {assume H: "cs \<noteq> 0"
   484     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
   485     let ?r = "1 + \<bar>r\<bar>"
   486     {fix z assume h: "1 + \<bar>r\<bar> \<le> cmod z"
   487       have r0: "r \<le> cmod z" using h by arith
   488       from r[rule_format, OF r0]
   489       have th0: "d + cmod a \<le> 1 * cmod(poly (pCons c cs) z)" by arith
   490       from h have z1: "cmod z \<ge> 1" by arith
   491       from order_trans[OF th0 mult_right_mono[OF z1 norm_ge_zero[of "poly (pCons c cs) z"]]]
   492       have th1: "d \<le> cmod(z * poly (pCons c cs) z) - cmod a"
   493         unfolding norm_mult by (simp add: algebra_simps)
   494       from complex_mod_triangle_sub[of "z * poly (pCons c cs) z" a]
   495       have th2: "cmod(z * poly (pCons c cs) z) - cmod a \<le> cmod (poly (pCons a (pCons c cs)) z)"
   496         by (simp add: algebra_simps)
   497       from th1 th2 have "d \<le> cmod (poly (pCons a (pCons c cs)) z)"  by arith}
   498     hence ?case by blast}
   499   moreover
   500   {assume cs0: "\<not> (cs \<noteq> 0)"
   501     with pCons.prems have c0: "c \<noteq> 0" by simp
   502     from cs0 have cs0': "cs = 0" by simp
   503     {fix z
   504       assume h: "(\<bar>d\<bar> + cmod a) / cmod c \<le> cmod z"
   505       from c0 have "cmod c > 0" by simp
   506       from h c0 have th0: "\<bar>d\<bar> + cmod a \<le> cmod (z*c)"
   507         by (simp add: field_simps norm_mult)
   508       have ath: "\<And>mzh mazh ma. mzh <= mazh + ma ==> abs(d) + ma <= mzh ==> d <= mazh" by arith
   509       from complex_mod_triangle_sub[of "z*c" a ]
   510       have th1: "cmod (z * c) \<le> cmod (a + z * c) + cmod a"
   511         by (simp add: algebra_simps)
   512       from ath[OF th1 th0] have "d \<le> cmod (poly (pCons a (pCons c cs)) z)"
   513         using cs0' by simp}
   514     then have ?case  by blast}
   515   ultimately show ?case by blast
   516 qed simp
   517 
   518 text {* Hence polynomial's modulus attains its minimum somewhere. *}
   519 lemma poly_minimum_modulus:
   520   "\<exists>z.\<forall>w. cmod (poly p z) \<le> cmod (poly p w)"
   521 proof(induct p)
   522   case (pCons c cs)
   523   {assume cs0: "cs \<noteq> 0"
   524     from poly_infinity[OF cs0, of "cmod (poly (pCons c cs) 0)" c]
   525     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
   526     have ath: "\<And>z r. r \<le> cmod z \<or> cmod z \<le> \<bar>r\<bar>" by arith
   527     from poly_minimum_modulus_disc[of "\<bar>r\<bar>" "pCons c cs"]
   528     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
   529     {fix z assume z: "r \<le> cmod z"
   530       from v[of 0] r[OF z]
   531       have "cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) z)"
   532         by simp }
   533     note v0 = this
   534     from v0 v ath[of r] have ?case by blast}
   535   moreover
   536   {assume cs0: "\<not> (cs \<noteq> 0)"
   537     hence th:"cs = 0" by simp
   538     from th pCons.hyps have ?case by simp}
   539   ultimately show ?case by blast
   540 qed simp
   541 
   542 text{* Constant function (non-syntactic characterization). *}
   543 definition "constant f = (\<forall>x y. f x = f y)"
   544 
   545 lemma nonconstant_length: "\<not> (constant (poly p)) \<Longrightarrow> psize p \<ge> 2"
   546   unfolding constant_def psize_def
   547   apply (induct p, auto)
   548   done
   549 
   550 lemma poly_replicate_append:
   551   "poly (monom 1 n * p) (x::'a::{comm_ring_1}) = x^n * poly p x"
   552   by (simp add: poly_monom)
   553 
   554 text {* Decomposition of polynomial, skipping zero coefficients
   555   after the first.  *}
   556 
   557 lemma poly_decompose_lemma:
   558  assumes nz: "\<not>(\<forall>z. z\<noteq>0 \<longrightarrow> poly p z = (0::'a::{idom}))"
   559   shows "\<exists>k a q. a\<noteq>0 \<and> Suc (psize q + k) = psize p \<and>
   560                  (\<forall>z. poly p z = z^k * poly (pCons a q) z)"
   561 unfolding psize_def
   562 using nz
   563 proof(induct p)
   564   case 0 thus ?case by simp
   565 next
   566   case (pCons c cs)
   567   {assume c0: "c = 0"
   568     from pCons.hyps pCons.prems c0 have ?case
   569       apply (auto)
   570       apply (rule_tac x="k+1" in exI)
   571       apply (rule_tac x="a" in exI, clarsimp)
   572       apply (rule_tac x="q" in exI)
   573       by (auto)}
   574   moreover
   575   {assume c0: "c\<noteq>0"
   576     hence ?case apply-
   577       apply (rule exI[where x=0])
   578       apply (rule exI[where x=c], clarsimp)
   579       apply (rule exI[where x=cs])
   580       apply auto
   581       done}
   582   ultimately show ?case by blast
   583 qed
   584 
   585 lemma poly_decompose:
   586   assumes nc: "~constant(poly p)"
   587   shows "\<exists>k a q. a\<noteq>(0::'a::{idom}) \<and> k\<noteq>0 \<and>
   588                psize q + k + 1 = psize p \<and>
   589               (\<forall>z. poly p z = poly p 0 + z^k * poly (pCons a q) z)"
   590 using nc
   591 proof(induct p)
   592   case 0 thus ?case by (simp add: constant_def)
   593 next
   594   case (pCons c cs)
   595   {assume C:"\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0"
   596     {fix x y
   597       from C have "poly (pCons c cs) x = poly (pCons c cs) y" by (cases "x=0", auto)}
   598     with pCons.prems have False by (auto simp add: constant_def)}
   599   hence th: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0)" ..
   600   from poly_decompose_lemma[OF th]
   601   show ?case
   602     apply clarsimp
   603     apply (rule_tac x="k+1" in exI)
   604     apply (rule_tac x="a" in exI)
   605     apply simp
   606     apply (rule_tac x="q" in exI)
   607     apply (auto simp add: psize_def split: if_splits)
   608     done
   609 qed
   610 
   611 text{* Fundamental theorem of algebra *}
   612 
   613 lemma fundamental_theorem_of_algebra:
   614   assumes nc: "~constant(poly p)"
   615   shows "\<exists>z::complex. poly p z = 0"
   616 using nc
   617 proof(induct "psize p" arbitrary: p rule: less_induct)
   618   case less
   619   let ?p = "poly p"
   620   let ?ths = "\<exists>z. ?p z = 0"
   621 
   622   from nonconstant_length[OF less(2)] have n2: "psize p \<ge> 2" .
   623   from poly_minimum_modulus obtain c where
   624     c: "\<forall>w. cmod (?p c) \<le> cmod (?p w)" by blast
   625   {assume pc: "?p c = 0" hence ?ths by blast}
   626   moreover
   627   {assume pc0: "?p c \<noteq> 0"
   628     from poly_offset[of p c] obtain q where
   629       q: "psize q = psize p" "\<forall>x. poly q x = ?p (c+x)" by blast
   630     {assume h: "constant (poly q)"
   631       from q(2) have th: "\<forall>x. poly q (x - c) = ?p x" by auto
   632       {fix x y
   633         from th have "?p x = poly q (x - c)" by auto
   634         also have "\<dots> = poly q (y - c)"
   635           using h unfolding constant_def by blast
   636         also have "\<dots> = ?p y" using th by auto
   637         finally have "?p x = ?p y" .}
   638       with less(2) have False unfolding constant_def by blast }
   639     hence qnc: "\<not> constant (poly q)" by blast
   640     from q(2) have pqc0: "?p c = poly q 0" by simp
   641     from c pqc0 have cq0: "\<forall>w. cmod (poly q 0) \<le> cmod (?p w)" by simp
   642     let ?a0 = "poly q 0"
   643     from pc0 pqc0 have a00: "?a0 \<noteq> 0" by simp
   644     from a00
   645     have qr: "\<forall>z. poly q z = poly (smult (inverse ?a0) q) z * ?a0"
   646       by simp
   647     let ?r = "smult (inverse ?a0) q"
   648     have lgqr: "psize q = psize ?r"
   649       using a00 unfolding psize_def degree_def
   650       by (simp add: poly_eq_iff)
   651     {assume h: "\<And>x y. poly ?r x = poly ?r y"
   652       {fix x y
   653         from qr[rule_format, of x]
   654         have "poly q x = poly ?r x * ?a0" by auto
   655         also have "\<dots> = poly ?r y * ?a0" using h by simp
   656         also have "\<dots> = poly q y" using qr[rule_format, of y] by simp
   657         finally have "poly q x = poly q y" .}
   658       with qnc have False unfolding constant_def by blast}
   659     hence rnc: "\<not> constant (poly ?r)" unfolding constant_def by blast
   660     from qr[rule_format, of 0] a00  have r01: "poly ?r 0 = 1" by auto
   661     {fix w
   662       have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w / ?a0) < 1"
   663         using qr[rule_format, of w] a00 by (simp add: divide_inverse mult_ac)
   664       also have "\<dots> \<longleftrightarrow> cmod (poly q w) < cmod ?a0"
   665         using a00 unfolding norm_divide by (simp add: field_simps)
   666       finally have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w) < cmod ?a0" .}
   667     note mrmq_eq = this
   668     from poly_decompose[OF rnc] obtain k a s where
   669       kas: "a\<noteq>0" "k\<noteq>0" "psize s + k + 1 = psize ?r"
   670       "\<forall>z. poly ?r z = poly ?r 0 + z^k* poly (pCons a s) z" by blast
   671     {assume "psize p = k + 1"
   672       with kas(3) lgqr[symmetric] q(1) have s0:"s=0" by auto
   673       {fix w
   674         have "cmod (poly ?r w) = cmod (1 + a * w ^ k)"
   675           using kas(4)[rule_format, of w] s0 r01 by (simp add: algebra_simps)}
   676       note hth = this [symmetric]
   677         from reduce_poly_simple[OF kas(1,2)]
   678       have "\<exists>w. cmod (poly ?r w) < 1" unfolding hth by blast}
   679     moreover
   680     {assume kn: "psize p \<noteq> k+1"
   681       from kn kas(3) q(1) lgqr have k1n: "k + 1 < psize p" by simp
   682       have th01: "\<not> constant (poly (pCons 1 (monom a (k - 1))))"
   683         unfolding constant_def poly_pCons poly_monom
   684         using kas(1) apply simp
   685         by (rule exI[where x=0], rule exI[where x=1], simp)
   686       from kas(1) kas(2) have th02: "k+1 = psize (pCons 1 (monom a (k - 1)))"
   687         by (simp add: psize_def degree_monom_eq)
   688       from less(1) [OF k1n [simplified th02] th01]
   689       obtain w where w: "1 + w^k * a = 0"
   690         unfolding poly_pCons poly_monom
   691         using kas(2) by (cases k, auto simp add: algebra_simps)
   692       from poly_bound_exists[of "cmod w" s] obtain m where
   693         m: "m > 0" "\<forall>z. cmod z \<le> cmod w \<longrightarrow> cmod (poly s z) \<le> m" by blast
   694       have w0: "w\<noteq>0" using kas(2) w by (auto simp add: power_0_left)
   695       from w have "(1 + w ^ k * a) - 1 = 0 - 1" by simp
   696       then have wm1: "w^k * a = - 1" by simp
   697       have inv0: "0 < inverse (cmod w ^ (k + 1) * m)"
   698         using norm_ge_zero[of w] w0 m(1)
   699           by (simp add: inverse_eq_divide zero_less_mult_iff)
   700       with real_down2[OF zero_less_one] obtain t where
   701         t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
   702       let ?ct = "complex_of_real t"
   703       let ?w = "?ct * w"
   704       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)
   705       also have "\<dots> = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
   706         unfolding wm1 by (simp)
   707       finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) = cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)"
   708         apply -
   709         apply (rule cong[OF refl[of cmod]])
   710         apply assumption
   711         done
   712       with norm_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"]
   713       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
   714       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
   715       have "t *cmod w \<le> 1 * cmod w" apply (rule mult_mono) using t(1,2) by auto
   716       then have tw: "cmod ?w \<le> cmod w" using t(1) by (simp add: norm_mult)
   717       from t inv0 have "t* (cmod w ^ (k + 1) * m) < 1"
   718         by (simp add: inverse_eq_divide field_simps)
   719       with zero_less_power[OF t(1), of k]
   720       have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1"
   721         apply - apply (rule mult_strict_left_mono) by simp_all
   722       have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k+1) * cmod (poly s ?w)))"  using w0 t(1)
   723         by (simp add: algebra_simps power_mult_distrib norm_power norm_mult)
   724       then have "cmod (?w^k * ?w * poly s ?w) \<le> t^k * (t* (cmod w ^ (k + 1) * m))"
   725         using t(1,2) m(2)[rule_format, OF tw] w0
   726         apply (simp only: )
   727         apply auto
   728         done
   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_down2[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           from k oop [of a] have "q ^ n = p * ?w"
   829             apply -
   830             apply (subst r, subst s, subst kpn)
   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 apply (cases "q = 0", simp_all)
   944 apply (erule dvd_imp_degree_le [OF pq])
   945 done
   946 
   947 (* Arithmetic operations on multivariate polynomials.                        *)
   948 
   949 lemma mpoly_base_conv:
   950   "(0::complex) \<equiv> poly 0 x" "c \<equiv> poly [:c:] x" "x \<equiv> poly [:0,1:] x" by simp_all
   951 
   952 lemma mpoly_norm_conv:
   953   "poly [:0:] (x::complex) \<equiv> poly 0 x" "poly [:poly 0 y:] x \<equiv> poly 0 x" by simp_all
   954 
   955 lemma mpoly_sub_conv:
   956   "poly p (x::complex) - poly q x \<equiv> poly p x + -1 * poly q x"
   957   by (simp add: diff_minus)
   958 
   959 lemma poly_pad_rule: "poly p x = 0 ==> poly (pCons 0 p) x = (0::complex)" by simp
   960 
   961 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
   962 
   963 lemma resolve_eq_raw:  "poly 0 x \<equiv> 0" "poly [:c:] x \<equiv> (c::complex)" by auto
   964 lemma  resolve_eq_then: "(P \<Longrightarrow> (Q \<equiv> Q1)) \<Longrightarrow> (\<not>P \<Longrightarrow> (Q \<equiv> Q2))
   965   \<Longrightarrow> Q \<equiv> P \<and> Q1 \<or> \<not>P\<and> Q2" apply (atomize (full)) by blast
   966 
   967 lemma poly_divides_pad_rule:
   968   fixes p q :: "complex poly"
   969   assumes pq: "p dvd q"
   970   shows "p dvd (pCons (0::complex) q)"
   971 proof-
   972   have "pCons 0 q = q * [:0,1:]" by simp
   973   then have "q dvd (pCons 0 q)" ..
   974   with pq show ?thesis by (rule dvd_trans)
   975 qed
   976 
   977 lemma poly_divides_pad_const_rule:
   978   fixes p q :: "complex poly"
   979   assumes pq: "p dvd q"
   980   shows "p dvd (smult a q)"
   981 proof-
   982   have "smult a q = q * [:a:]" by simp
   983   then have "q dvd smult a q" ..
   984   with pq show ?thesis by (rule dvd_trans)
   985 qed
   986 
   987 
   988 lemma poly_divides_conv0:
   989   fixes p :: "complex poly"
   990   assumes lgpq: "degree q < degree p" and lq:"p \<noteq> 0"
   991   shows "p dvd q \<equiv> q = 0" (is "?lhs \<equiv> ?rhs")
   992 proof-
   993   {assume r: ?rhs
   994     hence "q = p * 0" by simp
   995     hence ?lhs ..}
   996   moreover
   997   {assume l: ?lhs
   998     {assume q0: "q = 0"
   999       hence ?rhs by simp}
  1000     moreover
  1001     {assume q0: "q \<noteq> 0"
  1002       from l q0 have "degree p \<le> degree q"
  1003         by (rule dvd_imp_degree_le)
  1004       with lgpq have ?rhs by simp }
  1005     ultimately have ?rhs by blast }
  1006   ultimately show "?lhs \<equiv> ?rhs" by - (atomize (full), blast)
  1007 qed
  1008 
  1009 lemma poly_divides_conv1:
  1010   assumes a0: "a\<noteq> (0::complex)" and pp': "(p::complex poly) dvd p'"
  1011   and qrp': "smult a q - p' \<equiv> r"
  1012   shows "p dvd q \<equiv> p dvd (r::complex poly)" (is "?lhs \<equiv> ?rhs")
  1013 proof-
  1014   {
  1015   from pp' obtain t where t: "p' = p * t" ..
  1016   {assume l: ?lhs
  1017     then obtain u where u: "q = p * u" ..
  1018      have "r = p * (smult a u - t)"
  1019        using u qrp' [symmetric] t by (simp add: algebra_simps)
  1020      then have ?rhs ..}
  1021   moreover
  1022   {assume r: ?rhs
  1023     then obtain u where u: "r = p * u" ..
  1024     from u [symmetric] t qrp' [symmetric] a0
  1025     have "q = p * smult (1/a) (u + t)" by (simp add: algebra_simps)
  1026     hence ?lhs ..}
  1027   ultimately have "?lhs = ?rhs" by blast }
  1028 thus "?lhs \<equiv> ?rhs"  by - (atomize(full), blast)
  1029 qed
  1030 
  1031 lemma basic_cqe_conv1:
  1032   "(\<exists>x. poly p x = 0 \<and> poly 0 x \<noteq> 0) \<equiv> False"
  1033   "(\<exists>x. poly 0 x \<noteq> 0) \<equiv> False"
  1034   "(\<exists>x. poly [:c:] x \<noteq> 0) \<equiv> c\<noteq>0"
  1035   "(\<exists>x. poly 0 x = 0) \<equiv> True"
  1036   "(\<exists>x. poly [:c:] x = 0) \<equiv> c = 0" by simp_all
  1037 
  1038 lemma basic_cqe_conv2:
  1039   assumes l:"p \<noteq> 0"
  1040   shows "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True"
  1041 proof-
  1042   {fix h t
  1043     assume h: "h\<noteq>0" "t=0"  "pCons a (pCons b p) = pCons h t"
  1044     with l have False by simp}
  1045   hence th: "\<not> (\<exists> h t. h\<noteq>0 \<and> t=0 \<and> pCons a (pCons b p) = pCons h t)"
  1046     by blast
  1047   from fundamental_theorem_of_algebra_alt[OF th]
  1048   show "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True" by auto
  1049 qed
  1050 
  1051 lemma  basic_cqe_conv_2b: "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> (p \<noteq> 0)"
  1052 proof-
  1053   have "p = 0 \<longleftrightarrow> poly p = poly 0"
  1054     by (simp add: poly_eq_poly_eq_iff)
  1055   also have "\<dots> \<longleftrightarrow> (\<not> (\<exists>x. poly p x \<noteq> 0))" by auto
  1056   finally show "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> p \<noteq> 0"
  1057     by - (atomize (full), blast)
  1058 qed
  1059 
  1060 lemma basic_cqe_conv3:
  1061   fixes p q :: "complex poly"
  1062   assumes l: "p \<noteq> 0"
  1063   shows "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
  1064 proof-
  1065   from l have dp:"degree (pCons a p) = psize p" by (simp add: psize_def)
  1066   from nullstellensatz_univariate[of "pCons a p" q] l
  1067   show "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
  1068     unfolding dp
  1069     by - (atomize (full), auto)
  1070 qed
  1071 
  1072 lemma basic_cqe_conv4:
  1073   fixes p q :: "complex poly"
  1074   assumes h: "\<And>x. poly (q ^ n) x \<equiv> poly r x"
  1075   shows "p dvd (q ^ n) \<equiv> p dvd r"
  1076 proof-
  1077   from h have "poly (q ^ n) = poly r" by auto
  1078   then have "(q ^ n) = r" by (simp add: poly_eq_poly_eq_iff)
  1079   thus "p dvd (q ^ n) \<equiv> p dvd r" by simp
  1080 qed
  1081 
  1082 lemma pmult_Cons_Cons: "(pCons (a::complex) (pCons b p) * q = (smult a q) + (pCons 0 (pCons b p * q)))"
  1083   by simp
  1084 
  1085 lemma elim_neg_conv: "- z \<equiv> (-1) * (z::complex)" by simp
  1086 lemma eqT_intr: "PROP P \<Longrightarrow> (True \<Longrightarrow> PROP P )" "PROP P \<Longrightarrow> True" by blast+
  1087 lemma negate_negate_rule: "Trueprop P \<equiv> (\<not> P \<equiv> False)" by (atomize (full), auto)
  1088 
  1089 lemma complex_entire: "(z::complex) \<noteq> 0 \<and> w \<noteq> 0 \<equiv> z*w \<noteq> 0" by simp
  1090 lemma resolve_eq_ne: "(P \<equiv> True) \<equiv> (\<not>P \<equiv> False)" "(P \<equiv> False) \<equiv> (\<not>P \<equiv> True)"
  1091   by (atomize (full)) simp_all
  1092 lemma cqe_conv1: "poly 0 x = 0 \<longleftrightarrow> True"  by simp
  1093 lemma cqe_conv2: "(p \<Longrightarrow> (q \<equiv> r)) \<equiv> ((p \<and> q) \<equiv> (p \<and> r))"  (is "?l \<equiv> ?r")
  1094 proof
  1095   assume "p \<Longrightarrow> q \<equiv> r" thus "p \<and> q \<equiv> p \<and> r" apply - apply (atomize (full)) by blast
  1096 next
  1097   assume "p \<and> q \<equiv> p \<and> r" "p"
  1098   thus "q \<equiv> r" apply - apply (atomize (full)) apply blast done
  1099 qed
  1100 lemma poly_const_conv: "poly [:c:] (x::complex) = y \<longleftrightarrow> c = y" by simp
  1101 
  1102 end