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