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