src/HOL/Transcendental.thy
changeset 71585 4b1021677f15
parent 70817 dd675800469d
child 71837 dca11678c495
equal deleted inserted replaced
71565:24b68a932f26 71585:4b1021677f15
   616 
   616 
   617 lemma sumr_diff_mult_const2: "sum f {..<n} - of_nat n * r = (\<Sum>i<n. f i - r)"
   617 lemma sumr_diff_mult_const2: "sum f {..<n} - of_nat n * r = (\<Sum>i<n. f i - r)"
   618   for r :: "'a::ring_1"
   618   for r :: "'a::ring_1"
   619   by (simp add: sum_subtractf)
   619   by (simp add: sum_subtractf)
   620 
   620 
   621 lemma lemma_realpow_rev_sumr:
       
   622   "(\<Sum>p<Suc n. (x ^ p) * (y ^ (n - p))) = (\<Sum>p<Suc n. (x ^ (n - p)) * (y ^ p))"
       
   623   by (subst sum.nat_diff_reindex[symmetric]) simp
       
   624 
       
   625 lemma lemma_termdiff2:
   621 lemma lemma_termdiff2:
   626   fixes h :: "'a::field"
   622   fixes h :: "'a::field"
   627   assumes h: "h \<noteq> 0"
   623   assumes h: "h \<noteq> 0"
   628   shows "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
   624   shows "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
   629          h * (\<Sum>p< n - Suc 0. \<Sum>q< n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))"
   625          h * (\<Sum>p< n - Suc 0. \<Sum>q< n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))"
   630     (is "?lhs = ?rhs")
   626     (is "?lhs = ?rhs")
   631 proof (cases n)
   627 proof (cases n)
   632   case (Suc n)
   628   case (Suc m)
   633   have 0: "\<And>x k. (\<Sum>n<Suc k. h * (z ^ x * (z ^ (k - n) * (h + z) ^ n))) =
   629   have 0: "\<And>x k. (\<Sum>n<Suc k. h * (z ^ x * (z ^ (k - n) * (h + z) ^ n))) =
   634                  (\<Sum>j<Suc k.  h * ((h + z) ^ j * z ^ (x + k - j)))"
   630                  (\<Sum>j<Suc k.  h * ((h + z) ^ j * z ^ (x + k - j)))"
   635     apply (rule sum.cong [OF refl])
   631     by (auto simp add: power_add [symmetric] mult.commute intro: sum.cong)
   636     by (simp add: power_add [symmetric] mult.commute)
   632   have *: "(\<Sum>i<m. z ^ i * ((z + h) ^ (m - i) - z ^ (m - i))) =
   637   have *: "(\<Sum>i<n. z ^ i * ((z + h) ^ (n - i) - z ^ (n - i))) =
   633            (\<Sum>i<m. \<Sum>j<m - i. h * ((z + h) ^ j * z ^ (m - Suc j)))"
   638            (\<Sum>i<n. \<Sum>j<n - i. h * ((z + h) ^ j * z ^ (n - Suc j)))"
   634     by (force simp add: less_iff_Suc_add sum_distrib_left diff_power_eq_sum ac_simps 0
   639     apply (rule sum.cong [OF refl])
   635         simp del: sum.lessThan_Suc power_Suc intro: sum.cong)
   640     apply (clarsimp simp add: less_iff_Suc_add sum_distrib_left diff_power_eq_sum ac_simps 0
   636   have "h * ?lhs = (z + h) ^ n - z ^ n - h * of_nat n * z ^ (n - Suc 0)"
   641         simp del: sum.lessThan_Suc power_Suc)
   637     by (simp add: right_diff_distrib diff_divide_distrib h mult.assoc [symmetric])
   642     done
   638   also have "... = h * ((\<Sum>p<Suc m. (z + h) ^ p * z ^ (m - p)) - of_nat (Suc m) * z ^ m)"
   643   have "h * ?lhs = h * ?rhs"
   639     by (simp add: Suc diff_power_eq_sum h right_diff_distrib [symmetric] mult.assoc
   644     apply (simp add: right_diff_distrib diff_divide_distrib h mult.assoc [symmetric])
       
   645     using Suc
       
   646     apply (simp add: diff_power_eq_sum h right_diff_distrib [symmetric] mult.assoc
       
   647         del: power_Suc sum.lessThan_Suc of_nat_Suc)
   640         del: power_Suc sum.lessThan_Suc of_nat_Suc)
   648     apply (subst lemma_realpow_rev_sumr)
   641   also have "... = h * ((\<Sum>p<Suc m. (z + h) ^ (m - p) * z ^ p) - of_nat (Suc m) * z ^ m)"
   649     apply (subst sumr_diff_mult_const2)
   642     by (subst sum.nat_diff_reindex[symmetric]) simp
   650     apply (simp add: lemma_termdiff1 sum_distrib_left *)
   643   also have "... = h * (\<Sum>i<Suc m. (z + h) ^ (m - i) * z ^ i - z ^ m)"
   651     done
   644     by (simp add: sum_subtractf)
       
   645   also have "... = h * ?rhs"
       
   646     by (simp add: lemma_termdiff1 sum_distrib_left Suc *)
       
   647   finally have "h * ?lhs = h * ?rhs" .
   652   then show ?thesis
   648   then show ?thesis
   653     by (simp add: h)
   649     by (simp add: h)
   654 qed auto
   650 qed auto
   655 
   651 
   656 
   652 
   657 lemma real_sum_nat_ivl_bounded2:
   653 lemma real_sum_nat_ivl_bounded2:
   658   fixes K :: "'a::linordered_semidom"
   654   fixes K :: "'a::linordered_semidom"
   659   assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K"
   655   assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K" and K: "0 \<le> K"
   660     and K: "0 \<le> K"
       
   661   shows "sum f {..<n-k} \<le> of_nat n * K"
   656   shows "sum f {..<n-k} \<le> of_nat n * K"
   662   apply (rule order_trans [OF sum_mono [OF f]])
   657 proof -
   663   apply (auto simp: mult_right_mono K)
   658   have "sum f {..<n-k} \<le> (\<Sum>i<n - k. K)"
   664   done
   659     by (rule sum_mono [OF f]) auto
       
   660   also have "... \<le> of_nat n * K"
       
   661     by (auto simp: mult_right_mono K)
       
   662   finally show ?thesis .
       
   663 qed
   665 
   664 
   666 lemma lemma_termdiff3:
   665 lemma lemma_termdiff3:
   667   fixes h z :: "'a::real_normed_field"
   666   fixes h z :: "'a::real_normed_field"
   668   assumes 1: "h \<noteq> 0"
   667   assumes 1: "h \<noteq> 0"
   669     and 2: "norm z \<le> K"
   668     and 2: "norm z \<le> K"
   676     by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
   675     by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
   677   also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
   676   also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
   678   proof (rule mult_right_mono [OF _ norm_ge_zero])
   677   proof (rule mult_right_mono [OF _ norm_ge_zero])
   679     from norm_ge_zero 2 have K: "0 \<le> K"
   678     from norm_ge_zero 2 have K: "0 \<le> K"
   680       by (rule order_trans)
   679       by (rule order_trans)
   681     have le_Kn: "\<And>i j n. i + j = n \<Longrightarrow> norm ((z + h) ^ i * z ^ j) \<le> K ^ n"
   680     have le_Kn: "norm ((z + h) ^ i * z ^ j) \<le> K ^ n" if "i + j = n" for i j n
   682       apply (erule subst)
   681     proof -
   683       apply (simp only: norm_mult norm_power power_add)
   682       have "norm (z + h) ^ i * norm z ^ j \<le> K ^ i * K ^ j"
   684       apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
   683         by (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
   685       done
   684       also have "... = K^n"
       
   685         by (metis power_add that)
       
   686       finally show ?thesis
       
   687         by (simp add: norm_mult norm_power) 
       
   688     qed
       
   689     then have "\<And>p q.
       
   690        \<lbrakk>p < n; q < n - Suc 0\<rbrakk> \<Longrightarrow> norm ((z + h) ^ q * z ^ (n - 2 - q)) \<le> K ^ (n - 2)"
       
   691       by simp
       
   692     then
   686     show "norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) \<le>
   693     show "norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) \<le>
   687         of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
   694         of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
   688       apply (intro
   695       by (intro order_trans [OF norm_sum]
   689           order_trans [OF norm_sum]
   696           real_sum_nat_ivl_bounded2 mult_nonneg_nonneg of_nat_0_le_iff zero_le_power K)
   690           real_sum_nat_ivl_bounded2
       
   691           mult_nonneg_nonneg
       
   692           of_nat_0_le_iff
       
   693           zero_le_power K)
       
   694       apply (rule le_Kn, simp)
       
   695       done
       
   696   qed
   697   qed
   697   also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
   698   also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
   698     by (simp only: mult.assoc)
   699     by (simp only: mult.assoc)
   699   finally show ?thesis .
   700   finally show ?thesis .
   700 qed
   701 qed
   773     then have "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
   774     then have "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
   774       using r by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
   775       using r by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
   775     then have "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
   776     then have "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
   776       by (rule diffs_equiv [THEN sums_summable])
   777       by (rule diffs_equiv [THEN sums_summable])
   777     also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
   778     also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
   778       (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
   779                (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
   779       apply (rule ext)
   780       by (simp add: diffs_def r_neq_0 fun_eq_iff split: nat_diff_split)
   780       apply (case_tac n)
       
   781       apply (simp_all add: diffs_def r_neq_0)
       
   782       done
       
   783     finally have "summable
   781     finally have "summable
   784       (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
   782       (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
   785       by (rule diffs_equiv [THEN sums_summable])
   783       by (rule diffs_equiv [THEN sums_summable])
   786     also have
   784     also have
   787       "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0)) =
   785       "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0)) =
   788        (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
   786        (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
   789       apply (rule ext)
   787       by (rule ext) (simp add: r_neq_0 split: nat_diff_split)
   790       apply (case_tac n, simp)
       
   791       apply (rename_tac nat)
       
   792       apply (case_tac nat, simp)
       
   793       apply (simp add: r_neq_0)
       
   794       done
       
   795     finally show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
   788     finally show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
   796   next
   789   next
   797     fix h :: 'a
   790     fix h :: 'a and n
   798     fix n :: nat
       
   799     assume h: "h \<noteq> 0"
   791     assume h: "h \<noteq> 0"
   800     assume "norm h < r - norm x"
   792     assume "norm h < r - norm x"
   801     then have "norm x + norm h < r" by simp
   793     then have "norm x + norm h < r" by simp
   802     with norm_triangle_ineq have xh: "norm (x + h) < r"
   794     with norm_triangle_ineq 
       
   795     have xh: "norm (x + h) < r"
   803       by (rule order_le_less_trans)
   796       by (rule order_le_less_trans)
   804     show "norm (c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) \<le>
   797     have "norm (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0))
       
   798     \<le> real n * (real (n - Suc 0) * (r ^ (n - 2) * norm h))"
       
   799       by (metis (mono_tags, lifting) h mult.assoc lemma_termdiff3 less_eq_real_def r1 xh)
       
   800     then show "norm (c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) \<le>
   805       norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
   801       norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
   806       apply (simp only: norm_mult mult.assoc)
   802       by (simp only: norm_mult mult.assoc mult_left_mono [OF _ norm_ge_zero])
   807       apply (rule mult_left_mono [OF _ norm_ge_zero])
       
   808       apply (simp add: mult.assoc [symmetric])
       
   809       apply (metis h lemma_termdiff3 less_eq_real_def r1 xh)
       
   810       done
       
   811   qed
   803   qed
   812 qed
   804 qed
   813 
   805 
   814 lemma termdiffs:
   806 lemma termdiffs:
   815   fixes K x :: "'a::{real_normed_field,banach}"
   807   fixes K x :: "'a::{real_normed_field,banach}"
   898   fixes K x :: "'a::{real_normed_field,banach}"
   890   fixes K x :: "'a::{real_normed_field,banach}"
   899   assumes sm: "summable (\<lambda>n. c n * K ^ n)"
   891   assumes sm: "summable (\<lambda>n. c n * K ^ n)"
   900     and K: "norm x < norm K"
   892     and K: "norm x < norm K"
   901   shows "DERIV (\<lambda>x. \<Sum>n. c n * x^n) x :> (\<Sum>n. diffs c n * x^n)"
   893   shows "DERIV (\<lambda>x. \<Sum>n. c n * x^n) x :> (\<Sum>n. diffs c n * x^n)"
   902 proof -
   894 proof -
   903   have K2: "norm ((of_real (norm K) + of_real (norm x)) / 2 :: 'a) < norm K"
   895   have "norm K + norm x < norm K + norm K"
   904     using K
   896     using K by force
   905     apply (auto simp: norm_divide field_simps)
   897   then have K2: "norm ((of_real (norm K) + of_real (norm x)) / 2 :: 'a) < norm K"
   906     apply (rule le_less_trans [of _ "of_real (norm K) + of_real (norm x)"])
   898     by (auto simp: norm_triangle_lt norm_divide field_simps)
   907      apply (auto simp: mult_2_right norm_triangle_mono)
       
   908     done
       
   909   then have [simp]: "norm ((of_real (norm K) + of_real (norm x)) :: 'a) < norm K * 2"
   899   then have [simp]: "norm ((of_real (norm K) + of_real (norm x)) :: 'a) < norm K * 2"
   910     by simp
   900     by simp
   911   have "summable (\<lambda>n. c n * (of_real (norm x + norm K) / 2) ^ n)"
   901   have "summable (\<lambda>n. c n * (of_real (norm x + norm K) / 2) ^ n)"
   912     by (metis K2 summable_norm_cancel [OF powser_insidea [OF sm]] add.commute of_real_add)
   902     by (metis K2 summable_norm_cancel [OF powser_insidea [OF sm]] add.commute of_real_add)
   913   moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs c n * x ^ n)"
   903   moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs c n * x ^ n)"
   914     by (blast intro: sm termdiff_converges powser_inside)
   904     by (blast intro: sm termdiff_converges powser_inside)
   915   moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs(diffs c) n * x ^ n)"
   905   moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs(diffs c) n * x ^ n)"
   916     by (blast intro: sm termdiff_converges powser_inside)
   906     by (blast intro: sm termdiff_converges powser_inside)
   917   ultimately show ?thesis
   907   ultimately show ?thesis
   918     apply (rule termdiffs [where K = "of_real (norm x + norm K) / 2"])
   908     by (rule termdiffs [where K = "of_real (norm x + norm K) / 2"])
   919     using K
   909        (use K in \<open>auto simp: field_simps simp flip: of_real_add\<close>)
   920       apply (auto simp: field_simps)
       
   921     apply (simp flip: of_real_add)
       
   922     done
       
   923 qed
   910 qed
   924 
   911 
   925 lemma termdiffs_strong_converges_everywhere:
   912 lemma termdiffs_strong_converges_everywhere:
   926   fixes K x :: "'a::{real_normed_field,banach}"
   913   fixes K x :: "'a::{real_normed_field,banach}"
   927   assumes "\<And>y. summable (\<lambda>n. c n * y ^ n)"
   914   assumes "\<And>y. summable (\<lambda>n. c n * y ^ n)"
   997     by (rule termdiffs_strong) (use s in \<open>auto simp: norm_divide\<close>)
   984     by (rule termdiffs_strong) (use s in \<open>auto simp: norm_divide\<close>)
   998   then have "isCont (\<lambda>x. \<Sum>n. a n * x ^ n) 0"
   985   then have "isCont (\<lambda>x. \<Sum>n. a n * x ^ n) 0"
   999     by (blast intro: DERIV_continuous)
   986     by (blast intro: DERIV_continuous)
  1000   then have "((\<lambda>x. \<Sum>n. a n * x ^ n) \<longlongrightarrow> a 0) (at 0)"
   987   then have "((\<lambda>x. \<Sum>n. a n * x ^ n) \<longlongrightarrow> a 0) (at 0)"
  1001     by (simp add: continuous_within)
   988     by (simp add: continuous_within)
  1002   then show ?thesis
   989   moreover have "(\<lambda>x. f x - (\<Sum>n. a n * x ^ n)) \<midarrow>0\<rightarrow> 0"
  1003     apply (rule Lim_transform)
       
  1004     apply (clarsimp simp: LIM_eq)
   990     apply (clarsimp simp: LIM_eq)
  1005     apply (rule_tac x=s in exI)
   991     apply (rule_tac x=s in exI)
  1006     using s sm sums_unique by fastforce
   992     using s sm sums_unique by fastforce
       
   993   ultimately show ?thesis
       
   994     by (rule Lim_transform)
  1007 qed
   995 qed
  1008 
   996 
  1009 lemma powser_limit_0_strong:
   997 lemma powser_limit_0_strong:
  1010   fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
   998   fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
  1011   assumes s: "0 < s"
   999   assumes s: "0 < s"
  1013   shows "(f \<longlongrightarrow> a 0) (at 0)"
  1001   shows "(f \<longlongrightarrow> a 0) (at 0)"
  1014 proof -
  1002 proof -
  1015   have *: "((\<lambda>x. if x = 0 then a 0 else f x) \<longlongrightarrow> a 0) (at 0)"
  1003   have *: "((\<lambda>x. if x = 0 then a 0 else f x) \<longlongrightarrow> a 0) (at 0)"
  1016     by (rule powser_limit_0 [OF s]) (auto simp: powser_sums_zero sm)
  1004     by (rule powser_limit_0 [OF s]) (auto simp: powser_sums_zero sm)
  1017   show ?thesis
  1005   show ?thesis
  1018     apply (subst LIM_equal [where g = "(\<lambda>x. if x = 0 then a 0 else f x)"])
  1006     by (simp add: * LIM_equal [where g = "(\<lambda>x. if x = 0 then a 0 else f x)"])
  1019      apply (simp_all add: *)
       
  1020     done
       
  1021 qed
  1007 qed
  1022 
  1008 
  1023 
  1009 
  1024 subsection \<open>Derivability of power series\<close>
  1010 subsection \<open>Derivability of power series\<close>
  1025 
  1011 
  1589 proof
  1575 proof
  1590   assume "0 < x"
  1576   assume "0 < x"
  1591   have "1 + x \<le> (\<Sum>n<2. inverse (fact n) * x^n)"
  1577   have "1 + x \<le> (\<Sum>n<2. inverse (fact n) * x^n)"
  1592     by (auto simp: numeral_2_eq_2)
  1578     by (auto simp: numeral_2_eq_2)
  1593   also have "\<dots> \<le> (\<Sum>n. inverse (fact n) * x^n)"
  1579   also have "\<dots> \<le> (\<Sum>n. inverse (fact n) * x^n)"
  1594     apply (rule sum_le_suminf [OF summable_exp])
  1580   proof (rule sum_le_suminf [OF summable_exp])
  1595     using \<open>0 < x\<close>
  1581     show "\<forall>m\<in>- {..<2}. 0 \<le> inverse (fact m) * x ^ m"
  1596     apply (auto  simp add: zero_le_mult_iff)
  1582       using \<open>0 < x\<close> by (auto  simp add: zero_le_mult_iff)
  1597     done
  1583   qed auto
  1598   finally show "1 + x \<le> exp x"
  1584   finally show "1 + x \<le> exp x"
  1599     by (simp add: exp_def)
  1585     by (simp add: exp_def)
  1600 qed auto
  1586 qed auto
  1601 
  1587 
  1602 lemma exp_gt_one: "0 < x \<Longrightarrow> 1 < exp x"
  1588 lemma exp_gt_one: "0 < x \<Longrightarrow> 1 < exp x"
  2047 lemma exp_ge_add_one_self [simp]: "1 + x \<le> exp x"
  2033 lemma exp_ge_add_one_self [simp]: "1 + x \<le> exp x"
  2048   for x :: real
  2034   for x :: real
  2049 proof (cases "0 \<le> x \<or> x \<le> -1")
  2035 proof (cases "0 \<le> x \<or> x \<le> -1")
  2050   case True
  2036   case True
  2051   then show ?thesis
  2037   then show ?thesis
  2052     apply (rule disjE)
  2038     by (meson exp_ge_add_one_self_aux exp_ge_zero order.trans real_add_le_0_iff)
  2053      apply (simp add: exp_ge_add_one_self_aux)
       
  2054     using exp_ge_zero order_trans real_add_le_0_iff by blast
       
  2055 next
  2039 next
  2056   case False
  2040   case False
  2057   then have ln1: "ln (1 + x) \<le> x"
  2041   then have ln1: "ln (1 + x) \<le> x"
  2058     using ln_one_minus_pos_upper_bound [of "-x"] by simp
  2042     using ln_one_minus_pos_upper_bound [of "-x"] by simp
  2059   have "1 + x = exp (ln (1 + x))"
  2043   have "1 + x = exp (ln (1 + x))"
  2461   by (simp add: powr_def)
  2445   by (simp add: powr_def)
  2462 
  2446 
  2463 lemma powr_non_neg[simp]: "\<not>a powr x < 0" for a x::real
  2447 lemma powr_non_neg[simp]: "\<not>a powr x < 0" for a x::real
  2464   using powr_ge_pzero[of a x] by arith
  2448   using powr_ge_pzero[of a x] by arith
  2465 
  2449 
       
  2450 lemma inverse_powr: "\<And>y::real. 0 \<le> y \<Longrightarrow> inverse y powr a = inverse (y powr a)"
       
  2451     by (simp add: exp_minus ln_inverse powr_def)
       
  2452 
  2466 lemma powr_divide: "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
  2453 lemma powr_divide: "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
  2467   for a b x :: real
  2454   for a b x :: real
  2468   apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
  2455     by (simp add: divide_inverse powr_mult inverse_powr)
  2469   apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
       
  2470   done
       
  2471 
  2456 
  2472 lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
  2457 lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
  2473   for a b x :: "'a::{ln,real_normed_field}"
  2458   for a b x :: "'a::{ln,real_normed_field}"
  2474   by (simp add: powr_def exp_add [symmetric] distrib_right)
  2459   by (simp add: powr_def exp_add [symmetric] distrib_right)
  2475 
  2460 
  3196   unfolding cos_coeff_def sin_coeff_def
  3181   unfolding cos_coeff_def sin_coeff_def
  3197   by (simp del: mult_Suc) (auto elim: oddE)
  3182   by (simp del: mult_Suc) (auto elim: oddE)
  3198 
  3183 
  3199 lemma summable_norm_sin: "summable (\<lambda>n. norm (sin_coeff n *\<^sub>R x^n))"
  3184 lemma summable_norm_sin: "summable (\<lambda>n. norm (sin_coeff n *\<^sub>R x^n))"
  3200   for x :: "'a::{real_normed_algebra_1,banach}"
  3185   for x :: "'a::{real_normed_algebra_1,banach}"
  3201   unfolding sin_coeff_def
  3186 proof (rule summable_comparison_test [OF _ summable_norm_exp])
  3202   apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
  3187   show "\<exists>N. \<forall>n\<ge>N. norm (norm (sin_coeff n *\<^sub>R x ^ n)) \<le> norm (x ^ n /\<^sub>R fact n)"
  3203   apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  3188     unfolding sin_coeff_def
  3204   done
  3189     by (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
       
  3190 qed
  3205 
  3191 
  3206 lemma summable_norm_cos: "summable (\<lambda>n. norm (cos_coeff n *\<^sub>R x^n))"
  3192 lemma summable_norm_cos: "summable (\<lambda>n. norm (cos_coeff n *\<^sub>R x^n))"
  3207   for x :: "'a::{real_normed_algebra_1,banach}"
  3193   for x :: "'a::{real_normed_algebra_1,banach}"
  3208   unfolding cos_coeff_def
  3194 proof (rule summable_comparison_test [OF _ summable_norm_exp])
  3209   apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
  3195   show "\<exists>N. \<forall>n\<ge>N. norm (norm (cos_coeff n *\<^sub>R x ^ n)) \<le> norm (x ^ n /\<^sub>R fact n)"
  3210   apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  3196     unfolding cos_coeff_def
  3211   done
  3197     by (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
       
  3198 qed
       
  3199 
  3212 
  3200 
  3213 lemma sin_converges: "(\<lambda>n. sin_coeff n *\<^sub>R x^n) sums sin x"
  3201 lemma sin_converges: "(\<lambda>n. sin_coeff n *\<^sub>R x^n) sums sin x"
  3214   unfolding sin_def
  3202   unfolding sin_def
  3215   by (metis (full_types) summable_norm_cancel summable_norm_sin summable_sums)
  3203   by (metis (full_types) summable_norm_cancel summable_norm_sin summable_sums)
  3216 
  3204 
  3228   qed
  3216   qed
  3229   also have "\<dots> sums (sin (of_real x))"
  3217   also have "\<dots> sums (sin (of_real x))"
  3230     by (rule sin_converges)
  3218     by (rule sin_converges)
  3231   finally have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R x^n)) sums (sin (of_real x))" .
  3219   finally have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R x^n)) sums (sin (of_real x))" .
  3232   then show ?thesis
  3220   then show ?thesis
  3233     using sums_unique2 sums_of_real [OF sin_converges]
  3221     using sums_unique2 sums_of_real [OF sin_converges] by blast
  3234     by blast
       
  3235 qed
  3222 qed
  3236 
  3223 
  3237 corollary sin_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> sin z \<in> \<real>"
  3224 corollary sin_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> sin z \<in> \<real>"
  3238   by (metis Reals_cases Reals_of_real sin_of_real)
  3225   by (metis Reals_cases Reals_of_real sin_of_real)
  3239 
  3226 
  3315   by (rule DERIV_cos [THEN DERIV_isCont])
  3302   by (rule DERIV_cos [THEN DERIV_isCont])
  3316 
  3303 
  3317 lemma continuous_on_cos_real: "continuous_on {a..b} cos" for a::real
  3304 lemma continuous_on_cos_real: "continuous_on {a..b} cos" for a::real
  3318   using continuous_at_imp_continuous_on isCont_cos by blast
  3305   using continuous_at_imp_continuous_on isCont_cos by blast
  3319 
  3306 
       
  3307 
       
  3308 context
       
  3309   fixes f :: "'a::t2_space \<Rightarrow> 'b::{real_normed_field,banach}"
       
  3310 begin
       
  3311 
  3320 lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
  3312 lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
  3321   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3322   by (rule isCont_o2 [OF _ isCont_sin])
  3313   by (rule isCont_o2 [OF _ isCont_sin])
  3323 
  3314 
  3324 (* FIXME a context for f would be better *)
       
  3325 
       
  3326 lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
  3315 lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
  3327   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3328   by (rule isCont_o2 [OF _ isCont_cos])
  3316   by (rule isCont_o2 [OF _ isCont_cos])
  3329 
  3317 
  3330 lemma tendsto_sin [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) \<longlongrightarrow> sin a) F"
  3318 lemma tendsto_sin [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) \<longlongrightarrow> sin a) F"
  3331   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3332   by (rule isCont_tendsto_compose [OF isCont_sin])
  3319   by (rule isCont_tendsto_compose [OF isCont_sin])
  3333 
  3320 
  3334 lemma tendsto_cos [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) \<longlongrightarrow> cos a) F"
  3321 lemma tendsto_cos [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) \<longlongrightarrow> cos a) F"
  3335   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3336   by (rule isCont_tendsto_compose [OF isCont_cos])
  3322   by (rule isCont_tendsto_compose [OF isCont_cos])
  3337 
  3323 
  3338 lemma continuous_sin [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
  3324 lemma continuous_sin [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
  3339   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3340   unfolding continuous_def by (rule tendsto_sin)
  3325   unfolding continuous_def by (rule tendsto_sin)
  3341 
  3326 
  3342 lemma continuous_on_sin [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
  3327 lemma continuous_on_sin [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
  3343   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3344   unfolding continuous_on_def by (auto intro: tendsto_sin)
  3328   unfolding continuous_on_def by (auto intro: tendsto_sin)
       
  3329 
       
  3330 lemma continuous_cos [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
       
  3331   unfolding continuous_def by (rule tendsto_cos)
       
  3332 
       
  3333 lemma continuous_on_cos [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
       
  3334   unfolding continuous_on_def by (auto intro: tendsto_cos)
       
  3335 
       
  3336 end
  3345 
  3337 
  3346 lemma continuous_within_sin: "continuous (at z within s) sin"     
  3338 lemma continuous_within_sin: "continuous (at z within s) sin"     
  3347   for z :: "'a::{real_normed_field,banach}"
  3339   for z :: "'a::{real_normed_field,banach}"
  3348   by (simp add: continuous_within tendsto_sin)
  3340   by (simp add: continuous_within tendsto_sin)
  3349 
  3341 
  3350 lemma continuous_cos [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
       
  3351   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3352   unfolding continuous_def by (rule tendsto_cos)
       
  3353 
       
  3354 lemma continuous_on_cos [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
       
  3355   for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
       
  3356   unfolding continuous_on_def by (auto intro: tendsto_cos)
       
  3357 
       
  3358 lemma continuous_within_cos: "continuous (at z within s) cos"
  3342 lemma continuous_within_cos: "continuous (at z within s) cos"
  3359   for z :: "'a::{real_normed_field,banach}"
  3343   for z :: "'a::{real_normed_field,banach}"
  3360   by (simp add: continuous_within tendsto_cos)
  3344   by (simp add: continuous_within tendsto_cos)
  3361 
  3345 
  3362 
  3346 
  3367 
  3351 
  3368 lemma cos_zero [simp]: "cos 0 = 1"
  3352 lemma cos_zero [simp]: "cos 0 = 1"
  3369   by (simp add: cos_def cos_coeff_def scaleR_conv_of_real)
  3353   by (simp add: cos_def cos_coeff_def scaleR_conv_of_real)
  3370 
  3354 
  3371 lemma DERIV_fun_sin: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. sin (g x)) x :> cos (g x) * m"
  3355 lemma DERIV_fun_sin: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. sin (g x)) x :> cos (g x) * m"
  3372   by (auto intro!: derivative_intros)
  3356   by (fact derivative_intros)
  3373 
  3357 
  3374 lemma DERIV_fun_cos: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. cos(g x)) x :> - sin (g x) * m"
  3358 lemma DERIV_fun_cos: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. cos(g x)) x :> - sin (g x) * m"
  3375   by (auto intro!: derivative_eq_intros)
  3359   by (fact derivative_intros)
  3376 
  3360 
  3377 
  3361 
  3378 subsection \<open>Deriving the Addition Formulas\<close>
  3362 subsection \<open>Deriving the Addition Formulas\<close>
  3379 
  3363 
  3380 text \<open>The product of two cosine series.\<close>
  3364 text \<open>The product of two cosine series.\<close>
  3426     if "n \<le> p" for n p :: nat
  3410     if "n \<le> p" for n p :: nat
  3427   proof -
  3411   proof -
  3428     have "(-1) ^ ((n - Suc 0) div 2) * (-1) ^ ((p - Suc n) div 2) = - ((-1 :: real) ^ (p div 2))"
  3412     have "(-1) ^ ((n - Suc 0) div 2) * (-1) ^ ((p - Suc n) div 2) = - ((-1 :: real) ^ (p div 2))"
  3429       if np: "odd n" "even p"
  3413       if np: "odd n" "even p"
  3430     proof -
  3414     proof -
       
  3415       have "p > 0"
       
  3416         using \<open>n \<le> p\<close> neq0_conv that(1) by blast
       
  3417       then have \<section>: "(- 1::real) ^ (p div 2 - Suc 0) = - ((- 1) ^ (p div 2))"
       
  3418         using \<open>even p\<close> by (auto simp add: dvd_def power_eq_if)
  3431       from \<open>n \<le> p\<close> np have *: "n - Suc 0 + (p - Suc n) = p - Suc (Suc 0)" "Suc (Suc 0) \<le> p"
  3419       from \<open>n \<le> p\<close> np have *: "n - Suc 0 + (p - Suc n) = p - Suc (Suc 0)" "Suc (Suc 0) \<le> p"
  3432         by arith+
  3420         by arith+
  3433       have "(p - Suc (Suc 0)) div 2 = p div 2 - Suc 0"
  3421       have "(p - Suc (Suc 0)) div 2 = p div 2 - Suc 0"
  3434         by simp
  3422         by simp
  3435       with \<open>n \<le> p\<close> np * show ?thesis
  3423       with \<open>n \<le> p\<close> np  \<section> * show ?thesis
  3436         apply (simp add: power_add [symmetric] div_add [symmetric] del: div_add)
  3424         by (simp add: flip: div_add power_add)
  3437         apply (metis (no_types) One_nat_def Suc_1 le_div_geq minus_minus
       
  3438             mult.left_neutral mult_minus_left power.simps(2) zero_less_Suc)
       
  3439         done
       
  3440     qed
  3425     qed
  3441     then show ?thesis
  3426     then show ?thesis
  3442       using \<open>n\<le>p\<close> by (auto simp: algebra_simps sin_coeff_def binomial_fact)
  3427       using \<open>n\<le>p\<close> by (auto simp: algebra_simps sin_coeff_def binomial_fact)
  3443   qed
  3428   qed
  3444   then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> odd n
  3429   then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> odd n
  3882   by (simp add: cos_double)
  3867   by (simp add: cos_double)
  3883 
  3868 
  3884 lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  3869 lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  3885   by (simp add: sin_double)
  3870   by (simp add: sin_double)
  3886 
  3871 
       
  3872 context
       
  3873   fixes w :: "'a::{real_normed_field,banach}"
       
  3874 
       
  3875 begin
       
  3876 
  3887 lemma sin_times_sin: "sin w * sin z = (cos (w - z) - cos (w + z)) / 2"
  3877 lemma sin_times_sin: "sin w * sin z = (cos (w - z) - cos (w + z)) / 2"
  3888   for w :: "'a::{real_normed_field,banach}"
       
  3889   by (simp add: cos_diff cos_add)
  3878   by (simp add: cos_diff cos_add)
  3890 
  3879 
  3891 lemma sin_times_cos: "sin w * cos z = (sin (w + z) + sin (w - z)) / 2"
  3880 lemma sin_times_cos: "sin w * cos z = (sin (w + z) + sin (w - z)) / 2"
  3892   for w :: "'a::{real_normed_field,banach}"
       
  3893   by (simp add: sin_diff sin_add)
  3881   by (simp add: sin_diff sin_add)
  3894 
  3882 
  3895 lemma cos_times_sin: "cos w * sin z = (sin (w + z) - sin (w - z)) / 2"
  3883 lemma cos_times_sin: "cos w * sin z = (sin (w + z) - sin (w - z)) / 2"
  3896   for w :: "'a::{real_normed_field,banach}"
       
  3897   by (simp add: sin_diff sin_add)
  3884   by (simp add: sin_diff sin_add)
  3898 
  3885 
  3899 lemma cos_times_cos: "cos w * cos z = (cos (w - z) + cos (w + z)) / 2"
  3886 lemma cos_times_cos: "cos w * cos z = (cos (w - z) + cos (w + z)) / 2"
  3900   for w :: "'a::{real_normed_field,banach}"
       
  3901   by (simp add: cos_diff cos_add)
  3887   by (simp add: cos_diff cos_add)
       
  3888 
       
  3889 lemma cos_double_cos: "cos (2 * w) = 2 * cos w ^ 2 - 1"
       
  3890   by (simp add: cos_double sin_squared_eq)
       
  3891 
       
  3892 lemma cos_double_sin: "cos (2 * w) = 1 - 2 * sin w ^ 2"
       
  3893   by (simp add: cos_double sin_squared_eq)
       
  3894 
       
  3895 end
  3902 
  3896 
  3903 lemma sin_plus_sin: "sin w + sin z = 2 * sin ((w + z) / 2) * cos ((w - z) / 2)"
  3897 lemma sin_plus_sin: "sin w + sin z = 2 * sin ((w + z) / 2) * cos ((w - z) / 2)"
  3904   for w :: "'a::{real_normed_field,banach}" 
  3898   for w :: "'a::{real_normed_field,banach}" 
  3905   apply (simp add: mult.assoc sin_times_cos)
  3899   apply (simp add: mult.assoc sin_times_cos)
  3906   apply (simp add: field_simps)
  3900   apply (simp add: field_simps)
  3921 lemma cos_diff_cos: "cos w - cos z = 2 * sin ((w + z) / 2) * sin ((z - w) / 2)"
  3915 lemma cos_diff_cos: "cos w - cos z = 2 * sin ((w + z) / 2) * sin ((z - w) / 2)"
  3922   for w :: "'a::{real_normed_field,banach,field}"
  3916   for w :: "'a::{real_normed_field,banach,field}"
  3923   apply (simp add: mult.assoc sin_times_sin)
  3917   apply (simp add: mult.assoc sin_times_sin)
  3924   apply (simp add: field_simps)
  3918   apply (simp add: field_simps)
  3925   done
  3919   done
  3926 
       
  3927 lemma cos_double_cos: "cos (2 * z) = 2 * cos z ^ 2 - 1"
       
  3928   for z :: "'a::{real_normed_field,banach}"
       
  3929   by (simp add: cos_double sin_squared_eq)
       
  3930 
       
  3931 lemma cos_double_sin: "cos (2 * z) = 1 - 2 * sin z ^ 2"
       
  3932   for z :: "'a::{real_normed_field,banach}"
       
  3933   by (simp add: cos_double sin_squared_eq)
       
  3934 
  3920 
  3935 lemma sin_pi_minus [simp]: "sin (pi - x) = sin x"
  3921 lemma sin_pi_minus [simp]: "sin (pi - x) = sin x"
  3936   by (metis sin_minus sin_periodic_pi minus_minus uminus_add_conv_diff)
  3922   by (metis sin_minus sin_periodic_pi minus_minus uminus_add_conv_diff)
  3937 
  3923 
  3938 lemma cos_pi_minus [simp]: "cos (pi - x) = - (cos x)"
  3924 lemma cos_pi_minus [simp]: "cos (pi - x) = - (cos x)"
  4072   qed (use x in auto)
  4058   qed (use x in auto)
  4073 qed
  4059 qed
  4074 
  4060 
  4075 lemma cos_zero_lemma:
  4061 lemma cos_zero_lemma:
  4076   assumes "0 \<le> x" "cos x = 0"
  4062   assumes "0 \<le> x" "cos x = 0"
  4077   shows "\<exists>n. odd n \<and> x = of_nat n * (pi/2) \<and> n > 0"
  4063   shows "\<exists>n. odd n \<and> x = of_nat n * (pi/2)"
  4078 proof -
  4064 proof -
  4079   have xle: "x < (1 + real_of_int \<lfloor>x/pi\<rfloor>) * pi"
  4065   have xle: "x < (1 + real_of_int \<lfloor>x/pi\<rfloor>) * pi"
  4080     using floor_correct [of "x/pi"]
  4066     using floor_correct [of "x/pi"]
  4081     by (simp add: add.commute divide_less_eq)
  4067     by (simp add: add.commute divide_less_eq)
  4082   obtain n where "real n * pi \<le> x" "x < real (Suc n) * pi"
  4068   obtain n where "real n * pi \<le> x" "x < real (Suc n) * pi"
  4099     using pi_half_ge_zero uniq by fastforce
  4085     using pi_half_ge_zero uniq by fastforce
  4100   ultimately show ?thesis
  4086   ultimately show ?thesis
  4101     by (rule_tac x = "Suc (2 * n)" in exI) (simp add: algebra_simps)
  4087     by (rule_tac x = "Suc (2 * n)" in exI) (simp add: algebra_simps)
  4102 qed
  4088 qed
  4103 
  4089 
  4104 lemma sin_zero_lemma: "0 \<le> x \<Longrightarrow> sin x = 0 \<Longrightarrow> \<exists>n::nat. even n \<and> x = real n * (pi/2)"
  4090 lemma sin_zero_lemma:
  4105   using cos_zero_lemma [of "x + pi/2"]
  4091   assumes "0 \<le> x" "sin x = 0"
  4106   apply (clarsimp simp add: cos_add)
  4092   shows "\<exists>n::nat. even n \<and> x = real n * (pi/2)"
  4107   apply (rule_tac x = "n - 1" in exI)
  4093 proof -
  4108   apply (simp add: algebra_simps of_nat_diff)
  4094   obtain n where "odd n" and n: "x + pi/2 = of_nat n * (pi/2)" "n > 0"
  4109   done
  4095     using cos_zero_lemma [of "x + pi/2"] assms by (auto simp add: cos_add)
       
  4096   then have "x = real (n - 1) * (pi / 2)"
       
  4097     by (simp add: algebra_simps of_nat_diff)
       
  4098   then show ?thesis
       
  4099     by (simp add: \<open>odd n\<close>)
       
  4100 qed
  4110 
  4101 
  4111 lemma cos_zero_iff:
  4102 lemma cos_zero_iff:
  4112   "cos x = 0 \<longleftrightarrow> ((\<exists>n. odd n \<and> x = real n * (pi/2)) \<or> (\<exists>n. odd n \<and> x = - (real n * (pi/2))))"
  4103   "cos x = 0 \<longleftrightarrow> ((\<exists>n. odd n \<and> x = real n * (pi/2)) \<or> (\<exists>n. odd n \<and> x = - (real n * (pi/2))))"
  4113   (is "?lhs = ?rhs")
  4104   (is "?lhs = ?rhs")
  4114 proof -
  4105 proof -
  4144 proof
  4135 proof
  4145   show "x = 0" if "sin x = 0"
  4136   show "x = 0" if "sin x = 0"
  4146     using that assms by (auto simp: sin_zero_iff)
  4137     using that assms by (auto simp: sin_zero_iff)
  4147 qed auto
  4138 qed auto
  4148 
  4139 
  4149 lemma cos_zero_iff_int: "cos x = 0 \<longleftrightarrow> (\<exists>n. odd n \<and> x = of_int n * (pi/2))"
  4140 lemma cos_zero_iff_int: "cos x = 0 \<longleftrightarrow> (\<exists>i. odd i \<and> x = of_int i * (pi/2))"
  4150 proof -
  4141 proof -
  4151   have 1: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> real n = real_of_int i"
  4142   have 1: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> real n = real_of_int i"
  4152     by (metis even_of_nat of_int_of_nat_eq)
  4143     by (metis even_of_nat of_int_of_nat_eq)
  4153   have 2: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> - (real n * pi) = real_of_int i * pi"
  4144   have 2: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> - (real n * pi) = real_of_int i * pi"
  4154     by (metis even_minus even_of_nat mult.commute mult_minus_right of_int_minus of_int_of_nat_eq)
  4145     by (metis even_minus even_of_nat mult.commute mult_minus_right of_int_minus of_int_of_nat_eq)
  4157     by (cases i rule: int_cases2) auto
  4148     by (cases i rule: int_cases2) auto
  4158   show ?thesis
  4149   show ?thesis
  4159     by (force simp: cos_zero_iff intro!: 1 2 3)
  4150     by (force simp: cos_zero_iff intro!: 1 2 3)
  4160 qed
  4151 qed
  4161 
  4152 
  4162 lemma sin_zero_iff_int: "sin x = 0 \<longleftrightarrow> (\<exists>n. even n \<and> x = of_int n * (pi/2))"
  4153 lemma sin_zero_iff_int: "sin x = 0 \<longleftrightarrow> (\<exists>i. even i \<and> x = of_int i * (pi/2))" (is "?lhs = ?rhs")
  4163 proof safe
  4154 proof safe
  4164   assume "sin x = 0"
  4155   assume ?lhs
       
  4156   then consider (plus) n where "even n" "x = real n * (pi/2)" | (minus) n where "even n"  "x = - (real n * (pi/2))"
       
  4157     using sin_zero_iff by auto
  4165   then show "\<exists>n. even n \<and> x = of_int n * (pi/2)"
  4158   then show "\<exists>n. even n \<and> x = of_int n * (pi/2)"
  4166     apply (simp add: sin_zero_iff, safe)
  4159   proof cases
  4167      apply (metis even_of_nat of_int_of_nat_eq)
  4160     case plus
  4168     apply (rule_tac x="- (int n)" in exI)
  4161     then show ?rhs
  4169     apply simp
  4162       by (metis even_of_nat of_int_of_nat_eq)
  4170     done
  4163   next
       
  4164     case minus
       
  4165     then show ?thesis
       
  4166       by (rule_tac x="- (int n)" in exI) simp
       
  4167   qed
  4171 next
  4168 next
  4172   fix i :: int
  4169   fix i :: int
  4173   assume "even i"
  4170   assume "even i"
  4174   then show "sin (of_int i * (pi/2)) = 0"
  4171   then show "sin (of_int i * (pi/2)) = 0"
  4175     by (cases i rule: int_cases2, simp_all add: sin_zero_iff)
  4172     by (cases i rule: int_cases2, simp_all add: sin_zero_iff)
  4176 qed
  4173 qed
  4177 
  4174 
  4178 lemma sin_zero_iff_int2: "sin x = 0 \<longleftrightarrow> (\<exists>n::int. x = of_int n * pi)"
  4175 lemma sin_zero_iff_int2: "sin x = 0 \<longleftrightarrow> (\<exists>i::int. x = of_int i * pi)"
  4179   apply (simp only: sin_zero_iff_int)
  4176 proof -
  4180   apply (safe elim!: evenE)
  4177   have "sin x = 0 \<longleftrightarrow> (\<exists>i. even i \<and> x = real_of_int i * (pi / 2))"
  4181    apply (simp_all add: field_simps)
  4178     by (auto simp: sin_zero_iff_int)
  4182   using dvd_triv_left apply fastforce
  4179   also have "... = (\<exists>j. x = real_of_int (2*j) * (pi / 2))"
  4183   done
  4180     using dvd_triv_left by blast
       
  4181   also have "... = (\<exists>i::int. x = of_int i * pi)"
       
  4182     by auto
       
  4183   finally show ?thesis .
       
  4184 qed
  4184 
  4185 
  4185 lemma sin_npi_int [simp]: "sin (pi * of_int n) = 0"
  4186 lemma sin_npi_int [simp]: "sin (pi * of_int n) = 0"
  4186   by (simp add: sin_zero_iff_int2)
  4187   by (simp add: sin_zero_iff_int2)
  4187 
  4188 
  4188 lemma cos_monotone_0_pi:
  4189 lemma cos_monotone_0_pi:
  4250   shows "sin y \<le> sin x"
  4251   shows "sin y \<le> sin x"
  4251   by (metis assms le_less sin_monotone_2pi)
  4252   by (metis assms le_less sin_monotone_2pi)
  4252 
  4253 
  4253 lemma sin_x_le_x:
  4254 lemma sin_x_le_x:
  4254   fixes x :: real
  4255   fixes x :: real
  4255   assumes x: "x \<ge> 0"
  4256   assumes "x \<ge> 0"
  4256   shows "sin x \<le> x"
  4257   shows "sin x \<le> x"
  4257 proof -
  4258 proof -
  4258   let ?f = "\<lambda>x. x - sin x"
  4259   let ?f = "\<lambda>x. x - sin x"
  4259   from x have "?f x \<ge> ?f 0"
  4260   have "\<And>u. \<lbrakk>0 \<le> u; u \<le> x\<rbrakk> \<Longrightarrow> \<exists>y. (?f has_real_derivative 1 - cos u) (at u)"
  4260     apply (rule DERIV_nonneg_imp_nondecreasing)
  4261     by (auto intro!: derivative_eq_intros simp: field_simps)
  4261     apply (intro allI impI exI[of _ "1 - cos x" for x])
  4262   then have "?f x \<ge> ?f 0"
  4262     apply (auto intro!: derivative_eq_intros simp: field_simps)
  4263     by (metis cos_le_one diff_ge_0_iff_ge DERIV_nonneg_imp_nondecreasing [OF assms])
  4263     done
       
  4264   then show "sin x \<le> x" by simp
  4264   then show "sin x \<le> x" by simp
  4265 qed
  4265 qed
  4266 
  4266 
  4267 lemma sin_x_ge_neg_x:
  4267 lemma sin_x_ge_neg_x:
  4268   fixes x :: real
  4268   fixes x :: real
  4269   assumes x: "x \<ge> 0"
  4269   assumes x: "x \<ge> 0"
  4270   shows "sin x \<ge> - x"
  4270   shows "sin x \<ge> - x"
  4271 proof -
  4271 proof -
  4272   let ?f = "\<lambda>x. x + sin x"
  4272   let ?f = "\<lambda>x. x + sin x"
  4273   from x have "?f x \<ge> ?f 0"
  4273   have \<section>: "\<And>u. \<lbrakk>0 \<le> u; u \<le> x\<rbrakk> \<Longrightarrow> \<exists>y. (?f has_real_derivative 1 + cos u) (at u)"
  4274     apply (rule DERIV_nonneg_imp_nondecreasing)
  4274     by (auto intro!: derivative_eq_intros simp: field_simps)
  4275     apply (intro allI impI exI[of _ "1 + cos x" for x])
  4275   have "?f x \<ge> ?f 0"
  4276     apply (auto intro!: derivative_eq_intros simp: field_simps real_0_le_add_iff)
  4276     by (rule DERIV_nonneg_imp_nondecreasing [OF assms]) (use \<section> real_0_le_add_iff in force)
  4277     done
       
  4278   then show "sin x \<ge> -x" by simp
  4277   then show "sin x \<ge> -x" by simp
  4279 qed
  4278 qed
  4280 
  4279 
  4281 lemma abs_sin_x_le_abs_x: "\<bar>sin x\<bar> \<le> \<bar>x\<bar>"
  4280 lemma abs_sin_x_le_abs_x: "\<bar>sin x\<bar> \<le> \<bar>x\<bar>"
  4282   for x :: real
  4281   for x :: real
  4407   have *: "(sin x * (sin x * 3)) = 3 - (cos x * (cos x * 3))"
  4406   have *: "(sin x * (sin x * 3)) = 3 - (cos x * (cos x * 3))"
  4408     by (simp add: mult.assoc [symmetric] sin_squared_eq [unfolded power2_eq_square])
  4407     by (simp add: mult.assoc [symmetric] sin_squared_eq [unfolded power2_eq_square])
  4409   have "cos(3 * x) = cos(2*x + x)"
  4408   have "cos(3 * x) = cos(2*x + x)"
  4410     by simp
  4409     by simp
  4411   also have "\<dots> = 4 * cos x ^ 3 - 3 * cos x"
  4410   also have "\<dots> = 4 * cos x ^ 3 - 3 * cos x"
  4412     apply (simp only: cos_add cos_double sin_double)
  4411     unfolding cos_add cos_double sin_double
  4413     apply (simp add: * field_simps power2_eq_square power3_eq_cube)
  4412     by (simp add: * field_simps power2_eq_square power3_eq_cube)
  4414     done
       
  4415   finally show ?thesis .
  4413   finally show ?thesis .
  4416 qed
  4414 qed
  4417 
  4415 
  4418 lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
  4416 lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
  4419 proof -
  4417 proof -
  4480 
  4478 
  4481 lemma sin_int_2pin [simp]: "sin ((2 * pi) * of_int n) = 0"
  4479 lemma sin_int_2pin [simp]: "sin ((2 * pi) * of_int n) = 0"
  4482   by (metis Ints_of_int sin_integer_2pi)
  4480   by (metis Ints_of_int sin_integer_2pi)
  4483 
  4481 
  4484 lemma sincos_principal_value: "\<exists>y. (- pi < y \<and> y \<le> pi) \<and> (sin y = sin x \<and> cos y = cos x)"
  4482 lemma sincos_principal_value: "\<exists>y. (- pi < y \<and> y \<le> pi) \<and> (sin y = sin x \<and> cos y = cos x)"
  4485   apply (rule exI [where x="pi - (2 * pi) * frac ((pi - x) / (2 * pi))"])
  4483 proof -
  4486   apply (auto simp: field_simps frac_lt_1)
  4484   define y where "y \<equiv> pi - (2 * pi) * frac ((pi - x) / (2 * pi))"
  4487    apply (simp_all add: frac_def field_simps)
  4485   have "-pi < y"" y \<le> pi"
  4488    apply (simp_all add: add_divide_distrib diff_divide_distrib)
  4486     by (auto simp: field_simps frac_lt_1 y_def)
  4489    apply (simp_all add: sin_add cos_add mult.assoc [symmetric])
  4487   moreover
  4490   done
  4488   have "sin y = sin x" "cos y = cos x"
       
  4489     unfolding y_def
       
  4490      apply (simp_all add: frac_def divide_simps sin_add cos_add)
       
  4491     by (metis sin_int_2pin cos_int_2pin diff_zero add.right_neutral mult.commute mult.left_neutral mult_zero_left)+
       
  4492   ultimately
       
  4493   show ?thesis by metis
       
  4494 qed
  4491 
  4495 
  4492 
  4496 
  4493 subsection \<open>Tangent\<close>
  4497 subsection \<open>Tangent\<close>
  4494 
  4498 
  4495 definition tan :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
  4499 definition tan :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
  5236   show "- sqrt (1 - x\<^sup>2) \<noteq> 0"
  5240   show "- sqrt (1 - x\<^sup>2) \<noteq> 0"
  5237     using abs_square_eq_1 assms by force
  5241     using abs_square_eq_1 assms by force
  5238 qed (use assms isCont_arccos in auto)
  5242 qed (use assms isCont_arccos in auto)
  5239 
  5243 
  5240 lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
  5244 lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
  5241 proof (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
  5245 proof (rule DERIV_inverse_function)
  5242   show "(tan has_real_derivative 1 + x\<^sup>2) (at (arctan x))"
  5246   have "inverse ((cos (arctan x))\<^sup>2) = 1 + x\<^sup>2"
  5243     apply (rule derivative_eq_intros | simp)+
       
  5244     by (metis arctan cos_arctan_not_zero power_inverse tan_sec)
  5247     by (metis arctan cos_arctan_not_zero power_inverse tan_sec)
       
  5248   then show "(tan has_real_derivative 1 + x\<^sup>2) (at (arctan x))"
       
  5249     by (auto intro!: derivative_eq_intros)
  5245   show "\<And>y. \<lbrakk>x - 1 < y; y < x + 1\<rbrakk> \<Longrightarrow> tan (arctan y) = y"
  5250   show "\<And>y. \<lbrakk>x - 1 < y; y < x + 1\<rbrakk> \<Longrightarrow> tan (arctan y) = y"
  5246     using tan_arctan by blast
  5251     using tan_arctan by blast
  5247   show "1 + x\<^sup>2 \<noteq> 0"
  5252   show "1 + x\<^sup>2 \<noteq> 0"
  5248     by (metis power_one sum_power2_eq_zero_iff zero_neq_one)
  5253     by (metis power_one sum_power2_eq_zero_iff zero_neq_one)
  5249 qed (use isCont_arctan in auto)
  5254 qed (use isCont_arctan in auto)
  5997 
  6002 
  5998 lemma arctan_inverse:
  6003 lemma arctan_inverse:
  5999   assumes "x \<noteq> 0"
  6004   assumes "x \<noteq> 0"
  6000   shows "arctan (1 / x) = sgn x * pi/2 - arctan x"
  6005   shows "arctan (1 / x) = sgn x * pi/2 - arctan x"
  6001 proof (rule arctan_unique)
  6006 proof (rule arctan_unique)
       
  6007   have \<section>: "x > 0 \<Longrightarrow> arctan x < pi"
       
  6008     using arctan_bounded [of x] by linarith 
  6002   show "- (pi/2) < sgn x * pi/2 - arctan x"
  6009   show "- (pi/2) < sgn x * pi/2 - arctan x"
  6003     using arctan_bounded [of x] assms
  6010     using assms by (auto simp: sgn_real_def arctan algebra_simps \<section>)
  6004     unfolding sgn_real_def
       
  6005     apply (auto simp: arctan algebra_simps)
       
  6006     apply (drule zero_less_arctan_iff [THEN iffD2], arith)
       
  6007     done
       
  6008   show "sgn x * pi/2 - arctan x < pi/2"
  6011   show "sgn x * pi/2 - arctan x < pi/2"
  6009     using arctan_bounded [of "- x"] assms
  6012     using arctan_bounded [of "- x"] assms
  6010     unfolding sgn_real_def arctan_minus
  6013     by (auto simp: algebra_simps sgn_real_def arctan_minus)
  6011     by (auto simp: algebra_simps)
       
  6012   show "tan (sgn x * pi/2 - arctan x) = 1 / x"
  6014   show "tan (sgn x * pi/2 - arctan x) = 1 / x"
  6013     unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
  6015     unfolding tan_inverse [of "arctan x", unfolded tan_arctan] sgn_real_def
  6014     unfolding sgn_real_def
       
  6015     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
  6016     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
  6016 qed
  6017 qed
  6017 
  6018 
  6018 theorem pi_series: "pi / 4 = (\<Sum>k. (-1)^k * 1 / real (k * 2 + 1))"
  6019 theorem pi_series: "pi / 4 = (\<Sum>k. (-1)^k * 1 / real (k * 2 + 1))"
  6019   (is "_ = ?SUM")
  6020   (is "_ = ?SUM")
  6030 
  6031 
  6031 lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
  6032 lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
  6032   by (rule power2_le_imp_le [OF _ zero_le_one])
  6033   by (rule power2_le_imp_le [OF _ zero_le_one])
  6033     (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
  6034     (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
  6034 
  6035 
  6035 lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
       
  6036 
       
  6037 lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
       
  6038 
       
  6039 lemma polar_Ex: "\<exists>r::real. \<exists>a. x = r * cos a \<and> y = r * sin a"
  6036 lemma polar_Ex: "\<exists>r::real. \<exists>a. x = r * cos a \<and> y = r * sin a"
  6040 proof -
  6037 proof -
  6041   have polar_ex1: "0 < y \<Longrightarrow> \<exists>r a. x = r * cos a \<and> y = r * sin a" for y
  6038   have polar_ex1: "\<exists>r a. x = r * cos a \<and> y = r * sin a" if "0 < y" for y
  6042     apply (rule exI [where x = "sqrt (x\<^sup>2 + y\<^sup>2)"])
  6039   proof -
  6043     apply (rule exI [where x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))"])
  6040     have "x = sqrt (x\<^sup>2 + y\<^sup>2) * cos (arccos (x / sqrt (x\<^sup>2 + y\<^sup>2)))"
  6044     apply (simp add: cos_arccos_lemma1 sin_arccos_lemma1 power_divide
  6041       by (simp add: cos_arccos_abs [OF cos_x_y_le_one])
  6045         real_sqrt_mult [symmetric] right_diff_distrib)
  6042     moreover have "y = sqrt (x\<^sup>2 + y\<^sup>2) * sin (arccos (x / sqrt (x\<^sup>2 + y\<^sup>2)))"
  6046     done
  6043       using that
       
  6044       by (simp add: sin_arccos_abs [OF cos_x_y_le_one] power_divide right_diff_distrib flip: real_sqrt_mult)
       
  6045     ultimately show ?thesis
       
  6046       by blast
       
  6047   qed
  6047   show ?thesis
  6048   show ?thesis
  6048   proof (cases "0::real" y rule: linorder_cases)
  6049   proof (cases "0::real" y rule: linorder_cases)
  6049     case less
  6050     case less
  6050     then show ?thesis
  6051     then show ?thesis
  6051       by (rule polar_ex1)
  6052       by (rule polar_ex1)
  6081 lemma polynomial_product: (*with thanks to Chaitanya Mangla*)
  6082 lemma polynomial_product: (*with thanks to Chaitanya Mangla*)
  6082   fixes x :: "'a::idom"
  6083   fixes x :: "'a::idom"
  6083   assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
  6084   assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
  6084     and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
  6085     and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
  6085   shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
  6086   shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
  6086     (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6087          (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6087 proof -
  6088 proof -
       
  6089   have "\<And>i j. \<lbrakk>m + n - i < j; a i \<noteq> 0\<rbrakk> \<Longrightarrow> b j = 0"
       
  6090     by (meson le_add_diff leI le_less_trans m n)
       
  6091   then have \<section>: "(\<Sum>(i,j)\<in>(SIGMA i:{..m+n}. {m+n - i<..m+n}). a i * x ^ i * (b j * x ^ j)) = 0"
       
  6092     by (clarsimp simp add: sum_Un Sigma_interval_disjoint intro!: sum.neutral)
  6088   have "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) = (\<Sum>i\<le>m. \<Sum>j\<le>n. (a i * x ^ i) * (b j * x ^ j))"
  6093   have "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) = (\<Sum>i\<le>m. \<Sum>j\<le>n. (a i * x ^ i) * (b j * x ^ j))"
  6089     by (rule sum_product)
  6094     by (rule sum_product)
  6090   also have "\<dots> = (\<Sum>i\<le>m + n. \<Sum>j\<le>n + m. a i * x ^ i * (b j * x ^ j))"
  6095   also have "\<dots> = (\<Sum>i\<le>m + n. \<Sum>j\<le>n + m. a i * x ^ i * (b j * x ^ j))"
  6091     using assms by (auto simp: sum_up_index_split)
  6096     using assms by (auto simp: sum_up_index_split)
  6092   also have "\<dots> = (\<Sum>r\<le>m + n. \<Sum>j\<le>m + n - r. a r * x ^ r * (b j * x ^ j))"
  6097   also have "\<dots> = (\<Sum>r\<le>m + n. \<Sum>j\<le>m + n - r. a r * x ^ r * (b j * x ^ j))"
  6093     apply (simp add: add_ac sum.Sigma product_atMost_eq_Un)
  6098     by (simp add: add_ac sum.Sigma product_atMost_eq_Un sum_Un Sigma_interval_disjoint \<section>)
  6094     apply (clarsimp simp add: sum_Un Sigma_interval_disjoint intro!: sum.neutral)
       
  6095     apply (metis add_diff_assoc2 add.commute add_lessD1 leD m n nat_le_linear neqE)
       
  6096     done
       
  6097   also have "\<dots> = (\<Sum>(i,j)\<in>{(i,j). i+j \<le> m+n}. (a i * x ^ i) * (b j * x ^ j))"
  6099   also have "\<dots> = (\<Sum>(i,j)\<in>{(i,j). i+j \<le> m+n}. (a i * x ^ i) * (b j * x ^ j))"
  6098     by (auto simp: pairs_le_eq_Sigma sum.Sigma)
  6100     by (auto simp: pairs_le_eq_Sigma sum.Sigma)
       
  6101   also have "... = (\<Sum>k\<le>m + n. \<Sum>i\<le>k. a i * x ^ i * (b (k - i) * x ^ (k - i)))"
       
  6102     by (rule sum.triangle_reindex_eq)
  6099   also have "\<dots> = (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6103   also have "\<dots> = (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6100     apply (subst sum.triangle_reindex_eq)
  6104     by (auto simp: algebra_simps sum_distrib_left simp flip: power_add intro!: sum.cong)
  6101     apply (auto simp: algebra_simps sum_distrib_left intro!: sum.cong)
       
  6102     apply (metis le_add_diff_inverse power_add)
       
  6103     done
       
  6104   finally show ?thesis .
  6105   finally show ?thesis .
  6105 qed
  6106 qed
  6106 
  6107 
  6107 lemma polynomial_product_nat:
  6108 lemma polynomial_product_nat:
  6108   fixes x :: nat
  6109   fixes x :: nat
  6109   assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
  6110   assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
  6110     and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
  6111     and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
  6111   shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
  6112   shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
  6112     (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6113          (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
  6113   using polynomial_product [of m a n b x] assms
  6114   using polynomial_product [of m a n b x] assms
  6114   by (simp only: of_nat_mult [symmetric] of_nat_power [symmetric]
  6115   by (simp only: of_nat_mult [symmetric] of_nat_power [symmetric]
  6115       of_nat_eq_iff Int.int_sum [symmetric])
  6116       of_nat_eq_iff Int.int_sum [symmetric])
  6116 
  6117 
  6117 lemma polyfun_diff: (*COMPLEX_SUB_POLYFUN in HOL Light*)
  6118 lemma polyfun_diff: (*COMPLEX_SUB_POLYFUN in HOL Light*)
  6146     (x - y) * ((\<Sum>j<n. \<Sum>k<n-j. a(j + k + 1) * y^k * x^j))"
  6147     (x - y) * ((\<Sum>j<n. \<Sum>k<n-j. a(j + k + 1) * y^k * x^j))"
  6147 proof -
  6148 proof -
  6148   have "(\<Sum>i=Suc j..n. a i * y^(i - j - 1)) = (\<Sum>k<n-j. a(j+k+1) * y^k)"
  6149   have "(\<Sum>i=Suc j..n. a i * y^(i - j - 1)) = (\<Sum>k<n-j. a(j+k+1) * y^k)"
  6149     if "j < n" for j :: nat
  6150     if "j < n" for j :: nat
  6150   proof -
  6151   proof -
  6151     have h: "bij_betw (\<lambda>i. i - (j + 1)) {Suc j..n} (lessThan (n-j))"
  6152     have "\<And>k. k < n - j \<Longrightarrow> k \<in> (\<lambda>i. i - Suc j) ` {Suc j..n}"
  6152       apply (auto simp: bij_betw_def inj_on_def)
  6153       by (rule_tac x="k + Suc j" in image_eqI, auto)
  6153       apply (rule_tac x="x + Suc j" in image_eqI, auto)
  6154     then have h: "bij_betw (\<lambda>i. i - (j + 1)) {Suc j..n} (lessThan (n-j))"
  6154       done
  6155       by (auto simp: bij_betw_def inj_on_def)
  6155     then show ?thesis
  6156     then show ?thesis
  6156       by (auto simp: sum.reindex_bij_betw [OF h, symmetric] intro: sum.cong_simp)
  6157       by (auto simp: sum.reindex_bij_betw [OF h, symmetric] intro: sum.cong_simp)
  6157   qed
  6158   qed
  6158   then show ?thesis
  6159   then show ?thesis
  6159     by (simp add: polyfun_diff [OF assms] sum_distrib_right)
  6160     by (simp add: polyfun_diff [OF assms] sum_distrib_right)