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