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