src/HOL/Complex/Fundamental_Theorem_Algebra.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 26162 8dbf0e9d93d3
child 27108 e447b3107696
permissions -rw-r--r--
avoid rebinding of existing facts;
chaieb@26123
     1
(*  Title:       Fundamental_Theorem_Algebra.thy
chaieb@26123
     2
    ID:          $Id$
chaieb@26123
     3
    Author:      Amine Chaieb
chaieb@26123
     4
*)
chaieb@26123
     5
chaieb@26123
     6
header{*Fundamental Theorem of Algebra*}
chaieb@26123
     7
chaieb@26123
     8
theory Fundamental_Theorem_Algebra
chaieb@26162
     9
  imports  Univ_Poly Dense_Linear_Order Complex
chaieb@26123
    10
begin
chaieb@26123
    11
chaieb@26123
    12
section {* Square root of complex numbers *}
chaieb@26123
    13
definition csqrt :: "complex \<Rightarrow> complex" where
chaieb@26123
    14
"csqrt z = (if Im z = 0 then
chaieb@26123
    15
            if 0 \<le> Re z then Complex (sqrt(Re z)) 0
chaieb@26123
    16
            else Complex 0 (sqrt(- Re z))
chaieb@26123
    17
           else Complex (sqrt((cmod z + Re z) /2))
chaieb@26123
    18
                        ((Im z / abs(Im z)) * sqrt((cmod z - Re z) /2)))"
chaieb@26123
    19
chaieb@26123
    20
lemma csqrt: "csqrt z ^ 2 = z"
chaieb@26123
    21
proof-
chaieb@26123
    22
  obtain x y where xy: "z = Complex x y" by (cases z, simp_all)
chaieb@26123
    23
  {assume y0: "y = 0"
chaieb@26123
    24
    {assume x0: "x \<ge> 0" 
chaieb@26123
    25
      then have ?thesis using y0 xy real_sqrt_pow2[OF x0]
chaieb@26123
    26
	by (simp add: csqrt_def power2_eq_square)}
chaieb@26123
    27
    moreover
chaieb@26123
    28
    {assume "\<not> x \<ge> 0" hence x0: "- x \<ge> 0" by arith
chaieb@26123
    29
      then have ?thesis using y0 xy real_sqrt_pow2[OF x0] 
chaieb@26123
    30
	by (simp add: csqrt_def power2_eq_square) }
chaieb@26123
    31
    ultimately have ?thesis by blast}
chaieb@26123
    32
  moreover
