src/HOL/Transcendental.thy
author hoelzl
Tue Nov 05 09:44:57 2013 +0100 (2013-11-05)
changeset 54257 5c7a3b6b05a9
parent 54230 b1d955791529
child 54489 03ff4d1e6784
permissions -rw-r--r--
generalize SUP and INF to the syntactic type classes Sup and Inf
     1 (*  Title:      HOL/Transcendental.thy
     2     Author:     Jacques D. Fleuriot, University of Cambridge, University of Edinburgh
     3     Author:     Lawrence C Paulson
     4     Author:     Jeremy Avigad
     5 *)
     6 
     7 header{*Power Series, Transcendental Functions etc.*}
     8 
     9 theory Transcendental
    10 imports Fact Series Deriv NthRoot
    11 begin
    12 
    13 subsection {* Properties of Power Series *}
    14 
    15 lemma lemma_realpow_diff:
    16   fixes y :: "'a::monoid_mult"
    17   shows "p \<le> n \<Longrightarrow> y ^ (Suc n - p) = (y ^ (n - p)) * y"
    18 proof -
    19   assume "p \<le> n"
    20   hence "Suc n - p = Suc (n - p)" by (rule Suc_diff_le)
    21   thus ?thesis by (simp add: power_commutes)
    22 qed
    23 
    24 lemma lemma_realpow_diff_sumr:
    25   fixes y :: "'a::{comm_semiring_0,monoid_mult}"
    26   shows
    27     "(\<Sum>p=0..<Suc n. (x ^ p) * y ^ (Suc n - p)) =
    28       y * (\<Sum>p=0..<Suc n. (x ^ p) * y ^ (n - p))"
    29   by (simp add: setsum_right_distrib lemma_realpow_diff mult_ac del: setsum_op_ivl_Suc)
    30 
    31 lemma lemma_realpow_diff_sumr2:
    32   fixes y :: "'a::{comm_ring,monoid_mult}"
    33   shows
    34     "x ^ (Suc n) - y ^ (Suc n) =
    35       (x - y) * (\<Sum>p=0..<Suc n. (x ^ p) * y ^ (n - p))"
    36   apply (induct n)
    37   apply simp
    38   apply (simp del: setsum_op_ivl_Suc)
    39   apply (subst setsum_op_ivl_Suc)
    40   apply (subst lemma_realpow_diff_sumr)
    41   apply (simp add: distrib_left del: setsum_op_ivl_Suc)
    42   apply (subst mult_left_commute [of "x - y"])
    43   apply (erule subst)
    44   apply (simp add: algebra_simps)
    45   done
    46 
    47 lemma lemma_realpow_rev_sumr:
    48   "(\<Sum>p=0..<Suc n. (x ^ p) * (y ^ (n - p))) =
    49     (\<Sum>p=0..<Suc n. (x ^ (n - p)) * (y ^ p))"
    50   apply (rule setsum_reindex_cong [where f="\<lambda>i. n - i"])
    51   apply (rule inj_onI, simp)
    52   apply auto
    53   apply (rule_tac x="n - x" in image_eqI, simp, simp)
    54   done
    55 
    56 text{*Power series has a `circle` of convergence, i.e. if it sums for @{term
    57   x}, then it sums absolutely for @{term z} with @{term "\<bar>z\<bar> < \<bar>x\<bar>"}.*}
    58 
    59 lemma powser_insidea:
    60   fixes x z :: "'a::real_normed_div_algebra"
    61   assumes 1: "summable (\<lambda>n. f n * x ^ n)"
    62     and 2: "norm z < norm x"
    63   shows "summable (\<lambda>n. norm (f n * z ^ n))"
    64 proof -
    65   from 2 have x_neq_0: "x \<noteq> 0" by clarsimp
    66   from 1 have "(\<lambda>n. f n * x ^ n) ----> 0"
    67     by (rule summable_LIMSEQ_zero)
    68   hence "convergent (\<lambda>n. f n * x ^ n)"
    69     by (rule convergentI)
    70   hence "Cauchy (\<lambda>n. f n * x ^ n)"
    71     by (rule convergent_Cauchy)
    72   hence "Bseq (\<lambda>n. f n * x ^ n)"
    73     by (rule Cauchy_Bseq)
    74   then obtain K where 3: "0 < K" and 4: "\<forall>n. norm (f n * x ^ n) \<le> K"
    75     by (simp add: Bseq_def, safe)
    76   have "\<exists>N. \<forall>n\<ge>N. norm (norm (f n * z ^ n)) \<le>
    77                    K * norm (z ^ n) * inverse (norm (x ^ n))"
    78   proof (intro exI allI impI)
    79     fix n::nat
    80     assume "0 \<le> n"
    81     have "norm (norm (f n * z ^ n)) * norm (x ^ n) =
    82           norm (f n * x ^ n) * norm (z ^ n)"
    83       by (simp add: norm_mult abs_mult)
    84     also have "\<dots> \<le> K * norm (z ^ n)"
    85       by (simp only: mult_right_mono 4 norm_ge_zero)
    86     also have "\<dots> = K * norm (z ^ n) * (inverse (norm (x ^ n)) * norm (x ^ n))"
    87       by (simp add: x_neq_0)
    88     also have "\<dots> = K * norm (z ^ n) * inverse (norm (x ^ n)) * norm (x ^ n)"
    89       by (simp only: mult_assoc)
    90     finally show "norm (norm (f n * z ^ n)) \<le>
    91                   K * norm (z ^ n) * inverse (norm (x ^ n))"
    92       by (simp add: mult_le_cancel_right x_neq_0)
    93   qed
    94   moreover have "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x ^ n)))"
    95   proof -
    96     from 2 have "norm (norm (z * inverse x)) < 1"
    97       using x_neq_0
    98       by (simp add: norm_mult nonzero_norm_inverse divide_inverse [where 'a=real, symmetric])
    99     hence "summable (\<lambda>n. norm (z * inverse x) ^ n)"
   100       by (rule summable_geometric)
   101     hence "summable (\<lambda>n. K * norm (z * inverse x) ^ n)"
   102       by (rule summable_mult)
   103     thus "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x ^ n)))"
   104       using x_neq_0
   105       by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
   106                     power_inverse norm_power mult_assoc)
   107   qed
   108   ultimately show "summable (\<lambda>n. norm (f n * z ^ n))"
   109     by (rule summable_comparison_test)
   110 qed
   111 
   112 lemma powser_inside:
   113   fixes f :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}"
   114   shows
   115     "summable (\<lambda>n. f n * (x ^ n)) \<Longrightarrow> norm z < norm x \<Longrightarrow>
   116       summable (\<lambda>n. f n * (z ^ n))"
   117   by (rule powser_insidea [THEN summable_norm_cancel])
   118 
   119 lemma sum_split_even_odd:
   120   fixes f :: "nat \<Rightarrow> real"
   121   shows
   122     "(\<Sum> i = 0 ..< 2 * n. if even i then f i else g i) =
   123      (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1))"
   124 proof (induct n)
   125   case 0
   126   then show ?case by simp
   127 next
   128   case (Suc n)
   129   have "(\<Sum> i = 0 ..< 2 * Suc n. if even i then f i else g i) =
   130     (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
   131     using Suc.hyps unfolding One_nat_def by auto
   132   also have "\<dots> = (\<Sum> i = 0 ..< Suc n. f (2 * i)) + (\<Sum> i = 0 ..< Suc n. g (2 * i + 1))"
   133     by auto
   134   finally show ?case .
   135 qed
   136 
   137 lemma sums_if':
   138   fixes g :: "nat \<Rightarrow> real"
   139   assumes "g sums x"
   140   shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
   141   unfolding sums_def
   142 proof (rule LIMSEQ_I)
   143   fix r :: real
   144   assume "0 < r"
   145   from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
   146   obtain no where no_eq: "\<And> n. n \<ge> no \<Longrightarrow> (norm (setsum g { 0..<n } - x) < r)" by blast
   147 
   148   let ?SUM = "\<lambda> m. \<Sum> i = 0 ..< m. if even i then 0 else g ((i - 1) div 2)"
   149   {
   150     fix m
   151     assume "m \<ge> 2 * no"
   152     hence "m div 2 \<ge> no" by auto
   153     have sum_eq: "?SUM (2 * (m div 2)) = setsum g { 0 ..< m div 2 }"
   154       using sum_split_even_odd by auto
   155     hence "(norm (?SUM (2 * (m div 2)) - x) < r)"
   156       using no_eq unfolding sum_eq using `m div 2 \<ge> no` by auto
   157     moreover
   158     have "?SUM (2 * (m div 2)) = ?SUM m"
   159     proof (cases "even m")
   160       case True
   161       show ?thesis
   162         unfolding even_nat_div_two_times_two[OF True, unfolded numeral_2_eq_2[symmetric]] ..
   163     next
   164       case False
   165       hence "even (Suc m)" by auto
   166       from even_nat_div_two_times_two[OF this, unfolded numeral_2_eq_2[symmetric]]
   167         odd_nat_plus_one_div_two[OF False, unfolded numeral_2_eq_2[symmetric]]
   168       have eq: "Suc (2 * (m div 2)) = m" by auto
   169       hence "even (2 * (m div 2))" using `odd m` by auto
   170       have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
   171       also have "\<dots> = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
   172       finally show ?thesis by auto
   173     qed
   174     ultimately have "(norm (?SUM m - x) < r)" by auto
   175   }
   176   thus "\<exists> no. \<forall> m \<ge> no. norm (?SUM m - x) < r" by blast
   177 qed
   178 
   179 lemma sums_if:
   180   fixes g :: "nat \<Rightarrow> real"
   181   assumes "g sums x" and "f sums y"
   182   shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
   183 proof -
   184   let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
   185   {
   186     fix B T E
   187     have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
   188       by (cases B) auto
   189   } note if_sum = this
   190   have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
   191     using sums_if'[OF `g sums x`] .
   192   {
   193     have "?s 0 = 0" by auto
   194     have Suc_m1: "\<And> n. Suc n - 1 = n" by auto
   195     have if_eq: "\<And>B T E. (if \<not> B then T else E) = (if B then E else T)" by auto
   196 
   197     have "?s sums y" using sums_if'[OF `f sums y`] .
   198     from this[unfolded sums_def, THEN LIMSEQ_Suc]
   199     have "(\<lambda> n. if even n then f (n div 2) else 0) sums y"
   200       unfolding sums_def setsum_shift_lb_Suc0_0_upt[where f="?s", OF `?s 0 = 0`, symmetric]
   201                 image_Suc_atLeastLessThan[symmetric] setsum_reindex[OF inj_Suc, unfolded comp_def]
   202                 even_Suc Suc_m1 if_eq .
   203   }
   204   from sums_add[OF g_sums this] show ?thesis unfolding if_sum .
   205 qed
   206 
   207 subsection {* Alternating series test / Leibniz formula *}
   208 
   209 lemma sums_alternating_upper_lower:
   210   fixes a :: "nat \<Rightarrow> real"
   211   assumes mono: "\<And>n. a (Suc n) \<le> a n" and a_pos: "\<And>n. 0 \<le> a n" and "a ----> 0"
   212   shows "\<exists>l. ((\<forall>n. (\<Sum>i=0..<2*n. -1^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i=0..<2*n. -1^i*a i) ----> l) \<and>
   213              ((\<forall>n. l \<le> (\<Sum>i=0..<2*n + 1. -1^i*a i)) \<and> (\<lambda> n. \<Sum>i=0..<2*n + 1. -1^i*a i) ----> l)"
   214   (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
   215 proof (rule nested_sequence_unique)
   216   have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" unfolding One_nat_def by auto
   217 
   218   show "\<forall>n. ?f n \<le> ?f (Suc n)"
   219   proof
   220     fix n
   221     show "?f n \<le> ?f (Suc n)" using mono[of "2*n"] by auto
   222   qed
   223   show "\<forall>n. ?g (Suc n) \<le> ?g n"
   224   proof
   225     fix n
   226     show "?g (Suc n) \<le> ?g n" using mono[of "Suc (2*n)"]
   227       unfolding One_nat_def by auto
   228   qed
   229   show "\<forall>n. ?f n \<le> ?g n"
   230   proof
   231     fix n
   232     show "?f n \<le> ?g n" using fg_diff a_pos
   233       unfolding One_nat_def by auto
   234   qed
   235   show "(\<lambda>n. ?f n - ?g n) ----> 0" unfolding fg_diff
   236   proof (rule LIMSEQ_I)
   237     fix r :: real
   238     assume "0 < r"
   239     with `a ----> 0`[THEN LIMSEQ_D] obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r"
   240       by auto
   241     hence "\<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
   242     thus "\<exists>N. \<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
   243   qed
   244 qed
   245 
   246 lemma summable_Leibniz':
   247   fixes a :: "nat \<Rightarrow> real"
   248   assumes a_zero: "a ----> 0"
   249     and a_pos: "\<And> n. 0 \<le> a n"
   250     and a_monotone: "\<And> n. a (Suc n) \<le> a n"
   251   shows summable: "summable (\<lambda> n. (-1)^n * a n)"
   252     and "\<And>n. (\<Sum>i=0..<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
   253     and "(\<lambda>n. \<Sum>i=0..<2*n. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
   254     and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i=0..<2*n+1. (-1)^i*a i)"
   255     and "(\<lambda>n. \<Sum>i=0..<2*n+1. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
   256 proof -
   257   let ?S = "\<lambda>n. (-1)^n * a n"
   258   let ?P = "\<lambda>n. \<Sum>i=0..<n. ?S i"
   259   let ?f = "\<lambda>n. ?P (2 * n)"
   260   let ?g = "\<lambda>n. ?P (2 * n + 1)"
   261   obtain l :: real
   262     where below_l: "\<forall> n. ?f n \<le> l"
   263       and "?f ----> l"
   264       and above_l: "\<forall> n. l \<le> ?g n"
   265       and "?g ----> l"
   266     using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
   267 
   268   let ?Sa = "\<lambda>m. \<Sum> n = 0..<m. ?S n"
   269   have "?Sa ----> l"
   270   proof (rule LIMSEQ_I)
   271     fix r :: real
   272     assume "0 < r"
   273     with `?f ----> l`[THEN LIMSEQ_D]
   274     obtain f_no where f: "\<And> n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r" by auto
   275 
   276     from `0 < r` `?g ----> l`[THEN LIMSEQ_D]
   277     obtain g_no where g: "\<And> n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r" by auto
   278 
   279     {
   280       fix n :: nat
   281       assume "n \<ge> (max (2 * f_no) (2 * g_no))"
   282       hence "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
   283       have "norm (?Sa n - l) < r"
   284       proof (cases "even n")
   285         case True
   286         from even_nat_div_two_times_two[OF this]
   287         have n_eq: "2 * (n div 2) = n"
   288           unfolding numeral_2_eq_2[symmetric] by auto
   289         with `n \<ge> 2 * f_no` have "n div 2 \<ge> f_no"
   290           by auto
   291         from f[OF this] show ?thesis
   292           unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
   293       next
   294         case False
   295         hence "even (n - 1)" by simp
   296         from even_nat_div_two_times_two[OF this]
   297         have n_eq: "2 * ((n - 1) div 2) = n - 1"
   298           unfolding numeral_2_eq_2[symmetric] by auto
   299         hence range_eq: "n - 1 + 1 = n"
   300           using odd_pos[OF False] by auto
   301 
   302         from n_eq `n \<ge> 2 * g_no` have "(n - 1) div 2 \<ge> g_no"
   303           by auto
   304         from g[OF this] show ?thesis
   305           unfolding n_eq atLeastLessThanSuc_atLeastAtMost range_eq .
   306       qed
   307     }
   308     thus "\<exists>no. \<forall>n \<ge> no. norm (?Sa n - l) < r" by blast
   309   qed
   310   hence sums_l: "(\<lambda>i. (-1)^i * a i) sums l"
   311     unfolding sums_def atLeastLessThanSuc_atLeastAtMost[symmetric] .
   312   thus "summable ?S" using summable_def by auto
   313 
   314   have "l = suminf ?S" using sums_unique[OF sums_l] .
   315 
   316   fix n
   317   show "suminf ?S \<le> ?g n"
   318     unfolding sums_unique[OF sums_l, symmetric] using above_l by auto
   319   show "?f n \<le> suminf ?S"
   320     unfolding sums_unique[OF sums_l, symmetric] using below_l by auto
   321   show "?g ----> suminf ?S"
   322     using `?g ----> l` `l = suminf ?S` by auto
   323   show "?f ----> suminf ?S"
   324     using `?f ----> l` `l = suminf ?S` by auto
   325 qed
   326 
   327 theorem summable_Leibniz:
   328   fixes a :: "nat \<Rightarrow> real"
   329   assumes a_zero: "a ----> 0" and "monoseq a"
   330   shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
   331     and "0 < a 0 \<longrightarrow>
   332       (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n. -1^i * a i .. \<Sum>i=0..<2*n+1. -1^i * a i})" (is "?pos")
   333     and "a 0 < 0 \<longrightarrow>
   334       (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n+1. -1^i * a i .. \<Sum>i=0..<2*n. -1^i * a i})" (is "?neg")
   335     and "(\<lambda>n. \<Sum>i=0..<2*n. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?f")
   336     and "(\<lambda>n. \<Sum>i=0..<2*n+1. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?g")
   337 proof -
   338   have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
   339   proof (cases "(\<forall> n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
   340     case True
   341     hence ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m" and ge0: "\<And> n. 0 \<le> a n"
   342       by auto
   343     {
   344       fix n
   345       have "a (Suc n) \<le> a n"
   346         using ord[where n="Suc n" and m=n] by auto
   347     } note mono = this
   348     note leibniz = summable_Leibniz'[OF `a ----> 0` ge0]
   349     from leibniz[OF mono]
   350     show ?thesis using `0 \<le> a 0` by auto
   351   next
   352     let ?a = "\<lambda> n. - a n"
   353     case False
   354     with monoseq_le[OF `monoseq a` `a ----> 0`]
   355     have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
   356     hence ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n"
   357       by auto
   358     {
   359       fix n
   360       have "?a (Suc n) \<le> ?a n" using ord[where n="Suc n" and m=n]
   361         by auto
   362     } note monotone = this
   363     note leibniz =
   364       summable_Leibniz'[OF _ ge0, of "\<lambda>x. x",
   365         OF tendsto_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
   366     have "summable (\<lambda> n. (-1)^n * ?a n)"
   367       using leibniz(1) by auto
   368     then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l"
   369       unfolding summable_def by auto
   370     from this[THEN sums_minus] have "(\<lambda> n. (-1)^n * a n) sums -l"
   371       by auto
   372     hence ?summable unfolding summable_def by auto
   373     moreover
   374     have "\<And>a b :: real. \<bar>- a - - b\<bar> = \<bar>a - b\<bar>"
   375       unfolding minus_diff_minus by auto
   376 
   377     from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
   378     have move_minus: "(\<Sum>n. - (-1 ^ n * a n)) = - (\<Sum>n. -1 ^ n * a n)"
   379       by auto
   380 
   381     have ?pos using `0 \<le> ?a 0` by auto
   382     moreover have ?neg
   383       using leibniz(2,4)
   384       unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le
   385       by auto
   386     moreover have ?f and ?g
   387       using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN tendsto_minus_cancel]
   388       by auto
   389     ultimately show ?thesis by auto
   390   qed
   391   from this[THEN conjunct1]
   392     this[THEN conjunct2, THEN conjunct1]
   393     this[THEN conjunct2, THEN conjunct2, THEN conjunct1]
   394     this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct1]
   395     this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct2]
   396   show ?summable and ?pos and ?neg and ?f and ?g .
   397 qed
   398 
   399 subsection {* Term-by-Term Differentiability of Power Series *}
   400 
   401 definition diffs :: "(nat => 'a::ring_1) => nat => 'a"
   402   where "diffs c = (\<lambda>n. of_nat (Suc n) * c(Suc n))"
   403 
   404 text{*Lemma about distributing negation over it*}
   405 lemma diffs_minus: "diffs (\<lambda>n. - c n) = (\<lambda>n. - diffs c n)"
   406   by (simp add: diffs_def)
   407 
   408 lemma sums_Suc_imp:
   409   assumes f: "f 0 = 0"
   410   shows "(\<lambda>n. f (Suc n)) sums s \<Longrightarrow> (\<lambda>n. f n) sums s"
   411   unfolding sums_def
   412   apply (rule LIMSEQ_imp_Suc)
   413   apply (subst setsum_shift_lb_Suc0_0_upt [where f=f, OF f, symmetric])
   414   apply (simp only: setsum_shift_bounds_Suc_ivl)
   415   done
   416 
   417 lemma diffs_equiv:
   418   fixes x :: "'a::{real_normed_vector, ring_1}"
   419   shows "summable (\<lambda>n. (diffs c)(n) * (x ^ n)) \<Longrightarrow>
   420       (\<lambda>n. of_nat n * c(n) * (x ^ (n - Suc 0))) sums
   421          (\<Sum>n. (diffs c)(n) * (x ^ n))"
   422   unfolding diffs_def
   423   apply (drule summable_sums)
   424   apply (rule sums_Suc_imp, simp_all)
   425   done
   426 
   427 lemma lemma_termdiff1:
   428   fixes z :: "'a :: {monoid_mult,comm_ring}" shows
   429   "(\<Sum>p=0..<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =
   430    (\<Sum>p=0..<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
   431   by (auto simp add: algebra_simps power_add [symmetric])
   432 
   433 lemma sumr_diff_mult_const2:
   434   "setsum f {0..<n} - of_nat n * (r::'a::ring_1) = (\<Sum>i = 0..<n. f i - r)"
   435   by (simp add: setsum_subtractf)
   436 
   437 lemma lemma_termdiff2:
   438   fixes h :: "'a :: {field}"
   439   assumes h: "h \<noteq> 0"
   440   shows
   441     "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
   442      h * (\<Sum>p=0..< n - Suc 0. \<Sum>q=0..< n - Suc 0 - p.
   443           (z + h) ^ q * z ^ (n - 2 - q))" (is "?lhs = ?rhs")
   444   apply (subgoal_tac "h * ?lhs = h * ?rhs", simp add: h)
   445   apply (simp add: right_diff_distrib diff_divide_distrib h)
   446   apply (simp add: mult_assoc [symmetric])
   447   apply (cases "n", simp)
   448   apply (simp add: lemma_realpow_diff_sumr2 h
   449                    right_diff_distrib [symmetric] mult_assoc
   450               del: power_Suc setsum_op_ivl_Suc of_nat_Suc)
   451   apply (subst lemma_realpow_rev_sumr)
   452   apply (subst sumr_diff_mult_const2)
   453   apply simp
   454   apply (simp only: lemma_termdiff1 setsum_right_distrib)
   455   apply (rule setsum_cong [OF refl])
   456   apply (simp add: less_iff_Suc_add)
   457   apply (clarify)
   458   apply (simp add: setsum_right_distrib lemma_realpow_diff_sumr2 mult_ac
   459               del: setsum_op_ivl_Suc power_Suc)
   460   apply (subst mult_assoc [symmetric], subst power_add [symmetric])
   461   apply (simp add: mult_ac)
   462   done
   463 
   464 lemma real_setsum_nat_ivl_bounded2:
   465   fixes K :: "'a::linordered_semidom"
   466   assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K"
   467     and K: "0 \<le> K"
   468   shows "setsum f {0..<n-k} \<le> of_nat n * K"
   469   apply (rule order_trans [OF setsum_mono])
   470   apply (rule f, simp)
   471   apply (simp add: mult_right_mono K)
   472   done
   473 
   474 lemma lemma_termdiff3:
   475   fixes h z :: "'a::{real_normed_field}"
   476   assumes 1: "h \<noteq> 0"
   477     and 2: "norm z \<le> K"
   478     and 3: "norm (z + h) \<le> K"
   479   shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0))
   480           \<le> of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
   481 proof -
   482   have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
   483         norm (\<Sum>p = 0..<n - Suc 0. \<Sum>q = 0..<n - Suc 0 - p.
   484           (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
   485     apply (subst lemma_termdiff2 [OF 1])
   486     apply (subst norm_mult)
   487     apply (rule mult_commute)
   488     done
   489   also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
   490   proof (rule mult_right_mono [OF _ norm_ge_zero])
   491     from norm_ge_zero 2 have K: "0 \<le> K"
   492       by (rule order_trans)
   493     have le_Kn: "\<And>i j n. i + j = n \<Longrightarrow> norm ((z + h) ^ i * z ^ j) \<le> K ^ n"
   494       apply (erule subst)
   495       apply (simp only: norm_mult norm_power power_add)
   496       apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
   497       done
   498     show "norm (\<Sum>p = 0..<n - Suc 0. \<Sum>q = 0..<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))
   499           \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
   500       apply (intro
   501          order_trans [OF norm_setsum]
   502          real_setsum_nat_ivl_bounded2
   503          mult_nonneg_nonneg
   504          of_nat_0_le_iff
   505          zero_le_power K)
   506       apply (rule le_Kn, simp)
   507       done
   508   qed
   509   also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
   510     by (simp only: mult_assoc)
   511   finally show ?thesis .
   512 qed
   513 
   514 lemma lemma_termdiff4:
   515   fixes f :: "'a::{real_normed_field} \<Rightarrow>
   516               'b::real_normed_vector"
   517   assumes k: "0 < (k::real)"
   518     and le: "\<And>h. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (f h) \<le> K * norm h"
   519   shows "f -- 0 --> 0"
   520   unfolding LIM_eq diff_0_right
   521 proof safe
   522   let ?h = "of_real (k / 2)::'a"
   523   have "?h \<noteq> 0" and "norm ?h < k" using k by simp_all
   524   hence "norm (f ?h) \<le> K * norm ?h" by (rule le)
   525   hence "0 \<le> K * norm ?h" by (rule order_trans [OF norm_ge_zero])
   526   hence zero_le_K: "0 \<le> K" using k by (simp add: zero_le_mult_iff)
   527 
   528   fix r::real
   529   assume r: "0 < r"
   530   show "\<exists>s. 0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> norm x < s \<longrightarrow> norm (f x) < r)"
   531   proof cases
   532     assume "K = 0"
   533     with k r le have "0 < k \<and> (\<forall>x. x \<noteq> 0 \<and> norm x < k \<longrightarrow> norm (f x) < r)"
   534       by simp
   535     thus "\<exists>s. 0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> norm x < s \<longrightarrow> norm (f x) < r)" ..
   536   next
   537     assume K_neq_zero: "K \<noteq> 0"
   538     with zero_le_K have K: "0 < K" by simp
   539     show "\<exists>s. 0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> norm x < s \<longrightarrow> norm (f x) < r)"
   540     proof (rule exI, safe)
   541       from k r K
   542       show "0 < min k (r * inverse K / 2)"
   543         by (simp add: mult_pos_pos positive_imp_inverse_positive)
   544     next
   545       fix x::'a
   546       assume x1: "x \<noteq> 0" and x2: "norm x < min k (r * inverse K / 2)"
   547       from x2 have x3: "norm x < k" and x4: "norm x < r * inverse K / 2"
   548         by simp_all
   549       from x1 x3 le have "norm (f x) \<le> K * norm x" by simp
   550       also from x4 K have "K * norm x < K * (r * inverse K / 2)"
   551         by (rule mult_strict_left_mono)
   552       also have "\<dots> = r / 2"
   553         using K_neq_zero by simp
   554       also have "r / 2 < r"
   555         using r by simp
   556       finally show "norm (f x) < r" .
   557     qed
   558   qed
   559 qed
   560 
   561 lemma lemma_termdiff5:
   562   fixes g :: "'a::real_normed_field \<Rightarrow> nat \<Rightarrow> 'b::banach"
   563   assumes k: "0 < (k::real)"
   564   assumes f: "summable f"
   565   assumes le: "\<And>h n. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (g h n) \<le> f n * norm h"
   566   shows "(\<lambda>h. suminf (g h)) -- 0 --> 0"
   567 proof (rule lemma_termdiff4 [OF k])
   568   fix h::'a
   569   assume "h \<noteq> 0" and "norm h < k"
   570   hence A: "\<forall>n. norm (g h n) \<le> f n * norm h"
   571     by (simp add: le)
   572   hence "\<exists>N. \<forall>n\<ge>N. norm (norm (g h n)) \<le> f n * norm h"
   573     by simp
   574   moreover from f have B: "summable (\<lambda>n. f n * norm h)"
   575     by (rule summable_mult2)
   576   ultimately have C: "summable (\<lambda>n. norm (g h n))"
   577     by (rule summable_comparison_test)
   578   hence "norm (suminf (g h)) \<le> (\<Sum>n. norm (g h n))"
   579     by (rule summable_norm)
   580   also from A C B have "(\<Sum>n. norm (g h n)) \<le> (\<Sum>n. f n * norm h)"
   581     by (rule summable_le)
   582   also from f have "(\<Sum>n. f n * norm h) = suminf f * norm h"
   583     by (rule suminf_mult2 [symmetric])
   584   finally show "norm (suminf (g h)) \<le> suminf f * norm h" .
   585 qed
   586 
   587 
   588 text{* FIXME: Long proofs*}
   589 
   590 lemma termdiffs_aux:
   591   fixes x :: "'a::{real_normed_field,banach}"
   592   assumes 1: "summable (\<lambda>n. diffs (diffs c) n * K ^ n)"
   593     and 2: "norm x < norm K"
   594   shows "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x ^ n) / h
   595              - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
   596 proof -
   597   from dense [OF 2]
   598   obtain r where r1: "norm x < r" and r2: "r < norm K" by fast
   599   from norm_ge_zero r1 have r: "0 < r"
   600     by (rule order_le_less_trans)
   601   hence r_neq_0: "r \<noteq> 0" by simp
   602   show ?thesis
   603   proof (rule lemma_termdiff5)
   604     show "0 < r - norm x" using r1 by simp
   605     from r r2 have "norm (of_real r::'a) < norm K"
   606       by simp
   607     with 1 have "summable (\<lambda>n. norm (diffs (diffs c) n * (of_real r ^ n)))"
   608       by (rule powser_insidea)
   609     hence "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
   610       using r
   611       by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
   612     hence "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
   613       by (rule diffs_equiv [THEN sums_summable])
   614     also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
   615       (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
   616       apply (rule ext)
   617       apply (simp add: diffs_def)
   618       apply (case_tac n, simp_all add: r_neq_0)
   619       done
   620     finally have "summable
   621       (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
   622       by (rule diffs_equiv [THEN sums_summable])
   623     also have
   624       "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) *
   625            r ^ (n - Suc 0)) =
   626        (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
   627       apply (rule ext)
   628       apply (case_tac "n", simp)
   629       apply (case_tac "nat", simp)
   630       apply (simp add: r_neq_0)
   631       done
   632     finally
   633     show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
   634   next
   635     fix h::'a and n::nat
   636     assume h: "h \<noteq> 0"
   637     assume "norm h < r - norm x"
   638     hence "norm x + norm h < r" by simp
   639     with norm_triangle_ineq have xh: "norm (x + h) < r"
   640       by (rule order_le_less_trans)
   641     show "norm (c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))
   642           \<le> norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
   643       apply (simp only: norm_mult mult_assoc)
   644       apply (rule mult_left_mono [OF _ norm_ge_zero])
   645       apply (simp (no_asm) add: mult_assoc [symmetric])
   646       apply (rule lemma_termdiff3)
   647       apply (rule h)
   648       apply (rule r1 [THEN order_less_imp_le])
   649       apply (rule xh [THEN order_less_imp_le])
   650       done
   651   qed
   652 qed
   653 
   654 lemma termdiffs:
   655   fixes K x :: "'a::{real_normed_field,banach}"
   656   assumes 1: "summable (\<lambda>n. c n * K ^ n)"
   657     and 2: "summable (\<lambda>n. (diffs c) n * K ^ n)"
   658     and 3: "summable (\<lambda>n. (diffs (diffs c)) n * K ^ n)"
   659     and 4: "norm x < norm K"
   660   shows "DERIV (\<lambda>x. \<Sum>n. c n * x ^ n) x :> (\<Sum>n. (diffs c) n * x ^ n)"
   661   unfolding deriv_def
   662 proof (rule LIM_zero_cancel)
   663   show "(\<lambda>h. (suminf (\<lambda>n. c n * (x + h) ^ n) - suminf (\<lambda>n. c n * x ^ n)) / h
   664             - suminf (\<lambda>n. diffs c n * x ^ n)) -- 0 --> 0"
   665   proof (rule LIM_equal2)
   666     show "0 < norm K - norm x" using 4 by (simp add: less_diff_eq)
   667   next
   668     fix h :: 'a
   669     assume "h \<noteq> 0"
   670     assume "norm (h - 0) < norm K - norm x"
   671     hence "norm x + norm h < norm K" by simp
   672     hence 5: "norm (x + h) < norm K"
   673       by (rule norm_triangle_ineq [THEN order_le_less_trans])
   674     have A: "summable (\<lambda>n. c n * x ^ n)"
   675       by (rule powser_inside [OF 1 4])
   676     have B: "summable (\<lambda>n. c n * (x + h) ^ n)"
   677       by (rule powser_inside [OF 1 5])
   678     have C: "summable (\<lambda>n. diffs c n * x ^ n)"
   679       by (rule powser_inside [OF 2 4])
   680     show "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x ^ n)) / h
   681              - (\<Sum>n. diffs c n * x ^ n) =
   682           (\<Sum>n. c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))"
   683       apply (subst sums_unique [OF diffs_equiv [OF C]])
   684       apply (subst suminf_diff [OF B A])
   685       apply (subst suminf_divide [symmetric])
   686       apply (rule summable_diff [OF B A])
   687       apply (subst suminf_diff)
   688       apply (rule summable_divide)
   689       apply (rule summable_diff [OF B A])
   690       apply (rule sums_summable [OF diffs_equiv [OF C]])
   691       apply (rule arg_cong [where f="suminf"], rule ext)
   692       apply (simp add: algebra_simps)
   693       done
   694   next
   695     show "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
   696       by (rule termdiffs_aux [OF 3 4])
   697   qed
   698 qed
   699 
   700 
   701 subsection {* Derivability of power series *}
   702 
   703 lemma DERIV_series':
   704   fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
   705   assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
   706     and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)" and x0_in_I: "x0 \<in> {a <..< b}"
   707     and "summable (f' x0)"
   708     and "summable L"
   709     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>"
   710   shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
   711   unfolding deriv_def
   712 proof (rule LIM_I)
   713   fix r :: real
   714   assume "0 < r" hence "0 < r/3" by auto
   715 
   716   obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3"
   717     using suminf_exist_split[OF `0 < r/3` `summable L`] by auto
   718 
   719   obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3"
   720     using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto
   721 
   722   let ?N = "Suc (max N_L N_f')"
   723   have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3") and
   724     L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto
   725 
   726   let ?diff = "\<lambda>i x. (f (x0 + x) i - f x0 i) / x"
   727 
   728   let ?r = "r / (3 * real ?N)"
   729   have "0 < 3 * real ?N" by auto
   730   from divide_pos_pos[OF `0 < r` this]
   731   have "0 < ?r" .
   732 
   733   let "?s 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)"
   734   def S' \<equiv> "Min (?s ` { 0 ..< ?N })"
   735 
   736   have "0 < S'" unfolding S'_def
   737   proof (rule iffD2[OF Min_gr_iff])
   738     show "\<forall>x \<in> (?s ` { 0 ..< ?N }). 0 < x"
   739     proof
   740       fix x
   741       assume "x \<in> ?s ` {0..<?N}"
   742       then obtain n where "x = ?s n" and "n \<in> {0..<?N}"
   743         using image_iff[THEN iffD1] by blast
   744       from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def]
   745       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)"
   746         by auto
   747       have "0 < ?s n" by (rule someI2[where a=s]) (auto simp add: s_bound)
   748       thus "0 < x" unfolding `x = ?s n` .
   749     qed
   750   qed auto
   751 
   752   def S \<equiv> "min (min (x0 - a) (b - x0)) S'"
   753   hence "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0"
   754     and "S \<le> S'" using x0_in_I and `0 < S'`
   755     by auto
   756 
   757   {
   758     fix x
   759     assume "x \<noteq> 0" and "\<bar> x \<bar> < S"
   760     hence x_in_I: "x0 + x \<in> { a <..< b }"
   761       using S_a S_b by auto
   762 
   763     note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
   764     note div_smbl = summable_divide[OF diff_smbl]
   765     note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
   766     note ign = summable_ignore_initial_segment[where k="?N"]
   767     note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
   768     note div_shft_smbl = summable_divide[OF diff_shft_smbl]
   769     note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]
   770 
   771     {
   772       fix n
   773       have "\<bar> ?diff (n + ?N) x \<bar> \<le> L (n + ?N) * \<bar> (x0 + x) - x0 \<bar> / \<bar> x \<bar>"
   774         using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero]
   775         unfolding abs_divide .
   776       hence "\<bar> (\<bar>?diff (n + ?N) x \<bar>) \<bar> \<le> L (n + ?N)"
   777         using `x \<noteq> 0` by auto
   778     } note L_ge = summable_le2[OF allI[OF this] ign[OF `summable L`]]
   779     from order_trans[OF summable_rabs[OF conjunct1[OF L_ge]] L_ge[THEN conjunct2]]
   780     have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))" .
   781     hence "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> r / 3" (is "?L_part \<le> r/3")
   782       using L_estimate by auto
   783 
   784     have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> \<le>
   785       (\<Sum>n \<in> { 0 ..< ?N}. \<bar>?diff n x - f' x0 n \<bar>)" ..
   786     also have "\<dots> < (\<Sum>n \<in> { 0 ..< ?N}. ?r)"
   787     proof (rule setsum_strict_mono)
   788       fix n
   789       assume "n \<in> { 0 ..< ?N}"
   790       have "\<bar>x\<bar> < S" using `\<bar>x\<bar> < S` .
   791       also have "S \<le> S'" using `S \<le> S'` .
   792       also have "S' \<le> ?s n" unfolding S'_def
   793       proof (rule Min_le_iff[THEN iffD2])
   794         have "?s n \<in> (?s ` {0..<?N}) \<and> ?s n \<le> ?s n"
   795           using `n \<in> { 0 ..< ?N}` by auto
   796         thus "\<exists> a \<in> (?s ` {0..<?N}). a \<le> ?s n" by blast
   797       qed auto
   798       finally have "\<bar>x\<bar> < ?s n" .
   799 
   800       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]
   801       have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
   802       with `x \<noteq> 0` and `\<bar>x\<bar> < ?s n` show "\<bar>?diff n x - f' x0 n\<bar> < ?r"
   803         by blast
   804     qed auto
   805     also have "\<dots> = of_nat (card {0 ..< ?N}) * ?r"
   806       by (rule setsum_constant)
   807     also have "\<dots> = real ?N * ?r"
   808       unfolding real_eq_of_nat by auto
   809     also have "\<dots> = r/3" by auto
   810     finally have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
   811 
   812     from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
   813     have "\<bar>(suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0)\<bar> =
   814         \<bar>\<Sum>n. ?diff n x - f' x0 n\<bar>"
   815       unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric]
   816       using suminf_divide[OF diff_smbl, symmetric] by auto
   817     also have "\<dots> \<le> ?diff_part + \<bar> (\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N)) \<bar>"
   818       unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"]
   819       unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]]
   820       by (rule abs_triangle_ineq)
   821     also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part"
   822       using abs_triangle_ineq4 by auto
   823     also have "\<dots> < r /3 + r/3 + r/3"
   824       using `?diff_part < r/3` `?L_part \<le> r/3` and `?f'_part < r/3`
   825       by (rule add_strict_mono [OF add_less_le_mono])
   826     finally have "\<bar>(suminf (f (x0 + x)) - suminf (f x0)) / x - suminf (f' x0)\<bar> < r"
   827       by auto
   828   }
   829   thus "\<exists> s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow>
   830       norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r"
   831     using `0 < S` unfolding real_norm_def diff_0_right by blast
   832 qed
   833 
   834 lemma DERIV_power_series':
   835   fixes f :: "nat \<Rightarrow> real"
   836   assumes converges: "\<And> x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda> n. f n * real (Suc n) * x^n)"
   837     and x0_in_I: "x0 \<in> {-R <..< R}" and "0 < R"
   838   shows "DERIV (\<lambda> x. (\<Sum> n. f n * x^(Suc n))) x0 :> (\<Sum> n. f n * real (Suc n) * x0^n)"
   839   (is "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
   840 proof -
   841   {
   842     fix R'
   843     assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
   844     hence "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}"
   845       by auto
   846     have "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
   847     proof (rule DERIV_series')
   848       show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
   849       proof -
   850         have "(R' + R) / 2 < R" and "0 < (R' + R) / 2"
   851           using `0 < R'` `0 < R` `R' < R` by auto
   852         hence in_Rball: "(R' + R) / 2 \<in> {-R <..< R}"
   853           using `R' < R` by auto
   854         have "norm R' < norm ((R' + R) / 2)"
   855           using `0 < R'` `0 < R` `R' < R` by auto
   856         from powser_insidea[OF converges[OF in_Rball] this] show ?thesis
   857           by auto
   858       qed
   859       {
   860         fix n x y
   861         assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
   862         show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
   863         proof -
   864           have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> =
   865             (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar>"
   866             unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult
   867             by auto
   868           also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)"
   869           proof (rule mult_left_mono)
   870             have "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p = 0..<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)"
   871               by (rule setsum_abs)
   872             also have "\<dots> \<le> (\<Sum>p = 0..<Suc n. R' ^ n)"
   873             proof (rule setsum_mono)
   874               fix p
   875               assume "p \<in> {0..<Suc n}"
   876               hence "p \<le> n" by auto
   877               {
   878                 fix n
   879                 fix x :: real
   880                 assume "x \<in> {-R'<..<R'}"
   881                 hence "\<bar>x\<bar> \<le> R'"  by auto
   882                 hence "\<bar>x^n\<bar> \<le> R'^n"
   883                   unfolding power_abs by (rule power_mono, auto)
   884               }
   885               from mult_mono[OF this[OF `x \<in> {-R'<..<R'}`, of p] this[OF `y \<in> {-R'<..<R'}`, of "n-p"]] `0 < R'`
   886               have "\<bar>x^p * y^(n-p)\<bar> \<le> R'^p * R'^(n-p)"
   887                 unfolding abs_mult by auto
   888               thus "\<bar>x^p * y^(n-p)\<bar> \<le> R'^n"
   889                 unfolding power_add[symmetric] using `p \<le> n` by auto
   890             qed
   891             also have "\<dots> = real (Suc n) * R' ^ n"
   892               unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
   893             finally show "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>"
   894               unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
   895             show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>"
   896               unfolding abs_mult[symmetric] by auto
   897           qed
   898           also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>"
   899             unfolding abs_mult mult_assoc[symmetric] by algebra
   900           finally show ?thesis .
   901         qed
   902       }
   903       {
   904         fix n
   905         show "DERIV (\<lambda> x. ?f x n) x0 :> (?f' x0 n)"
   906           by (auto intro!: DERIV_intros simp del: power_Suc)
   907       }
   908       {
   909         fix x
   910         assume "x \<in> {-R' <..< R'}"
   911         hence "R' \<in> {-R <..< R}" and "norm x < norm R'"
   912           using assms `R' < R` by auto
   913         have "summable (\<lambda> n. f n * x^n)"
   914         proof (rule summable_le2[THEN conjunct1, OF _ powser_insidea[OF converges[OF `R' \<in> {-R <..< R}`] `norm x < norm R'`]], rule allI)
   915           fix n
   916           have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)"
   917             by (rule mult_left_mono) auto
   918           show "\<bar>f n * x ^ n\<bar> \<le> norm (f n * real (Suc n) * x ^ n)"
   919             unfolding real_norm_def abs_mult
   920             by (rule mult_right_mono) (auto simp add: le[unfolded mult_1_right])
   921         qed
   922         from this[THEN summable_mult2[where c=x], unfolded mult_assoc, unfolded mult_commute]
   923         show "summable (?f x)" by auto
   924       }
   925       show "summable (?f' x0)"
   926         using converges[OF `x0 \<in> {-R <..< R}`] .
   927       show "x0 \<in> {-R' <..< R'}"
   928         using `x0 \<in> {-R' <..< R'}` .
   929     qed
   930   } note for_subinterval = this
   931   let ?R = "(R + \<bar>x0\<bar>) / 2"
   932   have "\<bar>x0\<bar> < ?R" using assms by auto
   933   hence "- ?R < x0"
   934   proof (cases "x0 < 0")
   935     case True
   936     hence "- x0 < ?R" using `\<bar>x0\<bar> < ?R` by auto
   937     thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
   938   next
   939     case False
   940     have "- ?R < 0" using assms by auto
   941     also have "\<dots> \<le> x0" using False by auto
   942     finally show ?thesis .
   943   qed
   944   hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R"
   945     using assms by auto
   946   from for_subinterval[OF this]
   947   show ?thesis .
   948 qed
   949 
   950 
   951 subsection {* Exponential Function *}
   952 
   953 definition exp :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
   954   where "exp = (\<lambda>x. \<Sum>n. x ^ n /\<^sub>R real (fact n))"
   955 
   956 lemma summable_exp_generic:
   957   fixes x :: "'a::{real_normed_algebra_1,banach}"
   958   defines S_def: "S \<equiv> \<lambda>n. x ^ n /\<^sub>R real (fact n)"
   959   shows "summable S"
   960 proof -
   961   have S_Suc: "\<And>n. S (Suc n) = (x * S n) /\<^sub>R real (Suc n)"
   962     unfolding S_def by (simp del: mult_Suc)
   963   obtain r :: real where r0: "0 < r" and r1: "r < 1"
   964     using dense [OF zero_less_one] by fast
   965   obtain N :: nat where N: "norm x < real N * r"
   966     using reals_Archimedean3 [OF r0] by fast
   967   from r1 show ?thesis
   968   proof (rule ratio_test [rule_format])
   969     fix n :: nat
   970     assume n: "N \<le> n"
   971     have "norm x \<le> real N * r"
   972       using N by (rule order_less_imp_le)
   973     also have "real N * r \<le> real (Suc n) * r"
   974       using r0 n by (simp add: mult_right_mono)
   975     finally have "norm x * norm (S n) \<le> real (Suc n) * r * norm (S n)"
   976       using norm_ge_zero by (rule mult_right_mono)
   977     hence "norm (x * S n) \<le> real (Suc n) * r * norm (S n)"
   978       by (rule order_trans [OF norm_mult_ineq])
   979     hence "norm (x * S n) / real (Suc n) \<le> r * norm (S n)"
   980       by (simp add: pos_divide_le_eq mult_ac)
   981     thus "norm (S (Suc n)) \<le> r * norm (S n)"
   982       by (simp add: S_Suc inverse_eq_divide)
   983   qed
   984 qed
   985 
   986 lemma summable_norm_exp:
   987   fixes x :: "'a::{real_normed_algebra_1,banach}"
   988   shows "summable (\<lambda>n. norm (x ^ n /\<^sub>R real (fact n)))"
   989 proof (rule summable_norm_comparison_test [OF exI, rule_format])
   990   show "summable (\<lambda>n. norm x ^ n /\<^sub>R real (fact n))"
   991     by (rule summable_exp_generic)
   992   fix n
   993   show "norm (x ^ n /\<^sub>R real (fact n)) \<le> norm x ^ n /\<^sub>R real (fact n)"
   994     by (simp add: norm_power_ineq)
   995 qed
   996 
   997 lemma summable_exp: "summable (\<lambda>n. inverse (real (fact n)) * x ^ n)"
   998   using summable_exp_generic [where x=x] by simp
   999 
  1000 lemma exp_converges: "(\<lambda>n. x ^ n /\<^sub>R real (fact n)) sums exp x"
  1001   unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])
  1002 
  1003 
  1004 lemma exp_fdiffs:
  1005       "diffs (\<lambda>n. inverse(real (fact n))) = (\<lambda>n. inverse(real (fact n)))"
  1006   by (simp add: diffs_def mult_assoc [symmetric] real_of_nat_def of_nat_mult
  1007         del: mult_Suc of_nat_Suc)
  1008 
  1009 lemma diffs_of_real: "diffs (\<lambda>n. of_real (f n)) = (\<lambda>n. of_real (diffs f n))"
  1010   by (simp add: diffs_def)
  1011 
  1012 lemma DERIV_exp [simp]: "DERIV exp x :> exp(x)"
  1013   unfolding exp_def scaleR_conv_of_real
  1014   apply (rule DERIV_cong)
  1015   apply (rule termdiffs [where K="of_real (1 + norm x)"])
  1016   apply (simp_all only: diffs_of_real scaleR_conv_of_real exp_fdiffs)
  1017   apply (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])+
  1018   apply (simp del: of_real_add)
  1019   done
  1020 
  1021 declare DERIV_exp[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  1022 
  1023 lemma isCont_exp: "isCont exp x"
  1024   by (rule DERIV_exp [THEN DERIV_isCont])
  1025 
  1026 lemma isCont_exp' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. exp (f x)) a"
  1027   by (rule isCont_o2 [OF _ isCont_exp])
  1028 
  1029 lemma tendsto_exp [tendsto_intros]:
  1030   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. exp (f x)) ---> exp a) F"
  1031   by (rule isCont_tendsto_compose [OF isCont_exp])
  1032 
  1033 lemma continuous_exp [continuous_intros]:
  1034   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. exp (f x))"
  1035   unfolding continuous_def by (rule tendsto_exp)
  1036 
  1037 lemma continuous_on_exp [continuous_on_intros]:
  1038   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. exp (f x))"
  1039   unfolding continuous_on_def by (auto intro: tendsto_exp)
  1040 
  1041 
  1042 subsubsection {* Properties of the Exponential Function *}
  1043 
  1044 lemma powser_zero:
  1045   fixes f :: "nat \<Rightarrow> 'a::{real_normed_algebra_1}"
  1046   shows "(\<Sum>n. f n * 0 ^ n) = f 0"
  1047 proof -
  1048   have "(\<Sum>n = 0..<1. f n * 0 ^ n) = (\<Sum>n. f n * 0 ^ n)"
  1049     by (rule sums_unique [OF series_zero], simp add: power_0_left)
  1050   thus ?thesis unfolding One_nat_def by simp
  1051 qed
  1052 
  1053 lemma exp_zero [simp]: "exp 0 = 1"
  1054   unfolding exp_def by (simp add: scaleR_conv_of_real powser_zero)
  1055 
  1056 lemma setsum_cl_ivl_Suc2:
  1057   "(\<Sum>i=m..Suc n. f i) = (if Suc n < m then 0 else f m + (\<Sum>i=m..n. f (Suc i)))"
  1058   by (simp add: setsum_head_Suc setsum_shift_bounds_cl_Suc_ivl
  1059            del: setsum_cl_ivl_Suc)
  1060 
  1061 lemma exp_series_add:
  1062   fixes x y :: "'a::{real_field}"
  1063   defines S_def: "S \<equiv> \<lambda>x n. x ^ n /\<^sub>R real (fact n)"
  1064   shows "S (x + y) n = (\<Sum>i=0..n. S x i * S y (n - i))"
  1065 proof (induct n)
  1066   case 0
  1067   show ?case
  1068     unfolding S_def by simp
  1069 next
  1070   case (Suc n)
  1071   have S_Suc: "\<And>x n. S x (Suc n) = (x * S x n) /\<^sub>R real (Suc n)"
  1072     unfolding S_def by (simp del: mult_Suc)
  1073   hence times_S: "\<And>x n. x * S x n = real (Suc n) *\<^sub>R S x (Suc n)"
  1074     by simp
  1075 
  1076   have "real (Suc n) *\<^sub>R S (x + y) (Suc n) = (x + y) * S (x + y) n"
  1077     by (simp only: times_S)
  1078   also have "\<dots> = (x + y) * (\<Sum>i=0..n. S x i * S y (n-i))"
  1079     by (simp only: Suc)
  1080   also have "\<dots> = x * (\<Sum>i=0..n. S x i * S y (n-i))
  1081                 + y * (\<Sum>i=0..n. S x i * S y (n-i))"
  1082     by (rule distrib_right)
  1083   also have "\<dots> = (\<Sum>i=0..n. (x * S x i) * S y (n-i))
  1084                 + (\<Sum>i=0..n. S x i * (y * S y (n-i)))"
  1085     by (simp only: setsum_right_distrib mult_ac)
  1086   also have "\<dots> = (\<Sum>i=0..n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i)))
  1087                 + (\<Sum>i=0..n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
  1088     by (simp add: times_S Suc_diff_le)
  1089   also have "(\<Sum>i=0..n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i))) =
  1090              (\<Sum>i=0..Suc n. real i *\<^sub>R (S x i * S y (Suc n-i)))"
  1091     by (subst setsum_cl_ivl_Suc2, simp)
  1092   also have "(\<Sum>i=0..n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
  1093              (\<Sum>i=0..Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
  1094     by (subst setsum_cl_ivl_Suc, simp)
  1095   also have "(\<Sum>i=0..Suc n. real i *\<^sub>R (S x i * S y (Suc n-i))) +
  1096              (\<Sum>i=0..Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
  1097              (\<Sum>i=0..Suc n. real (Suc n) *\<^sub>R (S x i * S y (Suc n-i)))"
  1098     by (simp only: setsum_addf [symmetric] scaleR_left_distrib [symmetric]
  1099               real_of_nat_add [symmetric], simp)
  1100   also have "\<dots> = real (Suc n) *\<^sub>R (\<Sum>i=0..Suc n. S x i * S y (Suc n-i))"
  1101     by (simp only: scaleR_right.setsum)
  1102   finally show
  1103     "S (x + y) (Suc n) = (\<Sum>i=0..Suc n. S x i * S y (Suc n - i))"
  1104     by (simp del: setsum_cl_ivl_Suc)
  1105 qed
  1106 
  1107 lemma exp_add: "exp (x + y) = exp x * exp y"
  1108   unfolding exp_def
  1109   by (simp only: Cauchy_product summable_norm_exp exp_series_add)
  1110 
  1111 lemma mult_exp_exp: "exp x * exp y = exp (x + y)"
  1112   by (rule exp_add [symmetric])
  1113 
  1114 lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
  1115   unfolding exp_def
  1116   apply (subst suminf_of_real)
  1117   apply (rule summable_exp_generic)
  1118   apply (simp add: scaleR_conv_of_real)
  1119   done
  1120 
  1121 lemma exp_not_eq_zero [simp]: "exp x \<noteq> 0"
  1122 proof
  1123   have "exp x * exp (- x) = 1" by (simp add: mult_exp_exp)
  1124   also assume "exp x = 0"
  1125   finally show "False" by simp
  1126 qed
  1127 
  1128 lemma exp_minus: "exp (- x) = inverse (exp x)"
  1129   by (rule inverse_unique [symmetric], simp add: mult_exp_exp)
  1130 
  1131 lemma exp_diff: "exp (x - y) = exp x / exp y"
  1132   using exp_add [of x "- y"] by (simp add: exp_minus divide_inverse)
  1133 
  1134 
  1135 subsubsection {* Properties of the Exponential Function on Reals *}
  1136 
  1137 text {* Comparisons of @{term "exp x"} with zero. *}
  1138 
  1139 text{*Proof: because every exponential can be seen as a square.*}
  1140 lemma exp_ge_zero [simp]: "0 \<le> exp (x::real)"
  1141 proof -
  1142   have "0 \<le> exp (x/2) * exp (x/2)" by simp
  1143   thus ?thesis by (simp add: exp_add [symmetric])
  1144 qed
  1145 
  1146 lemma exp_gt_zero [simp]: "0 < exp (x::real)"
  1147   by (simp add: order_less_le)
  1148 
  1149 lemma not_exp_less_zero [simp]: "\<not> exp (x::real) < 0"
  1150   by (simp add: not_less)
  1151 
  1152 lemma not_exp_le_zero [simp]: "\<not> exp (x::real) \<le> 0"
  1153   by (simp add: not_le)
  1154 
  1155 lemma abs_exp_cancel [simp]: "\<bar>exp x::real\<bar> = exp x"
  1156   by simp
  1157 
  1158 lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
  1159   by (induct n) (auto simp add: real_of_nat_Suc distrib_left exp_add mult_commute)
  1160 
  1161 text {* Strict monotonicity of exponential. *}
  1162 
  1163 lemma exp_ge_add_one_self_aux: "0 \<le> (x::real) \<Longrightarrow> (1 + x) \<le> exp(x)"
  1164   apply (drule order_le_imp_less_or_eq, auto)
  1165   apply (simp add: exp_def)
  1166   apply (rule order_trans)
  1167   apply (rule_tac [2] n = 2 and f = "(\<lambda>n. inverse (real (fact n)) * x ^ n)" in series_pos_le)
  1168   apply (auto intro: summable_exp simp add: numeral_2_eq_2 zero_le_mult_iff)
  1169   done
  1170 
  1171 lemma exp_gt_one: "0 < (x::real) \<Longrightarrow> 1 < exp x"
  1172 proof -
  1173   assume x: "0 < x"
  1174   hence "1 < 1 + x" by simp
  1175   also from x have "1 + x \<le> exp x"
  1176     by (simp add: exp_ge_add_one_self_aux)
  1177   finally show ?thesis .
  1178 qed
  1179 
  1180 lemma exp_less_mono:
  1181   fixes x y :: real
  1182   assumes "x < y"
  1183   shows "exp x < exp y"
  1184 proof -
  1185   from `x < y` have "0 < y - x" by simp
  1186   hence "1 < exp (y - x)" by (rule exp_gt_one)
  1187   hence "1 < exp y / exp x" by (simp only: exp_diff)
  1188   thus "exp x < exp y" by simp
  1189 qed
  1190 
  1191 lemma exp_less_cancel: "exp (x::real) < exp y \<Longrightarrow> x < y"
  1192   apply (simp add: linorder_not_le [symmetric])
  1193   apply (auto simp add: order_le_less exp_less_mono)
  1194   done
  1195 
  1196 lemma exp_less_cancel_iff [iff]: "exp (x::real) < exp y \<longleftrightarrow> x < y"
  1197   by (auto intro: exp_less_mono exp_less_cancel)
  1198 
  1199 lemma exp_le_cancel_iff [iff]: "exp (x::real) \<le> exp y \<longleftrightarrow> x \<le> y"
  1200   by (auto simp add: linorder_not_less [symmetric])
  1201 
  1202 lemma exp_inj_iff [iff]: "exp (x::real) = exp y \<longleftrightarrow> x = y"
  1203   by (simp add: order_eq_iff)
  1204 
  1205 text {* Comparisons of @{term "exp x"} with one. *}
  1206 
  1207 lemma one_less_exp_iff [simp]: "1 < exp (x::real) \<longleftrightarrow> 0 < x"
  1208   using exp_less_cancel_iff [where x=0 and y=x] by simp
  1209 
  1210 lemma exp_less_one_iff [simp]: "exp (x::real) < 1 \<longleftrightarrow> x < 0"
  1211   using exp_less_cancel_iff [where x=x and y=0] by simp
  1212 
  1213 lemma one_le_exp_iff [simp]: "1 \<le> exp (x::real) \<longleftrightarrow> 0 \<le> x"
  1214   using exp_le_cancel_iff [where x=0 and y=x] by simp
  1215 
  1216 lemma exp_le_one_iff [simp]: "exp (x::real) \<le> 1 \<longleftrightarrow> x \<le> 0"
  1217   using exp_le_cancel_iff [where x=x and y=0] by simp
  1218 
  1219 lemma exp_eq_one_iff [simp]: "exp (x::real) = 1 \<longleftrightarrow> x = 0"
  1220   using exp_inj_iff [where x=x and y=0] by simp
  1221 
  1222 lemma lemma_exp_total: "1 \<le> y \<Longrightarrow> \<exists>x. 0 \<le> x & x \<le> y - 1 & exp(x::real) = y"
  1223 proof (rule IVT)
  1224   assume "1 \<le> y"
  1225   hence "0 \<le> y - 1" by simp
  1226   hence "1 + (y - 1) \<le> exp (y - 1)" by (rule exp_ge_add_one_self_aux)
  1227   thus "y \<le> exp (y - 1)" by simp
  1228 qed (simp_all add: le_diff_eq)
  1229 
  1230 lemma exp_total: "0 < (y::real) \<Longrightarrow> \<exists>x. exp x = y"
  1231 proof (rule linorder_le_cases [of 1 y])
  1232   assume "1 \<le> y"
  1233   thus "\<exists>x. exp x = y" by (fast dest: lemma_exp_total)
  1234 next
  1235   assume "0 < y" and "y \<le> 1"
  1236   hence "1 \<le> inverse y" by (simp add: one_le_inverse_iff)
  1237   then obtain x where "exp x = inverse y" by (fast dest: lemma_exp_total)
  1238   hence "exp (- x) = y" by (simp add: exp_minus)
  1239   thus "\<exists>x. exp x = y" ..
  1240 qed
  1241 
  1242 
  1243 subsection {* Natural Logarithm *}
  1244 
  1245 definition ln :: "real \<Rightarrow> real"
  1246   where "ln x = (THE u. exp u = x)"
  1247 
  1248 lemma ln_exp [simp]: "ln (exp x) = x"
  1249   by (simp add: ln_def)
  1250 
  1251 lemma exp_ln [simp]: "0 < x \<Longrightarrow> exp (ln x) = x"
  1252   by (auto dest: exp_total)
  1253 
  1254 lemma exp_ln_iff [simp]: "exp (ln x) = x \<longleftrightarrow> 0 < x"
  1255   by (metis exp_gt_zero exp_ln)
  1256 
  1257 lemma ln_unique: "exp y = x \<Longrightarrow> ln x = y"
  1258   by (erule subst, rule ln_exp)
  1259 
  1260 lemma ln_one [simp]: "ln 1 = 0"
  1261   by (rule ln_unique) simp
  1262 
  1263 lemma ln_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x * y) = ln x + ln y"
  1264   by (rule ln_unique) (simp add: exp_add)
  1265 
  1266 lemma ln_inverse: "0 < x \<Longrightarrow> ln (inverse x) = - ln x"
  1267   by (rule ln_unique) (simp add: exp_minus)
  1268 
  1269 lemma ln_div: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x / y) = ln x - ln y"
  1270   by (rule ln_unique) (simp add: exp_diff)
  1271 
  1272 lemma ln_realpow: "0 < x \<Longrightarrow> ln (x ^ n) = real n * ln x"
  1273   by (rule ln_unique) (simp add: exp_real_of_nat_mult)
  1274 
  1275 lemma ln_less_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x < ln y \<longleftrightarrow> x < y"
  1276   by (subst exp_less_cancel_iff [symmetric]) simp
  1277 
  1278 lemma ln_le_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x \<le> ln y \<longleftrightarrow> x \<le> y"
  1279   by (simp add: linorder_not_less [symmetric])
  1280 
  1281 lemma ln_inj_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x = ln y \<longleftrightarrow> x = y"
  1282   by (simp add: order_eq_iff)
  1283 
  1284 lemma ln_add_one_self_le_self [simp]: "0 \<le> x \<Longrightarrow> ln (1 + x) \<le> x"
  1285   apply (rule exp_le_cancel_iff [THEN iffD1])
  1286   apply (simp add: exp_ge_add_one_self_aux)
  1287   done
  1288 
  1289 lemma ln_less_self [simp]: "0 < x \<Longrightarrow> ln x < x"
  1290   by (rule order_less_le_trans [where y="ln (1 + x)"]) simp_all
  1291 
  1292 lemma ln_ge_zero [simp]: "1 \<le> x \<Longrightarrow> 0 \<le> ln x"
  1293   using ln_le_cancel_iff [of 1 x] by simp
  1294 
  1295 lemma ln_ge_zero_imp_ge_one: "0 \<le> ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> x"
  1296   using ln_le_cancel_iff [of 1 x] by simp
  1297 
  1298 lemma ln_ge_zero_iff [simp]: "0 < x \<Longrightarrow> 0 \<le> ln x \<longleftrightarrow> 1 \<le> x"
  1299   using ln_le_cancel_iff [of 1 x] by simp
  1300 
  1301 lemma ln_less_zero_iff [simp]: "0 < x \<Longrightarrow> ln x < 0 \<longleftrightarrow> x < 1"
  1302   using ln_less_cancel_iff [of x 1] by simp
  1303 
  1304 lemma ln_gt_zero: "1 < x \<Longrightarrow> 0 < ln x"
  1305   using ln_less_cancel_iff [of 1 x] by simp
  1306 
  1307 lemma ln_gt_zero_imp_gt_one: "0 < ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 < x"
  1308   using ln_less_cancel_iff [of 1 x] by simp
  1309 
  1310 lemma ln_gt_zero_iff [simp]: "0 < x \<Longrightarrow> 0 < ln x \<longleftrightarrow> 1 < x"
  1311   using ln_less_cancel_iff [of 1 x] by simp
  1312 
  1313 lemma ln_eq_zero_iff [simp]: "0 < x \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1"
  1314   using ln_inj_iff [of x 1] by simp
  1315 
  1316 lemma ln_less_zero: "0 < x \<Longrightarrow> x < 1 \<Longrightarrow> ln x < 0"
  1317   by simp
  1318 
  1319 lemma isCont_ln: "0 < x \<Longrightarrow> isCont ln x"
  1320   apply (subgoal_tac "isCont ln (exp (ln x))", simp)
  1321   apply (rule isCont_inverse_function [where f=exp], simp_all)
  1322   done
  1323 
  1324 lemma tendsto_ln [tendsto_intros]:
  1325   "(f ---> a) F \<Longrightarrow> 0 < a \<Longrightarrow> ((\<lambda>x. ln (f x)) ---> ln a) F"
  1326   by (rule isCont_tendsto_compose [OF isCont_ln])
  1327 
  1328 lemma continuous_ln:
  1329   "continuous F f \<Longrightarrow> 0 < f (Lim F (\<lambda>x. x)) \<Longrightarrow> continuous F (\<lambda>x. ln (f x))"
  1330   unfolding continuous_def by (rule tendsto_ln)
  1331 
  1332 lemma isCont_ln' [continuous_intros]:
  1333   "continuous (at x) f \<Longrightarrow> 0 < f x \<Longrightarrow> continuous (at x) (\<lambda>x. ln (f x))"
  1334   unfolding continuous_at by (rule tendsto_ln)
  1335 
  1336 lemma continuous_within_ln [continuous_intros]:
  1337   "continuous (at x within s) f \<Longrightarrow> 0 < f x \<Longrightarrow> continuous (at x within s) (\<lambda>x. ln (f x))"
  1338   unfolding continuous_within by (rule tendsto_ln)
  1339 
  1340 lemma continuous_on_ln [continuous_on_intros]:
  1341   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. 0 < f x) \<Longrightarrow> continuous_on s (\<lambda>x. ln (f x))"
  1342   unfolding continuous_on_def by (auto intro: tendsto_ln)
  1343 
  1344 lemma DERIV_ln: "0 < x \<Longrightarrow> DERIV ln x :> inverse x"
  1345   apply (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
  1346   apply (erule DERIV_cong [OF DERIV_exp exp_ln])
  1347   apply (simp_all add: abs_if isCont_ln)
  1348   done
  1349 
  1350 lemma DERIV_ln_divide: "0 < x \<Longrightarrow> DERIV ln x :> 1 / x"
  1351   by (rule DERIV_ln[THEN DERIV_cong], simp, simp add: divide_inverse)
  1352 
  1353 declare DERIV_ln_divide[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  1354 
  1355 lemma ln_series:
  1356   assumes "0 < x" and "x < 2"
  1357   shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
  1358   (is "ln x = suminf (?f (x - 1))")
  1359 proof -
  1360   let ?f' = "\<lambda>x n. (-1)^n * (x - 1)^n"
  1361 
  1362   have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
  1363   proof (rule DERIV_isconst3[where x=x])
  1364     fix x :: real
  1365     assume "x \<in> {0 <..< 2}"
  1366     hence "0 < x" and "x < 2" by auto
  1367     have "norm (1 - x) < 1"
  1368       using `0 < x` and `x < 2` by auto
  1369     have "1 / x = 1 / (1 - (1 - x))" by auto
  1370     also have "\<dots> = (\<Sum> n. (1 - x)^n)"
  1371       using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
  1372     also have "\<dots> = suminf (?f' x)"
  1373       unfolding power_mult_distrib[symmetric]
  1374       by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
  1375     finally have "DERIV ln x :> suminf (?f' x)"
  1376       using DERIV_ln[OF `0 < x`] unfolding divide_inverse by auto
  1377     moreover
  1378     have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
  1379     have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :>
  1380       (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
  1381     proof (rule DERIV_power_series')
  1382       show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1"
  1383         using `0 < x` `x < 2` by auto
  1384       fix x :: real
  1385       assume "x \<in> {- 1<..<1}"
  1386       hence "norm (-x) < 1" by auto
  1387       show "summable (\<lambda>n. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
  1388         unfolding One_nat_def
  1389         by (auto simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
  1390     qed
  1391     hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)"
  1392       unfolding One_nat_def by auto
  1393     hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)"
  1394       unfolding DERIV_iff repos .
  1395     ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
  1396       by (rule DERIV_diff)
  1397     thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
  1398   qed (auto simp add: assms)
  1399   thus ?thesis by auto
  1400 qed
  1401 
  1402 lemma exp_first_two_terms: "exp x = 1 + x + (\<Sum> n. inverse(fact (n+2)) * (x ^ (n+2)))"
  1403 proof -
  1404   have "exp x = suminf (\<lambda>n. inverse(fact n) * (x ^ n))"
  1405     by (simp add: exp_def)
  1406   also from summable_exp have "... = (\<Sum> n::nat = 0 ..< 2. inverse(fact n) * (x ^ n)) +
  1407       (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2)))" (is "_ = ?a + _")
  1408     by (rule suminf_split_initial_segment)
  1409   also have "?a = 1 + x"
  1410     by (simp add: numeral_2_eq_2)
  1411   finally show ?thesis .
  1412 qed
  1413 
  1414 lemma exp_bound: "0 <= (x::real) \<Longrightarrow> x <= 1 \<Longrightarrow> exp x <= 1 + x + x\<^sup>2"
  1415 proof -
  1416   assume a: "0 <= x"
  1417   assume b: "x <= 1"
  1418   {
  1419     fix n :: nat
  1420     have "2 * 2 ^ n \<le> fact (n + 2)"
  1421       by (induct n) simp_all
  1422     hence "real ((2::nat) * 2 ^ n) \<le> real (fact (n + 2))"
  1423       by (simp only: real_of_nat_le_iff)
  1424     hence "2 * 2 ^ n \<le> real (fact (n + 2))"
  1425       by simp
  1426     hence "inverse (fact (n + 2)) \<le> inverse (2 * 2 ^ n)"
  1427       by (rule le_imp_inverse_le) simp
  1428     hence "inverse (fact (n + 2)) \<le> 1/2 * (1/2)^n"
  1429       by (simp add: power_inverse)
  1430     hence "inverse (fact (n + 2)) * (x^n * x\<^sup>2) \<le> 1/2 * (1/2)^n * (1 * x\<^sup>2)"
  1431       by (rule mult_mono)
  1432         (rule mult_mono, simp_all add: power_le_one a b mult_nonneg_nonneg)
  1433     hence "inverse (fact (n + 2)) * x ^ (n + 2) \<le> (x\<^sup>2/2) * ((1/2)^n)"
  1434       unfolding power_add by (simp add: mult_ac del: fact_Suc) }
  1435   note aux1 = this
  1436   have "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums (x\<^sup>2 / 2 * (1 / (1 - 1 / 2)))"
  1437     by (intro sums_mult geometric_sums, simp)
  1438   hence aux2: "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums x\<^sup>2"
  1439     by simp
  1440   have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <= x\<^sup>2"
  1441   proof -
  1442     have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <=
  1443         suminf (\<lambda>n. (x\<^sup>2/2) * ((1/2)^n))"
  1444       apply (rule summable_le)
  1445       apply (rule allI, rule aux1)
  1446       apply (rule summable_exp [THEN summable_ignore_initial_segment])
  1447       by (rule sums_summable, rule aux2)
  1448     also have "... = x\<^sup>2"
  1449       by (rule sums_unique [THEN sym], rule aux2)
  1450     finally show ?thesis .
  1451   qed
  1452   thus ?thesis unfolding exp_first_two_terms by auto
  1453 qed
  1454 
  1455 lemma ln_one_minus_pos_upper_bound: "0 <= x \<Longrightarrow> x < 1 \<Longrightarrow> ln (1 - x) <= - x"
  1456 proof -
  1457   assume a: "0 <= (x::real)" and b: "x < 1"
  1458   have "(1 - x) * (1 + x + x\<^sup>2) = (1 - x^3)"
  1459     by (simp add: algebra_simps power2_eq_square power3_eq_cube)
  1460   also have "... <= 1"
  1461     by (auto simp add: a)
  1462   finally have "(1 - x) * (1 + x + x\<^sup>2) <= 1" .
  1463   moreover have c: "0 < 1 + x + x\<^sup>2"
  1464     by (simp add: add_pos_nonneg a)
  1465   ultimately have "1 - x <= 1 / (1 + x + x\<^sup>2)"
  1466     by (elim mult_imp_le_div_pos)
  1467   also have "... <= 1 / exp x"
  1468     apply (rule divide_left_mono)
  1469     apply (rule exp_bound, rule a)
  1470     apply (rule b [THEN less_imp_le])
  1471     apply simp
  1472     apply (rule mult_pos_pos)
  1473     apply (rule c)
  1474     apply simp
  1475     done
  1476   also have "... = exp (-x)"
  1477     by (auto simp add: exp_minus divide_inverse)
  1478   finally have "1 - x <= exp (- x)" .
  1479   also have "1 - x = exp (ln (1 - x))"
  1480   proof -
  1481     have "0 < 1 - x"
  1482       by (insert b, auto)
  1483     thus ?thesis
  1484       by (auto simp only: exp_ln_iff [THEN sym])
  1485   qed
  1486   finally have "exp (ln (1 - x)) <= exp (- x)" .
  1487   thus ?thesis by (auto simp only: exp_le_cancel_iff)
  1488 qed
  1489 
  1490 lemma exp_ge_add_one_self [simp]: "1 + (x::real) <= exp x"
  1491   apply (case_tac "0 <= x")
  1492   apply (erule exp_ge_add_one_self_aux)
  1493   apply (case_tac "x <= -1")
  1494   apply (subgoal_tac "1 + x <= 0")
  1495   apply (erule order_trans)
  1496   apply simp
  1497   apply simp
  1498   apply (subgoal_tac "1 + x = exp(ln (1 + x))")
  1499   apply (erule ssubst)
  1500   apply (subst exp_le_cancel_iff)
  1501   apply (subgoal_tac "ln (1 - (- x)) <= - (- x)")
  1502   apply simp
  1503   apply (rule ln_one_minus_pos_upper_bound)
  1504   apply auto
  1505 done
  1506 
  1507 lemma ln_one_plus_pos_lower_bound: "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> x - x\<^sup>2 <= ln (1 + x)"
  1508 proof -
  1509   assume a: "0 <= x" and b: "x <= 1"
  1510   have "exp (x - x\<^sup>2) = exp x / exp (x\<^sup>2)"
  1511     by (rule exp_diff)
  1512   also have "... <= (1 + x + x\<^sup>2) / exp (x \<^sup>2)"
  1513     apply (rule divide_right_mono)
  1514     apply (rule exp_bound)
  1515     apply (rule a, rule b)
  1516     apply simp
  1517     done
  1518   also have "... <= (1 + x + x\<^sup>2) / (1 + x\<^sup>2)"
  1519     apply (rule divide_left_mono)
  1520     apply (simp add: exp_ge_add_one_self_aux)
  1521     apply (simp add: a)
  1522     apply (simp add: mult_pos_pos add_pos_nonneg)
  1523     done
  1524   also from a have "... <= 1 + x"
  1525     by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
  1526   finally have "exp (x - x\<^sup>2) <= 1 + x" .
  1527   also have "... = exp (ln (1 + x))"
  1528   proof -
  1529     from a have "0 < 1 + x" by auto
  1530     thus ?thesis
  1531       by (auto simp only: exp_ln_iff [THEN sym])
  1532   qed
  1533   finally have "exp (x - x\<^sup>2) <= exp (ln (1 + x))" .
  1534   thus ?thesis by (auto simp only: exp_le_cancel_iff)
  1535 qed
  1536 
  1537 lemma aux5: "x < 1 \<Longrightarrow> ln(1 - x) = - ln(1 + x / (1 - x))"
  1538 proof -
  1539   assume a: "x < 1"
  1540   have "ln(1 - x) = - ln(1 / (1 - x))"
  1541   proof -
  1542     have "ln(1 - x) = - (- ln (1 - x))"
  1543       by auto
  1544     also have "- ln(1 - x) = ln 1 - ln(1 - x)"
  1545       by simp
  1546     also have "... = ln(1 / (1 - x))"
  1547       apply (rule ln_div [THEN sym])
  1548       using a apply auto
  1549       done
  1550     finally show ?thesis .
  1551   qed
  1552   also have " 1 / (1 - x) = 1 + x / (1 - x)" using a by(simp add:field_simps)
  1553   finally show ?thesis .
  1554 qed
  1555 
  1556 lemma ln_one_minus_pos_lower_bound:
  1557   "0 <= x \<Longrightarrow> x <= (1 / 2) \<Longrightarrow> - x - 2 * x\<^sup>2 <= ln (1 - x)"
  1558 proof -
  1559   assume a: "0 <= x" and b: "x <= (1 / 2)"
  1560   from b have c: "x < 1" by auto
  1561   then have "ln (1 - x) = - ln (1 + x / (1 - x))"
  1562     by (rule aux5)
  1563   also have "- (x / (1 - x)) <= ..."
  1564   proof -
  1565     have "ln (1 + x / (1 - x)) <= x / (1 - x)"
  1566       apply (rule ln_add_one_self_le_self)
  1567       apply (rule divide_nonneg_pos)
  1568       using a c apply auto
  1569       done
  1570     thus ?thesis
  1571       by auto
  1572   qed
  1573   also have "- (x / (1 - x)) = -x / (1 - x)"
  1574     by auto
  1575   finally have d: "- x / (1 - x) <= ln (1 - x)" .
  1576   have "0 < 1 - x" using a b by simp
  1577   hence e: "-x - 2 * x\<^sup>2 <= - x / (1 - x)"
  1578     using mult_right_le_one_le[of "x*x" "2*x"] a b
  1579     by (simp add: field_simps power2_eq_square)
  1580   from e d show "- x - 2 * x\<^sup>2 <= ln (1 - x)"
  1581     by (rule order_trans)
  1582 qed
  1583 
  1584 lemma ln_add_one_self_le_self2: "-1 < x \<Longrightarrow> ln(1 + x) <= x"
  1585   apply (subgoal_tac "ln (1 + x) \<le> ln (exp x)", simp)
  1586   apply (subst ln_le_cancel_iff)
  1587   apply auto
  1588   done
  1589 
  1590 lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
  1591   "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> abs(ln (1 + x) - x) <= x\<^sup>2"
  1592 proof -
  1593   assume x: "0 <= x"
  1594   assume x1: "x <= 1"
  1595   from x have "ln (1 + x) <= x"
  1596     by (rule ln_add_one_self_le_self)
  1597   then have "ln (1 + x) - x <= 0"
  1598     by simp
  1599   then have "abs(ln(1 + x) - x) = - (ln(1 + x) - x)"
  1600     by (rule abs_of_nonpos)
  1601   also have "... = x - ln (1 + x)"
  1602     by simp
  1603   also have "... <= x\<^sup>2"
  1604   proof -
  1605     from x x1 have "x - x\<^sup>2 <= ln (1 + x)"
  1606       by (intro ln_one_plus_pos_lower_bound)
  1607     thus ?thesis
  1608       by simp
  1609   qed
  1610   finally show ?thesis .
  1611 qed
  1612 
  1613 lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
  1614   "-(1 / 2) <= x \<Longrightarrow> x <= 0 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
  1615 proof -
  1616   assume a: "-(1 / 2) <= x"
  1617   assume b: "x <= 0"
  1618   have "abs(ln (1 + x) - x) = x - ln(1 - (-x))"
  1619     apply (subst abs_of_nonpos)
  1620     apply simp
  1621     apply (rule ln_add_one_self_le_self2)
  1622     using a apply auto
  1623     done
  1624   also have "... <= 2 * x\<^sup>2"
  1625     apply (subgoal_tac "- (-x) - 2 * (-x)\<^sup>2 <= ln (1 - (-x))")
  1626     apply (simp add: algebra_simps)
  1627     apply (rule ln_one_minus_pos_lower_bound)
  1628     using a b apply auto
  1629     done
  1630   finally show ?thesis .
  1631 qed
  1632 
  1633 lemma abs_ln_one_plus_x_minus_x_bound:
  1634     "abs x <= 1 / 2 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
  1635   apply (case_tac "0 <= x")
  1636   apply (rule order_trans)
  1637   apply (rule abs_ln_one_plus_x_minus_x_bound_nonneg)
  1638   apply auto
  1639   apply (rule abs_ln_one_plus_x_minus_x_bound_nonpos)
  1640   apply auto
  1641   done
  1642 
  1643 lemma ln_x_over_x_mono: "exp 1 <= x \<Longrightarrow> x <= y \<Longrightarrow> (ln y / y) <= (ln x / x)"
  1644 proof -
  1645   assume x: "exp 1 <= x" "x <= y"
  1646   moreover have "0 < exp (1::real)" by simp
  1647   ultimately have a: "0 < x" and b: "0 < y"
  1648     by (fast intro: less_le_trans order_trans)+
  1649   have "x * ln y - x * ln x = x * (ln y - ln x)"
  1650     by (simp add: algebra_simps)
  1651   also have "... = x * ln(y / x)"
  1652     by (simp only: ln_div a b)
  1653   also have "y / x = (x + (y - x)) / x"
  1654     by simp
  1655   also have "... = 1 + (y - x) / x"
  1656     using x a by (simp add: field_simps)
  1657   also have "x * ln(1 + (y - x) / x) <= x * ((y - x) / x)"
  1658     apply (rule mult_left_mono)
  1659     apply (rule ln_add_one_self_le_self)
  1660     apply (rule divide_nonneg_pos)
  1661     using x a apply simp_all
  1662     done
  1663   also have "... = y - x" using a by simp
  1664   also have "... = (y - x) * ln (exp 1)" by simp
  1665   also have "... <= (y - x) * ln x"
  1666     apply (rule mult_left_mono)
  1667     apply (subst ln_le_cancel_iff)
  1668     apply fact
  1669     apply (rule a)
  1670     apply (rule x)
  1671     using x apply simp
  1672     done
  1673   also have "... = y * ln x - x * ln x"
  1674     by (rule left_diff_distrib)
  1675   finally have "x * ln y <= y * ln x"
  1676     by arith
  1677   then have "ln y <= (y * ln x) / x" using a by (simp add: field_simps)
  1678   also have "... = y * (ln x / x)" by simp
  1679   finally show ?thesis using b by (simp add: field_simps)
  1680 qed
  1681 
  1682 lemma ln_le_minus_one: "0 < x \<Longrightarrow> ln x \<le> x - 1"
  1683   using exp_ge_add_one_self[of "ln x"] by simp
  1684 
  1685 lemma ln_eq_minus_one:
  1686   assumes "0 < x" "ln x = x - 1"
  1687   shows "x = 1"
  1688 proof -
  1689   let ?l = "\<lambda>y. ln y - y + 1"
  1690   have D: "\<And>x. 0 < x \<Longrightarrow> DERIV ?l x :> (1 / x - 1)"
  1691     by (auto intro!: DERIV_intros)
  1692 
  1693   show ?thesis
  1694   proof (cases rule: linorder_cases)
  1695     assume "x < 1"
  1696     from dense[OF `x < 1`] obtain a where "x < a" "a < 1" by blast
  1697     from `x < a` have "?l x < ?l a"
  1698     proof (rule DERIV_pos_imp_increasing, safe)
  1699       fix y
  1700       assume "x \<le> y" "y \<le> a"
  1701       with `0 < x` `a < 1` have "0 < 1 / y - 1" "0 < y"
  1702         by (auto simp: field_simps)
  1703       with D show "\<exists>z. DERIV ?l y :> z \<and> 0 < z"
  1704         by auto
  1705     qed
  1706     also have "\<dots> \<le> 0"
  1707       using ln_le_minus_one `0 < x` `x < a` by (auto simp: field_simps)
  1708     finally show "x = 1" using assms by auto
  1709   next
  1710     assume "1 < x"
  1711     from dense[OF this] obtain a where "1 < a" "a < x" by blast
  1712     from `a < x` have "?l x < ?l a"
  1713     proof (rule DERIV_neg_imp_decreasing, safe)
  1714       fix y
  1715       assume "a \<le> y" "y \<le> x"
  1716       with `1 < a` have "1 / y - 1 < 0" "0 < y"
  1717         by (auto simp: field_simps)
  1718       with D show "\<exists>z. DERIV ?l y :> z \<and> z < 0"
  1719         by blast
  1720     qed
  1721     also have "\<dots> \<le> 0"
  1722       using ln_le_minus_one `1 < a` by (auto simp: field_simps)
  1723     finally show "x = 1" using assms by auto
  1724   next
  1725     assume "x = 1"
  1726     then show ?thesis by simp
  1727   qed
  1728 qed
  1729 
  1730 lemma exp_at_bot: "(exp ---> (0::real)) at_bot"
  1731   unfolding tendsto_Zfun_iff
  1732 proof (rule ZfunI, simp add: eventually_at_bot_dense)
  1733   fix r :: real assume "0 < r"
  1734   {
  1735     fix x
  1736     assume "x < ln r"
  1737     then have "exp x < exp (ln r)"
  1738       by simp
  1739     with `0 < r` have "exp x < r"
  1740       by simp
  1741   }
  1742   then show "\<exists>k. \<forall>n<k. exp n < r" by auto
  1743 qed
  1744 
  1745 lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
  1746   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="ln"])
  1747      (auto intro: eventually_gt_at_top)
  1748 
  1749 lemma ln_at_0: "LIM x at_right 0. ln x :> at_bot"
  1750   by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
  1751      (auto simp: eventually_at_filter)
  1752 
  1753 lemma ln_at_top: "LIM x at_top. ln x :> at_top"
  1754   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
  1755      (auto intro: eventually_gt_at_top)
  1756 
  1757 lemma tendsto_power_div_exp_0: "((\<lambda>x. x ^ k / exp x) ---> (0::real)) at_top"
  1758 proof (induct k)
  1759   case 0
  1760   show "((\<lambda>x. x ^ 0 / exp x) ---> (0::real)) at_top"
  1761     by (simp add: inverse_eq_divide[symmetric])
  1762        (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
  1763               at_top_le_at_infinity order_refl)
  1764 next
  1765   case (Suc k)
  1766   show ?case
  1767   proof (rule lhospital_at_top_at_top)
  1768     show "eventually (\<lambda>x. DERIV (\<lambda>x. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
  1769       by eventually_elim (intro DERIV_intros, simp, simp)
  1770     show "eventually (\<lambda>x. DERIV exp x :> exp x) at_top"
  1771       by eventually_elim (auto intro!: DERIV_intros)
  1772     show "eventually (\<lambda>x. exp x \<noteq> 0) at_top"
  1773       by auto
  1774     from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
  1775     show "((\<lambda>x. real (Suc k) * x ^ k / exp x) ---> 0) at_top"
  1776       by simp
  1777   qed (rule exp_at_top)
  1778 qed
  1779 
  1780 
  1781 definition powr :: "[real,real] => real"  (infixr "powr" 80)
  1782   -- {*exponentation with real exponent*}
  1783   where "x powr a = exp(a * ln x)"
  1784 
  1785 definition log :: "[real,real] => real"
  1786   -- {*logarithm of @{term x} to base @{term a}*}
  1787   where "log a x = ln x / ln a"
  1788 
  1789 
  1790 lemma tendsto_log [tendsto_intros]:
  1791   "\<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"
  1792   unfolding log_def by (intro tendsto_intros) auto
  1793 
  1794 lemma continuous_log:
  1795   assumes "continuous F f"
  1796     and "continuous F g"
  1797     and "0 < f (Lim F (\<lambda>x. x))"
  1798     and "f (Lim F (\<lambda>x. x)) \<noteq> 1"
  1799     and "0 < g (Lim F (\<lambda>x. x))"
  1800   shows "continuous F (\<lambda>x. log (f x) (g x))"
  1801   using assms unfolding continuous_def by (rule tendsto_log)
  1802 
  1803 lemma continuous_at_within_log[continuous_intros]:
  1804   assumes "continuous (at a within s) f"
  1805     and "continuous (at a within s) g"
  1806     and "0 < f a"
  1807     and "f a \<noteq> 1"
  1808     and "0 < g a"
  1809   shows "continuous (at a within s) (\<lambda>x. log (f x) (g x))"
  1810   using assms unfolding continuous_within by (rule tendsto_log)
  1811 
  1812 lemma isCont_log[continuous_intros, simp]:
  1813   assumes "isCont f a" "isCont g a" "0 < f a" "f a \<noteq> 1" "0 < g a"
  1814   shows "isCont (\<lambda>x. log (f x) (g x)) a"
  1815   using assms unfolding continuous_at by (rule tendsto_log)
  1816 
  1817 lemma continuous_on_log[continuous_on_intros]:
  1818   assumes "continuous_on s f" "continuous_on s g"
  1819     and "\<forall>x\<in>s. 0 < f x" "\<forall>x\<in>s. f x \<noteq> 1" "\<forall>x\<in>s. 0 < g x"
  1820   shows "continuous_on s (\<lambda>x. log (f x) (g x))"
  1821   using assms unfolding continuous_on_def by (fast intro: tendsto_log)
  1822 
  1823 lemma powr_one_eq_one [simp]: "1 powr a = 1"
  1824   by (simp add: powr_def)
  1825 
  1826 lemma powr_zero_eq_one [simp]: "x powr 0 = 1"
  1827   by (simp add: powr_def)
  1828 
  1829 lemma powr_one_gt_zero_iff [simp]: "(x powr 1 = x) = (0 < x)"
  1830   by (simp add: powr_def)
  1831 declare powr_one_gt_zero_iff [THEN iffD2, simp]
  1832 
  1833 lemma powr_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x * y) powr a = (x powr a) * (y powr a)"
  1834   by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)
  1835 
  1836 lemma powr_gt_zero [simp]: "0 < x powr a"
  1837   by (simp add: powr_def)
  1838 
  1839 lemma powr_ge_pzero [simp]: "0 <= x powr y"
  1840   by (rule order_less_imp_le, rule powr_gt_zero)
  1841 
  1842 lemma powr_not_zero [simp]: "x powr a \<noteq> 0"
  1843   by (simp add: powr_def)
  1844 
  1845 lemma powr_divide: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
  1846   apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
  1847   apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
  1848   done
  1849 
  1850 lemma powr_divide2: "x powr a / x powr b = x powr (a - b)"
  1851   apply (simp add: powr_def)
  1852   apply (subst exp_diff [THEN sym])
  1853   apply (simp add: left_diff_distrib)
  1854   done
  1855 
  1856 lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
  1857   by (simp add: powr_def exp_add [symmetric] distrib_right)
  1858 
  1859 lemma powr_mult_base: "0 < x \<Longrightarrow>x * x powr y = x powr (1 + y)"
  1860   using assms by (auto simp: powr_add)
  1861 
  1862 lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
  1863   by (simp add: powr_def)
  1864 
  1865 lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
  1866   by (simp add: powr_powr mult_commute)
  1867 
  1868 lemma powr_minus: "x powr (-a) = inverse (x powr a)"
  1869   by (simp add: powr_def exp_minus [symmetric])
  1870 
  1871 lemma powr_minus_divide: "x powr (-a) = 1/(x powr a)"
  1872   by (simp add: divide_inverse powr_minus)
  1873 
  1874 lemma powr_less_mono: "a < b \<Longrightarrow> 1 < x \<Longrightarrow> x powr a < x powr b"
  1875   by (simp add: powr_def)
  1876 
  1877 lemma powr_less_cancel: "x powr a < x powr b \<Longrightarrow> 1 < x \<Longrightarrow> a < b"
  1878   by (simp add: powr_def)
  1879 
  1880 lemma powr_less_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a < x powr b) = (a < b)"
  1881   by (blast intro: powr_less_cancel powr_less_mono)
  1882 
  1883 lemma powr_le_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a \<le> x powr b) = (a \<le> b)"
  1884   by (simp add: linorder_not_less [symmetric])
  1885 
  1886 lemma log_ln: "ln x = log (exp(1)) x"
  1887   by (simp add: log_def)
  1888 
  1889 lemma DERIV_log:
  1890   assumes "x > 0"
  1891   shows "DERIV (\<lambda>y. log b y) x :> 1 / (ln b * x)"
  1892 proof -
  1893   def lb \<equiv> "1 / ln b"
  1894   moreover have "DERIV (\<lambda>y. lb * ln y) x :> lb / x"
  1895     using `x > 0` by (auto intro!: DERIV_intros)
  1896   ultimately show ?thesis
  1897     by (simp add: log_def)
  1898 qed
  1899 
  1900 lemmas DERIV_log[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  1901 
  1902 lemma powr_log_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> a powr (log a x) = x"
  1903   by (simp add: powr_def log_def)
  1904 
  1905 lemma log_powr_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a (a powr y) = y"
  1906   by (simp add: log_def powr_def)
  1907 
  1908 lemma log_mult:
  1909   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow>
  1910     log a (x * y) = log a x + log a y"
  1911   by (simp add: log_def ln_mult divide_inverse distrib_right)
  1912 
  1913 lemma log_eq_div_ln_mult_log:
  1914   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow>
  1915     log a x = (ln b/ln a) * log b x"
  1916   by (simp add: log_def divide_inverse)
  1917 
  1918 text{*Base 10 logarithms*}
  1919 lemma log_base_10_eq1: "0 < x \<Longrightarrow> log 10 x = (ln (exp 1) / ln 10) * ln x"
  1920   by (simp add: log_def)
  1921 
  1922 lemma log_base_10_eq2: "0 < x \<Longrightarrow> log 10 x = (log 10 (exp 1)) * ln x"
  1923   by (simp add: log_def)
  1924 
  1925 lemma log_one [simp]: "log a 1 = 0"
  1926   by (simp add: log_def)
  1927 
  1928 lemma log_eq_one [simp]: "[| 0 < a; a \<noteq> 1 |] ==> log a a = 1"
  1929   by (simp add: log_def)
  1930 
  1931 lemma log_inverse: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log a (inverse x) = - log a x"
  1932   apply (rule_tac a1 = "log a x" in add_left_cancel [THEN iffD1])
  1933   apply (simp add: log_mult [symmetric])
  1934   done
  1935 
  1936 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"
  1937   by (simp add: log_mult divide_inverse log_inverse)
  1938 
  1939 lemma log_less_cancel_iff [simp]:
  1940   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x < log a y \<longleftrightarrow> x < y"
  1941   apply safe
  1942   apply (rule_tac [2] powr_less_cancel)
  1943   apply (drule_tac a = "log a x" in powr_less_mono, auto)
  1944   done
  1945 
  1946 lemma log_inj:
  1947   assumes "1 < b"
  1948   shows "inj_on (log b) {0 <..}"
  1949 proof (rule inj_onI, simp)
  1950   fix x y
  1951   assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
  1952   show "x = y"
  1953   proof (cases rule: linorder_cases)
  1954     assume "x = y"
  1955     then show ?thesis by simp
  1956   next
  1957     assume "x < y" hence "log b x < log b y"
  1958       using log_less_cancel_iff[OF `1 < b`] pos by simp
  1959     then show ?thesis using * by simp
  1960   next
  1961     assume "y < x" hence "log b y < log b x"
  1962       using log_less_cancel_iff[OF `1 < b`] pos by simp
  1963     then show ?thesis using * by simp
  1964   qed
  1965 qed
  1966 
  1967 lemma log_le_cancel_iff [simp]:
  1968   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (log a x \<le> log a y) = (x \<le> y)"
  1969   by (simp add: linorder_not_less [symmetric])
  1970 
  1971 lemma zero_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < log a x \<longleftrightarrow> 1 < x"
  1972   using log_less_cancel_iff[of a 1 x] by simp
  1973 
  1974 lemma zero_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 \<le> log a x \<longleftrightarrow> 1 \<le> x"
  1975   using log_le_cancel_iff[of a 1 x] by simp
  1976 
  1977 lemma log_less_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 0 \<longleftrightarrow> x < 1"
  1978   using log_less_cancel_iff[of a x 1] by simp
  1979 
  1980 lemma log_le_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 0 \<longleftrightarrow> x \<le> 1"
  1981   using log_le_cancel_iff[of a x 1] by simp
  1982 
  1983 lemma one_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 < log a x \<longleftrightarrow> a < x"
  1984   using log_less_cancel_iff[of a a x] by simp
  1985 
  1986 lemma one_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> log a x \<longleftrightarrow> a \<le> x"
  1987   using log_le_cancel_iff[of a a x] by simp
  1988 
  1989 lemma log_less_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 1 \<longleftrightarrow> x < a"
  1990   using log_less_cancel_iff[of a x a] by simp
  1991 
  1992 lemma log_le_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 1 \<longleftrightarrow> x \<le> a"
  1993   using log_le_cancel_iff[of a x a] by simp
  1994 
  1995 lemma powr_realpow: "0 < x ==> x powr (real n) = x^n"
  1996   apply (induct n)
  1997   apply simp
  1998   apply (subgoal_tac "real(Suc n) = real n + 1")
  1999   apply (erule ssubst)
  2000   apply (subst powr_add, simp, simp)
  2001   done
  2002 
  2003 lemma powr_realpow_numeral: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x^(numeral n)"
  2004   unfolding real_of_nat_numeral[symmetric] by (rule powr_realpow)
  2005 
  2006 lemma powr_realpow2: "0 <= x ==> 0 < n ==> x^n = (if (x = 0) then 0 else x powr (real n))"
  2007   apply (case_tac "x = 0", simp, simp)
  2008   apply (rule powr_realpow [THEN sym], simp)
  2009   done
  2010 
  2011 lemma powr_int:
  2012   assumes "x > 0"
  2013   shows "x powr i = (if i \<ge> 0 then x ^ nat i else 1 / x ^ nat (-i))"
  2014 proof (cases "i < 0")
  2015   case True
  2016   have r: "x powr i = 1 / x powr (-i)" by (simp add: powr_minus field_simps)
  2017   show ?thesis using `i < 0` `x > 0` by (simp add: r field_simps powr_realpow[symmetric])
  2018 next
  2019   case False
  2020   then show ?thesis by (simp add: assms powr_realpow[symmetric])
  2021 qed
  2022 
  2023 lemma powr_numeral: "0 < x \<Longrightarrow> x powr numeral n = x^numeral n"
  2024   using powr_realpow[of x "numeral n"] by simp
  2025 
  2026 lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr neg_numeral n = 1 / x^numeral n"
  2027   using powr_int[of x "neg_numeral n"] by simp
  2028 
  2029 lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
  2030   by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
  2031 
  2032 lemma ln_powr: "0 < x ==> 0 < y ==> ln(x powr y) = y * ln x"
  2033   unfolding powr_def by simp
  2034 
  2035 lemma log_powr: "0 < x ==> 0 \<le> y ==> log b (x powr y) = y * log b x"
  2036   apply (cases "y = 0")
  2037   apply force
  2038   apply (auto simp add: log_def ln_powr field_simps)
  2039   done
  2040 
  2041 lemma log_nat_power: "0 < x ==> log b (x^n) = real n * log b x"
  2042   apply (subst powr_realpow [symmetric])
  2043   apply (auto simp add: log_powr)
  2044   done
  2045 
  2046 lemma ln_bound: "1 <= x ==> ln x <= x"
  2047   apply (subgoal_tac "ln(1 + (x - 1)) <= x - 1")
  2048   apply simp
  2049   apply (rule ln_add_one_self_le_self, simp)
  2050   done
  2051 
  2052 lemma powr_mono: "a <= b ==> 1 <= x ==> x powr a <= x powr b"
  2053   apply (cases "x = 1", simp)
  2054   apply (cases "a = b", simp)
  2055   apply (rule order_less_imp_le)
  2056   apply (rule powr_less_mono, auto)
  2057   done
  2058 
  2059 lemma ge_one_powr_ge_zero: "1 <= x ==> 0 <= a ==> 1 <= x powr a"
  2060   apply (subst powr_zero_eq_one [THEN sym])
  2061   apply (rule powr_mono, assumption+)
  2062   done
  2063 
  2064 lemma powr_less_mono2: "0 < a ==> 0 < x ==> x < y ==> x powr a < y powr a"
  2065   apply (unfold powr_def)
  2066   apply (rule exp_less_mono)
  2067   apply (rule mult_strict_left_mono)
  2068   apply (subst ln_less_cancel_iff, assumption)
  2069   apply (rule order_less_trans)
  2070   prefer 2
  2071   apply assumption+
  2072   done
  2073 
  2074 lemma powr_less_mono2_neg: "a < 0 ==> 0 < x ==> x < y ==> y powr a < x powr a"
  2075   apply (unfold powr_def)
  2076   apply (rule exp_less_mono)
  2077   apply (rule mult_strict_left_mono_neg)
  2078   apply (subst ln_less_cancel_iff)
  2079   apply assumption
  2080   apply (rule order_less_trans)
  2081   prefer 2
  2082   apply assumption+
  2083   done
  2084 
  2085 lemma powr_mono2: "0 <= a ==> 0 < x ==> x <= y ==> x powr a <= y powr a"
  2086   apply (case_tac "a = 0", simp)
  2087   apply (case_tac "x = y", simp)
  2088   apply (rule order_less_imp_le)
  2089   apply (rule powr_less_mono2, auto)
  2090   done
  2091 
  2092 lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
  2093   unfolding powr_def exp_inj_iff by simp
  2094 
  2095 lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
  2096   apply (rule mult_imp_le_div_pos)
  2097   apply (assumption)
  2098   apply (subst mult_commute)
  2099   apply (subst ln_powr [THEN sym])
  2100   apply auto
  2101   apply (rule ln_bound)
  2102   apply (erule ge_one_powr_ge_zero)
  2103   apply (erule order_less_imp_le)
  2104   done
  2105 
  2106 lemma ln_powr_bound2:
  2107   assumes "1 < x" and "0 < a"
  2108   shows "(ln x) powr a <= (a powr a) * x"
  2109 proof -
  2110   from assms have "ln x <= (x powr (1 / a)) / (1 / a)"
  2111     apply (intro ln_powr_bound)
  2112     apply (erule order_less_imp_le)
  2113     apply (rule divide_pos_pos)
  2114     apply simp_all
  2115     done
  2116   also have "... = a * (x powr (1 / a))"
  2117     by simp
  2118   finally have "(ln x) powr a <= (a * (x powr (1 / a))) powr a"
  2119     apply (intro powr_mono2)
  2120     apply (rule order_less_imp_le, rule assms)
  2121     apply (rule ln_gt_zero)
  2122     apply (rule assms)
  2123     apply assumption
  2124     done
  2125   also have "... = (a powr a) * ((x powr (1 / a)) powr a)"
  2126     apply (rule powr_mult)
  2127     apply (rule assms)
  2128     apply (rule powr_gt_zero)
  2129     done
  2130   also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
  2131     by (rule powr_powr)
  2132   also have "... = x"
  2133     apply simp
  2134     apply (subgoal_tac "a ~= 0")
  2135     using assms apply auto
  2136     done
  2137   finally show ?thesis .
  2138 qed
  2139 
  2140 lemma tendsto_powr [tendsto_intros]:
  2141   "\<lbrakk>(f ---> a) F; (g ---> b) F; 0 < a\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x powr g x) ---> a powr b) F"
  2142   unfolding powr_def by (intro tendsto_intros)
  2143 
  2144 lemma continuous_powr:
  2145   assumes "continuous F f"
  2146     and "continuous F g"
  2147     and "0 < f (Lim F (\<lambda>x. x))"
  2148   shows "continuous F (\<lambda>x. (f x) powr (g x))"
  2149   using assms unfolding continuous_def by (rule tendsto_powr)
  2150 
  2151 lemma continuous_at_within_powr[continuous_intros]:
  2152   assumes "continuous (at a within s) f"
  2153     and "continuous (at a within s) g"
  2154     and "0 < f a"
  2155   shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
  2156   using assms unfolding continuous_within by (rule tendsto_powr)
  2157 
  2158 lemma isCont_powr[continuous_intros, simp]:
  2159   assumes "isCont f a" "isCont g a" "0 < f a"
  2160   shows "isCont (\<lambda>x. (f x) powr g x) a"
  2161   using assms unfolding continuous_at by (rule tendsto_powr)
  2162 
  2163 lemma continuous_on_powr[continuous_on_intros]:
  2164   assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. 0 < f x"
  2165   shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
  2166   using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
  2167 
  2168 (* FIXME: generalize by replacing d by with g x and g ---> d? *)
  2169 lemma tendsto_zero_powrI:
  2170   assumes "eventually (\<lambda>x. 0 < f x ) F" and "(f ---> 0) F"
  2171     and "0 < d"
  2172   shows "((\<lambda>x. f x powr d) ---> 0) F"
  2173 proof (rule tendstoI)
  2174   fix e :: real assume "0 < e"
  2175   def Z \<equiv> "e powr (1 / d)"
  2176   with `0 < e` have "0 < Z" by simp
  2177   with assms have "eventually (\<lambda>x. 0 < f x \<and> dist (f x) 0 < Z) F"
  2178     by (intro eventually_conj tendstoD)
  2179   moreover
  2180   from assms have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> x powr d < Z powr d"
  2181     by (intro powr_less_mono2) (auto simp: dist_real_def)
  2182   with assms `0 < e` have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> dist (x powr d) 0 < e"
  2183     unfolding dist_real_def Z_def by (auto simp: powr_powr)
  2184   ultimately
  2185   show "eventually (\<lambda>x. dist (f x powr d) 0 < e) F" by (rule eventually_elim1)
  2186 qed
  2187 
  2188 lemma tendsto_neg_powr:
  2189   assumes "s < 0"
  2190     and "LIM x F. f x :> at_top"
  2191   shows "((\<lambda>x. f x powr s) ---> 0) F"
  2192 proof (rule tendstoI)
  2193   fix e :: real assume "0 < e"
  2194   def Z \<equiv> "e powr (1 / s)"
  2195   from assms have "eventually (\<lambda>x. Z < f x) F"
  2196     by (simp add: filterlim_at_top_dense)
  2197   moreover
  2198   from assms have "\<And>x. Z < x \<Longrightarrow> x powr s < Z powr s"
  2199     by (auto simp: Z_def intro!: powr_less_mono2_neg)
  2200   with assms `0 < e` have "\<And>x. Z < x \<Longrightarrow> dist (x powr s) 0 < e"
  2201     by (simp add: powr_powr Z_def dist_real_def)
  2202   ultimately
  2203   show "eventually (\<lambda>x. dist (f x powr s) 0 < e) F" by (rule eventually_elim1)
  2204 qed
  2205 
  2206 subsection {* Sine and Cosine *}
  2207 
  2208 definition sin_coeff :: "nat \<Rightarrow> real" where
  2209   "sin_coeff = (\<lambda>n. if even n then 0 else -1 ^ ((n - Suc 0) div 2) / real (fact n))"
  2210 
  2211 definition cos_coeff :: "nat \<Rightarrow> real" where
  2212   "cos_coeff = (\<lambda>n. if even n then (-1 ^ (n div 2)) / real (fact n) else 0)"
  2213 
  2214 definition sin :: "real \<Rightarrow> real"
  2215   where "sin = (\<lambda>x. \<Sum>n. sin_coeff n * x ^ n)"
  2216 
  2217 definition cos :: "real \<Rightarrow> real"
  2218   where "cos = (\<lambda>x. \<Sum>n. cos_coeff n * x ^ n)"
  2219 
  2220 lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
  2221   unfolding sin_coeff_def by simp
  2222 
  2223 lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
  2224   unfolding cos_coeff_def by simp
  2225 
  2226 lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
  2227   unfolding cos_coeff_def sin_coeff_def
  2228   by (simp del: mult_Suc)
  2229 
  2230 lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
  2231   unfolding cos_coeff_def sin_coeff_def
  2232   by (simp del: mult_Suc, auto simp add: odd_Suc_mult_two_ex)
  2233 
  2234 lemma summable_sin: "summable (\<lambda>n. sin_coeff n * x ^ n)"
  2235   unfolding sin_coeff_def
  2236   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
  2237   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  2238   done
  2239 
  2240 lemma summable_cos: "summable (\<lambda>n. cos_coeff n * x ^ n)"
  2241   unfolding cos_coeff_def
  2242   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
  2243   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  2244   done
  2245 
  2246 lemma sin_converges: "(\<lambda>n. sin_coeff n * x ^ n) sums sin(x)"
  2247   unfolding sin_def by (rule summable_sin [THEN summable_sums])
  2248 
  2249 lemma cos_converges: "(\<lambda>n. cos_coeff n * x ^ n) sums cos(x)"
  2250   unfolding cos_def by (rule summable_cos [THEN summable_sums])
  2251 
  2252 lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
  2253   by (simp add: diffs_def sin_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2254 
  2255 lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
  2256   by (simp add: diffs_def cos_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2257 
  2258 text{*Now at last we can get the derivatives of exp, sin and cos*}
  2259 
  2260 lemma DERIV_sin [simp]: "DERIV sin x :> cos(x)"
  2261   unfolding sin_def cos_def
  2262   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2263   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff
  2264     summable_minus summable_sin summable_cos)
  2265   done
  2266 
  2267 declare DERIV_sin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2268 
  2269 lemma DERIV_cos [simp]: "DERIV cos x :> -sin(x)"
  2270   unfolding cos_def sin_def
  2271   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2272   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff diffs_minus
  2273     summable_minus summable_sin summable_cos suminf_minus)
  2274   done
  2275 
  2276 declare DERIV_cos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2277 
  2278 lemma isCont_sin: "isCont sin x"
  2279   by (rule DERIV_sin [THEN DERIV_isCont])
  2280 
  2281 lemma isCont_cos: "isCont cos x"
  2282   by (rule DERIV_cos [THEN DERIV_isCont])
  2283 
  2284 lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
  2285   by (rule isCont_o2 [OF _ isCont_sin])
  2286 
  2287 lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
  2288   by (rule isCont_o2 [OF _ isCont_cos])
  2289 
  2290 lemma tendsto_sin [tendsto_intros]:
  2291   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) ---> sin a) F"
  2292   by (rule isCont_tendsto_compose [OF isCont_sin])
  2293 
  2294 lemma tendsto_cos [tendsto_intros]:
  2295   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) ---> cos a) F"
  2296   by (rule isCont_tendsto_compose [OF isCont_cos])
  2297 
  2298 lemma continuous_sin [continuous_intros]:
  2299   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
  2300   unfolding continuous_def by (rule tendsto_sin)
  2301 
  2302 lemma continuous_on_sin [continuous_on_intros]:
  2303   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
  2304   unfolding continuous_on_def by (auto intro: tendsto_sin)
  2305 
  2306 lemma continuous_cos [continuous_intros]:
  2307   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
  2308   unfolding continuous_def by (rule tendsto_cos)
  2309 
  2310 lemma continuous_on_cos [continuous_on_intros]:
  2311   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
  2312   unfolding continuous_on_def by (auto intro: tendsto_cos)
  2313 
  2314 subsection {* Properties of Sine and Cosine *}
  2315 
  2316 lemma sin_zero [simp]: "sin 0 = 0"
  2317   unfolding sin_def sin_coeff_def by (simp add: powser_zero)
  2318 
  2319 lemma cos_zero [simp]: "cos 0 = 1"
  2320   unfolding cos_def cos_coeff_def by (simp add: powser_zero)
  2321 
  2322 lemma sin_cos_squared_add [simp]: "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
  2323 proof -
  2324   have "\<forall>x. DERIV (\<lambda>x. (sin x)\<^sup>2 + (cos x)\<^sup>2) x :> 0"
  2325     by (auto intro!: DERIV_intros)
  2326   hence "(sin x)\<^sup>2 + (cos x)\<^sup>2 = (sin 0)\<^sup>2 + (cos 0)\<^sup>2"
  2327     by (rule DERIV_isconst_all)
  2328   thus "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1" by simp
  2329 qed
  2330 
  2331 lemma sin_cos_squared_add2 [simp]: "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
  2332   by (subst add_commute, rule sin_cos_squared_add)
  2333 
  2334 lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
  2335   using sin_cos_squared_add2 [unfolded power2_eq_square] .
  2336 
  2337 lemma sin_squared_eq: "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
  2338   unfolding eq_diff_eq by (rule sin_cos_squared_add)
  2339 
  2340 lemma cos_squared_eq: "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
  2341   unfolding eq_diff_eq by (rule sin_cos_squared_add2)
  2342 
  2343 lemma abs_sin_le_one [simp]: "\<bar>sin x\<bar> \<le> 1"
  2344   by (rule power2_le_imp_le, simp_all add: sin_squared_eq)
  2345 
  2346 lemma sin_ge_minus_one [simp]: "-1 \<le> sin x"
  2347   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2348 
  2349 lemma sin_le_one [simp]: "sin x \<le> 1"
  2350   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2351 
  2352 lemma abs_cos_le_one [simp]: "\<bar>cos x\<bar> \<le> 1"
  2353   by (rule power2_le_imp_le, simp_all add: cos_squared_eq)
  2354 
  2355 lemma cos_ge_minus_one [simp]: "-1 \<le> cos x"
  2356   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2357 
  2358 lemma cos_le_one [simp]: "cos x \<le> 1"
  2359   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2360 
  2361 lemma DERIV_fun_pow: "DERIV g x :> m ==>
  2362       DERIV (\<lambda>x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
  2363   by (auto intro!: DERIV_intros)
  2364 
  2365 lemma DERIV_fun_exp:
  2366      "DERIV g x :> m ==> DERIV (\<lambda>x. exp(g x)) x :> exp(g x) * m"
  2367   by (auto intro!: DERIV_intros)
  2368 
  2369 lemma DERIV_fun_sin:
  2370      "DERIV g x :> m ==> DERIV (\<lambda>x. sin(g x)) x :> cos(g x) * m"
  2371   by (auto intro!: DERIV_intros)
  2372 
  2373 lemma DERIV_fun_cos:
  2374      "DERIV g x :> m ==> DERIV (\<lambda>x. cos(g x)) x :> -sin(g x) * m"
  2375   by (auto intro!: DERIV_intros)
  2376 
  2377 lemma sin_cos_add_lemma:
  2378   "(sin (x + y) - (sin x * cos y + cos x * sin y))\<^sup>2 +
  2379     (cos (x + y) - (cos x * cos y - sin x * sin y))\<^sup>2 = 0"
  2380   (is "?f x = 0")
  2381 proof -
  2382   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2383     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2384   hence "?f x = ?f 0"
  2385     by (rule DERIV_isconst_all)
  2386   thus ?thesis by simp
  2387 qed
  2388 
  2389 lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
  2390   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2391 
  2392 lemma cos_add: "cos (x + y) = cos x * cos y - sin x * sin y"
  2393   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2394 
  2395 lemma sin_cos_minus_lemma:
  2396   "(sin(-x) + sin(x))\<^sup>2 + (cos(-x) - cos(x))\<^sup>2 = 0" (is "?f x = 0")
  2397 proof -
  2398   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2399     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2400   hence "?f x = ?f 0"
  2401     by (rule DERIV_isconst_all)
  2402   thus ?thesis by simp
  2403 qed
  2404 
  2405 lemma sin_minus [simp]: "sin (-x) = -sin(x)"
  2406   using sin_cos_minus_lemma [where x=x] by simp
  2407 
  2408 lemma cos_minus [simp]: "cos (-x) = cos(x)"
  2409   using sin_cos_minus_lemma [where x=x] by simp
  2410 
  2411 lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
  2412   using sin_add [of x "- y"] by simp
  2413 
  2414 lemma sin_diff2: "sin (x - y) = cos y * sin x - sin y * cos x"
  2415   by (simp add: sin_diff mult_commute)
  2416 
  2417 lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
  2418   using cos_add [of x "- y"] by simp
  2419 
  2420 lemma cos_diff2: "cos (x - y) = cos y * cos x + sin y * sin x"
  2421   by (simp add: cos_diff mult_commute)
  2422 
  2423 lemma sin_double [simp]: "sin(2 * x) = 2* sin x * cos x"
  2424   using sin_add [where x=x and y=x] by simp
  2425 
  2426 lemma cos_double: "cos(2* x) = ((cos x)\<^sup>2) - ((sin x)\<^sup>2)"
  2427   using cos_add [where x=x and y=x]
  2428   by (simp add: power2_eq_square)
  2429 
  2430 
  2431 subsection {* The Constant Pi *}
  2432 
  2433 definition pi :: real
  2434   where "pi = 2 * (THE x. 0 \<le> (x::real) & x \<le> 2 & cos x = 0)"
  2435 
  2436 text{*Show that there's a least positive @{term x} with @{term "cos(x) = 0"};
  2437    hence define pi.*}
  2438 
  2439 lemma sin_paired:
  2440   "(\<lambda>n. -1 ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) sums  sin x"
  2441 proof -
  2442   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
  2443     by (rule sin_converges [THEN sums_group], simp)
  2444   thus ?thesis unfolding One_nat_def sin_coeff_def by (simp add: mult_ac)
  2445 qed
  2446 
  2447 lemma sin_gt_zero:
  2448   assumes "0 < x" and "x < 2"
  2449   shows "0 < sin x"
  2450 proof -
  2451   let ?f = "\<lambda>n. \<Sum>k = n*2..<n*2+2. -1 ^ k / real (fact (2*k+1)) * x^(2*k+1)"
  2452   have pos: "\<forall>n. 0 < ?f n"
  2453   proof
  2454     fix n :: nat
  2455     let ?k2 = "real (Suc (Suc (4 * n)))"
  2456     let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
  2457     have "x * x < ?k2 * ?k3"
  2458       using assms by (intro mult_strict_mono', simp_all)
  2459     hence "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
  2460       by (intro mult_strict_right_mono zero_less_power `0 < x`)
  2461     thus "0 < ?f n"
  2462       by (simp del: mult_Suc,
  2463         simp add: less_divide_eq mult_pos_pos field_simps del: mult_Suc)
  2464   qed
  2465   have sums: "?f sums sin x"
  2466     by (rule sin_paired [THEN sums_group], simp)
  2467   show "0 < sin x"
  2468     unfolding sums_unique [OF sums]
  2469     using sums_summable [OF sums] pos
  2470     by (rule suminf_gt_zero)
  2471 qed
  2472 
  2473 lemma cos_double_less_one: "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
  2474   using sin_gt_zero [where x = x] by (auto simp add: cos_squared_eq cos_double)
  2475 
  2476 lemma cos_paired: "(\<lambda>n. -1 ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
  2477 proof -
  2478   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
  2479     by (rule cos_converges [THEN sums_group], simp)
  2480   thus ?thesis unfolding cos_coeff_def by (simp add: mult_ac)
  2481 qed
  2482 
  2483 lemma real_mult_inverse_cancel:
  2484      "[|(0::real) < x; 0 < x1; x1 * y < x * u |]
  2485       ==> inverse x * y < inverse x1 * u"
  2486   apply (rule_tac c=x in mult_less_imp_less_left)
  2487   apply (auto simp add: mult_assoc [symmetric])
  2488   apply (simp (no_asm) add: mult_ac)
  2489   apply (rule_tac c=x1 in mult_less_imp_less_right)
  2490   apply (auto simp add: mult_ac)
  2491   done
  2492 
  2493 lemma real_mult_inverse_cancel2:
  2494      "[|(0::real) < x;0 < x1; x1 * y < x * u |] ==> y * inverse x < u * inverse x1"
  2495   by (auto dest: real_mult_inverse_cancel simp add: mult_ac)
  2496 
  2497 lemmas realpow_num_eq_if = power_eq_if
  2498 
  2499 lemma cos_two_less_zero [simp]:
  2500   "cos 2 < 0"
  2501 proof -
  2502   note fact_Suc [simp del]
  2503   from cos_paired
  2504   have "(\<lambda>n. - (-1 ^ n / real (fact (2 * n)) * 2 ^ (2 * n))) sums - cos 2"
  2505     by (rule sums_minus)
  2506   then have *: "(\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n)))) sums - cos 2"
  2507     by simp
  2508   then have **: "summable (\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2509     by (rule sums_summable)
  2510   have "0 < (\<Sum>n = 0..<Suc (Suc (Suc 0)). - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2511     by (simp add: fact_num_eq_if_nat realpow_num_eq_if)
  2512   moreover have "(\<Sum>n = 0..<Suc (Suc (Suc 0)). - (-1 ^ n  * 2 ^ (2 * n) / real (fact (2 * n))))
  2513     < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2514   proof -
  2515     { fix d
  2516       have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
  2517        < real (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))) *
  2518            fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
  2519         by (simp only: real_of_nat_mult) (auto intro!: mult_strict_mono fact_less_mono_nat)
  2520       then have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
  2521         < real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))))"
  2522         by (simp only: fact_Suc [of "Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))"])
  2523       then have "4 * inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))))
  2524         < inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
  2525         by (simp add: inverse_eq_divide less_divide_eq)
  2526     }
  2527     note *** = this
  2528     have [simp]: "\<And>x y::real. 0 < x - y \<longleftrightarrow> y < x" by arith
  2529     from ** show ?thesis by (rule sumr_pos_lt_pair)
  2530       (simp add: divide_inverse mult_assoc [symmetric] ***)
  2531   qed
  2532   ultimately have "0 < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2533     by (rule order_less_trans)
  2534   moreover from * have "- cos 2 = (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2535     by (rule sums_unique)
  2536   ultimately have "0 < - cos 2" by simp
  2537   then show ?thesis by simp
  2538 qed
  2539 
  2540 lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
  2541 lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]
  2542 
  2543 lemma cos_is_zero: "EX! x. 0 \<le> x & x \<le> 2 \<and> cos x = 0"
  2544 proof (rule ex_ex1I)
  2545   show "\<exists>x. 0 \<le> x & x \<le> 2 & cos x = 0"
  2546     by (rule IVT2, simp_all)
  2547 next
  2548   fix x y
  2549   assume x: "0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
  2550   assume y: "0 \<le> y \<and> y \<le> 2 \<and> cos y = 0"
  2551   have [simp]: "\<forall>x. cos differentiable x"
  2552     unfolding differentiable_def by (auto intro: DERIV_cos)
  2553   from x y show "x = y"
  2554     apply (cut_tac less_linear [of x y], auto)
  2555     apply (drule_tac f = cos in Rolle)
  2556     apply (drule_tac [5] f = cos in Rolle)
  2557     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2558     apply (metis order_less_le_trans less_le sin_gt_zero)
  2559     apply (metis order_less_le_trans less_le sin_gt_zero)
  2560     done
  2561 qed
  2562 
  2563 lemma pi_half: "pi/2 = (THE x. 0 \<le> x & x \<le> 2 & cos x = 0)"
  2564   by (simp add: pi_def)
  2565 
  2566 lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
  2567   by (simp add: pi_half cos_is_zero [THEN theI'])
  2568 
  2569 lemma pi_half_gt_zero [simp]: "0 < pi / 2"
  2570   apply (rule order_le_neq_trans)
  2571   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2572   apply (rule notI, drule arg_cong [where f=cos], simp)
  2573   done
  2574 
  2575 lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
  2576 lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]
  2577 
  2578 lemma pi_half_less_two [simp]: "pi / 2 < 2"
  2579   apply (rule order_le_neq_trans)
  2580   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2581   apply (rule notI, drule arg_cong [where f=cos], simp)
  2582   done
  2583 
  2584 lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
  2585 lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]
  2586 
  2587 lemma pi_gt_zero [simp]: "0 < pi"
  2588   using pi_half_gt_zero by simp
  2589 
  2590 lemma pi_ge_zero [simp]: "0 \<le> pi"
  2591   by (rule pi_gt_zero [THEN order_less_imp_le])
  2592 
  2593 lemma pi_neq_zero [simp]: "pi \<noteq> 0"
  2594   by (rule pi_gt_zero [THEN less_imp_neq, symmetric])
  2595 
  2596 lemma pi_not_less_zero [simp]: "\<not> pi < 0"
  2597   by (simp add: linorder_not_less)
  2598 
  2599 lemma minus_pi_half_less_zero: "-(pi/2) < 0"
  2600   by simp
  2601 
  2602 lemma m2pi_less_pi: "- (2 * pi) < pi"
  2603   by simp
  2604 
  2605 lemma sin_pi_half [simp]: "sin(pi/2) = 1"
  2606   using sin_cos_squared_add2 [where x = "pi/2"]
  2607   using sin_gt_zero [OF pi_half_gt_zero pi_half_less_two]
  2608   by (simp add: power2_eq_1_iff)
  2609 
  2610 lemma cos_pi [simp]: "cos pi = -1"
  2611   using cos_add [where x = "pi/2" and y = "pi/2"] by simp
  2612 
  2613 lemma sin_pi [simp]: "sin pi = 0"
  2614   using sin_add [where x = "pi/2" and y = "pi/2"] by simp
  2615 
  2616 lemma sin_cos_eq: "sin x = cos (pi/2 - x)"
  2617   by (simp add: cos_diff)
  2618 
  2619 lemma minus_sin_cos_eq: "-sin x = cos (x + pi/2)"
  2620   by (simp add: cos_add)
  2621 
  2622 lemma cos_sin_eq: "cos x = sin (pi/2 - x)"
  2623   by (simp add: sin_diff)
  2624 
  2625 lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
  2626   by (simp add: sin_add)
  2627 
  2628 lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
  2629   by (simp add: sin_add)
  2630 
  2631 lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
  2632   by (simp add: cos_add)
  2633 
  2634 lemma sin_periodic [simp]: "sin (x + 2*pi) = sin x"
  2635   by (simp add: sin_add cos_double)
  2636 
  2637 lemma cos_periodic [simp]: "cos (x + 2*pi) = cos x"
  2638   by (simp add: cos_add cos_double)
  2639 
  2640 lemma cos_npi [simp]: "cos (real n * pi) = -1 ^ n"
  2641   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2642 
  2643 lemma cos_npi2 [simp]: "cos (pi * real n) = -1 ^ n"
  2644 proof -
  2645   have "cos (pi * real n) = cos (real n * pi)" by (simp only: mult_commute)
  2646   also have "... = -1 ^ n" by (rule cos_npi)
  2647   finally show ?thesis .
  2648 qed
  2649 
  2650 lemma sin_npi [simp]: "sin (real (n::nat) * pi) = 0"
  2651   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2652 
  2653 lemma sin_npi2 [simp]: "sin (pi * real (n::nat)) = 0"
  2654   by (simp add: mult_commute [of pi])
  2655 
  2656 lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
  2657   by (simp add: cos_double)
  2658 
  2659 lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  2660   by simp
  2661 
  2662 lemma sin_gt_zero2: "[| 0 < x; x < pi/2 |] ==> 0 < sin x"
  2663   apply (rule sin_gt_zero, assumption)
  2664   apply (rule order_less_trans, assumption)
  2665   apply (rule pi_half_less_two)
  2666   done
  2667 
  2668 lemma sin_less_zero:
  2669   assumes "- pi/2 < x" and "x < 0"
  2670   shows "sin x < 0"
  2671 proof -
  2672   have "0 < sin (- x)" using assms by (simp only: sin_gt_zero2)
  2673   thus ?thesis by simp
  2674 qed
  2675 
  2676 lemma pi_less_4: "pi < 4"
  2677   using pi_half_less_two by auto
  2678 
  2679 lemma cos_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < cos x"
  2680   apply (cut_tac pi_less_4)
  2681   apply (cut_tac f = cos and a = 0 and b = x and y = 0 in IVT2_objl, safe, simp_all)
  2682   apply (cut_tac cos_is_zero, safe)
  2683   apply (rename_tac y z)
  2684   apply (drule_tac x = y in spec)
  2685   apply (drule_tac x = "pi/2" in spec, simp)
  2686   done
  2687 
  2688 lemma cos_gt_zero_pi: "[| -(pi/2) < x; x < pi/2 |] ==> 0 < cos x"
  2689   apply (rule_tac x = x and y = 0 in linorder_cases)
  2690   apply (rule cos_minus [THEN subst])
  2691   apply (rule cos_gt_zero)
  2692   apply (auto intro: cos_gt_zero)
  2693   done
  2694 
  2695 lemma cos_ge_zero: "[| -(pi/2) \<le> x; x \<le> pi/2 |] ==> 0 \<le> cos x"
  2696   apply (auto simp add: order_le_less cos_gt_zero_pi)
  2697   apply (subgoal_tac "x = pi/2", auto)
  2698   done
  2699 
  2700 lemma sin_gt_zero_pi: "[| 0 < x; x < pi  |] ==> 0 < sin x"
  2701   by (simp add: sin_cos_eq cos_gt_zero_pi)
  2702 
  2703 lemma pi_ge_two: "2 \<le> pi"
  2704 proof (rule ccontr)
  2705   assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
  2706   have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
  2707   proof (cases "2 < 2 * pi")
  2708     case True with dense[OF `pi < 2`] show ?thesis by auto
  2709   next
  2710     case False have "pi < 2 * pi" by auto
  2711     from dense[OF this] and False show ?thesis by auto
  2712   qed
  2713   then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
  2714   hence "0 < sin y" using sin_gt_zero by auto
  2715   moreover
  2716   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
  2717   ultimately show False by auto
  2718 qed
  2719 
  2720 lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
  2721   by (auto simp add: order_le_less sin_gt_zero_pi)
  2722 
  2723 text {* FIXME: This proof is almost identical to lemma @{text cos_is_zero}.
  2724   It should be possible to factor out some of the common parts. *}
  2725 
  2726 lemma cos_total: "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. 0 \<le> x & x \<le> pi & (cos x = y)"
  2727 proof (rule ex_ex1I)
  2728   assume y: "-1 \<le> y" "y \<le> 1"
  2729   show "\<exists>x. 0 \<le> x & x \<le> pi & cos x = y"
  2730     by (rule IVT2, simp_all add: y)
  2731 next
  2732   fix a b
  2733   assume a: "0 \<le> a \<and> a \<le> pi \<and> cos a = y"
  2734   assume b: "0 \<le> b \<and> b \<le> pi \<and> cos b = y"
  2735   have [simp]: "\<forall>x. cos differentiable x"
  2736     unfolding differentiable_def by (auto intro: DERIV_cos)
  2737   from a b show "a = b"
  2738     apply (cut_tac less_linear [of a b], auto)
  2739     apply (drule_tac f = cos in Rolle)
  2740     apply (drule_tac [5] f = cos in Rolle)
  2741     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2742     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2743     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2744     done
  2745 qed
  2746 
  2747 lemma sin_total:
  2748      "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. -(pi/2) \<le> x & x \<le> pi/2 & (sin x = y)"
  2749 apply (rule ccontr)
  2750 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))")
  2751 apply (erule contrapos_np)
  2752 apply simp
  2753 apply (cut_tac y="-y" in cos_total, simp) apply simp
  2754 apply (erule ex1E)
  2755 apply (rule_tac a = "x - (pi/2)" in ex1I)
  2756 apply (simp (no_asm) add: add_assoc)
  2757 apply (rotate_tac 3)
  2758 apply (drule_tac x = "xa + pi/2" in spec, safe, simp_all add: cos_add)
  2759 done
  2760 
  2761 lemma reals_Archimedean4:
  2762      "[| 0 < y; 0 \<le> x |] ==> \<exists>n. real n * y \<le> x & x < real (Suc n) * y"
  2763 apply (auto dest!: reals_Archimedean3)
  2764 apply (drule_tac x = x in spec, clarify)
  2765 apply (subgoal_tac "x < real(LEAST m::nat. x < real m * y) * y")
  2766  prefer 2 apply (erule LeastI)
  2767 apply (case_tac "LEAST m::nat. x < real m * y", simp)
  2768 apply (subgoal_tac "~ x < real nat * y")
  2769  prefer 2 apply (rule not_less_Least, simp, force)
  2770 done
  2771 
  2772 (* Pre Isabelle99-2 proof was simpler- numerals arithmetic
  2773    now causes some unwanted re-arrangements of literals!   *)
  2774 lemma cos_zero_lemma:
  2775      "[| 0 \<le> x; cos x = 0 |] ==>
  2776       \<exists>n::nat. ~even n & x = real n * (pi/2)"
  2777 apply (drule pi_gt_zero [THEN reals_Archimedean4], safe)
  2778 apply (subgoal_tac "0 \<le> x - real n * pi &
  2779                     (x - real n * pi) \<le> pi & (cos (x - real n * pi) = 0) ")
  2780 apply (auto simp add: algebra_simps real_of_nat_Suc)
  2781  prefer 2 apply (simp add: cos_diff)
  2782 apply (simp add: cos_diff)
  2783 apply (subgoal_tac "EX! x. 0 \<le> x & x \<le> pi & cos x = 0")
  2784 apply (rule_tac [2] cos_total, safe)
  2785 apply (drule_tac x = "x - real n * pi" in spec)
  2786 apply (drule_tac x = "pi/2" in spec)
  2787 apply (simp add: cos_diff)
  2788 apply (rule_tac x = "Suc (2 * n)" in exI)
  2789 apply (simp add: real_of_nat_Suc algebra_simps, auto)
  2790 done
  2791 
  2792 lemma sin_zero_lemma:
  2793      "[| 0 \<le> x; sin x = 0 |] ==>
  2794       \<exists>n::nat. even n & x = real n * (pi/2)"
  2795 apply (subgoal_tac "\<exists>n::nat. ~ even n & x + pi/2 = real n * (pi/2) ")
  2796  apply (clarify, rule_tac x = "n - 1" in exI)
  2797  apply (force simp add: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2798 apply (rule cos_zero_lemma)
  2799 apply (simp_all add: cos_add)
  2800 done
  2801 
  2802 
  2803 lemma cos_zero_iff:
  2804      "(cos x = 0) =
  2805       ((\<exists>n::nat. ~even n & (x = real n * (pi/2))) |
  2806        (\<exists>n::nat. ~even n & (x = -(real n * (pi/2)))))"
  2807 apply (rule iffI)
  2808 apply (cut_tac linorder_linear [of 0 x], safe)
  2809 apply (drule cos_zero_lemma, assumption+)
  2810 apply (cut_tac x="-x" in cos_zero_lemma, simp, simp)
  2811 apply (force simp add: minus_equation_iff [of x])
  2812 apply (auto simp only: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2813 apply (auto simp add: cos_diff cos_add)
  2814 done
  2815 
  2816 (* ditto: but to a lesser extent *)
  2817 lemma sin_zero_iff:
  2818      "(sin x = 0) =
  2819       ((\<exists>n::nat. even n & (x = real n * (pi/2))) |
  2820        (\<exists>n::nat. even n & (x = -(real n * (pi/2)))))"
  2821 apply (rule iffI)
  2822 apply (cut_tac linorder_linear [of 0 x], safe)
  2823 apply (drule sin_zero_lemma, assumption+)
  2824 apply (cut_tac x="-x" in sin_zero_lemma, simp, simp, safe)
  2825 apply (force simp add: minus_equation_iff [of x])
  2826 apply (auto simp add: even_mult_two_ex)
  2827 done
  2828 
  2829 lemma cos_monotone_0_pi:
  2830   assumes "0 \<le> y" and "y < x" and "x \<le> pi"
  2831   shows "cos x < cos y"
  2832 proof -
  2833   have "- (x - y) < 0" using assms by auto
  2834 
  2835   from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
  2836   obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z"
  2837     by auto
  2838   hence "0 < z" and "z < pi" using assms by auto
  2839   hence "0 < sin z" using sin_gt_zero_pi by auto
  2840   hence "cos x - cos y < 0"
  2841     unfolding cos_diff minus_mult_commute[symmetric]
  2842     using `- (x - y) < 0` by (rule mult_pos_neg2)
  2843   thus ?thesis by auto
  2844 qed
  2845 
  2846 lemma cos_monotone_0_pi':
  2847   assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi"
  2848   shows "cos x \<le> cos y"
  2849 proof (cases "y < x")
  2850   case True
  2851   show ?thesis
  2852     using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
  2853 next
  2854   case False
  2855   hence "y = x" using `y \<le> x` by auto
  2856   thus ?thesis by auto
  2857 qed
  2858 
  2859 lemma cos_monotone_minus_pi_0:
  2860   assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
  2861   shows "cos y < cos x"
  2862 proof -
  2863   have "0 \<le> -x" and "-x < -y" and "-y \<le> pi"
  2864     using assms by auto
  2865   from cos_monotone_0_pi[OF this] show ?thesis
  2866     unfolding cos_minus .
  2867 qed
  2868 
  2869 lemma cos_monotone_minus_pi_0':
  2870   assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0"
  2871   shows "cos y \<le> cos x"
  2872 proof (cases "y < x")
  2873   case True
  2874   show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`]
  2875     by auto
  2876 next
  2877   case False
  2878   hence "y = x" using `y \<le> x` by auto
  2879   thus ?thesis by auto
  2880 qed
  2881 
  2882 lemma sin_monotone_2pi':
  2883   assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2"
  2884   shows "sin y \<le> sin x"
  2885 proof -
  2886   have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi"
  2887     using pi_ge_two and assms by auto
  2888   from cos_monotone_0_pi'[OF this] show ?thesis
  2889     unfolding minus_sin_cos_eq[symmetric] by auto
  2890 qed
  2891 
  2892 
  2893 subsection {* Tangent *}
  2894 
  2895 definition tan :: "real \<Rightarrow> real"
  2896   where "tan = (\<lambda>x. sin x / cos x)"
  2897 
  2898 lemma tan_zero [simp]: "tan 0 = 0"
  2899   by (simp add: tan_def)
  2900 
  2901 lemma tan_pi [simp]: "tan pi = 0"
  2902   by (simp add: tan_def)
  2903 
  2904 lemma tan_npi [simp]: "tan (real (n::nat) * pi) = 0"
  2905   by (simp add: tan_def)
  2906 
  2907 lemma tan_minus [simp]: "tan (-x) = - tan x"
  2908   by (simp add: tan_def)
  2909 
  2910 lemma tan_periodic [simp]: "tan (x + 2*pi) = tan x"
  2911   by (simp add: tan_def)
  2912 
  2913 lemma lemma_tan_add1:
  2914   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> 1 - tan x * tan y = cos (x + y)/(cos x * cos y)"
  2915   by (simp add: tan_def cos_add field_simps)
  2916 
  2917 lemma add_tan_eq:
  2918   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> tan x + tan y = sin(x + y)/(cos x * cos y)"
  2919   by (simp add: tan_def sin_add field_simps)
  2920 
  2921 lemma tan_add:
  2922      "[| cos x \<noteq> 0; cos y \<noteq> 0; cos (x + y) \<noteq> 0 |]
  2923       ==> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
  2924   by (simp add: add_tan_eq lemma_tan_add1, simp add: tan_def)
  2925 
  2926 lemma tan_double:
  2927      "[| cos x \<noteq> 0; cos (2 * x) \<noteq> 0 |]
  2928       ==> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
  2929   using tan_add [of x x] by (simp add: power2_eq_square)
  2930 
  2931 lemma tan_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < tan x"
  2932   by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
  2933 
  2934 lemma tan_less_zero:
  2935   assumes lb: "- pi/2 < x" and "x < 0"
  2936   shows "tan x < 0"
  2937 proof -
  2938   have "0 < tan (- x)" using assms by (simp only: tan_gt_zero)
  2939   thus ?thesis by simp
  2940 qed
  2941 
  2942 lemma tan_half: "tan x = sin (2 * x) / (cos (2 * x) + 1)"
  2943   unfolding tan_def sin_double cos_double sin_squared_eq
  2944   by (simp add: power2_eq_square)
  2945 
  2946 lemma DERIV_tan [simp]: "cos x \<noteq> 0 \<Longrightarrow> DERIV tan x :> inverse ((cos x)\<^sup>2)"
  2947   unfolding tan_def
  2948   by (auto intro!: DERIV_intros, simp add: divide_inverse power2_eq_square)
  2949 
  2950 lemma isCont_tan: "cos x \<noteq> 0 \<Longrightarrow> isCont tan x"
  2951   by (rule DERIV_tan [THEN DERIV_isCont])
  2952 
  2953 lemma isCont_tan' [simp]:
  2954   "\<lbrakk>isCont f a; cos (f a) \<noteq> 0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. tan (f x)) a"
  2955   by (rule isCont_o2 [OF _ isCont_tan])
  2956 
  2957 lemma tendsto_tan [tendsto_intros]:
  2958   "\<lbrakk>(f ---> a) F; cos a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. tan (f x)) ---> tan a) F"
  2959   by (rule isCont_tendsto_compose [OF isCont_tan])
  2960 
  2961 lemma continuous_tan:
  2962   "continuous F f \<Longrightarrow> cos (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. tan (f x))"
  2963   unfolding continuous_def by (rule tendsto_tan)
  2964 
  2965 lemma isCont_tan'' [continuous_intros]:
  2966   "continuous (at x) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. tan (f x))"
  2967   unfolding continuous_at by (rule tendsto_tan)
  2968 
  2969 lemma continuous_within_tan [continuous_intros]:
  2970   "continuous (at x within s) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
  2971   unfolding continuous_within by (rule tendsto_tan)
  2972 
  2973 lemma continuous_on_tan [continuous_on_intros]:
  2974   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. cos (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. tan (f x))"
  2975   unfolding continuous_on_def by (auto intro: tendsto_tan)
  2976 
  2977 lemma LIM_cos_div_sin: "(\<lambda>x. cos(x)/sin(x)) -- pi/2 --> 0"
  2978   by (rule LIM_cong_limit, (rule tendsto_intros)+, simp_all)
  2979 
  2980 lemma lemma_tan_total: "0 < y ==> \<exists>x. 0 < x & x < pi/2 & y < tan x"
  2981   apply (cut_tac LIM_cos_div_sin)
  2982   apply (simp only: LIM_eq)
  2983   apply (drule_tac x = "inverse y" in spec, safe, force)
  2984   apply (drule_tac ?d1.0 = s in pi_half_gt_zero [THEN [2] real_lbound_gt_zero], safe)
  2985   apply (rule_tac x = "(pi/2) - e" in exI)
  2986   apply (simp (no_asm_simp))
  2987   apply (drule_tac x = "(pi/2) - e" in spec)
  2988   apply (auto simp add: tan_def sin_diff cos_diff)
  2989   apply (rule inverse_less_iff_less [THEN iffD1])
  2990   apply (auto simp add: divide_inverse)
  2991   apply (rule mult_pos_pos)
  2992   apply (subgoal_tac [3] "0 < sin e & 0 < cos e")
  2993   apply (auto intro: cos_gt_zero sin_gt_zero2 simp add: mult_commute)
  2994   done
  2995 
  2996 lemma tan_total_pos: "0 \<le> y ==> \<exists>x. 0 \<le> x & x < pi/2 & tan x = y"
  2997   apply (frule order_le_imp_less_or_eq, safe)
  2998    prefer 2 apply force
  2999   apply (drule lemma_tan_total, safe)
  3000   apply (cut_tac f = tan and a = 0 and b = x and y = y in IVT_objl)
  3001   apply (auto intro!: DERIV_tan [THEN DERIV_isCont])
  3002   apply (drule_tac y = xa in order_le_imp_less_or_eq)
  3003   apply (auto dest: cos_gt_zero)
  3004   done
  3005 
  3006 lemma lemma_tan_total1: "\<exists>x. -(pi/2) < x & x < (pi/2) & tan x = y"
  3007   apply (cut_tac linorder_linear [of 0 y], safe)
  3008   apply (drule tan_total_pos)
  3009   apply (cut_tac [2] y="-y" in tan_total_pos, safe)
  3010   apply (rule_tac [3] x = "-x" in exI)
  3011   apply (auto del: exI intro!: exI)
  3012   done
  3013 
  3014 lemma tan_total: "EX! x. -(pi/2) < x & x < (pi/2) & tan x = y"
  3015   apply (cut_tac y = y in lemma_tan_total1, auto)
  3016   apply (cut_tac x = xa and y = y in linorder_less_linear, auto)
  3017   apply (subgoal_tac [2] "\<exists>z. y < z & z < xa & DERIV tan z :> 0")
  3018   apply (subgoal_tac "\<exists>z. xa < z & z < y & DERIV tan z :> 0")
  3019   apply (rule_tac [4] Rolle)
  3020   apply (rule_tac [2] Rolle)
  3021   apply (auto del: exI intro!: DERIV_tan DERIV_isCont exI
  3022               simp add: differentiable_def)
  3023   txt{*Now, simulate TRYALL*}
  3024   apply (rule_tac [!] DERIV_tan asm_rl)
  3025   apply (auto dest!: DERIV_unique [OF _ DERIV_tan]
  3026               simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym])
  3027   done
  3028 
  3029 lemma tan_monotone:
  3030   assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
  3031   shows "tan y < tan x"
  3032 proof -
  3033   have "\<forall>x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse ((cos x')\<^sup>2)"
  3034   proof (rule allI, rule impI)
  3035     fix x' :: real
  3036     assume "y \<le> x' \<and> x' \<le> x"
  3037     hence "-(pi/2) < x'" and "x' < pi/2" using assms by auto
  3038     from cos_gt_zero_pi[OF this]
  3039     have "cos x' \<noteq> 0" by auto
  3040     thus "DERIV tan x' :> inverse ((cos x')\<^sup>2)" by (rule DERIV_tan)
  3041   qed
  3042   from MVT2[OF `y < x` this]
  3043   obtain z where "y < z" and "z < x"
  3044     and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<^sup>2)" by auto
  3045   hence "- (pi / 2) < z" and "z < pi / 2" using assms by auto
  3046   hence "0 < cos z" using cos_gt_zero_pi by auto
  3047   hence inv_pos: "0 < inverse ((cos z)\<^sup>2)" by auto
  3048   have "0 < x - y" using `y < x` by auto
  3049   from mult_pos_pos [OF this inv_pos]
  3050   have "0 < tan x - tan y" unfolding tan_diff by auto
  3051   thus ?thesis by auto
  3052 qed
  3053 
  3054 lemma tan_monotone':
  3055   assumes "- (pi / 2) < y"
  3056     and "y < pi / 2"
  3057     and "- (pi / 2) < x"
  3058     and "x < pi / 2"
  3059   shows "(y < x) = (tan y < tan x)"
  3060 proof
  3061   assume "y < x"
  3062   thus "tan y < tan x"
  3063     using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
  3064 next
  3065   assume "tan y < tan x"
  3066   show "y < x"
  3067   proof (rule ccontr)
  3068     assume "\<not> y < x" hence "x \<le> y" by auto
  3069     hence "tan x \<le> tan y"
  3070     proof (cases "x = y")
  3071       case True thus ?thesis by auto
  3072     next
  3073       case False hence "x < y" using `x \<le> y` by auto
  3074       from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
  3075     qed
  3076     thus False using `tan y < tan x` by auto
  3077   qed
  3078 qed
  3079 
  3080 lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)"
  3081   unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
  3082 
  3083 lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
  3084   by (simp add: tan_def)
  3085 
  3086 lemma tan_periodic_nat[simp]:
  3087   fixes n :: nat
  3088   shows "tan (x + real n * pi) = tan x"
  3089 proof (induct n arbitrary: x)
  3090   case 0
  3091   then show ?case by simp
  3092 next
  3093   case (Suc n)
  3094   have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi"
  3095     unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right by auto
  3096   show ?case unfolding split_pi_off using Suc by auto
  3097 qed
  3098 
  3099 lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
  3100 proof (cases "0 \<le> i")
  3101   case True
  3102   hence i_nat: "real i = real (nat i)" by auto
  3103   show ?thesis unfolding i_nat by auto
  3104 next
  3105   case False
  3106   hence i_nat: "real i = - real (nat (-i))" by auto
  3107   have "tan x = tan (x + real i * pi - real i * pi)"
  3108     by auto
  3109   also have "\<dots> = tan (x + real i * pi)"
  3110     unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
  3111   finally show ?thesis by auto
  3112 qed
  3113 
  3114 lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
  3115   using tan_periodic_int[of _ "numeral n" ] unfolding real_numeral .
  3116 
  3117 subsection {* Inverse Trigonometric Functions *}
  3118 
  3119 definition arcsin :: "real => real"
  3120   where "arcsin y = (THE x. -(pi/2) \<le> x & x \<le> pi/2 & sin x = y)"
  3121 
  3122 definition arccos :: "real => real"
  3123   where "arccos y = (THE x. 0 \<le> x & x \<le> pi & cos x = y)"
  3124 
  3125 definition arctan :: "real => real"
  3126   where "arctan y = (THE x. -(pi/2) < x & x < pi/2 & tan x = y)"
  3127 
  3128 lemma arcsin:
  3129   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow>
  3130     -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2 & sin(arcsin y) = y"
  3131   unfolding arcsin_def by (rule theI' [OF sin_total])
  3132 
  3133 lemma arcsin_pi:
  3134   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi & sin(arcsin y) = y"
  3135   apply (drule (1) arcsin)
  3136   apply (force intro: order_trans)
  3137   done
  3138 
  3139 lemma sin_arcsin [simp]: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> sin(arcsin y) = y"
  3140   by (blast dest: arcsin)
  3141 
  3142 lemma arcsin_bounded: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2"
  3143   by (blast dest: arcsin)
  3144 
  3145 lemma arcsin_lbound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y"
  3146   by (blast dest: arcsin)
  3147 
  3148 lemma arcsin_ubound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin y \<le> pi/2"
  3149   by (blast dest: arcsin)
  3150 
  3151 lemma arcsin_lt_bounded:
  3152      "[| -1 < y; y < 1 |] ==> -(pi/2) < arcsin y & arcsin y < pi/2"
  3153   apply (frule order_less_imp_le)
  3154   apply (frule_tac y = y in order_less_imp_le)
  3155   apply (frule arcsin_bounded)
  3156   apply (safe, simp)
  3157   apply (drule_tac y = "arcsin y" in order_le_imp_less_or_eq)
  3158   apply (drule_tac [2] y = "pi/2" in order_le_imp_less_or_eq, safe)
  3159   apply (drule_tac [!] f = sin in arg_cong, auto)
  3160   done
  3161 
  3162 lemma arcsin_sin: "[|-(pi/2) \<le> x; x \<le> pi/2 |] ==> arcsin(sin x) = x"
  3163   apply (unfold arcsin_def)
  3164   apply (rule the1_equality)
  3165   apply (rule sin_total, auto)
  3166   done
  3167 
  3168 lemma arccos:
  3169      "[| -1 \<le> y; y \<le> 1 |]
  3170       ==> 0 \<le> arccos y & arccos y \<le> pi & cos(arccos y) = y"
  3171   unfolding arccos_def by (rule theI' [OF cos_total])
  3172 
  3173 lemma cos_arccos [simp]: "[| -1 \<le> y; y \<le> 1 |] ==> cos(arccos y) = y"
  3174   by (blast dest: arccos)
  3175 
  3176 lemma arccos_bounded: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y & arccos y \<le> pi"
  3177   by (blast dest: arccos)
  3178 
  3179 lemma arccos_lbound: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y"
  3180   by (blast dest: arccos)
  3181 
  3182 lemma arccos_ubound: "[| -1 \<le> y; y \<le> 1 |] ==> arccos y \<le> pi"
  3183   by (blast dest: arccos)
  3184 
  3185 lemma arccos_lt_bounded:
  3186      "[| -1 < y; y < 1 |]
  3187       ==> 0 < arccos y & arccos y < pi"
  3188   apply (frule order_less_imp_le)
  3189   apply (frule_tac y = y in order_less_imp_le)
  3190   apply (frule arccos_bounded, auto)
  3191   apply (drule_tac y = "arccos y" in order_le_imp_less_or_eq)
  3192   apply (drule_tac [2] y = pi in order_le_imp_less_or_eq, auto)
  3193   apply (drule_tac [!] f = cos in arg_cong, auto)
  3194   done
  3195 
  3196 lemma arccos_cos: "[|0 \<le> x; x \<le> pi |] ==> arccos(cos x) = x"
  3197   apply (simp add: arccos_def)
  3198   apply (auto intro!: the1_equality cos_total)
  3199   done
  3200 
  3201 lemma arccos_cos2: "[|x \<le> 0; -pi \<le> x |] ==> arccos(cos x) = -x"
  3202   apply (simp add: arccos_def)
  3203   apply (auto intro!: the1_equality cos_total)
  3204   done
  3205 
  3206 lemma cos_arcsin: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cos (arcsin x) = sqrt (1 - x\<^sup>2)"
  3207   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3208   apply (rule power2_eq_imp_eq)
  3209   apply (simp add: cos_squared_eq)
  3210   apply (rule cos_ge_zero)
  3211   apply (erule (1) arcsin_lbound)
  3212   apply (erule (1) arcsin_ubound)
  3213   apply simp
  3214   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3215   apply (rule power_mono, simp, simp)
  3216   done
  3217 
  3218 lemma sin_arccos: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> sin (arccos x) = sqrt (1 - x\<^sup>2)"
  3219   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3220   apply (rule power2_eq_imp_eq)
  3221   apply (simp add: sin_squared_eq)
  3222   apply (rule sin_ge_zero)
  3223   apply (erule (1) arccos_lbound)
  3224   apply (erule (1) arccos_ubound)
  3225   apply simp
  3226   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3227   apply (rule power_mono, simp, simp)
  3228   done
  3229 
  3230 lemma arctan [simp]: "- (pi/2) < arctan y  & arctan y < pi/2 & tan (arctan y) = y"
  3231   unfolding arctan_def by (rule theI' [OF tan_total])
  3232 
  3233 lemma tan_arctan: "tan (arctan y) = y"
  3234   by auto
  3235 
  3236 lemma arctan_bounded: "- (pi/2) < arctan y  & arctan y < pi/2"
  3237   by (auto simp only: arctan)
  3238 
  3239 lemma arctan_lbound: "- (pi/2) < arctan y"
  3240   by auto
  3241 
  3242 lemma arctan_ubound: "arctan y < pi/2"
  3243   by (auto simp only: arctan)
  3244 
  3245 lemma arctan_unique:
  3246   assumes "-(pi/2) < x"
  3247     and "x < pi/2"
  3248     and "tan x = y"
  3249   shows "arctan y = x"
  3250   using assms arctan [of y] tan_total [of y] by (fast elim: ex1E)
  3251 
  3252 lemma arctan_tan: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> arctan (tan x) = x"
  3253   by (rule arctan_unique) simp_all
  3254 
  3255 lemma arctan_zero_zero [simp]: "arctan 0 = 0"
  3256   by (rule arctan_unique) simp_all
  3257 
  3258 lemma arctan_minus: "arctan (- x) = - arctan x"
  3259   apply (rule arctan_unique)
  3260   apply (simp only: neg_less_iff_less arctan_ubound)
  3261   apply (metis minus_less_iff arctan_lbound)
  3262   apply simp
  3263   done
  3264 
  3265 lemma cos_arctan_not_zero [simp]: "cos (arctan x) \<noteq> 0"
  3266   by (intro less_imp_neq [symmetric] cos_gt_zero_pi
  3267     arctan_lbound arctan_ubound)
  3268 
  3269 lemma cos_arctan: "cos (arctan x) = 1 / sqrt (1 + x\<^sup>2)"
  3270 proof (rule power2_eq_imp_eq)
  3271   have "0 < 1 + x\<^sup>2" by (simp add: add_pos_nonneg)
  3272   show "0 \<le> 1 / sqrt (1 + x\<^sup>2)" by simp
  3273   show "0 \<le> cos (arctan x)"
  3274     by (intro less_imp_le cos_gt_zero_pi arctan_lbound arctan_ubound)
  3275   have "(cos (arctan x))\<^sup>2 * (1 + (tan (arctan x))\<^sup>2) = 1"
  3276     unfolding tan_def by (simp add: distrib_left power_divide)
  3277   thus "(cos (arctan x))\<^sup>2 = (1 / sqrt (1 + x\<^sup>2))\<^sup>2"
  3278     using `0 < 1 + x\<^sup>2` by (simp add: power_divide eq_divide_eq)
  3279 qed
  3280 
  3281 lemma sin_arctan: "sin (arctan x) = x / sqrt (1 + x\<^sup>2)"
  3282   using add_pos_nonneg [OF zero_less_one zero_le_power2 [of x]]
  3283   using tan_arctan [of x] unfolding tan_def cos_arctan
  3284   by (simp add: eq_divide_eq)
  3285 
  3286 lemma tan_sec: "cos x \<noteq> 0 ==> 1 + (tan x)\<^sup>2 = (inverse (cos x))\<^sup>2"
  3287   apply (rule power_inverse [THEN subst])
  3288   apply (rule_tac c1 = "(cos x)\<^sup>2" in real_mult_right_cancel [THEN iffD1])
  3289   apply (auto dest: field_power_not_zero
  3290           simp add: power_mult_distrib distrib_right power_divide tan_def
  3291                     mult_assoc power_inverse [symmetric])
  3292   done
  3293 
  3294 lemma arctan_less_iff: "arctan x < arctan y \<longleftrightarrow> x < y"
  3295   by (metis tan_monotone' arctan_lbound arctan_ubound tan_arctan)
  3296 
  3297 lemma arctan_le_iff: "arctan x \<le> arctan y \<longleftrightarrow> x \<le> y"
  3298   by (simp only: not_less [symmetric] arctan_less_iff)
  3299 
  3300 lemma arctan_eq_iff: "arctan x = arctan y \<longleftrightarrow> x = y"
  3301   by (simp only: eq_iff [where 'a=real] arctan_le_iff)
  3302 
  3303 lemma zero_less_arctan_iff [simp]: "0 < arctan x \<longleftrightarrow> 0 < x"
  3304   using arctan_less_iff [of 0 x] by simp
  3305 
  3306 lemma arctan_less_zero_iff [simp]: "arctan x < 0 \<longleftrightarrow> x < 0"
  3307   using arctan_less_iff [of x 0] by simp
  3308 
  3309 lemma zero_le_arctan_iff [simp]: "0 \<le> arctan x \<longleftrightarrow> 0 \<le> x"
  3310   using arctan_le_iff [of 0 x] by simp
  3311 
  3312 lemma arctan_le_zero_iff [simp]: "arctan x \<le> 0 \<longleftrightarrow> x \<le> 0"
  3313   using arctan_le_iff [of x 0] by simp
  3314 
  3315 lemma arctan_eq_zero_iff [simp]: "arctan x = 0 \<longleftrightarrow> x = 0"
  3316   using arctan_eq_iff [of x 0] by simp
  3317 
  3318 lemma continuous_on_arcsin': "continuous_on {-1 .. 1} arcsin"
  3319 proof -
  3320   have "continuous_on (sin ` {- pi / 2 .. pi / 2}) arcsin"
  3321     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arcsin_sin)
  3322   also have "sin ` {- pi / 2 .. pi / 2} = {-1 .. 1}"
  3323   proof safe
  3324     fix x :: real
  3325     assume "x \<in> {-1..1}"
  3326     then show "x \<in> sin ` {- pi / 2..pi / 2}"
  3327       using arcsin_lbound arcsin_ubound
  3328       by (intro image_eqI[where x="arcsin x"]) auto
  3329   qed simp
  3330   finally show ?thesis .
  3331 qed
  3332 
  3333 lemma continuous_on_arcsin [continuous_on_intros]:
  3334   "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))"
  3335   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arcsin']]
  3336   by (auto simp: comp_def subset_eq)
  3337 
  3338 lemma isCont_arcsin: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arcsin x"
  3339   using continuous_on_arcsin'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3340   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3341 
  3342 lemma continuous_on_arccos': "continuous_on {-1 .. 1} arccos"
  3343 proof -
  3344   have "continuous_on (cos ` {0 .. pi}) arccos"
  3345     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arccos_cos)
  3346   also have "cos ` {0 .. pi} = {-1 .. 1}"
  3347   proof safe
  3348     fix x :: real
  3349     assume "x \<in> {-1..1}"
  3350     then show "x \<in> cos ` {0..pi}"
  3351       using arccos_lbound arccos_ubound
  3352       by (intro image_eqI[where x="arccos x"]) auto
  3353   qed simp
  3354   finally show ?thesis .
  3355 qed
  3356 
  3357 lemma continuous_on_arccos [continuous_on_intros]:
  3358   "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))"
  3359   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arccos']]
  3360   by (auto simp: comp_def subset_eq)
  3361 
  3362 lemma isCont_arccos: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arccos x"
  3363   using continuous_on_arccos'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3364   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3365 
  3366 lemma isCont_arctan: "isCont arctan x"
  3367   apply (rule arctan_lbound [of x, THEN dense, THEN exE], clarify)
  3368   apply (rule arctan_ubound [of x, THEN dense, THEN exE], clarify)
  3369   apply (subgoal_tac "isCont arctan (tan (arctan x))", simp)
  3370   apply (erule (1) isCont_inverse_function2 [where f=tan])
  3371   apply (metis arctan_tan order_le_less_trans order_less_le_trans)
  3372   apply (metis cos_gt_zero_pi isCont_tan order_less_le_trans less_le)
  3373   done
  3374 
  3375 lemma tendsto_arctan [tendsto_intros]: "(f ---> x) F \<Longrightarrow> ((\<lambda>x. arctan (f x)) ---> arctan x) F"
  3376   by (rule isCont_tendsto_compose [OF isCont_arctan])
  3377 
  3378 lemma continuous_arctan [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arctan (f x))"
  3379   unfolding continuous_def by (rule tendsto_arctan)
  3380 
  3381 lemma continuous_on_arctan [continuous_on_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. arctan (f x))"
  3382   unfolding continuous_on_def by (auto intro: tendsto_arctan)
  3383 
  3384 lemma DERIV_arcsin:
  3385   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arcsin x :> inverse (sqrt (1 - x\<^sup>2))"
  3386   apply (rule DERIV_inverse_function [where f=sin and a="-1" and b="1"])
  3387   apply (rule DERIV_cong [OF DERIV_sin])
  3388   apply (simp add: cos_arcsin)
  3389   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3390   apply (rule power_strict_mono, simp, simp, simp)
  3391   apply assumption
  3392   apply assumption
  3393   apply simp
  3394   apply (erule (1) isCont_arcsin)
  3395   done
  3396 
  3397 lemma DERIV_arccos:
  3398   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arccos x :> inverse (- sqrt (1 - x\<^sup>2))"
  3399   apply (rule DERIV_inverse_function [where f=cos and a="-1" and b="1"])
  3400   apply (rule DERIV_cong [OF DERIV_cos])
  3401   apply (simp add: sin_arccos)
  3402   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3403   apply (rule power_strict_mono, simp, simp, simp)
  3404   apply assumption
  3405   apply assumption
  3406   apply simp
  3407   apply (erule (1) isCont_arccos)
  3408   done
  3409 
  3410 lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
  3411   apply (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
  3412   apply (rule DERIV_cong [OF DERIV_tan])
  3413   apply (rule cos_arctan_not_zero)
  3414   apply (simp add: power_inverse tan_sec [symmetric])
  3415   apply (subgoal_tac "0 < 1 + x\<^sup>2", simp)
  3416   apply (simp add: add_pos_nonneg)
  3417   apply (simp, simp, simp, rule isCont_arctan)
  3418   done
  3419 
  3420 declare
  3421   DERIV_arcsin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3422   DERIV_arccos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3423   DERIV_arctan[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3424 
  3425 lemma filterlim_tan_at_right: "filterlim tan at_bot (at_right (- pi/2))"
  3426   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])
  3427      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3428            intro!: tan_monotone exI[of _ "pi/2"])
  3429 
  3430 lemma filterlim_tan_at_left: "filterlim tan at_top (at_left (pi/2))"
  3431   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])
  3432      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3433            intro!: tan_monotone exI[of _ "pi/2"])
  3434 
  3435 lemma tendsto_arctan_at_top: "(arctan ---> (pi/2)) at_top"
  3436 proof (rule tendstoI)
  3437   fix e :: real
  3438   assume "0 < e"
  3439   def y \<equiv> "pi/2 - min (pi/2) e"
  3440   then have y: "0 \<le> y" "y < pi/2" "pi/2 \<le> e + y"
  3441     using `0 < e` by auto
  3442 
  3443   show "eventually (\<lambda>x. dist (arctan x) (pi / 2) < e) at_top"
  3444   proof (intro eventually_at_top_dense[THEN iffD2] exI allI impI)
  3445     fix x
  3446     assume "tan y < x"
  3447     then have "arctan (tan y) < arctan x"
  3448       by (simp add: arctan_less_iff)
  3449     with y have "y < arctan x"
  3450       by (subst (asm) arctan_tan) simp_all
  3451     with arctan_ubound[of x, arith] y `0 < e`
  3452     show "dist (arctan x) (pi / 2) < e"
  3453       by (simp add: dist_real_def)
  3454   qed
  3455 qed
  3456 
  3457 lemma tendsto_arctan_at_bot: "(arctan ---> - (pi/2)) at_bot"
  3458   unfolding filterlim_at_bot_mirror arctan_minus
  3459   by (intro tendsto_minus tendsto_arctan_at_top)
  3460 
  3461 
  3462 subsection {* More Theorems about Sin and Cos *}
  3463 
  3464 lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
  3465 proof -
  3466   let ?c = "cos (pi / 4)" and ?s = "sin (pi / 4)"
  3467   have nonneg: "0 \<le> ?c"
  3468     by (simp add: cos_ge_zero)
  3469   have "0 = cos (pi / 4 + pi / 4)"
  3470     by simp
  3471   also have "cos (pi / 4 + pi / 4) = ?c\<^sup>2 - ?s\<^sup>2"
  3472     by (simp only: cos_add power2_eq_square)
  3473   also have "\<dots> = 2 * ?c\<^sup>2 - 1"
  3474     by (simp add: sin_squared_eq)
  3475   finally have "?c\<^sup>2 = (sqrt 2 / 2)\<^sup>2"
  3476     by (simp add: power_divide)
  3477   thus ?thesis
  3478     using nonneg by (rule power2_eq_imp_eq) simp
  3479 qed
  3480 
  3481 lemma cos_30: "cos (pi / 6) = sqrt 3 / 2"
  3482 proof -
  3483   let ?c = "cos (pi / 6)" and ?s = "sin (pi / 6)"
  3484   have pos_c: "0 < ?c"
  3485     by (rule cos_gt_zero, simp, simp)
  3486   have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
  3487     by simp
  3488   also have "\<dots> = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
  3489     by (simp only: cos_add sin_add)
  3490   also have "\<dots> = ?c * (?c\<^sup>2 - 3 * ?s\<^sup>2)"
  3491     by (simp add: algebra_simps power2_eq_square)
  3492   finally have "?c\<^sup>2 = (sqrt 3 / 2)\<^sup>2"
  3493     using pos_c by (simp add: sin_squared_eq power_divide)
  3494   thus ?thesis
  3495     using pos_c [THEN order_less_imp_le]
  3496     by (rule power2_eq_imp_eq) simp
  3497 qed
  3498 
  3499 lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
  3500   by (simp add: sin_cos_eq cos_45)
  3501 
  3502 lemma sin_60: "sin (pi / 3) = sqrt 3 / 2"
  3503   by (simp add: sin_cos_eq cos_30)
  3504 
  3505 lemma cos_60: "cos (pi / 3) = 1 / 2"
  3506   apply (rule power2_eq_imp_eq)
  3507   apply (simp add: cos_squared_eq sin_60 power_divide)
  3508   apply (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
  3509   done
  3510 
  3511 lemma sin_30: "sin (pi / 6) = 1 / 2"
  3512   by (simp add: sin_cos_eq cos_60)
  3513 
  3514 lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
  3515   unfolding tan_def by (simp add: sin_30 cos_30)
  3516 
  3517 lemma tan_45: "tan (pi / 4) = 1"
  3518   unfolding tan_def by (simp add: sin_45 cos_45)
  3519 
  3520 lemma tan_60: "tan (pi / 3) = sqrt 3"
  3521   unfolding tan_def by (simp add: sin_60 cos_60)
  3522 
  3523 lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi / 2) = (-1) ^ n"
  3524 proof -
  3525   have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
  3526     by (auto simp add: algebra_simps sin_add)
  3527   thus ?thesis
  3528     by (simp add: real_of_nat_Suc distrib_right add_divide_distrib
  3529                   mult_commute [of pi])
  3530 qed
  3531 
  3532 lemma cos_2npi [simp]: "cos (2 * real (n::nat) * pi) = 1"
  3533   by (simp add: cos_double mult_assoc power_add [symmetric] numeral_2_eq_2)
  3534 
  3535 lemma cos_3over2_pi [simp]: "cos (3 / 2 * pi) = 0"
  3536   apply (subgoal_tac "cos (pi + pi/2) = 0", simp)
  3537   apply (subst cos_add, simp)
  3538   done
  3539 
  3540 lemma sin_2npi [simp]: "sin (2 * real (n::nat) * pi) = 0"
  3541   by (auto simp add: mult_assoc)
  3542 
  3543 lemma sin_3over2_pi [simp]: "sin (3 / 2 * pi) = - 1"
  3544   apply (subgoal_tac "sin (pi + pi/2) = - 1", simp)
  3545   apply (subst sin_add, simp)
  3546   done
  3547 
  3548 lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
  3549   apply (simp only: cos_add sin_add real_of_nat_Suc distrib_right distrib_left add_divide_distrib)
  3550   apply auto
  3551   done
  3552 
  3553 lemma DERIV_cos_add [simp]: "DERIV (\<lambda>x. cos (x + k)) xa :> - sin (xa + k)"
  3554   by (auto intro!: DERIV_intros)
  3555 
  3556 lemma sin_zero_abs_cos_one: "sin x = 0 ==> \<bar>cos x\<bar> = 1"
  3557   by (auto simp add: sin_zero_iff even_mult_two_ex)
  3558 
  3559 lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
  3560   using sin_cos_squared_add3 [where x = x] by auto
  3561 
  3562 
  3563 subsection {* Machins formula *}
  3564 
  3565 lemma arctan_one: "arctan 1 = pi / 4"
  3566   by (rule arctan_unique, simp_all add: tan_45 m2pi_less_pi)
  3567 
  3568 lemma tan_total_pi4:
  3569   assumes "\<bar>x\<bar> < 1"
  3570   shows "\<exists>z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
  3571 proof
  3572   show "- (pi / 4) < arctan x \<and> arctan x < pi / 4 \<and> tan (arctan x) = x"
  3573     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3574     unfolding arctan_less_iff using assms by auto
  3575 qed
  3576 
  3577 lemma arctan_add:
  3578   assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
  3579   shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
  3580 proof (rule arctan_unique [symmetric])
  3581   have "- (pi / 4) \<le> arctan x" and "- (pi / 4) < arctan y"
  3582     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3583     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3584   from add_le_less_mono [OF this]
  3585   show 1: "- (pi / 2) < arctan x + arctan y" by simp
  3586   have "arctan x \<le> pi / 4" and "arctan y < pi / 4"
  3587     unfolding arctan_one [symmetric]
  3588     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3589   from add_le_less_mono [OF this]
  3590   show 2: "arctan x + arctan y < pi / 2" by simp
  3591   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
  3592     using cos_gt_zero_pi [OF 1 2] by (simp add: tan_add)
  3593 qed
  3594 
  3595 theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
  3596 proof -
  3597   have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
  3598   from arctan_add[OF less_imp_le[OF this] this]
  3599   have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
  3600   moreover
  3601   have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
  3602   from arctan_add[OF less_imp_le[OF this] this]
  3603   have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
  3604   moreover
  3605   have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
  3606   from arctan_add[OF this]
  3607   have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
  3608   ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
  3609   thus ?thesis unfolding arctan_one by algebra
  3610 qed
  3611 
  3612 
  3613 subsection {* Introducing the arcus tangens power series *}
  3614 
  3615 lemma monoseq_arctan_series:
  3616   fixes x :: real
  3617   assumes "\<bar>x\<bar> \<le> 1"
  3618   shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
  3619 proof (cases "x = 0")
  3620   case True
  3621   thus ?thesis unfolding monoseq_def One_nat_def by auto
  3622 next
  3623   case False
  3624   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3625   show "monoseq ?a"
  3626   proof -
  3627     {
  3628       fix n
  3629       fix x :: real
  3630       assume "0 \<le> x" and "x \<le> 1"
  3631       have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le>
  3632         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
  3633       proof (rule mult_mono)
  3634         show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))"
  3635           by (rule frac_le) simp_all
  3636         show "0 \<le> 1 / real (Suc (n * 2))"
  3637           by auto
  3638         show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)"
  3639           by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
  3640         show "0 \<le> x ^ Suc (Suc n * 2)"
  3641           by (rule zero_le_power) (simp add: `0 \<le> x`)
  3642       qed
  3643     } note mono = this
  3644 
  3645     show ?thesis
  3646     proof (cases "0 \<le> x")
  3647       case True from mono[OF this `x \<le> 1`, THEN allI]
  3648       show ?thesis unfolding Suc_eq_plus1[symmetric]
  3649         by (rule mono_SucI2)
  3650     next
  3651       case False
  3652       hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
  3653       from mono[OF this]
  3654       have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge>
  3655         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
  3656       thus ?thesis unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI1[OF allI])
  3657     qed
  3658   qed
  3659 qed
  3660 
  3661 lemma zeroseq_arctan_series:
  3662   fixes x :: real
  3663   assumes "\<bar>x\<bar> \<le> 1"
  3664   shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
  3665 proof (cases "x = 0")
  3666   case True
  3667   thus ?thesis
  3668     unfolding One_nat_def by (auto simp add: tendsto_const)
  3669 next
  3670   case False
  3671   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3672   show "?a ----> 0"
  3673   proof (cases "\<bar>x\<bar> < 1")
  3674     case True
  3675     hence "norm x < 1" by auto
  3676     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
  3677     have "(\<lambda>n. 1 / real (n + 1) * x ^ (n + 1)) ----> 0"
  3678       unfolding inverse_eq_divide Suc_eq_plus1 by simp
  3679     then show ?thesis using pos2 by (rule LIMSEQ_linear)
  3680   next
  3681     case False
  3682     hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3683     hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x"
  3684       unfolding One_nat_def by auto
  3685     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] tendsto_const[of x]]
  3686     show ?thesis unfolding n_eq Suc_eq_plus1 by auto
  3687   qed
  3688 qed
  3689 
  3690 lemma summable_arctan_series:
  3691   fixes x :: real and n :: nat
  3692   assumes "\<bar>x\<bar> \<le> 1"
  3693   shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3694   (is "summable (?c x)")
  3695   by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
  3696 
  3697 lemma less_one_imp_sqr_less_one:
  3698   fixes x :: real
  3699   assumes "\<bar>x\<bar> < 1"
  3700   shows "x\<^sup>2 < 1"
  3701 proof -
  3702   from mult_left_mono[OF less_imp_le[OF `\<bar>x\<bar> < 1`] abs_ge_zero[of x]]
  3703   have "\<bar>x\<^sup>2\<bar> < 1" using `\<bar>x\<bar> < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
  3704   thus ?thesis using zero_le_power2 by auto
  3705 qed
  3706 
  3707 lemma DERIV_arctan_series:
  3708   assumes "\<bar> x \<bar> < 1"
  3709   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))"
  3710   (is "DERIV ?arctan _ :> ?Int")
  3711 proof -
  3712   let ?f = "\<lambda>n. if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
  3713 
  3714   have n_even: "\<And>n :: nat. even n \<Longrightarrow> 2 * (n div 2) = n"
  3715     by presburger
  3716   then have if_eq: "\<And>n x'. ?f n * real (Suc n) * x'^n =
  3717     (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)"
  3718     by auto
  3719 
  3720   {
  3721     fix x :: real
  3722     assume "\<bar>x\<bar> < 1"
  3723     hence "x\<^sup>2 < 1" by (rule less_one_imp_sqr_less_one)
  3724     have "summable (\<lambda> n. -1 ^ n * (x\<^sup>2) ^n)"
  3725       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`])
  3726     hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
  3727   } note summable_Integral = this
  3728 
  3729   {
  3730     fix f :: "nat \<Rightarrow> real"
  3731     have "\<And>x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3732     proof
  3733       fix x :: real
  3734       assume "f sums x"
  3735       from sums_if[OF sums_zero this]
  3736       show "(\<lambda>n. if even n then f (n div 2) else 0) sums x"
  3737         by auto
  3738     next
  3739       fix x :: real
  3740       assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3741       from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
  3742       show "f sums x" unfolding sums_def by auto
  3743     qed
  3744     hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
  3745   } note sums_even = this
  3746 
  3747   have Int_eq: "(\<Sum>n. ?f n * real (Suc n) * x^n) = ?Int"
  3748     unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
  3749     by auto
  3750 
  3751   {
  3752     fix x :: real
  3753     have if_eq': "\<And>n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
  3754       (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
  3755       using n_even by auto
  3756     have idx_eq: "\<And>n. n * 2 + 1 = Suc (2 * n)" by auto
  3757     have "(\<Sum>n. ?f n * x^(Suc n)) = ?arctan x"
  3758       unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
  3759       by auto
  3760   } note arctan_eq = this
  3761 
  3762   have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
  3763   proof (rule DERIV_power_series')
  3764     show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
  3765     {
  3766       fix x' :: real
  3767       assume x'_bounds: "x' \<in> {- 1 <..< 1}"
  3768       hence "\<bar>x'\<bar> < 1" by auto
  3769 
  3770       let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
  3771       show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
  3772         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`])
  3773     }
  3774   qed auto
  3775   thus ?thesis unfolding Int_eq arctan_eq .
  3776 qed
  3777 
  3778 lemma arctan_series:
  3779   assumes "\<bar> x \<bar> \<le> 1"
  3780   shows "arctan x = (\<Sum>k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3781   (is "_ = suminf (\<lambda> n. ?c x n)")
  3782 proof -
  3783   let ?c' = "\<lambda>x n. (-1)^n * x^(n*2)"
  3784 
  3785   {
  3786     fix r x :: real
  3787     assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
  3788     have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
  3789     from DERIV_arctan_series[OF this] have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
  3790   } note DERIV_arctan_suminf = this
  3791 
  3792   {
  3793     fix x :: real
  3794     assume "\<bar>x\<bar> \<le> 1"
  3795     note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]]
  3796   } note arctan_series_borders = this
  3797 
  3798   {
  3799     fix x :: real
  3800     assume "\<bar>x\<bar> < 1"
  3801     have "arctan x = (\<Sum>k. ?c x k)"
  3802     proof -
  3803       obtain r where "\<bar>x\<bar> < r" and "r < 1"
  3804         using dense[OF `\<bar>x\<bar> < 1`] by blast
  3805       hence "0 < r" and "-r < x" and "x < r" by auto
  3806 
  3807       have suminf_eq_arctan_bounded: "\<And>x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow>
  3808         suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3809       proof -
  3810         fix x a b
  3811         assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
  3812         hence "\<bar>x\<bar> < r" by auto
  3813         show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3814         proof (rule DERIV_isconst2[of "a" "b"])
  3815           show "a < b" and "a \<le> x" and "x \<le> b"
  3816             using `a < b` `a \<le> x` `x \<le> b` by auto
  3817           have "\<forall>x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3818           proof (rule allI, rule impI)
  3819             fix x
  3820             assume "-r < x \<and> x < r"
  3821             hence "\<bar>x\<bar> < r" by auto
  3822             hence "\<bar>x\<bar> < 1" using `r < 1` by auto
  3823             have "\<bar> - (x\<^sup>2) \<bar> < 1"
  3824               using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
  3825             hence "(\<lambda> n. (- (x\<^sup>2)) ^ n) sums (1 / (1 - (- (x\<^sup>2))))"
  3826               unfolding real_norm_def[symmetric] by (rule geometric_sums)
  3827             hence "(?c' x) sums (1 / (1 - (- (x\<^sup>2))))"
  3828               unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
  3829             hence suminf_c'_eq_geom: "inverse (1 + x\<^sup>2) = suminf (?c' x)"
  3830               using sums_unique unfolding inverse_eq_divide by auto
  3831             have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x\<^sup>2))"
  3832               unfolding suminf_c'_eq_geom
  3833               by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
  3834             from DERIV_add_minus[OF this DERIV_arctan]
  3835             show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3836               by auto
  3837           qed
  3838           hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3839             using `-r < a` `b < r` by auto
  3840           thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3841             using `\<bar>x\<bar> < r` by auto
  3842           show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y"
  3843             using DERIV_in_rball DERIV_isCont by auto
  3844         qed
  3845       qed
  3846 
  3847       have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
  3848         unfolding Suc_eq_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero
  3849         by auto
  3850 
  3851       have "suminf (?c x) - arctan x = 0"
  3852       proof (cases "x = 0")
  3853         case True
  3854         thus ?thesis using suminf_arctan_zero by auto
  3855       next
  3856         case False
  3857         hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
  3858         have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
  3859           by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric])
  3860             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3861         moreover
  3862         have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
  3863           by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"])
  3864             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3865         ultimately
  3866         show ?thesis using suminf_arctan_zero by auto
  3867       qed
  3868       thus ?thesis by auto
  3869     qed
  3870   } note when_less_one = this
  3871 
  3872   show "arctan x = suminf (\<lambda> n. ?c x n)"
  3873   proof (cases "\<bar>x\<bar> < 1")
  3874     case True
  3875     thus ?thesis by (rule when_less_one)
  3876   next
  3877     case False
  3878     hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3879     let ?a = "\<lambda>x n. \<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
  3880     let ?diff = "\<lambda> x n. \<bar> arctan x - (\<Sum> i = 0..<n. ?c x i)\<bar>"
  3881     {
  3882       fix n :: nat
  3883       have "0 < (1 :: real)" by auto
  3884       moreover
  3885       {
  3886         fix x :: real
  3887         assume "0 < x" and "x < 1"
  3888         hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
  3889         from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)"
  3890           by auto
  3891         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]
  3892         have "0 < 1 / real (n*2+1) * x^(n*2+1)"
  3893           by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
  3894         hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)"
  3895           by (rule abs_of_pos)
  3896         have "?diff x n \<le> ?a x n"
  3897         proof (cases "even n")
  3898           case True
  3899           hence sgn_pos: "(-1)^n = (1::real)" by auto
  3900           from `even n` obtain m where "2 * m = n"
  3901             unfolding even_mult_two_ex by auto
  3902           from bounds[of m, unfolded this atLeastAtMost_iff]
  3903           have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n + 1. (?c x i)) - (\<Sum>i = 0..<n. (?c x i))"
  3904             by auto
  3905           also have "\<dots> = ?c x n" unfolding One_nat_def by auto
  3906           also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
  3907           finally show ?thesis .
  3908         next
  3909           case False
  3910           hence sgn_neg: "(-1)^n = (-1::real)" by auto
  3911           from `odd n` obtain m where m_def: "2 * m + 1 = n"
  3912             unfolding odd_Suc_mult_two_ex by auto
  3913           hence m_plus: "2 * (m + 1) = n + 1" by auto
  3914           from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
  3915           have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n. (?c x i)) - (\<Sum>i = 0..<n+1. (?c x i))"
  3916             by auto
  3917           also have "\<dots> = - ?c x n" unfolding One_nat_def by auto
  3918           also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
  3919           finally show ?thesis .
  3920         qed
  3921         hence "0 \<le> ?a x n - ?diff x n" by auto
  3922       }
  3923       hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
  3924       moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
  3925         unfolding diff_conv_add_uminus divide_inverse
  3926         by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan
  3927           isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum
  3928           simp del: add_uminus_conv_diff)
  3929       ultimately have "0 \<le> ?a 1 n - ?diff 1 n"
  3930         by (rule LIM_less_bound)
  3931       hence "?diff 1 n \<le> ?a 1 n" by auto
  3932     }
  3933     have "?a 1 ----> 0"
  3934       unfolding tendsto_rabs_zero_iff power_one divide_inverse One_nat_def
  3935       by (auto intro!: tendsto_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
  3936     have "?diff 1 ----> 0"
  3937     proof (rule LIMSEQ_I)
  3938       fix r :: real
  3939       assume "0 < r"
  3940       obtain N :: nat where N_I: "\<And>n. N \<le> n \<Longrightarrow> ?a 1 n < r"
  3941         using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
  3942       {
  3943         fix n
  3944         assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
  3945         have "norm (?diff 1 n - 0) < r" by auto
  3946       }
  3947       thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
  3948     qed
  3949     from this [unfolded tendsto_rabs_zero_iff, THEN tendsto_add [OF _ tendsto_const], of "- arctan 1", THEN tendsto_minus]
  3950     have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
  3951     hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
  3952 
  3953     show ?thesis
  3954     proof (cases "x = 1")
  3955       case True
  3956       then show ?thesis by (simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
  3957     next
  3958       case False
  3959       hence "x = -1" using `\<bar>x\<bar> = 1` by auto
  3960 
  3961       have "- (pi / 2) < 0" using pi_gt_zero by auto
  3962       have "- (2 * pi) < 0" using pi_gt_zero by auto
  3963 
  3964       have c_minus_minus: "\<And>i. ?c (- 1) i = - ?c 1 i"
  3965         unfolding One_nat_def by auto
  3966 
  3967       have "arctan (- 1) = arctan (tan (-(pi / 4)))"
  3968         unfolding tan_45 tan_minus ..
  3969       also have "\<dots> = - (pi / 4)"
  3970         by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
  3971       also have "\<dots> = - (arctan (tan (pi / 4)))"
  3972         unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
  3973       also have "\<dots> = - (arctan 1)"
  3974         unfolding tan_45 ..
  3975       also have "\<dots> = - (\<Sum> i. ?c 1 i)"
  3976         using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
  3977       also have "\<dots> = (\<Sum> i. ?c (- 1) i)"
  3978         using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]]
  3979         unfolding c_minus_minus by auto
  3980       finally show ?thesis using `x = -1` by auto
  3981     qed
  3982   qed
  3983 qed
  3984 
  3985 lemma arctan_half:
  3986   fixes x :: real
  3987   shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x\<^sup>2)))"
  3988 proof -
  3989   obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x"
  3990     using tan_total by blast
  3991   hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2"
  3992     by auto
  3993 
  3994   have divide_nonzero_divide: "\<And>A B C :: real. C \<noteq> 0 \<Longrightarrow> A / B = (A / C) / (B / C)"
  3995     by auto
  3996 
  3997   have "0 < cos y" using cos_gt_zero_pi[OF low high] .
  3998   hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y)\<^sup>2) = cos y"
  3999     by auto
  4000 
  4001   have "1 + (tan y)\<^sup>2 = 1 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  4002     unfolding tan_def power_divide ..
  4003   also have "\<dots> = (cos y)\<^sup>2 / (cos y)\<^sup>2 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  4004     using `cos y \<noteq> 0` by auto
  4005   also have "\<dots> = 1 / (cos y)\<^sup>2"
  4006     unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
  4007   finally have "1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2" .
  4008 
  4009   have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)"
  4010     unfolding tan_def divide_nonzero_divide[OF `cos y \<noteq> 0`, symmetric] ..
  4011   also have "\<dots> = tan y / (1 + 1 / cos y)"
  4012     using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
  4013   also have "\<dots> = tan y / (1 + 1 / sqrt ((cos y)\<^sup>2))"
  4014     unfolding cos_sqrt ..
  4015   also have "\<dots> = tan y / (1 + sqrt (1 / (cos y)\<^sup>2))"
  4016     unfolding real_sqrt_divide by auto
  4017   finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)\<^sup>2))"
  4018     unfolding `1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2` .
  4019 
  4020   have "arctan x = y"
  4021     using arctan_tan low high y_eq by auto
  4022   also have "\<dots> = 2 * (arctan (tan (y/2)))"
  4023     using arctan_tan[OF low2 high2] by auto
  4024   also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))"
  4025     unfolding tan_half by auto
  4026   finally show ?thesis
  4027     unfolding eq `tan y = x` .
  4028 qed
  4029 
  4030 lemma arctan_monotone: "x < y \<Longrightarrow> arctan x < arctan y"
  4031   by (simp only: arctan_less_iff)
  4032 
  4033 lemma arctan_monotone': "x \<le> y \<Longrightarrow> arctan x \<le> arctan y"
  4034   by (simp only: arctan_le_iff)
  4035 
  4036 lemma arctan_inverse:
  4037   assumes "x \<noteq> 0"
  4038   shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
  4039 proof (rule arctan_unique)
  4040   show "- (pi / 2) < sgn x * pi / 2 - arctan x"
  4041     using arctan_bounded [of x] assms
  4042     unfolding sgn_real_def
  4043     apply (auto simp add: algebra_simps)
  4044     apply (drule zero_less_arctan_iff [THEN iffD2])
  4045     apply arith
  4046     done
  4047   show "sgn x * pi / 2 - arctan x < pi / 2"
  4048     using arctan_bounded [of "- x"] assms
  4049     unfolding sgn_real_def arctan_minus
  4050     by auto
  4051   show "tan (sgn x * pi / 2 - arctan x) = 1 / x"
  4052     unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
  4053     unfolding sgn_real_def
  4054     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
  4055 qed
  4056 
  4057 theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
  4058 proof -
  4059   have "pi / 4 = arctan 1" using arctan_one by auto
  4060   also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
  4061   finally show ?thesis by auto
  4062 qed
  4063 
  4064 
  4065 subsection {* Existence of Polar Coordinates *}
  4066 
  4067 lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
  4068   apply (rule power2_le_imp_le [OF _ zero_le_one])
  4069   apply (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
  4070   done
  4071 
  4072 lemma cos_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> cos (arccos y) = y"
  4073   by (simp add: abs_le_iff)
  4074 
  4075 lemma sin_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> sin (arccos y) = sqrt (1 - y\<^sup>2)"
  4076   by (simp add: sin_arccos abs_le_iff)
  4077 
  4078 lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
  4079 
  4080 lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
  4081 
  4082 lemma polar_ex1: "0 < y \<Longrightarrow> \<exists>r a. x = r * cos a & y = r * sin a"
  4083   apply (rule_tac x = "sqrt (x\<^sup>2 + y\<^sup>2)" in exI)
  4084   apply (rule_tac x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))" in exI)
  4085   apply (simp add: cos_arccos_lemma1)
  4086   apply (simp add: sin_arccos_lemma1)
  4087   apply (simp add: power_divide)
  4088   apply (simp add: real_sqrt_mult [symmetric])
  4089   apply (simp add: right_diff_distrib)
  4090   done
  4091 
  4092 lemma polar_ex2: "y < 0 ==> \<exists>r a. x = r * cos a & y = r * sin a"
  4093   using polar_ex1 [where x=x and y="-y"]
  4094   apply simp
  4095   apply clarify
  4096   apply (metis cos_minus minus_minus minus_mult_right sin_minus)
  4097   done
  4098 
  4099 lemma polar_Ex: "\<exists>r a. x = r * cos a & y = r * sin a"
  4100   apply (rule_tac x=0 and y=y in linorder_cases)
  4101   apply (erule polar_ex1)
  4102   apply (rule_tac x=x in exI, rule_tac x=0 in exI, simp)
  4103   apply (erule polar_ex2)
  4104   done
  4105 
  4106 end