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