chaieb@26123
    33
  {assume y0: "y\<noteq>0"
chaieb@26123
    34
    {fix x y
chaieb@26123
    35
      let ?z = "Complex x y"
chaieb@26123
    36
      from abs_Re_le_cmod[of ?z] have tha: "abs x \<le> cmod ?z" by auto
chaieb@26123
    37
      hence "cmod ?z - x \<ge> 0" "cmod ?z + x \<ge> 0" by (cases "x \<ge> 0", arith+)
chaieb@26123
    38
      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
    39
    note th = this
chaieb@26123
    40
    have sq4: "\<And>x::real. x^2 / 4 = (x / 2) ^ 2"
chaieb@26123
    41
      by (simp add: power2_eq_square) 
chaieb@26123
    42
    from th[of x y]
chaieb@26123
    43
    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
    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"
chaieb@26123
    45
      unfolding power2_eq_square by simp 
chaieb@26123
    46
    have "sqrt 4 = sqrt (2^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
chaieb@26123
    50
      unfolding power2_eq_square 
chaieb@26123
    51
      by (simp add: ring_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
chaieb@26123
    58
chaieb@26123
    59
section{* More lemmas about module of complex numbers *}
chaieb@26123
    60
chaieb@26123
    61
lemma complex_of_real_power: "complex_of_real x ^ n = complex_of_real (x^n)"
chaieb@26123
    62
  by (induct n, auto)
chaieb@26123
    63
chaieb@26123
    64
lemma cmod_pos: "cmod z \<ge> 0" by simp
chaieb@26123
    65
lemma complex_mod_triangle_ineq: "cmod (z + w) \<le> cmod z + cmod w"
chaieb@26123
    66
  using complex_mod_triangle_ineq2[of z w] by (simp add: ring_simps)
chaieb@26123
    67
chaieb@26123
    68
lemma cmod_mult: "cmod (z*w) = cmod z * cmod w"
chaieb@26123
    69
proof-
chaieb@26123
    70
  from rcis_Ex[of z] rcis_Ex[of w]
chaieb@26123
    71
  obtain rz az rw aw where z: "z = rcis rz az" and w: "w = rcis rw aw"  by blast
chaieb@26123
    72
  thus ?thesis by (simp add: rcis_mult abs_mult)
chaieb@26123
    73
qed
chaieb@26123
    74
chaieb@26123
    75
lemma cmod_divide: "cmod (z/w) = cmod z / cmod w"
chaieb@26123
    76
proof-
chaieb@26123
    77
  from rcis_Ex[of z] rcis_Ex[of w]
chaieb@26123
    78
  obtain rz az rw aw where z: "z = rcis rz az" and w: "w = rcis rw aw"  by blast
chaieb@26123
    79
  thus ?thesis by (simp add: rcis_divide)
chaieb@26123
    80
qed
chaieb@26123
    81
chaieb@26123
    82
lemma cmod_inverse: "cmod (inverse z) = inverse (cmod z)"
chaieb@26123
    83
  using cmod_divide[of 1 z] by (simp add: inverse_eq_divide)
chaieb@26123
    84
chaieb@26123
    85
lemma cmod_uminus: "cmod (- z) = cmod z"
chaieb@26123
    86
  unfolding cmod_def by simp
chaieb@26123
    87
lemma cmod_abs_norm: "\<bar>cmod w - cmod z\<bar> \<le> cmod (w - z)"
chaieb@26123
    88
proof-
chaieb@26123
    89
  have ath: "\<And>(a::real) b x. a - b <= x \<Longrightarrow> b - a <= x ==> abs(a - b) <= x"
chaieb@26123
    90
    by arith
chaieb@26123
    91
  from complex_mod_triangle_ineq2[of "w - z" z]
chaieb@26123
    92
  have th1: "cmod w - cmod z \<le> cmod (w - z)" by simp
chaieb@26123
    93
  from complex_mod_triangle_ineq2[of "- (w - z)" "w"] 
chaieb@26123
    94
  have th2: "cmod z - cmod w \<le> cmod (w - z)" using cmod_uminus [of "w - z"]
chaieb@26123
    95
    by simp
chaieb@26123
    96
  from ath[OF th1 th2] show ?thesis .
chaieb@26123
    97
qed
chaieb@26123
    98
chaieb@26123
    99
lemma cmod_power: "cmod (z ^n) = cmod z ^ n" by (induct n, auto simp add: cmod_mult)
chaieb@26123
   100
lemma real_down2: "(0::real) < d1 \<Longrightarrow> 0 < d2 ==> EX e. 0 < e & e < d1 & e < d2"
chaieb@26123
   101
  apply ferrack apply arith done
chaieb@26123
   102
chaieb@26123
   103
lemma cmod_complex_of_real: "cmod (complex_of_real x) = \<bar>x\<bar>"
chaieb@26123
   104
  unfolding cmod_def by auto
chaieb@26123
   105
chaieb@26123
   106
chaieb@26123
   107
text{* The triangle inequality for cmod *}
chaieb@26123
   108
lemma complex_mod_triangle_sub: "cmod w \<le> cmod (w + z) + norm z"
chaieb@26123
   109
  using complex_mod_triangle_ineq2[of "w + z" "-z"] by auto
chaieb@26123
   110
chaieb@26123
   111
section{* Basic lemmas about complex polynomials *}
chaieb@26123
   112
chaieb@26123
   113
lemma poly_bound_exists:
chaieb@26123
   114
  shows "\<exists>m. m > 0 \<and> (\<forall>z. cmod z <= r \<longrightarrow> cmod (poly p z) \<le> m)"
chaieb@26123
   115
proof(induct p)
chaieb@26123
   116
  case Nil thus ?case by (rule exI[where x=1], simp) 
chaieb@26123
   117
next
chaieb@26123
   118
  case (Cons c cs)
chaieb@26123
   119
  from Cons.hyps obtain m where m: "\<forall>z. cmod z \<le> r \<longrightarrow> cmod (poly cs z) \<le> m"
chaieb@26123
   120
    by blast
chaieb@26123
   121
  let ?k = " 1 + cmod c + \<bar>r * m\<bar>"
chaieb@26123
   122
  have kp: "?k > 0" using abs_ge_zero[of "r*m"] cmod_pos[of c] by arith
chaieb@26123
   123
  {fix z
chaieb@26123
   124
    assume H: "cmod z \<le> r"
chaieb@26123
   125
    from m H have th: "cmod (poly cs z) \<le> m" by blast
chaieb@26123
   126
    from H have rp: "r \<ge> 0" using cmod_pos[of z] by arith
chaieb@26123
   127
    have "cmod (poly (c # cs) z) \<le> cmod c + cmod (z* poly cs z)"
chaieb@26123
   128
      using complex_mod_triangle_ineq[of c "z* poly cs z"] by simp
chaieb@26123
   129
    also have "\<dots> \<le> cmod c + r*m" using mult_mono[OF H th rp cmod_pos[of "poly cs z"]] by (simp add: cmod_mult)
chaieb@26123
   130
    also have "\<dots> \<le> ?k" by simp
chaieb@26123
   131
    finally have "cmod (poly (c # cs) z) \<le> ?k" .}
chaieb@26123
   132
  with kp show ?case by blast
chaieb@26123
   133
qed
chaieb@26123
   134
chaieb@26123
   135
chaieb@26123
   136
text{* Offsetting the variable in a polynomial gives another of same degree *}
wenzelm@26135
   137
  (* FIXME : Lemma holds also in locale --- fix it later *)
chaieb@26123
   138
lemma  poly_offset_lemma:
chaieb@26123
   139
  shows "\<exists>b q. (length q = length p) \<and> (\<forall>x. poly (b#q) (x::complex) = (a + x) * poly p x)"
chaieb@26123
   140
proof(induct p)
chaieb@26123
   141
  case Nil thus ?case by simp
chaieb@26123
   142
next
chaieb@26123
   143
  case (Cons c cs)
chaieb@26123
   144
  from Cons.hyps obtain b q where 
chaieb@26123
   145
    bq: "length q = length cs" "\<forall>x. poly (b # q) x = (a + x) * poly cs x"
chaieb@26123
   146
    by blast
chaieb@26123
   147
  let ?b = "a*c"
chaieb@26123
   148
  let ?q = "(b+c)#q"
chaieb@26123
   149
  have lg: "length ?q = length (c#cs)" using bq(1) by simp
chaieb@26123
   150
  {fix x
chaieb@26123
   151
    from bq(2)[rule_format, of x]
chaieb@26123
   152
    have "x*poly (b # q) x = x*((a + x) * poly cs x)" by simp
chaieb@26123
   153
    hence "poly (?b# ?q) x = (a + x) * poly (c # cs) x"
chaieb@26123
   154
      by (simp add: ring_simps)}
chaieb@26123
   155
  with lg  show ?case by blast 
chaieb@26123
   156
qed
chaieb@26123
   157
chaieb@26123
   158
    (* FIXME : This one too*)
chaieb@26123
   159
lemma poly_offset: "\<exists> q. length q = length p \<and> (\<forall>x. poly q (x::complex) = poly p (a + x))"
chaieb@26123
   160
proof (induct p)
chaieb@26123
   161
  case Nil thus ?case by simp
chaieb@26123
   162
next
chaieb@26123
   163
  case (Cons c cs)
chaieb@26123
   164
  from Cons.hyps obtain q where q: "length q = length cs" "\<forall>x. poly q x = poly cs (a + x)" by blast
chaieb@26123
   165
  from poly_offset_lemma[of q a] obtain b p where 
chaieb@26123
   166
    bp: "length p = length q" "\<forall>x. poly (b # p) x = (a + x) * poly q x"
chaieb@26123
   167
    by blast
chaieb@26123
   168
  thus ?case using q bp by - (rule exI[where x="(c + b)#p"], simp)
chaieb@26123
   169
qed
chaieb@26123
   170
chaieb@26123
   171
text{* An alternative useful formulation of completeness of the reals *}
chaieb@26123
   172
lemma real_sup_exists: assumes ex: "\<exists>x. P x" and bz: "\<exists>z. \<forall>x. P x \<longrightarrow> x < z"
chaieb@26123
   173
  shows "\<exists>(s::real). \<forall>y. (\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < s"
chaieb@26123
   174
proof-
chaieb@26123
   175
  from ex bz obtain x Y where x: "P x" and Y: "\<And>x. P x \<Longrightarrow> x < Y"  by blast
chaieb@26123
   176
  from ex have thx:"\<exists>x. x \<in> Collect P" by blast
chaieb@26123
   177
  from bz have thY: "\<exists>Y. isUb UNIV (Collect P) Y" 
chaieb@26123
   178
    by(auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def order_le_less)
chaieb@26123
   179
  from reals_complete[OF thx thY] obtain L where L: "isLub UNIV (Collect P) L"
chaieb@26123
   180
    by blast
chaieb@26123
   181
  from Y[OF x] have xY: "x < Y" .
chaieb@26123
   182
  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)  
chaieb@26123
   183
  from Y have Y': "\<forall>x. P x \<longrightarrow> x \<le> Y" 
chaieb@26123
   184
    apply (clarsimp, atomize (full)) by auto 
chaieb@26123
   185
  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
   186
  {fix y
chaieb@26123
   187
    {fix z assume z: "P z" "y < z"
chaieb@26123
   188
      from L' z have "y < L" by auto }
chaieb@26123
   189
    moreover
chaieb@26123
   190
    {assume yL: "y < L" "\<forall>z. P z \<longrightarrow> \<not> y < z"
chaieb@26123
   191
      hence nox: "\<forall>z. P z \<longrightarrow> y \<ge> z" by auto
chaieb@26123
   192
      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
   193
      with yL(1) have False  by arith}
chaieb@26123
   194
    ultimately have "(\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < L" by blast}
chaieb@26123
   195
  thus ?thesis by blast
chaieb@26123
   196
qed
chaieb@26123
   197
chaieb@26123
   198
chaieb@26123
   199
section{* Some theorems about Sequences*}
chaieb@26123
   200
text{* Given a binary function @{text "f:: nat \<Rightarrow> 'a \<Rightarrow> 'a"}, its values are uniquely determined by a function g *}
chaieb@26123
   201
chaieb@26123
   202
lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
chaieb@26123
   203
  unfolding Ex1_def
chaieb@26123
   204
  apply (rule_tac x="nat_rec e f" in exI)
chaieb@26123
   205
  apply (rule conjI)+
chaieb@26123
   206
apply (rule def_nat_rec_0, simp)
chaieb@26123
   207
apply (rule allI, rule def_nat_rec_Suc, simp)
chaieb@26123
   208
apply (rule allI, rule impI, rule ext)
chaieb@26123
   209
apply (erule conjE)
chaieb@26123
   210
apply (induct_tac x)
chaieb@26123
   211
apply (simp add: nat_rec_0)
chaieb@26123
   212
apply (erule_tac x="n" in allE)
chaieb@26123
   213
apply (simp)
chaieb@26123
   214
done
chaieb@26123
   215
chaieb@26123
   216
 text{* An equivalent formulation of monotony -- Not used here, but might be useful *}
chaieb@26123
   217
lemma mono_Suc: "mono f = (\<forall>n. (f n :: 'a :: order) \<le> f (Suc n))"
chaieb@26123
   218
unfolding mono_def
chaieb@26123
   219
proof auto
chaieb@26123
   220
  fix A B :: nat
chaieb@26123
   221
  assume H: "\<forall>n. f n \<le> f (Suc n)" "A \<le> B"
chaieb@26123
   222
  hence "\<exists>k. B = A + k" apply -  apply (thin_tac "\<forall>n. f n \<le> f (Suc n)") 
chaieb@26123
   223
    by presburger
chaieb@26123
   224
  then obtain k where k: "B = A + k" by blast
chaieb@26123
   225
  {fix a k
chaieb@26123
   226
    have "f a \<le> f (a + k)"
chaieb@26123
   227
    proof (induct k)
chaieb@26123
   228
      case 0 thus ?case by simp
chaieb@26123
   229
    next
chaieb@26123
   230
      case (Suc k)
chaieb@26123
   231
      from Suc.hyps H(1)[rule_format, of "a + k"] show ?case by simp
chaieb@26123
   232
    qed}
chaieb@26123
   233
  with k show "f A \<le> f B" by blast
chaieb@26123
   234
qed
chaieb@26123
   235
chaieb@26123
   236
text{* for any sequence, there is a mootonic subsequence *}
chaieb@26123
   237
lemma seq_monosub: "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
chaieb@26123
   238
proof-
chaieb@26123
   239
  {assume H: "\<forall>n. \<exists>p >n. \<forall> m\<ge>p. s m \<le> s p"
chaieb@26123
   240
    let ?P = "\<lambda> p n. p > n \<and> (\<forall>m \<ge> p. s m \<le> s p)"
chaieb@26123
   241
    from num_Axiom[of "SOME p. ?P p 0" "\<lambda>p n. SOME p. ?P p n"]
chaieb@26123
   242
    obtain f where f: "f 0 = (SOME p. ?P p 0)" "\<forall>n. f (Suc n) = (SOME p. ?P p (f n))" by blast
chaieb@26123
   243
    have "?P (f 0) 0"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p 0"]
chaieb@26123
   244
      using H apply - 
chaieb@26123
   245
      apply (erule allE[where x=0], erule exE, rule_tac x="p" in exI) 
chaieb@26123
   246
      unfolding order_le_less by blast 
chaieb@26123
   247
    hence f0: "f 0 > 0" "\<forall>m \<ge> f 0. s m \<le> s (f 0)" by blast+
chaieb@26123
   248
    {fix n
chaieb@26123
   249
      have "?P (f (Suc n)) (f n)" 
chaieb@26123
   250
	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
chaieb@26123
   251
	using H apply - 
chaieb@26123
   252
      apply (erule allE[where x="f n"], erule exE, rule_tac x="p" in exI) 
chaieb@26123
   253
      unfolding order_le_less by blast 
chaieb@26123
   254
    hence "f (Suc n) > f n" "\<forall>m \<ge> f (Suc n). s m \<le> s (f (Suc n))" by blast+}
chaieb@26123
   255
  note fSuc = this
chaieb@26123
   256
    {fix p q assume pq: "p \<ge> f q"
chaieb@26123
   257
      have "s p \<le> s(f(q))"  using f0(2)[rule_format, of p] pq fSuc
chaieb@26123
   258
	by (cases q, simp_all) }
chaieb@26123
   259
    note pqth = this
chaieb@26123
   260
    {fix q
chaieb@26123
   261
      have "f (Suc q) > f q" apply (induct q) 
chaieb@26123
   262
	using f0(1) fSuc(1)[of 0] apply simp by (rule fSuc(1))}
chaieb@26123
   263
    note fss = this
chaieb@26123
   264
    from fss have th1: "subseq f" unfolding subseq_Suc_iff ..
chaieb@26123
   265
    {fix a b 
chaieb@26123
   266
      have "f a \<le> f (a + b)"
chaieb@26123
   267
      proof(induct b)
chaieb@26123
   268
	case 0 thus ?case by simp
chaieb@26123
   269
      next
chaieb@26123
   270
	case (Suc b)
chaieb@26123
   271
	from fSuc(1)[of "a + b"] Suc.hyps show ?case by simp
chaieb@26123
   272
      qed}
chaieb@26123
   273
    note fmon0 = this
chaieb@26123
   274
    have "monoseq (\<lambda>n. s (f n))" 
chaieb@26123
   275
    proof-
chaieb@26123
   276
      {fix n
chaieb@26123
   277
	have "s (f n) \<ge> s (f (Suc n))" 
chaieb@26123
   278
	proof(cases n)
chaieb@26123
   279
	  case 0
chaieb@26123
   280
	  assume n0: "n = 0"
chaieb@26123
   281
	  from fSuc(1)[of 0] have th0: "f 0 \<le> f (Suc 0)" by simp
chaieb@26123
   282
	  from f0(2)[rule_format, OF th0] show ?thesis  using n0 by simp
chaieb@26123
   283
	next
chaieb@26123
   284
	  case (Suc m)
chaieb@26123
   285
	  assume m: "n = Suc m"
chaieb@26123
   286
	  from fSuc(1)[of n] m have th0: "f (Suc m) \<le> f (Suc (Suc m))" by simp
chaieb@26123
   287
	  from m fSuc(2)[rule_format, OF th0] show ?thesis by simp 
chaieb@26123
   288
	qed}
chaieb@26123
   289
      thus "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc by blast 
chaieb@26123
   290
    qed
chaieb@26123
   291
    with th1 have ?thesis by blast}
chaieb@26123
   292
  moreover
chaieb@26123
   293
  {fix N assume N: "\<forall>p >N. \<exists> m\<ge>p. s m > s p"
chaieb@26123
   294
    {fix p assume p: "p \<ge> Suc N" 
chaieb@26123
   295
      hence pN: "p > N" by arith with N obtain m where m: "m \<ge> p" "s m > s p" by blast
chaieb@26123
   296
      have "m \<noteq> p" using m(2) by auto 
chaieb@26123
   297
      with m have "\<exists>m>p. s p < s m" by - (rule exI[where x=m], auto)}
chaieb@26123
   298
    note th0 = this
chaieb@26123
   299
    let ?P = "\<lambda>m x. m > x \<and> s x < s m"
chaieb@26123
   300
    from num_Axiom[of "SOME x. ?P x (Suc N)" "\<lambda>m x. SOME y. ?P y x"]
chaieb@26123
   301
    obtain f where f: "f 0 = (SOME x. ?P x (Suc N))" 
chaieb@26123
   302
      "\<forall>n. f (Suc n) = (SOME m. ?P m (f n))" by blast
chaieb@26123
   303
    have "?P (f 0) (Suc N)"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p (Suc N)"]
chaieb@26123
   304
      using N apply - 
chaieb@26123
   305
      apply (erule allE[where x="Suc N"], clarsimp)
chaieb@26123
   306
      apply (rule_tac x="m" in exI)
chaieb@26123
   307
      apply auto
chaieb@26123
   308
      apply (subgoal_tac "Suc N \<noteq> m")
chaieb@26123
   309
      apply simp
chaieb@26123
   310
      apply (rule ccontr, simp)
chaieb@26123
   311
      done
chaieb@26123
   312
    hence f0: "f 0 > Suc N" "s (Suc N) < s (f 0)" by blast+
chaieb@26123
   313
    {fix n
chaieb@26123
   314
      have "f n > N \<and> ?P (f (Suc n)) (f n)"
chaieb@26123
   315
	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
chaieb@26123
   316
      proof (induct n)
chaieb@26123
   317
	case 0 thus ?case
chaieb@26123
   318
	  using f0 N apply auto 
chaieb@26123
   319
	  apply (erule allE[where x="f 0"], clarsimp) 
chaieb@26123
   320
	  apply (rule_tac x="m" in exI, simp)
chaieb@26123
   321
	  by (subgoal_tac "f 0 \<noteq> m", auto)
chaieb@26123
   322
      next
chaieb@26123
   323
	case (Suc n)
chaieb@26123
   324
	from Suc.hyps have Nfn: "N < f n" by blast
chaieb@26123
   325
	from Suc.hyps obtain m where m: "m > f n" "s (f n) < s m" by blast
chaieb@26123
   326
	with Nfn have mN: "m > N" by arith
chaieb@26123
   327
	note key = Suc.hyps[unfolded some_eq_ex[of "\<lambda>p. ?P p (f n)", symmetric] f(2)[rule_format, of n, symmetric]]
chaieb@26123
   328
	
chaieb@26123
   329
	from key have th0: "f (Suc n) > N" by simp
chaieb@26123
   330
	from N[rule_format, OF th0]
chaieb@26123
   331
	obtain m' where m': "m' \<ge> f (Suc n)" "s (f (Suc n)) < s m'" by blast
chaieb@26123
   332
	have "m' \<noteq> f (Suc (n))" apply (rule ccontr) using m'(2) by auto
chaieb@26123
   333
	hence "m' > f (Suc n)" using m'(1) by simp
chaieb@26123
   334
	with key m'(2) show ?case by auto
chaieb@26123
   335
      qed}
chaieb@26123
   336
    note fSuc = this
chaieb@26123
   337
    {fix n
chaieb@26123
   338
      have "f n \<ge> Suc N \<and> f(Suc n) > f n \<and> s(f n) < s(f(Suc n))" using fSuc[of n] by auto 
chaieb@26123
   339
      hence "f n \<ge> Suc N" "f(Suc n) > f n" "s(f n) < s(f(Suc n))" by blast+}
chaieb@26123
   340
    note thf = this
chaieb@26123
   341
    have sqf: "subseq f" unfolding subseq_Suc_iff using thf by simp
chaieb@26123
   342
    have "monoseq (\<lambda>n. s (f n))"  unfolding monoseq_Suc using thf
chaieb@26123
   343
      apply -
chaieb@26123
   344
      apply (rule disjI1)
chaieb@26123
   345
      apply auto
chaieb@26123
   346
      apply (rule order_less_imp_le)
chaieb@26123
   347
      apply blast
chaieb@26123
   348
      done
chaieb@26123
   349
    then have ?thesis  using sqf by blast}
chaieb@26123
   350
  ultimately show ?thesis unfolding linorder_not_less[symmetric] by blast
chaieb@26123
   351
qed
chaieb@26123
   352
chaieb@26123
   353
lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
chaieb@26123
   354
proof(induct n)
chaieb@26123
   355
  case 0 thus ?case by simp
chaieb@26123
   356
next
chaieb@26123
   357
  case (Suc n)
chaieb@26123
   358
  from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
chaieb@26123
   359
  have "n < f (Suc n)" by arith 
chaieb@26123
   360
  thus ?case by arith
chaieb@26123
   361
qed
chaieb@26123
   362
chaieb@26123
   363
section {* Fundamental theorem of algebra *}
chaieb@26123
   364
lemma  unimodular_reduce_norm:
chaieb@26123
   365
  assumes md: "cmod z = 1"
chaieb@26123
   366
  shows "cmod (z + 1) < 1 \<or> cmod (z - 1) < 1 \<or> cmod (z + ii) < 1 \<or> cmod (z - ii) < 1"
chaieb@26123
   367
proof-
chaieb@26123
   368
  obtain x y where z: "z = Complex x y " by (cases z, auto)
chaieb@26123
   369
  from md z have xy: "x^2 + y^2 = 1" by (simp add: cmod_def)
chaieb@26123
   370
  {assume C: "cmod (z + 1) \<ge> 1" "cmod (z - 1) \<ge> 1" "cmod (z + ii) \<ge> 1" "cmod (z - ii) \<ge> 1"
chaieb@26123
   371
    from C z xy have "2*x \<le> 1" "2*x \<ge> -1" "2*y \<le> 1" "2*y \<ge> -1"
chaieb@26123
   372
      by (simp_all add: cmod_def power2_eq_square ring_simps)
chaieb@26123
   373
    hence "abs (2*x) \<le> 1" "abs (2*y) \<le> 1" by simp_all
chaieb@26123
   374
    hence "(abs (2 * x))^2 <= 1^2" "(abs (2 * y)) ^2 <= 1^2"
chaieb@26123
   375
      by - (rule power_mono, simp, simp)+
chaieb@26123
   376
    hence th0: "4*x^2 \<le> 1" "4*y^2 \<le> 1" 
chaieb@26123
   377
      by (simp_all  add: power2_abs power_mult_distrib)
chaieb@26123
   378
    from add_mono[OF th0] xy have False by simp }
chaieb@26123
   379
  thus ?thesis unfolding linorder_not_le[symmetric] by blast
chaieb@26123
   380
qed
chaieb@26123
   381
wenzelm@26135
   382
text{* Hence we can always reduce modulus of @{text "1 + b z^n"} if nonzero *}
chaieb@26123
   383
lemma reduce_poly_simple:
chaieb@26123
   384
 assumes b: "b \<noteq> 0" and n: "n\<noteq>0"
chaieb@26123
   385
  shows "\<exists>z. cmod (1 + b * z^n) < 1"
chaieb@26123
   386
using n
chaieb@26123
   387
proof(induct n rule: nat_less_induct)
chaieb@26123
   388
  fix n
chaieb@26123
   389
  assume IH: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>z. cmod (1 + b * z ^ m) < 1)" and n: "n \<noteq> 0"
chaieb@26123
   390
  let ?P = "\<lambda>z n. cmod (1 + b * z ^ n) < 1"
chaieb@26123
   391
  {assume e: "even n"
chaieb@26123
   392
    hence "\<exists>m. n = 2*m" by presburger
chaieb@26123
   393
    then obtain m where m: "n = 2*m" by blast
chaieb@26123
   394
    from n m have "m\<noteq>0" "m < n" by presburger+
chaieb@26123
   395
    with IH[rule_format, of m] obtain z where z: "?P z m" by blast
chaieb@26123
   396
    from z have "?P (csqrt z) n" by (simp add: m power_mult csqrt)
chaieb@26123
   397
    hence "\<exists>z. ?P z n" ..}
chaieb@26123
   398
  moreover
chaieb@26123
   399
  {assume o: "odd n"
chaieb@26123
   400
    from b have b': "b^2 \<noteq> 0" unfolding power2_eq_square by simp
chaieb@26123
   401
    have "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
chaieb@26123
   402
    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) = 
chaieb@26123
   403
    ((Re (inverse b))^2 + (Im (inverse b))^2) * \<bar>Im b * Im b + Re b * Re b\<bar>" by algebra
chaieb@26123
   404
    also have "\<dots> = cmod (inverse b) ^2 * cmod b ^ 2" 
chaieb@26123
   405
      apply (simp add: cmod_def) using realpow_two_le_add_order[of "Re b" "Im b"]
chaieb@26123
   406
      by (simp add: power2_eq_square)
chaieb@26123
   407
    finally 
chaieb@26123
   408
    have th0: "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
chaieb@26123
   409
    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) =
chaieb@26123
   410
    1" 
chaieb@26123
   411
      apply (simp add: power2_eq_square cmod_mult[symmetric] cmod_inverse[symmetric])
chaieb@26123
   412
      using right_inverse[OF b']
chaieb@26123
   413
      by (simp add: power2_eq_square[symmetric] power_inverse[symmetric] ring_simps)
chaieb@26123
   414
    have th0: "cmod (complex_of_real (cmod b) / b) = 1"
chaieb@26123
   415
      apply (simp add: complex_Re_mult cmod_def power2_eq_square Re_complex_of_real Im_complex_of_real divide_inverse ring_simps )
chaieb@26123
   416
      by (simp add: real_sqrt_mult[symmetric] th0)        
chaieb@26123
   417
    from o have "\<exists>m. n = Suc (2*m)" by presburger+
chaieb@26123
   418
    then obtain m where m: "n = Suc (2*m)" by blast
chaieb@26123
   419
    from unimodular_reduce_norm[OF th0] o
chaieb@26123
   420
    have "\<exists>v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
chaieb@26123
   421
      apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1", rule_tac x="1" in exI, simp)
chaieb@26123
   422
      apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1", rule_tac x="-1" in exI, simp add: diff_def)
chaieb@26123
   423
      apply (cases "cmod (complex_of_real (cmod b) / b + ii) < 1")
chaieb@26123
   424
      apply (cases "even m", rule_tac x="ii" in exI, simp add: m power_mult)
chaieb@26123
   425
      apply (rule_tac x="- ii" in exI, simp add: m power_mult)
chaieb@26123
   426
      apply (cases "even m", rule_tac x="- ii" in exI, simp add: m power_mult diff_def)
chaieb@26123
   427
      apply (rule_tac x="ii" in exI, simp add: m power_mult diff_def)
chaieb@26123
   428
      done
chaieb@26123
   429
    then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1" by blast
chaieb@26123
   430
    let ?w = "v / complex_of_real (root n (cmod b))"
chaieb@26123
   431
    from odd_real_root_pow[OF o, of "cmod b"]
chaieb@26123
   432
    have th1: "?w ^ n = v^n / complex_of_real (cmod b)" 
chaieb@26123
   433
      by (simp add: power_divide complex_of_real_power)
chaieb@26123
   434
    have th2:"cmod (complex_of_real (cmod b) / b) = 1" using b by (simp add: cmod_divide)
chaieb@26123
   435
    hence th3: "cmod (complex_of_real (cmod b) / b) \<ge> 0" by simp
chaieb@26123
   436
    have th4: "cmod (complex_of_real (cmod b) / b) *
chaieb@26123
   437
   cmod (1 + b * (v ^ n / complex_of_real (cmod b)))
chaieb@26123
   438
   < cmod (complex_of_real (cmod b) / b) * 1"
chaieb@26123
   439
      apply (simp only: cmod_mult[symmetric] right_distrib)
chaieb@26123
   440
      using b v by (simp add: th2)
chaieb@26123
   441
chaieb@26123
   442
    from mult_less_imp_less_left[OF th4 th3]
chaieb@26123
   443
    have "?P ?w n" unfolding th1 . 
chaieb@26123
   444
    hence "\<exists>z. ?P z n" .. }
chaieb@26123
   445
  ultimately show "\<exists>z. ?P z n" by blast
chaieb@26123
   446
qed
chaieb@26123
   447
chaieb@26123
   448
chaieb@26123
   449
text{* Bolzano-Weierstrass type property for closed disc in complex plane. *}
chaieb@26123
   450
chaieb@26123
   451
lemma metric_bound_lemma: "cmod (x - y) <= \<bar>Re x - Re y\<bar> + \<bar>Im x - Im y\<bar>"
chaieb@26123
   452
  using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y" ]
chaieb@26123
   453
  unfolding cmod_def by simp
chaieb@26123
   454
chaieb@26123
   455
lemma bolzano_weierstrass_complex_disc:
chaieb@26123
   456
  assumes r: "\<forall>n. cmod (s n) \<le> r"
chaieb@26123
   457
  shows "\<exists>f z. subseq f \<and> (\<forall>e >0. \<exists>N. \<forall>n \<ge> N. cmod (s (f n) - z) < e)"
chaieb@26123
   458
proof-
chaieb@26123
   459
  from seq_monosub[of "Re o s"] 
chaieb@26123
   460
  obtain f g where f: "subseq f" "monoseq (\<lambda>n. Re (s (f n)))" 
chaieb@26123
   461
    unfolding o_def by blast
chaieb@26123
   462
  from seq_monosub[of "Im o s o f"] 
chaieb@26123
   463
  obtain g where g: "subseq g" "monoseq (\<lambda>n. Im (s(f(g n))))" unfolding o_def by blast  
chaieb@26123
   464
  let ?h = "f o g"
chaieb@26123
   465
  from r[rule_format, of 0] have rp: "r \<ge> 0" using cmod_pos[of "s 0"] by arith 
chaieb@26123
   466
  have th:"\<forall>n. r + 1 \<ge> \<bar> Re (s n)\<bar>" 
chaieb@26123
   467
  proof
chaieb@26123
   468
    fix n
chaieb@26123
   469
    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
   470
  qed
chaieb@26123
   471
  have conv1: "convergent (\<lambda>n. Re (s ( f n)))"
chaieb@26123
   472
    apply (rule Bseq_monoseq_convergent)
chaieb@26123
   473
    apply (simp add: Bseq_def)
chaieb@26123
   474
    apply (rule exI[where x= "r + 1"])
chaieb@26123
   475
    using th rp apply simp
chaieb@26123
   476
    using f(2) .
chaieb@26123
   477
  have th:"\<forall>n. r + 1 \<ge> \<bar> Im (s n)\<bar>" 
chaieb@26123
   478
  proof
chaieb@26123
   479
    fix n
chaieb@26123
   480
    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
   481
  qed
chaieb@26123
   482
chaieb@26123
   483
  have conv2: "convergent (\<lambda>n. Im (s (f (g n))))"
chaieb@26123
   484
    apply (rule Bseq_monoseq_convergent)
chaieb@26123
   485
    apply (simp add: Bseq_def)
chaieb@26123
   486
    apply (rule exI[where x= "r + 1"])
chaieb@26123
   487
    using th rp apply simp
chaieb@26123
   488
    using g(2) .
chaieb@26123
   489
chaieb@26123
   490
  from conv1[unfolded convergent_def] obtain x where "LIMSEQ (\<lambda>n. Re (s (f n))) x" 
chaieb@26123
   491
    by blast 
chaieb@26123
   492
  hence  x: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Re (s (f n)) - x \<bar> < r" 
chaieb@26123
   493
    unfolding LIMSEQ_def real_norm_def .
chaieb@26123
   494
chaieb@26123
   495
  from conv2[unfolded convergent_def] obtain y where "LIMSEQ (\<lambda>n. Im (s (f (g n)))) y" 
chaieb@26123
   496
    by blast 
chaieb@26123
   497
  hence  y: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Im (s (f (g n))) - y \<bar> < r" 
chaieb@26123
   498
    unfolding LIMSEQ_def real_norm_def .
chaieb@26123
   499
  let ?w = "Complex x y"
chaieb@26123
   500
  from f(1) g(1) have hs: "subseq ?h" unfolding subseq_def by auto 
chaieb@26123
   501
  {fix e assume ep: "e > (0::real)"
chaieb@26123
   502
    hence e2: "e/2 > 0" by simp
chaieb@26123
   503
    from x[rule_format, OF e2] y[rule_format, OF e2]
chaieb@26123
   504
    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
   505
    {fix n assume nN12: "n \<ge> N1 + N2"
chaieb@26123
   506
      hence nN1: "g n \<ge> N1" and nN2: "n \<ge> N2" using seq_suble[OF g(1), of n] by arith+
chaieb@26123
   507
      from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
chaieb@26123
   508
      have "cmod (s (?h n) - ?w) < e" 
chaieb@26123
   509
	using metric_bound_lemma[of "s (f (g n))" ?w] by simp }
chaieb@26123
   510
    hence "\<exists>N. \<forall>n\<ge>N. cmod (s (?h n) - ?w) < e" by blast }
chaieb@26123
   511
  with hs show ?thesis  by blast  
chaieb@26123
   512
qed
chaieb@26123
   513
chaieb@26123
   514
text{* Polynomial is continuous. *}
chaieb@26123
   515
chaieb@26123
   516
lemma poly_cont:
chaieb@26123
   517
  assumes ep: "e > 0" 
chaieb@26123
   518
  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
   519
proof-
chaieb@26123
   520
  from poly_offset[of p z] obtain q where q: "length q = length p" "\<And>x. poly q x = poly p (z + x)" by blast
chaieb@26123
   521
  {fix w
chaieb@26123
   522
    note q(2)[of "w - z", simplified]}
chaieb@26123
   523
  note th = this
chaieb@26123
   524
  show ?thesis unfolding th[symmetric]
chaieb@26123
   525
  proof(induct q)
chaieb@26123
   526
    case Nil thus ?case  using ep by auto
chaieb@26123
   527
  next
chaieb@26123
   528
    case (Cons c cs)
chaieb@26123
   529
    from poly_bound_exists[of 1 "cs"] 
chaieb@26123
   530
    obtain m where m: "m > 0" "\<And>z. cmod z \<le> 1 \<Longrightarrow> cmod (poly cs z) \<le> m" by blast
chaieb@26123
   531
    from ep m(1) have em0: "e/m > 0" by (simp add: field_simps)
chaieb@26123
   532
    have one0: "1 > (0::real)"  by arith
chaieb@26123
   533
    from real_lbound_gt_zero[OF one0 em0] 
chaieb@26123
   534
    obtain d where d: "d >0" "d < 1" "d < e / m" by blast
chaieb@26123
   535
    from d(1,3) m(1) have dm: "d*m > 0" "d*m < e" 
chaieb@26123
   536
      by (simp_all add: field_simps real_mult_order)
chaieb@26123
   537
    show ?case 
chaieb@26123
   538
      proof(rule ex_forward[OF real_lbound_gt_zero[OF one0 em0]], clarsimp simp add: cmod_mult)
chaieb@26123
   539
	fix d w
chaieb@26123
   540
	assume H: "d > 0" "d < 1" "d < e/m" "w\<noteq>z" "cmod (w-z) < d"
chaieb@26123
   541
	hence d1: "cmod (w-z) \<le> 1" "d \<ge> 0" by simp_all
chaieb@26123
   542
	from H(3) m(1) have dme: "d*m < e" by (simp add: field_simps)
chaieb@26123
   543
	from H have th: "cmod (w-z) \<le> d" by simp 
chaieb@26123
   544
	from mult_mono[OF th m(2)[OF d1(1)] d1(2) cmod_pos] dme
chaieb@26123
   545
	show "cmod (w - z) * cmod (poly cs (w - z)) < e" by simp
chaieb@26123
   546
      qed  
chaieb@26123
   547
    qed
chaieb@26123
   548
qed
chaieb@26123
   549
chaieb@26123
   550
text{* Hence a polynomial attains minimum on a closed disc 
chaieb@26123
   551
  in the complex plane. *}
chaieb@26123
   552
lemma  poly_minimum_modulus_disc:
chaieb@26123
   553
  "\<exists>z. \<forall>w. cmod w \<le> r \<longrightarrow> cmod (poly p z) \<le> cmod (poly p w)"
chaieb@26123
   554
proof-
chaieb@26123
   555
  {assume "\<not> r \<ge> 0" hence ?thesis unfolding linorder_not_le
chaieb@26123
   556
      apply -
chaieb@26123
   557
      apply (rule exI[where x=0]) 
chaieb@26123
   558
      apply auto
chaieb@26123
   559
      apply (subgoal_tac "cmod w < 0")
chaieb@26123
   560
      apply simp
chaieb@26123
   561
      apply arith
chaieb@26123
   562
      done }
chaieb@26123
   563
  moreover
chaieb@26123
   564
  {assume rp: "r \<ge> 0"
chaieb@26123
   565
    from rp have "cmod 0 \<le> r \<and> cmod (poly p 0) = - (- cmod (poly p 0))" by simp 
chaieb@26123
   566
    hence mth1: "\<exists>x z. cmod z \<le> r \<and> cmod (poly p z) = - x"  by blast
chaieb@26123
   567
    {fix x z
chaieb@26123
   568
      assume H: "cmod z \<le> r" "cmod (poly p z) = - x" "\<not>x < 1"
chaieb@26123
   569
      hence "- x < 0 " by arith
chaieb@26123
   570
      with H(2) cmod_pos[of "poly p z"]  have False by simp }
chaieb@26123
   571
    then have mth2: "\<exists>z. \<forall>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<longrightarrow> x < z" by blast
chaieb@26123
   572
    from real_sup_exists[OF mth1 mth2] obtain s where 
chaieb@26123
   573
      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
   574
    let ?m = "-s"
chaieb@26123
   575
    {fix y
chaieb@26123
   576
      from s[rule_format, of "-y"] have 
chaieb@26123
   577
    "(\<exists>z x. cmod z \<le> r \<and> -(- cmod (poly p z)) < y) \<longleftrightarrow> ?m < y" 
chaieb@26123
   578
	unfolding minus_less_iff[of y ] equation_minus_iff by blast }
chaieb@26123
   579
    note s1 = this[unfolded minus_minus]
chaieb@26123
   580
    from s1[of ?m] have s1m: "\<And>z x. cmod z \<le> r \<Longrightarrow> cmod (poly p z) \<ge> ?m" 
chaieb@26123
   581
      by auto
chaieb@26123
   582
    {fix n::nat
chaieb@26123
   583
      from s1[rule_format, of "?m + 1/real (Suc n)"] 
chaieb@26123
   584
      have "\<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)"
chaieb@26123
   585
	by simp}
chaieb@26123
   586
    hence th: "\<forall>n. \<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)" ..
chaieb@26123
   587
    from choice[OF th] obtain g where 
chaieb@26123
   588
      g: "\<forall>n. cmod (g n) \<le> r" "\<forall>n. cmod (poly p (g n)) <?m+1 /real(Suc n)" 
chaieb@26123
   589
      by blast
chaieb@26123
   590
    from bolzano_weierstrass_complex_disc[OF g(1)] 
chaieb@26123
   591
    obtain f z where fz: "subseq f" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. cmod (g (f n) - z) < e"
chaieb@26123
   592
      by blast    
chaieb@26123
   593
    {fix w 
chaieb@26123
   594
      assume wr: "cmod w \<le> r"
chaieb@26123
   595
      let ?e = "\<bar>cmod (poly p z) - ?m\<bar>"
chaieb@26123
   596
      {assume e: "?e > 0"
chaieb@26123
   597
	hence e2: "?e/2 > 0" by simp
chaieb@26123
   598
	from poly_cont[OF e2, of z p] obtain d where
chaieb@26123
   599
	  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
chaieb@26123
   600
	{fix w assume w: "cmod (w - z) < d"
chaieb@26123
   601
	  have "cmod(poly p w - poly p z) < ?e / 2"
chaieb@26123
   602
	    using d(2)[rule_format, of w] w e by (cases "w=z", simp_all)}
chaieb@26123
   603
	note th1 = this
chaieb@26123
   604
	
chaieb@26123
   605
	from fz(2)[rule_format, OF d(1)] obtain N1 where 
chaieb@26123
   606
	  N1: "\<forall>n\<ge>N1. cmod (g (f n) - z) < d" by blast
chaieb@26123
   607
	from reals_Archimedean2[of "2/?e"] obtain N2::nat where
chaieb@26123
   608
	  N2: "2/?e < real N2" by blast
chaieb@26123
   609
	have th2: "cmod(poly p (g(f(N1 + N2))) - poly p z) < ?e/2"
chaieb@26123
   610
	  using N1[rule_format, of "N1 + N2"] th1 by simp
chaieb@26123
   611
	{fix a b e2 m :: real
chaieb@26123
   612
	have "a < e2 \<Longrightarrow> abs(b - m) < e2 \<Longrightarrow> 2 * e2 <= abs(b - m) + a
chaieb@26123
   613
          ==> False" by arith}
chaieb@26123
   614
      note th0 = this
chaieb@26123
   615
      have ath: 
chaieb@26123
   616
	"\<And>m x e. m <= x \<Longrightarrow>  x < m + e ==> abs(x - m::real) < e" by arith
chaieb@26123
   617
      from s1m[OF g(1)[rule_format]]
chaieb@26123
   618
      have th31: "?m \<le> cmod(poly p (g (f (N1 + N2))))" .
chaieb@26123
   619
      from seq_suble[OF fz(1), of "N1+N2"]
chaieb@26123
   620
      have th00: "real (Suc (N1+N2)) \<le> real (Suc (f (N1+N2)))" by simp
chaieb@26123
   621
      have th000: "0 \<le> (1::real)" "(1::real) \<le> 1" "real (Suc (N1+N2)) > 0"  
chaieb@26123
   622
	using N2 by auto
chaieb@26123
   623
      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
   624
      from g(2)[rule_format, of "f (N1 + N2)"]
chaieb@26123
   625
      have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
chaieb@26123
   626
      from order_less_le_trans[OF th01 th00]
chaieb@26123
   627
      have th32: "cmod(poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
chaieb@26123
   628
      from N2 have "2/?e < real (Suc (N1 + N2))" by arith
chaieb@26123
   629
      with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
chaieb@26123
   630
      have "?e/2 > 1/ real (Suc (N1 + N2))" by (simp add: inverse_eq_divide)
chaieb@26123
   631
      with ath[OF th31 th32]
chaieb@26123
   632
      have thc1:"\<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar>< ?e/2" by arith  
chaieb@26123
   633
      have ath2: "\<And>(a::real) b c m. \<bar>a - b\<bar> <= c ==> \<bar>b - m\<bar> <= \<bar>a - m\<bar> + c" 
chaieb@26123
   634
	by arith
chaieb@26123
   635
      have th22: "\<bar>cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)\<bar>
chaieb@26123
   636
\<le> cmod (poly p (g (f (N1 + N2))) - poly p z)" 
chaieb@26123
   637
	by (simp add: cmod_abs_norm)
chaieb@26123
   638
      from ath2[OF th22, of ?m]
chaieb@26123
   639
      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
   640
      from th0[OF th2 thc1 thc2] have False .}
chaieb@26123
   641
      hence "?e = 0" by auto
chaieb@26123
   642
      then have "cmod (poly p z) = ?m" by simp  
chaieb@26123
   643
      with s1m[OF wr]
chaieb@26123
   644
      have "cmod (poly p z) \<le> cmod (poly p w)" by simp }
chaieb@26123
   645
    hence ?thesis by blast}
chaieb@26123
   646
  ultimately show ?thesis by blast
chaieb@26123
   647
qed
chaieb@26123
   648
chaieb@26123
   649
lemma "(rcis (sqrt (abs r)) (a/2)) ^ 2 = rcis (abs r) a"
chaieb@26123
   650
  unfolding power2_eq_square
chaieb@26123
   651
  apply (simp add: rcis_mult)
chaieb@26123
   652
  apply (simp add: power2_eq_square[symmetric])
chaieb@26123
   653
  done
chaieb@26123
   654
chaieb@26123
   655
lemma cispi: "cis pi = -1" 
chaieb@26123
   656
  unfolding cis_def
chaieb@26123
   657
  by simp
chaieb@26123
   658
chaieb@26123
   659
lemma "(rcis (sqrt (abs r)) ((pi + a)/2)) ^ 2 = rcis (- abs r) a"
chaieb@26123
   660
  unfolding power2_eq_square
chaieb@26123
   661
  apply (simp add: rcis_mult add_divide_distrib)
chaieb@26123
   662
  apply (simp add: power2_eq_square[symmetric] rcis_def cispi cis_mult[symmetric])
chaieb@26123
   663
  done
chaieb@26123
   664
chaieb@26123
   665
text {* Nonzero polynomial in z goes to infinity as z does. *}
chaieb@26123
   666
chaieb@26123
   667
instance complex::idom_char_0 by (intro_classes)
chaieb@26123
   668
instance complex :: recpower_idom_char_0 by intro_classes
chaieb@26123
   669
chaieb@26123
   670
lemma poly_infinity:
chaieb@26123
   671
  assumes ex: "list_ex (\<lambda>c. c \<noteq> 0) p"
chaieb@26123
   672
  shows "\<exists>r. \<forall>z. r \<le> cmod z \<longrightarrow> d \<le> cmod (poly (a#p) z)"
chaieb@26123
   673
using ex
chaieb@26123
   674
proof(induct p arbitrary: a d)
chaieb@26123
   675
  case (Cons c cs a d) 
chaieb@26123
   676
  {assume H: "list_ex (\<lambda>c. c\<noteq>0) cs"
chaieb@26123
   677
    with Cons.hyps obtain r where r: "\<forall>z. r \<le> cmod z \<longrightarrow> d + cmod a \<le> cmod (poly (c # cs) z)" by blast
chaieb@26123
   678
    let ?r = "1 + \<bar>r\<bar>"
chaieb@26123
   679
    {fix z assume h: "1 + \<bar>r\<bar> \<le> cmod z"
chaieb@26123
   680
      have r0: "r \<le> cmod z" using h by arith
chaieb@26123
   681
      from r[rule_format, OF r0]
chaieb@26123
   682
      have th0: "d + cmod a \<le> 1 * cmod(poly (c#cs) z)" by arith
chaieb@26123
   683
      from h have z1: "cmod z \<ge> 1" by arith
chaieb@26123
   684
      from order_trans[OF th0 mult_right_mono[OF z1 cmod_pos[of "poly (c#cs) z"]]]
chaieb@26123
   685
      have th1: "d \<le> cmod(z * poly (c#cs) z) - cmod a"
chaieb@26123
   686
	unfolding cmod_mult by (simp add: ring_simps)
chaieb@26123
   687
      from complex_mod_triangle_sub[of "z * poly (c#cs) z" a]
chaieb@26123
   688
      have th2: "cmod(z * poly (c#cs) z) - cmod a \<le> cmod (poly (a#c#cs) z)" 
chaieb@26123
   689
	by (simp add: diff_le_eq ring_simps) 
chaieb@26123
   690
      from th1 th2 have "d \<le> cmod (poly (a#c#cs) z)"  by arith}
chaieb@26123
   691
    hence ?case by blast}
chaieb@26123
   692
  moreover
chaieb@26123
   693
  {assume cs0: "\<not> (list_ex (\<lambda>c. c \<noteq> 0) cs)"
chaieb@26123
   694
    with Cons.prems have c0: "c \<noteq> 0" by simp
chaieb@26123
   695
    from cs0 have cs0': "list_all (\<lambda>c. c = 0) cs" 
chaieb@26123
   696
      by (auto simp add: list_all_iff list_ex_iff)
chaieb@26123
   697
    {fix z
chaieb@26123
   698
      assume h: "(\<bar>d\<bar> + cmod a) / cmod c \<le> cmod z"
chaieb@26123
   699
      from c0 have "cmod c > 0" by simp
chaieb@26123
   700
      from h c0 have th0: "\<bar>d\<bar> + cmod a \<le> cmod (z*c)" 
chaieb@26123
   701
	by (simp add: field_simps cmod_mult)
chaieb@26123
   702
      have ath: "\<And>mzh mazh ma. mzh <= mazh + ma ==> abs(d) + ma <= mzh ==> d <= mazh" by arith
chaieb@26123
   703
      from complex_mod_triangle_sub[of "z*c" a ]
chaieb@26123
   704
      have th1: "cmod (z * c) \<le> cmod (a + z * c) + cmod a"
chaieb@26123
   705
	by (simp add: ring_simps)
chaieb@26123
   706
      from ath[OF th1 th0] have "d \<le> cmod (poly (a # c # cs) z)" 
chaieb@26123
   707
	using poly_0[OF cs0'] by simp}
chaieb@26123
   708
    then have ?case  by blast}
chaieb@26123
   709
  ultimately show ?case by blast
chaieb@26123
   710
qed simp
chaieb@26123
   711
chaieb@26123
   712
text {* Hence polynomial's modulus attains its minimum somewhere. *}
chaieb@26123
   713
lemma poly_minimum_modulus:
chaieb@26123
   714
  "\<exists>z.\<forall>w. cmod (poly p z) \<le> cmod (poly p w)"
chaieb@26123
   715
proof(induct p)
chaieb@26123
   716
  case (Cons c cs) 
chaieb@26123
   717
  {assume cs0: "list_ex (\<lambda>c. c \<noteq> 0) cs"
chaieb@26123
   718
    from poly_infinity[OF cs0, of "cmod (poly (c#cs) 0)" c]
chaieb@26123
   719
    obtain r where r: "\<And>z. r \<le> cmod z \<Longrightarrow> cmod (poly (c # cs) 0) \<le> cmod (poly (c # cs) z)" by blast
chaieb@26123
   720
    have ath: "\<And>z r. r \<le> cmod z \<or> cmod z \<le> \<bar>r\<bar>" by arith
chaieb@26123
   721
    from poly_minimum_modulus_disc[of "\<bar>r\<bar>" "c#cs"] 
chaieb@26123
   722
    obtain v where v: "\<And>w. cmod w \<le> \<bar>r\<bar> \<Longrightarrow> cmod (poly (c # cs) v) \<le> cmod (poly (c # cs) w)" by blast
chaieb@26123
   723
    {fix z assume z: "r \<le> cmod z"
chaieb@26123
   724
      from v[of 0] r[OF z] 
chaieb@26123
   725
      have "cmod (poly (c # cs) v) \<le> cmod (poly (c # cs) z)"
chaieb@26123
   726
	by simp }
chaieb@26123
   727
    note v0 = this
chaieb@26123
   728
    from v0 v ath[of r] have ?case by blast}
chaieb@26123
   729
  moreover
chaieb@26123
   730
  {assume cs0: "\<not> (list_ex (\<lambda>c. c\<noteq>0) cs)"
chaieb@26123
   731
    hence th:"list_all (\<lambda>c. c = 0) cs" by (simp add: list_all_iff list_ex_iff)
chaieb@26123
   732
    from poly_0[OF th] Cons.hyps have ?case by simp}
chaieb@26123
   733
  ultimately show ?case by blast
chaieb@26123
   734
qed simp
chaieb@26123
   735
chaieb@26123
   736
text{* Constant function (non-syntactic characterization). *}
chaieb@26123
   737
definition "constant f = (\<forall>x y. f x = f y)"
chaieb@26123
   738
chaieb@26123
   739
lemma nonconstant_length: "\<not> (constant (poly p)) \<Longrightarrow> length p \<ge> 2"
chaieb@26123
   740
  unfolding constant_def
chaieb@26123
   741
  apply (induct p, auto)
chaieb@26123
   742
  apply (unfold not_less[symmetric])
chaieb@26123
   743
  apply simp
chaieb@26123
   744
  apply (rule ccontr)
chaieb@26123
   745
  apply auto
chaieb@26123
   746
  done
chaieb@26123
   747
 
chaieb@26123
   748
lemma poly_replicate_append:
chaieb@26123
   749
  "poly ((replicate n 0)@p) (x::'a::{recpower, comm_ring}) = x^n * poly p x"
chaieb@26123
   750
  by(induct n, auto simp add: power_Suc ring_simps)
chaieb@26123
   751
chaieb@26123
   752
text {* Decomposition of polynomial, skipping zero coefficients 
chaieb@26123
   753
  after the first.  *}
chaieb@26123
   754
chaieb@26123
   755
lemma poly_decompose_lemma:
chaieb@26123
   756
 assumes nz: "\<not>(\<forall>z. z\<noteq>0 \<longrightarrow> poly p z = (0::'a::{recpower,idom}))"
chaieb@26123
   757
  shows "\<exists>k a q. a\<noteq>0 \<and> Suc (length q + k) = length p \<and> 
chaieb@26123
   758
                 (\<forall>z. poly p z = z^k * poly (a#q) z)"
chaieb@26123
   759
using nz
chaieb@26123
   760
proof(induct p)
chaieb@26123
   761
  case Nil thus ?case by simp
chaieb@26123
   762
next
chaieb@26123
   763
  case (Cons c cs)
chaieb@26123
   764
  {assume c0: "c = 0"
chaieb@26123
   765
    
chaieb@26123
   766
    from Cons.hyps Cons.prems c0 have ?case apply auto
chaieb@26123
   767
      apply (rule_tac x="k+1" in exI)
chaieb@26123
   768
      apply (rule_tac x="a" in exI, clarsimp)
chaieb@26123
   769
      apply (rule_tac x="q" in exI)
chaieb@26123
   770
      by (auto simp add: power_Suc)}
chaieb@26123
   771
  moreover
chaieb@26123
   772
  {assume c0: "c\<noteq>0"
chaieb@26123
   773
    hence ?case apply-
chaieb@26123
   774
      apply (rule exI[where x=0])
chaieb@26123
   775
      apply (rule exI[where x=c], clarsimp)
chaieb@26123
   776
      apply (rule exI[where x=cs])
chaieb@26123
   777
      apply auto
chaieb@26123
   778
      done}
chaieb@26123
   779
  ultimately show ?case by blast
chaieb@26123
   780
qed
chaieb@26123
   781
chaieb@26123
   782
lemma poly_decompose:
chaieb@26123
   783
  assumes nc: "~constant(poly p)"
chaieb@26123
   784
  shows "\<exists>k a q. a\<noteq>(0::'a::{recpower,idom}) \<and> k\<noteq>0 \<and>
chaieb@26123
   785
               length q + k + 1 = length p \<and> 
chaieb@26123
   786
              (\<forall>z. poly p z = poly p 0 + z^k * poly (a#q) z)"
chaieb@26123
   787
using nc 
chaieb@26123
   788
proof(induct p)
chaieb@26123
   789
  case Nil thus ?case by (simp add: constant_def)
chaieb@26123
   790
next
chaieb@26123
   791
  case (Cons c cs)
chaieb@26123
   792
  {assume C:"\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0"
chaieb@26123
   793
    {fix x y
chaieb@26123
   794
      from C have "poly (c#cs) x = poly (c#cs) y" by (cases "x=0", auto)}
chaieb@26123
   795
    with Cons.prems have False by (auto simp add: constant_def)}
chaieb@26123
   796
  hence th: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0)" ..
chaieb@26123
   797
  from poly_decompose_lemma[OF th] 
chaieb@26123
   798
  show ?case 
chaieb@26123
   799
    apply clarsimp    
chaieb@26123
   800
    apply (rule_tac x="k+1" in exI)
chaieb@26123
   801
    apply (rule_tac x="a" in exI)
chaieb@26123
   802
    apply simp
chaieb@26123
   803
    apply (rule_tac x="q" in exI)
chaieb@26123
   804
    apply (auto simp add: power_Suc)
chaieb@26123
   805
    done
chaieb@26123
   806
qed
chaieb@26123
   807
chaieb@26123
   808
text{* Fundamental theorem of algebral *}
chaieb@26123
   809
chaieb@26123
   810
lemma fundamental_theorem_of_algebra:
chaieb@26123
   811
  assumes nc: "~constant(poly p)"
chaieb@26123
   812
  shows "\<exists>z::complex. poly p z = 0"
chaieb@26123
   813
using nc
chaieb@26123
   814
proof(induct n\<equiv> "length p" arbitrary: p rule: nat_less_induct)
chaieb@26123
   815
  fix n fix p :: "complex list"
chaieb@26123
   816
  let ?p = "poly p"
chaieb@26123
   817
  assume H: "\<forall>m<n. \<forall>p. \<not> constant (poly p) \<longrightarrow> m = length p \<longrightarrow> (\<exists>(z::complex). poly p z = 0)" and nc: "\<not> constant ?p" and n: "n = length p"
chaieb@26123
   818
  let ?ths = "\<exists>z. ?p z = 0"
chaieb@26123
   819
chaieb@26123
   820
  from nonconstant_length[OF nc] have n2: "n\<ge> 2" by (simp add: n)
chaieb@26123
   821
  from poly_minimum_modulus obtain c where 
chaieb@26123
   822
    c: "\<forall>w. cmod (?p c) \<le> cmod (?p w)" by blast
chaieb@26123
   823
  {assume pc: "?p c = 0" hence ?ths by blast}
chaieb@26123
   824
  moreover
chaieb@26123
   825
  {assume pc0: "?p c \<noteq> 0"
chaieb@26123
   826
    from poly_offset[of p c] obtain q where
chaieb@26123
   827
      q: "length q = length p" "\<forall>x. poly q x = ?p (c+x)" by blast
chaieb@26123
   828
    {assume h: "constant (poly q)"
chaieb@26123
   829
      from q(2) have th: "\<forall>x. poly q (x - c) = ?p x" by auto
chaieb@26123
   830
      {fix x y
chaieb@26123
   831
	from th have "?p x = poly q (x - c)" by auto 
chaieb@26123
   832
	also have "\<dots> = poly q (y - c)" 
chaieb@26123
   833
	  using h unfolding constant_def by blast
chaieb@26123
   834
	also have "\<dots> = ?p y" using th by auto
chaieb@26123
   835
	finally have "?p x = ?p y" .}
chaieb@26123
   836
      with nc have False unfolding constant_def by blast }
chaieb@26123
   837
    hence qnc: "\<not> constant (poly q)" by blast
chaieb@26123
   838
    from q(2) have pqc0: "?p c = poly q 0" by simp
chaieb@26123
   839
    from c pqc0 have cq0: "\<forall>w. cmod (poly q 0) \<le> cmod (?p w)" by simp 
chaieb@26123
   840
    let ?a0 = "poly q 0"
chaieb@26123
   841
    from pc0 pqc0 have a00: "?a0 \<noteq> 0" by simp 
chaieb@26123
   842
    from a00 
chaieb@26123
   843
    have qr: "\<forall>z. poly q z = poly (map (op * (inverse ?a0)) q) z * ?a0"
chaieb@26123
   844
      by (simp add: poly_cmult_map)
chaieb@26123
   845
    let ?r = "map (op * (inverse ?a0)) q"
chaieb@26123
   846
    have lgqr: "length q = length ?r" by simp 
chaieb@26123
   847
    {assume h: "\<And>x y. poly ?r x = poly ?r y"
chaieb@26123
   848
      {fix x y
chaieb@26123
   849
	from qr[rule_format, of x] 
chaieb@26123
   850
	have "poly q x = poly ?r x * ?a0" by auto
chaieb@26123
   851
	also have "\<dots> = poly ?r y * ?a0" using h by simp
chaieb@26123
   852
	also have "\<dots> = poly q y" using qr[rule_format, of y] by simp
chaieb@26123
   853
	finally have "poly q x = poly q y" .} 
chaieb@26123
   854
      with qnc have False unfolding constant_def by blast}
chaieb@26123
   855
    hence rnc: "\<not> constant (poly ?r)" unfolding constant_def by blast
chaieb@26123
   856
    from qr[rule_format, of 0] a00  have r01: "poly ?r 0 = 1" by auto
chaieb@26123
   857
    {fix w 
chaieb@26123
   858
      have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w / ?a0) < 1"
chaieb@26123
   859
	using qr[rule_format, of w] a00 by simp
chaieb@26123
   860
      also have "\<dots> \<longleftrightarrow> cmod (poly q w) < cmod ?a0"
chaieb@26123
   861
	using a00 unfolding cmod_divide by (simp add: field_simps)
chaieb@26123
   862
      finally have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w) < cmod ?a0" .}
chaieb@26123
   863
    note mrmq_eq = this
chaieb@26123
   864
    from poly_decompose[OF rnc] obtain k a s where 
chaieb@26123
   865
      kas: "a\<noteq>0" "k\<noteq>0" "length s + k + 1 = length ?r" 
chaieb@26123
   866
      "\<forall>z. poly ?r z = poly ?r 0 + z^k* poly (a#s) z" by blast
chaieb@26123
   867
    {assume "k + 1 = n"
chaieb@26123
   868
      with kas(3) lgqr[symmetric] q(1) n[symmetric] have s0:"s=[]" by auto
chaieb@26123
   869
      {fix w
chaieb@26123
   870
	have "cmod (poly ?r w) = cmod (1 + a * w ^ k)" 
chaieb@26123
   871
	  using kas(4)[rule_format, of w] s0 r01 by (simp add: ring_simps)}
chaieb@26123
   872
      note hth = this [symmetric]
chaieb@26123
   873
	from reduce_poly_simple[OF kas(1,2)] 
chaieb@26123
   874
      have "\<exists>w. cmod (poly ?r w) < 1" unfolding hth by blast}
chaieb@26123
   875
    moreover
chaieb@26123
   876
    {assume kn: "k+1 \<noteq> n"
chaieb@26123
   877
      from kn kas(3) q(1) n[symmetric] have k1n: "k + 1 < n" by simp
chaieb@26123
   878
      have th01: "\<not> constant (poly (1#((replicate (k - 1) 0)@[a])))" 
chaieb@26123
   879
	unfolding constant_def poly_Nil poly_Cons poly_replicate_append
chaieb@26123
   880
	using kas(1) apply simp 
chaieb@26123
   881
	by (rule exI[where x=0], rule exI[where x=1], simp)
chaieb@26123
   882
      from kas(2) have th02: "k+1 = length (1#((replicate (k - 1) 0)@[a]))" 
chaieb@26123
   883
	by simp
chaieb@26123
   884
      from H[rule_format, OF k1n th01 th02]
chaieb@26123
   885
      obtain w where w: "1 + w^k * a = 0"
chaieb@26123
   886
	unfolding poly_Nil poly_Cons poly_replicate_append
chaieb@26123
   887
	using kas(2) by (auto simp add: power_Suc[symmetric, of _ "k - Suc 0"] 
chaieb@26123
   888
	  mult_assoc[of _ _ a, symmetric])
chaieb@26123
   889
      from poly_bound_exists[of "cmod w" s] obtain m where 
chaieb@26123
   890
	m: "m > 0" "\<forall>z. cmod z \<le> cmod w \<longrightarrow> cmod (poly s z) \<le> m" by blast
chaieb@26123
   891
      have w0: "w\<noteq>0" using kas(2) w by (auto simp add: power_0_left)
chaieb@26123
   892
      from w have "(1 + w ^ k * a) - 1 = 0 - 1" by simp
chaieb@26123
   893
      then have wm1: "w^k * a = - 1" by simp
chaieb@26123
   894
      have inv0: "0 < inverse (cmod w ^ (k + 1) * m)" 
chaieb@26123
   895
	using cmod_pos[of w] w0 m(1)
chaieb@26123
   896
	  by (simp add: inverse_eq_divide zero_less_mult_iff)
chaieb@26123
   897
      with real_down2[OF zero_less_one] obtain t where
chaieb@26123
   898
	t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
chaieb@26123
   899
      let ?ct = "complex_of_real t"
chaieb@26123
   900
      let ?w = "?ct * w"
chaieb@26123
   901
      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: ring_simps power_mult_distrib)
chaieb@26123
   902
      also have "\<dots> = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
chaieb@26123
   903
	unfolding wm1 by (simp)
chaieb@26123
   904
      finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) = cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)" 
chaieb@26123
   905
	apply -
chaieb@26123
   906
	apply (rule cong[OF refl[of cmod]])
chaieb@26123
   907
	apply assumption
chaieb@26123
   908
	done
chaieb@26123
   909
      with complex_mod_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"] 
chaieb@26123
   910
      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 cmod_complex_of_real by simp 
chaieb@26123
   911
      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
   912
      have "t *cmod w \<le> 1 * cmod w" apply (rule mult_mono) using t(1,2) by auto
chaieb@26123
   913
      then have tw: "cmod ?w \<le> cmod w" using t(1) by (simp add: cmod_mult) 
chaieb@26123
   914
      from t inv0 have "t* (cmod w ^ (k + 1) * m) < 1"
chaieb@26123
   915
	by (simp add: inverse_eq_divide field_simps)
chaieb@26123
   916
      with zero_less_power[OF t(1), of k] 
chaieb@26123
   917
      have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1" 
chaieb@26123
   918
	apply - apply (rule mult_strict_left_mono) by simp_all
chaieb@26123
   919
      have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k+1) * cmod (poly s ?w)))"  using w0 t(1)
chaieb@26123
   920
	by (simp add: ring_simps power_mult_distrib cmod_complex_of_real cmod_power cmod_mult)
chaieb@26123
   921
      then have "cmod (?w^k * ?w * poly s ?w) \<le> t^k * (t* (cmod w ^ (k + 1) * m))"
chaieb@26123
   922
	using t(1,2) m(2)[rule_format, OF tw] w0
chaieb@26123
   923
	apply (simp only: )
chaieb@26123
   924
	apply auto
chaieb@26123
   925
	apply (rule mult_mono, simp_all add: cmod_pos)+
chaieb@26123
   926
	apply (simp add: zero_le_mult_iff zero_le_power)
chaieb@26123
   927
	done
chaieb@26123
   928
      with th30 have th120: "cmod (?w^k * ?w * poly s ?w) < t^k" by simp 
chaieb@26123
   929
      from power_strict_mono[OF t(2), of k] t(1) kas(2) have th121: "t^k \<le> 1" 
chaieb@26123
   930
	by auto
chaieb@26123
   931
      from ath[OF cmod_pos[of "?w^k * ?w * poly s ?w"] th120 th121]
chaieb@26123
   932
      have th12: "\<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w) < 1" . 
chaieb@26123
   933
      from th11 th12
chaieb@26123
   934
      have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) < 1"  by arith 
chaieb@26123
   935
      then have "cmod (poly ?r ?w) < 1" 
chaieb@26123
   936
	unfolding kas(4)[rule_format, of ?w] r01 by simp 
chaieb@26123
   937
      then have "\<exists>w. cmod (poly ?r w) < 1" by blast}
chaieb@26123
   938
    ultimately have cr0_contr: "\<exists>w. cmod (poly ?r w) < 1" by blast
chaieb@26123
   939
    from cr0_contr cq0 q(2)
chaieb@26123
   940
    have ?ths unfolding mrmq_eq not_less[symmetric] by auto}
