
wenzelm@32960: (*  Title:      HOL/Transcendental.thy
wenzelm@32960:     Author:     Jacques D. Fleuriot, University of Cambridge, University of Edinburgh
wenzelm@32960:     Author:     Lawrence C Paulson
hoelzl@51527:     Author:     Jeremy Avigad
paulson@12196: *)
paulson@12196: 
paulson@15077: header{*Power Series, Transcendental Functions etc.*}
paulson@15077: 
nipkow@15131: theory Transcendental
haftmann@25600: imports Fact Series Deriv NthRoot
nipkow@15131: begin
paulson@15077: 
hoelzl@57025: lemma root_test_convergence:
hoelzl@57025:   fixes f :: "nat \<Rightarrow> 'a::banach"
hoelzl@57025:   assumes f: "(\<lambda>n. root n (norm (f n))) ----> x" -- "could be weakened to lim sup"
hoelzl@57025:   assumes "x < 1"
hoelzl@57025:   shows "summable f"
hoelzl@57025: proof -
hoelzl@57025:   have "0 \<le> x"
hoelzl@57025:     by (rule LIMSEQ_le[OF tendsto_const f]) (auto intro!: exI[of _ 1])
hoelzl@57025:   from `x < 1` obtain z where z: "x < z" "z < 1"
hoelzl@57025:     by (metis dense)
hoelzl@57025:   from f `x < z`
hoelzl@57025:   have "eventually (\<lambda>n. root n (norm (f n)) < z) sequentially"
hoelzl@57025:     by (rule order_tendstoD)
hoelzl@57025:   then have "eventually (\<lambda>n. norm (f n) \<le> z^n) sequentially"
hoelzl@57025:     using eventually_ge_at_top
hoelzl@57025:   proof eventually_elim
hoelzl@57025:     fix n assume less: "root n (norm (f n)) < z" and n: "1 \<le> n"
hoelzl@57025:     from power_strict_mono[OF less, of n] n
hoelzl@57025:     show "norm (f n) \<le> z ^ n"
hoelzl@57025:       by simp
hoelzl@57025:   qed
hoelzl@57025:   then show "summable f"
hoelzl@57025:     unfolding eventually_sequentially
hoelzl@57025:     using z `0 \<le> x` by (auto intro!: summable_comparison_test[OF _  summable_geometric])
hoelzl@57025: qed
hoelzl@57025: 
huffman@29164: subsection {* Properties of Power Series *}
paulson@15077: 
huffman@23082: lemma lemma_realpow_diff:
haftmann@31017:   fixes y :: "'a::monoid_mult"
huffman@23082:   shows "p \<le> n \<Longrightarrow> y ^ (Suc n - p) = (y ^ (n - p)) * y"
huffman@23082: proof -
huffman@23082:   assume "p \<le> n"
huffman@23082:   hence "Suc n - p = Suc (n - p)" by (rule Suc_diff_le)
huffman@30273:   thus ?thesis by (simp add: power_commutes)
huffman@23082: qed
paulson@15077: 
paulson@15229: lemma lemma_realpow_diff_sumr2:
wenzelm@53079:   fixes y :: "'a::{comm_ring,monoid_mult}"
wenzelm@53079:   shows
wenzelm@53079:     "x ^ (Suc n) - y ^ (Suc n) =
hoelzl@56193:       (x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))"
paulson@54573: proof (induct n)
paulson@54573:   case (Suc n)
paulson@54573:   have "x ^ Suc (Suc n) - y ^ Suc (Suc n) = x * (x * x ^ n) - y * (y * y ^ n)"
paulson@54573:     by simp
paulson@54573:   also have "... = y * (x ^ (Suc n) - y ^ (Suc n)) + (x - y) * (x * x ^ n)"
paulson@54573:     by (simp add: algebra_simps)
hoelzl@56193:   also have "... = y * ((x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x ^ n)"
paulson@54573:     by (simp only: Suc)
hoelzl@56193:   also have "... = (x - y) * (y * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x ^ n)"
haftmann@57512:     by (simp only: mult.left_commute)
hoelzl@56193:   also have "... = (x - y) * (\<Sum>p<Suc (Suc n). x ^ p * y ^ (Suc n - p))"
hoelzl@56193:     by (simp add: field_simps Suc_diff_le setsum_left_distrib setsum_right_distrib)
paulson@54573:   finally show ?case .
hoelzl@56193: qed simp
paulson@15077: 
wenzelm@55832: corollary power_diff_sumr2: --{* @{text COMPLEX_POLYFUN} in HOL Light *}
lp15@55734:   fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193:   shows   "x^n - y^n = (x - y) * (\<Sum>i<n. y^(n - Suc i) * x^i)"
lp15@55734: using lemma_realpow_diff_sumr2[of x "n - 1" y]
lp15@55734: by (cases "n = 0") (simp_all add: field_simps)
lp15@55734: 
paulson@15229: lemma lemma_realpow_rev_sumr:
hoelzl@56193:    "(\<Sum>p<Suc n. (x ^ p) * (y ^ (n - p))) =
hoelzl@56193:     (\<Sum>p<Suc n. (x ^ (n - p)) * (y ^ p))"
hoelzl@57129:   by (subst nat_diff_setsum_reindex[symmetric]) simp
paulson@15077: 
lp15@55719: lemma power_diff_1_eq:
lp15@55719:   fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193:   shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"
lp15@55719: using lemma_realpow_diff_sumr2 [of x _ 1] 
lp15@55719:   by (cases n) auto
lp15@55719: 
lp15@55719: lemma one_diff_power_eq':
lp15@55719:   fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193:   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"
lp15@55719: using lemma_realpow_diff_sumr2 [of 1 _ x] 
lp15@55719:   by (cases n) auto
lp15@55719: 
lp15@55719: lemma one_diff_power_eq:
lp15@55719:   fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193:   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^i)"
lp15@55719: by (metis one_diff_power_eq' [of n x] nat_diff_setsum_reindex)
lp15@55719: 
paulson@15077: text{*Power series has a `circle` of convergence, i.e. if it sums for @{term
wenzelm@53079:   x}, then it sums absolutely for @{term z} with @{term "\<bar>z\<bar> < \<bar>x\<bar>"}.*}
paulson@15077: 
paulson@15077: lemma powser_insidea:
huffman@53599:   fixes x z :: "'a::real_normed_div_algebra"
huffman@20849:   assumes 1: "summable (\<lambda>n. f n * x ^ n)"
wenzelm@53079:     and 2: "norm z < norm x"
huffman@23082:   shows "summable (\<lambda>n. norm (f n * z ^ n))"
huffman@20849: proof -
huffman@20849:   from 2 have x_neq_0: "x \<noteq> 0" by clarsimp
huffman@20849:   from 1 have "(\<lambda>n. f n * x ^ n) ----> 0"
huffman@20849:     by (rule summable_LIMSEQ_zero)
huffman@20849:   hence "convergent (\<lambda>n. f n * x ^ n)"
huffman@20849:     by (rule convergentI)
huffman@20849:   hence "Cauchy (\<lambda>n. f n * x ^ n)"
huffman@44726:     by (rule convergent_Cauchy)
huffman@20849:   hence "Bseq (\<lambda>n. f n * x ^ n)"
huffman@20849:     by (rule Cauchy_Bseq)
huffman@23082:   then obtain K where 3: "0 < K" and 4: "\<forall>n. norm (f n * x ^ n) \<le> K"
huffman@20849:     by (simp add: Bseq_def, safe)
huffman@23082:   have "\<exists>N. \<forall>n\<ge>N. norm (norm (f n * z ^ n)) \<le>
huffman@23082:                    K * norm (z ^ n) * inverse (norm (x ^ n))"
huffman@20849:   proof (intro exI allI impI)
wenzelm@53079:     fix n::nat
wenzelm@53079:     assume "0 \<le> n"
huffman@23082:     have "norm (norm (f n * z ^ n)) * norm (x ^ n) =
huffman@23082:           norm (f n * x ^ n) * norm (z ^ n)"
huffman@23082:       by (simp add: norm_mult abs_mult)
huffman@23082:     also have "\<dots> \<le> K * norm (z ^ n)"
huffman@23082:       by (simp only: mult_right_mono 4 norm_ge_zero)
huffman@23082:     also have "\<dots> = K * norm (z ^ n) * (inverse (norm (x ^ n)) * norm (x ^ n))"
huffman@20849:       by (simp add: x_neq_0)
huffman@23082:     also have "\<dots> = K * norm (z ^ n) * inverse (norm (x ^ n)) * norm (x ^ n)"
haftmann@57512:       by (simp only: mult.assoc)
huffman@23082:     finally show "norm (norm (f n * z ^ n)) \<le>
huffman@23082:                   K * norm (z ^ n) * inverse (norm (x ^ n))"
huffman@20849:       by (simp add: mult_le_cancel_right x_neq_0)
huffman@20849:   qed
huffman@23082:   moreover have "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x ^ n)))"
huffman@20849:   proof -
huffman@23082:     from 2 have "norm (norm (z * inverse x)) < 1"
huffman@23082:       using x_neq_0
huffman@53599:       by (simp add: norm_mult nonzero_norm_inverse divide_inverse [where 'a=real, symmetric])
huffman@23082:     hence "summable (\<lambda>n. norm (z * inverse x) ^ n)"
huffman@20849:       by (rule summable_geometric)
huffman@23082:     hence "summable (\<lambda>n. K * norm (z * inverse x) ^ n)"
huffman@20849:       by (rule summable_mult)
huffman@23082:     thus "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x ^ n)))"
huffman@23082:       using x_neq_0
huffman@23082:       by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
haftmann@57512:                     power_inverse norm_power mult.assoc)
huffman@20849:   qed
huffman@23082:   ultimately show "summable (\<lambda>n. norm (f n * z ^ n))"
huffman@20849:     by (rule summable_comparison_test)
huffman@20849: qed
paulson@15077: 
paulson@15229: lemma powser_inside:
huffman@53599:   fixes f :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}"
wenzelm@53079:   shows
wenzelm@53079:     "summable (\<lambda>n. f n * (x ^ n)) \<Longrightarrow> norm z < norm x \<Longrightarrow>
wenzelm@53079:       summable (\<lambda>n. f n * (z ^ n))"
wenzelm@53079:   by (rule powser_insidea [THEN summable_norm_cancel])
wenzelm@53079: 
wenzelm@53079: lemma sum_split_even_odd:
wenzelm@53079:   fixes f :: "nat \<Rightarrow> real"
wenzelm@53079:   shows
hoelzl@56193:     "(\<Sum>i<2 * n. if even i then f i else g i) =
hoelzl@56193:      (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1))"
hoelzl@29803: proof (induct n)
wenzelm@53079:   case 0
wenzelm@53079:   then show ?case by simp
wenzelm@53079: next
hoelzl@29803:   case (Suc n)
hoelzl@56193:   have "(\<Sum>i<2 * Suc n. if even i then f i else g i) =
hoelzl@56193:     (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
huffman@30082:     using Suc.hyps unfolding One_nat_def by auto
hoelzl@56193:   also have "\<dots> = (\<Sum>i<Suc n. f (2 * i)) + (\<Sum>i<Suc n. g (2 * i + 1))"
wenzelm@53079:     by auto
hoelzl@29803:   finally show ?case .
wenzelm@53079: qed
wenzelm@53079: 
wenzelm@53079: lemma sums_if':
wenzelm@53079:   fixes g :: "nat \<Rightarrow> real"
wenzelm@53079:   assumes "g sums x"
hoelzl@29803:   shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
hoelzl@29803:   unfolding sums_def
hoelzl@29803: proof (rule LIMSEQ_I)
wenzelm@53079:   fix r :: real
wenzelm@53079:   assume "0 < r"
hoelzl@29803:   from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
hoelzl@56193:   obtain no where no_eq: "\<And> n. n \<ge> no \<Longrightarrow> (norm (setsum g {..<n} - x) < r)" by blast
hoelzl@56193: 
hoelzl@56193:   let ?SUM = "\<lambda> m. \<Sum>i<m. if even i then 0 else g ((i - 1) div 2)"
wenzelm@53079:   {
wenzelm@53079:     fix m
wenzelm@53079:     assume "m \<ge> 2 * no"
wenzelm@53079:     hence "m div 2 \<ge> no" by auto
hoelzl@56193:     have sum_eq: "?SUM (2 * (m div 2)) = setsum g {..< m div 2}"
hoelzl@29803:       using sum_split_even_odd by auto
wenzelm@53079:     hence "(norm (?SUM (2 * (m div 2)) - x) < r)"
wenzelm@53079:       using no_eq unfolding sum_eq using `m div 2 \<ge> no` by auto
hoelzl@29803:     moreover
hoelzl@29803:     have "?SUM (2 * (m div 2)) = ?SUM m"
hoelzl@29803:     proof (cases "even m")
wenzelm@53079:       case True
wenzelm@53079:       show ?thesis
wenzelm@53079:         unfolding even_nat_div_two_times_two[OF True, unfolded numeral_2_eq_2[symmetric]] ..
hoelzl@29803:     next
wenzelm@53079:       case False
wenzelm@53079:       hence "even (Suc m)" by auto
wenzelm@53079:       from even_nat_div_two_times_two[OF this, unfolded numeral_2_eq_2[symmetric]]
wenzelm@53079:         odd_nat_plus_one_div_two[OF False, unfolded numeral_2_eq_2[symmetric]]
hoelzl@29803:       have eq: "Suc (2 * (m div 2)) = m" by auto
hoelzl@29803:       hence "even (2 * (m div 2))" using `odd m` by auto
hoelzl@29803:       have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
hoelzl@29803:       also have "\<dots> = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
hoelzl@29803:       finally show ?thesis by auto
hoelzl@29803:     qed
hoelzl@29803:     ultimately have "(norm (?SUM m - x) < r)" by auto
hoelzl@29803:   }
hoelzl@29803:   thus "\<exists> no. \<forall> m \<ge> no. norm (?SUM m - x) < r" by blast
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma sums_if:
wenzelm@53079:   fixes g :: "nat \<Rightarrow> real"
wenzelm@53079:   assumes "g sums x" and "f sums y"
hoelzl@29803:   shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
hoelzl@29803: proof -
hoelzl@29803:   let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
wenzelm@53079:   {
wenzelm@53079:     fix B T E
wenzelm@53079:     have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
wenzelm@53079:       by (cases B) auto
wenzelm@53079:   } note if_sum = this
wenzelm@53079:   have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
wenzelm@53079:     using sums_if'[OF `g sums x`] .
hoelzl@41970:   {
wenzelm@41550:     have if_eq: "\<And>B T E. (if \<not> B then T else E) = (if B then E else T)" by auto
hoelzl@29803: 
hoelzl@29803:     have "?s sums y" using sums_if'[OF `f sums y`] .
hoelzl@41970:     from this[unfolded sums_def, THEN LIMSEQ_Suc]
hoelzl@29803:     have "(\<lambda> n. if even n then f (n div 2) else 0) sums y"
haftmann@57418:       by (simp add: lessThan_Suc_eq_insert_0 image_iff setsum.reindex if_eq sums_def cong del: if_cong)
wenzelm@53079:   }
wenzelm@53079:   from sums_add[OF g_sums this] show ?thesis unfolding if_sum .
hoelzl@29803: qed
hoelzl@29803: 
hoelzl@29803: subsection {* Alternating series test / Leibniz formula *}
hoelzl@29803: 
hoelzl@29803: lemma sums_alternating_upper_lower:
hoelzl@29803:   fixes a :: "nat \<Rightarrow> real"
hoelzl@29803:   assumes mono: "\<And>n. a (Suc n) \<le> a n" and a_pos: "\<And>n. 0 \<le> a n" and "a ----> 0"
hoelzl@56193:   shows "\<exists>l. ((\<forall>n. (\<Sum>i<2*n. -1^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i<2*n. -1^i*a i) ----> l) \<and>
hoelzl@56193:              ((\<forall>n. l \<le> (\<Sum>i<2*n + 1. -1^i*a i)) \<and> (\<lambda> n. \<Sum>i<2*n + 1. -1^i*a i) ----> l)"
hoelzl@29803:   (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
wenzelm@53079: proof (rule nested_sequence_unique)
huffman@30082:   have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" unfolding One_nat_def by auto
hoelzl@29803: 
wenzelm@53079:   show "\<forall>n. ?f n \<le> ?f (Suc n)"
wenzelm@53079:   proof
wenzelm@53079:     fix n
wenzelm@53079:     show "?f n \<le> ?f (Suc n)" using mono[of "2*n"] by auto
wenzelm@53079:   qed
wenzelm@53079:   show "\<forall>n. ?g (Suc n) \<le> ?g n"
wenzelm@53079:   proof
wenzelm@53079:     fix n
wenzelm@53079:     show "?g (Suc n) \<le> ?g n" using mono[of "Suc (2*n)"]
wenzelm@53079:       unfolding One_nat_def by auto
wenzelm@53079:   qed
wenzelm@53079:   show "\<forall>n. ?f n \<le> ?g n"
wenzelm@53079:   proof
wenzelm@53079:     fix n
wenzelm@53079:     show "?f n \<le> ?g n" using fg_diff a_pos
wenzelm@53079:       unfolding One_nat_def by auto
hoelzl@29803:   qed
wenzelm@53079:   show "(\<lambda>n. ?f n - ?g n) ----> 0" unfolding fg_diff
wenzelm@53079:   proof (rule LIMSEQ_I)
wenzelm@53079:     fix r :: real
wenzelm@53079:     assume "0 < r"
wenzelm@53079:     with `a ----> 0`[THEN LIMSEQ_D] obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r"
wenzelm@53079:       by auto
wenzelm@53079:     hence "\<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
wenzelm@53079:     thus "\<exists>N. \<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
wenzelm@53079:   qed
hoelzl@41970: qed
hoelzl@29803: 
wenzelm@53079: lemma summable_Leibniz':
wenzelm@53079:   fixes a :: "nat \<Rightarrow> real"
wenzelm@53079:   assumes a_zero: "a ----> 0"
wenzelm@53079:     and a_pos: "\<And> n. 0 \<le> a n"
wenzelm@53079:     and a_monotone: "\<And> n. a (Suc n) \<le> a n"
hoelzl@29803:   shows summable: "summable (\<lambda> n. (-1)^n * a n)"
hoelzl@56193:     and "\<And>n. (\<Sum>i<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
hoelzl@56193:     and "(\<lambda>n. \<Sum>i<2*n. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
hoelzl@56193:     and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i<2*n+1. (-1)^i*a i)"
hoelzl@56193:     and "(\<lambda>n. \<Sum>i<2*n+1. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
hoelzl@29803: proof -
wenzelm@53079:   let ?S = "\<lambda>n. (-1)^n * a n"
hoelzl@56193:   let ?P = "\<lambda>n. \<Sum>i<n. ?S i"
wenzelm@53079:   let ?f = "\<lambda>n. ?P (2 * n)"
wenzelm@53079:   let ?g = "\<lambda>n. ?P (2 * n + 1)"
wenzelm@53079:   obtain l :: real
wenzelm@53079:     where below_l: "\<forall> n. ?f n \<le> l"
wenzelm@53079:       and "?f ----> l"
wenzelm@53079:       and above_l: "\<forall> n. l \<le> ?g n"
wenzelm@53079:       and "?g ----> l"
hoelzl@29803:     using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
hoelzl@41970: 
hoelzl@56193:   let ?Sa = "\<lambda>m. \<Sum>n<m. ?S n"
hoelzl@29803:   have "?Sa ----> l"
hoelzl@29803:   proof (rule LIMSEQ_I)
wenzelm@53079:     fix r :: real
wenzelm@53079:     assume "0 < r"
hoelzl@41970:     with `?f ----> l`[THEN LIMSEQ_D]
hoelzl@29803:     obtain f_no where f: "\<And> n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r" by auto
hoelzl@29803: 
hoelzl@41970:     from `0 < r` `?g ----> l`[THEN LIMSEQ_D]
hoelzl@29803:     obtain g_no where g: "\<And> n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r" by auto
hoelzl@29803: 
wenzelm@53079:     {
wenzelm@53079:       fix n :: nat
wenzelm@53079:       assume "n \<ge> (max (2 * f_no) (2 * g_no))"
wenzelm@53079:       hence "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
hoelzl@29803:       have "norm (?Sa n - l) < r"
hoelzl@29803:       proof (cases "even n")
wenzelm@53079:         case True
wenzelm@53079:         from even_nat_div_two_times_two[OF this]
wenzelm@53079:         have n_eq: "2 * (n div 2) = n"
wenzelm@53079:           unfolding numeral_2_eq_2[symmetric] by auto
wenzelm@53079:         with `n \<ge> 2 * f_no` have "n div 2 \<ge> f_no"
wenzelm@53079:           by auto
wenzelm@53079:         from f[OF this] show ?thesis
wenzelm@53079:           unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
hoelzl@29803:       next
wenzelm@53079:         case False
wenzelm@53079:         hence "even (n - 1)" by simp
wenzelm@32960:         from even_nat_div_two_times_two[OF this]
wenzelm@53079:         have n_eq: "2 * ((n - 1) div 2) = n - 1"
wenzelm@53079:           unfolding numeral_2_eq_2[symmetric] by auto
wenzelm@53079:         hence range_eq: "n - 1 + 1 = n"
wenzelm@53079:           using odd_pos[OF False] by auto
wenzelm@53079: 
wenzelm@53079:         from n_eq `n \<ge> 2 * g_no` have "(n - 1) div 2 \<ge> g_no"
wenzelm@53079:           by auto
wenzelm@53079:         from g[OF this] show ?thesis
hoelzl@56193:           unfolding n_eq range_eq .
hoelzl@29803:       qed
hoelzl@29803:     }
wenzelm@53079:     thus "\<exists>no. \<forall>n \<ge> no. norm (?Sa n - l) < r" by blast
hoelzl@29803:   qed
wenzelm@53079:   hence sums_l: "(\<lambda>i. (-1)^i * a i) sums l"
hoelzl@56193:     unfolding sums_def .
hoelzl@29803:   thus "summable ?S" using summable_def by auto
hoelzl@29803: 
hoelzl@29803:   have "l = suminf ?S" using sums_unique[OF sums_l] .
hoelzl@29803: 
wenzelm@53079:   fix n
wenzelm@53079:   show "suminf ?S \<le> ?g n"
wenzelm@53079:     unfolding sums_unique[OF sums_l, symmetric] using above_l by auto
wenzelm@53079:   show "?f n \<le> suminf ?S"
wenzelm@53079:     unfolding sums_unique[OF sums_l, symmetric] using below_l by auto
wenzelm@53079:   show "?g ----> suminf ?S"
wenzelm@53079:     using `?g ----> l` `l = suminf ?S` by auto
wenzelm@53079:   show "?f ----> suminf ?S"
wenzelm@53079:     using `?f ----> l` `l = suminf ?S` by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: theorem summable_Leibniz:
wenzelm@53079:   fixes a :: "nat \<Rightarrow> real"
hoelzl@29803:   assumes a_zero: "a ----> 0" and "monoseq a"
hoelzl@29803:   shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
wenzelm@53079:     and "0 < a 0 \<longrightarrow>
hoelzl@56193:       (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i<2*n. -1^i * a i .. \<Sum>i<2*n+1. -1^i * a i})" (is "?pos")
wenzelm@53079:     and "a 0 < 0 \<longrightarrow>
hoelzl@56193:       (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i<2*n+1. -1^i * a i .. \<Sum>i<2*n. -1^i * a i})" (is "?neg")
hoelzl@56193:     and "(\<lambda>n. \<Sum>i<2*n. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?f")
hoelzl@56193:     and "(\<lambda>n. \<Sum>i<2*n+1. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?g")
hoelzl@29803: proof -
hoelzl@29803:   have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
hoelzl@29803:   proof (cases "(\<forall> n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
hoelzl@29803:     case True
wenzelm@53079:     hence ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m" and ge0: "\<And> n. 0 \<le> a n"
wenzelm@53079:       by auto
wenzelm@53079:     {
wenzelm@53079:       fix n
wenzelm@53079:       have "a (Suc n) \<le> a n"
wenzelm@53079:         using ord[where n="Suc n" and m=n] by auto
wenzelm@53079:     } note mono = this
wenzelm@53079:     note leibniz = summable_Leibniz'[OF `a ----> 0` ge0]
hoelzl@29803:     from leibniz[OF mono]
hoelzl@29803:     show ?thesis using `0 \<le> a 0` by auto
hoelzl@29803:   next
hoelzl@29803:     let ?a = "\<lambda> n. - a n"
hoelzl@29803:     case False
hoelzl@29803:     with monoseq_le[OF `monoseq a` `a ----> 0`]
hoelzl@29803:     have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
wenzelm@53079:     hence ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n"
wenzelm@53079:       by auto
wenzelm@53079:     {
wenzelm@53079:       fix n
wenzelm@53079:       have "?a (Suc n) \<le> ?a n" using ord[where n="Suc n" and m=n]
wenzelm@53079:         by auto
wenzelm@53079:     } note monotone = this
wenzelm@53079:     note leibniz =
wenzelm@53079:       summable_Leibniz'[OF _ ge0, of "\<lambda>x. x",
wenzelm@53079:         OF tendsto_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
wenzelm@53079:     have "summable (\<lambda> n. (-1)^n * ?a n)"
wenzelm@53079:       using leibniz(1) by auto
wenzelm@53079:     then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l"
wenzelm@53079:       unfolding summable_def by auto
wenzelm@53079:     from this[THEN sums_minus] have "(\<lambda> n. (-1)^n * a n) sums -l"
wenzelm@53079:       by auto
hoelzl@29803:     hence ?summable unfolding summable_def by auto
hoelzl@29803:     moreover
wenzelm@53079:     have "\<And>a b :: real. \<bar>- a - - b\<bar> = \<bar>a - b\<bar>"
wenzelm@53079:       unfolding minus_diff_minus by auto
hoelzl@41970: 
hoelzl@29803:     from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
wenzelm@53079:     have move_minus: "(\<Sum>n. - (-1 ^ n * a n)) = - (\<Sum>n. -1 ^ n * a n)"
wenzelm@53079:       by auto
hoelzl@29803: 
hoelzl@29803:     have ?pos using `0 \<le> ?a 0` by auto
wenzelm@53079:     moreover have ?neg
wenzelm@53079:       using leibniz(2,4)
wenzelm@53079:       unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le
wenzelm@53079:       by auto
wenzelm@53079:     moreover have ?f and ?g
wenzelm@53079:       using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN tendsto_minus_cancel]
wenzelm@53079:       by auto
hoelzl@29803:     ultimately show ?thesis by auto
hoelzl@29803:   qed
paulson@54576:   then show ?summable and ?pos and ?neg and ?f and ?g 
paulson@54573:     by safe
hoelzl@29803: qed
paulson@15077: 
huffman@29164: subsection {* Term-by-Term Differentiability of Power Series *}
huffman@23043: 
hoelzl@56193: definition diffs :: "(nat \<Rightarrow> 'a::ring_1) \<Rightarrow> nat \<Rightarrow> 'a"
hoelzl@56193:   where "diffs c = (\<lambda>n. of_nat (Suc n) * c (Suc n))"
paulson@15077: 
paulson@15077: text{*Lemma about distributing negation over it*}
wenzelm@53079: lemma diffs_minus: "diffs (\<lambda>n. - c n) = (\<lambda>n. - diffs c n)"
wenzelm@53079:   by (simp add: diffs_def)
paulson@15077: 
huffman@29163: lemma sums_Suc_imp:
hoelzl@56193:   "(f::nat \<Rightarrow> 'a::real_normed_vector) 0 = 0 \<Longrightarrow> (\<lambda>n. f (Suc n)) sums s \<Longrightarrow> (\<lambda>n. f n) sums s"
hoelzl@56193:   using sums_Suc_iff[of f] by simp
paulson@15077: 
paulson@15229: lemma diffs_equiv:
hoelzl@41970:   fixes x :: "'a::{real_normed_vector, ring_1}"
hoelzl@56193:   shows "summable (\<lambda>n. diffs c n * x^n) \<Longrightarrow>
hoelzl@56193:       (\<lambda>n. of_nat n * c n * x^(n - Suc 0)) sums (\<Sum>n. diffs c n * x^n)"
wenzelm@53079:   unfolding diffs_def
paulson@54573:   by (simp add: summable_sums sums_Suc_imp)
paulson@15077: 
paulson@15077: lemma lemma_termdiff1:
haftmann@31017:   fixes z :: "'a :: {monoid_mult,comm_ring}" shows
hoelzl@56193:   "(\<Sum>p<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =
hoelzl@56193:    (\<Sum>p<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
wenzelm@53079:   by (auto simp add: algebra_simps power_add [symmetric])
paulson@15077: 
huffman@23082: lemma sumr_diff_mult_const2:
hoelzl@56193:   "setsum f {..<n} - of_nat n * (r::'a::ring_1) = (\<Sum>i<n. f i - r)"
wenzelm@53079:   by (simp add: setsum_subtractf)
huffman@23082: 
paulson@15229: lemma lemma_termdiff2:
haftmann@31017:   fixes h :: "'a :: {field}"
wenzelm@53079:   assumes h: "h \<noteq> 0"
wenzelm@53079:   shows
wenzelm@53079:     "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
hoelzl@56193:      h * (\<Sum>p< n - Suc 0. \<Sum>q< n - Suc 0 - p.
wenzelm@53079:           (z + h) ^ q * z ^ (n - 2 - q))" (is "?lhs = ?rhs")
wenzelm@53079:   apply (subgoal_tac "h * ?lhs = h * ?rhs", simp add: h)
wenzelm@53079:   apply (simp add: right_diff_distrib diff_divide_distrib h)
haftmann@57512:   apply (simp add: mult.assoc [symmetric])
wenzelm@53079:   apply (cases "n", simp)
wenzelm@53079:   apply (simp add: lemma_realpow_diff_sumr2 h
haftmann@57512:                    right_diff_distrib [symmetric] mult.assoc
hoelzl@56193:               del: power_Suc setsum_lessThan_Suc of_nat_Suc)
wenzelm@53079:   apply (subst lemma_realpow_rev_sumr)
wenzelm@53079:   apply (subst sumr_diff_mult_const2)
wenzelm@53079:   apply simp
wenzelm@53079:   apply (simp only: lemma_termdiff1 setsum_right_distrib)
haftmann@57418:   apply (rule setsum.cong [OF refl])
haftmann@54230:   apply (simp add: less_iff_Suc_add)
wenzelm@53079:   apply (clarify)
haftmann@57514:   apply (simp add: setsum_right_distrib lemma_realpow_diff_sumr2 ac_simps
hoelzl@56193:               del: setsum_lessThan_Suc power_Suc)
haftmann@57512:   apply (subst mult.assoc [symmetric], subst power_add [symmetric])
haftmann@57514:   apply (simp add: ac_simps)
wenzelm@53079:   done
huffman@20860: 
huffman@20860: lemma real_setsum_nat_ivl_bounded2:
haftmann@35028:   fixes K :: "'a::linordered_semidom"
huffman@23082:   assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K"
wenzelm@53079:     and K: "0 \<le> K"
hoelzl@56193:   shows "setsum f {..<n-k} \<le> of_nat n * K"
wenzelm@53079:   apply (rule order_trans [OF setsum_mono])
wenzelm@53079:   apply (rule f, simp)
wenzelm@53079:   apply (simp add: mult_right_mono K)
wenzelm@53079:   done
paulson@15077: 
paulson@15229: lemma lemma_termdiff3:
haftmann@31017:   fixes h z :: "'a::{real_normed_field}"
huffman@20860:   assumes 1: "h \<noteq> 0"
wenzelm@53079:     and 2: "norm z \<le> K"
wenzelm@53079:     and 3: "norm (z + h) \<le> K"
huffman@23082:   shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0))
huffman@23082:           \<le> of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
huffman@20860: proof -
huffman@23082:   have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
hoelzl@56193:         norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p.
huffman@23082:           (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
haftmann@57512:     by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
huffman@23082:   also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
huffman@23082:   proof (rule mult_right_mono [OF _ norm_ge_zero])
wenzelm@53079:     from norm_ge_zero 2 have K: "0 \<le> K"
wenzelm@53079:       by (rule order_trans)
huffman@23082:     have le_Kn: "\<And>i j n. i + j = n \<Longrightarrow> norm ((z + h) ^ i * z ^ j) \<le> K ^ n"
huffman@20860:       apply (erule subst)
huffman@23082:       apply (simp only: norm_mult norm_power power_add)
huffman@23082:       apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
huffman@20860:       done
hoelzl@56193:     show "norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))
huffman@23082:           \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
huffman@20860:       apply (intro
huffman@23082:          order_trans [OF norm_setsum]
huffman@20860:          real_setsum_nat_ivl_bounded2
huffman@20860:          mult_nonneg_nonneg
huffman@47489:          of_nat_0_le_iff
huffman@20860:          zero_le_power K)
huffman@20860:       apply (rule le_Kn, simp)
huffman@20860:       done
huffman@20860:   qed
huffman@23082:   also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
haftmann@57512:     by (simp only: mult.assoc)
huffman@20860:   finally show ?thesis .
huffman@20860: qed
paulson@15077: 
huffman@20860: lemma lemma_termdiff4:
huffman@56167:   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
huffman@20860:   assumes k: "0 < (k::real)"
wenzelm@53079:     and le: "\<And>h. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (f h) \<le> K * norm h"
huffman@20860:   shows "f -- 0 --> 0"
huffman@56167: proof (rule tendsto_norm_zero_cancel)
huffman@56167:   show "(\<lambda>h. norm (f h)) -- 0 --> 0"
huffman@56167:   proof (rule real_tendsto_sandwich)
huffman@56167:     show "eventually (\<lambda>h. 0 \<le> norm (f h)) (at 0)"
huffman@20860:       by simp
huffman@56167:     show "eventually (\<lambda>h. norm (f h) \<le> K * norm h) (at 0)"
huffman@56167:       using k by (auto simp add: eventually_at dist_norm le)
huffman@56167:     show "(\<lambda>h. 0) -- (0::'a) --> (0::real)"
huffman@56167:       by (rule tendsto_const)
huffman@56167:     have "(\<lambda>h. K * norm h) -- (0::'a) --> K * norm (0::'a)"
huffman@56167:       by (intro tendsto_intros)
huffman@56167:     then show "(\<lambda>h. K * norm h) -- (0::'a) --> 0"
huffman@56167:       by simp
huffman@20860:   qed
huffman@20860: qed
paulson@15077: 
paulson@15229: lemma lemma_termdiff5:
huffman@56167:   fixes g :: "'a::real_normed_vector \<Rightarrow> nat \<Rightarrow> 'b::banach"
huffman@20860:   assumes k: "0 < (k::real)"
huffman@20860:   assumes f: "summable f"
huffman@23082:   assumes le: "\<And>h n. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (g h n) \<le> f n * norm h"
huffman@20860:   shows "(\<lambda>h. suminf (g h)) -- 0 --> 0"
huffman@20860: proof (rule lemma_termdiff4 [OF k])
wenzelm@53079:   fix h::'a
wenzelm@53079:   assume "h \<noteq> 0" and "norm h < k"
huffman@23082:   hence A: "\<forall>n. norm (g h n) \<le> f n * norm h"
huffman@20860:     by (simp add: le)
huffman@23082:   hence "\<exists>N. \<forall>n\<ge>N. norm (norm (g h n)) \<le> f n * norm h"
huffman@20860:     by simp
huffman@23082:   moreover from f have B: "summable (\<lambda>n. f n * norm h)"
huffman@20860:     by (rule summable_mult2)
huffman@23082:   ultimately have C: "summable (\<lambda>n. norm (g h n))"
huffman@20860:     by (rule summable_comparison_test)
huffman@23082:   hence "norm (suminf (g h)) \<le> (\<Sum>n. norm (g h n))"
huffman@23082:     by (rule summable_norm)
huffman@23082:   also from A C B have "(\<Sum>n. norm (g h n)) \<le> (\<Sum>n. f n * norm h)"
hoelzl@56213:     by (rule suminf_le)
huffman@23082:   also from f have "(\<Sum>n. f n * norm h) = suminf f * norm h"
huffman@20860:     by (rule suminf_mult2 [symmetric])
huffman@23082:   finally show "norm (suminf (g h)) \<le> suminf f * norm h" .
huffman@20860: qed
paulson@15077: 
paulson@15077: 
paulson@15077: text{* FIXME: Long proofs*}
paulson@15077: 
paulson@15077: lemma termdiffs_aux:
haftmann@31017:   fixes x :: "'a::{real_normed_field,banach}"
huffman@20849:   assumes 1: "summable (\<lambda>n. diffs (diffs c) n * K ^ n)"
wenzelm@53079:     and 2: "norm x < norm K"
huffman@20860:   shows "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x ^ n) / h
huffman@23082:              - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
huffman@20849: proof -
huffman@20860:   from dense [OF 2]
huffman@23082:   obtain r where r1: "norm x < r" and r2: "r < norm K" by fast
huffman@23082:   from norm_ge_zero r1 have r: "0 < r"
huffman@20860:     by (rule order_le_less_trans)
huffman@20860:   hence r_neq_0: "r \<noteq> 0" by simp
huffman@20860:   show ?thesis
huffman@20849:   proof (rule lemma_termdiff5)
huffman@23082:     show "0 < r - norm x" using r1 by simp
huffman@23082:     from r r2 have "norm (of_real r::'a) < norm K"
huffman@23082:       by simp
huffman@23082:     with 1 have "summable (\<lambda>n. norm (diffs (diffs c) n * (of_real r ^ n)))"
huffman@20860:       by (rule powser_insidea)
huffman@23082:     hence "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
huffman@23082:       using r
huffman@23082:       by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
huffman@23082:     hence "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
huffman@20860:       by (rule diffs_equiv [THEN sums_summable])
wenzelm@53079:     also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
wenzelm@53079:       (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
huffman@20849:       apply (rule ext)
huffman@20849:       apply (simp add: diffs_def)
huffman@20849:       apply (case_tac n, simp_all add: r_neq_0)
huffman@20849:       done
hoelzl@41970:     finally have "summable
huffman@23082:       (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
huffman@20860:       by (rule diffs_equiv [THEN sums_summable])
huffman@20860:     also have
huffman@23082:       "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) *
huffman@20860:            r ^ (n - Suc 0)) =
huffman@23082:        (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
huffman@20849:       apply (rule ext)
huffman@20849:       apply (case_tac "n", simp)
blanchet@55417:       apply (rename_tac nat)
huffman@20849:       apply (case_tac "nat", simp)
huffman@20849:       apply (simp add: r_neq_0)
huffman@20849:       done
wenzelm@53079:     finally
wenzelm@53079:     show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
huffman@20849:   next
huffman@23082:     fix h::'a and n::nat
huffman@20860:     assume h: "h \<noteq> 0"
huffman@23082:     assume "norm h < r - norm x"
huffman@23082:     hence "norm x + norm h < r" by simp
huffman@23082:     with norm_triangle_ineq have xh: "norm (x + h) < r"
huffman@20860:       by (rule order_le_less_trans)
huffman@23082:     show "norm (c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))
huffman@23082:           \<le> norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
haftmann@57512:       apply (simp only: norm_mult mult.assoc)
huffman@23082:       apply (rule mult_left_mono [OF _ norm_ge_zero])
haftmann@57512:       apply (simp add: mult.assoc [symmetric])
paulson@54575:       apply (metis h lemma_termdiff3 less_eq_real_def r1 xh)
huffman@20860:       done
huffman@20849:   qed
huffman@20849: qed
webertj@20217: 
huffman@20860: lemma termdiffs:
haftmann@31017:   fixes K x :: "'a::{real_normed_field,banach}"
huffman@20860:   assumes 1: "summable (\<lambda>n. c n * K ^ n)"
paulson@54575:       and 2: "summable (\<lambda>n. (diffs c) n * K ^ n)"
paulson@54575:       and 3: "summable (\<lambda>n. (diffs (diffs c)) n * K ^ n)"
paulson@54575:       and 4: "norm x < norm K"
huffman@20860:   shows "DERIV (\<lambda>x. \<Sum>n. c n * x ^ n) x :> (\<Sum>n. (diffs c) n * x ^ n)"
hoelzl@56381:   unfolding DERIV_def
huffman@29163: proof (rule LIM_zero_cancel)
huffman@20860:   show "(\<lambda>h. (suminf (\<lambda>n. c n * (x + h) ^ n) - suminf (\<lambda>n. c n * x ^ n)) / h
huffman@20860:             - suminf (\<lambda>n. diffs c n * x ^ n)) -- 0 --> 0"
huffman@20860:   proof (rule LIM_equal2)
huffman@29163:     show "0 < norm K - norm x" using 4 by (simp add: less_diff_eq)
huffman@20860:   next
huffman@23082:     fix h :: 'a
huffman@23082:     assume "norm (h - 0) < norm K - norm x"
huffman@23082:     hence "norm x + norm h < norm K" by simp
huffman@23082:     hence 5: "norm (x + h) < norm K"
huffman@23082:       by (rule norm_triangle_ineq [THEN order_le_less_trans])
huffman@56167:     have "summable (\<lambda>n. c n * x ^ n)"
huffman@56167:       and "summable (\<lambda>n. c n * (x + h) ^ n)"
huffman@56167:       and "summable (\<lambda>n. diffs c n * x ^ n)"
huffman@56167:       using 1 2 4 5 by (auto elim: powser_inside)
huffman@56167:     then have "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x ^ n)) / h - (\<Sum>n. diffs c n * x ^ n) =
huffman@56167:           (\<Sum>n. (c n * (x + h) ^ n - c n * x ^ n) / h - of_nat n * c n * x ^ (n - Suc 0))"
huffman@56167:       by (intro sums_unique sums_diff sums_divide diffs_equiv summable_sums)
huffman@56167:     then show "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x ^ n)) / h - (\<Sum>n. diffs c n * x ^ n) =
huffman@56167:           (\<Sum>n. c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))"
paulson@54575:       by (simp add: algebra_simps)
huffman@20860:   next
wenzelm@53079:     show "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
wenzelm@53079:       by (rule termdiffs_aux [OF 3 4])
huffman@20860:   qed
huffman@20860: qed
huffman@20860: 
paulson@15077: 
hoelzl@29803: subsection {* Derivability of power series *}
hoelzl@29803: 
wenzelm@53079: lemma DERIV_series':
wenzelm@53079:   fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
hoelzl@29803:   assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
wenzelm@53079:     and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)" and x0_in_I: "x0 \<in> {a <..< b}"
wenzelm@53079:     and "summable (f' x0)"
wenzelm@53079:     and "summable L"
wenzelm@53079:     and L_def: "\<And>n x y. \<lbrakk> x \<in> { a <..< b} ; y \<in> { a <..< b} \<rbrakk> \<Longrightarrow> \<bar>f x n - f y n\<bar> \<le> L n * \<bar>x - y\<bar>"
hoelzl@29803:   shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
hoelzl@56381:   unfolding DERIV_def
hoelzl@29803: proof (rule LIM_I)
wenzelm@53079:   fix r :: real
wenzelm@53079:   assume "0 < r" hence "0 < r/3" by auto
hoelzl@29803: 
hoelzl@41970:   obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3"
hoelzl@29803:     using suminf_exist_split[OF `0 < r/3` `summable L`] by auto
hoelzl@29803: 
hoelzl@41970:   obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3"
hoelzl@29803:     using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto
hoelzl@29803: 
hoelzl@29803:   let ?N = "Suc (max N_L N_f')"
hoelzl@29803:   have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3") and
hoelzl@29803:     L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto
hoelzl@29803: 
wenzelm@53079:   let ?diff = "\<lambda>i x. (f (x0 + x) i - f x0 i) / x"
hoelzl@29803: 
hoelzl@29803:   let ?r = "r / (3 * real ?N)"
nipkow@56541:   from `0 < r` have "0 < ?r" by simp
hoelzl@29803: 
hoelzl@56193:   let ?s = "\<lambda>n. SOME s. 0 < s \<and> (\<forall> x. x \<noteq> 0 \<and> \<bar> x \<bar> < s \<longrightarrow> \<bar> ?diff n x - f' x0 n \<bar> < ?r)"
hoelzl@56193:   def S' \<equiv> "Min (?s ` {..< ?N })"
hoelzl@29803: 
hoelzl@29803:   have "0 < S'" unfolding S'_def
hoelzl@29803:   proof (rule iffD2[OF Min_gr_iff])
hoelzl@56193:     show "\<forall>x \<in> (?s ` {..< ?N }). 0 < x"
wenzelm@53079:     proof
wenzelm@53079:       fix x
hoelzl@56193:       assume "x \<in> ?s ` {..<?N}"
hoelzl@56193:       then obtain n where "x = ?s n" and "n \<in> {..<?N}"
wenzelm@53079:         using image_iff[THEN iffD1] by blast
hoelzl@41970:       from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def]
wenzelm@53079:       obtain s where s_bound: "0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < s \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r)"
wenzelm@53079:         by auto
wenzelm@53079:       have "0 < ?s n" by (rule someI2[where a=s]) (auto simp add: s_bound)
hoelzl@29803:       thus "0 < x" unfolding `x = ?s n` .
hoelzl@29803:     qed
hoelzl@29803:   qed auto
hoelzl@29803: 
hoelzl@29803:   def S \<equiv> "min (min (x0 - a) (b - x0)) S'"
wenzelm@53079:   hence "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0"
wenzelm@53079:     and "S \<le> S'" using x0_in_I and `0 < S'`
hoelzl@29803:     by auto
hoelzl@29803: 
wenzelm@53079:   {
wenzelm@53079:     fix x
wenzelm@53079:     assume "x \<noteq> 0" and "\<bar> x \<bar> < S"
wenzelm@53079:     hence x_in_I: "x0 + x \<in> { a <..< b }"
wenzelm@53079:       using S_a S_b by auto
hoelzl@41970: 
hoelzl@29803:     note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
hoelzl@29803:     note div_smbl = summable_divide[OF diff_smbl]
hoelzl@29803:     note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
hoelzl@29803:     note ign = summable_ignore_initial_segment[where k="?N"]
hoelzl@29803:     note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
hoelzl@29803:     note div_shft_smbl = summable_divide[OF diff_shft_smbl]
hoelzl@29803:     note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]
hoelzl@29803: 
hoelzl@56193:     { fix n
hoelzl@41970:       have "\<bar> ?diff (n + ?N) x \<bar> \<le> L (n + ?N) * \<bar> (x0 + x) - x0 \<bar> / \<bar> x \<bar>"
wenzelm@53079:         using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero]
wenzelm@53079:         unfolding abs_divide .
wenzelm@53079:       hence "\<bar> (\<bar>?diff (n + ?N) x \<bar>) \<bar> \<le> L (n + ?N)"
hoelzl@56193:         using `x \<noteq> 0` by auto }
hoelzl@56193:     note 1 = this and 2 = summable_rabs_comparison_test[OF _ ign[OF `summable L`]]
hoelzl@56193:     then have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))"
hoelzl@56213:       by (metis (lifting) abs_idempotent order_trans[OF summable_rabs[OF 2] suminf_le[OF _ 2 ign[OF `summable L`]]])
hoelzl@56193:     then have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> r / 3" (is "?L_part \<le> r/3")
wenzelm@53079:       using L_estimate by auto
wenzelm@53079: 
hoelzl@56193:     have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n \<bar> \<le> (\<Sum>n<?N. \<bar>?diff n x - f' x0 n \<bar>)" ..
hoelzl@56193:     also have "\<dots> < (\<Sum>n<?N. ?r)"
hoelzl@29803:     proof (rule setsum_strict_mono)
wenzelm@53079:       fix n
hoelzl@56193:       assume "n \<in> {..< ?N}"
wenzelm@53079:       have "\<bar>x\<bar> < S" using `\<bar>x\<bar> < S` .
hoelzl@29803:       also have "S \<le> S'" using `S \<le> S'` .
hoelzl@41970:       also have "S' \<le> ?s n" unfolding S'_def
hoelzl@29803:       proof (rule Min_le_iff[THEN iffD2])
hoelzl@56193:         have "?s n \<in> (?s ` {..<?N}) \<and> ?s n \<le> ?s n"
hoelzl@56193:           using `n \<in> {..< ?N}` by auto
hoelzl@56193:         thus "\<exists> a \<in> (?s ` {..<?N}). a \<le> ?s n" by blast
hoelzl@29803:       qed auto
wenzelm@53079:       finally have "\<bar>x\<bar> < ?s n" .
hoelzl@29803: 
hoelzl@29803:       from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
hoelzl@29803:       have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
wenzelm@53079:       with `x \<noteq> 0` and `\<bar>x\<bar> < ?s n` show "\<bar>?diff n x - f' x0 n\<bar> < ?r"
wenzelm@53079:         by blast
hoelzl@29803:     qed auto
hoelzl@56193:     also have "\<dots> = of_nat (card {..<?N}) * ?r"
wenzelm@53079:       by (rule setsum_constant)
wenzelm@53079:     also have "\<dots> = real ?N * ?r"
wenzelm@53079:       unfolding real_eq_of_nat by auto
hoelzl@29803:     also have "\<dots> = r/3" by auto
hoelzl@56193:     finally have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
hoelzl@29803: 
hoelzl@29803:     from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
wenzelm@53079:     have "\<bar>(suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0)\<bar> =
wenzelm@53079:         \<bar>\<Sum>n. ?diff n x - f' x0 n\<bar>"
wenzelm@53079:       unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric]
wenzelm@53079:       using suminf_divide[OF diff_smbl, symmetric] by auto
wenzelm@53079:     also have "\<dots> \<le> ?diff_part + \<bar> (\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N)) \<bar>"
wenzelm@53079:       unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"]
wenzelm@53079:       unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]]
haftmann@57512:       apply (subst (5) add.commute)
wenzelm@53079:       by (rule abs_triangle_ineq)
wenzelm@53079:     also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part"
wenzelm@53079:       using abs_triangle_ineq4 by auto
hoelzl@41970:     also have "\<dots> < r /3 + r/3 + r/3"
huffman@36842:       using `?diff_part < r/3` `?L_part \<le> r/3` and `?f'_part < r/3`
huffman@36842:       by (rule add_strict_mono [OF add_less_le_mono])
wenzelm@53079:     finally have "\<bar>(suminf (f (x0 + x)) - suminf (f x0)) / x - suminf (f' x0)\<bar> < r"
hoelzl@29803:       by auto
wenzelm@53079:   }
wenzelm@53079:   thus "\<exists> s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow>
wenzelm@53079:       norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r"
wenzelm@53079:     using `0 < S` unfolding real_norm_def diff_0_right by blast
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma DERIV_power_series':
wenzelm@53079:   fixes f :: "nat \<Rightarrow> real"
hoelzl@29803:   assumes converges: "\<And> x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda> n. f n * real (Suc n) * x^n)"
wenzelm@53079:     and x0_in_I: "x0 \<in> {-R <..< R}" and "0 < R"
hoelzl@29803:   shows "DERIV (\<lambda> x. (\<Sum> n. f n * x^(Suc n))) x0 :> (\<Sum> n. f n * real (Suc n) * x0^n)"
hoelzl@29803:   (is "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
hoelzl@29803: proof -
wenzelm@53079:   {
wenzelm@53079:     fix R'
wenzelm@53079:     assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
wenzelm@53079:     hence "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}"
wenzelm@53079:       by auto
hoelzl@29803:     have "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
hoelzl@29803:     proof (rule DERIV_series')
hoelzl@29803:       show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
hoelzl@29803:       proof -
wenzelm@53079:         have "(R' + R) / 2 < R" and "0 < (R' + R) / 2"
wenzelm@53079:           using `0 < R'` `0 < R` `R' < R` by auto
wenzelm@53079:         hence in_Rball: "(R' + R) / 2 \<in> {-R <..< R}"
wenzelm@53079:           using `R' < R` by auto
wenzelm@53079:         have "norm R' < norm ((R' + R) / 2)"
wenzelm@53079:           using `0 < R'` `0 < R` `R' < R` by auto
wenzelm@53079:         from powser_insidea[OF converges[OF in_Rball] this] show ?thesis
wenzelm@53079:           by auto
hoelzl@29803:       qed
wenzelm@53079:       {
wenzelm@53079:         fix n x y
wenzelm@53079:         assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
wenzelm@32960:         show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
wenzelm@32960:         proof -
wenzelm@53079:           have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> =
hoelzl@56193:             (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar>"
wenzelm@53079:             unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult
wenzelm@53079:             by auto
hoelzl@41970:           also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)"
wenzelm@32960:           proof (rule mult_left_mono)
hoelzl@56193:             have "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)"
wenzelm@53079:               by (rule setsum_abs)
hoelzl@56193:             also have "\<dots> \<le> (\<Sum>p<Suc n. R' ^ n)"
wenzelm@32960:             proof (rule setsum_mono)
wenzelm@53079:               fix p
hoelzl@56193:               assume "p \<in> {..<Suc n}"
wenzelm@53079:               hence "p \<le> n" by auto
wenzelm@53079:               {
wenzelm@53079:                 fix n
wenzelm@53079:                 fix x :: real
wenzelm@53079:                 assume "x \<in> {-R'<..<R'}"
wenzelm@32960:                 hence "\<bar>x\<bar> \<le> R'"  by auto
wenzelm@53079:                 hence "\<bar>x^n\<bar> \<le> R'^n"
wenzelm@53079:                   unfolding power_abs by (rule power_mono, auto)
wenzelm@53079:               }
wenzelm@53079:               from mult_mono[OF this[OF `x \<in> {-R'<..<R'}`, of p] this[OF `y \<in> {-R'<..<R'}`, of "n-p"]] `0 < R'`
wenzelm@53079:               have "\<bar>x^p * y^(n-p)\<bar> \<le> R'^p * R'^(n-p)"
wenzelm@53079:                 unfolding abs_mult by auto
wenzelm@53079:               thus "\<bar>x^p * y^(n-p)\<bar> \<le> R'^n"
wenzelm@53079:                 unfolding power_add[symmetric] using `p \<le> n` by auto
wenzelm@32960:             qed
wenzelm@53079:             also have "\<dots> = real (Suc n) * R' ^ n"
wenzelm@53079:               unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
hoelzl@56193:             finally show "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>"
wenzelm@53079:               unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
wenzelm@53079:             show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>"
wenzelm@53079:               unfolding abs_mult[symmetric] by auto
wenzelm@32960:           qed
wenzelm@53079:           also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>"
haftmann@57512:             unfolding abs_mult mult.assoc[symmetric] by algebra
wenzelm@32960:           finally show ?thesis .
wenzelm@53079:         qed
wenzelm@53079:       }
wenzelm@53079:       {
wenzelm@53079:         fix n
wenzelm@53079:         show "DERIV (\<lambda> x. ?f x n) x0 :> (?f' x0 n)"
hoelzl@56381:           by (auto intro!: derivative_eq_intros simp del: power_Suc simp: real_of_nat_def)
wenzelm@53079:       }
wenzelm@53079:       {
wenzelm@53079:         fix x
wenzelm@53079:         assume "x \<in> {-R' <..< R'}"
wenzelm@53079:         hence "R' \<in> {-R <..< R}" and "norm x < norm R'"
wenzelm@53079:           using assms `R' < R` by auto
wenzelm@32960:         have "summable (\<lambda> n. f n * x^n)"
hoelzl@56193:         proof (rule summable_comparison_test, intro exI allI impI)
wenzelm@32960:           fix n
wenzelm@53079:           have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)"
wenzelm@53079:             by (rule mult_left_mono) auto
hoelzl@56193:           show "norm (f n * x ^ n) \<le> norm (f n * real (Suc n) * x ^ n)"
wenzelm@53079:             unfolding real_norm_def abs_mult
wenzelm@53079:             by (rule mult_right_mono) (auto simp add: le[unfolded mult_1_right])
hoelzl@56193:         qed (rule powser_insidea[OF converges[OF `R' \<in> {-R <..< R}`] `norm x < norm R'`])
haftmann@57512:         from this[THEN summable_mult2[where c=x], unfolded mult.assoc, unfolded mult.commute]
wenzelm@53079:         show "summable (?f x)" by auto
wenzelm@53079:       }
wenzelm@53079:       show "summable (?f' x0)"
wenzelm@53079:         using converges[OF `x0 \<in> {-R <..< R}`] .
wenzelm@53079:       show "x0 \<in> {-R' <..< R'}"
wenzelm@53079:         using `x0 \<in> {-R' <..< R'}` .
hoelzl@29803:     qed
hoelzl@29803:   } note for_subinterval = this
hoelzl@29803:   let ?R = "(R + \<bar>x0\<bar>) / 2"
hoelzl@29803:   have "\<bar>x0\<bar> < ?R" using assms by auto
hoelzl@29803:   hence "- ?R < x0"
hoelzl@29803:   proof (cases "x0 < 0")
hoelzl@29803:     case True
hoelzl@29803:     hence "- x0 < ?R" using `\<bar>x0\<bar> < ?R` by auto
hoelzl@29803:     thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
hoelzl@29803:   next
hoelzl@29803:     case False
hoelzl@29803:     have "- ?R < 0" using assms by auto
hoelzl@41970:     also have "\<dots> \<le> x0" using False by auto
hoelzl@29803:     finally show ?thesis .
hoelzl@29803:   qed
wenzelm@53079:   hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R"
wenzelm@53079:     using assms by auto
hoelzl@29803:   from for_subinterval[OF this]
hoelzl@29803:   show ?thesis .
hoelzl@29803: qed
chaieb@29695: 
wenzelm@53079: 
huffman@29164: subsection {* Exponential Function *}
huffman@23043: 
wenzelm@53079: definition exp :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
wenzelm@53079:   where "exp = (\<lambda>x. \<Sum>n. x ^ n /\<^sub>R real (fact n))"
huffman@23043: 
huffman@23115: lemma summable_exp_generic:
haftmann@31017:   fixes x :: "'a::{real_normed_algebra_1,banach}"
haftmann@25062:   defines S_def: "S \<equiv> \<lambda>n. x ^ n /\<^sub>R real (fact n)"
huffman@23115:   shows "summable S"
huffman@23115: proof -
haftmann@25062:   have S_Suc: "\<And>n. S (Suc n) = (x * S n) /\<^sub>R real (Suc n)"
huffman@30273:     unfolding S_def by (simp del: mult_Suc)
huffman@23115:   obtain r :: real where r0: "0 < r" and r1: "r < 1"
huffman@23115:     using dense [OF zero_less_one] by fast
huffman@23115:   obtain N :: nat where N: "norm x < real N * r"
huffman@23115:     using reals_Archimedean3 [OF r0] by fast
huffman@23115:   from r1 show ?thesis
hoelzl@56193:   proof (rule summable_ratio_test [rule_format])
huffman@23115:     fix n :: nat
huffman@23115:     assume n: "N \<le> n"
huffman@23115:     have "norm x \<le> real N * r"
huffman@23115:       using N by (rule order_less_imp_le)
huffman@23115:     also have "real N * r \<le> real (Suc n) * r"
huffman@23115:       using r0 n by (simp add: mult_right_mono)
huffman@23115:     finally have "norm x * norm (S n) \<le> real (Suc n) * r * norm (S n)"
huffman@23115:       using norm_ge_zero by (rule mult_right_mono)
huffman@23115:     hence "norm (x * S n) \<le> real (Suc n) * r * norm (S n)"
huffman@23115:       by (rule order_trans [OF norm_mult_ineq])
huffman@23115:     hence "norm (x * S n) / real (Suc n) \<le> r * norm (S n)"
haftmann@57514:       by (simp add: pos_divide_le_eq ac_simps)
huffman@23115:     thus "norm (S (Suc n)) \<le> r * norm (S n)"
huffman@35216:       by (simp add: S_Suc inverse_eq_divide)
huffman@23115:   qed
huffman@23115: qed
huffman@23115: 
huffman@23115: lemma summable_norm_exp:
haftmann@31017:   fixes x :: "'a::{real_normed_algebra_1,banach}"
haftmann@25062:   shows "summable (\<lambda>n. norm (x ^ n /\<^sub>R real (fact n)))"
huffman@23115: proof (rule summable_norm_comparison_test [OF exI, rule_format])
haftmann@25062:   show "summable (\<lambda>n. norm x ^ n /\<^sub>R real (fact n))"
huffman@23115:     by (rule summable_exp_generic)
wenzelm@53079:   fix n
wenzelm@53079:   show "norm (x ^ n /\<^sub>R real (fact n)) \<le> norm x ^ n /\<^sub>R real (fact n)"
huffman@35216:     by (simp add: norm_power_ineq)
huffman@23115: qed
huffman@23115: 
wenzelm@53079: lemma summable_exp: "summable (\<lambda>n. inverse (real (fact n)) * x ^ n)"
wenzelm@53079:   using summable_exp_generic [where x=x] by simp
huffman@23043: 
haftmann@25062: lemma exp_converges: "(\<lambda>n. x ^ n /\<^sub>R real (fact n)) sums exp x"
wenzelm@53079:   unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])
huffman@23043: 
huffman@23043: 
hoelzl@41970: lemma exp_fdiffs:
wenzelm@53079:       "diffs (\<lambda>n. inverse(real (fact n))) = (\<lambda>n. inverse(real (fact n)))"
haftmann@57512:   by (simp add: diffs_def mult.assoc [symmetric] real_of_nat_def of_nat_mult
wenzelm@53079:         del: mult_Suc of_nat_Suc)
paulson@15077: 
huffman@23115: lemma diffs_of_real: "diffs (\<lambda>n. of_real (f n)) = (\<lambda>n. of_real (diffs f n))"
wenzelm@53079:   by (simp add: diffs_def)
huffman@23115: 
paulson@15077: lemma DERIV_exp [simp]: "DERIV exp x :> exp(x)"
wenzelm@53079:   unfolding exp_def scaleR_conv_of_real
wenzelm@53079:   apply (rule DERIV_cong)
wenzelm@53079:   apply (rule termdiffs [where K="of_real (1 + norm x)"])
wenzelm@53079:   apply (simp_all only: diffs_of_real scaleR_conv_of_real exp_fdiffs)
wenzelm@53079:   apply (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])+
wenzelm@53079:   apply (simp del: of_real_add)
wenzelm@53079:   done
paulson@15077: 
hoelzl@56381: declare DERIV_exp[THEN DERIV_chain2, derivative_intros]
hoelzl@51527: 
huffman@44311: lemma isCont_exp: "isCont exp x"
huffman@44311:   by (rule DERIV_exp [THEN DERIV_isCont])
huffman@44311: 
huffman@44311: lemma isCont_exp' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. exp (f x)) a"
huffman@44311:   by (rule isCont_o2 [OF _ isCont_exp])
huffman@44311: 
huffman@44311: lemma tendsto_exp [tendsto_intros]:
huffman@44311:   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. exp (f x)) ---> exp a) F"
huffman@44311:   by (rule isCont_tendsto_compose [OF isCont_exp])
huffman@23045: 
wenzelm@53079: lemma continuous_exp [continuous_intros]:
wenzelm@53079:   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. exp (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_exp)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_exp [continuous_intros]:
wenzelm@53079:   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. exp (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_exp)
hoelzl@51478: 
wenzelm@53079: 
huffman@29167: subsubsection {* Properties of the Exponential Function *}
paulson@15077: 
huffman@23278: lemma powser_zero:
haftmann@31017:   fixes f :: "nat \<Rightarrow> 'a::{real_normed_algebra_1}"
huffman@23278:   shows "(\<Sum>n. f n * 0 ^ n) = f 0"
paulson@15077: proof -
hoelzl@56193:   have "(\<Sum>n<1. f n * 0 ^ n) = (\<Sum>n. f n * 0 ^ n)"
hoelzl@56213:     by (subst suminf_finite[where N="{0}"]) (auto simp: power_0_left)
huffman@30082:   thus ?thesis unfolding One_nat_def by simp
paulson@15077: qed
paulson@15077: 
huffman@23278: lemma exp_zero [simp]: "exp 0 = 1"
wenzelm@53079:   unfolding exp_def by (simp add: scaleR_conv_of_real powser_zero)
huffman@23278: 
huffman@23115: lemma exp_series_add:
haftmann@31017:   fixes x y :: "'a::{real_field}"
haftmann@25062:   defines S_def: "S \<equiv> \<lambda>x n. x ^ n /\<^sub>R real (fact n)"
hoelzl@56213:   shows "S (x + y) n = (\<Sum>i\<le>n. S x i * S y (n - i))"
huffman@23115: proof (induct n)
huffman@23115:   case 0
huffman@23115:   show ?case
huffman@23115:     unfolding S_def by simp
huffman@23115: next
huffman@23115:   case (Suc n)
haftmann@25062:   have S_Suc: "\<And>x n. S x (Suc n) = (x * S x n) /\<^sub>R real (Suc n)"
huffman@30273:     unfolding S_def by (simp del: mult_Suc)
haftmann@25062:   hence times_S: "\<And>x n. x * S x n = real (Suc n) *\<^sub>R S x (Suc n)"
huffman@23115:     by simp
huffman@23115: 
haftmann@25062:   have "real (Suc n) *\<^sub>R S (x + y) (Suc n) = (x + y) * S (x + y) n"
huffman@23115:     by (simp only: times_S)
hoelzl@56213:   also have "\<dots> = (x + y) * (\<Sum>i\<le>n. S x i * S y (n-i))"
huffman@23115:     by (simp only: Suc)
hoelzl@56213:   also have "\<dots> = x * (\<Sum>i\<le>n. S x i * S y (n-i))
hoelzl@56213:                 + y * (\<Sum>i\<le>n. S x i * S y (n-i))"
webertj@49962:     by (rule distrib_right)
hoelzl@56213:   also have "\<dots> = (\<Sum>i\<le>n. (x * S x i) * S y (n-i))
hoelzl@56213:                 + (\<Sum>i\<le>n. S x i * (y * S y (n-i)))"
haftmann@57514:     by (simp only: setsum_right_distrib ac_simps)
hoelzl@56213:   also have "\<dots> = (\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i)))
hoelzl@56213:                 + (\<Sum>i\<le>n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
huffman@23115:     by (simp add: times_S Suc_diff_le)
hoelzl@56213:   also have "(\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i))) =
hoelzl@56213:              (\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n-i)))"
hoelzl@56213:     by (subst setsum_atMost_Suc_shift) simp
hoelzl@56213:   also have "(\<Sum>i\<le>n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
hoelzl@56213:              (\<Sum>i\<le>Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
hoelzl@56213:     by simp
hoelzl@56213:   also have "(\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n-i))) +
hoelzl@56213:              (\<Sum>i\<le>Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
hoelzl@56213:              (\<Sum>i\<le>Suc n. real (Suc n) *\<^sub>R (S x i * S y (Suc n-i)))"
haftmann@57418:     by (simp only: setsum.distrib [symmetric] scaleR_left_distrib [symmetric]
hoelzl@56213:                    real_of_nat_add [symmetric]) simp
hoelzl@56213:   also have "\<dots> = real (Suc n) *\<^sub>R (\<Sum>i\<le>Suc n. S x i * S y (Suc n-i))"
huffman@23127:     by (simp only: scaleR_right.setsum)
huffman@23115:   finally show
hoelzl@56213:     "S (x + y) (Suc n) = (\<Sum>i\<le>Suc n. S x i * S y (Suc n - i))"
huffman@35216:     by (simp del: setsum_cl_ivl_Suc)
huffman@23115: qed
huffman@23115: 
huffman@23115: lemma exp_add: "exp (x + y) = exp x * exp y"
wenzelm@53079:   unfolding exp_def
wenzelm@53079:   by (simp only: Cauchy_product summable_norm_exp exp_series_add)
huffman@23115: 
huffman@29170: lemma mult_exp_exp: "exp x * exp y = exp (x + y)"
wenzelm@53079:   by (rule exp_add [symmetric])
huffman@29170: 
huffman@23241: lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
wenzelm@53079:   unfolding exp_def
wenzelm@53079:   apply (subst suminf_of_real)
wenzelm@53079:   apply (rule summable_exp_generic)
wenzelm@53079:   apply (simp add: scaleR_conv_of_real)
wenzelm@53079:   done
huffman@23241: 
huffman@29170: lemma exp_not_eq_zero [simp]: "exp x \<noteq> 0"
huffman@29170: proof
huffman@29170:   have "exp x * exp (- x) = 1" by (simp add: mult_exp_exp)
huffman@29170:   also assume "exp x = 0"
huffman@29170:   finally show "False" by simp
paulson@15077: qed
paulson@15077: 
huffman@29170: lemma exp_minus: "exp (- x) = inverse (exp x)"
wenzelm@53079:   by (rule inverse_unique [symmetric], simp add: mult_exp_exp)
paulson@15077: 
huffman@29170: lemma exp_diff: "exp (x - y) = exp x / exp y"
haftmann@54230:   using exp_add [of x "- y"] by (simp add: exp_minus divide_inverse)
paulson@15077: 
huffman@29167: 
huffman@29167: subsubsection {* Properties of the Exponential Function on Reals *}
huffman@29167: 
huffman@29170: text {* Comparisons of @{term "exp x"} with zero. *}
huffman@29167: 
huffman@29167: text{*Proof: because every exponential can be seen as a square.*}
huffman@29167: lemma exp_ge_zero [simp]: "0 \<le> exp (x::real)"
huffman@29167: proof -
huffman@29167:   have "0 \<le> exp (x/2) * exp (x/2)" by simp
huffman@29167:   thus ?thesis by (simp add: exp_add [symmetric])
huffman@29167: qed
huffman@29167: 
huffman@23115: lemma exp_gt_zero [simp]: "0 < exp (x::real)"
wenzelm@53079:   by (simp add: order_less_le)
paulson@15077: 
huffman@29170: lemma not_exp_less_zero [simp]: "\<not> exp (x::real) < 0"
wenzelm@53079:   by (simp add: not_less)
huffman@29170: 
huffman@29170: lemma not_exp_le_zero [simp]: "\<not> exp (x::real) \<le> 0"
wenzelm@53079:   by (simp add: not_le)
paulson@15077: 
huffman@23115: lemma abs_exp_cancel [simp]: "\<bar>exp x::real\<bar> = exp x"
wenzelm@53079:   by simp
paulson@15077: 
paulson@15077: lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
haftmann@57512:   by (induct n) (auto simp add: real_of_nat_Suc distrib_left exp_add mult.commute)
paulson@15077: 
huffman@29170: text {* Strict monotonicity of exponential. *}
huffman@29170: 
paulson@54575: lemma exp_ge_add_one_self_aux: 
paulson@54575:   assumes "0 \<le> (x::real)" shows "1+x \<le> exp(x)"
paulson@54575: using order_le_imp_less_or_eq [OF assms]
paulson@54575: proof 
paulson@54575:   assume "0 < x"
hoelzl@56193:   have "1+x \<le> (\<Sum>n<2. inverse (real (fact n)) * x ^ n)"
paulson@54575:     by (auto simp add: numeral_2_eq_2)
paulson@54575:   also have "... \<le> (\<Sum>n. inverse (real (fact n)) * x ^ n)"
hoelzl@56213:     apply (rule setsum_le_suminf [OF summable_exp])
paulson@54575:     using `0 < x`
paulson@54575:     apply (auto  simp add:  zero_le_mult_iff)
paulson@54575:     done
paulson@54575:   finally show "1+x \<le> exp x" 
paulson@54575:     by (simp add: exp_def)
paulson@54575: next
paulson@54575:   assume "0 = x"
paulson@54575:   then show "1 + x \<le> exp x"
paulson@54575:     by auto
paulson@54575: qed
huffman@29170: 
huffman@29170: lemma exp_gt_one: "0 < (x::real) \<Longrightarrow> 1 < exp x"
huffman@29170: proof -
huffman@29170:   assume x: "0 < x"
huffman@29170:   hence "1 < 1 + x" by simp
huffman@29170:   also from x have "1 + x \<le> exp x"
huffman@29170:     by (simp add: exp_ge_add_one_self_aux)
huffman@29170:   finally show ?thesis .
huffman@29170: qed
huffman@29170: 
paulson@15077: lemma exp_less_mono:
huffman@23115:   fixes x y :: real
wenzelm@53079:   assumes "x < y"
wenzelm@53079:   shows "exp x < exp y"
paulson@15077: proof -
huffman@29165:   from `x < y` have "0 < y - x" by simp
huffman@29165:   hence "1 < exp (y - x)" by (rule exp_gt_one)
huffman@29165:   hence "1 < exp y / exp x" by (simp only: exp_diff)
huffman@29165:   thus "exp x < exp y" by simp
paulson@15077: qed
paulson@15077: 
wenzelm@53079: lemma exp_less_cancel: "exp (x::real) < exp y \<Longrightarrow> x < y"
paulson@54575:   unfolding linorder_not_le [symmetric]
paulson@54575:   by (auto simp add: order_le_less exp_less_mono)
paulson@15077: 
huffman@29170: lemma exp_less_cancel_iff [iff]: "exp (x::real) < exp y \<longleftrightarrow> x < y"
wenzelm@53079:   by (auto intro: exp_less_mono exp_less_cancel)
paulson@15077: 
huffman@29170: lemma exp_le_cancel_iff [iff]: "exp (x::real) \<le> exp y \<longleftrightarrow> x \<le> y"
wenzelm@53079:   by (auto simp add: linorder_not_less [symmetric])
paulson@15077: 
huffman@29170: lemma exp_inj_iff [iff]: "exp (x::real) = exp y \<longleftrightarrow> x = y"
wenzelm@53079:   by (simp add: order_eq_iff)
paulson@15077: 
huffman@29170: text {* Comparisons of @{term "exp x"} with one. *}
huffman@29170: 
huffman@29170: lemma one_less_exp_iff [simp]: "1 < exp (x::real) \<longleftrightarrow> 0 < x"
huffman@29170:   using exp_less_cancel_iff [where x=0 and y=x] by simp
huffman@29170: 
huffman@29170: lemma exp_less_one_iff [simp]: "exp (x::real) < 1 \<longleftrightarrow> x < 0"
huffman@29170:   using exp_less_cancel_iff [where x=x and y=0] by simp
huffman@29170: 
huffman@29170: lemma one_le_exp_iff [simp]: "1 \<le> exp (x::real) \<longleftrightarrow> 0 \<le> x"
huffman@29170:   using exp_le_cancel_iff [where x=0 and y=x] by simp
huffman@29170: 
huffman@29170: lemma exp_le_one_iff [simp]: "exp (x::real) \<le> 1 \<longleftrightarrow> x \<le> 0"
huffman@29170:   using exp_le_cancel_iff [where x=x and y=0] by simp
huffman@29170: 
huffman@29170: lemma exp_eq_one_iff [simp]: "exp (x::real) = 1 \<longleftrightarrow> x = 0"
huffman@29170:   using exp_inj_iff [where x=x and y=0] by simp
huffman@29170: 
wenzelm@53079: lemma lemma_exp_total: "1 \<le> y \<Longrightarrow> \<exists>x. 0 \<le> x & x \<le> y - 1 & exp(x::real) = y"
huffman@44755: proof (rule IVT)
huffman@44755:   assume "1 \<le> y"
huffman@44755:   hence "0 \<le> y - 1" by simp
huffman@44755:   hence "1 + (y - 1) \<le> exp (y - 1)" by (rule exp_ge_add_one_self_aux)
huffman@44755:   thus "y \<le> exp (y - 1)" by simp
huffman@44755: qed (simp_all add: le_diff_eq)
paulson@15077: 
wenzelm@53079: lemma exp_total: "0 < (y::real) \<Longrightarrow> \<exists>x. exp x = y"
huffman@44755: proof (rule linorder_le_cases [of 1 y])
wenzelm@53079:   assume "1 \<le> y"
wenzelm@53079:   thus "\<exists>x. exp x = y" by (fast dest: lemma_exp_total)
huffman@44755: next
huffman@44755:   assume "0 < y" and "y \<le> 1"
huffman@44755:   hence "1 \<le> inverse y" by (simp add: one_le_inverse_iff)
huffman@44755:   then obtain x where "exp x = inverse y" by (fast dest: lemma_exp_total)
huffman@44755:   hence "exp (- x) = y" by (simp add: exp_minus)
huffman@44755:   thus "\<exists>x. exp x = y" ..
huffman@44755: qed
paulson@15077: 
paulson@15077: 
huffman@29164: subsection {* Natural Logarithm *}
paulson@15077: 
wenzelm@53079: definition ln :: "real \<Rightarrow> real"
wenzelm@53079:   where "ln x = (THE u. exp u = x)"
huffman@23043: 
huffman@23043: lemma ln_exp [simp]: "ln (exp x) = x"
huffman@44308:   by (simp add: ln_def)
paulson@15077: 
huffman@22654: lemma exp_ln [simp]: "0 < x \<Longrightarrow> exp (ln x) = x"
huffman@44308:   by (auto dest: exp_total)
huffman@22654: 
huffman@29171: lemma exp_ln_iff [simp]: "exp (ln x) = x \<longleftrightarrow> 0 < x"
huffman@44308:   by (metis exp_gt_zero exp_ln)
paulson@15077: 
huffman@29171: lemma ln_unique: "exp y = x \<Longrightarrow> ln x = y"
huffman@44308:   by (erule subst, rule ln_exp)
huffman@29171: 
huffman@29171: lemma ln_one [simp]: "ln 1 = 0"
wenzelm@53079:   by (rule ln_unique) simp
wenzelm@53079: 
wenzelm@53079: lemma ln_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x * y) = ln x + ln y"
wenzelm@53079:   by (rule ln_unique) (simp add: exp_add)
huffman@29171: 
huffman@29171: lemma ln_inverse: "0 < x \<Longrightarrow> ln (inverse x) = - ln x"
wenzelm@53079:   by (rule ln_unique) (simp add: exp_minus)
wenzelm@53079: 
wenzelm@53079: lemma ln_div: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x / y) = ln x - ln y"
wenzelm@53079:   by (rule ln_unique) (simp add: exp_diff)
paulson@15077: 
huffman@29171: lemma ln_realpow: "0 < x \<Longrightarrow> ln (x ^ n) = real n * ln x"
wenzelm@53079:   by (rule ln_unique) (simp add: exp_real_of_nat_mult)
wenzelm@53079: 
wenzelm@53079: lemma ln_less_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x < ln y \<longleftrightarrow> x < y"
wenzelm@53079:   by (subst exp_less_cancel_iff [symmetric]) simp
wenzelm@53079: 
wenzelm@53079: lemma ln_le_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x \<le> ln y \<longleftrightarrow> x \<le> y"
huffman@44308:   by (simp add: linorder_not_less [symmetric])
huffman@29171: 
wenzelm@53079: lemma ln_inj_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x = ln y \<longleftrightarrow> x = y"
huffman@44308:   by (simp add: order_eq_iff)
huffman@29171: 
huffman@29171: lemma ln_add_one_self_le_self [simp]: "0 \<le> x \<Longrightarrow> ln (1 + x) \<le> x"
huffman@44308:   apply (rule exp_le_cancel_iff [THEN iffD1])
huffman@44308:   apply (simp add: exp_ge_add_one_self_aux)
huffman@44308:   done
paulson@15077: 
huffman@29171: lemma ln_less_self [simp]: "0 < x \<Longrightarrow> ln x < x"
huffman@44308:   by (rule order_less_le_trans [where y="ln (1 + x)"]) simp_all
huffman@44308: 
huffman@44308: lemma ln_ge_zero [simp]: "1 \<le> x \<Longrightarrow> 0 \<le> ln x"
huffman@44308:   using ln_le_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_ge_zero_imp_ge_one: "0 \<le> ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> x"
huffman@44308:   using ln_le_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_ge_zero_iff [simp]: "0 < x \<Longrightarrow> 0 \<le> ln x \<longleftrightarrow> 1 \<le> x"
huffman@44308:   using ln_le_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_less_zero_iff [simp]: "0 < x \<Longrightarrow> ln x < 0 \<longleftrightarrow> x < 1"
huffman@44308:   using ln_less_cancel_iff [of x 1] by simp
huffman@44308: 
huffman@44308: lemma ln_gt_zero: "1 < x \<Longrightarrow> 0 < ln x"
huffman@44308:   using ln_less_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_gt_zero_imp_gt_one: "0 < ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 < x"
huffman@44308:   using ln_less_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_gt_zero_iff [simp]: "0 < x \<Longrightarrow> 0 < ln x \<longleftrightarrow> 1 < x"
huffman@44308:   using ln_less_cancel_iff [of 1 x] by simp
huffman@44308: 
wenzelm@53079: lemma ln_eq_zero_iff [simp]: "0 < x \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1"
huffman@44308:   using ln_inj_iff [of x 1] by simp
huffman@44308: 
wenzelm@53079: lemma ln_less_zero: "0 < x \<Longrightarrow> x < 1 \<Longrightarrow> ln x < 0"
huffman@44308:   by simp
paulson@15077: 
hoelzl@57275: lemma ln_neg_is_const: "x \<le> 0 \<Longrightarrow> ln x = (THE x. False)"
hoelzl@57275:   by (auto simp add: ln_def intro!: arg_cong[where f=The])
hoelzl@57275: 
hoelzl@57275: lemma isCont_ln: assumes "x \<noteq> 0" shows "isCont ln x"
hoelzl@57275: proof cases
hoelzl@57275:   assume "0 < x"
hoelzl@57275:   moreover then have "isCont ln (exp (ln x))"
hoelzl@57275:     by (intro isCont_inv_fun[where d="\<bar>x\<bar>" and f=exp]) auto
hoelzl@57275:   ultimately show ?thesis
hoelzl@57275:     by simp
hoelzl@57275: next
hoelzl@57275:   assume "\<not> 0 < x" with `x \<noteq> 0` show "isCont ln x"
hoelzl@57275:     unfolding isCont_def
hoelzl@57275:     by (subst filterlim_cong[OF _ refl, of _ "nhds (ln 0)" _ "\<lambda>_. ln 0"])
hoelzl@57275:        (auto simp: ln_neg_is_const not_less eventually_at dist_real_def
hoelzl@57275:                 intro!: tendsto_const exI[of _ "\<bar>x\<bar>"])
hoelzl@57275: qed
huffman@23045: 
huffman@45915: lemma tendsto_ln [tendsto_intros]:
hoelzl@57275:   "(f ---> a) F \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. ln (f x)) ---> ln a) F"
huffman@45915:   by (rule isCont_tendsto_compose [OF isCont_ln])
huffman@45915: 
hoelzl@51478: lemma continuous_ln:
hoelzl@57275:   "continuous F f \<Longrightarrow> f (Lim F (\<lambda>x. x)) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. ln (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_ln)
hoelzl@51478: 
hoelzl@51478: lemma isCont_ln' [continuous_intros]:
hoelzl@57275:   "continuous (at x) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. ln (f x))"
hoelzl@51478:   unfolding continuous_at by (rule tendsto_ln)
hoelzl@51478: 
hoelzl@51478: lemma continuous_within_ln [continuous_intros]:
hoelzl@57275:   "continuous (at x within s) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. ln (f x))"
hoelzl@51478:   unfolding continuous_within by (rule tendsto_ln)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_ln [continuous_intros]:
hoelzl@57275:   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. f x \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. ln (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_ln)
hoelzl@51478: 
huffman@23045: lemma DERIV_ln: "0 < x \<Longrightarrow> DERIV ln x :> inverse x"
huffman@44308:   apply (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
paulson@54576:   apply (auto intro: DERIV_cong [OF DERIV_exp exp_ln] isCont_ln)
huffman@44308:   done
huffman@23045: 
wenzelm@53079: lemma DERIV_ln_divide: "0 < x \<Longrightarrow> DERIV ln x :> 1 / x"
paulson@33667:   by (rule DERIV_ln[THEN DERIV_cong], simp, simp add: divide_inverse)
paulson@33667: 
hoelzl@56381: declare DERIV_ln_divide[THEN DERIV_chain2, derivative_intros]
hoelzl@51527: 
wenzelm@53079: lemma ln_series:
wenzelm@53079:   assumes "0 < x" and "x < 2"
wenzelm@53079:   shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
wenzelm@53079:   (is "ln x = suminf (?f (x - 1))")
hoelzl@29803: proof -
wenzelm@53079:   let ?f' = "\<lambda>x n. (-1)^n * (x - 1)^n"
hoelzl@29803: 
hoelzl@29803:   have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
hoelzl@29803:   proof (rule DERIV_isconst3[where x=x])
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "x \<in> {0 <..< 2}"
wenzelm@53079:     hence "0 < x" and "x < 2" by auto
wenzelm@53079:     have "norm (1 - x) < 1"
wenzelm@53079:       using `0 < x` and `x < 2` by auto
hoelzl@29803:     have "1 / x = 1 / (1 - (1 - x))" by auto
wenzelm@53079:     also have "\<dots> = (\<Sum> n. (1 - x)^n)"
wenzelm@53079:       using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
wenzelm@53079:     also have "\<dots> = suminf (?f' x)"
wenzelm@53079:       unfolding power_mult_distrib[symmetric]
wenzelm@53079:       by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
wenzelm@53079:     finally have "DERIV ln x :> suminf (?f' x)"
wenzelm@53079:       using DERIV_ln[OF `0 < x`] unfolding divide_inverse by auto
hoelzl@29803:     moreover
hoelzl@29803:     have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
wenzelm@53079:     have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :>
wenzelm@53079:       (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
hoelzl@29803:     proof (rule DERIV_power_series')
wenzelm@53079:       show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1"
wenzelm@53079:         using `0 < x` `x < 2` by auto
wenzelm@53079:       fix x :: real
wenzelm@53079:       assume "x \<in> {- 1<..<1}"
wenzelm@53079:       hence "norm (-x) < 1" by auto
wenzelm@53079:       show "summable (\<lambda>n. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
wenzelm@53079:         unfolding One_nat_def
wenzelm@53079:         by (auto simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
hoelzl@29803:     qed
wenzelm@53079:     hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)"
wenzelm@53079:       unfolding One_nat_def by auto
wenzelm@53079:     hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)"
hoelzl@56381:       unfolding DERIV_def repos .
hoelzl@29803:     ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
hoelzl@29803:       by (rule DERIV_diff)
hoelzl@29803:     thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
hoelzl@29803:   qed (auto simp add: assms)
huffman@44289:   thus ?thesis by auto
hoelzl@29803: qed
paulson@15077: 
hoelzl@50326: lemma exp_first_two_terms: "exp x = 1 + x + (\<Sum> n. inverse(fact (n+2)) * (x ^ (n+2)))"
hoelzl@50326: proof -
wenzelm@53079:   have "exp x = suminf (\<lambda>n. inverse(fact n) * (x ^ n))"
hoelzl@50326:     by (simp add: exp_def)
hoelzl@56193:   also from summable_exp have "... = (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2))) + 
hoelzl@56193:     (\<Sum> n::nat<2. inverse(fact n) * (x ^ n))" (is "_ = _ + ?a")
hoelzl@50326:     by (rule suminf_split_initial_segment)
hoelzl@50326:   also have "?a = 1 + x"
hoelzl@50326:     by (simp add: numeral_2_eq_2)
hoelzl@56193:   finally show ?thesis
hoelzl@56193:     by simp
hoelzl@50326: qed
hoelzl@50326: 
wenzelm@53079: lemma exp_bound: "0 <= (x::real) \<Longrightarrow> x <= 1 \<Longrightarrow> exp x <= 1 + x + x\<^sup>2"
hoelzl@50326: proof -
hoelzl@50326:   assume a: "0 <= x"
hoelzl@50326:   assume b: "x <= 1"
wenzelm@53079:   {
wenzelm@53079:     fix n :: nat
hoelzl@50326:     have "2 * 2 ^ n \<le> fact (n + 2)"
wenzelm@53079:       by (induct n) simp_all
hoelzl@50326:     hence "real ((2::nat) * 2 ^ n) \<le> real (fact (n + 2))"
hoelzl@50326:       by (simp only: real_of_nat_le_iff)
hoelzl@50326:     hence "2 * 2 ^ n \<le> real (fact (n + 2))"
hoelzl@50326:       by simp
hoelzl@50326:     hence "inverse (fact (n + 2)) \<le> inverse (2 * 2 ^ n)"
hoelzl@50326:       by (rule le_imp_inverse_le) simp
hoelzl@50326:     hence "inverse (fact (n + 2)) \<le> 1/2 * (1/2)^n"
wenzelm@53079:       by (simp add: power_inverse)
wenzelm@53015:     hence "inverse (fact (n + 2)) * (x^n * x\<^sup>2) \<le> 1/2 * (1/2)^n * (1 * x\<^sup>2)"
hoelzl@50326:       by (rule mult_mono)
nipkow@56536:         (rule mult_mono, simp_all add: power_le_one a b)
wenzelm@53015:     hence "inverse (fact (n + 2)) * x ^ (n + 2) \<le> (x\<^sup>2/2) * ((1/2)^n)"
haftmann@57514:       unfolding power_add by (simp add: ac_simps del: fact_Suc) }
hoelzl@50326:   note aux1 = this
wenzelm@53015:   have "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums (x\<^sup>2 / 2 * (1 / (1 - 1 / 2)))"
hoelzl@50326:     by (intro sums_mult geometric_sums, simp)
wenzelm@53076:   hence aux2: "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums x\<^sup>2"
hoelzl@50326:     by simp
wenzelm@53079:   have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <= x\<^sup>2"
hoelzl@50326:   proof -
wenzelm@53079:     have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <=
wenzelm@53079:         suminf (\<lambda>n. (x\<^sup>2/2) * ((1/2)^n))"
hoelzl@56213:       apply (rule suminf_le)
hoelzl@50326:       apply (rule allI, rule aux1)
hoelzl@50326:       apply (rule summable_exp [THEN summable_ignore_initial_segment])
hoelzl@50326:       by (rule sums_summable, rule aux2)
wenzelm@53076:     also have "... = x\<^sup>2"
hoelzl@50326:       by (rule sums_unique [THEN sym], rule aux2)
hoelzl@50326:     finally show ?thesis .
hoelzl@50326:   qed
hoelzl@50326:   thus ?thesis unfolding exp_first_two_terms by auto
hoelzl@50326: qed
hoelzl@50326: 
wenzelm@53079: lemma ln_one_minus_pos_upper_bound: "0 <= x \<Longrightarrow> x < 1 \<Longrightarrow> ln (1 - x) <= - x"
hoelzl@50326: proof -
hoelzl@50326:   assume a: "0 <= (x::real)" and b: "x < 1"
wenzelm@53076:   have "(1 - x) * (1 + x + x\<^sup>2) = (1 - x^3)"
hoelzl@50326:     by (simp add: algebra_simps power2_eq_square power3_eq_cube)
hoelzl@50326:   also have "... <= 1"
hoelzl@50326:     by (auto simp add: a)
wenzelm@53076:   finally have "(1 - x) * (1 + x + x\<^sup>2) <= 1" .
wenzelm@53015:   moreover have c: "0 < 1 + x + x\<^sup>2"
hoelzl@50326:     by (simp add: add_pos_nonneg a)
wenzelm@53076:   ultimately have "1 - x <= 1 / (1 + x + x\<^sup>2)"
hoelzl@50326:     by (elim mult_imp_le_div_pos)
hoelzl@50326:   also have "... <= 1 / exp x"
paulson@54576:     by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs 
paulson@54576:               real_sqrt_pow2_iff real_sqrt_power)
hoelzl@50326:   also have "... = exp (-x)"
hoelzl@50326:     by (auto simp add: exp_minus divide_inverse)
hoelzl@50326:   finally have "1 - x <= exp (- x)" .
hoelzl@50326:   also have "1 - x = exp (ln (1 - x))"
paulson@54576:     by (metis b diff_0 exp_ln_iff less_iff_diff_less_0 minus_diff_eq)
hoelzl@50326:   finally have "exp (ln (1 - x)) <= exp (- x)" .
hoelzl@50326:   thus ?thesis by (auto simp only: exp_le_cancel_iff)
hoelzl@50326: qed
hoelzl@50326: 
hoelzl@50326: lemma exp_ge_add_one_self [simp]: "1 + (x::real) <= exp x"
hoelzl@50326:   apply (case_tac "0 <= x")
hoelzl@50326:   apply (erule exp_ge_add_one_self_aux)
hoelzl@50326:   apply (case_tac "x <= -1")
hoelzl@50326:   apply (subgoal_tac "1 + x <= 0")
hoelzl@50326:   apply (erule order_trans)
hoelzl@50326:   apply simp
hoelzl@50326:   apply simp
hoelzl@50326:   apply (subgoal_tac "1 + x = exp(ln (1 + x))")
hoelzl@50326:   apply (erule ssubst)
hoelzl@50326:   apply (subst exp_le_cancel_iff)
hoelzl@50326:   apply (subgoal_tac "ln (1 - (- x)) <= - (- x)")
hoelzl@50326:   apply simp
hoelzl@50326:   apply (rule ln_one_minus_pos_upper_bound)
hoelzl@50326:   apply auto
hoelzl@50326: done
hoelzl@50326: 
wenzelm@53079: lemma ln_one_plus_pos_lower_bound: "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> x - x\<^sup>2 <= ln (1 + x)"
hoelzl@51527: proof -
hoelzl@51527:   assume a: "0 <= x" and b: "x <= 1"
wenzelm@53076:   have "exp (x - x\<^sup>2) = exp x / exp (x\<^sup>2)"
hoelzl@51527:     by (rule exp_diff)
wenzelm@53076:   also have "... <= (1 + x + x\<^sup>2) / exp (x \<^sup>2)"
paulson@54576:     by (metis a b divide_right_mono exp_bound exp_ge_zero)
wenzelm@53076:   also have "... <= (1 + x + x\<^sup>2) / (1 + x\<^sup>2)"
nipkow@56544:     by (simp add: a divide_left_mono add_pos_nonneg)
hoelzl@51527:   also from a have "... <= 1 + x"
hoelzl@51527:     by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
wenzelm@53076:   finally have "exp (x - x\<^sup>2) <= 1 + x" .
hoelzl@51527:   also have "... = exp (ln (1 + x))"
hoelzl@51527:   proof -
hoelzl@51527:     from a have "0 < 1 + x" by auto
hoelzl@51527:     thus ?thesis
hoelzl@51527:       by (auto simp only: exp_ln_iff [THEN sym])
hoelzl@51527:   qed
wenzelm@53076:   finally have "exp (x - x\<^sup>2) <= exp (ln (1 + x))" .
paulson@54576:   thus ?thesis
paulson@54576:     by (metis exp_le_cancel_iff) 
hoelzl@51527: qed
hoelzl@51527: 
wenzelm@53079: lemma ln_one_minus_pos_lower_bound:
wenzelm@53079:   "0 <= x \<Longrightarrow> x <= (1 / 2) \<Longrightarrow> - x - 2 * x\<^sup>2 <= ln (1 - x)"
hoelzl@51527: proof -
hoelzl@51527:   assume a: "0 <= x" and b: "x <= (1 / 2)"
wenzelm@53079:   from b have c: "x < 1" by auto
hoelzl@51527:   then have "ln (1 - x) = - ln (1 + x / (1 - x))"
paulson@54576:     apply (subst ln_inverse [symmetric])
paulson@54576:     apply (simp add: field_simps)
paulson@54576:     apply (rule arg_cong [where f=ln])
paulson@54576:     apply (simp add: field_simps)
paulson@54576:     done
hoelzl@51527:   also have "- (x / (1 - x)) <= ..."
wenzelm@53079:   proof -
hoelzl@51527:     have "ln (1 + x / (1 - x)) <= x / (1 - x)"
hoelzl@56571:       using a c by (intro ln_add_one_self_le_self) auto
hoelzl@51527:     thus ?thesis
hoelzl@51527:       by auto
hoelzl@51527:   qed
hoelzl@51527:   also have "- (x / (1 - x)) = -x / (1 - x)"
hoelzl@51527:     by auto
hoelzl@51527:   finally have d: "- x / (1 - x) <= ln (1 - x)" .
hoelzl@51527:   have "0 < 1 - x" using a b by simp
wenzelm@53076:   hence e: "-x - 2 * x\<^sup>2 <= - x / (1 - x)"
hoelzl@51527:     using mult_right_le_one_le[of "x*x" "2*x"] a b
wenzelm@53079:     by (simp add: field_simps power2_eq_square)
wenzelm@53076:   from e d show "- x - 2 * x\<^sup>2 <= ln (1 - x)"
hoelzl@51527:     by (rule order_trans)
hoelzl@51527: qed
hoelzl@51527: 
wenzelm@53079: lemma ln_add_one_self_le_self2: "-1 < x \<Longrightarrow> ln(1 + x) <= x"
hoelzl@51527:   apply (subgoal_tac "ln (1 + x) \<le> ln (exp x)", simp)
hoelzl@51527:   apply (subst ln_le_cancel_iff)
hoelzl@51527:   apply auto
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
wenzelm@53079:   "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> abs(ln (1 + x) - x) <= x\<^sup>2"
hoelzl@51527: proof -
hoelzl@51527:   assume x: "0 <= x"
hoelzl@51527:   assume x1: "x <= 1"
hoelzl@51527:   from x have "ln (1 + x) <= x"
hoelzl@51527:     by (rule ln_add_one_self_le_self)
wenzelm@53079:   then have "ln (1 + x) - x <= 0"
hoelzl@51527:     by simp
hoelzl@51527:   then have "abs(ln(1 + x) - x) = - (ln(1 + x) - x)"
hoelzl@51527:     by (rule abs_of_nonpos)
wenzelm@53079:   also have "... = x - ln (1 + x)"
hoelzl@51527:     by simp
wenzelm@53076:   also have "... <= x\<^sup>2"
hoelzl@51527:   proof -
wenzelm@53076:     from x x1 have "x - x\<^sup>2 <= ln (1 + x)"
hoelzl@51527:       by (intro ln_one_plus_pos_lower_bound)
hoelzl@51527:     thus ?thesis
hoelzl@51527:       by simp
hoelzl@51527:   qed
hoelzl@51527:   finally show ?thesis .
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@51527: lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
wenzelm@53079:   "-(1 / 2) <= x \<Longrightarrow> x <= 0 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
hoelzl@51527: proof -
hoelzl@51527:   assume a: "-(1 / 2) <= x"
hoelzl@51527:   assume b: "x <= 0"
wenzelm@53079:   have "abs(ln (1 + x) - x) = x - ln(1 - (-x))"
hoelzl@51527:     apply (subst abs_of_nonpos)
hoelzl@51527:     apply simp
hoelzl@51527:     apply (rule ln_add_one_self_le_self2)
hoelzl@51527:     using a apply auto
hoelzl@51527:     done
wenzelm@53076:   also have "... <= 2 * x\<^sup>2"
wenzelm@53076:     apply (subgoal_tac "- (-x) - 2 * (-x)\<^sup>2 <= ln (1 - (-x))")
hoelzl@51527:     apply (simp add: algebra_simps)
hoelzl@51527:     apply (rule ln_one_minus_pos_lower_bound)
hoelzl@51527:     using a b apply auto
hoelzl@51527:     done
hoelzl@51527:   finally show ?thesis .
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@51527: lemma abs_ln_one_plus_x_minus_x_bound:
wenzelm@53079:     "abs x <= 1 / 2 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
hoelzl@51527:   apply (case_tac "0 <= x")
hoelzl@51527:   apply (rule order_trans)
hoelzl@51527:   apply (rule abs_ln_one_plus_x_minus_x_bound_nonneg)
hoelzl@51527:   apply auto
hoelzl@51527:   apply (rule abs_ln_one_plus_x_minus_x_bound_nonpos)
hoelzl@51527:   apply auto
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma ln_x_over_x_mono: "exp 1 <= x \<Longrightarrow> x <= y \<Longrightarrow> (ln y / y) <= (ln x / x)"
hoelzl@51527: proof -
hoelzl@51527:   assume x: "exp 1 <= x" "x <= y"
hoelzl@51527:   moreover have "0 < exp (1::real)" by simp
hoelzl@51527:   ultimately have a: "0 < x" and b: "0 < y"
hoelzl@51527:     by (fast intro: less_le_trans order_trans)+
hoelzl@51527:   have "x * ln y - x * ln x = x * (ln y - ln x)"
hoelzl@51527:     by (simp add: algebra_simps)
hoelzl@51527:   also have "... = x * ln(y / x)"
hoelzl@51527:     by (simp only: ln_div a b)
hoelzl@51527:   also have "y / x = (x + (y - x)) / x"
hoelzl@51527:     by simp
hoelzl@51527:   also have "... = 1 + (y - x) / x"
hoelzl@51527:     using x a by (simp add: field_simps)
hoelzl@51527:   also have "x * ln(1 + (y - x) / x) <= x * ((y - x) / x)"
hoelzl@56571:     using x a 
hoelzl@56571:     by (intro mult_left_mono ln_add_one_self_le_self) simp_all
hoelzl@51527:   also have "... = y - x" using a by simp
hoelzl@51527:   also have "... = (y - x) * ln (exp 1)" by simp
hoelzl@51527:   also have "... <= (y - x) * ln x"
hoelzl@51527:     apply (rule mult_left_mono)
hoelzl@51527:     apply (subst ln_le_cancel_iff)
hoelzl@51527:     apply fact
hoelzl@51527:     apply (rule a)
hoelzl@51527:     apply (rule x)
hoelzl@51527:     using x apply simp
hoelzl@51527:     done
hoelzl@51527:   also have "... = y * ln x - x * ln x"
hoelzl@51527:     by (rule left_diff_distrib)
hoelzl@51527:   finally have "x * ln y <= y * ln x"
hoelzl@51527:     by arith
hoelzl@51527:   then have "ln y <= (y * ln x) / x" using a by (simp add: field_simps)
hoelzl@51527:   also have "... = y * (ln x / x)" by simp
hoelzl@51527:   finally show ?thesis using b by (simp add: field_simps)
hoelzl@51527: qed
hoelzl@51527: 
wenzelm@53079: lemma ln_le_minus_one: "0 < x \<Longrightarrow> ln x \<le> x - 1"
hoelzl@51527:   using exp_ge_add_one_self[of "ln x"] by simp
hoelzl@51527: 
hoelzl@51527: lemma ln_eq_minus_one:
wenzelm@53079:   assumes "0 < x" "ln x = x - 1"
wenzelm@53079:   shows "x = 1"
hoelzl@51527: proof -
wenzelm@53079:   let ?l = "\<lambda>y. ln y - y + 1"
hoelzl@51527:   have D: "\<And>x. 0 < x \<Longrightarrow> DERIV ?l x :> (1 / x - 1)"
hoelzl@56381:     by (auto intro!: derivative_eq_intros)
hoelzl@51527: 
hoelzl@51527:   show ?thesis
hoelzl@51527:   proof (cases rule: linorder_cases)
hoelzl@51527:     assume "x < 1"
hoelzl@51527:     from dense[OF `x < 1`] obtain a where "x < a" "a < 1" by blast
hoelzl@51527:     from `x < a` have "?l x < ?l a"
hoelzl@51527:     proof (rule DERIV_pos_imp_increasing, safe)
wenzelm@53079:       fix y
wenzelm@53079:       assume "x \<le> y" "y \<le> a"
hoelzl@51527:       with `0 < x` `a < 1` have "0 < 1 / y - 1" "0 < y"
hoelzl@51527:         by (auto simp: field_simps)
hoelzl@51527:       with D show "\<exists>z. DERIV ?l y :> z \<and> 0 < z"
hoelzl@51527:         by auto
hoelzl@51527:     qed
hoelzl@51527:     also have "\<dots> \<le> 0"
hoelzl@51527:       using ln_le_minus_one `0 < x` `x < a` by (auto simp: field_simps)
hoelzl@51527:     finally show "x = 1" using assms by auto
hoelzl@51527:   next
hoelzl@51527:     assume "1 < x"
wenzelm@53079:     from dense[OF this] obtain a where "1 < a" "a < x" by blast
hoelzl@51527:     from `a < x` have "?l x < ?l a"
hoelzl@51527:     proof (rule DERIV_neg_imp_decreasing, safe)
wenzelm@53079:       fix y
wenzelm@53079:       assume "a \<le> y" "y \<le> x"
hoelzl@51527:       with `1 < a` have "1 / y - 1 < 0" "0 < y"
hoelzl@51527:         by (auto simp: field_simps)
hoelzl@51527:       with D show "\<exists>z. DERIV ?l y :> z \<and> z < 0"
hoelzl@51527:         by blast
hoelzl@51527:     qed
hoelzl@51527:     also have "\<dots> \<le> 0"
hoelzl@51527:       using ln_le_minus_one `1 < a` by (auto simp: field_simps)
hoelzl@51527:     finally show "x = 1" using assms by auto
wenzelm@53079:   next
wenzelm@53079:     assume "x = 1"
wenzelm@53079:     then show ?thesis by simp
wenzelm@53079:   qed
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@50326: lemma exp_at_bot: "(exp ---> (0::real)) at_bot"
hoelzl@50326:   unfolding tendsto_Zfun_iff
hoelzl@50326: proof (rule ZfunI, simp add: eventually_at_bot_dense)
hoelzl@50326:   fix r :: real assume "0 < r"
wenzelm@53079:   {
wenzelm@53079:     fix x
wenzelm@53079:     assume "x < ln r"
hoelzl@50326:     then have "exp x < exp (ln r)"
hoelzl@50326:       by simp
hoelzl@50326:     with `0 < r` have "exp x < r"
wenzelm@53079:       by simp
wenzelm@53079:   }
hoelzl@50326:   then show "\<exists>k. \<forall>n<k. exp n < r" by auto
hoelzl@50326: qed
hoelzl@50326: 
hoelzl@50326: lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
hoelzl@50346:   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="ln"])
hoelzl@50346:      (auto intro: eventually_gt_at_top)
hoelzl@50326: 
hoelzl@50326: lemma ln_at_0: "LIM x at_right 0. ln x :> at_bot"
hoelzl@50346:   by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
hoelzl@51641:      (auto simp: eventually_at_filter)
hoelzl@50326: 
hoelzl@50326: lemma ln_at_top: "LIM x at_top. ln x :> at_top"
hoelzl@50346:   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
hoelzl@50346:      (auto intro: eventually_gt_at_top)
hoelzl@50326: 
hoelzl@50347: lemma tendsto_power_div_exp_0: "((\<lambda>x. x ^ k / exp x) ---> (0::real)) at_top"
hoelzl@50347: proof (induct k)
wenzelm@53079:   case 0
hoelzl@50347:   show "((\<lambda>x. x ^ 0 / exp x) ---> (0::real)) at_top"
hoelzl@50347:     by (simp add: inverse_eq_divide[symmetric])
hoelzl@50347:        (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
hoelzl@50347:               at_top_le_at_infinity order_refl)
hoelzl@50347: next
hoelzl@50347:   case (Suc k)
hoelzl@50347:   show ?case
hoelzl@50347:   proof (rule lhospital_at_top_at_top)
hoelzl@50347:     show "eventually (\<lambda>x. DERIV (\<lambda>x. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
hoelzl@56381:       by eventually_elim (intro derivative_eq_intros, auto)
hoelzl@50347:     show "eventually (\<lambda>x. DERIV exp x :> exp x) at_top"
hoelzl@56381:       by eventually_elim auto
hoelzl@50347:     show "eventually (\<lambda>x. exp x \<noteq> 0) at_top"
hoelzl@50347:       by auto
hoelzl@50347:     from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
hoelzl@50347:     show "((\<lambda>x. real (Suc k) * x ^ k / exp x) ---> 0) at_top"
hoelzl@50347:       by simp
hoelzl@50347:   qed (rule exp_at_top)
hoelzl@50347: qed
hoelzl@50347: 
hoelzl@51527: 
wenzelm@53079: definition powr :: "[real,real] => real"  (infixr "powr" 80)
wenzelm@53079:   -- {*exponentation with real exponent*}
wenzelm@53079:   where "x powr a = exp(a * ln x)"
wenzelm@53079: 
wenzelm@53079: definition log :: "[real,real] => real"
wenzelm@53079:   -- {*logarithm of @{term x} to base @{term a}*}
wenzelm@53079:   where "log a x = ln x / ln a"
hoelzl@51527: 
hoelzl@51527: 
hoelzl@51527: lemma tendsto_log [tendsto_intros]:
hoelzl@51527:   "\<lbrakk>(f ---> a) F; (g ---> b) F; 0 < a; a \<noteq> 1; 0 < b\<rbrakk> \<Longrightarrow> ((\<lambda>x. log (f x) (g x)) ---> log a b) F"
hoelzl@51527:   unfolding log_def by (intro tendsto_intros) auto
hoelzl@51527: 
hoelzl@51527: lemma continuous_log:
wenzelm@53079:   assumes "continuous F f"
wenzelm@53079:     and "continuous F g"
wenzelm@53079:     and "0 < f (Lim F (\<lambda>x. x))"
wenzelm@53079:     and "f (Lim F (\<lambda>x. x)) \<noteq> 1"
wenzelm@53079:     and "0 < g (Lim F (\<lambda>x. x))"
hoelzl@51527:   shows "continuous F (\<lambda>x. log (f x) (g x))"
hoelzl@51527:   using assms unfolding continuous_def by (rule tendsto_log)
hoelzl@51527: 
hoelzl@51527: lemma continuous_at_within_log[continuous_intros]:
wenzelm@53079:   assumes "continuous (at a within s) f"
wenzelm@53079:     and "continuous (at a within s) g"
wenzelm@53079:     and "0 < f a"
wenzelm@53079:     and "f a \<noteq> 1"
wenzelm@53079:     and "0 < g a"
hoelzl@51527:   shows "continuous (at a within s) (\<lambda>x. log (f x) (g x))"
hoelzl@51527:   using assms unfolding continuous_within by (rule tendsto_log)
hoelzl@51527: 
hoelzl@51527: lemma isCont_log[continuous_intros, simp]:
hoelzl@51527:   assumes "isCont f a" "isCont g a" "0 < f a" "f a \<noteq> 1" "0 < g a"
hoelzl@51527:   shows "isCont (\<lambda>x. log (f x) (g x)) a"
hoelzl@51527:   using assms unfolding continuous_at by (rule tendsto_log)
hoelzl@51527: 
hoelzl@56371: lemma continuous_on_log[continuous_intros]:
wenzelm@53079:   assumes "continuous_on s f" "continuous_on s g"
wenzelm@53079:     and "\<forall>x\<in>s. 0 < f x" "\<forall>x\<in>s. f x \<noteq> 1" "\<forall>x\<in>s. 0 < g x"
hoelzl@51527:   shows "continuous_on s (\<lambda>x. log (f x) (g x))"
hoelzl@51527:   using assms unfolding continuous_on_def by (fast intro: tendsto_log)
hoelzl@51527: 
hoelzl@51527: lemma powr_one_eq_one [simp]: "1 powr a = 1"
wenzelm@53079:   by (simp add: powr_def)
hoelzl@51527: 
hoelzl@51527: lemma powr_zero_eq_one [simp]: "x powr 0 = 1"
wenzelm@53079:   by (simp add: powr_def)
hoelzl@51527: 
hoelzl@51527: lemma powr_one_gt_zero_iff [simp]: "(x powr 1 = x) = (0 < x)"
wenzelm@53079:   by (simp add: powr_def)
hoelzl@51527: declare powr_one_gt_zero_iff [THEN iffD2, simp]
hoelzl@51527: 
wenzelm@53079: lemma powr_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x * y) powr a = (x powr a) * (y powr a)"
wenzelm@53079:   by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)
hoelzl@51527: 
hoelzl@51527: lemma powr_gt_zero [simp]: "0 < x powr a"
wenzelm@53079:   by (simp add: powr_def)
hoelzl@51527: 
hoelzl@51527: lemma powr_ge_pzero [simp]: "0 <= x powr y"
wenzelm@53079:   by (rule order_less_imp_le, rule powr_gt_zero)
hoelzl@51527: 
hoelzl@51527: lemma powr_not_zero [simp]: "x powr a \<noteq> 0"
wenzelm@53079:   by (simp add: powr_def)
wenzelm@53079: 
wenzelm@53079: lemma powr_divide: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
wenzelm@53079:   apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
wenzelm@53079:   apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma powr_divide2: "x powr a / x powr b = x powr (a - b)"
hoelzl@51527:   apply (simp add: powr_def)
hoelzl@51527:   apply (subst exp_diff [THEN sym])
hoelzl@51527:   apply (simp add: left_diff_distrib)
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
wenzelm@53079:   by (simp add: powr_def exp_add [symmetric] distrib_right)
wenzelm@53079: 
wenzelm@53079: lemma powr_mult_base: "0 < x \<Longrightarrow>x * x powr y = x powr (1 + y)"
wenzelm@53079:   using assms by (auto simp: powr_add)
hoelzl@51527: 
hoelzl@51527: lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
wenzelm@53079:   by (simp add: powr_def)
hoelzl@51527: 
hoelzl@51527: lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
haftmann@57512:   by (simp add: powr_powr mult.commute)
hoelzl@51527: 
hoelzl@51527: lemma powr_minus: "x powr (-a) = inverse (x powr a)"
wenzelm@53079:   by (simp add: powr_def exp_minus [symmetric])
hoelzl@51527: 
hoelzl@51527: lemma powr_minus_divide: "x powr (-a) = 1/(x powr a)"
wenzelm@53079:   by (simp add: divide_inverse powr_minus)
wenzelm@53079: 
wenzelm@53079: lemma powr_less_mono: "a < b \<Longrightarrow> 1 < x \<Longrightarrow> x powr a < x powr b"
wenzelm@53079:   by (simp add: powr_def)
wenzelm@53079: 
wenzelm@53079: lemma powr_less_cancel: "x powr a < x powr b \<Longrightarrow> 1 < x \<Longrightarrow> a < b"
wenzelm@53079:   by (simp add: powr_def)
wenzelm@53079: 
wenzelm@53079: lemma powr_less_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a < x powr b) = (a < b)"
wenzelm@53079:   by (blast intro: powr_less_cancel powr_less_mono)
wenzelm@53079: 
wenzelm@53079: lemma powr_le_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a \<le> x powr b) = (a \<le> b)"
wenzelm@53079:   by (simp add: linorder_not_less [symmetric])
hoelzl@51527: 
hoelzl@51527: lemma log_ln: "ln x = log (exp(1)) x"
wenzelm@53079:   by (simp add: log_def)
wenzelm@53079: 
wenzelm@53079: lemma DERIV_log:
wenzelm@53079:   assumes "x > 0"
wenzelm@53079:   shows "DERIV (\<lambda>y. log b y) x :> 1 / (ln b * x)"
hoelzl@51527: proof -
hoelzl@51527:   def lb \<equiv> "1 / ln b"
hoelzl@51527:   moreover have "DERIV (\<lambda>y. lb * ln y) x :> lb / x"
hoelzl@56381:     using `x > 0` by (auto intro!: derivative_eq_intros)
hoelzl@51527:   ultimately show ?thesis
hoelzl@51527:     by (simp add: log_def)
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@56381: lemmas DERIV_log[THEN DERIV_chain2, derivative_intros]
hoelzl@51527: 
wenzelm@53079: lemma powr_log_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> a powr (log a x) = x"
wenzelm@53079:   by (simp add: powr_def log_def)
wenzelm@53079: 
wenzelm@53079: lemma log_powr_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a (a powr y) = y"
wenzelm@53079:   by (simp add: log_def powr_def)
wenzelm@53079: 
wenzelm@53079: lemma log_mult:
wenzelm@53079:   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow>
wenzelm@53079:     log a (x * y) = log a x + log a y"
wenzelm@53079:   by (simp add: log_def ln_mult divide_inverse distrib_right)
wenzelm@53079: 
wenzelm@53079: lemma log_eq_div_ln_mult_log:
wenzelm@53079:   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow>
wenzelm@53079:     log a x = (ln b/ln a) * log b x"
wenzelm@53079:   by (simp add: log_def divide_inverse)
hoelzl@51527: 
hoelzl@51527: text{*Base 10 logarithms*}
wenzelm@53079: lemma log_base_10_eq1: "0 < x \<Longrightarrow> log 10 x = (ln (exp 1) / ln 10) * ln x"
wenzelm@53079:   by (simp add: log_def)
wenzelm@53079: 
wenzelm@53079: lemma log_base_10_eq2: "0 < x \<Longrightarrow> log 10 x = (log 10 (exp 1)) * ln x"
wenzelm@53079:   by (simp add: log_def)
hoelzl@51527: 
hoelzl@51527: lemma log_one [simp]: "log a 1 = 0"
wenzelm@53079:   by (simp add: log_def)
hoelzl@51527: 
hoelzl@51527: lemma log_eq_one [simp]: "[| 0 < a; a \<noteq> 1 |] ==> log a a = 1"
wenzelm@53079:   by (simp add: log_def)
wenzelm@53079: 
wenzelm@53079: lemma log_inverse: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log a (inverse x) = - log a x"
wenzelm@53079:   apply (rule_tac a1 = "log a x" in add_left_cancel [THEN iffD1])
wenzelm@53079:   apply (simp add: log_mult [symmetric])
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma log_divide: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a (x/y) = log a x - log a y"
wenzelm@53079:   by (simp add: log_mult divide_inverse log_inverse)
hoelzl@51527: 
hoelzl@51527: lemma log_less_cancel_iff [simp]:
wenzelm@53079:   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x < log a y \<longleftrightarrow> x < y"
wenzelm@53079:   apply safe
wenzelm@53079:   apply (rule_tac [2] powr_less_cancel)
wenzelm@53079:   apply (drule_tac a = "log a x" in powr_less_mono, auto)
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma log_inj:
wenzelm@53079:   assumes "1 < b"
wenzelm@53079:   shows "inj_on (log b) {0 <..}"
hoelzl@51527: proof (rule inj_onI, simp)
wenzelm@53079:   fix x y
wenzelm@53079:   assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
hoelzl@51527:   show "x = y"
hoelzl@51527:   proof (cases rule: linorder_cases)
wenzelm@53079:     assume "x = y"
wenzelm@53079:     then show ?thesis by simp
wenzelm@53079:   next
hoelzl@51527:     assume "x < y" hence "log b x < log b y"
hoelzl@51527:       using log_less_cancel_iff[OF `1 < b`] pos by simp
wenzelm@53079:     then show ?thesis using * by simp
hoelzl@51527:   next
hoelzl@51527:     assume "y < x" hence "log b y < log b x"
hoelzl@51527:       using log_less_cancel_iff[OF `1 < b`] pos by simp
wenzelm@53079:     then show ?thesis using * by simp
wenzelm@53079:   qed
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@51527: lemma log_le_cancel_iff [simp]:
wenzelm@53079:   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (log a x \<le> log a y) = (x \<le> y)"
wenzelm@53079:   by (simp add: linorder_not_less [symmetric])
hoelzl@51527: 
hoelzl@51527: lemma zero_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < log a x \<longleftrightarrow> 1 < x"
hoelzl@51527:   using log_less_cancel_iff[of a 1 x] by simp
hoelzl@51527: 
hoelzl@51527: lemma zero_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 \<le> log a x \<longleftrightarrow> 1 \<le> x"
hoelzl@51527:   using log_le_cancel_iff[of a 1 x] by simp
hoelzl@51527: 
hoelzl@51527: lemma log_less_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 0 \<longleftrightarrow> x < 1"
hoelzl@51527:   using log_less_cancel_iff[of a x 1] by simp
hoelzl@51527: 
hoelzl@51527: lemma log_le_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 0 \<longleftrightarrow> x \<le> 1"
hoelzl@51527:   using log_le_cancel_iff[of a x 1] by simp
hoelzl@51527: 
hoelzl@51527: lemma one_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 < log a x \<longleftrightarrow> a < x"
hoelzl@51527:   using log_less_cancel_iff[of a a x] by simp
hoelzl@51527: 
hoelzl@51527: lemma one_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> log a x \<longleftrightarrow> a \<le> x"
hoelzl@51527:   using log_le_cancel_iff[of a a x] by simp
hoelzl@51527: 
hoelzl@51527: lemma log_less_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 1 \<longleftrightarrow> x < a"
hoelzl@51527:   using log_less_cancel_iff[of a x a] by simp
hoelzl@51527: 
hoelzl@51527: lemma log_le_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 1 \<longleftrightarrow> x \<le> a"
hoelzl@51527:   using log_le_cancel_iff[of a x a] by simp
hoelzl@51527: 
hoelzl@51527: lemma powr_realpow: "0 < x ==> x powr (real n) = x^n"
wenzelm@53079:   apply (induct n)
wenzelm@53079:   apply simp
hoelzl@51527:   apply (subgoal_tac "real(Suc n) = real n + 1")
hoelzl@51527:   apply (erule ssubst)
hoelzl@51527:   apply (subst powr_add, simp, simp)
wenzelm@53079:   done
hoelzl@51527: 
haftmann@54489: lemma powr_realpow_numeral: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x ^ (numeral n)"
haftmann@54489:   unfolding real_of_nat_numeral [symmetric] by (rule powr_realpow)
noschinl@52139: 
nipkow@57180: lemma powr2_sqrt[simp]: "0 < x \<Longrightarrow> sqrt x powr 2 = x"
nipkow@57180: by(simp add: powr_realpow_numeral)
nipkow@57180: 
hoelzl@51527: lemma powr_realpow2: "0 <= x ==> 0 < n ==> x^n = (if (x = 0) then 0 else x powr (real n))"
hoelzl@51527:   apply (case_tac "x = 0", simp, simp)
hoelzl@51527:   apply (rule powr_realpow [THEN sym], simp)
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma powr_int:
hoelzl@51527:   assumes "x > 0"
hoelzl@51527:   shows "x powr i = (if i \<ge> 0 then x ^ nat i else 1 / x ^ nat (-i))"
wenzelm@53079: proof (cases "i < 0")
wenzelm@53079:   case True
hoelzl@51527:   have r: "x powr i = 1 / x powr (-i)" by (simp add: powr_minus field_simps)
hoelzl@51527:   show ?thesis using `i < 0` `x > 0` by (simp add: r field_simps powr_realpow[symmetric])
wenzelm@53079: next
wenzelm@53079:   case False
wenzelm@53079:   then show ?thesis by (simp add: assms powr_realpow[symmetric])
wenzelm@53079: qed
hoelzl@51527: 
haftmann@54489: lemma powr_one: "0 < x \<Longrightarrow> x powr 1 = x"
haftmann@54489:   using powr_realpow [of x 1] by simp
haftmann@54489: 
haftmann@54489: lemma powr_numeral: "0 < x \<Longrightarrow> x powr numeral n = x ^ numeral n"
haftmann@54489:   by (fact powr_realpow_numeral)
haftmann@54489: 
haftmann@54489: lemma powr_neg_one: "0 < x \<Longrightarrow> x powr - 1 = 1 / x"
haftmann@54489:   using powr_int [of x "- 1"] by simp
haftmann@54489: 
haftmann@54489: lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr - numeral n = 1 / x ^ numeral n"
haftmann@54489:   using powr_int [of x "- numeral n"] by simp
hoelzl@51527: 
wenzelm@53079: lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
hoelzl@51527:   by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
hoelzl@51527: 
hoelzl@56483: lemma ln_powr: "ln (x powr y) = y * ln x"
hoelzl@56483:   by (simp add: powr_def)
hoelzl@56483: 
nipkow@56952: lemma ln_root: "\<lbrakk> n > 0; b > 0 \<rbrakk> \<Longrightarrow> ln (root n b) =  ln b / n"
nipkow@56952: by(simp add: root_powr_inverse ln_powr)
nipkow@56952: 
hoelzl@57275: lemma ln_sqrt: "0 < x \<Longrightarrow> ln (sqrt x) = ln x / 2"
haftmann@57512:   by (simp add: ln_powr powr_numeral ln_powr[symmetric] mult.commute)
hoelzl@57275: 
nipkow@56952: lemma log_root: "\<lbrakk> n > 0; a > 0 \<rbrakk> \<Longrightarrow> log b (root n a) =  log b a / n"
nipkow@56952: by(simp add: log_def ln_root)
nipkow@56952: 
hoelzl@56483: lemma log_powr: "log b (x powr y) = y * log b x"
hoelzl@56483:   by (simp add: log_def ln_powr)
hoelzl@56483: 
hoelzl@56483: lemma log_nat_power: "0 < x \<Longrightarrow> log b (x ^ n) = real n * log b x"
hoelzl@56483:   by (simp add: log_powr powr_realpow [symmetric])
hoelzl@56483: 
hoelzl@56483: lemma log_base_change: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log b x = log a x / log a b"
hoelzl@56483:   by (simp add: log_def)
hoelzl@56483: 
hoelzl@56483: lemma log_base_pow: "0 < a \<Longrightarrow> log (a ^ n) x = log a x / n"
hoelzl@56483:   by (simp add: log_def ln_realpow)
hoelzl@56483: 
hoelzl@56483: lemma log_base_powr: "log (a powr b) x = log a x / b"
hoelzl@56483:   by (simp add: log_def ln_powr)
hoelzl@51527: 
nipkow@56952: lemma log_base_root: "\<lbrakk> n > 0; b > 0 \<rbrakk> \<Longrightarrow> log (root n b) x = n * (log b x)"
nipkow@56952: by(simp add: log_def ln_root)
nipkow@56952: 
hoelzl@51527: lemma ln_bound: "1 <= x ==> ln x <= x"
hoelzl@51527:   apply (subgoal_tac "ln(1 + (x - 1)) <= x - 1")
hoelzl@51527:   apply simp
hoelzl@51527:   apply (rule ln_add_one_self_le_self, simp)
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma powr_mono: "a <= b ==> 1 <= x ==> x powr a <= x powr b"
wenzelm@53079:   apply (cases "x = 1", simp)
wenzelm@53079:   apply (cases "a = b", simp)
hoelzl@51527:   apply (rule order_less_imp_le)
hoelzl@51527:   apply (rule powr_less_mono, auto)
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma ge_one_powr_ge_zero: "1 <= x ==> 0 <= a ==> 1 <= x powr a"
hoelzl@51527:   apply (subst powr_zero_eq_one [THEN sym])
hoelzl@51527:   apply (rule powr_mono, assumption+)
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma powr_less_mono2: "0 < a ==> 0 < x ==> x < y ==> x powr a < y powr a"
hoelzl@51527:   apply (unfold powr_def)
hoelzl@51527:   apply (rule exp_less_mono)
hoelzl@51527:   apply (rule mult_strict_left_mono)
hoelzl@51527:   apply (subst ln_less_cancel_iff, assumption)
hoelzl@51527:   apply (rule order_less_trans)
hoelzl@51527:   prefer 2
hoelzl@51527:   apply assumption+
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma powr_less_mono2_neg: "a < 0 ==> 0 < x ==> x < y ==> y powr a < x powr a"
hoelzl@51527:   apply (unfold powr_def)
hoelzl@51527:   apply (rule exp_less_mono)
hoelzl@51527:   apply (rule mult_strict_left_mono_neg)
hoelzl@51527:   apply (subst ln_less_cancel_iff)
hoelzl@51527:   apply assumption
hoelzl@51527:   apply (rule order_less_trans)
hoelzl@51527:   prefer 2
hoelzl@51527:   apply assumption+
wenzelm@53079:   done
hoelzl@51527: 
hoelzl@51527: lemma powr_mono2: "0 <= a ==> 0 < x ==> x <= y ==> x powr a <= y powr a"
hoelzl@51527:   apply (case_tac "a = 0", simp)
hoelzl@51527:   apply (case_tac "x = y", simp)
paulson@54575:   apply (metis less_eq_real_def powr_less_mono2)
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
hoelzl@51527:   unfolding powr_def exp_inj_iff by simp
hoelzl@51527: 
hoelzl@51527: lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
haftmann@57512:   by (metis less_eq_real_def ln_less_self mult_imp_le_div_pos ln_powr mult.commute 
paulson@54575:             order.strict_trans2 powr_gt_zero zero_less_one)
hoelzl@51527: 
hoelzl@51527: lemma ln_powr_bound2:
hoelzl@51527:   assumes "1 < x" and "0 < a"
hoelzl@51527:   shows "(ln x) powr a <= (a powr a) * x"
hoelzl@51527: proof -
hoelzl@51527:   from assms have "ln x <= (x powr (1 / a)) / (1 / a)"
paulson@54575:     by (metis less_eq_real_def ln_powr_bound zero_less_divide_1_iff)
hoelzl@51527:   also have "... = a * (x powr (1 / a))"
hoelzl@51527:     by simp
hoelzl@51527:   finally have "(ln x) powr a <= (a * (x powr (1 / a))) powr a"
paulson@54575:     by (metis assms less_imp_le ln_gt_zero powr_mono2)
hoelzl@51527:   also have "... = (a powr a) * ((x powr (1 / a)) powr a)"
paulson@54575:     by (metis assms(2) powr_mult powr_gt_zero)
hoelzl@51527:   also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
hoelzl@51527:     by (rule powr_powr)
paulson@54575:   also have "... = x" using assms
paulson@54575:     by auto
hoelzl@51527:   finally show ?thesis .
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@51527: lemma tendsto_powr [tendsto_intros]:
hoelzl@57275:   "\<lbrakk>(f ---> a) F; (g ---> b) F; a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x powr g x) ---> a powr b) F"
hoelzl@51527:   unfolding powr_def by (intro tendsto_intros)
hoelzl@51527: 
hoelzl@51527: lemma continuous_powr:
wenzelm@53079:   assumes "continuous F f"
wenzelm@53079:     and "continuous F g"
hoelzl@57275:     and "f (Lim F (\<lambda>x. x)) \<noteq> 0"
hoelzl@51527:   shows "continuous F (\<lambda>x. (f x) powr (g x))"
hoelzl@51527:   using assms unfolding continuous_def by (rule tendsto_powr)
hoelzl@51527: 
hoelzl@51527: lemma continuous_at_within_powr[continuous_intros]:
wenzelm@53079:   assumes "continuous (at a within s) f"
wenzelm@53079:     and "continuous (at a within s) g"
hoelzl@57275:     and "f a \<noteq> 0"
hoelzl@51527:   shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
hoelzl@51527:   using assms unfolding continuous_within by (rule tendsto_powr)
hoelzl@51527: 
hoelzl@51527: lemma isCont_powr[continuous_intros, simp]:
hoelzl@57275:   assumes "isCont f a" "isCont g a" "f a \<noteq> 0"
hoelzl@51527:   shows "isCont (\<lambda>x. (f x) powr g x) a"
hoelzl@51527:   using assms unfolding continuous_at by (rule tendsto_powr)
hoelzl@51527: 
hoelzl@56371: lemma continuous_on_powr[continuous_intros]:
hoelzl@57275:   assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. f x \<noteq> 0"
hoelzl@51527:   shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
hoelzl@51527:   using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
hoelzl@51527: 
hoelzl@51527: (* FIXME: generalize by replacing d by with g x and g ---> d? *)
hoelzl@51527: lemma tendsto_zero_powrI:
hoelzl@51527:   assumes "eventually (\<lambda>x. 0 < f x ) F" and "(f ---> 0) F"
wenzelm@53079:     and "0 < d"
hoelzl@51527:   shows "((\<lambda>x. f x powr d) ---> 0) F"
hoelzl@51527: proof (rule tendstoI)
hoelzl@51527:   fix e :: real assume "0 < e"
hoelzl@51527:   def Z \<equiv> "e powr (1 / d)"
hoelzl@51527:   with `0 < e` have "0 < Z" by simp
hoelzl@51527:   with assms have "eventually (\<lambda>x. 0 < f x \<and> dist (f x) 0 < Z) F"
hoelzl@51527:     by (intro eventually_conj tendstoD)
hoelzl@51527:   moreover
hoelzl@51527:   from assms have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> x powr d < Z powr d"
hoelzl@51527:     by (intro powr_less_mono2) (auto simp: dist_real_def)
hoelzl@51527:   with assms `0 < e` have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> dist (x powr d) 0 < e"
hoelzl@51527:     unfolding dist_real_def Z_def by (auto simp: powr_powr)
hoelzl@51527:   ultimately
hoelzl@51527:   show "eventually (\<lambda>x. dist (f x powr d) 0 < e) F" by (rule eventually_elim1)
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@51527: lemma tendsto_neg_powr:
wenzelm@53079:   assumes "s < 0"
wenzelm@53079:     and "LIM x F. f x :> at_top"
hoelzl@51527:   shows "((\<lambda>x. f x powr s) ---> 0) F"
hoelzl@51527: proof (rule tendstoI)
hoelzl@51527:   fix e :: real assume "0 < e"
hoelzl@51527:   def Z \<equiv> "e powr (1 / s)"
hoelzl@51527:   from assms have "eventually (\<lambda>x. Z < f x) F"
hoelzl@51527:     by (simp add: filterlim_at_top_dense)
hoelzl@51527:   moreover
hoelzl@51527:   from assms have "\<And>x. Z < x \<Longrightarrow> x powr s < Z powr s"
hoelzl@51527:     by (auto simp: Z_def intro!: powr_less_mono2_neg)
hoelzl@51527:   with assms `0 < e` have "\<And>x. Z < x \<Longrightarrow> dist (x powr s) 0 < e"
hoelzl@51527:     by (simp add: powr_powr Z_def dist_real_def)
hoelzl@51527:   ultimately
hoelzl@51527:   show "eventually (\<lambda>x. dist (f x powr s) 0 < e) F" by (rule eventually_elim1)
hoelzl@51527: qed
hoelzl@51527: 
hoelzl@57275: (* it is funny that this isn't in the library! It could go in Transcendental *)
hoelzl@57275: lemma tendsto_exp_limit_at_right:
hoelzl@57275:   fixes x :: real
hoelzl@57275:   shows "((\<lambda>y. (1 + x * y) powr (1 / y)) ---> exp x) (at_right 0)"
hoelzl@57275: proof cases
hoelzl@57275:   assume "x \<noteq> 0"
hoelzl@57275: 
hoelzl@57275:   have "((\<lambda>y. ln (1 + x * y)::real) has_real_derivative 1 * x) (at 0)"
hoelzl@57275:     by (auto intro!: derivative_eq_intros)
hoelzl@57275:   then have "((\<lambda>y. ln (1 + x * y) / y) ---> x) (at 0)"
hoelzl@57275:     by (auto simp add: has_field_derivative_def field_has_derivative_at) 
hoelzl@57275:   then have *: "((\<lambda>y. exp (ln (1 + x * y) / y)) ---> exp x) (at 0)"
hoelzl@57275:     by (rule tendsto_intros)
hoelzl@57275:   then show ?thesis
hoelzl@57275:   proof (rule filterlim_mono_eventually)
hoelzl@57275:     show "eventually (\<lambda>xa. exp (ln (1 + x * xa) / xa) = (1 + x * xa) powr (1 / xa)) (at_right 0)"
hoelzl@57275:       unfolding eventually_at_right[OF zero_less_one]
hoelzl@57275:       using `x \<noteq> 0` by (intro exI[of _ "1 / \<bar>x\<bar>"]) (auto simp: field_simps powr_def)
hoelzl@57275:   qed (simp_all add: at_eq_sup_left_right)
hoelzl@57275: qed (simp add: tendsto_const)
hoelzl@57275: 
hoelzl@57275: lemma tendsto_exp_limit_at_top:
hoelzl@57275:   fixes x :: real
hoelzl@57275:   shows "((\<lambda>y. (1 + x / y) powr y) ---> exp x) at_top"
hoelzl@57275:   apply (subst filterlim_at_top_to_right)
hoelzl@57275:   apply (simp add: inverse_eq_divide)
hoelzl@57275:   apply (rule tendsto_exp_limit_at_right)
hoelzl@57275:   done
hoelzl@57275: 
hoelzl@57275: lemma tendsto_exp_limit_sequentially:
hoelzl@57275:   fixes x :: real
hoelzl@57275:   shows "(\<lambda>n. (1 + x / n) ^ n) ----> exp x"
hoelzl@57275: proof (rule filterlim_mono_eventually)
hoelzl@57275:   from reals_Archimedean2 [of "abs x"] obtain n :: nat where *: "real n > abs x" ..
hoelzl@57275:   hence "eventually (\<lambda>n :: nat. 0 < 1 + x / real n) at_top"
hoelzl@57275:     apply (intro eventually_sequentiallyI [of n])
hoelzl@57275:     apply (case_tac "x \<ge> 0")
hoelzl@57275:     apply (rule add_pos_nonneg, auto intro: divide_nonneg_nonneg)
hoelzl@57275:     apply (subgoal_tac "x / real xa > -1")
hoelzl@57275:     apply (auto simp add: field_simps)
hoelzl@57275:     done
hoelzl@57275:   then show "eventually (\<lambda>n. (1 + x / n) powr n = (1 + x / n) ^ n) at_top"
hoelzl@57275:     by (rule eventually_elim1) (erule powr_realpow)
hoelzl@57275:   show "(\<lambda>n. (1 + x / real n) powr real n) ----> exp x"
hoelzl@57275:     by (rule filterlim_compose [OF tendsto_exp_limit_at_top filterlim_real_sequentially])
hoelzl@57275: qed auto
hoelzl@57275: 
huffman@29164: subsection {* Sine and Cosine *}
huffman@29164: 
huffman@44308: definition sin_coeff :: "nat \<Rightarrow> real" where
huffman@31271:   "sin_coeff = (\<lambda>n. if even n then 0 else -1 ^ ((n - Suc 0) div 2) / real (fact n))"
huffman@31271: 
huffman@44308: definition cos_coeff :: "nat \<Rightarrow> real" where
huffman@31271:   "cos_coeff = (\<lambda>n. if even n then (-1 ^ (n div 2)) / real (fact n) else 0)"
huffman@31271: 
wenzelm@53079: definition sin :: "real \<Rightarrow> real"
wenzelm@53079:   where "sin = (\<lambda>x. \<Sum>n. sin_coeff n * x ^ n)"
wenzelm@53079: 
wenzelm@53079: definition cos :: "real \<Rightarrow> real"
wenzelm@53079:   where "cos = (\<lambda>x. \<Sum>n. cos_coeff n * x ^ n)"
huffman@31271: 
huffman@44319: lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
huffman@44319:   unfolding sin_coeff_def by simp
huffman@44319: 
huffman@44319: lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
huffman@44319:   unfolding cos_coeff_def by simp
huffman@44319: 
huffman@44319: lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
huffman@44319:   unfolding cos_coeff_def sin_coeff_def
huffman@44319:   by (simp del: mult_Suc)
huffman@44319: 
huffman@44319: lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
huffman@44319:   unfolding cos_coeff_def sin_coeff_def
hoelzl@56479:   by (simp del: mult_Suc, auto simp add: odd_Suc_mult_two_ex)
huffman@44319: 
huffman@31271: lemma summable_sin: "summable (\<lambda>n. sin_coeff n * x ^ n)"
wenzelm@53079:   unfolding sin_coeff_def
wenzelm@53079:   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
wenzelm@53079:   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
wenzelm@53079:   done
huffman@29164: 
huffman@31271: lemma summable_cos: "summable (\<lambda>n. cos_coeff n * x ^ n)"
wenzelm@53079:   unfolding cos_coeff_def
wenzelm@53079:   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
wenzelm@53079:   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
wenzelm@53079:   done
huffman@29164: 
huffman@31271: lemma sin_converges: "(\<lambda>n. sin_coeff n * x ^ n) sums sin(x)"
wenzelm@53079:   unfolding sin_def by (rule summable_sin [THEN summable_sums])
huffman@29164: 
huffman@31271: lemma cos_converges: "(\<lambda>n. cos_coeff n * x ^ n) sums cos(x)"
wenzelm@53079:   unfolding cos_def by (rule summable_cos [THEN summable_sums])
huffman@29164: 
huffman@44319: lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
huffman@44319:   by (simp add: diffs_def sin_coeff_Suc real_of_nat_def del: of_nat_Suc)
huffman@44319: 
huffman@44319: lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
hoelzl@56479:   by (simp add: diffs_def cos_coeff_Suc real_of_nat_def del: of_nat_Suc)
huffman@29164: 
huffman@29164: text{*Now at last we can get the derivatives of exp, sin and cos*}
huffman@29164: 
huffman@29164: lemma DERIV_sin [simp]: "DERIV sin x :> cos(x)"
huffman@44319:   unfolding sin_def cos_def
huffman@44319:   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
huffman@44319:   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff
huffman@44319:     summable_minus summable_sin summable_cos)
huffman@44319:   done
huffman@29164: 
hoelzl@56381: declare DERIV_sin[THEN DERIV_chain2, derivative_intros]
hoelzl@51527: 
huffman@29164: lemma DERIV_cos [simp]: "DERIV cos x :> -sin(x)"
huffman@44319:   unfolding cos_def sin_def
huffman@44319:   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
huffman@44319:   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff diffs_minus
huffman@44319:     summable_minus summable_sin summable_cos suminf_minus)
huffman@44319:   done
huffman@29164: 
hoelzl@56381: declare DERIV_cos[THEN DERIV_chain2, derivative_intros]
hoelzl@51527: 
huffman@44311: lemma isCont_sin: "isCont sin x"
huffman@44311:   by (rule DERIV_sin [THEN DERIV_isCont])
huffman@44311: 
huffman@44311: lemma isCont_cos: "isCont cos x"
huffman@44311:   by (rule DERIV_cos [THEN DERIV_isCont])
huffman@44311: 
huffman@44311: lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
huffman@44311:   by (rule isCont_o2 [OF _ isCont_sin])
huffman@44311: 
huffman@44311: lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
huffman@44311:   by (rule isCont_o2 [OF _ isCont_cos])
huffman@44311: 
huffman@44311: lemma tendsto_sin [tendsto_intros]:
huffman@44311:   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) ---> sin a) F"
huffman@44311:   by (rule isCont_tendsto_compose [OF isCont_sin])
huffman@44311: 
huffman@44311: lemma tendsto_cos [tendsto_intros]:
huffman@44311:   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) ---> cos a) F"
huffman@44311:   by (rule isCont_tendsto_compose [OF isCont_cos])
huffman@29164: 
hoelzl@51478: lemma continuous_sin [continuous_intros]:
hoelzl@51478:   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_sin)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_sin [continuous_intros]:
hoelzl@51478:   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_sin)
hoelzl@51478: 
hoelzl@51478: lemma continuous_cos [continuous_intros]:
hoelzl@51478:   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_cos)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_cos [continuous_intros]:
hoelzl@51478:   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_cos)
hoelzl@51478: 
huffman@29164: subsection {* Properties of Sine and Cosine *}
paulson@15077: 
paulson@15077: lemma sin_zero [simp]: "sin 0 = 0"
huffman@44311:   unfolding sin_def sin_coeff_def by (simp add: powser_zero)
paulson@15077: 
paulson@15077: lemma cos_zero [simp]: "cos 0 = 1"
huffman@44311:   unfolding cos_def cos_coeff_def by (simp add: powser_zero)
paulson@15077: 
wenzelm@53015: lemma sin_cos_squared_add [simp]: "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
huffman@44308: proof -
wenzelm@53015:   have "\<forall>x. DERIV (\<lambda>x. (sin x)\<^sup>2 + (cos x)\<^sup>2) x :> 0"
hoelzl@56381:     by (auto intro!: derivative_eq_intros)
wenzelm@53015:   hence "(sin x)\<^sup>2 + (cos x)\<^sup>2 = (sin 0)\<^sup>2 + (cos 0)\<^sup>2"
huffman@44308:     by (rule DERIV_isconst_all)
wenzelm@53015:   thus "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1" by simp
huffman@44308: qed
huffman@44308: 
wenzelm@53015: lemma sin_cos_squared_add2 [simp]: "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
haftmann@57512:   by (subst add.commute, rule sin_cos_squared_add)
paulson@15077: 
paulson@15077: lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
huffman@44308:   using sin_cos_squared_add2 [unfolded power2_eq_square] .
paulson@15077: 
wenzelm@53015: lemma sin_squared_eq: "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
huffman@44308:   unfolding eq_diff_eq by (rule sin_cos_squared_add)
paulson@15077: 
wenzelm@53015: lemma cos_squared_eq: "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
huffman@44308:   unfolding eq_diff_eq by (rule sin_cos_squared_add2)
paulson@15077: 
paulson@15081: lemma abs_sin_le_one [simp]: "\<bar>sin x\<bar> \<le> 1"
huffman@44308:   by (rule power2_le_imp_le, simp_all add: sin_squared_eq)
paulson@15077: 
paulson@15077: lemma sin_ge_minus_one [simp]: "-1 \<le> sin x"
huffman@44308:   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
paulson@15077: 
paulson@15077: lemma sin_le_one [simp]: "sin x \<le> 1"
huffman@44308:   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
paulson@15077: 
paulson@15081: lemma abs_cos_le_one [simp]: "\<bar>cos x\<bar> \<le> 1"
huffman@44308:   by (rule power2_le_imp_le, simp_all add: cos_squared_eq)
paulson@15077: 
paulson@15077: lemma cos_ge_minus_one [simp]: "-1 \<le> cos x"
huffman@44308:   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
paulson@15077: 
paulson@15077: lemma cos_le_one [simp]: "cos x \<le> 1"
huffman@44308:   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
paulson@15077: 
hoelzl@41970: lemma DERIV_fun_pow: "DERIV g x :> m ==>
wenzelm@53079:       DERIV (\<lambda>x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
hoelzl@56381:   by (auto intro!: derivative_eq_intros simp: real_of_nat_def)
paulson@15077: 
paulson@15229: lemma DERIV_fun_exp:
wenzelm@53079:      "DERIV g x :> m ==> DERIV (\<lambda>x. exp(g x)) x :> exp(g x) * m"
hoelzl@56381:   by (auto intro!: derivative_intros)
paulson@15077: 
paulson@15229: lemma DERIV_fun_sin:
wenzelm@53079:      "DERIV g x :> m ==> DERIV (\<lambda>x. sin(g x)) x :> cos(g x) * m"
hoelzl@56381:   by (auto intro!: derivative_intros)
paulson@15077: 
paulson@15229: lemma DERIV_fun_cos:
wenzelm@53079:      "DERIV g x :> m ==> DERIV (\<lambda>x. cos(g x)) x :> -sin(g x) * m"
hoelzl@56381:   by (auto intro!: derivative_eq_intros simp: real_of_nat_def)
paulson@15077: 
huffman@44308: lemma sin_cos_add_lemma:
wenzelm@53079:   "(sin (x + y) - (sin x * cos y + cos x * sin y))\<^sup>2 +
wenzelm@53079:     (cos (x + y) - (cos x * cos y - sin x * sin y))\<^sup>2 = 0"
huffman@44308:   (is "?f x = 0")
huffman@44308: proof -
huffman@44308:   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
hoelzl@56381:     by (auto intro!: derivative_eq_intros simp add: algebra_simps)
huffman@44308:   hence "?f x = ?f 0"
huffman@44308:     by (rule DERIV_isconst_all)
huffman@44308:   thus ?thesis by simp
huffman@44308: qed
paulson@15077: 
paulson@15077: lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
huffman@44308:   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
paulson@15077: 
paulson@15077: lemma cos_add: "cos (x + y) = cos x * cos y - sin x * sin y"
huffman@44308:   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
huffman@44308: 
huffman@44308: lemma sin_cos_minus_lemma:
wenzelm@53015:   "(sin(-x) + sin(x))\<^sup>2 + (cos(-x) - cos(x))\<^sup>2 = 0" (is "?f x = 0")
huffman@44308: proof -
huffman@44308:   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
hoelzl@56381:     by (auto intro!: derivative_eq_intros simp add: algebra_simps)
huffman@44308:   hence "?f x = ?f 0"
huffman@44308:     by (rule DERIV_isconst_all)
huffman@44308:   thus ?thesis by simp
huffman@44308: qed
paulson@15077: 
paulson@15077: lemma sin_minus [simp]: "sin (-x) = -sin(x)"
huffman@44308:   using sin_cos_minus_lemma [where x=x] by simp
paulson@15077: 
paulson@15077: lemma cos_minus [simp]: "cos (-x) = cos(x)"
huffman@44308:   using sin_cos_minus_lemma [where x=x] by simp
paulson@15077: 
paulson@15077: lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
haftmann@54230:   using sin_add [of x "- y"] by simp
paulson@15077: 
paulson@15077: lemma sin_diff2: "sin (x - y) = cos y * sin x - sin y * cos x"
haftmann@57512:   by (simp add: sin_diff mult.commute)
paulson@15077: 
paulson@15077: lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
haftmann@54230:   using cos_add [of x "- y"] by simp
paulson@15077: 
paulson@15077: lemma cos_diff2: "cos (x - y) = cos y * cos x + sin y * sin x"
haftmann@57512:   by (simp add: cos_diff mult.commute)
paulson@15077: 
paulson@15077: lemma sin_double [simp]: "sin(2 * x) = 2* sin x * cos x"
huffman@29165:   using sin_add [where x=x and y=x] by simp
paulson@15077: 
wenzelm@53015: lemma cos_double: "cos(2* x) = ((cos x)\<^sup>2) - ((sin x)\<^sup>2)"
huffman@29165:   using cos_add [where x=x and y=x]
huffman@29165:   by (simp add: power2_eq_square)
paulson@15077: 
hoelzl@57275: lemma sin_x_le_x: assumes x: "x \<ge> 0" shows "sin x \<le> x"
hoelzl@57275: proof -
hoelzl@57275:   let ?f = "\<lambda>x. x - sin x"
hoelzl@57275:   from x have "?f x \<ge> ?f 0"
hoelzl@57275:     apply (rule DERIV_nonneg_imp_nondecreasing)
hoelzl@57275:     apply (intro allI impI exI[of _ "1 - cos x" for x])
hoelzl@57275:     apply (auto intro!: derivative_eq_intros simp: field_simps)
hoelzl@57275:     done
hoelzl@57275:   thus "sin x \<le> x" by simp
hoelzl@57275: qed
hoelzl@57275: 
hoelzl@57275: lemma sin_x_ge_neg_x: assumes x: "x \<ge> 0" shows "sin x \<ge> - x"
hoelzl@57275: proof -
hoelzl@57275:   let ?f = "\<lambda>x. x + sin x"
hoelzl@57275:   from x have "?f x \<ge> ?f 0"
hoelzl@57275:     apply (rule DERIV_nonneg_imp_nondecreasing)
hoelzl@57275:     apply (intro allI impI exI[of _ "1 + cos x" for x])
hoelzl@57275:     apply (auto intro!: derivative_eq_intros simp: field_simps real_0_le_add_iff)
hoelzl@57275:     done
hoelzl@57275:   thus "sin x \<ge> -x" by simp
hoelzl@57275: qed
hoelzl@57275: 
hoelzl@57275: lemma abs_sin_x_le_abs_x: "\<bar>sin x\<bar> \<le> \<bar>x\<bar>"
hoelzl@57275:   using sin_x_ge_neg_x [of x] sin_x_le_x [of x] sin_x_ge_neg_x [of "-x"] sin_x_le_x [of "-x"]
hoelzl@57275:   by (auto simp: abs_real_def)
paulson@15077: 
huffman@29164: subsection {* The Constant Pi *}
paulson@15077: 
wenzelm@53079: definition pi :: real
wenzelm@53079:   where "pi = 2 * (THE x. 0 \<le> (x::real) & x \<le> 2 & cos x = 0)"
huffman@23043: 
hoelzl@41970: text{*Show that there's a least positive @{term x} with @{term "cos(x) = 0"};
paulson@15077:    hence define pi.*}
paulson@15077: 
paulson@15077: lemma sin_paired:
wenzelm@53079:   "(\<lambda>n. -1 ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) sums  sin x"
paulson@15077: proof -
huffman@31271:   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
huffman@44727:     by (rule sin_converges [THEN sums_group], simp)
haftmann@57514:   thus ?thesis unfolding One_nat_def sin_coeff_def by (simp add: ac_simps)
paulson@15077: qed
paulson@15077: 
huffman@44728: lemma sin_gt_zero:
wenzelm@53079:   assumes "0 < x" and "x < 2"
wenzelm@53079:   shows "0 < sin x"
huffman@44728: proof -
huffman@44728:   let ?f = "\<lambda>n. \<Sum>k = n*2..<n*2+2. -1 ^ k / real (fact (2*k+1)) * x^(2*k+1)"
huffman@44728:   have pos: "\<forall>n. 0 < ?f n"
huffman@44728:   proof
huffman@44728:     fix n :: nat
huffman@44728:     let ?k2 = "real (Suc (Suc (4 * n)))"
huffman@44728:     let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
huffman@44728:     have "x * x < ?k2 * ?k3"
huffman@44728:       using assms by (intro mult_strict_mono', simp_all)
huffman@44728:     hence "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
huffman@44728:       by (intro mult_strict_right_mono zero_less_power `0 < x`)
huffman@44728:     thus "0 < ?f n"
huffman@44728:       by (simp del: mult_Suc,
nipkow@56544:         simp add: less_divide_eq field_simps del: mult_Suc)
huffman@44728:   qed
huffman@44728:   have sums: "?f sums sin x"
huffman@44728:     by (rule sin_paired [THEN sums_group], simp)
huffman@44728:   show "0 < sin x"
huffman@44728:     unfolding sums_unique [OF sums]
huffman@44728:     using sums_summable [OF sums] pos
hoelzl@56213:     by (rule suminf_pos)
huffman@44728: qed
paulson@15077: 
wenzelm@53079: lemma cos_double_less_one: "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
wenzelm@53079:   using sin_gt_zero [where x = x] by (auto simp add: cos_squared_eq cos_double)
wenzelm@53079: 
wenzelm@53079: lemma cos_paired: "(\<lambda>n. -1 ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
paulson@15077: proof -
huffman@31271:   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
huffman@44727:     by (rule cos_converges [THEN sums_group], simp)
haftmann@57514:   thus ?thesis unfolding cos_coeff_def by (simp add: ac_simps)
paulson@15077: qed
paulson@15077: 
haftmann@53602: lemmas realpow_num_eq_if = power_eq_if
haftmann@53602: 
hoelzl@56193: lemma sumr_pos_lt_pair:
hoelzl@56193:   fixes f :: "nat \<Rightarrow> real"
hoelzl@56193:   shows "\<lbrakk>summable f;
hoelzl@56193:         \<And>d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc(Suc 0) * d) + 1))\<rbrakk>
hoelzl@56193:       \<Longrightarrow> setsum f {..<k} < suminf f"
hoelzl@56193: unfolding One_nat_def
hoelzl@56193: apply (subst suminf_split_initial_segment [where k="k"])
hoelzl@56193: apply assumption
hoelzl@56193: apply simp
hoelzl@56193: apply (drule_tac k="k" in summable_ignore_initial_segment)
hoelzl@56193: apply (drule_tac k="Suc (Suc 0)" in sums_group [OF summable_sums], simp)
hoelzl@56193: apply simp
hoelzl@56193: apply (frule sums_unique)
hoelzl@56193: apply (drule sums_summable)
hoelzl@56193: apply simp
hoelzl@56213: apply (erule suminf_pos)
haftmann@57514: apply (simp add: ac_simps)
hoelzl@56193: done
hoelzl@56193: 
haftmann@53602: lemma cos_two_less_zero [simp]:
haftmann@53602:   "cos 2 < 0"
haftmann@53602: proof -
haftmann@53602:   note fact_Suc [simp del]
haftmann@53602:   from cos_paired
haftmann@53602:   have "(\<lambda>n. - (-1 ^ n / real (fact (2 * n)) * 2 ^ (2 * n))) sums - cos 2"
haftmann@53602:     by (rule sums_minus)
haftmann@53602:   then have *: "(\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n)))) sums - cos 2"
haftmann@53602:     by simp
haftmann@53602:   then have **: "summable (\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
haftmann@53602:     by (rule sums_summable)
hoelzl@56193:   have "0 < (\<Sum>n<Suc (Suc (Suc 0)). - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
haftmann@53602:     by (simp add: fact_num_eq_if_nat realpow_num_eq_if)
hoelzl@56193:   moreover have "(\<Sum>n<Suc (Suc (Suc 0)). - (-1 ^ n  * 2 ^ (2 * n) / real (fact (2 * n))))
haftmann@53602:     < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
haftmann@53602:   proof -
haftmann@53602:     { fix d
haftmann@53602:       have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
haftmann@53602:        < real (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))) *
haftmann@53602:            fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
haftmann@53602:         by (simp only: real_of_nat_mult) (auto intro!: mult_strict_mono fact_less_mono_nat)
haftmann@53602:       then have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
haftmann@53602:         < real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))))"
haftmann@53602:         by (simp only: fact_Suc [of "Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))"])
haftmann@53602:       then have "4 * inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))))
haftmann@53602:         < inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
haftmann@53602:         by (simp add: inverse_eq_divide less_divide_eq)
haftmann@53602:     }
haftmann@53602:     note *** = this
haftmann@54230:     have [simp]: "\<And>x y::real. 0 < x - y \<longleftrightarrow> y < x" by arith
haftmann@53602:     from ** show ?thesis by (rule sumr_pos_lt_pair)
haftmann@57512:       (simp add: divide_inverse mult.assoc [symmetric] ***)
haftmann@53602:   qed
haftmann@53602:   ultimately have "0 < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
haftmann@53602:     by (rule order_less_trans)
haftmann@53602:   moreover from * have "- cos 2 = (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
haftmann@53602:     by (rule sums_unique)
haftmann@53602:   ultimately have "0 < - cos 2" by simp
haftmann@53602:   then show ?thesis by simp
haftmann@53602: qed
huffman@23053: 
huffman@23053: lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
huffman@23053: lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]
paulson@15077: 
wenzelm@53079: lemma cos_is_zero: "EX! x. 0 \<le> x & x \<le> 2 \<and> cos x = 0"
huffman@44730: proof (rule ex_ex1I)
huffman@44730:   show "\<exists>x. 0 \<le> x & x \<le> 2 & cos x = 0"
huffman@44730:     by (rule IVT2, simp_all)
huffman@44730: next
huffman@44730:   fix x y
huffman@44730:   assume x: "0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
huffman@44730:   assume y: "0 \<le> y \<and> y \<le> 2 \<and> cos y = 0"
hoelzl@56181:   have [simp]: "\<forall>x. cos differentiable (at x)"
hoelzl@56181:     unfolding real_differentiable_def by (auto intro: DERIV_cos)
huffman@44730:   from x y show "x = y"
huffman@44730:     apply (cut_tac less_linear [of x y], auto)
huffman@44730:     apply (drule_tac f = cos in Rolle)
huffman@44730:     apply (drule_tac [5] f = cos in Rolle)
huffman@44730:     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
huffman@44730:     apply (metis order_less_le_trans less_le sin_gt_zero)
huffman@44730:     apply (metis order_less_le_trans less_le sin_gt_zero)
huffman@44730:     done
huffman@44730: qed
hoelzl@31880: 
huffman@23053: lemma pi_half: "pi/2 = (THE x. 0 \<le> x & x \<le> 2 & cos x = 0)"
wenzelm@53079:   by (simp add: pi_def)
paulson@15077: 
paulson@15077: lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
wenzelm@53079:   by (simp add: pi_half cos_is_zero [THEN theI'])
huffman@23053: 
huffman@23053: lemma pi_half_gt_zero [simp]: "0 < pi / 2"
wenzelm@53079:   apply (rule order_le_neq_trans)
wenzelm@53079:   apply (simp add: pi_half cos_is_zero [THEN theI'])
paulson@54575:   apply (metis cos_pi_half cos_zero zero_neq_one)
wenzelm@53079:   done
paulson@15077: 
huffman@23053: lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
huffman@23053: lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]
paulson@15077: 
huffman@23053: lemma pi_half_less_two [simp]: "pi / 2 < 2"
wenzelm@53079:   apply (rule order_le_neq_trans)
wenzelm@53079:   apply (simp add: pi_half cos_is_zero [THEN theI'])
paulson@54575:   apply (metis cos_pi_half cos_two_neq_zero)
wenzelm@53079:   done
huffman@23053: 
huffman@23053: lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
huffman@23053: lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]
paulson@15077: 
paulson@15077: lemma pi_gt_zero [simp]: "0 < pi"
wenzelm@53079:   using pi_half_gt_zero by simp
huffman@23053: 
huffman@23053: lemma pi_ge_zero [simp]: "0 \<le> pi"
wenzelm@53079:   by (rule pi_gt_zero [THEN order_less_imp_le])
paulson@15077: 
paulson@15077: lemma pi_neq_zero [simp]: "pi \<noteq> 0"
wenzelm@53079:   by (rule pi_gt_zero [THEN less_imp_neq, symmetric])
paulson@15077: 
huffman@23053: lemma pi_not_less_zero [simp]: "\<not> pi < 0"
wenzelm@53079:   by (simp add: linorder_not_less)
paulson@15077: 
huffman@29165: lemma minus_pi_half_less_zero: "-(pi/2) < 0"
wenzelm@53079:   by simp
paulson@15077: 
hoelzl@29803: lemma m2pi_less_pi: "- (2 * pi) < pi"
wenzelm@53079:   by simp
hoelzl@29803: 
paulson@15077: lemma sin_pi_half [simp]: "sin(pi/2) = 1"
wenzelm@53079:   using sin_cos_squared_add2 [where x = "pi/2"]
wenzelm@53079:   using sin_gt_zero [OF pi_half_gt_zero pi_half_less_two]
wenzelm@53079:   by (simp add: power2_eq_1_iff)
paulson@15077: 
paulson@15077: lemma cos_pi [simp]: "cos pi = -1"
wenzelm@53079:   using cos_add [where x = "pi/2" and y = "pi/2"] by simp
paulson@15077: 
paulson@15077: lemma sin_pi [simp]: "sin pi = 0"
wenzelm@53079:   using sin_add [where x = "pi/2" and y = "pi/2"] by simp
paulson@15077: 
paulson@15077: lemma sin_cos_eq: "sin x = cos (pi/2 - x)"
wenzelm@53079:   by (simp add: cos_diff)
paulson@15077: 
paulson@15077: lemma minus_sin_cos_eq: "-sin x = cos (x + pi/2)"
wenzelm@53079:   by (simp add: cos_add)
paulson@15077: 
paulson@15077: lemma cos_sin_eq: "cos x = sin (pi/2 - x)"
wenzelm@53079:   by (simp add: sin_diff)
paulson@15077: 
paulson@15077: lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
wenzelm@53079:   by (simp add: sin_add)
paulson@15077: 
paulson@15077: lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
wenzelm@53079:   by (simp add: sin_add)
paulson@15077: 
paulson@15077: lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
wenzelm@53079:   by (simp add: cos_add)
paulson@15077: 
paulson@15077: lemma sin_periodic [simp]: "sin (x + 2*pi) = sin x"
wenzelm@53079:   by (simp add: sin_add cos_double)
paulson@15077: 
paulson@15077: lemma cos_periodic [simp]: "cos (x + 2*pi) = cos x"
wenzelm@53079:   by (simp add: cos_add cos_double)
paulson@15077: 
paulson@15077: lemma cos_npi [simp]: "cos (real n * pi) = -1 ^ n"
wenzelm@53079:   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
paulson@15077: 
paulson@15383: lemma cos_npi2 [simp]: "cos (pi * real n) = -1 ^ n"
haftmann@57512:   by (metis cos_npi mult.commute)
paulson@15383: 
paulson@15077: lemma sin_npi [simp]: "sin (real (n::nat) * pi) = 0"
wenzelm@53079:   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
paulson@15077: 
paulson@15077: lemma sin_npi2 [simp]: "sin (pi * real (n::nat)) = 0"
haftmann@57512:   by (simp add: mult.commute [of pi])
paulson@15077: 
paulson@15077: lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
wenzelm@53079:   by (simp add: cos_double)
paulson@15077: 
paulson@15077: lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
wenzelm@53079:   by simp
paulson@15077: 
paulson@15077: lemma sin_gt_zero2: "[| 0 < x; x < pi/2 |] ==> 0 < sin x"
paulson@54575:   by (metis sin_gt_zero order_less_trans pi_half_less_two)
paulson@15077: 
hoelzl@41970: lemma sin_less_zero:
wenzelm@53079:   assumes "- pi/2 < x" and "x < 0"
wenzelm@53079:   shows "sin x < 0"
paulson@15077: proof -
hoelzl@41970:   have "0 < sin (- x)" using assms by (simp only: sin_gt_zero2)
paulson@15077:   thus ?thesis by simp
paulson@15077: qed
paulson@15077: 
paulson@15077: lemma pi_less_4: "pi < 4"
wenzelm@53079:   using pi_half_less_two by auto
paulson@15077: 
paulson@15077: lemma cos_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < cos x"
wenzelm@53079:   apply (cut_tac pi_less_4)
wenzelm@53079:   apply (cut_tac f = cos and a = 0 and b = x and y = 0 in IVT2_objl, safe, simp_all)
wenzelm@53079:   apply (cut_tac cos_is_zero, safe)
wenzelm@53079:   apply (rename_tac y z)
wenzelm@53079:   apply (drule_tac x = y in spec)
wenzelm@53079:   apply (drule_tac x = "pi/2" in spec, simp)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma cos_gt_zero_pi: "[| -(pi/2) < x; x < pi/2 |] ==> 0 < cos x"
wenzelm@53079:   apply (rule_tac x = x and y = 0 in linorder_cases)
paulson@54575:   apply (metis cos_gt_zero cos_minus minus_less_iff neg_0_less_iff_less)
wenzelm@53079:   apply (auto intro: cos_gt_zero)
wenzelm@53079:   done
hoelzl@41970: 
paulson@15077: lemma cos_ge_zero: "[| -(pi/2) \<le> x; x \<le> pi/2 |] ==> 0 \<le> cos x"
wenzelm@53079:   apply (auto simp add: order_le_less cos_gt_zero_pi)
wenzelm@53079:   apply (subgoal_tac "x = pi/2", auto)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma sin_gt_zero_pi: "[| 0 < x; x < pi  |] ==> 0 < sin x"
wenzelm@53079:   by (simp add: sin_cos_eq cos_gt_zero_pi)
hoelzl@29803: 
hoelzl@29803: lemma pi_ge_two: "2 \<le> pi"
hoelzl@29803: proof (rule ccontr)
hoelzl@29803:   assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
hoelzl@29803:   have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
hoelzl@29803:   proof (cases "2 < 2 * pi")
hoelzl@29803:     case True with dense[OF `pi < 2`] show ?thesis by auto
hoelzl@29803:   next
hoelzl@29803:     case False have "pi < 2 * pi" by auto
hoelzl@29803:     from dense[OF this] and False show ?thesis by auto
hoelzl@29803:   qed
hoelzl@29803:   then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
hoelzl@29803:   hence "0 < sin y" using sin_gt_zero by auto
hoelzl@41970:   moreover
hoelzl@29803:   have "sin y < 0" using sin_gt_zero_pi[of "y - pi"] `pi < y` and `y < 2 * pi` sin_periodic_pi[of "y - pi"] by auto
hoelzl@29803:   ultimately show False by auto
hoelzl@29803: qed
hoelzl@29803: 
paulson@15077: lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
wenzelm@53079:   by (auto simp add: order_le_less sin_gt_zero_pi)
paulson@15077: 
huffman@44745: text {* FIXME: This proof is almost identical to lemma @{text cos_is_zero}.
huffman@44745:   It should be possible to factor out some of the common parts. *}
huffman@44745: 
paulson@15077: lemma cos_total: "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. 0 \<le> x & x \<le> pi & (cos x = y)"
huffman@44745: proof (rule ex_ex1I)
huffman@44745:   assume y: "-1 \<le> y" "y \<le> 1"
huffman@44745:   show "\<exists>x. 0 \<le> x & x \<le> pi & cos x = y"
huffman@44745:     by (rule IVT2, simp_all add: y)
huffman@44745: next
huffman@44745:   fix a b
huffman@44745:   assume a: "0 \<le> a \<and> a \<le> pi \<and> cos a = y"
huffman@44745:   assume b: "0 \<le> b \<and> b \<le> pi \<and> cos b = y"
hoelzl@56181:   have [simp]: "\<forall>x. cos differentiable (at x)"
hoelzl@56181:     unfolding real_differentiable_def by (auto intro: DERIV_cos)
huffman@44745:   from a b show "a = b"
huffman@44745:     apply (cut_tac less_linear [of a b], auto)
huffman@44745:     apply (drule_tac f = cos in Rolle)
huffman@44745:     apply (drule_tac [5] f = cos in Rolle)
huffman@44745:     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
huffman@44745:     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
huffman@44745:     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
huffman@44745:     done
huffman@44745: qed
paulson@15077: 
paulson@15077: lemma sin_total:
paulson@15077:      "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. -(pi/2) \<le> x & x \<le> pi/2 & (sin x = y)"
paulson@15077: apply (rule ccontr)
paulson@15077: apply (subgoal_tac "\<forall>x. (- (pi/2) \<le> x & x \<le> pi/2 & (sin x = y)) = (0 \<le> (x + pi/2) & (x + pi/2) \<le> pi & (cos (x + pi/2) = -y))")
wenzelm@18585: apply (erule contrapos_np)
huffman@45309: apply simp
hoelzl@41970: apply (cut_tac y="-y" in cos_total, simp) apply simp
paulson@15077: apply (erule ex1E)
paulson@15229: apply (rule_tac a = "x - (pi/2)" in ex1I)
haftmann@57512: apply (simp (no_asm) add: add.assoc)
paulson@15077: apply (rotate_tac 3)
huffman@45309: apply (drule_tac x = "xa + pi/2" in spec, safe, simp_all add: cos_add)
paulson@15077: done
paulson@15077: 
paulson@15077: lemma reals_Archimedean4:
paulson@15077:      "[| 0 < y; 0 \<le> x |] ==> \<exists>n. real n * y \<le> x & x < real (Suc n) * y"
paulson@15077: apply (auto dest!: reals_Archimedean3)
hoelzl@41970: apply (drule_tac x = x in spec, clarify)
paulson@15077: apply (subgoal_tac "x < real(LEAST m::nat. x < real m * y) * y")
hoelzl@41970:  prefer 2 apply (erule LeastI)
hoelzl@41970: apply (case_tac "LEAST m::nat. x < real m * y", simp)
blanchet@55417: apply (rename_tac m)
blanchet@55417: apply (subgoal_tac "~ x < real m * y")
hoelzl@41970:  prefer 2 apply (rule not_less_Least, simp, force)
paulson@15077: done
paulson@15077: 
hoelzl@41970: (* Pre Isabelle99-2 proof was simpler- numerals arithmetic
paulson@15077:    now causes some unwanted re-arrangements of literals!   *)
paulson@15229: lemma cos_zero_lemma:
hoelzl@41970:      "[| 0 \<le> x; cos x = 0 |] ==>
paulson@15077:       \<exists>n::nat. ~even n & x = real n * (pi/2)"
paulson@15077: apply (drule pi_gt_zero [THEN reals_Archimedean4], safe)
hoelzl@41970: apply (subgoal_tac "0 \<le> x - real n * pi &
paulson@15086:                     (x - real n * pi) \<le> pi & (cos (x - real n * pi) = 0) ")
nipkow@29667: apply (auto simp add: algebra_simps real_of_nat_Suc)
nipkow@29667:  prefer 2 apply (simp add: cos_diff)
paulson@15077: apply (simp add: cos_diff)
paulson@15077: apply (subgoal_tac "EX! x. 0 \<le> x & x \<le> pi & cos x = 0")
paulson@15077: apply (rule_tac [2] cos_total, safe)
paulson@15077: apply (drule_tac x = "x - real n * pi" in spec)
paulson@15077: apply (drule_tac x = "pi/2" in spec)
paulson@15077: apply (simp add: cos_diff)
paulson@15229: apply (rule_tac x = "Suc (2 * n)" in exI)
nipkow@29667: apply (simp add: real_of_nat_Suc algebra_simps, auto)
paulson@15077: done
paulson@15077: 
paulson@15229: lemma sin_zero_lemma:
hoelzl@41970:      "[| 0 \<le> x; sin x = 0 |] ==>
paulson@15077:       \<exists>n::nat. even n & x = real n * (pi/2)"
paulson@15077: apply (subgoal_tac "\<exists>n::nat. ~ even n & x + pi/2 = real n * (pi/2) ")
paulson@15077:  apply (clarify, rule_tac x = "n - 1" in exI)
webertj@49962:  apply (force simp add: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
paulson@15085: apply (rule cos_zero_lemma)
huffman@45309: apply (simp_all add: cos_add)
paulson@15077: done
paulson@15077: 
paulson@15077: 
paulson@15229: lemma cos_zero_iff:
hoelzl@41970:      "(cos x = 0) =
hoelzl@41970:       ((\<exists>n::nat. ~even n & (x = real n * (pi/2))) |
paulson@15077:        (\<exists>n::nat. ~even n & (x = -(real n * (pi/2)))))"
paulson@15077: apply (rule iffI)
paulson@15077: apply (cut_tac linorder_linear [of 0 x], safe)
paulson@15077: apply (drule cos_zero_lemma, assumption+)
hoelzl@41970: apply (cut_tac x="-x" in cos_zero_lemma, simp, simp)
hoelzl@41970: apply (force simp add: minus_equation_iff [of x])
webertj@49962: apply (auto simp only: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
haftmann@54230: apply (auto simp add: cos_diff cos_add)
paulson@15077: done
paulson@15077: 
paulson@15077: (* ditto: but to a lesser extent *)
paulson@15229: lemma sin_zero_iff:
hoelzl@41970:      "(sin x = 0) =
hoelzl@41970:       ((\<exists>n::nat. even n & (x = real n * (pi/2))) |
paulson@15077:        (\<exists>n::nat. even n & (x = -(real n * (pi/2)))))"
paulson@15077: apply (rule iffI)
paulson@15077: apply (cut_tac linorder_linear [of 0 x], safe)
paulson@15077: apply (drule sin_zero_lemma, assumption+)
paulson@15077: apply (cut_tac x="-x" in sin_zero_lemma, simp, simp, safe)
hoelzl@41970: apply (force simp add: minus_equation_iff [of x])
nipkow@15539: apply (auto simp add: even_mult_two_ex)
paulson@15077: done
paulson@15077: 
wenzelm@53079: lemma cos_monotone_0_pi:
wenzelm@53079:   assumes "0 \<le> y" and "y < x" and "x \<le> pi"
hoelzl@29803:   shows "cos x < cos y"
hoelzl@29803: proof -
wenzelm@33549:   have "- (x - y) < 0" using assms by auto
hoelzl@29803: 
hoelzl@29803:   from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
wenzelm@53079:   obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z"
wenzelm@53079:     by auto
wenzelm@33549:   hence "0 < z" and "z < pi" using assms by auto
hoelzl@29803:   hence "0 < sin z" using sin_gt_zero_pi by auto
wenzelm@53079:   hence "cos x - cos y < 0"
wenzelm@53079:     unfolding cos_diff minus_mult_commute[symmetric]
wenzelm@53079:     using `- (x - y) < 0` by (rule mult_pos_neg2)
hoelzl@29803:   thus ?thesis by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma cos_monotone_0_pi':
wenzelm@53079:   assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi"
wenzelm@53079:   shows "cos x \<le> cos y"
hoelzl@29803: proof (cases "y < x")
wenzelm@53079:   case True
wenzelm@53079:   show ?thesis
wenzelm@53079:     using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
hoelzl@29803: next
wenzelm@53079:   case False
wenzelm@53079:   hence "y = x" using `y \<le> x` by auto
hoelzl@29803:   thus ?thesis by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma cos_monotone_minus_pi_0:
wenzelm@53079:   assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
hoelzl@29803:   shows "cos y < cos x"
hoelzl@29803: proof -
wenzelm@53079:   have "0 \<le> -x" and "-x < -y" and "-y \<le> pi"
wenzelm@53079:     using assms by auto
wenzelm@53079:   from cos_monotone_0_pi[OF this] show ?thesis
wenzelm@53079:     unfolding cos_minus .
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma cos_monotone_minus_pi_0':
wenzelm@53079:   assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0"
wenzelm@53079:   shows "cos y \<le> cos x"
hoelzl@29803: proof (cases "y < x")
wenzelm@53079:   case True
wenzelm@53079:   show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`]
wenzelm@53079:     by auto
hoelzl@29803: next
wenzelm@53079:   case False
wenzelm@53079:   hence "y = x" using `y \<le> x` by auto
hoelzl@29803:   thus ?thesis by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma sin_monotone_2pi':
wenzelm@53079:   assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2"
wenzelm@53079:   shows "sin y \<le> sin x"
hoelzl@29803: proof -
wenzelm@33549:   have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi"
wenzelm@33549:     using pi_ge_two and assms by auto
wenzelm@53079:   from cos_monotone_0_pi'[OF this] show ?thesis
wenzelm@53079:     unfolding minus_sin_cos_eq[symmetric] by auto
hoelzl@29803: qed
paulson@15077: 
wenzelm@53079: 
huffman@29164: subsection {* Tangent *}
paulson@15077: 
wenzelm@53079: definition tan :: "real \<Rightarrow> real"
wenzelm@53079:   where "tan = (\<lambda>x. sin x / cos x)"
huffman@23043: 
paulson@15077: lemma tan_zero [simp]: "tan 0 = 0"
huffman@44311:   by (simp add: tan_def)
paulson@15077: 
paulson@15077: lemma tan_pi [simp]: "tan pi = 0"
huffman@44311:   by (simp add: tan_def)
paulson@15077: 
paulson@15077: lemma tan_npi [simp]: "tan (real (n::nat) * pi) = 0"
huffman@44311:   by (simp add: tan_def)
paulson@15077: 
paulson@15077: lemma tan_minus [simp]: "tan (-x) = - tan x"
huffman@44311:   by (simp add: tan_def)
paulson@15077: 
paulson@15077: lemma tan_periodic [simp]: "tan (x + 2*pi) = tan x"
huffman@44311:   by (simp add: tan_def)
paulson@15077: 
hoelzl@41970: lemma lemma_tan_add1:
huffman@44311:   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> 1 - tan x * tan y = cos (x + y)/(cos x * cos y)"
huffman@44311:   by (simp add: tan_def cos_add field_simps)
paulson@15077: 
hoelzl@41970: lemma add_tan_eq:
huffman@44311:   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> tan x + tan y = sin(x + y)/(cos x * cos y)"
huffman@44311:   by (simp add: tan_def sin_add field_simps)
paulson@15077: 
paulson@15229: lemma tan_add:
hoelzl@41970:      "[| cos x \<noteq> 0; cos y \<noteq> 0; cos (x + y) \<noteq> 0 |]
paulson@15077:       ==> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
huffman@44311:   by (simp add: add_tan_eq lemma_tan_add1, simp add: tan_def)
paulson@15077: 
paulson@15229: lemma tan_double:
hoelzl@41970:      "[| cos x \<noteq> 0; cos (2 * x) \<noteq> 0 |]
wenzelm@53076:       ==> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
huffman@44311:   using tan_add [of x x] by (simp add: power2_eq_square)
paulson@15077: 
paulson@15077: lemma tan_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < tan x"
wenzelm@53079:   by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
hoelzl@41970: 
hoelzl@41970: lemma tan_less_zero:
wenzelm@53079:   assumes lb: "- pi/2 < x" and "x < 0"
wenzelm@53079:   shows "tan x < 0"
paulson@15077: proof -
hoelzl@41970:   have "0 < tan (- x)" using assms by (simp only: tan_gt_zero)
paulson@15077:   thus ?thesis by simp
paulson@15077: qed
paulson@15077: 
huffman@44756: lemma tan_half: "tan x = sin (2 * x) / (cos (2 * x) + 1)"
huffman@44756:   unfolding tan_def sin_double cos_double sin_squared_eq
huffman@44756:   by (simp add: power2_eq_square)
hoelzl@29803: 
wenzelm@53015: lemma DERIV_tan [simp]: "cos x \<noteq> 0 \<Longrightarrow> DERIV tan x :> inverse ((cos x)\<^sup>2)"
huffman@44311:   unfolding tan_def
hoelzl@56381:   by (auto intro!: derivative_eq_intros, simp add: divide_inverse power2_eq_square)
huffman@44311: 
huffman@44311: lemma isCont_tan: "cos x \<noteq> 0 \<Longrightarrow> isCont tan x"
huffman@44311:   by (rule DERIV_tan [THEN DERIV_isCont])
huffman@44311: 
huffman@44311: lemma isCont_tan' [simp]:
huffman@44311:   "\<lbrakk>isCont f a; cos (f a) \<noteq> 0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. tan (f x)) a"
huffman@44311:   by (rule isCont_o2 [OF _ isCont_tan])
huffman@44311: 
huffman@44311: lemma tendsto_tan [tendsto_intros]:
huffman@44311:   "\<lbrakk>(f ---> a) F; cos a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. tan (f x)) ---> tan a) F"
huffman@44311:   by (rule isCont_tendsto_compose [OF isCont_tan])
huffman@44311: 
hoelzl@51478: lemma continuous_tan:
hoelzl@51478:   "continuous F f \<Longrightarrow> cos (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. tan (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_tan)
hoelzl@51478: 
hoelzl@51478: lemma isCont_tan'' [continuous_intros]:
hoelzl@51478:   "continuous (at x) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. tan (f x))"
hoelzl@51478:   unfolding continuous_at by (rule tendsto_tan)
hoelzl@51478: 
hoelzl@51478: lemma continuous_within_tan [continuous_intros]:
hoelzl@51478:   "continuous (at x within s) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
hoelzl@51478:   unfolding continuous_within by (rule tendsto_tan)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_tan [continuous_intros]:
hoelzl@51478:   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. cos (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. tan (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_tan)
hoelzl@51478: 
wenzelm@53079: lemma LIM_cos_div_sin: "(\<lambda>x. cos(x)/sin(x)) -- pi/2 --> 0"
huffman@44311:   by (rule LIM_cong_limit, (rule tendsto_intros)+, simp_all)
paulson@15077: 
paulson@15077: lemma lemma_tan_total: "0 < y ==> \<exists>x. 0 < x & x < pi/2 & y < tan x"
wenzelm@53079:   apply (cut_tac LIM_cos_div_sin)
wenzelm@53079:   apply (simp only: LIM_eq)
wenzelm@53079:   apply (drule_tac x = "inverse y" in spec, safe, force)
wenzelm@53079:   apply (drule_tac ?d1.0 = s in pi_half_gt_zero [THEN [2] real_lbound_gt_zero], safe)
wenzelm@53079:   apply (rule_tac x = "(pi/2) - e" in exI)
wenzelm@53079:   apply (simp (no_asm_simp))
wenzelm@53079:   apply (drule_tac x = "(pi/2) - e" in spec)
wenzelm@53079:   apply (auto simp add: tan_def sin_diff cos_diff)
wenzelm@53079:   apply (rule inverse_less_iff_less [THEN iffD1])
wenzelm@53079:   apply (auto simp add: divide_inverse)
wenzelm@53079:   apply (rule mult_pos_pos)
wenzelm@53079:   apply (subgoal_tac [3] "0 < sin e & 0 < cos e")
haftmann@57512:   apply (auto intro: cos_gt_zero sin_gt_zero2 simp add: mult.commute)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma tan_total_pos: "0 \<le> y ==> \<exists>x. 0 \<le> x & x < pi/2 & tan x = y"
wenzelm@53079:   apply (frule order_le_imp_less_or_eq, safe)
wenzelm@53079:    prefer 2 apply force
wenzelm@53079:   apply (drule lemma_tan_total, safe)
wenzelm@53079:   apply (cut_tac f = tan and a = 0 and b = x and y = y in IVT_objl)
wenzelm@53079:   apply (auto intro!: DERIV_tan [THEN DERIV_isCont])
wenzelm@53079:   apply (drule_tac y = xa in order_le_imp_less_or_eq)
wenzelm@53079:   apply (auto dest: cos_gt_zero)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma lemma_tan_total1: "\<exists>x. -(pi/2) < x & x < (pi/2) & tan x = y"
wenzelm@53079:   apply (cut_tac linorder_linear [of 0 y], safe)
wenzelm@53079:   apply (drule tan_total_pos)
wenzelm@53079:   apply (cut_tac [2] y="-y" in tan_total_pos, safe)
wenzelm@53079:   apply (rule_tac [3] x = "-x" in exI)
wenzelm@53079:   apply (auto del: exI intro!: exI)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma tan_total: "EX! x. -(pi/2) < x & x < (pi/2) & tan x = y"
wenzelm@53079:   apply (cut_tac y = y in lemma_tan_total1, auto)
thomas@57492:   apply hypsubst_thin
wenzelm@53079:   apply (cut_tac x = xa and y = y in linorder_less_linear, auto)
wenzelm@53079:   apply (subgoal_tac [2] "\<exists>z. y < z & z < xa & DERIV tan z :> 0")
wenzelm@53079:   apply (subgoal_tac "\<exists>z. xa < z & z < y & DERIV tan z :> 0")
wenzelm@53079:   apply (rule_tac [4] Rolle)
wenzelm@53079:   apply (rule_tac [2] Rolle)
wenzelm@53079:   apply (auto del: exI intro!: DERIV_tan DERIV_isCont exI
hoelzl@56181:               simp add: real_differentiable_def)
wenzelm@53079:   txt{*Now, simulate TRYALL*}
wenzelm@53079:   apply (rule_tac [!] DERIV_tan asm_rl)
wenzelm@53079:   apply (auto dest!: DERIV_unique [OF _ DERIV_tan]
wenzelm@53079:               simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym])
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma tan_monotone:
wenzelm@53079:   assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
hoelzl@29803:   shows "tan y < tan x"
hoelzl@29803: proof -
wenzelm@53079:   have "\<forall>x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse ((cos x')\<^sup>2)"
hoelzl@29803:   proof (rule allI, rule impI)
wenzelm@53079:     fix x' :: real
wenzelm@53079:     assume "y \<le> x' \<and> x' \<le> x"
wenzelm@33549:     hence "-(pi/2) < x'" and "x' < pi/2" using assms by auto
hoelzl@29803:     from cos_gt_zero_pi[OF this]
hoelzl@29803:     have "cos x' \<noteq> 0" by auto
wenzelm@53076:     thus "DERIV tan x' :> inverse ((cos x')\<^sup>2)" by (rule DERIV_tan)
hoelzl@29803:   qed
hoelzl@41970:   from MVT2[OF `y < x` this]
wenzelm@53079:   obtain z where "y < z" and "z < x"
wenzelm@53079:     and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<^sup>2)" by auto
wenzelm@33549:   hence "- (pi / 2) < z" and "z < pi / 2" using assms by auto
hoelzl@29803:   hence "0 < cos z" using cos_gt_zero_pi by auto
wenzelm@53015:   hence inv_pos: "0 < inverse ((cos z)\<^sup>2)" by auto
hoelzl@29803:   have "0 < x - y" using `y < x` by auto
nipkow@56544:   with inv_pos have "0 < tan x - tan y" unfolding tan_diff by auto
hoelzl@29803:   thus ?thesis by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma tan_monotone':
wenzelm@53079:   assumes "- (pi / 2) < y"
wenzelm@53079:     and "y < pi / 2"
wenzelm@53079:     and "- (pi / 2) < x"
wenzelm@53079:     and "x < pi / 2"
hoelzl@29803:   shows "(y < x) = (tan y < tan x)"
hoelzl@29803: proof
wenzelm@53079:   assume "y < x"
wenzelm@53079:   thus "tan y < tan x"
wenzelm@53079:     using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
hoelzl@29803: next
hoelzl@29803:   assume "tan y < tan x"
hoelzl@29803:   show "y < x"
hoelzl@29803:   proof (rule ccontr)
hoelzl@29803:     assume "\<not> y < x" hence "x \<le> y" by auto
hoelzl@41970:     hence "tan x \<le> tan y"
hoelzl@29803:     proof (cases "x = y")
hoelzl@29803:       case True thus ?thesis by auto
hoelzl@29803:     next
hoelzl@29803:       case False hence "x < y" using `x \<le> y` by auto
hoelzl@29803:       from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
hoelzl@29803:     qed
hoelzl@29803:     thus False using `tan y < tan x` by auto
hoelzl@29803:   qed
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)"
wenzelm@53079:   unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
hoelzl@29803: 
hoelzl@41970: lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
hoelzl@29803:   by (simp add: tan_def)
hoelzl@29803: 
wenzelm@53079: lemma tan_periodic_nat[simp]:
wenzelm@53079:   fixes n :: nat
wenzelm@53079:   shows "tan (x + real n * pi) = tan x"
hoelzl@29803: proof (induct n arbitrary: x)
wenzelm@53079:   case 0
wenzelm@53079:   then show ?case by simp
wenzelm@53079: next
hoelzl@29803:   case (Suc n)
wenzelm@53079:   have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi"
wenzelm@53079:     unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right by auto
hoelzl@29803:   show ?case unfolding split_pi_off using Suc by auto
wenzelm@53079: qed
hoelzl@29803: 
hoelzl@29803: lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
hoelzl@29803: proof (cases "0 \<le> i")
wenzelm@53079:   case True
wenzelm@53079:   hence i_nat: "real i = real (nat i)" by auto
hoelzl@29803:   show ?thesis unfolding i_nat by auto
hoelzl@29803: next
wenzelm@53079:   case False
wenzelm@53079:   hence i_nat: "real i = - real (nat (-i))" by auto
wenzelm@53079:   have "tan x = tan (x + real i * pi - real i * pi)"
wenzelm@53079:     by auto
wenzelm@53079:   also have "\<dots> = tan (x + real i * pi)"
wenzelm@53079:     unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
hoelzl@29803:   finally show ?thesis by auto
hoelzl@29803: qed
hoelzl@29803: 
huffman@47108: lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
huffman@47108:   using tan_periodic_int[of _ "numeral n" ] unfolding real_numeral .
huffman@23043: 
huffman@23043: subsection {* Inverse Trigonometric Functions *}
huffman@23043: 
wenzelm@53079: definition arcsin :: "real => real"
wenzelm@53079:   where "arcsin y = (THE x. -(pi/2) \<le> x & x \<le> pi/2 & sin x = y)"
wenzelm@53079: 
wenzelm@53079: definition arccos :: "real => real"
wenzelm@53079:   where "arccos y = (THE x. 0 \<le> x & x \<le> pi & cos x = y)"
wenzelm@53079: 
wenzelm@53079: definition arctan :: "real => real"
wenzelm@53079:   where "arctan y = (THE x. -(pi/2) < x & x < pi/2 & tan x = y)"
huffman@23043: 
paulson@15229: lemma arcsin:
wenzelm@53079:   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow>
wenzelm@53079:     -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2 & sin(arcsin y) = y"
wenzelm@53079:   unfolding arcsin_def by (rule theI' [OF sin_total])
huffman@23011: 
huffman@23011: lemma arcsin_pi:
wenzelm@53079:   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi & sin(arcsin y) = y"
wenzelm@53079:   apply (drule (1) arcsin)
wenzelm@53079:   apply (force intro: order_trans)
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma sin_arcsin [simp]: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> sin(arcsin y) = y"
wenzelm@53079:   by (blast dest: arcsin)
wenzelm@53079: 
wenzelm@53079: lemma arcsin_bounded: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2"
wenzelm@53079:   by (blast dest: arcsin)
wenzelm@53079: 
wenzelm@53079: lemma arcsin_lbound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y"
wenzelm@53079:   by (blast dest: arcsin)
wenzelm@53079: 
wenzelm@53079: lemma arcsin_ubound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin y \<le> pi/2"
wenzelm@53079:   by (blast dest: arcsin)
paulson@15077: 
paulson@15077: lemma arcsin_lt_bounded:
paulson@15077:      "[| -1 < y; y < 1 |] ==> -(pi/2) < arcsin y & arcsin y < pi/2"
wenzelm@53079:   apply (frule order_less_imp_le)
wenzelm@53079:   apply (frule_tac y = y in order_less_imp_le)
wenzelm@53079:   apply (frule arcsin_bounded)
wenzelm@53079:   apply (safe, simp)
wenzelm@53079:   apply (drule_tac y = "arcsin y" in order_le_imp_less_or_eq)
wenzelm@53079:   apply (drule_tac [2] y = "pi/2" in order_le_imp_less_or_eq, safe)
wenzelm@53079:   apply (drule_tac [!] f = sin in arg_cong, auto)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma arcsin_sin: "[|-(pi/2) \<le> x; x \<le> pi/2 |] ==> arcsin(sin x) = x"
wenzelm@53079:   apply (unfold arcsin_def)
wenzelm@53079:   apply (rule the1_equality)
wenzelm@53079:   apply (rule sin_total, auto)
wenzelm@53079:   done
paulson@15077: 
huffman@22975: lemma arccos:
hoelzl@41970:      "[| -1 \<le> y; y \<le> 1 |]
huffman@22975:       ==> 0 \<le> arccos y & arccos y \<le> pi & cos(arccos y) = y"
wenzelm@53079:   unfolding arccos_def by (rule theI' [OF cos_total])
paulson@15077: 
huffman@22975: lemma cos_arccos [simp]: "[| -1 \<le> y; y \<le> 1 |] ==> cos(arccos y) = y"
wenzelm@53079:   by (blast dest: arccos)
hoelzl@41970: 
huffman@22975: lemma arccos_bounded: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y & arccos y \<le> pi"
wenzelm@53079:   by (blast dest: arccos)
paulson@15077: 
huffman@22975: lemma arccos_lbound: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y"
wenzelm@53079:   by (blast dest: arccos)
paulson@15077: 
huffman@22975: lemma arccos_ubound: "[| -1 \<le> y; y \<le> 1 |] ==> arccos y \<le> pi"
wenzelm@53079:   by (blast dest: arccos)
paulson@15077: 
huffman@22975: lemma arccos_lt_bounded:
hoelzl@41970:      "[| -1 < y; y < 1 |]
huffman@22975:       ==> 0 < arccos y & arccos y < pi"
wenzelm@53079:   apply (frule order_less_imp_le)
wenzelm@53079:   apply (frule_tac y = y in order_less_imp_le)
wenzelm@53079:   apply (frule arccos_bounded, auto)
wenzelm@53079:   apply (drule_tac y = "arccos y" in order_le_imp_less_or_eq)
wenzelm@53079:   apply (drule_tac [2] y = pi in order_le_imp_less_or_eq, auto)
wenzelm@53079:   apply (drule_tac [!] f = cos in arg_cong, auto)
wenzelm@53079:   done
paulson@15077: 
huffman@22975: lemma arccos_cos: "[|0 \<le> x; x \<le> pi |] ==> arccos(cos x) = x"
wenzelm@53079:   apply (simp add: arccos_def)
wenzelm@53079:   apply (auto intro!: the1_equality cos_total)
wenzelm@53079:   done
paulson@15077: 
huffman@22975: lemma arccos_cos2: "[|x \<le> 0; -pi \<le> x |] ==> arccos(cos x) = -x"
wenzelm@53079:   apply (simp add: arccos_def)
wenzelm@53079:   apply (auto intro!: the1_equality cos_total)
wenzelm@53079:   done
paulson@15077: 
wenzelm@53015: lemma cos_arcsin: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cos (arcsin x) = sqrt (1 - x\<^sup>2)"
wenzelm@53079:   apply (subgoal_tac "x\<^sup>2 \<le> 1")
wenzelm@53079:   apply (rule power2_eq_imp_eq)
wenzelm@53079:   apply (simp add: cos_squared_eq)
wenzelm@53079:   apply (rule cos_ge_zero)
wenzelm@53079:   apply (erule (1) arcsin_lbound)
wenzelm@53079:   apply (erule (1) arcsin_ubound)
wenzelm@53079:   apply simp
wenzelm@53079:   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
wenzelm@53079:   apply (rule power_mono, simp, simp)
wenzelm@53079:   done
huffman@23045: 
wenzelm@53015: lemma sin_arccos: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> sin (arccos x) = sqrt (1 - x\<^sup>2)"
wenzelm@53079:   apply (subgoal_tac "x\<^sup>2 \<le> 1")
wenzelm@53079:   apply (rule power2_eq_imp_eq)
wenzelm@53079:   apply (simp add: sin_squared_eq)
wenzelm@53079:   apply (rule sin_ge_zero)
wenzelm@53079:   apply (erule (1) arccos_lbound)
wenzelm@53079:   apply (erule (1) arccos_ubound)
wenzelm@53079:   apply simp
wenzelm@53079:   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
wenzelm@53079:   apply (rule power_mono, simp, simp)
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma arctan [simp]: "- (pi/2) < arctan y  & arctan y < pi/2 & tan (arctan y) = y"
wenzelm@53079:   unfolding arctan_def by (rule theI' [OF tan_total])
wenzelm@53079: 
wenzelm@53079: lemma tan_arctan: "tan (arctan y) = y"
wenzelm@53079:   by auto
paulson@15077: 
paulson@15077: lemma arctan_bounded: "- (pi/2) < arctan y  & arctan y < pi/2"
wenzelm@53079:   by (auto simp only: arctan)
paulson@15077: 
paulson@15077: lemma arctan_lbound: "- (pi/2) < arctan y"
wenzelm@53079:   by auto
paulson@15077: 
paulson@15077: lemma arctan_ubound: "arctan y < pi/2"
wenzelm@53079:   by (auto simp only: arctan)
paulson@15077: 
huffman@44746: lemma arctan_unique:
wenzelm@53079:   assumes "-(pi/2) < x"
wenzelm@53079:     and "x < pi/2"
wenzelm@53079:     and "tan x = y"
huffman@44746:   shows "arctan y = x"
huffman@44746:   using assms arctan [of y] tan_total [of y] by (fast elim: ex1E)
huffman@44746: 
wenzelm@53079: lemma arctan_tan: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> arctan (tan x) = x"
wenzelm@53079:   by (rule arctan_unique) simp_all
paulson@15077: 
paulson@15077: lemma arctan_zero_zero [simp]: "arctan 0 = 0"
wenzelm@53079:   by (rule arctan_unique) simp_all
huffman@44746: 
huffman@44746: lemma arctan_minus: "arctan (- x) = - arctan x"
huffman@44746:   apply (rule arctan_unique)
huffman@44746:   apply (simp only: neg_less_iff_less arctan_ubound)
huffman@44746:   apply (metis minus_less_iff arctan_lbound)
huffman@44746:   apply simp
huffman@44746:   done
paulson@15077: 
huffman@44725: lemma cos_arctan_not_zero [simp]: "cos (arctan x) \<noteq> 0"
huffman@44725:   by (intro less_imp_neq [symmetric] cos_gt_zero_pi
huffman@44725:     arctan_lbound arctan_ubound)
huffman@44725: 
wenzelm@53015: lemma cos_arctan: "cos (arctan x) = 1 / sqrt (1 + x\<^sup>2)"
huffman@44725: proof (rule power2_eq_imp_eq)
wenzelm@53015:   have "0 < 1 + x\<^sup>2" by (simp add: add_pos_nonneg)
wenzelm@53015:   show "0 \<le> 1 / sqrt (1 + x\<^sup>2)" by simp
huffman@44725:   show "0 \<le> cos (arctan x)"
huffman@44725:     by (intro less_imp_le cos_gt_zero_pi arctan_lbound arctan_ubound)
wenzelm@53015:   have "(cos (arctan x))\<^sup>2 * (1 + (tan (arctan x))\<^sup>2) = 1"
webertj@49962:     unfolding tan_def by (simp add: distrib_left power_divide)
wenzelm@53015:   thus "(cos (arctan x))\<^sup>2 = (1 / sqrt (1 + x\<^sup>2))\<^sup>2"
wenzelm@53015:     using `0 < 1 + x\<^sup>2` by (simp add: power_divide eq_divide_eq)
huffman@44725: qed
huffman@44725: 
wenzelm@53015: lemma sin_arctan: "sin (arctan x) = x / sqrt (1 + x\<^sup>2)"
huffman@44725:   using add_pos_nonneg [OF zero_less_one zero_le_power2 [of x]]
huffman@44725:   using tan_arctan [of x] unfolding tan_def cos_arctan
huffman@44725:   by (simp add: eq_divide_eq)
paulson@15077: 
wenzelm@53076: lemma tan_sec: "cos x \<noteq> 0 ==> 1 + (tan x)\<^sup>2 = (inverse (cos x))\<^sup>2"
wenzelm@53079:   apply (rule power_inverse [THEN subst])
lp15@56217:   apply (rule_tac c1 = "(cos x)\<^sup>2" in mult_right_cancel [THEN iffD1])
wenzelm@53079:   apply (auto dest: field_power_not_zero
wenzelm@53079:           simp add: power_mult_distrib distrib_right power_divide tan_def
haftmann@57512:                     mult.assoc power_inverse [symmetric])
wenzelm@53079:   done
paulson@15077: 
huffman@44746: lemma arctan_less_iff: "arctan x < arctan y \<longleftrightarrow> x < y"
huffman@44746:   by (metis tan_monotone' arctan_lbound arctan_ubound tan_arctan)
huffman@44746: 
huffman@44746: lemma arctan_le_iff: "arctan x \<le> arctan y \<longleftrightarrow> x \<le> y"
huffman@44746:   by (simp only: not_less [symmetric] arctan_less_iff)
huffman@44746: 
huffman@44746: lemma arctan_eq_iff: "arctan x = arctan y \<longleftrightarrow> x = y"
huffman@44746:   by (simp only: eq_iff [where 'a=real] arctan_le_iff)
huffman@44746: 
huffman@44746: lemma zero_less_arctan_iff [simp]: "0 < arctan x \<longleftrightarrow> 0 < x"
huffman@44746:   using arctan_less_iff [of 0 x] by simp
huffman@44746: 
huffman@44746: lemma arctan_less_zero_iff [simp]: "arctan x < 0 \<longleftrightarrow> x < 0"
huffman@44746:   using arctan_less_iff [of x 0] by simp
huffman@44746: 
huffman@44746: lemma zero_le_arctan_iff [simp]: "0 \<le> arctan x \<longleftrightarrow> 0 \<le> x"
huffman@44746:   using arctan_le_iff [of 0 x] by simp
huffman@44746: 
huffman@44746: lemma arctan_le_zero_iff [simp]: "arctan x \<le> 0 \<longleftrightarrow> x \<le> 0"
huffman@44746:   using arctan_le_iff [of x 0] by simp
huffman@44746: 
huffman@44746: lemma arctan_eq_zero_iff [simp]: "arctan x = 0 \<longleftrightarrow> x = 0"
huffman@44746:   using arctan_eq_iff [of x 0] by simp
huffman@44746: 
hoelzl@51482: lemma continuous_on_arcsin': "continuous_on {-1 .. 1} arcsin"
hoelzl@51482: proof -
hoelzl@51482:   have "continuous_on (sin ` {- pi / 2 .. pi / 2}) arcsin"
hoelzl@56371:     by (rule continuous_on_inv) (auto intro: continuous_intros simp: arcsin_sin)
hoelzl@51482:   also have "sin ` {- pi / 2 .. pi / 2} = {-1 .. 1}"
hoelzl@51482:   proof safe
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "x \<in> {-1..1}"
wenzelm@53079:     then show "x \<in> sin ` {- pi / 2..pi / 2}"
wenzelm@53079:       using arcsin_lbound arcsin_ubound
hoelzl@56479:       by (intro image_eqI[where x="arcsin x"]) auto
hoelzl@51482:   qed simp
hoelzl@51482:   finally show ?thesis .
hoelzl@51482: qed
hoelzl@51482: 
hoelzl@56371: lemma continuous_on_arcsin [continuous_intros]:
hoelzl@51482:   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. -1 \<le> f x \<and> f x \<le> 1) \<Longrightarrow> continuous_on s (\<lambda>x. arcsin (f x))"
hoelzl@51482:   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arcsin']]
hoelzl@51482:   by (auto simp: comp_def subset_eq)
hoelzl@51482: 
hoelzl@51482: lemma isCont_arcsin: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arcsin x"
hoelzl@51482:   using continuous_on_arcsin'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
hoelzl@51482:   by (auto simp: continuous_on_eq_continuous_at subset_eq)
hoelzl@51482: 
hoelzl@51482: lemma continuous_on_arccos': "continuous_on {-1 .. 1} arccos"
hoelzl@51482: proof -
hoelzl@51482:   have "continuous_on (cos ` {0 .. pi}) arccos"
hoelzl@56371:     by (rule continuous_on_inv) (auto intro: continuous_intros simp: arccos_cos)
hoelzl@51482:   also have "cos ` {0 .. pi} = {-1 .. 1}"
hoelzl@51482:   proof safe
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "x \<in> {-1..1}"
wenzelm@53079:     then show "x \<in> cos ` {0..pi}"
wenzelm@53079:       using arccos_lbound arccos_ubound
wenzelm@53079:       by (intro image_eqI[where x="arccos x"]) auto
hoelzl@51482:   qed simp
hoelzl@51482:   finally show ?thesis .
hoelzl@51482: qed
hoelzl@51482: 
hoelzl@56371: lemma continuous_on_arccos [continuous_intros]:
hoelzl@51482:   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. -1 \<le> f x \<and> f x \<le> 1) \<Longrightarrow> continuous_on s (\<lambda>x. arccos (f x))"
hoelzl@51482:   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arccos']]
hoelzl@51482:   by (auto simp: comp_def subset_eq)
hoelzl@51482: 
hoelzl@51482: lemma isCont_arccos: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arccos x"
hoelzl@51482:   using continuous_on_arccos'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
hoelzl@51482:   by (auto simp: continuous_on_eq_continuous_at subset_eq)
huffman@23045: 
huffman@23045: lemma isCont_arctan: "isCont arctan x"
wenzelm@53079:   apply (rule arctan_lbound [of x, THEN dense, THEN exE], clarify)
wenzelm@53079:   apply (rule arctan_ubound [of x, THEN dense, THEN exE], clarify)
wenzelm@53079:   apply (subgoal_tac "isCont arctan (tan (arctan x))", simp)
wenzelm@53079:   apply (erule (1) isCont_inverse_function2 [where f=tan])
wenzelm@53079:   apply (metis arctan_tan order_le_less_trans order_less_le_trans)
wenzelm@53079:   apply (metis cos_gt_zero_pi isCont_tan order_less_le_trans less_le)
wenzelm@53079:   done
huffman@23045: 
hoelzl@51478: lemma tendsto_arctan [tendsto_intros]: "(f ---> x) F \<Longrightarrow> ((\<lambda>x. arctan (f x)) ---> arctan x) F"
hoelzl@51478:   by (rule isCont_tendsto_compose [OF isCont_arctan])
hoelzl@51478: 
hoelzl@51478: lemma continuous_arctan [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arctan (f x))"
hoelzl@51478:   unfolding continuous_def by (rule tendsto_arctan)
hoelzl@51478: 
hoelzl@56371: lemma continuous_on_arctan [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. arctan (f x))"
hoelzl@51478:   unfolding continuous_on_def by (auto intro: tendsto_arctan)
wenzelm@53079: 
huffman@23045: lemma DERIV_arcsin:
wenzelm@53015:   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arcsin x :> inverse (sqrt (1 - x\<^sup>2))"
wenzelm@53079:   apply (rule DERIV_inverse_function [where f=sin and a="-1" and b="1"])
wenzelm@53079:   apply (rule DERIV_cong [OF DERIV_sin])
wenzelm@53079:   apply (simp add: cos_arcsin)
wenzelm@53079:   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
wenzelm@53079:   apply (rule power_strict_mono, simp, simp, simp)
wenzelm@53079:   apply assumption
wenzelm@53079:   apply assumption
wenzelm@53079:   apply simp
wenzelm@53079:   apply (erule (1) isCont_arcsin)
wenzelm@53079:   done
huffman@23045: 
huffman@23045: lemma DERIV_arccos:
wenzelm@53015:   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arccos x :> inverse (- sqrt (1 - x\<^sup>2))"
wenzelm@53079:   apply (rule DERIV_inverse_function [where f=cos and a="-1" and b="1"])
wenzelm@53079:   apply (rule DERIV_cong [OF DERIV_cos])
wenzelm@53079:   apply (simp add: sin_arccos)
wenzelm@53079:   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
wenzelm@53079:   apply (rule power_strict_mono, simp, simp, simp)
wenzelm@53079:   apply assumption
wenzelm@53079:   apply assumption
wenzelm@53079:   apply simp
wenzelm@53079:   apply (erule (1) isCont_arccos)
wenzelm@53079:   done
huffman@23045: 
wenzelm@53015: lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
wenzelm@53079:   apply (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
wenzelm@53079:   apply (rule DERIV_cong [OF DERIV_tan])
wenzelm@53079:   apply (rule cos_arctan_not_zero)
wenzelm@53079:   apply (simp add: power_inverse tan_sec [symmetric])
wenzelm@53079:   apply (subgoal_tac "0 < 1 + x\<^sup>2", simp)
wenzelm@53079:   apply (simp add: add_pos_nonneg)
wenzelm@53079:   apply (simp, simp, simp, rule isCont_arctan)
wenzelm@53079:   done
huffman@23045: 
hoelzl@31880: declare
hoelzl@56381:   DERIV_arcsin[THEN DERIV_chain2, derivative_intros]
hoelzl@56381:   DERIV_arccos[THEN DERIV_chain2, derivative_intros]
hoelzl@56381:   DERIV_arctan[THEN DERIV_chain2, derivative_intros]
hoelzl@31880: 
hoelzl@50346: lemma filterlim_tan_at_right: "filterlim tan at_bot (at_right (- pi/2))"
hoelzl@50346:   by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. - pi/2 < x \<and> x < pi/2" and P="\<lambda>x. True" and g=arctan])
hoelzl@56479:      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
hoelzl@50346:            intro!: tan_monotone exI[of _ "pi/2"])
hoelzl@50346: 
hoelzl@50346: lemma filterlim_tan_at_left: "filterlim tan at_top (at_left (pi/2))"
hoelzl@50346:   by (rule filterlim_at_top_at_left[where Q="\<lambda>x. - pi/2 < x \<and> x < pi/2" and P="\<lambda>x. True" and g=arctan])
hoelzl@56479:      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
hoelzl@50346:            intro!: tan_monotone exI[of _ "pi/2"])
hoelzl@50346: 
hoelzl@50346: lemma tendsto_arctan_at_top: "(arctan ---> (pi/2)) at_top"
hoelzl@50346: proof (rule tendstoI)
wenzelm@53079:   fix e :: real
wenzelm@53079:   assume "0 < e"
hoelzl@50346:   def y \<equiv> "pi/2 - min (pi/2) e"
hoelzl@50346:   then have y: "0 \<le> y" "y < pi/2" "pi/2 \<le> e + y"
hoelzl@50346:     using `0 < e` by auto
hoelzl@50346: 
hoelzl@50346:   show "eventually (\<lambda>x. dist (arctan x) (pi / 2) < e) at_top"
hoelzl@50346:   proof (intro eventually_at_top_dense[THEN iffD2] exI allI impI)
wenzelm@53079:     fix x
wenzelm@53079:     assume "tan y < x"
hoelzl@50346:     then have "arctan (tan y) < arctan x"
hoelzl@50346:       by (simp add: arctan_less_iff)
hoelzl@50346:     with y have "y < arctan x"
hoelzl@50346:       by (subst (asm) arctan_tan) simp_all
hoelzl@50346:     with arctan_ubound[of x, arith] y `0 < e`
hoelzl@50346:     show "dist (arctan x) (pi / 2) < e"
hoelzl@50346:       by (simp add: dist_real_def)
hoelzl@50346:   qed
hoelzl@50346: qed
hoelzl@50346: 
hoelzl@50346: lemma tendsto_arctan_at_bot: "(arctan ---> - (pi/2)) at_bot"
wenzelm@53079:   unfolding filterlim_at_bot_mirror arctan_minus
wenzelm@53079:   by (intro tendsto_minus tendsto_arctan_at_top)
wenzelm@53079: 
hoelzl@50346: 
huffman@23043: subsection {* More Theorems about Sin and Cos *}
huffman@23043: 
huffman@23052: lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
huffman@23052: proof -
huffman@23052:   let ?c = "cos (pi / 4)" and ?s = "sin (pi / 4)"
huffman@23052:   have nonneg: "0 \<le> ?c"
huffman@45308:     by (simp add: cos_ge_zero)
huffman@23052:   have "0 = cos (pi / 4 + pi / 4)"
huffman@23052:     by simp
wenzelm@53015:   also have "cos (pi / 4 + pi / 4) = ?c\<^sup>2 - ?s\<^sup>2"
huffman@23052:     by (simp only: cos_add power2_eq_square)
wenzelm@53015:   also have "\<dots> = 2 * ?c\<^sup>2 - 1"
huffman@23052:     by (simp add: sin_squared_eq)
wenzelm@53015:   finally have "?c\<^sup>2 = (sqrt 2 / 2)\<^sup>2"
huffman@23052:     by (simp add: power_divide)
huffman@23052:   thus ?thesis
huffman@23052:     using nonneg by (rule power2_eq_imp_eq) simp
huffman@23052: qed
huffman@23052: 
huffman@23052: lemma cos_30: "cos (pi / 6) = sqrt 3 / 2"
huffman@23052: proof -
huffman@23052:   let ?c = "cos (pi / 6)" and ?s = "sin (pi / 6)"
huffman@23052:   have pos_c: "0 < ?c"
huffman@23052:     by (rule cos_gt_zero, simp, simp)
huffman@23052:   have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
huffman@23066:     by simp
huffman@23052:   also have "\<dots> = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
huffman@23052:     by (simp only: cos_add sin_add)
wenzelm@53015:   also have "\<dots> = ?c * (?c\<^sup>2 - 3 * ?s\<^sup>2)"
nipkow@29667:     by (simp add: algebra_simps power2_eq_square)
wenzelm@53015:   finally have "?c\<^sup>2 = (sqrt 3 / 2)\<^sup>2"
huffman@23052:     using pos_c by (simp add: sin_squared_eq power_divide)
huffman@23052:   thus ?thesis
huffman@23052:     using pos_c [THEN order_less_imp_le]
huffman@23052:     by (rule power2_eq_imp_eq) simp
huffman@23052: qed
huffman@23052: 
huffman@23052: lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
wenzelm@53079:   by (simp add: sin_cos_eq cos_45)
huffman@23052: 
huffman@23052: lemma sin_60: "sin (pi / 3) = sqrt 3 / 2"
wenzelm@53079:   by (simp add: sin_cos_eq cos_30)
huffman@23052: 
huffman@23052: lemma cos_60: "cos (pi / 3) = 1 / 2"
wenzelm@53079:   apply (rule power2_eq_imp_eq)
wenzelm@53079:   apply (simp add: cos_squared_eq sin_60 power_divide)
wenzelm@53079:   apply (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
wenzelm@53079:   done
huffman@23052: 
huffman@23052: lemma sin_30: "sin (pi / 6) = 1 / 2"
wenzelm@53079:   by (simp add: sin_cos_eq cos_60)
huffman@23052: 
huffman@23052: lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
wenzelm@53079:   unfolding tan_def by (simp add: sin_30 cos_30)
huffman@23052: 
huffman@23052: lemma tan_45: "tan (pi / 4) = 1"
wenzelm@53079:   unfolding tan_def by (simp add: sin_45 cos_45)
huffman@23052: 
huffman@23052: lemma tan_60: "tan (pi / 3) = sqrt 3"
wenzelm@53079:   unfolding tan_def by (simp add: sin_60 cos_60)
huffman@23052: 
paulson@15383: lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi / 2) = (-1) ^ n"
paulson@15383: proof -
paulson@15383:   have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
nipkow@29667:     by (auto simp add: algebra_simps sin_add)
paulson@15383:   thus ?thesis
webertj@49962:     by (simp add: real_of_nat_Suc distrib_right add_divide_distrib
haftmann@57512:                   mult.commute [of pi])
paulson@15383: qed
paulson@15077: 
paulson@15077: lemma cos_2npi [simp]: "cos (2 * real (n::nat) * pi) = 1"
haftmann@57512:   by (simp add: cos_double mult.assoc power_add [symmetric] numeral_2_eq_2)
paulson@15077: 
paulson@15077: lemma cos_3over2_pi [simp]: "cos (3 / 2 * pi) = 0"
wenzelm@53079:   apply (subgoal_tac "cos (pi + pi/2) = 0", simp)
wenzelm@53079:   apply (subst cos_add, simp)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma sin_2npi [simp]: "sin (2 * real (n::nat) * pi) = 0"
haftmann@57512:   by (auto simp add: mult.assoc)
paulson@15077: 
paulson@15077: lemma sin_3over2_pi [simp]: "sin (3 / 2 * pi) = - 1"
wenzelm@53079:   apply (subgoal_tac "sin (pi + pi/2) = - 1", simp)
wenzelm@53079:   apply (subst sin_add, simp)
wenzelm@53079:   done
paulson@15077: 
paulson@15077: lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
wenzelm@53079:   apply (simp only: cos_add sin_add real_of_nat_Suc distrib_right distrib_left add_divide_distrib)
wenzelm@53079:   apply auto
wenzelm@53079:   done
wenzelm@53079: 
wenzelm@53079: lemma DERIV_cos_add [simp]: "DERIV (\<lambda>x. cos (x + k)) xa :> - sin (xa + k)"
hoelzl@56381:   by (auto intro!: derivative_eq_intros)
paulson@15077: 
paulson@15081: lemma sin_zero_abs_cos_one: "sin x = 0 ==> \<bar>cos x\<bar> = 1"
wenzelm@53079:   by (auto simp add: sin_zero_iff even_mult_two_ex)
paulson@15077: 
paulson@15077: lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
wenzelm@53079:   using sin_cos_squared_add3 [where x = x] by auto
wenzelm@53079: 
paulson@15077: 
hoelzl@29803: subsection {* Machins formula *}
hoelzl@29803: 
huffman@44746: lemma arctan_one: "arctan 1 = pi / 4"
huffman@44746:   by (rule arctan_unique, simp_all add: tan_45 m2pi_less_pi)
huffman@44746: 
wenzelm@53079: lemma tan_total_pi4:
wenzelm@53079:   assumes "\<bar>x\<bar> < 1"
wenzelm@53079:   shows "\<exists>z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
huffman@44746: proof
huffman@44746:   show "- (pi / 4) < arctan x \<and> arctan x < pi / 4 \<and> tan (arctan x) = x"
huffman@44746:     unfolding arctan_one [symmetric] arctan_minus [symmetric]
huffman@44746:     unfolding arctan_less_iff using assms by auto
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma arctan_add:
wenzelm@53079:   assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
hoelzl@29803:   shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
huffman@44746: proof (rule arctan_unique [symmetric])
huffman@44746:   have "- (pi / 4) \<le> arctan x" and "- (pi / 4) < arctan y"
huffman@44746:     unfolding arctan_one [symmetric] arctan_minus [symmetric]
huffman@44746:     unfolding arctan_le_iff arctan_less_iff using assms by auto
huffman@44746:   from add_le_less_mono [OF this]
huffman@44746:   show 1: "- (pi / 2) < arctan x + arctan y" by simp
huffman@44746:   have "arctan x \<le> pi / 4" and "arctan y < pi / 4"
huffman@44746:     unfolding arctan_one [symmetric]
huffman@44746:     unfolding arctan_le_iff arctan_less_iff using assms by auto
huffman@44746:   from add_le_less_mono [OF this]
huffman@44746:   show 2: "arctan x + arctan y < pi / 2" by simp
huffman@44746:   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
huffman@44746:     using cos_gt_zero_pi [OF 1 2] by (simp add: tan_add)
hoelzl@29803: qed
hoelzl@29803: 
hoelzl@29803: theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
hoelzl@29803: proof -
hoelzl@29803:   have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
hoelzl@29803:   from arctan_add[OF less_imp_le[OF this] this]
hoelzl@29803:   have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
hoelzl@29803:   moreover
hoelzl@29803:   have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
hoelzl@29803:   from arctan_add[OF less_imp_le[OF this] this]
hoelzl@29803:   have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
hoelzl@41970:   moreover
hoelzl@29803:   have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
hoelzl@29803:   from arctan_add[OF this]
hoelzl@29803:   have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
hoelzl@29803:   ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
huffman@44746:   thus ?thesis unfolding arctan_one by algebra
hoelzl@29803: qed
huffman@44746: 
wenzelm@53079: 
hoelzl@29803: subsection {* Introducing the arcus tangens power series *}
hoelzl@29803: 
wenzelm@53079: lemma monoseq_arctan_series:
wenzelm@53079:   fixes x :: real
wenzelm@53079:   assumes "\<bar>x\<bar> \<le> 1"
wenzelm@53079:   shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
wenzelm@53079: proof (cases "x = 0")
wenzelm@53079:   case True
wenzelm@53079:   thus ?thesis unfolding monoseq_def One_nat_def by auto
hoelzl@29803: next
hoelzl@29803:   case False
hoelzl@29803:   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
hoelzl@29803:   show "monoseq ?a"
hoelzl@29803:   proof -
wenzelm@53079:     {
wenzelm@53079:       fix n
wenzelm@53079:       fix x :: real
wenzelm@53079:       assume "0 \<le> x" and "x \<le> 1"
wenzelm@53079:       have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le>
wenzelm@53079:         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
hoelzl@29803:       proof (rule mult_mono)
wenzelm@53079:         show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))"
wenzelm@53079:           by (rule frac_le) simp_all
wenzelm@53079:         show "0 \<le> 1 / real (Suc (n * 2))"
wenzelm@53079:           by auto
wenzelm@53079:         show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)"
wenzelm@53079:           by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
wenzelm@53079:         show "0 \<le> x ^ Suc (Suc n * 2)"
wenzelm@53079:           by (rule zero_le_power) (simp add: `0 \<le> x`)
hoelzl@29803:       qed
hoelzl@29803:     } note mono = this
hoelzl@41970: 
hoelzl@29803:     show ?thesis
hoelzl@29803:     proof (cases "0 \<le> x")
hoelzl@29803:       case True from mono[OF this `x \<le> 1`, THEN allI]
wenzelm@53079:       show ?thesis unfolding Suc_eq_plus1[symmetric]
wenzelm@53079:         by (rule mono_SucI2)
hoelzl@29803:     next
wenzelm@53079:       case False
wenzelm@53079:       hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
hoelzl@29803:       from mono[OF this]
wenzelm@53079:       have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge>
wenzelm@53079:         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
nipkow@31790:       thus ?thesis unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI1[OF allI])
hoelzl@29803:     qed
hoelzl@29803:   qed
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma zeroseq_arctan_series:
wenzelm@53079:   fixes x :: real
wenzelm@53079:   assumes "\<bar>x\<bar> \<le> 1"
wenzelm@53079:   shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
wenzelm@53079: proof (cases "x = 0")
wenzelm@53079:   case True
wenzelm@53079:   thus ?thesis
wenzelm@53079:     unfolding One_nat_def by (auto simp add: tendsto_const)
hoelzl@29803: next
hoelzl@29803:   case False
hoelzl@29803:   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
hoelzl@29803:   show "?a ----> 0"
hoelzl@29803:   proof (cases "\<bar>x\<bar> < 1")
wenzelm@53079:     case True
wenzelm@53079:     hence "norm x < 1" by auto
huffman@44568:     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
huffman@30082:     have "(\<lambda>n. 1 / real (n + 1) * x ^ (n + 1)) ----> 0"
nipkow@31790:       unfolding inverse_eq_divide Suc_eq_plus1 by simp
huffman@30082:     then show ?thesis using pos2 by (rule LIMSEQ_linear)
hoelzl@29803:   next
wenzelm@53079:     case False
wenzelm@53079:     hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
wenzelm@53079:     hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x"
wenzelm@53079:       unfolding One_nat_def by auto
huffman@44568:     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] tendsto_const[of x]]
nipkow@31790:     show ?thesis unfolding n_eq Suc_eq_plus1 by auto
hoelzl@29803:   qed
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma summable_arctan_series:
wenzelm@53079:   fixes x :: real and n :: nat
wenzelm@53079:   assumes "\<bar>x\<bar> \<le> 1"
wenzelm@53079:   shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
wenzelm@53079:   (is "summable (?c x)")
hoelzl@29803:   by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
hoelzl@29803: 
wenzelm@53079: lemma less_one_imp_sqr_less_one:
wenzelm@53079:   fixes x :: real
wenzelm@53079:   assumes "\<bar>x\<bar> < 1"
wenzelm@53079:   shows "x\<^sup>2 < 1"
hoelzl@29803: proof -
paulson@54573:   have "\<bar>x\<^sup>2\<bar> < 1"
paulson@54573:     by (metis abs_power2 assms pos2 power2_abs power_0 power_strict_decreasing zero_eq_power2 zero_less_abs_iff) 
hoelzl@29803:   thus ?thesis using zero_le_power2 by auto
hoelzl@41970: qed
hoelzl@29803: 
wenzelm@53079: lemma DERIV_arctan_series:
wenzelm@53079:   assumes "\<bar> x \<bar> < 1"
wenzelm@53079:   shows "DERIV (\<lambda> x'. \<Sum> k. (-1)^k * (1 / real (k*2+1) * x' ^ (k*2+1))) x :> (\<Sum> k. (-1)^k * x^(k*2))"
wenzelm@53079:   (is "DERIV ?arctan _ :> ?Int")
hoelzl@29803: proof -
wenzelm@53079:   let ?f = "\<lambda>n. if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
wenzelm@53079: 
wenzelm@53079:   have n_even: "\<And>n :: nat. even n \<Longrightarrow> 2 * (n div 2) = n"
wenzelm@53079:     by presburger
wenzelm@53079:   then have if_eq: "\<And>n x'. ?f n * real (Suc n) * x'^n =
wenzelm@53079:     (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)"
wenzelm@53079:     by auto
wenzelm@53079: 
wenzelm@53079:   {
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "\<bar>x\<bar> < 1"
wenzelm@53079:     hence "x\<^sup>2 < 1" by (rule less_one_imp_sqr_less_one)
wenzelm@53076:     have "summable (\<lambda> n. -1 ^ n * (x\<^sup>2) ^n)"
wenzelm@53076:       by (rule summable_Leibniz(1), auto intro!: LIMSEQ_realpow_zero monoseq_realpow `x\<^sup>2 < 1` order_less_imp_le[OF `x\<^sup>2 < 1`])
hoelzl@29803:     hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
hoelzl@29803:   } note summable_Integral = this
hoelzl@29803: 
wenzelm@53079:   {
wenzelm@53079:     fix f :: "nat \<Rightarrow> real"
wenzelm@53079:     have "\<And>x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
hoelzl@29803:     proof
wenzelm@53079:       fix x :: real
wenzelm@53079:       assume "f sums x"
hoelzl@29803:       from sums_if[OF sums_zero this]
wenzelm@53079:       show "(\<lambda>n. if even n then f (n div 2) else 0) sums x"
wenzelm@53079:         by auto
hoelzl@29803:     next
wenzelm@53079:       fix x :: real
wenzelm@53079:       assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
haftmann@57512:       from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult.commute]]
hoelzl@29803:       show "f sums x" unfolding sums_def by auto
hoelzl@29803:     qed
hoelzl@29803:     hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
hoelzl@29803:   } note sums_even = this
hoelzl@29803: 
wenzelm@53079:   have Int_eq: "(\<Sum>n. ?f n * real (Suc n) * x^n) = ?Int"
haftmann@57512:     unfolding if_eq mult.commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
hoelzl@29803:     by auto
hoelzl@29803: 
wenzelm@53079:   {
wenzelm@53079:     fix x :: real
wenzelm@53079:     have if_eq': "\<And>n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
hoelzl@29803:       (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
hoelzl@29803:       using n_even by auto
wenzelm@53079:     have idx_eq: "\<And>n. n * 2 + 1 = Suc (2 * n)" by auto
wenzelm@53079:     have "(\<Sum>n. ?f n * x^(Suc n)) = ?arctan x"
wenzelm@53079:       unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
hoelzl@29803:       by auto
hoelzl@29803:   } note arctan_eq = this
hoelzl@29803: 
hoelzl@29803:   have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
hoelzl@29803:   proof (rule DERIV_power_series')
hoelzl@29803:     show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
wenzelm@53079:     {
wenzelm@53079:       fix x' :: real
wenzelm@53079:       assume x'_bounds: "x' \<in> {- 1 <..< 1}"
hoelzl@29803:       hence "\<bar>x'\<bar> < 1" by auto
hoelzl@29803: 
hoelzl@29803:       let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
hoelzl@29803:       show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
wenzelm@32960:         by (rule sums_summable[where l="0 + ?S"], rule sums_if, rule sums_zero, rule summable_sums, rule summable_Integral[OF `\<bar>x'\<bar> < 1`])
hoelzl@29803:     }
hoelzl@29803:   qed auto
hoelzl@29803:   thus ?thesis unfolding Int_eq arctan_eq .
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma arctan_series:
wenzelm@53079:   assumes "\<bar> x \<bar> \<le> 1"
wenzelm@53079:   shows "arctan x = (\<Sum>k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
wenzelm@53079:   (is "_ = suminf (\<lambda> n. ?c x n)")
hoelzl@29803: proof -
wenzelm@53079:   let ?c' = "\<lambda>x n. (-1)^n * x^(n*2)"
wenzelm@53079: 
wenzelm@53079:   {
wenzelm@53079:     fix r x :: real
wenzelm@53079:     assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
hoelzl@29803:     have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
wenzelm@53079:     from DERIV_arctan_series[OF this] have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
hoelzl@29803:   } note DERIV_arctan_suminf = this
hoelzl@29803: 
wenzelm@53079:   {
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "\<bar>x\<bar> \<le> 1"
wenzelm@53079:     note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]]
wenzelm@53079:   } note arctan_series_borders = this
wenzelm@53079: 
wenzelm@53079:   {
wenzelm@53079:     fix x :: real
wenzelm@53079:     assume "\<bar>x\<bar> < 1"
wenzelm@53079:     have "arctan x = (\<Sum>k. ?c x k)"
hoelzl@29803:     proof -
wenzelm@53079:       obtain r where "\<bar>x\<bar> < r" and "r < 1"
wenzelm@53079:         using dense[OF `\<bar>x\<bar> < 1`] by blast
wenzelm@53079:       hence "0 < r" and "-r < x" and "x < r" by auto
wenzelm@53079: 
wenzelm@53079:       have suminf_eq_arctan_bounded: "\<And>x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow>
wenzelm@53079:         suminf (?c x) - arctan x = suminf (?c a) - arctan a"
wenzelm@53079:       proof -
wenzelm@53079:         fix x a b
wenzelm@53079:         assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
wenzelm@53079:         hence "\<bar>x\<bar> < r" by auto
wenzelm@53079:         show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
wenzelm@53079:         proof (rule DERIV_isconst2[of "a" "b"])
wenzelm@53079:           show "a < b" and "a \<le> x" and "x \<le> b"
wenzelm@53079:             using `a < b` `a \<le> x` `x \<le> b` by auto
wenzelm@53079:           have "\<forall>x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
wenzelm@53079:           proof (rule allI, rule impI)
wenzelm@53079:             fix x
wenzelm@53079:             assume "-r < x \<and> x < r"
wenzelm@53079:             hence "\<bar>x\<bar> < r" by auto
wenzelm@53079:             hence "\<bar>x\<bar> < 1" using `r < 1` by auto
wenzelm@53079:             have "\<bar> - (x\<^sup>2) \<bar> < 1"
wenzelm@53079:               using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
wenzelm@53079:             hence "(\<lambda> n. (- (x\<^sup>2)) ^ n) sums (1 / (1 - (- (x\<^sup>2))))"
wenzelm@53079:               unfolding real_norm_def[symmetric] by (rule geometric_sums)
wenzelm@53079:             hence "(?c' x) sums (1 / (1 - (- (x\<^sup>2))))"
haftmann@57512:               unfolding power_mult_distrib[symmetric] power_mult mult.commute[of _ 2] by auto
wenzelm@53079:             hence suminf_c'_eq_geom: "inverse (1 + x\<^sup>2) = suminf (?c' x)"
wenzelm@53079:               using sums_unique unfolding inverse_eq_divide by auto
wenzelm@53079:             have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x\<^sup>2))"
wenzelm@53079:               unfolding suminf_c'_eq_geom
wenzelm@53079:               by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
lp15@56261:             from DERIV_diff [OF this DERIV_arctan]
wenzelm@53079:             show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
haftmann@54230:               by auto
wenzelm@53079:           qed
wenzelm@53079:           hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
wenzelm@53079:             using `-r < a` `b < r` by auto
wenzelm@53079:           thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
wenzelm@53079:             using `\<bar>x\<bar> < r` by auto
wenzelm@53079:           show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y"
wenzelm@53079:             using DERIV_in_rball DERIV_isCont by auto
wenzelm@32960:         qed
hoelzl@29803:       qed
wenzelm@53079: 
wenzelm@53079:       have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
wenzelm@53079:         unfolding Suc_eq_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero
wenzelm@53079:         by auto
wenzelm@53079: 
wenzelm@53079:       have "suminf (?c x) - arctan x = 0"
wenzelm@53079:       proof (cases "x = 0")
wenzelm@53079:         case True
wenzelm@53079:         thus ?thesis using suminf_arctan_zero by auto
wenzelm@53079:       next
wenzelm@53079:         case False
wenzelm@53079:         hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
wenzelm@53079:         have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
wenzelm@53079:           by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric])
wenzelm@53079:             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
wenzelm@53079:         moreover
wenzelm@53079:         have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
wenzelm@53079:           by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"])
paulson@54573:              (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
wenzelm@53079:         ultimately
wenzelm@53079:         show ?thesis using suminf_arctan_zero by auto
wenzelm@53079:       qed
wenzelm@53079:       thus ?thesis by auto
hoelzl@29803:     qed
wenzelm@53079:   } note when_less_one = this
hoelzl@29803: 
hoelzl@29803:   show "arctan x = suminf (\<lambda> n. ?c x n)"
hoelzl@29803:   proof (cases "\<bar>x\<bar> < 1")
wenzelm@53079:     case True
wenzelm@53079:     thus ?thesis by (rule when_less_one)
wenzelm@53079:   next
wenzelm@53079:     case False
wenzelm@53079:     hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
wenzelm@53079:     let ?a = "\<lambda>x n. \<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
hoelzl@56193:     let ?diff = "\<lambda> x n. \<bar> arctan x - (\<Sum> i<n. ?c x i)\<bar>"
wenzelm@53079:     {
wenzelm@53079:       fix n :: nat
hoelzl@29803:       have "0 < (1 :: real)" by auto
hoelzl@29803:       moreover
wenzelm@53079:       {
wenzelm@53079:         fix x :: real
wenzelm@53079:         assume "0 < x" and "x < 1"
wenzelm@53079:         hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
wenzelm@53079:         from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)"
wenzelm@53079:           by auto
wenzelm@32960:         note bounds = mp[OF arctan_series_borders(2)[OF `\<bar>x\<bar> \<le> 1`] this, unfolded when_less_one[OF `\<bar>x\<bar> < 1`, symmetric], THEN spec]
wenzelm@53079:         have "0 < 1 / real (n*2+1) * x^(n*2+1)"
wenzelm@53079:           by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
wenzelm@53079:         hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)"
wenzelm@53079:           by (rule abs_of_pos)
hoelzl@29803:         have "?diff x n \<le> ?a x n"
wenzelm@32960:         proof (cases "even n")
wenzelm@53079:           case True
wenzelm@53079:           hence sgn_pos: "(-1)^n = (1::real)" by auto
wenzelm@53079:           from `even n` obtain m where "2 * m = n"
wenzelm@53079:             unfolding even_mult_two_ex by auto
wenzelm@32960:           from bounds[of m, unfolded this atLeastAtMost_iff]
hoelzl@56193:           have "\<bar>arctan x - (\<Sum>i<n. (?c x i))\<bar> \<le> (\<Sum>i<n + 1. (?c x i)) - (\<Sum>i<n. (?c x i))"
wenzelm@53079:             by auto
wenzelm@32960:           also have "\<dots> = ?c x n" unfolding One_nat_def by auto
wenzelm@32960:           also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
wenzelm@32960:           finally show ?thesis .
wenzelm@32960:         next
wenzelm@53079:           case False
wenzelm@53079:           hence sgn_neg: "(-1)^n = (-1::real)" by auto
wenzelm@53079:           from `odd n` obtain m where m_def: "2 * m + 1 = n"
wenzelm@53079:             unfolding odd_Suc_mult_two_ex by auto
wenzelm@32960:           hence m_plus: "2 * (m + 1) = n + 1" by auto
wenzelm@32960:           from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
hoelzl@56193:           have "\<bar>arctan x - (\<Sum>i<n. (?c x i))\<bar> \<le> (\<Sum>i<n. (?c x i)) - (\<Sum>i<n+1. (?c x i))"
wenzelm@53079:             by auto
wenzelm@32960:           also have "\<dots> = - ?c x n" unfolding One_nat_def by auto
wenzelm@32960:           also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
wenzelm@32960:           finally show ?thesis .
wenzelm@32960:         qed
hoelzl@29803:         hence "0 \<le> ?a x n - ?diff x n" by auto
hoelzl@29803:       }
hoelzl@29803:       hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
hoelzl@29803:       moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
haftmann@54230:         unfolding diff_conv_add_uminus divide_inverse
wenzelm@53079:         by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan
haftmann@54230:           isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum
haftmann@54230:           simp del: add_uminus_conv_diff)
wenzelm@53079:       ultimately have "0 \<le> ?a 1 n - ?diff 1 n"
wenzelm@53079:         by (rule LIM_less_bound)
hoelzl@29803:       hence "?diff 1 n \<le> ?a 1 n" by auto
hoelzl@29803:     }
huffman@30082:     have "?a 1 ----> 0"
huffman@44568:       unfolding tendsto_rabs_zero_iff power_one divide_inverse One_nat_def
huffman@44568:       by (auto intro!: tendsto_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
hoelzl@29803:     have "?diff 1 ----> 0"
hoelzl@29803:     proof (rule LIMSEQ_I)
wenzelm@53079:       fix r :: real
wenzelm@53079:       assume "0 < r"
wenzelm@53079:       obtain N :: nat where N_I: "\<And>n. N \<le> n \<Longrightarrow> ?a 1 n < r"
wenzelm@53079:         using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
wenzelm@53079:       {
wenzelm@53079:         fix n
wenzelm@53079:         assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
wenzelm@53079:         have "norm (?diff 1 n - 0) < r" by auto
wenzelm@53079:       }
hoelzl@29803:       thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
hoelzl@29803:     qed
huffman@44710:     from this [unfolded tendsto_rabs_zero_iff, THEN tendsto_add [OF _ tendsto_const], of "- arctan 1", THEN tendsto_minus]
hoelzl@29803:     have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
hoelzl@29803:     hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
hoelzl@41970: 
hoelzl@29803:     show ?thesis
wenzelm@53079:     proof (cases "x = 1")
wenzelm@53079:       case True
wenzelm@53079:       then show ?thesis by (simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
wenzelm@53079:     next
wenzelm@53079:       case False
wenzelm@53079:       hence "x = -1" using `\<bar>x\<bar> = 1` by auto
hoelzl@41970: 
hoelzl@29803:       have "- (pi / 2) < 0" using pi_gt_zero by auto
hoelzl@29803:       have "- (2 * pi) < 0" using pi_gt_zero by auto
hoelzl@41970: 
wenzelm@53079:       have c_minus_minus: "\<And>i. ?c (- 1) i = - ?c 1 i"
wenzelm@53079:         unfolding One_nat_def by auto
wenzelm@53079: 
wenzelm@53079:       have "arctan (- 1) = arctan (tan (-(pi / 4)))"
wenzelm@53079:         unfolding tan_45 tan_minus ..
wenzelm@53079:       also have "\<dots> = - (pi / 4)"
wenzelm@53079:         by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
wenzelm@53079:       also have "\<dots> = - (arctan (tan (pi / 4)))"
wenzelm@53079:         unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
wenzelm@53079:       also have "\<dots> = - (arctan 1)"
wenzelm@53079:         unfolding tan_45 ..
wenzelm@53079:       also have "\<dots> = - (\<Sum> i. ?c 1 i)"
wenzelm@53079:         using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
wenzelm@53079:       also have "\<dots> = (\<Sum> i. ?c (- 1) i)"
wenzelm@53079:         using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]]
wenzelm@53079:         unfolding c_minus_minus by auto
hoelzl@29803:       finally show ?thesis using `x = -1` by auto
hoelzl@29803:     qed
hoelzl@29803:   qed
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma arctan_half:
wenzelm@53079:   fixes x :: real
wenzelm@53076:   shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x\<^sup>2)))"
hoelzl@29803: proof -
wenzelm@53079:   obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x"
wenzelm@53079:     using tan_total by blast
wenzelm@53079:   hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2"
wenzelm@53079:     by auto
wenzelm@53079: 
hoelzl@29803:   have "0 < cos y" using cos_gt_zero_pi[OF low high] .
wenzelm@53079:   hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y)\<^sup>2) = cos y"
wenzelm@53079:     by auto
wenzelm@53079: 
wenzelm@53079:   have "1 + (tan y)\<^sup>2 = 1 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
wenzelm@53079:     unfolding tan_def power_divide ..
wenzelm@53079:   also have "\<dots> = (cos y)\<^sup>2 / (cos y)\<^sup>2 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
wenzelm@53079:     using `cos y \<noteq> 0` by auto
wenzelm@53079:   also have "\<dots> = 1 / (cos y)\<^sup>2"
wenzelm@53079:     unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
wenzelm@53076:   finally have "1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2" .
hoelzl@29803: 
wenzelm@53079:   have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)"
hoelzl@56571:     unfolding tan_def using `cos y \<noteq> 0` by (simp add: field_simps)
wenzelm@53079:   also have "\<dots> = tan y / (1 + 1 / cos y)"
wenzelm@53079:     using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
wenzelm@53079:   also have "\<dots> = tan y / (1 + 1 / sqrt ((cos y)\<^sup>2))"
wenzelm@53079:     unfolding cos_sqrt ..
wenzelm@53079:   also have "\<dots> = tan y / (1 + sqrt (1 / (cos y)\<^sup>2))"
wenzelm@53079:     unfolding real_sqrt_divide by auto
wenzelm@53079:   finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)\<^sup>2))"
wenzelm@53079:     unfolding `1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2` .
wenzelm@53079: 
wenzelm@53079:   have "arctan x = y"
wenzelm@53079:     using arctan_tan low high y_eq by auto
wenzelm@53079:   also have "\<dots> = 2 * (arctan (tan (y/2)))"
wenzelm@53079:     using arctan_tan[OF low2 high2] by auto
wenzelm@53079:   also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))"
wenzelm@53079:     unfolding tan_half by auto
wenzelm@53079:   finally show ?thesis
wenzelm@53079:     unfolding eq `tan y = x` .
hoelzl@29803: qed
hoelzl@29803: 
wenzelm@53079: lemma arctan_monotone: "x < y \<Longrightarrow> arctan x < arctan y"
wenzelm@53079:   by (simp only: arctan_less_iff)
wenzelm@53079: 
wenzelm@53079: lemma arctan_monotone': "x \<le> y \<Longrightarrow> arctan x \<le> arctan y"
wenzelm@53079:   by (simp only: arctan_le_iff)
huffman@44746: 
huffman@44746: lemma arctan_inverse:
wenzelm@53079:   assumes "x \<noteq> 0"
wenzelm@53079:   shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
huffman@44746: proof (rule arctan_unique)
huffman@44746:   show "- (pi / 2) < sgn x * pi / 2 - arctan x"
huffman@44746:     using arctan_bounded [of x] assms
huffman@44746:     unfolding sgn_real_def
huffman@44746:     apply (auto simp add: algebra_simps)
huffman@44746:     apply (drule zero_less_arctan_iff [THEN iffD2])
huffman@44746:     apply arith
huffman@44746:     done
huffman@44746:   show "sgn x * pi / 2 - arctan x < pi / 2"
huffman@44746:     using arctan_bounded [of "- x"] assms
huffman@44746:     unfolding sgn_real_def arctan_minus
haftmann@54489:     by (auto simp add: algebra_simps)
huffman@44746:   show "tan (sgn x * pi / 2 - arctan x) = 1 / x"
huffman@44746:     unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
huffman@44746:     unfolding sgn_real_def
hoelzl@56479:     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
hoelzl@29803: qed
hoelzl@29803: 
hoelzl@29803: theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
hoelzl@29803: proof -
huffman@44746:   have "pi / 4 = arctan 1" using arctan_one by auto
hoelzl@29803:   also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
hoelzl@29803:   finally show ?thesis by auto
hoelzl@29803: qed
paulson@15077: 
wenzelm@53079: 
huffman@22978: subsection {* Existence of Polar Coordinates *}
paulson@15077: 
wenzelm@53015: lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
wenzelm@53079:   apply (rule power2_le_imp_le [OF _ zero_le_one])
wenzelm@53079:   apply (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
wenzelm@53079:   done
paulson@15077: 
huffman@22978: lemma cos_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> cos (arccos y) = y"
wenzelm@53079:   by (simp add: abs_le_iff)
paulson@15077: 
wenzelm@53015: lemma sin_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> sin (arccos y) = sqrt (1 - y\<^sup>2)"
wenzelm@53079:   by (simp add: sin_arccos abs_le_iff)
paulson@15077: 
huffman@22978: lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
paulson@15228: 
huffman@23045: lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
paulson@15077: 
paulson@15077: lemma polar_Ex: "\<exists>r a. x = r * cos a & y = r * sin a"
paulson@54573: proof -
paulson@54573:   have polar_ex1: "\<And>y. 0 < y \<Longrightarrow> \<exists>r a. x = r * cos a & y = r * sin a"
paulson@54573:     apply (rule_tac x = "sqrt (x\<^sup>2 + y\<^sup>2)" in exI)
paulson@54573:     apply (rule_tac x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))" in exI)
paulson@54573:     apply (simp add: cos_arccos_lemma1 sin_arccos_lemma1 power_divide
paulson@54573:                      real_sqrt_mult [symmetric] right_diff_distrib)
paulson@54573:     done
paulson@54573:   show ?thesis
paulson@54573:   proof (cases "0::real" y rule: linorder_cases)
paulson@54573:     case less 
paulson@54573:       then show ?thesis by (rule polar_ex1)
paulson@54573:   next
paulson@54573:     case equal
paulson@54573:       then show ?thesis
paulson@54573:         by (force simp add: intro!: cos_zero sin_zero)
paulson@54573:   next
paulson@54573:     case greater
paulson@54573:       then show ?thesis 
paulson@54573:      using polar_ex1 [where y="-y"]
paulson@54573:     by auto (metis cos_minus minus_minus minus_mult_right sin_minus)
paulson@54573:   qed
paulson@54573: qed
paulson@15077: 
huffman@30082: end




