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