chaieb@26123
   941
  ultimately show ?ths by blast
chaieb@26123
   942
qed
chaieb@26123
   943
chaieb@26123
   944
text {* Alternative version with a syntactic notion of constant polynomial. *}
chaieb@26123
   945
chaieb@26123
   946
lemma fundamental_theorem_of_algebra_alt:
chaieb@26123
   947
  assumes nc: "~(\<exists>a l. a\<noteq> 0 \<and> list_all(\<lambda>b. b = 0) l \<and> p = a#l)"
chaieb@26123
   948
  shows "\<exists>z. poly p z = (0::complex)"
chaieb@26123
   949
using nc
chaieb@26123
   950
proof(induct p)
chaieb@26123
   951
  case (Cons c cs)
chaieb@26123
   952
  {assume "c=0" hence ?case by auto}
chaieb@26123
   953
  moreover
chaieb@26123
   954
  {assume c0: "c\<noteq>0"
chaieb@26123
   955
    {assume nc: "constant (poly (c#cs))"
chaieb@26123
   956
      from nc[unfolded constant_def, rule_format, of 0] 
chaieb@26123
   957
      have "\<forall>w. w \<noteq> 0 \<longrightarrow> poly cs w = 0" by auto 
chaieb@26123
   958
      hence "list_all (\<lambda>c. c=0) cs"
chaieb@26123
   959
	proof(induct cs)
chaieb@26123
   960
	  case (Cons d ds)
chaieb@26123
   961
	  {assume "d=0" hence ?case using Cons.prems Cons.hyps by simp}
chaieb@26123
   962
	  moreover
chaieb@26123
   963
	  {assume d0: "d\<noteq>0"
chaieb@26123
   964
	    from poly_bound_exists[of 1 ds] obtain m where 
chaieb@26123
   965
	      m: "m > 0" "\<forall>z. \<forall>z. cmod z \<le> 1 \<longrightarrow> cmod (poly ds z) \<le> m" by blast
chaieb@26123
   966
	    have dm: "cmod d / m > 0" using d0 m(1) by (simp add: field_simps)
chaieb@26123
   967
	    from real_down2[OF dm zero_less_one] obtain x where 
chaieb@26123
   968
	      x: "x > 0" "x < cmod d / m" "x < 1" by blast
chaieb@26123
   969
	    let ?x = "complex_of_real x"
chaieb@26123
   970
	    from x have cx: "?x \<noteq> 0"  "cmod ?x \<le> 1" by simp_all
chaieb@26123
   971
	    from Cons.prems[rule_format, OF cx(1)]
chaieb@26123
   972
	    have cth: "cmod (?x*poly ds ?x) = cmod d" by (simp add: eq_diff_eq[symmetric])
chaieb@26123
   973
	    from m(2)[rule_format, OF cx(2)] x(1)
chaieb@26123
   974
	    have th0: "cmod (?x*poly ds ?x) \<le> x*m"
chaieb@26123
   975
	      by (simp add: cmod_mult)
chaieb@26123
   976
	    from x(2) m(1) have "x*m < cmod d" by (simp add: field_simps)
chaieb@26123
   977
	    with th0 have "cmod (?x*poly ds ?x) \<noteq> cmod d" by auto
chaieb@26123
   978
	    with cth  have ?case by blast}
chaieb@26123
   979
	  ultimately show ?case by blast 
chaieb@26123
   980
	qed simp}
chaieb@26123
   981
      then have nc: "\<not> constant (poly (c#cs))" using Cons.prems c0 
chaieb@26123
   982
	by blast
chaieb@26123
   983
      from fundamental_theorem_of_algebra[OF nc] have ?case .}
