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