src/HOL/Library/Poly_Deriv.thy
author haftmann
Sun Sep 07 09:49:05 2014 +0200 (2014-09-07)
changeset 58199 5fbe474b5da8
parent 56383 8e7052e9fda4
child 58881 b9556a055632
permissions -rw-r--r--
explicit theory with additional, less commonly used list operations
     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   by (induct p arbitrary: n) 
    32      (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
    33 
    34 primrec pderiv_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list"
    35 where
    36   "pderiv_coeffs [] = []"
    37 | "pderiv_coeffs (x # xs) = plus_coeffs xs (cCons 0 (pderiv_coeffs xs))"
    38 
    39 lemma coeffs_pderiv [code abstract]:
    40   "coeffs (pderiv p) = pderiv_coeffs (coeffs p)"
    41   by (rule sym, induct p) (simp_all add: pderiv_pCons coeffs_plus_eq_plus_coeffs cCons_def)
    42 
    43 lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
    44   apply (rule iffI)
    45   apply (cases p, simp)
    46   apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
    47   apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
    48   done
    49 
    50 lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
    51   apply (rule order_antisym [OF degree_le])
    52   apply (simp add: coeff_pderiv coeff_eq_0)
    53   apply (cases "degree p", simp)
    54   apply (rule le_degree)
    55   apply (simp add: coeff_pderiv del: of_nat_Suc)
    56   apply (metis degree_0 leading_coeff_0_iff nat.distinct(1))
    57   done
    58 
    59 lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
    60 by (simp add: pderiv_pCons)
    61 
    62 lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
    63 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    64 
    65 lemma pderiv_minus: "pderiv (- p) = - pderiv p"
    66 by (rule poly_eqI, simp add: coeff_pderiv)
    67 
    68 lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
    69 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    70 
    71 lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
    72 by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
    73 
    74 lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
    75 by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
    76 
    77 lemma pderiv_power_Suc:
    78   "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
    79 apply (induct n)
    80 apply simp
    81 apply (subst power_Suc)
    82 apply (subst pderiv_mult)
    83 apply (erule ssubst)
    84 apply (simp only: of_nat_Suc smult_add_left smult_1_left)
    85 apply (simp add: algebra_simps)
    86 done
    87 
    88 lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
    89 by (rule DERIV_cong, rule DERIV_pow, simp)
    90 declare DERIV_pow2 [simp] DERIV_pow [simp]
    91 
    92 lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
    93 by (rule DERIV_cong, rule DERIV_add, auto)
    94 
    95 lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
    96   by (induct p, auto intro!: derivative_eq_intros simp add: pderiv_pCons)
    97 
    98 text{* Consequences of the derivative theorem above*}
    99 
   100 lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
   101 apply (simp add: real_differentiable_def)
   102 apply (blast intro: poly_DERIV)
   103 done
   104 
   105 lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
   106 by (rule poly_DERIV [THEN DERIV_isCont])
   107 
   108 lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
   109       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   110 using IVT_objl [of "poly p" a 0 b]
   111 by (auto simp add: order_le_less)
   112 
   113 lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
   114       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
   115 by (insert poly_IVT_pos [where p = "- p" ]) simp
   116 
   117 lemma poly_MVT: "(a::real) < b ==>
   118      \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
   119 using MVT [of a b "poly p"]
   120 apply auto
   121 apply (rule_tac x = z in exI)
   122 apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
   123 done
   124 
   125 text{*Lemmas for Derivatives*}
   126 
   127 lemma order_unique_lemma:
   128   fixes p :: "'a::idom poly"
   129   assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
   130   shows "n = order a p"
   131 unfolding Polynomial.order_def
   132 apply (rule Least_equality [symmetric])
   133 apply (fact assms)
   134 apply (rule classical)
   135 apply (erule notE)
   136 unfolding not_less_eq_eq
   137 using assms(1) apply (rule power_le_dvd)
   138 apply assumption
   139 done
   140 
   141 lemma lemma_order_pderiv1:
   142   "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
   143     smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
   144 apply (simp only: pderiv_mult pderiv_power_Suc)
   145 apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
   146 done
   147 
   148 lemma dvd_add_cancel1:
   149   fixes a b c :: "'a::comm_ring_1"
   150   shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   151   by (drule (1) Rings.dvd_diff, simp)
   152 
   153 lemma lemma_order_pderiv:
   154   assumes n: "0 < n" 
   155       and pd: "pderiv p \<noteq> 0" 
   156       and pe: "p = [:- a, 1:] ^ n * q" 
   157       and nd: "~ [:- a, 1:] dvd q"
   158     shows "n = Suc (order a (pderiv p))"
   159 using n 
   160 proof -
   161   have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
   162     using assms by auto
   163   obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
   164     using assms by (cases n) auto
   165   then have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l"
   166     by (metis dvd_add_cancel1 dvd_smult_iff dvd_triv_left of_nat_eq_0_iff old.nat.distinct(2))
   167   have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" 
   168   proof (rule order_unique_lemma)
   169     show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
   170       apply (subst lemma_order_pderiv1)
   171       apply (rule dvd_add)
   172       apply (metis dvdI dvd_mult2 power_Suc2)
   173       apply (metis dvd_smult dvd_triv_right)
   174       done
   175   next
   176     show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
   177      apply (subst lemma_order_pderiv1)
   178      by (metis * nd dvd_mult_cancel_right field_power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
   179   qed
   180   then show ?thesis
   181     by (metis `n = Suc n'` pe)
   182 qed
   183 
   184 lemma order_decomp:
   185      "p \<noteq> 0
   186       ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
   187                 ~([:-a, 1:] dvd q)"
   188 apply (drule order [where a=a])
   189 by (metis dvdE dvd_mult_cancel_left power_Suc2)
   190 
   191 lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   192       ==> (order a p = Suc (order a (pderiv p)))"
   193 apply (case_tac "p = 0", simp)
   194 apply (drule_tac a = a and p = p in order_decomp)
   195 using neq0_conv
   196 apply (blast intro: lemma_order_pderiv)
   197 done
   198 
   199 lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
   200 proof -
   201   def i \<equiv> "order a p"
   202   def j \<equiv> "order a q"
   203   def t \<equiv> "[:-a, 1:]"
   204   have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
   205     unfolding t_def by (simp add: dvd_iff_poly_eq_0)
   206   assume "p * q \<noteq> 0"
   207   then show "order a (p * q) = i + j"
   208     apply clarsimp
   209     apply (drule order [where a=a and p=p, folded i_def t_def])
   210     apply (drule order [where a=a and p=q, folded j_def t_def])
   211     apply clarify
   212     apply (erule dvdE)+
   213     apply (rule order_unique_lemma [symmetric], fold t_def)
   214     apply (simp_all add: power_add t_dvd_iff)
   215     done
   216 qed
   217 
   218 text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
   219 
   220 lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
   221 apply (cases "p = 0", auto)
   222 apply (drule order_2 [where a=a and p=p])
   223 apply (metis not_less_eq_eq power_le_dvd)
   224 apply (erule power_le_dvd [OF order_1])
   225 done
   226 
   227 lemma poly_squarefree_decomp_order:
   228   assumes "pderiv p \<noteq> 0"
   229   and p: "p = q * d"
   230   and p': "pderiv p = e * d"
   231   and d: "d = r * p + s * pderiv p"
   232   shows "order a q = (if order a p = 0 then 0 else 1)"
   233 proof (rule classical)
   234   assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
   235   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   236   with p have "order a p = order a q + order a d"
   237     by (simp add: order_mult)
   238   with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
   239   have "order a (pderiv p) = order a e + order a d"
   240     using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
   241   have "order a p = Suc (order a (pderiv p))"
   242     using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
   243   have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
   244   have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
   245     apply (simp add: d)
   246     apply (rule dvd_add)
   247     apply (rule dvd_mult)
   248     apply (simp add: order_divides `p \<noteq> 0`
   249            `order a p = Suc (order a (pderiv p))`)
   250     apply (rule dvd_mult)
   251     apply (simp add: order_divides)
   252     done
   253   then have "order a (pderiv p) \<le> order a d"
   254     using `d \<noteq> 0` by (simp add: order_divides)
   255   show ?thesis
   256     using `order a p = order a q + order a d`
   257     using `order a (pderiv p) = order a e + order a d`
   258     using `order a p = Suc (order a (pderiv p))`
   259     using `order a (pderiv p) \<le> order a d`
   260     by auto
   261 qed
   262 
   263 lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
   264          p = q * d;
   265          pderiv p = e * d;
   266          d = r * p + s * pderiv p
   267       |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   268 by (blast intro: poly_squarefree_decomp_order)
   269 
   270 lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
   271       ==> (order a (pderiv p) = n) = (order a p = Suc n)"
   272 by (auto dest: order_pderiv)
   273 
   274 definition
   275   rsquarefree :: "'a::idom poly => bool" where
   276   "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
   277 
   278 lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
   279 apply (simp add: pderiv_eq_0_iff)
   280 apply (case_tac p, auto split: if_splits)
   281 done
   282 
   283 lemma rsquarefree_roots:
   284   "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
   285 apply (simp add: rsquarefree_def)
   286 apply (case_tac "p = 0", simp, simp)
   287 apply (case_tac "pderiv p = 0")
   288 apply simp
   289 apply (drule pderiv_iszero, clarsimp)
   290 apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
   291 apply (force simp add: order_root order_pderiv2)
   292 done
   293 
   294 lemma poly_squarefree_decomp:
   295   assumes "pderiv p \<noteq> 0"
   296     and "p = q * d"
   297     and "pderiv p = e * d"
   298     and "d = r * p + s * pderiv p"
   299   shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
   300 proof -
   301   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
   302   with `p = q * d` have "q \<noteq> 0" by simp
   303   have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
   304     using assms by (rule poly_squarefree_decomp_order2)
   305   with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
   306     by (simp add: rsquarefree_def order_root)
   307 qed
   308 
   309 end