chaieb@26123
   984
  ultimately show ?case by blast  
chaieb@26123
   985
qed simp
chaieb@26123
   986
chaieb@26123
   987
section{* Nullstellenstatz, degrees and divisibility of polynomials *}
chaieb@26123
   988
chaieb@26123
   989
lemma nullstellensatz_lemma:
chaieb@26123
   990
  fixes p :: "complex list"
chaieb@26123
   991
  assumes "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0"
chaieb@26123
   992
  and "degree p = n" and "n \<noteq> 0"
chaieb@26123
   993
  shows "p divides (pexp q n)"
chaieb@26123
   994
using prems
chaieb@26123
   995
proof(induct n arbitrary: p q rule: nat_less_induct)
chaieb@26123
   996
  fix n::nat fix p q :: "complex list"
chaieb@26123
   997
  assume IH: "\<forall>m<n. \<forall>p q.
chaieb@26123
   998
                 (\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longrightarrow>
chaieb@26123
   999
                 degree p = m \<longrightarrow> m \<noteq> 0 \<longrightarrow> p divides (q %^ m)"
chaieb@26123
  1000
    and pq0: "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0" 
chaieb@26123
  1001
    and dpn: "degree p = n" and n0: "n \<noteq> 0"
chaieb@26123
  1002
  let ?ths = "p divides (q %^ n)"
chaieb@26123
  1003
  {fix a assume a: "poly p a = 0"
chaieb@26123
  1004
    {assume p0: "poly p = poly []" 
chaieb@26123
  1005
      hence ?ths unfolding divides_def  using pq0 n0
chaieb@26123
  1006
	apply - apply (rule exI[where x="[]"], rule ext)
chaieb@26123
  1007
	by (auto simp add: poly_mult poly_exp)}
chaieb@26123
  1008
    moreover
chaieb@26123
  1009
    {assume p0: "poly p \<noteq> poly []" 
chaieb@26123
  1010
      and oa: "order  a p \<noteq> 0"
chaieb@26123
  1011
      from p0 have pne: "p \<noteq> []" by auto
chaieb@26123
  1012
      let ?op = "order a p"
chaieb@26123
  1013
      from p0 have ap: "([- a, 1] %^ ?op) divides p" 
chaieb@26123
  1014
	"\<not> pexp [- a, 1] (Suc ?op) divides p" using order by blast+ 
chaieb@26123
  1015
      note oop = order_degree[OF p0, unfolded dpn]
chaieb@26123
  1016
      {assume q0: "q = []"
chaieb@26123
  1017
	hence ?ths using n0 unfolding divides_def 
chaieb@26123
  1018
	  apply simp
chaieb@26123
  1019
	  apply (rule exI[where x="[]"], rule ext)
chaieb@26123
  1020
	  by (simp add: divides_def poly_exp poly_mult)}
chaieb@26123
  1021
      moreover
chaieb@26123
  1022
      {assume q0: "q\<noteq>[]"
chaieb@26123
  1023
	from pq0[rule_format, OF a, unfolded poly_linear_divides] q0
chaieb@26123
  1024
	obtain r where r: "q = pmult [- a, 1] r" by blast
chaieb@26123
  1025
	from ap[unfolded divides_def] obtain s where
chaieb@26123
  1026
	  s: "poly p = poly (pmult (pexp [- a, 1] ?op) s)" by blast
chaieb@26123
  1027
	have s0: "poly s \<noteq> poly []"
chaieb@26123
  1028
	  using s p0 by (simp add: poly_entire)
chaieb@26123
  1029
	hence pns0: "poly (pnormalize s) \<noteq> poly []" and sne: "s\<noteq>[]" by auto
chaieb@26123
  1030
	{assume ds0: "degree s = 0"
chaieb@26123
  1031
	  from ds0 pns0 have "\<exists>k. pnormalize s = [k]" unfolding degree_def 
chaieb@26123
  1032
	    by (cases "pnormalize s", auto)
chaieb@26123
  1033
	  then obtain k where kpn: "pnormalize s = [k]" by blast
chaieb@26123
  1034
	  from pns0[unfolded poly_zero] kpn have k: "k \<noteq>0" "poly s = poly [k]"
chaieb@26123
  1035
	    using poly_normalize[of s] by simp_all
chaieb@26123
  1036
	  let ?w = "pmult (pmult [1/k] (pexp [-a,1] (n - ?op))) (pexp r n)"
chaieb@26123
  1037
	  from k r s oop have "poly (pexp q n) = poly (pmult p ?w)"
chaieb@26123
  1038
	    by - (rule ext, simp add: poly_mult poly_exp poly_cmult poly_add power_add[symmetric] ring_simps power_mult_distrib[symmetric])
chaieb@26123
  1039
	  hence ?ths unfolding divides_def by blast}
chaieb@26123
  1040
	moreover
chaieb@26123
  1041
	{assume ds0: "degree s \<noteq> 0"
chaieb@26123
  1042
	  from ds0 s0 dpn degree_unique[OF s, unfolded linear_pow_mul_degree] oa
chaieb@26123
  1043
	    have dsn: "degree s < n" by auto 
chaieb@26123
  1044
	    {fix x assume h: "poly s x = 0"
chaieb@26123
  1045
	      {assume xa: "x = a"
chaieb@26123
  1046
		from h[unfolded xa poly_linear_divides] sne obtain u where
chaieb@26123
  1047
		  u: "s = pmult [- a, 1] u" by blast
chaieb@26123
  1048
		have "poly p = poly (pmult (pexp [- a, 1] (Suc ?op)) u)"
chaieb@26123
  1049
		  unfolding s u
chaieb@26123
  1050
		  apply (rule ext)
chaieb@26123
  1051
		  by (simp add: ring_simps power_mult_distrib[symmetric] poly_mult poly_cmult poly_add poly_exp)
chaieb@26123
  1052
		with ap(2)[unfolded divides_def] have False by blast}
chaieb@26123
  1053
	      note xa = this
chaieb@26123
  1054
	      from h s have "poly p x = 0" by (simp add: poly_mult)
chaieb@26123
  1055
	      with pq0 have "poly q x = 0" by blast
chaieb@26123
  1056
	      with r xa have "poly r x = 0"
chaieb@26123
  1057
		by (auto simp add: poly_mult poly_add poly_cmult eq_diff_eq[symmetric])}
chaieb@26123
  1058
	    note impth = this
chaieb@26123
  1059
	    from IH[rule_format, OF dsn, of s r] impth ds0
chaieb@26123
  1060
	    have "s divides (pexp r (degree s))" by blast
chaieb@26123
  1061
	    then obtain u where u: "poly (pexp r (degree s)) = poly (pmult s u)"
chaieb@26123
  1062
	      unfolding divides_def by blast
chaieb@26123
  1063
	    hence u': "\<And>x. poly s x * poly u x = poly r x ^ degree s"
chaieb@26123
  1064
	      by (simp add: poly_mult[symmetric] poly_exp[symmetric])
chaieb@26123
  1065
	    let ?w = "pmult (pmult u (pexp [-a,1] (n - ?op))) (pexp r (n - degree s))"
chaieb@26123
  1066
	    from u' s r oop[of a] dsn have "poly (pexp q n) = poly (pmult p ?w)"
chaieb@26123
  1067
	      apply - apply (rule ext)
chaieb@26123
  1068
	      apply (simp only:  power_mult_distrib power_add[symmetric] poly_add poly_mult poly_exp poly_cmult ring_simps)
chaieb@26123
  1069
	      
chaieb@26123
  1070
	      apply (simp add:  power_mult_distrib power_add[symmetric] poly_add poly_mult poly_exp poly_cmult mult_assoc[symmetric])
chaieb@26123
  1071
	      done
chaieb@26123
  1072
	    hence ?ths unfolding divides_def by blast}
chaieb@26123
  1073
      ultimately have ?ths by blast }
chaieb@26123
  1074
      ultimately have ?ths by blast}
chaieb@26123
  1075
    ultimately have ?ths using a order_root by blast}
chaieb@26123
  1076
  moreover
chaieb@26123
  1077
  {assume exa: "\<not> (\<exists>a. poly p a = 0)"
chaieb@26123
  1078
    from fundamental_theorem_of_algebra_alt[of p] exa obtain c cs where
chaieb@26123
  1079
      ccs: "c\<noteq>0" "list_all (\<lambda>c. c = 0) cs" "p = c#cs" by blast
chaieb@26123
  1080
    
chaieb@26123
  1081
    from poly_0[OF ccs(2)] ccs(3) 
chaieb@26123
  1082
    have pp: "\<And>x. poly p x =  c" by simp
chaieb@26123
  1083
    let ?w = "pmult [1/c] (pexp q n)"
chaieb@26123
  1084
    from pp ccs(1) 
chaieb@26123
  1085
    have "poly (pexp q n) = poly (pmult p ?w) "
chaieb@26123
  1086
      apply - apply (rule ext)
chaieb@26123
  1087
      unfolding poly_mult_assoc[symmetric] by (simp add: poly_mult)
chaieb@26123
  1088
    hence ?ths unfolding divides_def by blast}
chaieb@26123
  1089
  ultimately show ?ths by blast
chaieb@26123
  1090
qed
chaieb@26123
  1091
chaieb@26123
  1092
lemma nullstellensatz_univariate:
chaieb@26123
  1093
  "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> 
chaieb@26123
  1094
    p divides (q %^ (degree p)) \<or> (poly p = poly [] \<and> poly q = poly [])"
chaieb@26123
  1095
proof-
chaieb@26123
  1096
  {assume pe: "poly p = poly []"
chaieb@26123
  1097
    hence eq: "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> poly q = poly []"
chaieb@26123
  1098
      apply auto
chaieb@26123
  1099
      by (rule ext, simp)
chaieb@26123
  1100
    {assume "p divides (pexp q (degree p))"
chaieb@26123
  1101
      then obtain r where r: "poly (pexp q (degree p)) = poly (pmult p r)" 
chaieb@26123
  1102
	unfolding divides_def by blast
chaieb@26123
  1103
      from cong[OF r refl] pe degree_unique[OF pe]
chaieb@26123
  1104
      have False by (simp add: poly_mult degree_def)}
chaieb@26123
  1105
    with eq pe have ?thesis by blast}
chaieb@26123
  1106
  moreover
chaieb@26123
  1107
  {assume pe: "poly p \<noteq> poly []"
chaieb@26123
  1108
    have p0: "poly [0] = poly []" by (rule ext, simp)
chaieb@26123
  1109
    {assume dp: "degree p = 0"
chaieb@26123
  1110
      then obtain k where "pnormalize p = [k]" using pe poly_normalize[of p]
chaieb@26123
  1111
	unfolding degree_def by (cases "pnormalize p", auto)
chaieb@26123
  1112
      hence k: "pnormalize p = [k]" "poly p = poly [k]" "k\<noteq>0"
chaieb@26123
  1113
	using pe poly_normalize[of p] by (auto simp add: p0)
chaieb@26123
  1114
      hence th1: "\<forall>x. poly p x \<noteq> 0" by simp
chaieb@26123
  1115
      from k(2,3) dp have "poly (pexp q (degree p)) = poly (pmult p [1/k]) "
chaieb@26123
  1116
	by - (rule ext, simp add: poly_mult poly_exp)
chaieb@26123
  1117
      hence th2: "p divides (pexp q (degree p))" unfolding divides_def by blast
chaieb@26123
  1118
      from th1 th2 pe have ?thesis by blast}
chaieb@26123
  1119
    moreover
chaieb@26123
  1120
    {assume dp: "degree p \<noteq> 0"
chaieb@26123
  1121
      then obtain n where n: "degree p = Suc n " by (cases "degree p", auto)
chaieb@26123
  1122
      {assume "p divides (pexp q (Suc n))"
chaieb@26123
  1123
	then obtain u where u: "poly (pexp q (Suc n)) = poly (pmult p u)"
chaieb@26123
  1124
	  unfolding divides_def by blast
chaieb@26123
  1125
	hence u' :"\<And>x. poly (pexp q (Suc n)) x = poly (pmult p u) x" by simp_all
chaieb@26123
  1126
	{fix x assume h: "poly p x = 0" "poly q x \<noteq> 0"
chaieb@26123
  1127
	  hence "poly (pexp q (Suc n)) x \<noteq> 0" by (simp only: poly_exp) simp	  
chaieb@26123
  1128
	  hence False using u' h(1) by (simp only: poly_mult poly_exp) simp}}
chaieb@26123
  1129
	with n nullstellensatz_lemma[of p q "degree p"] dp 
chaieb@26123
  1130
	have ?thesis by auto}
chaieb@26123
  1131
    ultimately have ?thesis by blast}
chaieb@26123
  1132
  ultimately show ?thesis by blast
chaieb@26123
  1133
qed
chaieb@26123
  1134
chaieb@26123
  1135
text{* Useful lemma *}
chaieb@26123
  1136
chaieb@26123
  1137
lemma (in idom_char_0) constant_degree: "constant (poly p) \<longleftrightarrow> degree p = 0" (is "?lhs = ?rhs")
chaieb@26123
  1138
proof
chaieb@26123
  1139
  assume l: ?lhs
chaieb@26123
  1140
  from l[unfolded constant_def, rule_format, of _ "zero"]
chaieb@26123
  1141
  have th: "poly p = poly [poly p 0]" apply - by (rule ext, simp)
chaieb@26123
  1142
  from degree_unique[OF th] show ?rhs by (simp add: degree_def)
chaieb@26123
  1143
next
chaieb@26123
  1144
  assume r: ?rhs
chaieb@26123
  1145
  from r have "pnormalize p = [] \<or> (\<exists>k. pnormalize p = [k])"
chaieb@26123
  1146
    unfolding degree_def by (cases "pnormalize p", auto)
chaieb@26123
  1147
  then show ?lhs unfolding constant_def poly_normalize[of p, symmetric]
chaieb@26123
  1148
    by (auto simp del: poly_normalize)
chaieb@26123
  1149
qed
chaieb@26123
  1150
chaieb@26123
  1151
(* It would be nicer to prove this without using algebraic closure...        *)
chaieb@26123
  1152
chaieb@26123
  1153
lemma divides_degree_lemma: assumes dpn: "degree (p::complex list) = n"
chaieb@26123
  1154
  shows "n \<le> degree (p *** q) \<or> poly (p *** q) = poly []"
chaieb@26123
  1155
  using dpn
chaieb@26123
  1156
proof(induct n arbitrary: p q)
chaieb@26123
  1157
  case 0 thus ?case by simp
chaieb@26123
  1158
next
chaieb@26123
  1159
  case (Suc n p q)
chaieb@26123
  1160
  from Suc.prems fundamental_theorem_of_algebra[of p] constant_degree[of p]
chaieb@26123
  1161
  obtain a where a: "poly p a = 0" by auto
chaieb@26123
  1162
  then obtain r where r: "p = pmult [-a, 1] r" unfolding poly_linear_divides
chaieb@26123
  1163
    using Suc.prems by (auto simp add: degree_def)
chaieb@26123
  1164
  {assume h: "poly (pmult r q) = poly []"
chaieb@26123
  1165
    hence "poly (pmult p q) = poly []" using r
chaieb@26123
  1166
      apply - apply (rule ext)  by (auto simp add: poly_entire poly_mult poly_add poly_cmult) hence ?case by blast}
chaieb@26123
  1167
  moreover
chaieb@26123
  1168
  {assume h: "poly (pmult r q) \<noteq> poly []" 
chaieb@26123
  1169
    hence r0: "poly r \<noteq> poly []" and q0: "poly q \<noteq> poly []"
chaieb@26123
  1170
      by (auto simp add: poly_entire)
chaieb@26123
  1171
    have eq: "poly (pmult p q) = poly (pmult [-a, 1] (pmult r q))"
chaieb@26123
  1172
      apply - apply (rule ext)
chaieb@26123
  1173
      by (simp add: r poly_mult poly_add poly_cmult ring_simps)
chaieb@26123
  1174
    from linear_mul_degree[OF h, of "- a"]
chaieb@26123
  1175
    have dqe: "degree (pmult p q) = degree (pmult r q) + 1"
chaieb@26123
  1176
      unfolding degree_unique[OF eq] .
chaieb@26123
  1177
    from linear_mul_degree[OF r0, of "- a", unfolded r[symmetric]] r Suc.prems 
chaieb@26123
  1178
    have dr: "degree r = n" by auto
chaieb@26123
  1179
    from  Suc.hyps[OF dr, of q] have "Suc n \<le> degree (pmult p q)"
chaieb@26123
  1180
      unfolding dqe using h by (auto simp del: poly.simps) 
chaieb@26123
  1181
    hence ?case by blast}
chaieb@26123
  1182
  ultimately show ?case by blast
chaieb@26123
  1183
qed
chaieb@26123
  1184
chaieb@26123
  1185
lemma divides_degree: assumes pq: "p divides (q:: complex list)"
chaieb@26123
  1186
  shows "degree p \<le> degree q \<or> poly q = poly []"
chaieb@26123
  1187
using pq  divides_degree_lemma[OF refl, of p]
chaieb@26123
  1188
apply (auto simp add: divides_def poly_entire)
chaieb@26123
  1189
apply atomize
chaieb@26123
  1190
apply (erule_tac x="qa" in allE, auto)
chaieb@26123
  1191
apply (subgoal_tac "degree q = degree (p *** qa)", simp)
chaieb@26123
  1192
apply (rule degree_unique, simp)
chaieb@26123
  1193
done
chaieb@26123
  1194
chaieb@26123
  1195
(* Arithmetic operations on multivariate polynomials.                        *)
chaieb@26123
  1196
chaieb@26123
  1197
lemma mpoly_base_conv: 
chaieb@26123
  1198
  "(0::complex) \<equiv> poly [] x" "c \<equiv> poly [c] x" "x \<equiv> poly [0,1] x" by simp_all
chaieb@26123
  1199
chaieb@26123
  1200
lemma mpoly_norm_conv: 
chaieb@26123
  1201
  "poly [0] (x::complex) \<equiv> poly [] x" "poly [poly [] y] x \<equiv> poly [] x" by simp_all
chaieb@26123
  1202
chaieb@26123
  1203
lemma mpoly_sub_conv: 
chaieb@26123
  1204
  "poly p (x::complex) - poly q x \<equiv> poly p x + -1 * poly q x"
chaieb@26123
  1205
  by (simp add: diff_def)
chaieb@26123
  1206
chaieb@26123
  1207
lemma poly_pad_rule: "poly p x = 0 ==> poly (0#p) x = (0::complex)" by simp
chaieb@26123
  1208
chaieb@26123
  1209
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
  1210
chaieb@26123
  1211
lemma resolve_eq_raw:  "poly [] x \<equiv> 0" "poly [c] x \<equiv> (c::complex)" by auto
chaieb@26123
  1212
lemma  resolve_eq_then: "(P \<Longrightarrow> (Q \<equiv> Q1)) \<Longrightarrow> (\<not>P \<Longrightarrow> (Q \<equiv> Q2))
chaieb@26123
  1213
  \<Longrightarrow> Q \<equiv> P \<and> Q1 \<or> \<not>P\<and> Q2" apply (atomize (full)) by blast 
chaieb@26123
  1214
lemma expand_ex_beta_conv: "list_ex P [c] \<equiv> P c" by simp
chaieb@26123
  1215
chaieb@26123
  1216
lemma poly_divides_pad_rule: 
chaieb@26123
  1217
  fixes p q :: "complex list"
chaieb@26123
  1218
  assumes pq: "p divides q"
chaieb@26123
  1219
  shows "p divides ((0::complex)#q)"
chaieb@26123
  1220
proof-
chaieb@26123
  1221
  from pq obtain r where r: "poly q = poly (p *** r)" unfolding divides_def by blast
chaieb@26123
  1222
  hence "poly (0#q) = poly (p *** ([0,1] *** r))" 
chaieb@26123
  1223
    by - (rule ext, simp add: poly_mult poly_cmult poly_add)
chaieb@26123
  1224
  thus ?thesis unfolding divides_def by blast
chaieb@26123
  1225
qed
chaieb@26123
  1226
chaieb@26123
  1227
lemma poly_divides_pad_const_rule: 
chaieb@26123
  1228
  fixes p q :: "complex list"
chaieb@26123
  1229
  assumes pq: "p divides q"
chaieb@26123
  1230
  shows "p divides (a %* q)"
chaieb@26123
  1231
proof-
chaieb@26123
  1232
  from pq obtain r where r: "poly q = poly (p *** r)" unfolding divides_def by blast
chaieb@26123
  1233
  hence "poly (a %* q) = poly (p *** (a %* r))" 
chaieb@26123
  1234
    by - (rule ext, simp add: poly_mult poly_cmult poly_add)
chaieb@26123
  1235
  thus ?thesis unfolding divides_def by blast
chaieb@26123
  1236
qed
chaieb@26123
  1237
chaieb@26123
  1238
chaieb@26123
  1239
lemma poly_divides_conv0:  
chaieb@26123
  1240
  fixes p :: "complex list"
chaieb@26123
  1241
  assumes lgpq: "length q < length p" and lq:"last p \<noteq> 0"
chaieb@26123
  1242
  shows "p divides q \<equiv> (\<not> (list_ex (\<lambda>c. c \<noteq> 0) q))" (is "?lhs \<equiv> ?rhs")
chaieb@26123
  1243
proof-
chaieb@26123
  1244
  {assume r: ?rhs 
chaieb@26123
  1245
    hence eq: "poly q = poly []" unfolding poly_zero 
chaieb@26123
  1246
      by (simp add: list_all_iff list_ex_iff)
chaieb@26123
  1247
    hence "poly q = poly (p *** [])" by - (rule ext, simp add: poly_mult)
chaieb@26123
  1248
    hence ?lhs unfolding divides_def  by blast}
chaieb@26123
  1249
  moreover
chaieb@26123
  1250
  {assume l: ?lhs
chaieb@26123
  1251
    have ath: "\<And>lq lp dq::nat. lq < lp ==> lq \<noteq> 0 \<Longrightarrow> dq <= lq - 1 ==> dq < lp - 1"
chaieb@26123
  1252
      by arith
chaieb@26123
  1253
    {assume q0: "length q = 0"
chaieb@26123
  1254
      hence "q = []" by simp
chaieb@26123
  1255
      hence ?rhs by simp}
chaieb@26123
  1256
    moreover
chaieb@26123
  1257
    {assume lgq0: "length q \<noteq> 0"
chaieb@26123
  1258
      from pnormalize_length[of q] have dql: "degree q \<le> length q - 1" 
chaieb@26123
  1259
	unfolding degree_def by simp
chaieb@26123
  1260
      from ath[OF lgpq lgq0 dql, unfolded pnormal_degree[OF lq, symmetric]] divides_degree[OF l] have "poly q = poly []" by auto
chaieb@26123
  1261
      hence ?rhs unfolding poly_zero by (simp add: list_all_iff list_ex_iff)}
chaieb@26123
  1262
    ultimately have ?rhs by blast }
chaieb@26123
  1263
  ultimately show "?lhs \<equiv> ?rhs" by - (atomize (full), blast) 
chaieb@26123
  1264
qed
chaieb@26123
  1265
chaieb@26123
  1266
lemma poly_divides_conv1: 
chaieb@26123
  1267
  assumes a0: "a\<noteq> (0::complex)" and pp': "(p::complex list) divides p'"
chaieb@26123
  1268
  and qrp': "\<And>x. a * poly q x - poly p' x \<equiv> poly r x"
chaieb@26123
  1269
  shows "p divides q \<equiv> p divides (r::complex list)" (is "?lhs \<equiv> ?rhs")
chaieb@26123
  1270
proof-
chaieb@26123
  1271
  {
chaieb@26123
  1272
  from pp' obtain t where t: "poly p' = poly (p *** t)" 
chaieb@26123
  1273
    unfolding divides_def by blast
chaieb@26123
  1274
  {assume l: ?lhs
chaieb@26123
  1275
    then obtain u where u: "poly q = poly (p *** u)" unfolding divides_def by blast
chaieb@26123
  1276
     have "poly r = poly (p *** ((a %* u) +++ (-- t)))"
chaieb@26123
  1277
       using u qrp' t
chaieb@26123
  1278
       by - (rule ext, 
chaieb@26123
  1279
	 simp add: poly_add poly_mult poly_cmult poly_minus ring_simps)
chaieb@26123
  1280
     then have ?rhs unfolding divides_def by blast}
chaieb@26123
  1281
  moreover
chaieb@26123
  1282
  {assume r: ?rhs
chaieb@26123
  1283
    then obtain u where u: "poly r = poly (p *** u)" unfolding divides_def by blast
chaieb@26123
  1284
    from u t qrp' a0 have "poly q = poly (p *** ((1/a) %* (u +++ t)))"
chaieb@26123
  1285
      by - (rule ext, atomize (full), simp add: poly_mult poly_add poly_cmult field_simps)
chaieb@26123
  1286
    hence ?lhs  unfolding divides_def by blast}
chaieb@26123
  1287
  ultimately have "?lhs = ?rhs" by blast }
chaieb@26123
  1288
thus "?lhs \<equiv> ?rhs"  by - (atomize(full), blast) 
chaieb@26123
  1289
qed
chaieb@26123
  1290
chaieb@26123
  1291
lemma basic_cqe_conv1:
chaieb@26123
  1292
  "(\<exists>x. poly p x = 0 \<and> poly [] x \<noteq> 0) \<equiv> False"
chaieb@26123
  1293
  "(\<exists>x. poly [] x \<noteq> 0) \<equiv> False"
chaieb@26123
  1294
  "(\<exists>x. poly [c] x \<noteq> 0) \<equiv> c\<noteq>0"
chaieb@26123
  1295
  "(\<exists>x. poly [] x = 0) \<equiv> True"
chaieb@26123
  1296
  "(\<exists>x. poly [c] x = 0) \<equiv> c = 0" by simp_all
chaieb@26123
  1297
chaieb@26123
  1298
lemma basic_cqe_conv2: 
chaieb@26123
  1299
  assumes l:"last (a#b#p) \<noteq> 0" 
chaieb@26123
  1300
  shows "(\<exists>x. poly (a#b#p) x = (0::complex)) \<equiv> True"
chaieb@26123
  1301
proof-
chaieb@26123
  1302
  {fix h t
chaieb@26123
  1303
    assume h: "h\<noteq>0" "list_all (\<lambda>c. c=(0::complex)) t"  "a#b#p = h#t"
chaieb@26123
  1304
    hence "list_all (\<lambda>c. c= 0) (b#p)" by simp
chaieb@26123
  1305
    moreover have "last (b#p) \<in> set (b#p)" by simp
chaieb@26123
  1306
    ultimately have "last (b#p) = 0" by (simp add: list_all_iff)
chaieb@26123
  1307
    with l have False by simp}
chaieb@26123
  1308
  hence th: "\<not> (\<exists> h t. h\<noteq>0 \<and> list_all (\<lambda>c. c=0) t \<and> a#b#p = h#t)"
chaieb@26123
  1309
    by blast
chaieb@26123
  1310
  from fundamental_theorem_of_algebra_alt[OF th] 
chaieb@26123
  1311
  show "(\<exists>x. poly (a#b#p) x = (0::complex)) \<equiv> True" by auto
chaieb@26123
  1312
qed
chaieb@26123
  1313
chaieb@26123
  1314
lemma  basic_cqe_conv_2b: "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> (list_ex (\<lambda>c. c \<noteq> 0) p)"
chaieb@26123
  1315
proof-
chaieb@26123
  1316
  have "\<not> (list_ex (\<lambda>c. c \<noteq> 0) p) \<longleftrightarrow> poly p = poly []" 
chaieb@26123
  1317
    by (simp add: poly_zero list_all_iff list_ex_iff)
chaieb@26123
  1318
  also have "\<dots> \<longleftrightarrow> (\<not> (\<exists>x. poly p x \<noteq> 0))" by (auto intro: ext)
chaieb@26123
  1319
  finally show "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> (list_ex (\<lambda>c. c \<noteq> 0) p)"
chaieb@26123
  1320
    by - (atomize (full), blast)
chaieb@26123
  1321
qed
chaieb@26123
  1322
chaieb@26123
  1323
lemma basic_cqe_conv3:
chaieb@26123
  1324
  fixes p q :: "complex list"
chaieb@26123
  1325
  assumes l: "last (a#p) \<noteq> 0" 
chaieb@26123
  1326
  shows "(\<exists>x. poly (a#p) x =0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((a#p) divides (q %^ (length p)))"
chaieb@26123
  1327
proof-
chaieb@26123
  1328
  note np = pnormalize_eq[OF l]
chaieb@26123
  1329
  {assume "poly (a#p) = poly []" hence False using l
chaieb@26123
  1330
      unfolding poly_zero apply (auto simp add: list_all_iff del: last.simps)
chaieb@26123
  1331
      apply (cases p, simp_all) done}
chaieb@26123
  1332
  then have p0: "poly (a#p) \<noteq> poly []"  by blast
chaieb@26123
  1333
  from np have dp:"degree (a#p) = length p" by (simp add: degree_def)
chaieb@26123
  1334
  from nullstellensatz_univariate[of "a#p" q] p0 dp
chaieb@26123
  1335
  show "(\<exists>x. poly (a#p) x =0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((a#p) divides (q %^ (length p)))"
chaieb@26123
  1336
    by - (atomize (full), auto)
chaieb@26123
  1337
qed
chaieb@26123
  1338
chaieb@26123
  1339
lemma basic_cqe_conv4:
chaieb@26123
  1340
  fixes p q :: "complex list"
chaieb@26123
  1341
  assumes h: "\<And>x. poly (q %^ n) x \<equiv> poly r x"
chaieb@26123
  1342
  shows "p divides (q %^ n) \<equiv> p divides r"
chaieb@26123
  1343
proof-
chaieb@26123
  1344
  from h have "poly (q %^ n) = poly r" by (auto intro: ext)  
chaieb@26123
  1345
  thus "p divides (q %^ n) \<equiv> p divides r" unfolding divides_def by simp
chaieb@26123
  1346
qed
chaieb@26123
  1347
chaieb@26123
  1348
lemma pmult_Cons_Cons: "((a::complex)#b#p) *** q = (a %*q) +++ (0#((b#p) *** q))"
chaieb@26123
  1349
  by simp
chaieb@26123
  1350
chaieb@26123
  1351
lemma elim_neg_conv: "- z \<equiv> (-1) * (z::complex)" by simp
chaieb@26123
  1352
lemma eqT_intr: "PROP P \<Longrightarrow> (True \<Longrightarrow> PROP P )" "PROP P \<Longrightarrow> True" by blast+
chaieb@26123
  1353
lemma negate_negate_rule: "Trueprop P \<equiv> \<not> P \<equiv> False" by (atomize (full), auto)
chaieb@26123
  1354
lemma last_simps: "last [x] = x" "last (x#y#ys) = last (y#ys)" by simp_all
chaieb@26123
  1355
lemma length_simps: "length [] = 0" "length (x#y#xs) = length xs + 2" "length [x] = 1" by simp_all
chaieb@26123
  1356
chaieb@26123
  1357
lemma complex_entire: "(z::complex) \<noteq> 0 \<and> w \<noteq> 0 \<equiv> z*w \<noteq> 0" by simp
chaieb@26123
  1358
lemma resolve_eq_ne: "(P \<equiv> True) \<equiv> (\<not>P \<equiv> False)" "(P \<equiv> False) \<equiv> (\<not>P \<equiv> True)" 
chaieb@26123
  1359
  by (atomize (full)) simp_all
chaieb@26123
  1360
lemma cqe_conv1: "poly [] x = 0 \<longleftrightarrow> True"  by simp
chaieb@26123
  1361
lemma cqe_conv2: "(p \<Longrightarrow> (q \<equiv> r)) \<equiv> ((p \<and> q) \<equiv> (p \<and> r))"  (is "?l \<equiv> ?r")
chaieb@26123
  1362
proof
chaieb@26123
  1363
  assume "p \<Longrightarrow> q \<equiv> r" thus "p \<and> q \<equiv> p \<and> r" apply - apply (atomize (full)) by blast
chaieb@26123
  1364
next
chaieb@26123
  1365
  assume "p \<and> q \<equiv> p \<and> r" "p"
chaieb@26123
  1366
  thus "q \<equiv> r" apply - apply (atomize (full)) apply blast done
chaieb@26123
  1367
qed
chaieb@26123
  1368
lemma poly_const_conv: "poly [c] (x::complex) = y \<longleftrightarrow> c = y" by simp
chaieb@26123
  1369
chaieb@26123
  1370
end