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