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