src/HOL/Library/Polynomial.thy
 changeset 62352 35a9e1cbb5b3 parent 62351 fd049b54ad68 child 62422 4aa35fd6c152
```     1.1 --- a/src/HOL/Library/Polynomial.thy	Wed Feb 17 21:51:58 2016 +0100
1.2 +++ b/src/HOL/Library/Polynomial.thy	Wed Feb 17 21:51:58 2016 +0100
1.3 @@ -1,13 +1,15 @@
1.4  (*  Title:      HOL/Library/Polynomial.thy
1.5      Author:     Brian Huffman
1.6      Author:     Clemens Ballarin
1.7 +    Author:     Amine Chaieb
1.8      Author:     Florian Haftmann
1.9  *)
1.10
1.11  section \<open>Polynomials as type over a ring structure\<close>
1.12
1.13  theory Polynomial
1.14 -imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/More_List" "~~/src/HOL/Library/Infinite_Set"
1.15 +imports Main "~~/src/HOL/Deriv" "~~/src/HOL/Library/More_List"
1.16 +  "~~/src/HOL/Library/Infinite_Set"
1.17  begin
1.18
1.19  subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
1.20 @@ -1900,133 +1902,6 @@
1.21    by (subst (asm) order_root) auto
1.22
1.23
1.24 -subsection \<open>GCD of polynomials\<close>
1.25 -
1.26 -instantiation poly :: (field) gcd
1.27 -begin
1.28 -
1.29 -function gcd_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
1.30 -where
1.31 -  "gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x"
1.32 -| "y \<noteq> 0 \<Longrightarrow> gcd (x::'a poly) y = gcd y (x mod y)"
1.33 -by auto
1.34 -
1.35 -termination "gcd :: _ poly \<Rightarrow> _"
1.36 -by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
1.37 -   (auto dest: degree_mod_less)
1.38 -
1.39 -declare gcd_poly.simps [simp del]
1.40 -
1.41 -definition lcm_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
1.42 -where
1.43 -  "lcm_poly a b = a * b div smult (coeff a (degree a) * coeff b (degree b)) (gcd a b)"
1.44 -
1.45 -instance ..
1.46 -
1.47 -end
1.48 -
1.49 -lemma
1.50 -  fixes x y :: "_ poly"
1.51 -  shows poly_gcd_dvd1 [iff]: "gcd x y dvd x"
1.52 -    and poly_gcd_dvd2 [iff]: "gcd x y dvd y"
1.53 -  apply (induct x y rule: gcd_poly.induct)
1.54 -  apply (simp_all add: gcd_poly.simps)
1.55 -  apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
1.56 -  apply (blast dest: dvd_mod_imp_dvd)
1.57 -  done
1.58 -
1.59 -lemma poly_gcd_greatest:
1.60 -  fixes k x y :: "_ poly"
1.61 -  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
1.62 -  by (induct x y rule: gcd_poly.induct)
1.63 -     (simp_all add: gcd_poly.simps dvd_mod dvd_smult)
1.64 -
1.65 -lemma dvd_poly_gcd_iff [iff]:
1.66 -  fixes k x y :: "_ poly"
1.67 -  shows "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
1.68 -  by (auto intro!: poly_gcd_greatest intro: dvd_trans [of _ "gcd x y"])
1.69 -
1.70 -lemma poly_gcd_monic:
1.71 -  fixes x y :: "_ poly"
1.72 -  shows "coeff (gcd x y) (degree (gcd x y)) =
1.73 -    (if x = 0 \<and> y = 0 then 0 else 1)"
1.74 -  by (induct x y rule: gcd_poly.induct)
1.75 -     (simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero)
1.76 -
1.77 -lemma poly_gcd_zero_iff [simp]:
1.78 -  fixes x y :: "_ poly"
1.79 -  shows "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
1.80 -  by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
1.81 -
1.82 -lemma poly_gcd_0_0 [simp]:
1.83 -  "gcd (0::_ poly) 0 = 0"
1.84 -  by simp
1.85 -
1.86 -lemma poly_dvd_antisym:
1.87 -  fixes p q :: "'a::idom poly"
1.88 -  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
1.89 -  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
1.90 -proof (cases "p = 0")
1.91 -  case True with coeff show "p = q" by simp
1.92 -next
1.93 -  case False with coeff have "q \<noteq> 0" by auto
1.94 -  have degree: "degree p = degree q"
1.95 -    using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
1.96 -    by (intro order_antisym dvd_imp_degree_le)
1.97 -
1.98 -  from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
1.99 -  with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
1.100 -  with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
1.101 -    by (simp add: degree_mult_eq)
1.102 -  with coeff a show "p = q"
1.103 -    by (cases a, auto split: if_splits)
1.104 -qed
1.105 -
1.106 -lemma poly_gcd_unique:
1.107 -  fixes d x y :: "_ poly"
1.108 -  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
1.109 -    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
1.110 -    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
1.111 -  shows "gcd x y = d"
1.112 -proof -
1.113 -  have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)"
1.114 -    by (simp_all add: poly_gcd_monic monic)
1.115 -  moreover have "gcd x y dvd d"
1.116 -    using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
1.117 -  moreover have "d dvd gcd x y"
1.118 -    using dvd1 dvd2 by (rule poly_gcd_greatest)
1.119 -  ultimately show ?thesis
1.120 -    by (rule poly_dvd_antisym)
1.121 -qed
1.122 -
1.123 -instance poly :: (field) semiring_gcd
1.124 -proof
1.125 -  fix p q :: "'a::field poly"
1.126 -  show "normalize (gcd p q) = gcd p q"
1.127 -    by (induct p q rule: gcd_poly.induct)
1.128 -      (simp_all add: gcd_poly.simps normalize_poly_def)
1.129 -  show "lcm p q = normalize (p * q) div gcd p q"
1.130 -    by (simp add: coeff_degree_mult div_smult_left div_smult_right lcm_poly_def normalize_poly_def)
1.131 -      (metis (no_types, lifting) div_smult_right inverse_mult_distrib inverse_zero mult.commute pdivmod_rel pdivmod_rel_def smult_eq_0_iff)
1.132 -qed simp_all
1.133 -
1.134 -lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)"
1.135 -by (rule poly_gcd_unique) simp_all
1.136 -
1.137 -lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)"
1.138 -by (rule poly_gcd_unique) simp_all
1.139 -
1.140 -lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)"
1.141 -by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
1.142 -
1.143 -lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)"
1.144 -by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
1.145 -
1.146 -lemma poly_gcd_code [code]:
1.147 -  "gcd x y = (if y = 0 then normalize x else gcd y (x mod (y :: _ poly)))"
1.148 -  by (simp add: gcd_poly.simps)
1.149 -
1.150 -
1.151  subsection \<open>Additional induction rules on polynomials\<close>
1.152
1.153  text \<open>
1.154 @@ -2338,8 +2213,630 @@
1.155
1.158 +
1.159 +
1.160 +subsection \<open>Derivatives of univariate polynomials\<close>
1.161 +
1.162 +function pderiv :: "('a :: semidom) poly \<Rightarrow> 'a poly"
1.163 +where
1.164 +  [simp del]: "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))"
1.165 +  by (auto intro: pCons_cases)
1.166 +
1.167 +termination pderiv
1.168 +  by (relation "measure degree") simp_all
1.169 +
1.170 +lemma pderiv_0 [simp]:
1.171 +  "pderiv 0 = 0"
1.172 +  using pderiv.simps [of 0 0] by simp
1.173 +
1.174 +lemma pderiv_pCons:
1.175 +  "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
1.176 +  by (simp add: pderiv.simps)
1.177 +
1.178 +lemma pderiv_1 [simp]: "pderiv 1 = 0"
1.179 +  unfolding one_poly_def by (simp add: pderiv_pCons)
1.180 +
1.181 +lemma pderiv_of_nat  [simp]: "pderiv (of_nat n) = 0"
1.182 +  and pderiv_numeral [simp]: "pderiv (numeral m) = 0"
1.183 +  by (simp_all add: of_nat_poly numeral_poly pderiv_pCons)
1.184 +
1.185 +lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
1.186 +  by (induct p arbitrary: n)
1.187 +     (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
1.188 +
1.189 +fun pderiv_coeffs_code :: "('a :: semidom) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
1.190 +  "pderiv_coeffs_code f (x # xs) = cCons (f * x) (pderiv_coeffs_code (f+1) xs)"
1.191 +| "pderiv_coeffs_code f [] = []"
1.192 +
1.193 +definition pderiv_coeffs :: "('a :: semidom) list \<Rightarrow> 'a list" where
1.194 +  "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)"
1.195 +
1.196 +(* Efficient code for pderiv contributed by RenĂ© Thiemann and Akihisa Yamada *)
1.197 +lemma pderiv_coeffs_code:
1.198 +  "nth_default 0 (pderiv_coeffs_code f xs) n = (f + of_nat n) * (nth_default 0 xs n)"
1.199 +proof (induct xs arbitrary: f n)
1.200 +  case (Cons x xs f n)
1.201 +  show ?case
1.202 +  proof (cases n)
1.203 +    case 0
1.204 +    thus ?thesis by (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0", auto simp: cCons_def)
1.205 +  next
1.206 +    case (Suc m) note n = this
1.207 +    show ?thesis
1.208 +    proof (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0")
1.209 +      case False
1.210 +      hence "nth_default 0 (pderiv_coeffs_code f (x # xs)) n =
1.211 +               nth_default 0 (pderiv_coeffs_code (f + 1) xs) m"
1.212 +        by (auto simp: cCons_def n)
1.213 +      also have "\<dots> = (f + of_nat n) * (nth_default 0 xs m)"
1.215 +      finally show ?thesis by (simp add: n)
1.216 +    next
1.217 +      case True
1.218 +      {
1.219 +        fix g
1.220 +        have "pderiv_coeffs_code g xs = [] \<Longrightarrow> g + of_nat m = 0 \<or> nth_default 0 xs m = 0"
1.221 +        proof (induct xs arbitrary: g m)
1.222 +          case (Cons x xs g)
1.223 +          from Cons(2) have empty: "pderiv_coeffs_code (g + 1) xs = []"
1.224 +                            and g: "(g = 0 \<or> x = 0)"
1.225 +            by (auto simp: cCons_def split: if_splits)
1.226 +          note IH = Cons(1)[OF empty]
1.227 +          from IH[of m] IH[of "m - 1"] g
1.228 +          show ?case by (cases m, auto simp: field_simps)
1.229 +        qed simp
1.230 +      } note empty = this
1.231 +      from True have "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = 0"
1.232 +        by (auto simp: cCons_def n)
1.233 +      moreover have "(f + of_nat n) * nth_default 0 (x # xs) n = 0" using True
1.234 +        by (simp add: n, insert empty[of "f+1"], auto simp: field_simps)
1.235 +      ultimately show ?thesis by simp
1.236 +    qed
1.237 +  qed
1.238 +qed simp
1.239 +
1.240 +lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda> i. f (Suc i)) [0 ..< n]"
1.241 +  by (induct n arbitrary: f, auto)
1.242 +
1.243 +lemma coeffs_pderiv_code [code abstract]:
1.244 +  "coeffs (pderiv p) = pderiv_coeffs (coeffs p)" unfolding pderiv_coeffs_def
1.245 +proof (rule coeffs_eqI, unfold pderiv_coeffs_code coeff_pderiv, goal_cases)
1.246 +  case (1 n)
1.247 +  have id: "coeff p (Suc n) = nth_default 0 (map (\<lambda>i. coeff p (Suc i)) [0..<degree p]) n"
1.248 +    by (cases "n < degree p", auto simp: nth_default_def coeff_eq_0)
1.249 +  show ?case unfolding coeffs_def map_upt_Suc by (auto simp: id)
1.250 +next
1.251 +  case 2
1.252 +  obtain n xs where id: "tl (coeffs p) = xs" "(1 :: 'a) = n" by auto
1.253 +  from 2 show ?case
1.254 +    unfolding id by (induct xs arbitrary: n, auto simp: cCons_def)
1.255 +qed
1.256 +
1.257 +context
1.258 +  assumes "SORT_CONSTRAINT('a::{semidom, semiring_char_0})"
1.259 +begin
1.260 +
1.261 +lemma pderiv_eq_0_iff:
1.262 +  "pderiv (p :: 'a poly) = 0 \<longleftrightarrow> degree p = 0"
1.263 +  apply (rule iffI)
1.264 +  apply (cases p, simp)
1.265 +  apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
1.266 +  apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
1.267 +  done
1.268 +
1.269 +lemma degree_pderiv: "degree (pderiv (p :: 'a poly)) = degree p - 1"
1.270 +  apply (rule order_antisym [OF degree_le])
1.271 +  apply (simp add: coeff_pderiv coeff_eq_0)
1.272 +  apply (cases "degree p", simp)
1.273 +  apply (rule le_degree)
1.274 +  apply (simp add: coeff_pderiv del: of_nat_Suc)
1.275 +  apply (metis degree_0 leading_coeff_0_iff nat.distinct(1))
1.276 +  done
1.277 +
1.278 +lemma not_dvd_pderiv:
1.279 +  assumes "degree (p :: 'a poly) \<noteq> 0"
1.280 +  shows "\<not> p dvd pderiv p"
1.281 +proof
1.282 +  assume dvd: "p dvd pderiv p"
1.283 +  then obtain q where p: "pderiv p = p * q" unfolding dvd_def by auto
1.284 +  from dvd have le: "degree p \<le> degree (pderiv p)"
1.285 +    by (simp add: assms dvd_imp_degree_le pderiv_eq_0_iff)
1.286 +  from this[unfolded degree_pderiv] assms show False by auto
1.287 +qed
1.288 +
1.289 +lemma dvd_pderiv_iff [simp]: "(p :: 'a poly) dvd pderiv p \<longleftrightarrow> degree p = 0"
1.290 +  using not_dvd_pderiv[of p] by (auto simp: pderiv_eq_0_iff [symmetric])
1.291 +
1.292 +end
1.293 +
1.294 +lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
1.296 +
1.297 +lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
1.298 +by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
1.299 +
1.300 +lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p"
1.301 +by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
1.302 +
1.303 +lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
1.304 +by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
1.305 +
1.306 +lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
1.307 +by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
1.308 +
1.309 +lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
1.310 +by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
1.311 +
1.312 +lemma pderiv_power_Suc:
1.313 +  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
1.314 +apply (induct n)
1.315 +apply simp
1.316 +apply (subst power_Suc)
1.317 +apply (subst pderiv_mult)
1.318 +apply (erule ssubst)
1.319 +apply (simp only: of_nat_Suc smult_add_left smult_1_left)
1.321 +done
1.322 +
1.323 +lemma pderiv_setprod: "pderiv (setprod f (as)) =
1.324 +  (\<Sum>a \<in> as. setprod f (as - {a}) * pderiv (f a))"
1.325 +proof (induct as rule: infinite_finite_induct)
1.326 +  case (insert a as)
1.327 +  hence id: "setprod f (insert a as) = f a * setprod f as"
1.328 +    "\<And> g. setsum g (insert a as) = g a + setsum g as"
1.329 +    "insert a as - {a} = as"
1.330 +    by auto
1.331 +  {
1.332 +    fix b
1.333 +    assume "b \<in> as"
1.334 +    hence id2: "insert a as - {b} = insert a (as - {b})" using \<open>a \<notin> as\<close> by auto
1.335 +    have "setprod f (insert a as - {b}) = f a * setprod f (as - {b})"
1.336 +      unfolding id2
1.337 +      by (subst setprod.insert, insert insert, auto)
1.338 +  } note id2 = this
1.339 +  show ?case
1.340 +    unfolding id pderiv_mult insert(3) setsum_right_distrib
1.341 +    by (auto simp add: ac_simps id2 intro!: setsum.cong)
1.342 +qed auto
1.343 +
1.344 +lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
1.345 +by (rule DERIV_cong, rule DERIV_pow, simp)
1.346 +declare DERIV_pow2 [simp] DERIV_pow [simp]
1.347 +
1.348 +lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
1.349 +by (rule DERIV_cong, rule DERIV_add, auto)
1.350 +
1.351 +lemma poly_DERIV [simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
1.352 +  by (induct p, auto intro!: derivative_eq_intros simp add: pderiv_pCons)
1.353 +
1.354 +lemma continuous_on_poly [continuous_intros]:
1.355 +  fixes p :: "'a :: {real_normed_field} poly"
1.356 +  assumes "continuous_on A f"
1.357 +  shows   "continuous_on A (\<lambda>x. poly p (f x))"
1.358 +proof -
1.359 +  have "continuous_on A (\<lambda>x. (\<Sum>i\<le>degree p. (f x) ^ i * coeff p i))"
1.360 +    by (intro continuous_intros assms)
1.361 +  also have "\<dots> = (\<lambda>x. poly p (f x))" by (intro ext) (simp add: poly_altdef mult_ac)
1.362 +  finally show ?thesis .
1.363 +qed
1.364 +
1.365 +text\<open>Consequences of the derivative theorem above\<close>
1.366 +
1.367 +lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
1.369 +apply (blast intro: poly_DERIV)
1.370 +done
1.371 +
1.372 +lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
1.373 +by (rule poly_DERIV [THEN DERIV_isCont])
1.374 +
1.375 +lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
1.376 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
1.377 +using IVT_objl [of "poly p" a 0 b]
1.378 +by (auto simp add: order_le_less)
1.379 +
1.380 +lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
1.381 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
1.382 +by (insert poly_IVT_pos [where p = "- p" ]) simp
1.383 +
1.384 +lemma poly_IVT:
1.385 +  fixes p::"real poly"
1.386 +  assumes "a<b" and "poly p a * poly p b < 0"
1.387 +  shows "\<exists>x>a. x < b \<and> poly p x = 0"
1.388 +by (metis assms(1) assms(2) less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos)
1.389 +
1.390 +lemma poly_MVT: "(a::real) < b ==>
1.391 +     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
1.392 +using MVT [of a b "poly p"]
1.393 +apply auto
1.394 +apply (rule_tac x = z in exI)
1.395 +apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
1.396 +done
1.397 +
1.398 +lemma poly_MVT':
1.399 +  assumes "{min a b..max a b} \<subseteq> A"
1.400 +  shows   "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) (x::real)"
1.401 +proof (cases a b rule: linorder_cases)
1.402 +  case less
1.403 +  from poly_MVT[OF less, of p] guess x by (elim exE conjE)
1.404 +  thus ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms])
1.405 +
1.406 +next
1.407 +  case greater
1.408 +  from poly_MVT[OF greater, of p] guess x by (elim exE conjE)
1.409 +  thus ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms])
1.410 +qed (insert assms, auto)
1.411 +
1.412 +lemma poly_pinfty_gt_lc:
1.413 +  fixes p:: "real poly"
1.414 +  assumes  "lead_coeff p > 0"
1.415 +  shows "\<exists> n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p" using assms
1.416 +proof (induct p)
1.417 +  case 0
1.418 +  thus ?case by auto
1.419 +next
1.420 +  case (pCons a p)
1.421 +  have "\<lbrakk>a\<noteq>0;p=0\<rbrakk> \<Longrightarrow> ?case" by auto
1.422 +  moreover have "p\<noteq>0 \<Longrightarrow> ?case"
1.423 +    proof -
1.424 +      assume "p\<noteq>0"
1.425 +      then obtain n1 where gte_lcoeff:"\<forall>x\<ge>n1. lead_coeff p \<le> poly p x" using that pCons by auto
1.426 +      have gt_0:"lead_coeff p >0" using pCons(3) \<open>p\<noteq>0\<close> by auto
1.427 +      def n\<equiv>"max n1 (1+ \<bar>a\<bar>/(lead_coeff p))"
1.428 +      show ?thesis
1.429 +        proof (rule_tac x=n in exI,rule,rule)
1.430 +          fix x assume "n \<le> x"
1.431 +          hence "lead_coeff p \<le> poly p x"
1.432 +            using gte_lcoeff unfolding n_def by auto
1.433 +          hence " \<bar>a\<bar>/(lead_coeff p) \<ge> \<bar>a\<bar>/(poly p x)" and "poly p x>0" using gt_0
1.434 +            by (intro frac_le,auto)
1.435 +          hence "x\<ge>1+ \<bar>a\<bar>/(poly p x)" using \<open>n\<le>x\<close>[unfolded n_def] by auto
1.436 +          thus "lead_coeff (pCons a p) \<le> poly (pCons a p) x"
1.437 +            using \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x>0\<close> \<open>p\<noteq>0\<close>
1.438 +            by (auto simp add:field_simps)
1.439 +        qed
1.440 +    qed
1.441 +  ultimately show ?case by fastforce
1.442 +qed
1.443 +
1.444 +
1.445 +subsection \<open>Algebraic numbers\<close>
1.446 +
1.447 +text \<open>
1.448 +  Algebraic numbers can be defined in two equivalent ways: all real numbers that are
1.449 +  roots of rational polynomials or of integer polynomials. The Algebraic-Numbers AFP entry
1.450 +  uses the rational definition, but we need the integer definition.
1.451 +
1.452 +  The equivalence is obvious since any rational polynomial can be multiplied with the
1.453 +  LCM of its coefficients, yielding an integer polynomial with the same roots.
1.454 +\<close>
1.455 +subsection \<open>Algebraic numbers\<close>
1.456 +
1.457 +definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool" where
1.458 +  "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<int>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
1.459 +
1.460 +lemma algebraicI:
1.461 +  assumes "\<And>i. coeff p i \<in> \<int>" "p \<noteq> 0" "poly p x = 0"
1.462 +  shows   "algebraic x"
1.463 +  using assms unfolding algebraic_def by blast
1.464
1.465 +lemma algebraicE:
1.466 +  assumes "algebraic x"
1.467 +  obtains p where "\<And>i. coeff p i \<in> \<int>" "p \<noteq> 0" "poly p x = 0"
1.468 +  using assms unfolding algebraic_def by blast
1.469 +
1.470 +lemma quotient_of_denom_pos': "snd (quotient_of x) > 0"
1.471 +  using quotient_of_denom_pos[OF surjective_pairing] .
1.472
1.473 +lemma of_int_div_in_Ints:
1.474 +  "b dvd a \<Longrightarrow> of_int a div of_int b \<in> (\<int> :: 'a :: ring_div set)"
1.475 +proof (cases "of_int b = (0 :: 'a)")
1.476 +  assume "b dvd a" "of_int b \<noteq> (0::'a)"
1.477 +  then obtain c where "a = b * c" by (elim dvdE)
1.478 +  with \<open>of_int b \<noteq> (0::'a)\<close> show ?thesis by simp
1.479 +qed auto
1.480 +
1.481 +lemma of_int_divide_in_Ints:
1.482 +  "b dvd a \<Longrightarrow> of_int a / of_int b \<in> (\<int> :: 'a :: field set)"
1.483 +proof (cases "of_int b = (0 :: 'a)")
1.484 +  assume "b dvd a" "of_int b \<noteq> (0::'a)"
1.485 +  then obtain c where "a = b * c" by (elim dvdE)
1.486 +  with \<open>of_int b \<noteq> (0::'a)\<close> show ?thesis by simp
1.487 +qed auto
1.488 +
1.489 +lemma algebraic_altdef:
1.490 +  fixes p :: "'a :: field_char_0 poly"
1.491 +  shows "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
1.492 +proof safe
1.493 +  fix p assume rat: "\<forall>i. coeff p i \<in> \<rat>" and root: "poly p x = 0" and nz: "p \<noteq> 0"
1.494 +  def cs \<equiv> "coeffs p"
1.495 +  from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'" unfolding Rats_def by blast
1.496 +  then obtain f where f: "\<And>i. coeff p i = of_rat (f (coeff p i))"
1.497 +    by (subst (asm) bchoice_iff) blast
1.498 +  def cs' \<equiv> "map (quotient_of \<circ> f) (coeffs p)"
1.499 +  def d \<equiv> "Lcm (set (map snd cs'))"
1.500 +  def p' \<equiv> "smult (of_int d) p"
1.501 +
1.502 +  have "\<forall>n. coeff p' n \<in> \<int>"
1.503 +  proof
1.504 +    fix n :: nat
1.505 +    show "coeff p' n \<in> \<int>"
1.506 +    proof (cases "n \<le> degree p")
1.507 +      case True
1.508 +      def c \<equiv> "coeff p n"
1.509 +      def a \<equiv> "fst (quotient_of (f (coeff p n)))" and b \<equiv> "snd (quotient_of (f (coeff p n)))"
1.510 +      have b_pos: "b > 0" unfolding b_def using quotient_of_denom_pos' by simp
1.511 +      have "coeff p' n = of_int d * coeff p n" by (simp add: p'_def)
1.512 +      also have "coeff p n = of_rat (of_int a / of_int b)" unfolding a_def b_def
1.513 +        by (subst quotient_of_div [of "f (coeff p n)", symmetric])
1.514 +           (simp_all add: f [symmetric])
1.515 +      also have "of_int d * \<dots> = of_rat (of_int (a*d) / of_int b)"
1.516 +        by (simp add: of_rat_mult of_rat_divide)
1.517 +      also from nz True have "b \<in> snd ` set cs'" unfolding cs'_def
1.518 +        by (force simp: o_def b_def coeffs_def simp del: upt_Suc)
1.519 +      hence "b dvd (a * d)" unfolding d_def by simp
1.520 +      hence "of_int (a * d) / of_int b \<in> (\<int> :: rat set)"
1.521 +        by (rule of_int_divide_in_Ints)
1.522 +      hence "of_rat (of_int (a * d) / of_int b) \<in> \<int>" by (elim Ints_cases) auto
1.523 +      finally show ?thesis .
1.524 +    qed (auto simp: p'_def not_le coeff_eq_0)
1.525 +  qed
1.526 +
1.527 +  moreover have "set (map snd cs') \<subseteq> {0<..}"
1.528 +    unfolding cs'_def using quotient_of_denom_pos' by (auto simp: coeffs_def simp del: upt_Suc)
1.529 +  hence "d \<noteq> 0" unfolding d_def by (induction cs') simp_all
1.530 +  with nz have "p' \<noteq> 0" by (simp add: p'_def)
1.531 +  moreover from root have "poly p' x = 0" by (simp add: p'_def)
1.532 +  ultimately show "algebraic x" unfolding algebraic_def by blast
1.533 +next
1.534 +
1.535 +  assume "algebraic x"
1.536 +  then obtain p where p: "\<And>i. coeff p i \<in> \<int>" "poly p x = 0" "p \<noteq> 0"
1.537 +    by (force simp: algebraic_def)
1.538 +  moreover have "coeff p i \<in> \<int> \<Longrightarrow> coeff p i \<in> \<rat>" for i by (elim Ints_cases) simp
1.539 +  ultimately show  "(\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)" by auto
1.540 +qed
1.541 +
1.542 +
1.543 +text\<open>Lemmas for Derivatives\<close>
1.544 +
1.545 +lemma order_unique_lemma:
1.546 +  fixes p :: "'a::idom poly"
1.547 +  assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
1.548 +  shows "n = order a p"
1.549 +unfolding Polynomial.order_def
1.550 +apply (rule Least_equality [symmetric])
1.551 +apply (fact assms)
1.552 +apply (rule classical)
1.553 +apply (erule notE)
1.554 +unfolding not_less_eq_eq
1.555 +using assms(1) apply (rule power_le_dvd)
1.556 +apply assumption
1.557 +done
1.558 +
1.559 +lemma lemma_order_pderiv1:
1.560 +  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
1.561 +    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
1.562 +apply (simp only: pderiv_mult pderiv_power_Suc)
1.563 +apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
1.564 +done
1.565 +
1.566 +lemma lemma_order_pderiv:
1.567 +  fixes p :: "'a :: field_char_0 poly"
1.568 +  assumes n: "0 < n"
1.569 +      and pd: "pderiv p \<noteq> 0"
1.570 +      and pe: "p = [:- a, 1:] ^ n * q"
1.571 +      and nd: "~ [:- a, 1:] dvd q"
1.572 +    shows "n = Suc (order a (pderiv p))"
1.573 +using n
1.574 +proof -
1.575 +  have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
1.576 +    using assms by auto
1.577 +  obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
1.578 +    using assms by (cases n) auto
1.579 +  have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l"
1.580 +    by (auto simp del: of_nat_Suc simp: dvd_add_right_iff dvd_smult_iff)
1.581 +  have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))"
1.582 +  proof (rule order_unique_lemma)
1.583 +    show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
1.584 +      apply (subst lemma_order_pderiv1)
1.586 +      apply (metis dvdI dvd_mult2 power_Suc2)
1.587 +      apply (metis dvd_smult dvd_triv_right)
1.588 +      done
1.589 +  next
1.590 +    show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
1.591 +     apply (subst lemma_order_pderiv1)
1.592 +     by (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
1.593 +  qed
1.594 +  then show ?thesis
1.595 +    by (metis \<open>n = Suc n'\<close> pe)
1.596 +qed
1.597 +
1.598 +lemma order_decomp:
1.599 +  assumes "p \<noteq> 0"
1.600 +  shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q"
1.601 +proof -
1.602 +  from assms have A: "[:- a, 1:] ^ order a p dvd p"
1.603 +    and B: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p" by (auto dest: order)
1.604 +  from A obtain q where C: "p = [:- a, 1:] ^ order a p * q" ..
1.605 +  with B have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q"
1.606 +    by simp
1.607 +  then have "\<not> [:- a, 1:] ^ order a p * [:- a, 1:] dvd [:- a, 1:] ^ order a p * q"
1.608 +    by simp
1.609 +  then have D: "\<not> [:- a, 1:] dvd q"
1.610 +    using idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q]
1.611 +    by auto
1.612 +  from C D show ?thesis by blast
1.613 +qed
1.614 +
1.615 +lemma order_pderiv:
1.616 +  "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk> \<Longrightarrow>
1.617 +     (order a p = Suc (order a (pderiv p)))"
1.618 +apply (case_tac "p = 0", simp)
1.619 +apply (drule_tac a = a and p = p in order_decomp)
1.620 +using neq0_conv
1.621 +apply (blast intro: lemma_order_pderiv)
1.622 +done
1.623 +
1.624 +lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
1.625 +proof -
1.626 +  def i \<equiv> "order a p"
1.627 +  def j \<equiv> "order a q"
1.628 +  def t \<equiv> "[:-a, 1:]"
1.629 +  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
1.630 +    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
1.631 +  assume "p * q \<noteq> 0"
1.632 +  then show "order a (p * q) = i + j"
1.633 +    apply clarsimp
1.634 +    apply (drule order [where a=a and p=p, folded i_def t_def])
1.635 +    apply (drule order [where a=a and p=q, folded j_def t_def])
1.636 +    apply clarify
1.637 +    apply (erule dvdE)+
1.638 +    apply (rule order_unique_lemma [symmetric], fold t_def)
1.640 +    done
1.641 +qed
1.642 +
1.643 +lemma order_smult:
1.644 +  assumes "c \<noteq> 0"
1.645 +  shows "order x (smult c p) = order x p"
1.646 +proof (cases "p = 0")
1.647 +  case False
1.648 +  have "smult c p = [:c:] * p" by simp
1.649 +  also from assms False have "order x \<dots> = order x [:c:] + order x p"
1.650 +    by (subst order_mult) simp_all
1.651 +  also from assms have "order x [:c:] = 0" by (intro order_0I) auto
1.652 +  finally show ?thesis by simp
1.653 +qed simp
1.654 +
1.655 +(* Next two lemmas contributed by Wenda Li *)
1.656 +lemma order_1_eq_0 [simp]:"order x 1 = 0"
1.657 +  by (metis order_root poly_1 zero_neq_one)
1.658 +
1.659 +lemma order_power_n_n: "order a ([:-a,1:]^n)=n"
1.660 +proof (induct n) (*might be proved more concisely using nat_less_induct*)
1.661 +  case 0
1.662 +  thus ?case by (metis order_root poly_1 power_0 zero_neq_one)
1.663 +next
1.664 +  case (Suc n)
1.665 +  have "order a ([:- a, 1:] ^ Suc n)=order a ([:- a, 1:] ^ n) + order a [:-a,1:]"
1.667 +      one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right)
1.668 +  moreover have "order a [:-a,1:]=1" unfolding order_def
1.669 +    proof (rule Least_equality,rule ccontr)
1.670 +      assume  "\<not> \<not> [:- a, 1:] ^ Suc 1 dvd [:- a, 1:]"
1.671 +      hence "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" by simp
1.672 +      hence "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:] )"
1.673 +        by (rule dvd_imp_degree_le,auto)
1.674 +      thus False by auto
1.675 +    next
1.676 +      fix y assume asm:"\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]"
1.677 +      show "1 \<le> y"
1.678 +        proof (rule ccontr)
1.679 +          assume "\<not> 1 \<le> y"
1.680 +          hence "y=0" by auto
1.681 +          hence "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto
1.682 +          thus False using asm by auto
1.683 +        qed
1.684 +    qed
1.685 +  ultimately show ?case using Suc by auto
1.686 +qed
1.687 +
1.688 +text\<open>Now justify the standard squarefree decomposition, i.e. f / gcd(f,f').\<close>
1.689 +
1.690 +lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
1.691 +apply (cases "p = 0", auto)
1.692 +apply (drule order_2 [where a=a and p=p])
1.693 +apply (metis not_less_eq_eq power_le_dvd)
1.694 +apply (erule power_le_dvd [OF order_1])
1.695 +done
1.696 +
1.697 +lemma poly_squarefree_decomp_order:
1.698 +  assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0"
1.699 +  and p: "p = q * d"
1.700 +  and p': "pderiv p = e * d"
1.701 +  and d: "d = r * p + s * pderiv p"
1.702 +  shows "order a q = (if order a p = 0 then 0 else 1)"
1.703 +proof (rule classical)
1.704 +  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
1.705 +  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
1.706 +  with p have "order a p = order a q + order a d"
1.707 +    by (simp add: order_mult)
1.708 +  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
1.709 +  have "order a (pderiv p) = order a e + order a d"
1.710 +    using \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> by (simp add: order_mult)
1.711 +  have "order a p = Suc (order a (pderiv p))"
1.712 +    using \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> by (rule order_pderiv)
1.713 +  have "d \<noteq> 0" using \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> by simp
1.714 +  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
1.715 +    apply (simp add: d)
1.717 +    apply (rule dvd_mult)
1.718 +    apply (simp add: order_divides \<open>p \<noteq> 0\<close>
1.719 +           \<open>order a p = Suc (order a (pderiv p))\<close>)
1.720 +    apply (rule dvd_mult)
1.721 +    apply (simp add: order_divides)
1.722 +    done
1.723 +  then have "order a (pderiv p) \<le> order a d"
1.724 +    using \<open>d \<noteq> 0\<close> by (simp add: order_divides)
1.725 +  show ?thesis
1.726 +    using \<open>order a p = order a q + order a d\<close>
1.727 +    using \<open>order a (pderiv p) = order a e + order a d\<close>
1.728 +    using \<open>order a p = Suc (order a (pderiv p))\<close>
1.729 +    using \<open>order a (pderiv p) \<le> order a d\<close>
1.730 +    by auto
1.731 +qed
1.732 +
1.733 +lemma poly_squarefree_decomp_order2:
1.734 +     "\<lbrakk>pderiv p \<noteq> (0 :: 'a :: field_char_0 poly);
1.735 +       p = q * d;
1.736 +       pderiv p = e * d;
1.737 +       d = r * p + s * pderiv p
1.738 +      \<rbrakk> \<Longrightarrow> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
1.739 +by (blast intro: poly_squarefree_decomp_order)
1.740 +
1.741 +lemma order_pderiv2:
1.742 +  "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk>
1.743 +      \<Longrightarrow> (order a (pderiv p) = n) = (order a p = Suc n)"
1.744 +by (auto dest: order_pderiv)
1.745 +
1.746 +definition
1.747 +  rsquarefree :: "'a::idom poly => bool" where
1.748 +  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
1.749 +
1.750 +lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h :: 'a :: {semidom,semiring_char_0}:]"
1.752 +apply (case_tac p, auto split: if_splits)
1.753 +done
1.754 +
1.755 +lemma rsquarefree_roots:
1.756 +  fixes p :: "'a :: field_char_0 poly"
1.757 +  shows "rsquarefree p = (\<forall>a. \<not>(poly p a = 0 \<and> poly (pderiv p) a = 0))"
1.759 +apply (case_tac "p = 0", simp, simp)
1.760 +apply (case_tac "pderiv p = 0")
1.761 +apply simp
1.762 +apply (drule pderiv_iszero, clarsimp)
1.763 +apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
1.764 +apply (force simp add: order_root order_pderiv2)
1.765 +done
1.766 +
1.767 +lemma poly_squarefree_decomp:
1.768 +  assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0"
1.769 +    and "p = q * d"
1.770 +    and "pderiv p = e * d"
1.771 +    and "d = r * p + s * pderiv p"
1.772 +  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
1.773 +proof -
1.774 +  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
1.775 +  with \<open>p = q * d\<close> have "q \<noteq> 0" by simp
1.776 +  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
1.777 +    using assms by (rule poly_squarefree_decomp_order2)
1.778 +  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis
1.779 +    by (simp add: rsquarefree_def order_root)
1.780 +qed
1.781 +
1.782
1.783  no_notation cCons (infixr "##" 65)
1.784
```