src/HOL/Library/Poly_Deriv.thy
author hoelzl
Mon Mar 17 19:12:52 2014 +0100 (2014-03-17)
changeset 56181 2aa0b19e74f3
parent 52380 3cc46b8cca5e
child 56217 dc429a5b13c4
permissions -rw-r--r--
unify syntax for has_derivative and differentiable
     1 (*  Title:      HOL/Library/Poly_Deriv.thy
     2     Author:     Amine Chaieb
     3     Author:     Brian Huffman
     4 *)
     5 
     6 header{* Polynomials and Differentiation *}
     7 
     8 theory Poly_Deriv
     9 imports Deriv Polynomial
    10 begin
    11 
    12 subsection {* Derivatives of univariate polynomials *}
    13 
    14 function pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly"
    15 where
    16   [simp del]: "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))"
    17   by (auto intro: pCons_cases)
    18 
    19 termination pderiv
    20   by (relation "measure degree") simp_all
    21 
    22 lemma pderiv_0 [simp]:
    23   "pderiv 0 = 0"
    24   using pderiv.simps [of 0 0] by simp
    25 
    26 lemma pderiv_pCons:
    27   "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
    28   by (simp add: pderiv.simps)
    29 
    30 lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
    31   apply (induct p arbitrary: n, simp)
    32   apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
    33   done
    34 
    35 primrec pderiv_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list"
    36 where
    37   "pderiv_coeffs [] = []"
    38 | "pderiv_coeffs (x # xs) = plus_coeffs xs (cCons 0 (pderiv_coeffs xs))"
    39 
    40 lemma coeffs_pderiv [code abstract]:
    41   "coeffs (pderiv p) = pderiv_coeffs (coeffs p)"
    42   by (rule sym, induct p) (simp_all add: pderiv_pCons coeffs_plus_eq_plus_coeffs cCons_def)
    43 
    44 lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
    45   apply (rule iffI)
    46   apply (cases p, simp)
    47   apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
    48   apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
    49   done
    50 
    51 lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
    52   apply (rule order_antisym [OF degree_le])
    53   apply (simp add: coeff_pderiv coeff_eq_0)
    54   apply (cases "degree p", simp)
    55   apply (rule le_degree)
    56   apply (simp add: coeff_pderiv del: of_nat_Suc)
    57   apply (rule subst, assumption)
    58   apply (rule leading_coeff_neq_0, clarsimp)
    59   done
    60 
    61 lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
    62 by (simp add: pderiv_pCons)
    63 
    64 lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
    65 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    66 
    67 lemma pderiv_minus: "pderiv (- p) = - pderiv p"
    68 by (rule poly_eqI, simp add: coeff_pderiv)
    69 
    70 lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
    71 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    72 
    73 lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
    74 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    75 
    76 lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
    77 apply (induct p)
    78 apply simp
    79 apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
    80 done
    81 
    82 lemma pderiv_power_Suc:
    83   "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
    84 apply (induct n)
    85 apply simp
    86 apply (subst power_Suc)
    87 apply (subst pderiv_mult)
    88 apply (erule ssubst)
    89 apply (simp only: of_nat_Suc smult_add_left smult_1_left)
    90 apply (simp add: algebra_simps) (* FIXME *)
    91 done
    92 
    93 
    94 lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
    95 by (simp add: DERIV_cmult mult_commute [of _ c])
    96 
    97 lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
    98 by (rule DERIV_cong, rule DERIV_pow, simp)
    99 declare DERIV_pow2 [simp] DERIV_pow [simp]
   100 
   101 lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
   102 by (rule DERIV_cong, rule DERIV_add, auto)
   103 
   104 lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
   105   by (induct p, auto intro!: DERIV_intros simp add: pderiv_pCons)
   106 
   107 text{* Consequences of the derivative theorem above*}
   108 
   109 lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
   110 apply (simp add: real_differentiable_def)
   111 apply (blast intro: poly_DERIV)
   112 done
   113 
   114 lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
   115 by (rule poly_DERIV [THEN DERIV_isCont])
   116 
   117 lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
   118       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   119 apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
   120 apply (auto simp add: order_le_less)
   121 done
   122 
   123 lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
   124       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   125 by (insert poly_IVT_pos [where p = "- p" ]) simp
   126 
   127 lemma poly_MVT: "(a::real) < b ==>
   128      \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
   129 apply (drule_tac f = "poly p" in MVT, auto)
   130 apply (rule_tac x = z in exI)
   131 apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
   132 done
   133 
   134 text{*Lemmas for Derivatives*}
   135 
   136 lemma order_unique_lemma:
   137   fixes p :: "'a::idom poly"
   138   assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
   139   shows "n = order a p"
   140 unfolding Polynomial.order_def
   141 apply (rule Least_equality [symmetric])
   142 apply (rule assms [THEN conjunct2])
   143 apply (erule contrapos_np)
   144 apply (rule power_le_dvd)
   145 apply (rule assms [THEN conjunct1])
   146 apply simp
   147 done
   148 
   149 lemma lemma_order_pderiv1:
   150   "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
   151     smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
   152 apply (simp only: pderiv_mult pderiv_power_Suc)
   153 apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
   154 done
   155 
   156 lemma dvd_add_cancel1:
   157   fixes a b c :: "'a::comm_ring_1"
   158   shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   159   by (drule (1) Rings.dvd_diff, simp)
   160 
   161 lemma lemma_order_pderiv [rule_format]:
   162      "\<forall>p q a. 0 < n &
   163        pderiv p \<noteq> 0 &
   164        p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
   165        --> n = Suc (order a (pderiv p))"
   166  apply (cases "n", safe, rename_tac n p q a)
   167  apply (rule order_unique_lemma)
   168  apply (rule conjI)
   169   apply (subst lemma_order_pderiv1)
   170   apply (rule dvd_add)
   171    apply (rule dvd_mult2)
   172    apply (rule le_imp_power_dvd, simp)
   173   apply (rule dvd_smult)
   174   apply (rule dvd_mult)
   175   apply (rule dvd_refl)
   176  apply (subst lemma_order_pderiv1)
   177  apply (erule contrapos_nn) back
   178  apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
   179   apply (simp del: mult_pCons_left)
   180  apply (drule dvd_add_cancel1)
   181   apply (simp del: mult_pCons_left)
   182  apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
   183  apply assumption
   184 done
   185 
   186 lemma order_decomp:
   187      "p \<noteq> 0
   188       ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
   189                 ~([:-a, 1:] dvd q)"
   190 apply (drule order [where a=a])
   191 apply (erule conjE)
   192 apply (erule dvdE)
   193 apply (rule exI)
   194 apply (rule conjI, assumption)
   195 apply (erule contrapos_nn)
   196 apply (erule ssubst) back
   197 apply (subst power_Suc2)
   198 apply (erule mult_dvd_mono [OF dvd_refl])
   199 done
   200 
   201 lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   202       ==> (order a p = Suc (order a (pderiv p)))"
   203 apply (case_tac "p = 0", simp)
   204 apply (drule_tac a = a and p = p in order_decomp)
   205 using neq0_conv
   206 apply (blast intro: lemma_order_pderiv)
   207 done
   208 
   209 lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
   210 proof -
   211   def i \<equiv> "order a p"
   212   def j \<equiv> "order a q"
   213   def t \<equiv> "[:-a, 1:]"
   214   have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
   215     unfolding t_def by (simp add: dvd_iff_poly_eq_0)
   216   assume "p * q \<noteq> 0"
   217   then show "order a (p * q) = i + j"
   218     apply clarsimp
   219     apply (drule order [where a=a and p=p, folded i_def t_def])
   220     apply (drule order [where a=a and p=q, folded j_def t_def])
   221     apply clarify
   222     apply (rule order_unique_lemma [symmetric], fold t_def)
   223     apply (erule dvdE)+
   224     apply (simp add: power_add t_dvd_iff)
   225     done
   226 qed
   227 
   228 text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
   229 
   230 lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
   231 apply (cases "p = 0", auto)
   232 apply (drule order_2 [where a=a and p=p])
   233 apply (erule contrapos_np)
   234 apply (erule power_le_dvd)
   235 apply simp
   236 apply (erule power_le_dvd [OF order_1])
   237 done
   238 
   239 lemma poly_squarefree_decomp_order:
   240   assumes "pderiv p \<noteq> 0"
   241   and p: "p = q * d"
   242   and p': "pderiv p = e * d"
   243   and d: "d = r * p + s * pderiv p"
   244   shows "order a q = (if order a p = 0 then 0 else 1)"
   245 proof (rule classical)
   246   assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
   247   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   248   with p have "order a p = order a q + order a d"
   249     by (simp add: order_mult)
   250   with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
   251   have "order a (pderiv p) = order a e + order a d"
   252     using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
   253   have "order a p = Suc (order a (pderiv p))"
   254     using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
   255   have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
   256   have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
   257     apply (simp add: d)
   258     apply (rule dvd_add)
   259     apply (rule dvd_mult)
   260     apply (simp add: order_divides `p \<noteq> 0`
   261            `order a p = Suc (order a (pderiv p))`)
   262     apply (rule dvd_mult)
   263     apply (simp add: order_divides)
   264     done
   265   then have "order a (pderiv p) \<le> order a d"
   266     using `d \<noteq> 0` by (simp add: order_divides)
   267   show ?thesis
   268     using `order a p = order a q + order a d`
   269     using `order a (pderiv p) = order a e + order a d`
   270     using `order a p = Suc (order a (pderiv p))`
   271     using `order a (pderiv p) \<le> order a d`
   272     by auto
   273 qed
   274 
   275 lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
   276          p = q * d;
   277          pderiv p = e * d;
   278          d = r * p + s * pderiv p
   279       |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   280 apply (blast intro: poly_squarefree_decomp_order)
   281 done
   282 
   283 lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   284       ==> (order a (pderiv p) = n) = (order a p = Suc n)"
   285 apply (auto dest: order_pderiv)
   286 done
   287 
   288 definition
   289   rsquarefree :: "'a::idom poly => bool" where
   290   "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
   291 
   292 lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
   293 apply (simp add: pderiv_eq_0_iff)
   294 apply (case_tac p, auto split: if_splits)
   295 done
   296 
   297 lemma rsquarefree_roots:
   298   "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
   299 apply (simp add: rsquarefree_def)
   300 apply (case_tac "p = 0", simp, simp)
   301 apply (case_tac "pderiv p = 0")
   302 apply simp
   303 apply (drule pderiv_iszero, clarify)
   304 apply simp
   305 apply (rule allI)
   306 apply (cut_tac p = "[:h:]" and a = a in order_root)
   307 apply simp
   308 apply (auto simp add: order_root order_pderiv2)
   309 apply (erule_tac x="a" in allE, simp)
   310 done
   311 
   312 lemma poly_squarefree_decomp:
   313   assumes "pderiv p \<noteq> 0"
   314     and "p = q * d"
   315     and "pderiv p = e * d"
   316     and "d = r * p + s * pderiv p"
   317   shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
   318 proof -
   319   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   320   with `p = q * d` have "q \<noteq> 0" by simp
   321   have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   322     using assms by (rule poly_squarefree_decomp_order2)
   323   with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
   324     by (simp add: rsquarefree_def order_root)
   325 qed
   326 
   327 end