
wenzelm@41959: (*  Title:      HOL/Library/Poly_Deriv.thy
huffman@29985:     Author:     Amine Chaieb
wenzelm@41959:     Author:     Brian Huffman
huffman@29985: *)
huffman@29985: 
huffman@29985: header{* Polynomials and Differentiation *}
huffman@29985: 
huffman@29985: theory Poly_Deriv
huffman@29985: imports Deriv Polynomial
huffman@29985: begin
huffman@29985: 
huffman@29985: subsection {* Derivatives of univariate polynomials *}
huffman@29985: 
huffman@29985: definition
huffman@29985:   pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly" where
huffman@29985:   "pderiv = poly_rec 0 (\<lambda>a p p'. p + pCons 0 p')"
huffman@29985: 
huffman@29985: lemma pderiv_0 [simp]: "pderiv 0 = 0"
huffman@29985:   unfolding pderiv_def by (simp add: poly_rec_0)
huffman@29985: 
huffman@29985: lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
huffman@29985:   unfolding pderiv_def by (simp add: poly_rec_pCons)
huffman@29985: 
huffman@29985: lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
huffman@29985:   apply (induct p arbitrary: n, simp)
huffman@29985:   apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
huffman@29985:   done
huffman@29985: 
huffman@29985: lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
huffman@29985:   apply (rule iffI)
huffman@29985:   apply (cases p, simp)
huffman@29985:   apply (simp add: expand_poly_eq coeff_pderiv del: of_nat_Suc)
huffman@29985:   apply (simp add: expand_poly_eq coeff_pderiv coeff_eq_0)
huffman@29985:   done
huffman@29985: 
huffman@29985: lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
huffman@29985:   apply (rule order_antisym [OF degree_le])
huffman@29985:   apply (simp add: coeff_pderiv coeff_eq_0)
huffman@29985:   apply (cases "degree p", simp)
huffman@29985:   apply (rule le_degree)
huffman@29985:   apply (simp add: coeff_pderiv del: of_nat_Suc)
huffman@29985:   apply (rule subst, assumption)
huffman@29985:   apply (rule leading_coeff_neq_0, clarsimp)
huffman@29985:   done
huffman@29985: 
huffman@29985: lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
huffman@29985: by (simp add: pderiv_pCons)
huffman@29985: 
huffman@29985: lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
huffman@29985: by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
huffman@29985: 
huffman@29985: lemma pderiv_minus: "pderiv (- p) = - pderiv p"
huffman@29985: by (rule poly_ext, simp add: coeff_pderiv)
huffman@29985: 
huffman@29985: lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
huffman@29985: by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
huffman@29985: 
huffman@29985: lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
huffman@29985: by (rule poly_ext, simp add: coeff_pderiv algebra_simps)
huffman@29985: 
huffman@29985: lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
huffman@29985: apply (induct p)
huffman@29985: apply simp
huffman@29985: apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma pderiv_power_Suc:
huffman@29985:   "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
huffman@29985: apply (induct n)
huffman@29985: apply simp
huffman@29985: apply (subst power_Suc)
huffman@29985: apply (subst pderiv_mult)
huffman@29985: apply (erule ssubst)
huffman@29985: apply (simp add: smult_add_left algebra_simps)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
huffman@29985: by (simp add: DERIV_cmult mult_commute [of _ c])
huffman@29985: 
huffman@29985: lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
huffman@29985: by (rule lemma_DERIV_subst, rule DERIV_pow, simp)
huffman@29985: declare DERIV_pow2 [simp] DERIV_pow [simp]
huffman@29985: 
huffman@29985: lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
huffman@29985: by (rule lemma_DERIV_subst, rule DERIV_add, auto)
huffman@29985: 
huffman@29985: lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
hoelzl@31881:   by (induct p, auto intro!: DERIV_intros simp add: pderiv_pCons)
huffman@29985: 
huffman@29985: text{* Consequences of the derivative theorem above*}
huffman@29985: 
huffman@29985: lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
huffman@29985: apply (simp add: differentiable_def)
huffman@29985: apply (blast intro: poly_DERIV)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
huffman@29985: by (rule poly_DERIV [THEN DERIV_isCont])
huffman@29985: 
huffman@29985: lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
huffman@29985:       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985: apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
huffman@29985: apply (auto simp add: order_le_less)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
huffman@29985:       ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985: by (insert poly_IVT_pos [where p = "- p" ]) simp
huffman@29985: 
huffman@29985: lemma poly_MVT: "(a::real) < b ==>
huffman@29985:      \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
huffman@29985: apply (drule_tac f = "poly p" in MVT, auto)
huffman@29985: apply (rule_tac x = z in exI)
huffman@29985: apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
huffman@29985: done
huffman@29985: 
huffman@29985: text{*Lemmas for Derivatives*}
huffman@29985: 
huffman@29985: lemma order_unique_lemma:
huffman@29985:   fixes p :: "'a::idom poly"
huffman@29985:   assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
huffman@29985:   shows "n = order a p"
huffman@29985: unfolding Polynomial.order_def
huffman@29985: apply (rule Least_equality [symmetric])
huffman@29985: apply (rule assms [THEN conjunct2])
huffman@29985: apply (erule contrapos_np)
huffman@29985: apply (rule power_le_dvd)
huffman@29985: apply (rule assms [THEN conjunct1])
huffman@29985: apply simp
huffman@29985: done
huffman@29985: 
huffman@29985: lemma lemma_order_pderiv1:
huffman@29985:   "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
huffman@29985:     smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
huffman@29985: apply (simp only: pderiv_mult pderiv_power_Suc)
huffman@30273: apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma dvd_add_cancel1:
huffman@29985:   fixes a b c :: "'a::comm_ring_1"
huffman@29985:   shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
haftmann@35050:   by (drule (1) Rings.dvd_diff, simp)
huffman@29985: 
huffman@29985: lemma lemma_order_pderiv [rule_format]:
huffman@29985:      "\<forall>p q a. 0 < n &
huffman@29985:        pderiv p \<noteq> 0 &
huffman@29985:        p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
huffman@29985:        --> n = Suc (order a (pderiv p))"
huffman@29985:  apply (cases "n", safe, rename_tac n p q a)
huffman@29985:  apply (rule order_unique_lemma)
huffman@29985:  apply (rule conjI)
huffman@29985:   apply (subst lemma_order_pderiv1)
huffman@29985:   apply (rule dvd_add)
huffman@29985:    apply (rule dvd_mult2)
huffman@29985:    apply (rule le_imp_power_dvd, simp)
huffman@29985:   apply (rule dvd_smult)
huffman@29985:   apply (rule dvd_mult)
huffman@29985:   apply (rule dvd_refl)
huffman@29985:  apply (subst lemma_order_pderiv1)
huffman@29985:  apply (erule contrapos_nn) back
huffman@29985:  apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
huffman@29985:   apply (simp del: mult_pCons_left)
huffman@29985:  apply (drule dvd_add_cancel1)
huffman@29985:   apply (simp del: mult_pCons_left)
huffman@29985:  apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
huffman@29985:  apply assumption
huffman@29985: done
huffman@29985: 
huffman@29985: lemma order_decomp:
huffman@29985:      "p \<noteq> 0
huffman@29985:       ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
huffman@29985:                 ~([:-a, 1:] dvd q)"
huffman@29985: apply (drule order [where a=a])
huffman@29985: apply (erule conjE)
huffman@29985: apply (erule dvdE)
huffman@29985: apply (rule exI)
huffman@29985: apply (rule conjI, assumption)
huffman@29985: apply (erule contrapos_nn)
huffman@29985: apply (erule ssubst) back
huffman@29985: apply (subst power_Suc2)
huffman@29985: apply (erule mult_dvd_mono [OF dvd_refl])
huffman@29985: done
huffman@29985: 
huffman@29985: lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985:       ==> (order a p = Suc (order a (pderiv p)))"
huffman@29985: apply (case_tac "p = 0", simp)
huffman@29985: apply (drule_tac a = a and p = p in order_decomp)
huffman@29985: using neq0_conv
huffman@29985: apply (blast intro: lemma_order_pderiv)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
huffman@29985: proof -
huffman@29985:   def i \<equiv> "order a p"
huffman@29985:   def j \<equiv> "order a q"
huffman@29985:   def t \<equiv> "[:-a, 1:]"
huffman@29985:   have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
huffman@29985:     unfolding t_def by (simp add: dvd_iff_poly_eq_0)
huffman@29985:   assume "p * q \<noteq> 0"
huffman@29985:   then show "order a (p * q) = i + j"
huffman@29985:     apply clarsimp
huffman@29985:     apply (drule order [where a=a and p=p, folded i_def t_def])
huffman@29985:     apply (drule order [where a=a and p=q, folded j_def t_def])
huffman@29985:     apply clarify
huffman@29985:     apply (rule order_unique_lemma [symmetric], fold t_def)
huffman@29985:     apply (erule dvdE)+
huffman@29985:     apply (simp add: power_add t_dvd_iff)
huffman@29985:     done
huffman@29985: qed
huffman@29985: 
huffman@29985: text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
huffman@29985: 
huffman@29985: lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
huffman@29985: apply (cases "p = 0", auto)
huffman@29985: apply (drule order_2 [where a=a and p=p])
huffman@29985: apply (erule contrapos_np)
huffman@29985: apply (erule power_le_dvd)
huffman@29985: apply simp
huffman@29985: apply (erule power_le_dvd [OF order_1])
huffman@29985: done
huffman@29985: 
huffman@29985: lemma poly_squarefree_decomp_order:
huffman@29985:   assumes "pderiv p \<noteq> 0"
huffman@29985:   and p: "p = q * d"
huffman@29985:   and p': "pderiv p = e * d"
huffman@29985:   and d: "d = r * p + s * pderiv p"
huffman@29985:   shows "order a q = (if order a p = 0 then 0 else 1)"
huffman@29985: proof (rule classical)
huffman@29985:   assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
huffman@29985:   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
huffman@29985:   with p have "order a p = order a q + order a d"
huffman@29985:     by (simp add: order_mult)
huffman@29985:   with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
huffman@29985:   have "order a (pderiv p) = order a e + order a d"
huffman@29985:     using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
huffman@29985:   have "order a p = Suc (order a (pderiv p))"
huffman@29985:     using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
huffman@29985:   have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
huffman@29985:   have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
huffman@29985:     apply (simp add: d)
huffman@29985:     apply (rule dvd_add)
huffman@29985:     apply (rule dvd_mult)
huffman@29985:     apply (simp add: order_divides `p \<noteq> 0`
huffman@29985:            `order a p = Suc (order a (pderiv p))`)
huffman@29985:     apply (rule dvd_mult)
huffman@29985:     apply (simp add: order_divides)
huffman@29985:     done
huffman@29985:   then have "order a (pderiv p) \<le> order a d"
huffman@29985:     using `d \<noteq> 0` by (simp add: order_divides)
huffman@29985:   show ?thesis
huffman@29985:     using `order a p = order a q + order a d`
huffman@29985:     using `order a (pderiv p) = order a e + order a d`
huffman@29985:     using `order a p = Suc (order a (pderiv p))`
huffman@29985:     using `order a (pderiv p) \<le> order a d`
huffman@29985:     by auto
huffman@29985: qed
huffman@29985: 
huffman@29985: lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
huffman@29985:          p = q * d;
huffman@29985:          pderiv p = e * d;
huffman@29985:          d = r * p + s * pderiv p
huffman@29985:       |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985: apply (blast intro: poly_squarefree_decomp_order)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985:       ==> (order a (pderiv p) = n) = (order a p = Suc n)"
huffman@29985: apply (auto dest: order_pderiv)
huffman@29985: done
huffman@29985: 
huffman@29985: definition
huffman@29985:   rsquarefree :: "'a::idom poly => bool" where
huffman@29985:   "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
huffman@29985: 
huffman@29985: lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
huffman@29985: apply (simp add: pderiv_eq_0_iff)
huffman@29985: apply (case_tac p, auto split: if_splits)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma rsquarefree_roots:
huffman@29985:   "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
huffman@29985: apply (simp add: rsquarefree_def)
huffman@29985: apply (case_tac "p = 0", simp, simp)
huffman@29985: apply (case_tac "pderiv p = 0")
huffman@29985: apply simp
huffman@29985: apply (drule pderiv_iszero, clarify)
huffman@29985: apply simp
huffman@29985: apply (rule allI)
huffman@29985: apply (cut_tac p = "[:h:]" and a = a in order_root)
huffman@29985: apply simp
huffman@29985: apply (auto simp add: order_root order_pderiv2)
huffman@29985: apply (erule_tac x="a" in allE, simp)
huffman@29985: done
huffman@29985: 
huffman@29985: lemma poly_squarefree_decomp:
huffman@29985:   assumes "pderiv p \<noteq> 0"
huffman@29985:     and "p = q * d"
huffman@29985:     and "pderiv p = e * d"
huffman@29985:     and "d = r * p + s * pderiv p"
huffman@29985:   shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
huffman@29985: proof -
huffman@29985:   from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
huffman@29985:   with `p = q * d` have "q \<noteq> 0" by simp
huffman@29985:   have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985:     using assms by (rule poly_squarefree_decomp_order2)
huffman@29985:   with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
huffman@29985:     by (simp add: rsquarefree_def order_root)
huffman@29985: qed
huffman@29985: 
huffman@29985: end




