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