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