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