split polynomial-related stuff from Deriv.thy into Library/Poly_Deriv.thy
authorhuffman
Wed Feb 18 19:32:26 2009 -0800 (2009-02-18)
changeset 2998557975b45ab70
parent 29983 5155c7c45233
child 29986 6b1ccda8bf19
split polynomial-related stuff from Deriv.thy into Library/Poly_Deriv.thy
src/HOL/Deriv.thy
src/HOL/IsaMakefile
src/HOL/Library/Library.thy
src/HOL/Library/Poly_Deriv.thy
     1.1 --- a/src/HOL/Deriv.thy	Wed Feb 18 17:02:38 2009 -0800
     1.2 +++ b/src/HOL/Deriv.thy	Wed Feb 18 19:32:26 2009 -0800
     1.3 @@ -1457,311 +1457,6 @@
     1.4  qed
     1.5  
     1.6  
     1.7 -subsection {* Derivatives of univariate polynomials *}
     1.8 -
     1.9 -definition
    1.10 -  pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly" where
    1.11 -  "pderiv = poly_rec 0 (\<lambda>a p p'. p + pCons 0 p')"
    1.12 -
    1.13 -lemma pderiv_0 [simp]: "pderiv 0 = 0"
    1.14 -  unfolding pderiv_def by (simp add: poly_rec_0)
    1.15 -
    1.16 -lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
    1.17 -  unfolding pderiv_def by (simp add: poly_rec_pCons)
    1.18 -
    1.19 -lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
    1.20 -  apply (induct p arbitrary: n, simp)
    1.21 -  apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
    1.22 -  done
    1.23 -
    1.24 -lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
    1.25 -  apply (rule iffI)
    1.26 -  apply (cases p, simp)
    1.27 -  apply (simp add: expand_poly_eq coeff_pderiv del: of_nat_Suc)
    1.28 -  apply (simp add: expand_poly_eq coeff_pderiv coeff_eq_0)
    1.29 -  done
    1.30 -
    1.31 -lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
    1.32 -  apply (rule order_antisym [OF degree_le])
    1.33 -  apply (simp add: coeff_pderiv coeff_eq_0)
    1.34 -  apply (cases "degree p", simp)
    1.35 -  apply (rule le_degree)
    1.36 -  apply (simp add: coeff_pderiv del: of_nat_Suc)
    1.37 -  apply (rule subst, assumption)
    1.38 -  apply (rule leading_coeff_neq_0, clarsimp)
    1.39 -  done
    1.40 -
    1.41 -lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
    1.42 -by (simp add: pderiv_pCons)
    1.43 -
    1.44 -lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
    1.45 -by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.46 -
    1.47 -lemma pderiv_minus: "pderiv (- p) = - pderiv p"
    1.48 -by (rule poly_ext, simp add: coeff_pderiv)
    1.49 -
    1.50 -lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
    1.51 -by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.52 -
    1.53 -lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
    1.54 -by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.55 -
    1.56 -lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
    1.57 -apply (induct p)
    1.58 -apply simp
    1.59 -apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
    1.60 -done
    1.61 -
    1.62 -lemma pderiv_power_Suc:
    1.63 -  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
    1.64 -apply (induct n)
    1.65 -apply simp
    1.66 -apply (subst power_Suc)
    1.67 -apply (subst pderiv_mult)
    1.68 -apply (erule ssubst)
    1.69 -apply (simp add: smult_add_left algebra_simps)
    1.70 -done
    1.71 -
    1.72 -lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
    1.73 -by (simp add: DERIV_cmult mult_commute [of _ c])
    1.74 -
    1.75 -lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
    1.76 -by (rule lemma_DERIV_subst, rule DERIV_pow, simp)
    1.77 -declare DERIV_pow2 [simp] DERIV_pow [simp]
    1.78 -
    1.79 -lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
    1.80 -by (rule lemma_DERIV_subst, rule DERIV_add, auto)
    1.81 -
    1.82 -lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
    1.83 -apply (induct p)
    1.84 -apply simp
    1.85 -apply (simp add: pderiv_pCons)
    1.86 -apply (rule lemma_DERIV_subst)
    1.87 -apply (rule DERIV_add DERIV_mult DERIV_const DERIV_ident | assumption)+
    1.88 -apply simp
    1.89 -done
    1.90 -
    1.91 -text{* Consequences of the derivative theorem above*}
    1.92 -
    1.93 -lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
    1.94 -apply (simp add: differentiable_def)
    1.95 -apply (blast intro: poly_DERIV)
    1.96 -done
    1.97 -
    1.98 -lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
    1.99 -by (rule poly_DERIV [THEN DERIV_isCont])
   1.100 -
   1.101 -lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
   1.102 -      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   1.103 -apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
   1.104 -apply (auto simp add: order_le_less)
   1.105 -done
   1.106 -
   1.107 -lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
   1.108 -      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   1.109 -by (insert poly_IVT_pos [where p = "- p" ]) simp
   1.110 -
   1.111 -lemma poly_MVT: "(a::real) < b ==>
   1.112 -     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
   1.113 -apply (drule_tac f = "poly p" in MVT, auto)
   1.114 -apply (rule_tac x = z in exI)
   1.115 -apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
   1.116 -done
   1.117 -
   1.118 -text{*Lemmas for Derivatives*}
   1.119 -
   1.120 -lemma order_unique_lemma:
   1.121 -  fixes p :: "'a::idom poly"
   1.122 -  assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
   1.123 -  shows "n = order a p"
   1.124 -unfolding Polynomial.order_def
   1.125 -apply (rule Least_equality [symmetric])
   1.126 -apply (rule assms [THEN conjunct2])
   1.127 -apply (erule contrapos_np)
   1.128 -apply (rule power_le_dvd)
   1.129 -apply (rule assms [THEN conjunct1])
   1.130 -apply simp
   1.131 -done
   1.132 -
   1.133 -lemma lemma_order_pderiv1:
   1.134 -  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
   1.135 -    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
   1.136 -apply (simp only: pderiv_mult pderiv_power_Suc)
   1.137 -apply (simp del: power_poly_Suc of_nat_Suc add: pderiv_pCons)
   1.138 -done
   1.139 -
   1.140 -lemma dvd_add_cancel1:
   1.141 -  fixes a b c :: "'a::comm_ring_1"
   1.142 -  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   1.143 -  by (drule (1) Ring_and_Field.dvd_diff, simp)
   1.144 -
   1.145 -lemma lemma_order_pderiv [rule_format]:
   1.146 -     "\<forall>p q a. 0 < n &
   1.147 -       pderiv p \<noteq> 0 &
   1.148 -       p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
   1.149 -       --> n = Suc (order a (pderiv p))"
   1.150 - apply (cases "n", safe, rename_tac n p q a)
   1.151 - apply (rule order_unique_lemma)
   1.152 - apply (rule conjI)
   1.153 -  apply (subst lemma_order_pderiv1)
   1.154 -  apply (rule dvd_add)
   1.155 -   apply (rule dvd_mult2)
   1.156 -   apply (rule le_imp_power_dvd, simp)
   1.157 -  apply (rule dvd_smult)
   1.158 -  apply (rule dvd_mult)
   1.159 -  apply (rule dvd_refl)
   1.160 - apply (subst lemma_order_pderiv1)
   1.161 - apply (erule contrapos_nn) back
   1.162 - apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
   1.163 -  apply (simp del: mult_pCons_left)
   1.164 - apply (drule dvd_add_cancel1)
   1.165 -  apply (simp del: mult_pCons_left)
   1.166 - apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
   1.167 - apply assumption
   1.168 -done
   1.169 -
   1.170 -lemma order_decomp:
   1.171 -     "p \<noteq> 0
   1.172 -      ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
   1.173 -                ~([:-a, 1:] dvd q)"
   1.174 -apply (drule order [where a=a])
   1.175 -apply (erule conjE)
   1.176 -apply (erule dvdE)
   1.177 -apply (rule exI)
   1.178 -apply (rule conjI, assumption)
   1.179 -apply (erule contrapos_nn)
   1.180 -apply (erule ssubst) back
   1.181 -apply (subst power_Suc2)
   1.182 -apply (erule mult_dvd_mono [OF dvd_refl])
   1.183 -done
   1.184 -
   1.185 -lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   1.186 -      ==> (order a p = Suc (order a (pderiv p)))"
   1.187 -apply (case_tac "p = 0", simp)
   1.188 -apply (drule_tac a = a and p = p in order_decomp)
   1.189 -using neq0_conv
   1.190 -apply (blast intro: lemma_order_pderiv)
   1.191 -done
   1.192 -
   1.193 -lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
   1.194 -proof -
   1.195 -  def i \<equiv> "order a p"
   1.196 -  def j \<equiv> "order a q"
   1.197 -  def t \<equiv> "[:-a, 1:]"
   1.198 -  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
   1.199 -    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
   1.200 -  assume "p * q \<noteq> 0"
   1.201 -  then show "order a (p * q) = i + j"
   1.202 -    apply clarsimp
   1.203 -    apply (drule order [where a=a and p=p, folded i_def t_def])
   1.204 -    apply (drule order [where a=a and p=q, folded j_def t_def])
   1.205 -    apply clarify
   1.206 -    apply (rule order_unique_lemma [symmetric], fold t_def)
   1.207 -    apply (erule dvdE)+
   1.208 -    apply (simp add: power_add t_dvd_iff)
   1.209 -    done
   1.210 -qed
   1.211 -
   1.212 -text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
   1.213 -
   1.214 -lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
   1.215 -apply (cases "p = 0", auto)
   1.216 -apply (drule order_2 [where a=a and p=p])
   1.217 -apply (erule contrapos_np)
   1.218 -apply (erule power_le_dvd)
   1.219 -apply simp
   1.220 -apply (erule power_le_dvd [OF order_1])
   1.221 -done
   1.222 -
   1.223 -lemma poly_squarefree_decomp_order:
   1.224 -  assumes "pderiv p \<noteq> 0"
   1.225 -  and p: "p = q * d"
   1.226 -  and p': "pderiv p = e * d"
   1.227 -  and d: "d = r * p + s * pderiv p"
   1.228 -  shows "order a q = (if order a p = 0 then 0 else 1)"
   1.229 -proof (rule classical)
   1.230 -  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
   1.231 -  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   1.232 -  with p have "order a p = order a q + order a d"
   1.233 -    by (simp add: order_mult)
   1.234 -  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
   1.235 -  have "order a (pderiv p) = order a e + order a d"
   1.236 -    using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
   1.237 -  have "order a p = Suc (order a (pderiv p))"
   1.238 -    using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
   1.239 -  have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
   1.240 -  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
   1.241 -    apply (simp add: d)
   1.242 -    apply (rule dvd_add)
   1.243 -    apply (rule dvd_mult)
   1.244 -    apply (simp add: order_divides `p \<noteq> 0`
   1.245 -           `order a p = Suc (order a (pderiv p))`)
   1.246 -    apply (rule dvd_mult)
   1.247 -    apply (simp add: order_divides)
   1.248 -    done
   1.249 -  then have "order a (pderiv p) \<le> order a d"
   1.250 -    using `d \<noteq> 0` by (simp add: order_divides)
   1.251 -  show ?thesis
   1.252 -    using `order a p = order a q + order a d`
   1.253 -    using `order a (pderiv p) = order a e + order a d`
   1.254 -    using `order a p = Suc (order a (pderiv p))`
   1.255 -    using `order a (pderiv p) \<le> order a d`
   1.256 -    by auto
   1.257 -qed
   1.258 -
   1.259 -lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
   1.260 -         p = q * d;
   1.261 -         pderiv p = e * d;
   1.262 -         d = r * p + s * pderiv p
   1.263 -      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   1.264 -apply (blast intro: poly_squarefree_decomp_order)
   1.265 -done
   1.266 -
   1.267 -lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   1.268 -      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
   1.269 -apply (auto dest: order_pderiv)
   1.270 -done
   1.271 -
   1.272 -definition
   1.273 -  rsquarefree :: "'a::idom poly => bool" where
   1.274 -  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
   1.275 -
   1.276 -lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
   1.277 -apply (simp add: pderiv_eq_0_iff)
   1.278 -apply (case_tac p, auto split: if_splits)
   1.279 -done
   1.280 -
   1.281 -lemma rsquarefree_roots:
   1.282 -  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
   1.283 -apply (simp add: rsquarefree_def)
   1.284 -apply (case_tac "p = 0", simp, simp)
   1.285 -apply (case_tac "pderiv p = 0")
   1.286 -apply simp
   1.287 -apply (drule pderiv_iszero, clarify)
   1.288 -apply simp
   1.289 -apply (rule allI)
   1.290 -apply (cut_tac p = "[:h:]" and a = a in order_root)
   1.291 -apply simp
   1.292 -apply (auto simp add: order_root order_pderiv2)
   1.293 -apply (erule_tac x="a" in allE, simp)
   1.294 -done
   1.295 -
   1.296 -lemma poly_squarefree_decomp:
   1.297 -  assumes "pderiv p \<noteq> 0"
   1.298 -    and "p = q * d"
   1.299 -    and "pderiv p = e * d"
   1.300 -    and "d = r * p + s * pderiv p"
   1.301 -  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
   1.302 -proof -
   1.303 -  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   1.304 -  with `p = q * d` have "q \<noteq> 0" by simp
   1.305 -  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   1.306 -    using assms by (rule poly_squarefree_decomp_order2)
   1.307 -  with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
   1.308 -    by (simp add: rsquarefree_def order_root)
   1.309 -qed
   1.310 -
   1.311 -
   1.312  subsection {* Theorems about Limits *}
   1.313  
   1.314  (* need to rename second isCont_inverse *)
     2.1 --- a/src/HOL/IsaMakefile	Wed Feb 18 17:02:38 2009 -0800
     2.2 +++ b/src/HOL/IsaMakefile	Wed Feb 18 19:32:26 2009 -0800
     2.3 @@ -336,6 +336,7 @@
     2.4    Library/Boolean_Algebra.thy Library/Countable.thy	\
     2.5    Library/RBT.thy	Library/Univ_Poly.thy	\
     2.6    Library/Random.thy	Library/Quickcheck.thy	\
     2.7 +  Library/Poly_Deriv.thy \
     2.8    Library/Enum.thy Library/Float.thy $(SRC)/Tools/float.ML $(SRC)/HOL/Tools/float_arith.ML \
     2.9    Library/reify_data.ML Library/reflection.ML
    2.10  	@cd Library; $(ISABELLE_TOOL) usedir $(OUT)/HOL Library
     3.1 --- a/src/HOL/Library/Library.thy	Wed Feb 18 17:02:38 2009 -0800
     3.2 +++ b/src/HOL/Library/Library.thy	Wed Feb 18 19:32:26 2009 -0800
     3.3 @@ -35,6 +35,7 @@
     3.4    Option_ord
     3.5    Permutation
     3.6    Pocklington
     3.7 +  Poly_Deriv
     3.8    Primes
     3.9    Quickcheck
    3.10    Quicksort
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Library/Poly_Deriv.thy	Wed Feb 18 19:32:26 2009 -0800
     4.3 @@ -0,0 +1,316 @@
     4.4 +(*  Title:      Poly_Deriv.thy
     4.5 +    Author:     Amine Chaieb
     4.6 +                Ported to new Polynomial library by Brian Huffman
     4.7 +*)
     4.8 +
     4.9 +header{* Polynomials and Differentiation *}
    4.10 +
    4.11 +theory Poly_Deriv
    4.12 +imports Deriv Polynomial
    4.13 +begin
    4.14 +
    4.15 +subsection {* Derivatives of univariate polynomials *}
    4.16 +
    4.17 +definition
    4.18 +  pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly" where
    4.19 +  "pderiv = poly_rec 0 (\<lambda>a p p'. p + pCons 0 p')"
    4.20 +
    4.21 +lemma pderiv_0 [simp]: "pderiv 0 = 0"
    4.22 +  unfolding pderiv_def by (simp add: poly_rec_0)
    4.23 +
    4.24 +lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
    4.25 +  unfolding pderiv_def by (simp add: poly_rec_pCons)
    4.26 +
    4.27 +lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
    4.28 +  apply (induct p arbitrary: n, simp)
    4.29 +  apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
    4.30 +  done
    4.31 +
    4.32 +lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
    4.33 +  apply (rule iffI)
    4.34 +  apply (cases p, simp)
    4.35 +  apply (simp add: expand_poly_eq coeff_pderiv del: of_nat_Suc)
    4.36 +  apply (simp add: expand_poly_eq coeff_pderiv coeff_eq_0)
    4.37 +  done
    4.38 +
    4.39 +lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
    4.40 +  apply (rule order_antisym [OF degree_le])
    4.41 +  apply (simp add: coeff_pderiv coeff_eq_0)
    4.42 +  apply (cases "degree p", simp)
    4.43 +  apply (rule le_degree)
    4.44 +  apply (simp add: coeff_pderiv del: of_nat_Suc)
    4.45 +  apply (rule subst, assumption)
    4.46 +  apply (rule leading_coeff_neq_0, clarsimp)
    4.47 +  done
    4.48 +
    4.49 +lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
    4.50 +by (simp add: pderiv_pCons)
    4.51 +
    4.52 +lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
    4.53 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    4.54 +
    4.55 +lemma pderiv_minus: "pderiv (- p) = - pderiv p"
    4.56 +by (rule poly_ext, simp add: coeff_pderiv)
    4.57 +
    4.58 +lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
    4.59 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    4.60 +
    4.61 +lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
    4.62 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    4.63 +
    4.64 +lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
    4.65 +apply (induct p)
    4.66 +apply simp
    4.67 +apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
    4.68 +done
    4.69 +
    4.70 +lemma pderiv_power_Suc:
    4.71 +  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
    4.72 +apply (induct n)
    4.73 +apply simp
    4.74 +apply (subst power_Suc)
    4.75 +apply (subst pderiv_mult)
    4.76 +apply (erule ssubst)
    4.77 +apply (simp add: smult_add_left algebra_simps)
    4.78 +done
    4.79 +
    4.80 +lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
    4.81 +by (simp add: DERIV_cmult mult_commute [of _ c])
    4.82 +
    4.83 +lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
    4.84 +by (rule lemma_DERIV_subst, rule DERIV_pow, simp)
    4.85 +declare DERIV_pow2 [simp] DERIV_pow [simp]
    4.86 +
    4.87 +lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
    4.88 +by (rule lemma_DERIV_subst, rule DERIV_add, auto)
    4.89 +
    4.90 +lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
    4.91 +apply (induct p)
    4.92 +apply simp
    4.93 +apply (simp add: pderiv_pCons)
    4.94 +apply (rule lemma_DERIV_subst)
    4.95 +apply (rule DERIV_add DERIV_mult DERIV_const DERIV_ident | assumption)+
    4.96 +apply simp
    4.97 +done
    4.98 +
    4.99 +text{* Consequences of the derivative theorem above*}
   4.100 +
   4.101 +lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
   4.102 +apply (simp add: differentiable_def)
   4.103 +apply (blast intro: poly_DERIV)
   4.104 +done
   4.105 +
   4.106 +lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
   4.107 +by (rule poly_DERIV [THEN DERIV_isCont])
   4.108 +
   4.109 +lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
   4.110 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   4.111 +apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
   4.112 +apply (auto simp add: order_le_less)
   4.113 +done
   4.114 +
   4.115 +lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
   4.116 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   4.117 +by (insert poly_IVT_pos [where p = "- p" ]) simp
   4.118 +
   4.119 +lemma poly_MVT: "(a::real) < b ==>
   4.120 +     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
   4.121 +apply (drule_tac f = "poly p" in MVT, auto)
   4.122 +apply (rule_tac x = z in exI)
   4.123 +apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
   4.124 +done
   4.125 +
   4.126 +text{*Lemmas for Derivatives*}
   4.127 +
   4.128 +lemma order_unique_lemma:
   4.129 +  fixes p :: "'a::idom poly"
   4.130 +  assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
   4.131 +  shows "n = order a p"
   4.132 +unfolding Polynomial.order_def
   4.133 +apply (rule Least_equality [symmetric])
   4.134 +apply (rule assms [THEN conjunct2])
   4.135 +apply (erule contrapos_np)
   4.136 +apply (rule power_le_dvd)
   4.137 +apply (rule assms [THEN conjunct1])
   4.138 +apply simp
   4.139 +done
   4.140 +
   4.141 +lemma lemma_order_pderiv1:
   4.142 +  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
   4.143 +    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
   4.144 +apply (simp only: pderiv_mult pderiv_power_Suc)
   4.145 +apply (simp del: power_poly_Suc of_nat_Suc add: pderiv_pCons)
   4.146 +done
   4.147 +
   4.148 +lemma dvd_add_cancel1:
   4.149 +  fixes a b c :: "'a::comm_ring_1"
   4.150 +  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   4.151 +  by (drule (1) Ring_and_Field.dvd_diff, simp)
   4.152 +
   4.153 +lemma lemma_order_pderiv [rule_format]:
   4.154 +     "\<forall>p q a. 0 < n &
   4.155 +       pderiv p \<noteq> 0 &
   4.156 +       p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
   4.157 +       --> n = Suc (order a (pderiv p))"
   4.158 + apply (cases "n", safe, rename_tac n p q a)
   4.159 + apply (rule order_unique_lemma)
   4.160 + apply (rule conjI)
   4.161 +  apply (subst lemma_order_pderiv1)
   4.162 +  apply (rule dvd_add)
   4.163 +   apply (rule dvd_mult2)
   4.164 +   apply (rule le_imp_power_dvd, simp)
   4.165 +  apply (rule dvd_smult)
   4.166 +  apply (rule dvd_mult)
   4.167 +  apply (rule dvd_refl)
   4.168 + apply (subst lemma_order_pderiv1)
   4.169 + apply (erule contrapos_nn) back
   4.170 + apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
   4.171 +  apply (simp del: mult_pCons_left)
   4.172 + apply (drule dvd_add_cancel1)
   4.173 +  apply (simp del: mult_pCons_left)
   4.174 + apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
   4.175 + apply assumption
   4.176 +done
   4.177 +
   4.178 +lemma order_decomp:
   4.179 +     "p \<noteq> 0
   4.180 +      ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
   4.181 +                ~([:-a, 1:] dvd q)"
   4.182 +apply (drule order [where a=a])
   4.183 +apply (erule conjE)
   4.184 +apply (erule dvdE)
   4.185 +apply (rule exI)
   4.186 +apply (rule conjI, assumption)
   4.187 +apply (erule contrapos_nn)
   4.188 +apply (erule ssubst) back
   4.189 +apply (subst power_Suc2)
   4.190 +apply (erule mult_dvd_mono [OF dvd_refl])
   4.191 +done
   4.192 +
   4.193 +lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   4.194 +      ==> (order a p = Suc (order a (pderiv p)))"
   4.195 +apply (case_tac "p = 0", simp)
   4.196 +apply (drule_tac a = a and p = p in order_decomp)
   4.197 +using neq0_conv
   4.198 +apply (blast intro: lemma_order_pderiv)
   4.199 +done
   4.200 +
   4.201 +lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
   4.202 +proof -
   4.203 +  def i \<equiv> "order a p"
   4.204 +  def j \<equiv> "order a q"
   4.205 +  def t \<equiv> "[:-a, 1:]"
   4.206 +  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
   4.207 +    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
   4.208 +  assume "p * q \<noteq> 0"
   4.209 +  then show "order a (p * q) = i + j"
   4.210 +    apply clarsimp
   4.211 +    apply (drule order [where a=a and p=p, folded i_def t_def])
   4.212 +    apply (drule order [where a=a and p=q, folded j_def t_def])
   4.213 +    apply clarify
   4.214 +    apply (rule order_unique_lemma [symmetric], fold t_def)
   4.215 +    apply (erule dvdE)+
   4.216 +    apply (simp add: power_add t_dvd_iff)
   4.217 +    done
   4.218 +qed
   4.219 +
   4.220 +text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
   4.221 +
   4.222 +lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
   4.223 +apply (cases "p = 0", auto)
   4.224 +apply (drule order_2 [where a=a and p=p])
   4.225 +apply (erule contrapos_np)
   4.226 +apply (erule power_le_dvd)
   4.227 +apply simp
   4.228 +apply (erule power_le_dvd [OF order_1])
   4.229 +done
   4.230 +
   4.231 +lemma poly_squarefree_decomp_order:
   4.232 +  assumes "pderiv p \<noteq> 0"
   4.233 +  and p: "p = q * d"
   4.234 +  and p': "pderiv p = e * d"
   4.235 +  and d: "d = r * p + s * pderiv p"
   4.236 +  shows "order a q = (if order a p = 0 then 0 else 1)"
   4.237 +proof (rule classical)
   4.238 +  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
   4.239 +  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   4.240 +  with p have "order a p = order a q + order a d"
   4.241 +    by (simp add: order_mult)
   4.242 +  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
   4.243 +  have "order a (pderiv p) = order a e + order a d"
   4.244 +    using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
   4.245 +  have "order a p = Suc (order a (pderiv p))"
   4.246 +    using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
   4.247 +  have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
   4.248 +  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
   4.249 +    apply (simp add: d)
   4.250 +    apply (rule dvd_add)
   4.251 +    apply (rule dvd_mult)
   4.252 +    apply (simp add: order_divides `p \<noteq> 0`
   4.253 +           `order a p = Suc (order a (pderiv p))`)
   4.254 +    apply (rule dvd_mult)
   4.255 +    apply (simp add: order_divides)
   4.256 +    done
   4.257 +  then have "order a (pderiv p) \<le> order a d"
   4.258 +    using `d \<noteq> 0` by (simp add: order_divides)
   4.259 +  show ?thesis
   4.260 +    using `order a p = order a q + order a d`
   4.261 +    using `order a (pderiv p) = order a e + order a d`
   4.262 +    using `order a p = Suc (order a (pderiv p))`
   4.263 +    using `order a (pderiv p) \<le> order a d`
   4.264 +    by auto
   4.265 +qed
   4.266 +
   4.267 +lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
   4.268 +         p = q * d;
   4.269 +         pderiv p = e * d;
   4.270 +         d = r * p + s * pderiv p
   4.271 +      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   4.272 +apply (blast intro: poly_squarefree_decomp_order)
   4.273 +done
   4.274 +
   4.275 +lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   4.276 +      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
   4.277 +apply (auto dest: order_pderiv)
   4.278 +done
   4.279 +
   4.280 +definition
   4.281 +  rsquarefree :: "'a::idom poly => bool" where
   4.282 +  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
   4.283 +
   4.284 +lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
   4.285 +apply (simp add: pderiv_eq_0_iff)
   4.286 +apply (case_tac p, auto split: if_splits)
   4.287 +done
   4.288 +
   4.289 +lemma rsquarefree_roots:
   4.290 +  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
   4.291 +apply (simp add: rsquarefree_def)
   4.292 +apply (case_tac "p = 0", simp, simp)
   4.293 +apply (case_tac "pderiv p = 0")
   4.294 +apply simp
   4.295 +apply (drule pderiv_iszero, clarify)
   4.296 +apply simp
   4.297 +apply (rule allI)
   4.298 +apply (cut_tac p = "[:h:]" and a = a in order_root)
   4.299 +apply simp
   4.300 +apply (auto simp add: order_root order_pderiv2)
   4.301 +apply (erule_tac x="a" in allE, simp)
   4.302 +done
   4.303 +
   4.304 +lemma poly_squarefree_decomp:
   4.305 +  assumes "pderiv p \<noteq> 0"
   4.306 +    and "p = q * d"
   4.307 +    and "pderiv p = e * d"
   4.308 +    and "d = r * p + s * pderiv p"
   4.309 +  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
   4.310 +proof -
   4.311 +  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   4.312 +  with `p = q * d` have "q \<noteq> 0" by simp
   4.313 +  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   4.314 +    using assms by (rule poly_squarefree_decomp_order2)
   4.315 +  with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
   4.316 +    by (simp add: rsquarefree_def order_root)
   4.317 +qed
   4.318 +
   4.319 +end