src/HOL/Library/Poly_Deriv.thy
changeset 29985 57975b45ab70
child 30273 ecd6f0ca62ea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Poly_Deriv.thy	Wed Feb 18 19:32:26 2009 -0800
     1.3 @@ -0,0 +1,316 @@
     1.4 +(*  Title:      Poly_Deriv.thy
     1.5 +    Author:     Amine Chaieb
     1.6 +                Ported to new Polynomial library by Brian Huffman
     1.7 +*)
     1.8 +
     1.9 +header{* Polynomials and Differentiation *}
    1.10 +
    1.11 +theory Poly_Deriv
    1.12 +imports Deriv Polynomial
    1.13 +begin
    1.14 +
    1.15 +subsection {* Derivatives of univariate polynomials *}
    1.16 +
    1.17 +definition
    1.18 +  pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly" where
    1.19 +  "pderiv = poly_rec 0 (\<lambda>a p p'. p + pCons 0 p')"
    1.20 +
    1.21 +lemma pderiv_0 [simp]: "pderiv 0 = 0"
    1.22 +  unfolding pderiv_def by (simp add: poly_rec_0)
    1.23 +
    1.24 +lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
    1.25 +  unfolding pderiv_def by (simp add: poly_rec_pCons)
    1.26 +
    1.27 +lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
    1.28 +  apply (induct p arbitrary: n, simp)
    1.29 +  apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
    1.30 +  done
    1.31 +
    1.32 +lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
    1.33 +  apply (rule iffI)
    1.34 +  apply (cases p, simp)
    1.35 +  apply (simp add: expand_poly_eq coeff_pderiv del: of_nat_Suc)
    1.36 +  apply (simp add: expand_poly_eq coeff_pderiv coeff_eq_0)
    1.37 +  done
    1.38 +
    1.39 +lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
    1.40 +  apply (rule order_antisym [OF degree_le])
    1.41 +  apply (simp add: coeff_pderiv coeff_eq_0)
    1.42 +  apply (cases "degree p", simp)
    1.43 +  apply (rule le_degree)
    1.44 +  apply (simp add: coeff_pderiv del: of_nat_Suc)
    1.45 +  apply (rule subst, assumption)
    1.46 +  apply (rule leading_coeff_neq_0, clarsimp)
    1.47 +  done
    1.48 +
    1.49 +lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
    1.50 +by (simp add: pderiv_pCons)
    1.51 +
    1.52 +lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
    1.53 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.54 +
    1.55 +lemma pderiv_minus: "pderiv (- p) = - pderiv p"
    1.56 +by (rule poly_ext, simp add: coeff_pderiv)
    1.57 +
    1.58 +lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
    1.59 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.60 +
    1.61 +lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
    1.62 +by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
    1.63 +
    1.64 +lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
    1.65 +apply (induct p)
    1.66 +apply simp
    1.67 +apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
    1.68 +done
    1.69 +
    1.70 +lemma pderiv_power_Suc:
    1.71 +  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
    1.72 +apply (induct n)
    1.73 +apply simp
    1.74 +apply (subst power_Suc)
    1.75 +apply (subst pderiv_mult)
    1.76 +apply (erule ssubst)
    1.77 +apply (simp add: smult_add_left algebra_simps)
    1.78 +done
    1.79 +
    1.80 +lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
    1.81 +by (simp add: DERIV_cmult mult_commute [of _ c])
    1.82 +
    1.83 +lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
    1.84 +by (rule lemma_DERIV_subst, rule DERIV_pow, simp)
    1.85 +declare DERIV_pow2 [simp] DERIV_pow [simp]
    1.86 +
    1.87 +lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
    1.88 +by (rule lemma_DERIV_subst, rule DERIV_add, auto)
    1.89 +
    1.90 +lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
    1.91 +apply (induct p)
    1.92 +apply simp
    1.93 +apply (simp add: pderiv_pCons)
    1.94 +apply (rule lemma_DERIV_subst)
    1.95 +apply (rule DERIV_add DERIV_mult DERIV_const DERIV_ident | assumption)+
    1.96 +apply simp
    1.97 +done
    1.98 +
    1.99 +text{* Consequences of the derivative theorem above*}
   1.100 +
   1.101 +lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
   1.102 +apply (simp add: differentiable_def)
   1.103 +apply (blast intro: poly_DERIV)
   1.104 +done
   1.105 +
   1.106 +lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
   1.107 +by (rule poly_DERIV [THEN DERIV_isCont])
   1.108 +
   1.109 +lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
   1.110 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   1.111 +apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
   1.112 +apply (auto simp add: order_le_less)
   1.113 +done
   1.114 +
   1.115 +lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
   1.116 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   1.117 +by (insert poly_IVT_pos [where p = "- p" ]) simp
   1.118 +
   1.119 +lemma poly_MVT: "(a::real) < b ==>
   1.120 +     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
   1.121 +apply (drule_tac f = "poly p" in MVT, auto)
   1.122 +apply (rule_tac x = z in exI)
   1.123 +apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
   1.124 +done
   1.125 +
   1.126 +text{*Lemmas for Derivatives*}
   1.127 +
   1.128 +lemma order_unique_lemma:
   1.129 +  fixes p :: "'a::idom poly"
   1.130 +  assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
   1.131 +  shows "n = order a p"
   1.132 +unfolding Polynomial.order_def
   1.133 +apply (rule Least_equality [symmetric])
   1.134 +apply (rule assms [THEN conjunct2])
   1.135 +apply (erule contrapos_np)
   1.136 +apply (rule power_le_dvd)
   1.137 +apply (rule assms [THEN conjunct1])
   1.138 +apply simp
   1.139 +done
   1.140 +
   1.141 +lemma lemma_order_pderiv1:
   1.142 +  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
   1.143 +    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
   1.144 +apply (simp only: pderiv_mult pderiv_power_Suc)
   1.145 +apply (simp del: power_poly_Suc of_nat_Suc add: pderiv_pCons)
   1.146 +done
   1.147 +
   1.148 +lemma dvd_add_cancel1:
   1.149 +  fixes a b c :: "'a::comm_ring_1"
   1.150 +  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   1.151 +  by (drule (1) Ring_and_Field.dvd_diff, simp)
   1.152 +
   1.153 +lemma lemma_order_pderiv [rule_format]:
   1.154 +     "\<forall>p q a. 0 < n &
   1.155 +       pderiv p \<noteq> 0 &
   1.156 +       p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
   1.157 +       --> n = Suc (order a (pderiv p))"
   1.158 + apply (cases "n", safe, rename_tac n p q a)
   1.159 + apply (rule order_unique_lemma)
   1.160 + apply (rule conjI)
   1.161 +  apply (subst lemma_order_pderiv1)
   1.162 +  apply (rule dvd_add)
   1.163 +   apply (rule dvd_mult2)
   1.164 +   apply (rule le_imp_power_dvd, simp)
   1.165 +  apply (rule dvd_smult)
   1.166 +  apply (rule dvd_mult)
   1.167 +  apply (rule dvd_refl)
   1.168 + apply (subst lemma_order_pderiv1)
   1.169 + apply (erule contrapos_nn) back
   1.170 + apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
   1.171 +  apply (simp del: mult_pCons_left)
   1.172 + apply (drule dvd_add_cancel1)
   1.173 +  apply (simp del: mult_pCons_left)
   1.174 + apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
   1.175 + apply assumption
   1.176 +done
   1.177 +
   1.178 +lemma order_decomp:
   1.179 +     "p \<noteq> 0
   1.180 +      ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
   1.181 +                ~([:-a, 1:] dvd q)"
   1.182 +apply (drule order [where a=a])
   1.183 +apply (erule conjE)
   1.184 +apply (erule dvdE)
   1.185 +apply (rule exI)
   1.186 +apply (rule conjI, assumption)
   1.187 +apply (erule contrapos_nn)
   1.188 +apply (erule ssubst) back
   1.189 +apply (subst power_Suc2)
   1.190 +apply (erule mult_dvd_mono [OF dvd_refl])
   1.191 +done
   1.192 +
   1.193 +lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   1.194 +      ==> (order a p = Suc (order a (pderiv p)))"
   1.195 +apply (case_tac "p = 0", simp)
   1.196 +apply (drule_tac a = a and p = p in order_decomp)
   1.197 +using neq0_conv
   1.198 +apply (blast intro: lemma_order_pderiv)
   1.199 +done
   1.200 +
   1.201 +lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
   1.202 +proof -
   1.203 +  def i \<equiv> "order a p"
   1.204 +  def j \<equiv> "order a q"
   1.205 +  def t \<equiv> "[:-a, 1:]"
   1.206 +  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
   1.207 +    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
   1.208 +  assume "p * q \<noteq> 0"
   1.209 +  then show "order a (p * q) = i + j"
   1.210 +    apply clarsimp
   1.211 +    apply (drule order [where a=a and p=p, folded i_def t_def])
   1.212 +    apply (drule order [where a=a and p=q, folded j_def t_def])
   1.213 +    apply clarify
   1.214 +    apply (rule order_unique_lemma [symmetric], fold t_def)
   1.215 +    apply (erule dvdE)+
   1.216 +    apply (simp add: power_add t_dvd_iff)
   1.217 +    done
   1.218 +qed
   1.219 +
   1.220 +text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
   1.221 +
   1.222 +lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
   1.223 +apply (cases "p = 0", auto)
   1.224 +apply (drule order_2 [where a=a and p=p])
   1.225 +apply (erule contrapos_np)
   1.226 +apply (erule power_le_dvd)
   1.227 +apply simp
   1.228 +apply (erule power_le_dvd [OF order_1])
   1.229 +done
   1.230 +
   1.231 +lemma poly_squarefree_decomp_order:
   1.232 +  assumes "pderiv p \<noteq> 0"
   1.233 +  and p: "p = q * d"
   1.234 +  and p': "pderiv p = e * d"
   1.235 +  and d: "d = r * p + s * pderiv p"
   1.236 +  shows "order a q = (if order a p = 0 then 0 else 1)"
   1.237 +proof (rule classical)
   1.238 +  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
   1.239 +  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   1.240 +  with p have "order a p = order a q + order a d"
   1.241 +    by (simp add: order_mult)
   1.242 +  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
   1.243 +  have "order a (pderiv p) = order a e + order a d"
   1.244 +    using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
   1.245 +  have "order a p = Suc (order a (pderiv p))"
   1.246 +    using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
   1.247 +  have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
   1.248 +  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
   1.249 +    apply (simp add: d)
   1.250 +    apply (rule dvd_add)
   1.251 +    apply (rule dvd_mult)
   1.252 +    apply (simp add: order_divides `p \<noteq> 0`
   1.253 +           `order a p = Suc (order a (pderiv p))`)
   1.254 +    apply (rule dvd_mult)
   1.255 +    apply (simp add: order_divides)
   1.256 +    done
   1.257 +  then have "order a (pderiv p) \<le> order a d"
   1.258 +    using `d \<noteq> 0` by (simp add: order_divides)
   1.259 +  show ?thesis
   1.260 +    using `order a p = order a q + order a d`
   1.261 +    using `order a (pderiv p) = order a e + order a d`
   1.262 +    using `order a p = Suc (order a (pderiv p))`
   1.263 +    using `order a (pderiv p) \<le> order a d`
   1.264 +    by auto
   1.265 +qed
   1.266 +
   1.267 +lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
   1.268 +         p = q * d;
   1.269 +         pderiv p = e * d;
   1.270 +         d = r * p + s * pderiv p
   1.271 +      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   1.272 +apply (blast intro: poly_squarefree_decomp_order)
   1.273 +done
   1.274 +
   1.275 +lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   1.276 +      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
   1.277 +apply (auto dest: order_pderiv)
   1.278 +done
   1.279 +
   1.280 +definition
   1.281 +  rsquarefree :: "'a::idom poly => bool" where
   1.282 +  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
   1.283 +
   1.284 +lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
   1.285 +apply (simp add: pderiv_eq_0_iff)
   1.286 +apply (case_tac p, auto split: if_splits)
   1.287 +done
   1.288 +
   1.289 +lemma rsquarefree_roots:
   1.290 +  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
   1.291 +apply (simp add: rsquarefree_def)
   1.292 +apply (case_tac "p = 0", simp, simp)
   1.293 +apply (case_tac "pderiv p = 0")
   1.294 +apply simp
   1.295 +apply (drule pderiv_iszero, clarify)
   1.296 +apply simp
   1.297 +apply (rule allI)
   1.298 +apply (cut_tac p = "[:h:]" and a = a in order_root)
   1.299 +apply simp
   1.300 +apply (auto simp add: order_root order_pderiv2)
   1.301 +apply (erule_tac x="a" in allE, simp)
   1.302 +done
   1.303 +
   1.304 +lemma poly_squarefree_decomp:
   1.305 +  assumes "pderiv p \<noteq> 0"
   1.306 +    and "p = q * d"
   1.307 +    and "pderiv p = e * d"
   1.308 +    and "d = r * p + s * pderiv p"
   1.309 +  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
   1.310 +proof -
   1.311 +  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   1.312 +  with `p = q * d` have "q \<noteq> 0" by simp
   1.313 +  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   1.314 +    using assms by (rule poly_squarefree_decomp_order2)
   1.315 +  with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
   1.316 +    by (simp add: rsquarefree_def order_root)
   1.317 +qed
   1.318 +
   1.319 +end