src/HOL/Transcendental.thy
author haftmann
Tue Nov 19 10:05:53 2013 +0100 (2013-11-19)
changeset 54489 03ff4d1e6784
parent 54230 b1d955791529
child 54573 07864001495d
permissions -rw-r--r--
eliminiated neg_numeral in favour of - (numeral _)
     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_one: "0 < x \<Longrightarrow> x powr 1 = x"
  2024   using powr_realpow [of x 1] by simp
  2025 
  2026 lemma powr_numeral: "0 < x \<Longrightarrow> x powr numeral n = x ^ numeral n"
  2027   by (fact powr_realpow_numeral)
  2028 
  2029 lemma powr_neg_one: "0 < x \<Longrightarrow> x powr - 1 = 1 / x"
  2030   using powr_int [of x "- 1"] by simp
  2031 
  2032 lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr - numeral n = 1 / x ^ numeral n"
  2033   using powr_int [of x "- numeral n"] by simp
  2034 
  2035 lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
  2036   by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
  2037 
  2038 lemma ln_powr: "0 < x ==> 0 < y ==> ln(x powr y) = y * ln x"
  2039   unfolding powr_def by simp
  2040 
  2041 lemma log_powr: "0 < x ==> 0 \<le> y ==> log b (x powr y) = y * log b x"
  2042   apply (cases "y = 0")
  2043   apply force
  2044   apply (auto simp add: log_def ln_powr field_simps)
  2045   done
  2046 
  2047 lemma log_nat_power: "0 < x ==> log b (x^n) = real n * log b x"
  2048   apply (subst powr_realpow [symmetric])
  2049   apply (auto simp add: log_powr)
  2050   done
  2051 
  2052 lemma ln_bound: "1 <= x ==> ln x <= x"
  2053   apply (subgoal_tac "ln(1 + (x - 1)) <= x - 1")
  2054   apply simp
  2055   apply (rule ln_add_one_self_le_self, simp)
  2056   done
  2057 
  2058 lemma powr_mono: "a <= b ==> 1 <= x ==> x powr a <= x powr b"
  2059   apply (cases "x = 1", simp)
  2060   apply (cases "a = b", simp)
  2061   apply (rule order_less_imp_le)
  2062   apply (rule powr_less_mono, auto)
  2063   done
  2064 
  2065 lemma ge_one_powr_ge_zero: "1 <= x ==> 0 <= a ==> 1 <= x powr a"
  2066   apply (subst powr_zero_eq_one [THEN sym])
  2067   apply (rule powr_mono, assumption+)
  2068   done
  2069 
  2070 lemma powr_less_mono2: "0 < a ==> 0 < x ==> x < y ==> x powr a < y powr a"
  2071   apply (unfold powr_def)
  2072   apply (rule exp_less_mono)
  2073   apply (rule mult_strict_left_mono)
  2074   apply (subst ln_less_cancel_iff, assumption)
  2075   apply (rule order_less_trans)
  2076   prefer 2
  2077   apply assumption+
  2078   done
  2079 
  2080 lemma powr_less_mono2_neg: "a < 0 ==> 0 < x ==> x < y ==> y powr a < x powr a"
  2081   apply (unfold powr_def)
  2082   apply (rule exp_less_mono)
  2083   apply (rule mult_strict_left_mono_neg)
  2084   apply (subst ln_less_cancel_iff)
  2085   apply assumption
  2086   apply (rule order_less_trans)
  2087   prefer 2
  2088   apply assumption+
  2089   done
  2090 
  2091 lemma powr_mono2: "0 <= a ==> 0 < x ==> x <= y ==> x powr a <= y powr a"
  2092   apply (case_tac "a = 0", simp)
  2093   apply (case_tac "x = y", simp)
  2094   apply (rule order_less_imp_le)
  2095   apply (rule powr_less_mono2, auto)
  2096   done
  2097 
  2098 lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
  2099   unfolding powr_def exp_inj_iff by simp
  2100 
  2101 lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
  2102   apply (rule mult_imp_le_div_pos)
  2103   apply (assumption)
  2104   apply (subst mult_commute)
  2105   apply (subst ln_powr [THEN sym])
  2106   apply auto
  2107   apply (rule ln_bound)
  2108   apply (erule ge_one_powr_ge_zero)
  2109   apply (erule order_less_imp_le)
  2110   done
  2111 
  2112 lemma ln_powr_bound2:
  2113   assumes "1 < x" and "0 < a"
  2114   shows "(ln x) powr a <= (a powr a) * x"
  2115 proof -
  2116   from assms have "ln x <= (x powr (1 / a)) / (1 / a)"
  2117     apply (intro ln_powr_bound)
  2118     apply (erule order_less_imp_le)
  2119     apply (rule divide_pos_pos)
  2120     apply simp_all
  2121     done
  2122   also have "... = a * (x powr (1 / a))"
  2123     by simp
  2124   finally have "(ln x) powr a <= (a * (x powr (1 / a))) powr a"
  2125     apply (intro powr_mono2)
  2126     apply (rule order_less_imp_le, rule assms)
  2127     apply (rule ln_gt_zero)
  2128     apply (rule assms)
  2129     apply assumption
  2130     done
  2131   also have "... = (a powr a) * ((x powr (1 / a)) powr a)"
  2132     apply (rule powr_mult)
  2133     apply (rule assms)
  2134     apply (rule powr_gt_zero)
  2135     done
  2136   also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
  2137     by (rule powr_powr)
  2138   also have "... = x"
  2139     apply simp
  2140     apply (subgoal_tac "a ~= 0")
  2141     using assms apply auto
  2142     done
  2143   finally show ?thesis .
  2144 qed
  2145 
  2146 lemma tendsto_powr [tendsto_intros]:
  2147   "\<lbrakk>(f ---> a) F; (g ---> b) F; 0 < a\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x powr g x) ---> a powr b) F"
  2148   unfolding powr_def by (intro tendsto_intros)
  2149 
  2150 lemma continuous_powr:
  2151   assumes "continuous F f"
  2152     and "continuous F g"
  2153     and "0 < f (Lim F (\<lambda>x. x))"
  2154   shows "continuous F (\<lambda>x. (f x) powr (g x))"
  2155   using assms unfolding continuous_def by (rule tendsto_powr)
  2156 
  2157 lemma continuous_at_within_powr[continuous_intros]:
  2158   assumes "continuous (at a within s) f"
  2159     and "continuous (at a within s) g"
  2160     and "0 < f a"
  2161   shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
  2162   using assms unfolding continuous_within by (rule tendsto_powr)
  2163 
  2164 lemma isCont_powr[continuous_intros, simp]:
  2165   assumes "isCont f a" "isCont g a" "0 < f a"
  2166   shows "isCont (\<lambda>x. (f x) powr g x) a"
  2167   using assms unfolding continuous_at by (rule tendsto_powr)
  2168 
  2169 lemma continuous_on_powr[continuous_on_intros]:
  2170   assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. 0 < f x"
  2171   shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
  2172   using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
  2173 
  2174 (* FIXME: generalize by replacing d by with g x and g ---> d? *)
  2175 lemma tendsto_zero_powrI:
  2176   assumes "eventually (\<lambda>x. 0 < f x ) F" and "(f ---> 0) F"
  2177     and "0 < d"
  2178   shows "((\<lambda>x. f x powr d) ---> 0) F"
  2179 proof (rule tendstoI)
  2180   fix e :: real assume "0 < e"
  2181   def Z \<equiv> "e powr (1 / d)"
  2182   with `0 < e` have "0 < Z" by simp
  2183   with assms have "eventually (\<lambda>x. 0 < f x \<and> dist (f x) 0 < Z) F"
  2184     by (intro eventually_conj tendstoD)
  2185   moreover
  2186   from assms have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> x powr d < Z powr d"
  2187     by (intro powr_less_mono2) (auto simp: dist_real_def)
  2188   with assms `0 < e` have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> dist (x powr d) 0 < e"
  2189     unfolding dist_real_def Z_def by (auto simp: powr_powr)
  2190   ultimately
  2191   show "eventually (\<lambda>x. dist (f x powr d) 0 < e) F" by (rule eventually_elim1)
  2192 qed
  2193 
  2194 lemma tendsto_neg_powr:
  2195   assumes "s < 0"
  2196     and "LIM x F. f x :> at_top"
  2197   shows "((\<lambda>x. f x powr s) ---> 0) F"
  2198 proof (rule tendstoI)
  2199   fix e :: real assume "0 < e"
  2200   def Z \<equiv> "e powr (1 / s)"
  2201   from assms have "eventually (\<lambda>x. Z < f x) F"
  2202     by (simp add: filterlim_at_top_dense)
  2203   moreover
  2204   from assms have "\<And>x. Z < x \<Longrightarrow> x powr s < Z powr s"
  2205     by (auto simp: Z_def intro!: powr_less_mono2_neg)
  2206   with assms `0 < e` have "\<And>x. Z < x \<Longrightarrow> dist (x powr s) 0 < e"
  2207     by (simp add: powr_powr Z_def dist_real_def)
  2208   ultimately
  2209   show "eventually (\<lambda>x. dist (f x powr s) 0 < e) F" by (rule eventually_elim1)
  2210 qed
  2211 
  2212 subsection {* Sine and Cosine *}
  2213 
  2214 definition sin_coeff :: "nat \<Rightarrow> real" where
  2215   "sin_coeff = (\<lambda>n. if even n then 0 else -1 ^ ((n - Suc 0) div 2) / real (fact n))"
  2216 
  2217 definition cos_coeff :: "nat \<Rightarrow> real" where
  2218   "cos_coeff = (\<lambda>n. if even n then (-1 ^ (n div 2)) / real (fact n) else 0)"
  2219 
  2220 definition sin :: "real \<Rightarrow> real"
  2221   where "sin = (\<lambda>x. \<Sum>n. sin_coeff n * x ^ n)"
  2222 
  2223 definition cos :: "real \<Rightarrow> real"
  2224   where "cos = (\<lambda>x. \<Sum>n. cos_coeff n * x ^ n)"
  2225 
  2226 lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
  2227   unfolding sin_coeff_def by simp
  2228 
  2229 lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
  2230   unfolding cos_coeff_def by simp
  2231 
  2232 lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
  2233   unfolding cos_coeff_def sin_coeff_def
  2234   by (simp del: mult_Suc)
  2235 
  2236 lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
  2237   unfolding cos_coeff_def sin_coeff_def
  2238   by (simp del: mult_Suc, auto simp add: odd_Suc_mult_two_ex)
  2239 
  2240 lemma summable_sin: "summable (\<lambda>n. sin_coeff n * x ^ n)"
  2241   unfolding sin_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 summable_cos: "summable (\<lambda>n. cos_coeff n * x ^ n)"
  2247   unfolding cos_coeff_def
  2248   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
  2249   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  2250   done
  2251 
  2252 lemma sin_converges: "(\<lambda>n. sin_coeff n * x ^ n) sums sin(x)"
  2253   unfolding sin_def by (rule summable_sin [THEN summable_sums])
  2254 
  2255 lemma cos_converges: "(\<lambda>n. cos_coeff n * x ^ n) sums cos(x)"
  2256   unfolding cos_def by (rule summable_cos [THEN summable_sums])
  2257 
  2258 lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
  2259   by (simp add: diffs_def sin_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2260 
  2261 lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
  2262   by (simp add: diffs_def cos_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2263 
  2264 text{*Now at last we can get the derivatives of exp, sin and cos*}
  2265 
  2266 lemma DERIV_sin [simp]: "DERIV sin x :> cos(x)"
  2267   unfolding sin_def cos_def
  2268   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2269   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff
  2270     summable_minus summable_sin summable_cos)
  2271   done
  2272 
  2273 declare DERIV_sin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2274 
  2275 lemma DERIV_cos [simp]: "DERIV cos x :> -sin(x)"
  2276   unfolding cos_def sin_def
  2277   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2278   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff diffs_minus
  2279     summable_minus summable_sin summable_cos suminf_minus)
  2280   done
  2281 
  2282 declare DERIV_cos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2283 
  2284 lemma isCont_sin: "isCont sin x"
  2285   by (rule DERIV_sin [THEN DERIV_isCont])
  2286 
  2287 lemma isCont_cos: "isCont cos x"
  2288   by (rule DERIV_cos [THEN DERIV_isCont])
  2289 
  2290 lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
  2291   by (rule isCont_o2 [OF _ isCont_sin])
  2292 
  2293 lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
  2294   by (rule isCont_o2 [OF _ isCont_cos])
  2295 
  2296 lemma tendsto_sin [tendsto_intros]:
  2297   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) ---> sin a) F"
  2298   by (rule isCont_tendsto_compose [OF isCont_sin])
  2299 
  2300 lemma tendsto_cos [tendsto_intros]:
  2301   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) ---> cos a) F"
  2302   by (rule isCont_tendsto_compose [OF isCont_cos])
  2303 
  2304 lemma continuous_sin [continuous_intros]:
  2305   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
  2306   unfolding continuous_def by (rule tendsto_sin)
  2307 
  2308 lemma continuous_on_sin [continuous_on_intros]:
  2309   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
  2310   unfolding continuous_on_def by (auto intro: tendsto_sin)
  2311 
  2312 lemma continuous_cos [continuous_intros]:
  2313   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
  2314   unfolding continuous_def by (rule tendsto_cos)
  2315 
  2316 lemma continuous_on_cos [continuous_on_intros]:
  2317   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
  2318   unfolding continuous_on_def by (auto intro: tendsto_cos)
  2319 
  2320 subsection {* Properties of Sine and Cosine *}
  2321 
  2322 lemma sin_zero [simp]: "sin 0 = 0"
  2323   unfolding sin_def sin_coeff_def by (simp add: powser_zero)
  2324 
  2325 lemma cos_zero [simp]: "cos 0 = 1"
  2326   unfolding cos_def cos_coeff_def by (simp add: powser_zero)
  2327 
  2328 lemma sin_cos_squared_add [simp]: "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
  2329 proof -
  2330   have "\<forall>x. DERIV (\<lambda>x. (sin x)\<^sup>2 + (cos x)\<^sup>2) x :> 0"
  2331     by (auto intro!: DERIV_intros)
  2332   hence "(sin x)\<^sup>2 + (cos x)\<^sup>2 = (sin 0)\<^sup>2 + (cos 0)\<^sup>2"
  2333     by (rule DERIV_isconst_all)
  2334   thus "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1" by simp
  2335 qed
  2336 
  2337 lemma sin_cos_squared_add2 [simp]: "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
  2338   by (subst add_commute, rule sin_cos_squared_add)
  2339 
  2340 lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
  2341   using sin_cos_squared_add2 [unfolded power2_eq_square] .
  2342 
  2343 lemma sin_squared_eq: "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
  2344   unfolding eq_diff_eq by (rule sin_cos_squared_add)
  2345 
  2346 lemma cos_squared_eq: "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
  2347   unfolding eq_diff_eq by (rule sin_cos_squared_add2)
  2348 
  2349 lemma abs_sin_le_one [simp]: "\<bar>sin x\<bar> \<le> 1"
  2350   by (rule power2_le_imp_le, simp_all add: sin_squared_eq)
  2351 
  2352 lemma sin_ge_minus_one [simp]: "-1 \<le> sin x"
  2353   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2354 
  2355 lemma sin_le_one [simp]: "sin x \<le> 1"
  2356   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2357 
  2358 lemma abs_cos_le_one [simp]: "\<bar>cos x\<bar> \<le> 1"
  2359   by (rule power2_le_imp_le, simp_all add: cos_squared_eq)
  2360 
  2361 lemma cos_ge_minus_one [simp]: "-1 \<le> cos x"
  2362   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2363 
  2364 lemma cos_le_one [simp]: "cos x \<le> 1"
  2365   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2366 
  2367 lemma DERIV_fun_pow: "DERIV g x :> m ==>
  2368       DERIV (\<lambda>x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
  2369   by (auto intro!: DERIV_intros)
  2370 
  2371 lemma DERIV_fun_exp:
  2372      "DERIV g x :> m ==> DERIV (\<lambda>x. exp(g x)) x :> exp(g x) * m"
  2373   by (auto intro!: DERIV_intros)
  2374 
  2375 lemma DERIV_fun_sin:
  2376      "DERIV g x :> m ==> DERIV (\<lambda>x. sin(g x)) x :> cos(g x) * m"
  2377   by (auto intro!: DERIV_intros)
  2378 
  2379 lemma DERIV_fun_cos:
  2380      "DERIV g x :> m ==> DERIV (\<lambda>x. cos(g x)) x :> -sin(g x) * m"
  2381   by (auto intro!: DERIV_intros)
  2382 
  2383 lemma sin_cos_add_lemma:
  2384   "(sin (x + y) - (sin x * cos y + cos x * sin y))\<^sup>2 +
  2385     (cos (x + y) - (cos x * cos y - sin x * sin y))\<^sup>2 = 0"
  2386   (is "?f x = 0")
  2387 proof -
  2388   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2389     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2390   hence "?f x = ?f 0"
  2391     by (rule DERIV_isconst_all)
  2392   thus ?thesis by simp
  2393 qed
  2394 
  2395 lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
  2396   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2397 
  2398 lemma cos_add: "cos (x + y) = cos x * cos y - sin x * sin y"
  2399   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2400 
  2401 lemma sin_cos_minus_lemma:
  2402   "(sin(-x) + sin(x))\<^sup>2 + (cos(-x) - cos(x))\<^sup>2 = 0" (is "?f x = 0")
  2403 proof -
  2404   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2405     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2406   hence "?f x = ?f 0"
  2407     by (rule DERIV_isconst_all)
  2408   thus ?thesis by simp
  2409 qed
  2410 
  2411 lemma sin_minus [simp]: "sin (-x) = -sin(x)"
  2412   using sin_cos_minus_lemma [where x=x] by simp
  2413 
  2414 lemma cos_minus [simp]: "cos (-x) = cos(x)"
  2415   using sin_cos_minus_lemma [where x=x] by simp
  2416 
  2417 lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
  2418   using sin_add [of x "- y"] by simp
  2419 
  2420 lemma sin_diff2: "sin (x - y) = cos y * sin x - sin y * cos x"
  2421   by (simp add: sin_diff mult_commute)
  2422 
  2423 lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
  2424   using cos_add [of x "- y"] by simp
  2425 
  2426 lemma cos_diff2: "cos (x - y) = cos y * cos x + sin y * sin x"
  2427   by (simp add: cos_diff mult_commute)
  2428 
  2429 lemma sin_double [simp]: "sin(2 * x) = 2* sin x * cos x"
  2430   using sin_add [where x=x and y=x] by simp
  2431 
  2432 lemma cos_double: "cos(2* x) = ((cos x)\<^sup>2) - ((sin x)\<^sup>2)"
  2433   using cos_add [where x=x and y=x]
  2434   by (simp add: power2_eq_square)
  2435 
  2436 
  2437 subsection {* The Constant Pi *}
  2438 
  2439 definition pi :: real
  2440   where "pi = 2 * (THE x. 0 \<le> (x::real) & x \<le> 2 & cos x = 0)"
  2441 
  2442 text{*Show that there's a least positive @{term x} with @{term "cos(x) = 0"};
  2443    hence define pi.*}
  2444 
  2445 lemma sin_paired:
  2446   "(\<lambda>n. -1 ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) sums  sin x"
  2447 proof -
  2448   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
  2449     by (rule sin_converges [THEN sums_group], simp)
  2450   thus ?thesis unfolding One_nat_def sin_coeff_def by (simp add: mult_ac)
  2451 qed
  2452 
  2453 lemma sin_gt_zero:
  2454   assumes "0 < x" and "x < 2"
  2455   shows "0 < sin x"
  2456 proof -
  2457   let ?f = "\<lambda>n. \<Sum>k = n*2..<n*2+2. -1 ^ k / real (fact (2*k+1)) * x^(2*k+1)"
  2458   have pos: "\<forall>n. 0 < ?f n"
  2459   proof
  2460     fix n :: nat
  2461     let ?k2 = "real (Suc (Suc (4 * n)))"
  2462     let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
  2463     have "x * x < ?k2 * ?k3"
  2464       using assms by (intro mult_strict_mono', simp_all)
  2465     hence "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
  2466       by (intro mult_strict_right_mono zero_less_power `0 < x`)
  2467     thus "0 < ?f n"
  2468       by (simp del: mult_Suc,
  2469         simp add: less_divide_eq mult_pos_pos field_simps del: mult_Suc)
  2470   qed
  2471   have sums: "?f sums sin x"
  2472     by (rule sin_paired [THEN sums_group], simp)
  2473   show "0 < sin x"
  2474     unfolding sums_unique [OF sums]
  2475     using sums_summable [OF sums] pos
  2476     by (rule suminf_gt_zero)
  2477 qed
  2478 
  2479 lemma cos_double_less_one: "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
  2480   using sin_gt_zero [where x = x] by (auto simp add: cos_squared_eq cos_double)
  2481 
  2482 lemma cos_paired: "(\<lambda>n. -1 ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
  2483 proof -
  2484   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
  2485     by (rule cos_converges [THEN sums_group], simp)
  2486   thus ?thesis unfolding cos_coeff_def by (simp add: mult_ac)
  2487 qed
  2488 
  2489 lemma real_mult_inverse_cancel:
  2490      "[|(0::real) < x; 0 < x1; x1 * y < x * u |]
  2491       ==> inverse x * y < inverse x1 * u"
  2492   apply (rule_tac c=x in mult_less_imp_less_left)
  2493   apply (auto simp add: mult_assoc [symmetric])
  2494   apply (simp (no_asm) add: mult_ac)
  2495   apply (rule_tac c=x1 in mult_less_imp_less_right)
  2496   apply (auto simp add: mult_ac)
  2497   done
  2498 
  2499 lemma real_mult_inverse_cancel2:
  2500      "[|(0::real) < x;0 < x1; x1 * y < x * u |] ==> y * inverse x < u * inverse x1"
  2501   by (auto dest: real_mult_inverse_cancel simp add: mult_ac)
  2502 
  2503 lemmas realpow_num_eq_if = power_eq_if
  2504 
  2505 lemma cos_two_less_zero [simp]:
  2506   "cos 2 < 0"
  2507 proof -
  2508   note fact_Suc [simp del]
  2509   from cos_paired
  2510   have "(\<lambda>n. - (-1 ^ n / real (fact (2 * n)) * 2 ^ (2 * n))) sums - cos 2"
  2511     by (rule sums_minus)
  2512   then have *: "(\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n)))) sums - cos 2"
  2513     by simp
  2514   then have **: "summable (\<lambda>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2515     by (rule sums_summable)
  2516   have "0 < (\<Sum>n = 0..<Suc (Suc (Suc 0)). - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2517     by (simp add: fact_num_eq_if_nat realpow_num_eq_if)
  2518   moreover have "(\<Sum>n = 0..<Suc (Suc (Suc 0)). - (-1 ^ n  * 2 ^ (2 * n) / real (fact (2 * n))))
  2519     < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2520   proof -
  2521     { fix d
  2522       have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
  2523        < real (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))) *
  2524            fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
  2525         by (simp only: real_of_nat_mult) (auto intro!: mult_strict_mono fact_less_mono_nat)
  2526       then have "4 * real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))
  2527         < real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))))))"
  2528         by (simp only: fact_Suc [of "Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))"])
  2529       then have "4 * inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))))
  2530         < inverse (real (fact (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))))"
  2531         by (simp add: inverse_eq_divide less_divide_eq)
  2532     }
  2533     note *** = this
  2534     have [simp]: "\<And>x y::real. 0 < x - y \<longleftrightarrow> y < x" by arith
  2535     from ** show ?thesis by (rule sumr_pos_lt_pair)
  2536       (simp add: divide_inverse mult_assoc [symmetric] ***)
  2537   qed
  2538   ultimately have "0 < (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2539     by (rule order_less_trans)
  2540   moreover from * have "- cos 2 = (\<Sum>n. - (-1 ^ n * 2 ^ (2 * n) / real (fact (2 * n))))"
  2541     by (rule sums_unique)
  2542   ultimately have "0 < - cos 2" by simp
  2543   then show ?thesis by simp
  2544 qed
  2545 
  2546 lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
  2547 lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]
  2548 
  2549 lemma cos_is_zero: "EX! x. 0 \<le> x & x \<le> 2 \<and> cos x = 0"
  2550 proof (rule ex_ex1I)
  2551   show "\<exists>x. 0 \<le> x & x \<le> 2 & cos x = 0"
  2552     by (rule IVT2, simp_all)
  2553 next
  2554   fix x y
  2555   assume x: "0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
  2556   assume y: "0 \<le> y \<and> y \<le> 2 \<and> cos y = 0"
  2557   have [simp]: "\<forall>x. cos differentiable x"
  2558     unfolding differentiable_def by (auto intro: DERIV_cos)
  2559   from x y show "x = y"
  2560     apply (cut_tac less_linear [of x y], auto)
  2561     apply (drule_tac f = cos in Rolle)
  2562     apply (drule_tac [5] f = cos in Rolle)
  2563     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2564     apply (metis order_less_le_trans less_le sin_gt_zero)
  2565     apply (metis order_less_le_trans less_le sin_gt_zero)
  2566     done
  2567 qed
  2568 
  2569 lemma pi_half: "pi/2 = (THE x. 0 \<le> x & x \<le> 2 & cos x = 0)"
  2570   by (simp add: pi_def)
  2571 
  2572 lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
  2573   by (simp add: pi_half cos_is_zero [THEN theI'])
  2574 
  2575 lemma pi_half_gt_zero [simp]: "0 < pi / 2"
  2576   apply (rule order_le_neq_trans)
  2577   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2578   apply (rule notI, drule arg_cong [where f=cos], simp)
  2579   done
  2580 
  2581 lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
  2582 lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]
  2583 
  2584 lemma pi_half_less_two [simp]: "pi / 2 < 2"
  2585   apply (rule order_le_neq_trans)
  2586   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2587   apply (rule notI, drule arg_cong [where f=cos], simp)
  2588   done
  2589 
  2590 lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
  2591 lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]
  2592 
  2593 lemma pi_gt_zero [simp]: "0 < pi"
  2594   using pi_half_gt_zero by simp
  2595 
  2596 lemma pi_ge_zero [simp]: "0 \<le> pi"
  2597   by (rule pi_gt_zero [THEN order_less_imp_le])
  2598 
  2599 lemma pi_neq_zero [simp]: "pi \<noteq> 0"
  2600   by (rule pi_gt_zero [THEN less_imp_neq, symmetric])
  2601 
  2602 lemma pi_not_less_zero [simp]: "\<not> pi < 0"
  2603   by (simp add: linorder_not_less)
  2604 
  2605 lemma minus_pi_half_less_zero: "-(pi/2) < 0"
  2606   by simp
  2607 
  2608 lemma m2pi_less_pi: "- (2 * pi) < pi"
  2609   by simp
  2610 
  2611 lemma sin_pi_half [simp]: "sin(pi/2) = 1"
  2612   using sin_cos_squared_add2 [where x = "pi/2"]
  2613   using sin_gt_zero [OF pi_half_gt_zero pi_half_less_two]
  2614   by (simp add: power2_eq_1_iff)
  2615 
  2616 lemma cos_pi [simp]: "cos pi = -1"
  2617   using cos_add [where x = "pi/2" and y = "pi/2"] by simp
  2618 
  2619 lemma sin_pi [simp]: "sin pi = 0"
  2620   using sin_add [where x = "pi/2" and y = "pi/2"] by simp
  2621 
  2622 lemma sin_cos_eq: "sin x = cos (pi/2 - x)"
  2623   by (simp add: cos_diff)
  2624 
  2625 lemma minus_sin_cos_eq: "-sin x = cos (x + pi/2)"
  2626   by (simp add: cos_add)
  2627 
  2628 lemma cos_sin_eq: "cos x = sin (pi/2 - x)"
  2629   by (simp add: sin_diff)
  2630 
  2631 lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
  2632   by (simp add: sin_add)
  2633 
  2634 lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
  2635   by (simp add: sin_add)
  2636 
  2637 lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
  2638   by (simp add: cos_add)
  2639 
  2640 lemma sin_periodic [simp]: "sin (x + 2*pi) = sin x"
  2641   by (simp add: sin_add cos_double)
  2642 
  2643 lemma cos_periodic [simp]: "cos (x + 2*pi) = cos x"
  2644   by (simp add: cos_add cos_double)
  2645 
  2646 lemma cos_npi [simp]: "cos (real n * pi) = -1 ^ n"
  2647   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2648 
  2649 lemma cos_npi2 [simp]: "cos (pi * real n) = -1 ^ n"
  2650 proof -
  2651   have "cos (pi * real n) = cos (real n * pi)" by (simp only: mult_commute)
  2652   also have "... = -1 ^ n" by (rule cos_npi)
  2653   finally show ?thesis .
  2654 qed
  2655 
  2656 lemma sin_npi [simp]: "sin (real (n::nat) * pi) = 0"
  2657   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2658 
  2659 lemma sin_npi2 [simp]: "sin (pi * real (n::nat)) = 0"
  2660   by (simp add: mult_commute [of pi])
  2661 
  2662 lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
  2663   by (simp add: cos_double)
  2664 
  2665 lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  2666   by simp
  2667 
  2668 lemma sin_gt_zero2: "[| 0 < x; x < pi/2 |] ==> 0 < sin x"
  2669   apply (rule sin_gt_zero, assumption)
  2670   apply (rule order_less_trans, assumption)
  2671   apply (rule pi_half_less_two)
  2672   done
  2673 
  2674 lemma sin_less_zero:
  2675   assumes "- pi/2 < x" and "x < 0"
  2676   shows "sin x < 0"
  2677 proof -
  2678   have "0 < sin (- x)" using assms by (simp only: sin_gt_zero2)
  2679   thus ?thesis by simp
  2680 qed
  2681 
  2682 lemma pi_less_4: "pi < 4"
  2683   using pi_half_less_two by auto
  2684 
  2685 lemma cos_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < cos x"
  2686   apply (cut_tac pi_less_4)
  2687   apply (cut_tac f = cos and a = 0 and b = x and y = 0 in IVT2_objl, safe, simp_all)
  2688   apply (cut_tac cos_is_zero, safe)
  2689   apply (rename_tac y z)
  2690   apply (drule_tac x = y in spec)
  2691   apply (drule_tac x = "pi/2" in spec, simp)
  2692   done
  2693 
  2694 lemma cos_gt_zero_pi: "[| -(pi/2) < x; x < pi/2 |] ==> 0 < cos x"
  2695   apply (rule_tac x = x and y = 0 in linorder_cases)
  2696   apply (rule cos_minus [THEN subst])
  2697   apply (rule cos_gt_zero)
  2698   apply (auto intro: cos_gt_zero)
  2699   done
  2700 
  2701 lemma cos_ge_zero: "[| -(pi/2) \<le> x; x \<le> pi/2 |] ==> 0 \<le> cos x"
  2702   apply (auto simp add: order_le_less cos_gt_zero_pi)
  2703   apply (subgoal_tac "x = pi/2", auto)
  2704   done
  2705 
  2706 lemma sin_gt_zero_pi: "[| 0 < x; x < pi  |] ==> 0 < sin x"
  2707   by (simp add: sin_cos_eq cos_gt_zero_pi)
  2708 
  2709 lemma pi_ge_two: "2 \<le> pi"
  2710 proof (rule ccontr)
  2711   assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
  2712   have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
  2713   proof (cases "2 < 2 * pi")
  2714     case True with dense[OF `pi < 2`] show ?thesis by auto
  2715   next
  2716     case False have "pi < 2 * pi" by auto
  2717     from dense[OF this] and False show ?thesis by auto
  2718   qed
  2719   then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
  2720   hence "0 < sin y" using sin_gt_zero by auto
  2721   moreover
  2722   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
  2723   ultimately show False by auto
  2724 qed
  2725 
  2726 lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
  2727   by (auto simp add: order_le_less sin_gt_zero_pi)
  2728 
  2729 text {* FIXME: This proof is almost identical to lemma @{text cos_is_zero}.
  2730   It should be possible to factor out some of the common parts. *}
  2731 
  2732 lemma cos_total: "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. 0 \<le> x & x \<le> pi & (cos x = y)"
  2733 proof (rule ex_ex1I)
  2734   assume y: "-1 \<le> y" "y \<le> 1"
  2735   show "\<exists>x. 0 \<le> x & x \<le> pi & cos x = y"
  2736     by (rule IVT2, simp_all add: y)
  2737 next
  2738   fix a b
  2739   assume a: "0 \<le> a \<and> a \<le> pi \<and> cos a = y"
  2740   assume b: "0 \<le> b \<and> b \<le> pi \<and> cos b = y"
  2741   have [simp]: "\<forall>x. cos differentiable x"
  2742     unfolding differentiable_def by (auto intro: DERIV_cos)
  2743   from a b show "a = b"
  2744     apply (cut_tac less_linear [of a b], auto)
  2745     apply (drule_tac f = cos in Rolle)
  2746     apply (drule_tac [5] f = cos in Rolle)
  2747     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2748     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2749     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2750     done
  2751 qed
  2752 
  2753 lemma sin_total:
  2754      "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. -(pi/2) \<le> x & x \<le> pi/2 & (sin x = y)"
  2755 apply (rule ccontr)
  2756 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))")
  2757 apply (erule contrapos_np)
  2758 apply simp
  2759 apply (cut_tac y="-y" in cos_total, simp) apply simp
  2760 apply (erule ex1E)
  2761 apply (rule_tac a = "x - (pi/2)" in ex1I)
  2762 apply (simp (no_asm) add: add_assoc)
  2763 apply (rotate_tac 3)
  2764 apply (drule_tac x = "xa + pi/2" in spec, safe, simp_all add: cos_add)
  2765 done
  2766 
  2767 lemma reals_Archimedean4:
  2768      "[| 0 < y; 0 \<le> x |] ==> \<exists>n. real n * y \<le> x & x < real (Suc n) * y"
  2769 apply (auto dest!: reals_Archimedean3)
  2770 apply (drule_tac x = x in spec, clarify)
  2771 apply (subgoal_tac "x < real(LEAST m::nat. x < real m * y) * y")
  2772  prefer 2 apply (erule LeastI)
  2773 apply (case_tac "LEAST m::nat. x < real m * y", simp)
  2774 apply (subgoal_tac "~ x < real nat * y")
  2775  prefer 2 apply (rule not_less_Least, simp, force)
  2776 done
  2777 
  2778 (* Pre Isabelle99-2 proof was simpler- numerals arithmetic
  2779    now causes some unwanted re-arrangements of literals!   *)
  2780 lemma cos_zero_lemma:
  2781      "[| 0 \<le> x; cos x = 0 |] ==>
  2782       \<exists>n::nat. ~even n & x = real n * (pi/2)"
  2783 apply (drule pi_gt_zero [THEN reals_Archimedean4], safe)
  2784 apply (subgoal_tac "0 \<le> x - real n * pi &
  2785                     (x - real n * pi) \<le> pi & (cos (x - real n * pi) = 0) ")
  2786 apply (auto simp add: algebra_simps real_of_nat_Suc)
  2787  prefer 2 apply (simp add: cos_diff)
  2788 apply (simp add: cos_diff)
  2789 apply (subgoal_tac "EX! x. 0 \<le> x & x \<le> pi & cos x = 0")
  2790 apply (rule_tac [2] cos_total, safe)
  2791 apply (drule_tac x = "x - real n * pi" in spec)
  2792 apply (drule_tac x = "pi/2" in spec)
  2793 apply (simp add: cos_diff)
  2794 apply (rule_tac x = "Suc (2 * n)" in exI)
  2795 apply (simp add: real_of_nat_Suc algebra_simps, auto)
  2796 done
  2797 
  2798 lemma sin_zero_lemma:
  2799      "[| 0 \<le> x; sin x = 0 |] ==>
  2800       \<exists>n::nat. even n & x = real n * (pi/2)"
  2801 apply (subgoal_tac "\<exists>n::nat. ~ even n & x + pi/2 = real n * (pi/2) ")
  2802  apply (clarify, rule_tac x = "n - 1" in exI)
  2803  apply (force simp add: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2804 apply (rule cos_zero_lemma)
  2805 apply (simp_all add: cos_add)
  2806 done
  2807 
  2808 
  2809 lemma cos_zero_iff:
  2810      "(cos x = 0) =
  2811       ((\<exists>n::nat. ~even n & (x = real n * (pi/2))) |
  2812        (\<exists>n::nat. ~even n & (x = -(real n * (pi/2)))))"
  2813 apply (rule iffI)
  2814 apply (cut_tac linorder_linear [of 0 x], safe)
  2815 apply (drule cos_zero_lemma, assumption+)
  2816 apply (cut_tac x="-x" in cos_zero_lemma, simp, simp)
  2817 apply (force simp add: minus_equation_iff [of x])
  2818 apply (auto simp only: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2819 apply (auto simp add: cos_diff cos_add)
  2820 done
  2821 
  2822 (* ditto: but to a lesser extent *)
  2823 lemma sin_zero_iff:
  2824      "(sin x = 0) =
  2825       ((\<exists>n::nat. even n & (x = real n * (pi/2))) |
  2826        (\<exists>n::nat. even n & (x = -(real n * (pi/2)))))"
  2827 apply (rule iffI)
  2828 apply (cut_tac linorder_linear [of 0 x], safe)
  2829 apply (drule sin_zero_lemma, assumption+)
  2830 apply (cut_tac x="-x" in sin_zero_lemma, simp, simp, safe)
  2831 apply (force simp add: minus_equation_iff [of x])
  2832 apply (auto simp add: even_mult_two_ex)
  2833 done
  2834 
  2835 lemma cos_monotone_0_pi:
  2836   assumes "0 \<le> y" and "y < x" and "x \<le> pi"
  2837   shows "cos x < cos y"
  2838 proof -
  2839   have "- (x - y) < 0" using assms by auto
  2840 
  2841   from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
  2842   obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z"
  2843     by auto
  2844   hence "0 < z" and "z < pi" using assms by auto
  2845   hence "0 < sin z" using sin_gt_zero_pi by auto
  2846   hence "cos x - cos y < 0"
  2847     unfolding cos_diff minus_mult_commute[symmetric]
  2848     using `- (x - y) < 0` by (rule mult_pos_neg2)
  2849   thus ?thesis by auto
  2850 qed
  2851 
  2852 lemma cos_monotone_0_pi':
  2853   assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi"
  2854   shows "cos x \<le> cos y"
  2855 proof (cases "y < x")
  2856   case True
  2857   show ?thesis
  2858     using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
  2859 next
  2860   case False
  2861   hence "y = x" using `y \<le> x` by auto
  2862   thus ?thesis by auto
  2863 qed
  2864 
  2865 lemma cos_monotone_minus_pi_0:
  2866   assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
  2867   shows "cos y < cos x"
  2868 proof -
  2869   have "0 \<le> -x" and "-x < -y" and "-y \<le> pi"
  2870     using assms by auto
  2871   from cos_monotone_0_pi[OF this] show ?thesis
  2872     unfolding cos_minus .
  2873 qed
  2874 
  2875 lemma cos_monotone_minus_pi_0':
  2876   assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0"
  2877   shows "cos y \<le> cos x"
  2878 proof (cases "y < x")
  2879   case True
  2880   show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`]
  2881     by auto
  2882 next
  2883   case False
  2884   hence "y = x" using `y \<le> x` by auto
  2885   thus ?thesis by auto
  2886 qed
  2887 
  2888 lemma sin_monotone_2pi':
  2889   assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2"
  2890   shows "sin y \<le> sin x"
  2891 proof -
  2892   have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi"
  2893     using pi_ge_two and assms by auto
  2894   from cos_monotone_0_pi'[OF this] show ?thesis
  2895     unfolding minus_sin_cos_eq[symmetric] by auto
  2896 qed
  2897 
  2898 
  2899 subsection {* Tangent *}
  2900 
  2901 definition tan :: "real \<Rightarrow> real"
  2902   where "tan = (\<lambda>x. sin x / cos x)"
  2903 
  2904 lemma tan_zero [simp]: "tan 0 = 0"
  2905   by (simp add: tan_def)
  2906 
  2907 lemma tan_pi [simp]: "tan pi = 0"
  2908   by (simp add: tan_def)
  2909 
  2910 lemma tan_npi [simp]: "tan (real (n::nat) * pi) = 0"
  2911   by (simp add: tan_def)
  2912 
  2913 lemma tan_minus [simp]: "tan (-x) = - tan x"
  2914   by (simp add: tan_def)
  2915 
  2916 lemma tan_periodic [simp]: "tan (x + 2*pi) = tan x"
  2917   by (simp add: tan_def)
  2918 
  2919 lemma lemma_tan_add1:
  2920   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> 1 - tan x * tan y = cos (x + y)/(cos x * cos y)"
  2921   by (simp add: tan_def cos_add field_simps)
  2922 
  2923 lemma add_tan_eq:
  2924   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> tan x + tan y = sin(x + y)/(cos x * cos y)"
  2925   by (simp add: tan_def sin_add field_simps)
  2926 
  2927 lemma tan_add:
  2928      "[| cos x \<noteq> 0; cos y \<noteq> 0; cos (x + y) \<noteq> 0 |]
  2929       ==> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
  2930   by (simp add: add_tan_eq lemma_tan_add1, simp add: tan_def)
  2931 
  2932 lemma tan_double:
  2933      "[| cos x \<noteq> 0; cos (2 * x) \<noteq> 0 |]
  2934       ==> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
  2935   using tan_add [of x x] by (simp add: power2_eq_square)
  2936 
  2937 lemma tan_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < tan x"
  2938   by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
  2939 
  2940 lemma tan_less_zero:
  2941   assumes lb: "- pi/2 < x" and "x < 0"
  2942   shows "tan x < 0"
  2943 proof -
  2944   have "0 < tan (- x)" using assms by (simp only: tan_gt_zero)
  2945   thus ?thesis by simp
  2946 qed
  2947 
  2948 lemma tan_half: "tan x = sin (2 * x) / (cos (2 * x) + 1)"
  2949   unfolding tan_def sin_double cos_double sin_squared_eq
  2950   by (simp add: power2_eq_square)
  2951 
  2952 lemma DERIV_tan [simp]: "cos x \<noteq> 0 \<Longrightarrow> DERIV tan x :> inverse ((cos x)\<^sup>2)"
  2953   unfolding tan_def
  2954   by (auto intro!: DERIV_intros, simp add: divide_inverse power2_eq_square)
  2955 
  2956 lemma isCont_tan: "cos x \<noteq> 0 \<Longrightarrow> isCont tan x"
  2957   by (rule DERIV_tan [THEN DERIV_isCont])
  2958 
  2959 lemma isCont_tan' [simp]:
  2960   "\<lbrakk>isCont f a; cos (f a) \<noteq> 0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. tan (f x)) a"
  2961   by (rule isCont_o2 [OF _ isCont_tan])
  2962 
  2963 lemma tendsto_tan [tendsto_intros]:
  2964   "\<lbrakk>(f ---> a) F; cos a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. tan (f x)) ---> tan a) F"
  2965   by (rule isCont_tendsto_compose [OF isCont_tan])
  2966 
  2967 lemma continuous_tan:
  2968   "continuous F f \<Longrightarrow> cos (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. tan (f x))"
  2969   unfolding continuous_def by (rule tendsto_tan)
  2970 
  2971 lemma isCont_tan'' [continuous_intros]:
  2972   "continuous (at x) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. tan (f x))"
  2973   unfolding continuous_at by (rule tendsto_tan)
  2974 
  2975 lemma continuous_within_tan [continuous_intros]:
  2976   "continuous (at x within s) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
  2977   unfolding continuous_within by (rule tendsto_tan)
  2978 
  2979 lemma continuous_on_tan [continuous_on_intros]:
  2980   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. cos (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. tan (f x))"
  2981   unfolding continuous_on_def by (auto intro: tendsto_tan)
  2982 
  2983 lemma LIM_cos_div_sin: "(\<lambda>x. cos(x)/sin(x)) -- pi/2 --> 0"
  2984   by (rule LIM_cong_limit, (rule tendsto_intros)+, simp_all)
  2985 
  2986 lemma lemma_tan_total: "0 < y ==> \<exists>x. 0 < x & x < pi/2 & y < tan x"
  2987   apply (cut_tac LIM_cos_div_sin)
  2988   apply (simp only: LIM_eq)
  2989   apply (drule_tac x = "inverse y" in spec, safe, force)
  2990   apply (drule_tac ?d1.0 = s in pi_half_gt_zero [THEN [2] real_lbound_gt_zero], safe)
  2991   apply (rule_tac x = "(pi/2) - e" in exI)
  2992   apply (simp (no_asm_simp))
  2993   apply (drule_tac x = "(pi/2) - e" in spec)
  2994   apply (auto simp add: tan_def sin_diff cos_diff)
  2995   apply (rule inverse_less_iff_less [THEN iffD1])
  2996   apply (auto simp add: divide_inverse)
  2997   apply (rule mult_pos_pos)
  2998   apply (subgoal_tac [3] "0 < sin e & 0 < cos e")
  2999   apply (auto intro: cos_gt_zero sin_gt_zero2 simp add: mult_commute)
  3000   done
  3001 
  3002 lemma tan_total_pos: "0 \<le> y ==> \<exists>x. 0 \<le> x & x < pi/2 & tan x = y"
  3003   apply (frule order_le_imp_less_or_eq, safe)
  3004    prefer 2 apply force
  3005   apply (drule lemma_tan_total, safe)
  3006   apply (cut_tac f = tan and a = 0 and b = x and y = y in IVT_objl)
  3007   apply (auto intro!: DERIV_tan [THEN DERIV_isCont])
  3008   apply (drule_tac y = xa in order_le_imp_less_or_eq)
  3009   apply (auto dest: cos_gt_zero)
  3010   done
  3011 
  3012 lemma lemma_tan_total1: "\<exists>x. -(pi/2) < x & x < (pi/2) & tan x = y"
  3013   apply (cut_tac linorder_linear [of 0 y], safe)
  3014   apply (drule tan_total_pos)
  3015   apply (cut_tac [2] y="-y" in tan_total_pos, safe)
  3016   apply (rule_tac [3] x = "-x" in exI)
  3017   apply (auto del: exI intro!: exI)
  3018   done
  3019 
  3020 lemma tan_total: "EX! x. -(pi/2) < x & x < (pi/2) & tan x = y"
  3021   apply (cut_tac y = y in lemma_tan_total1, auto)
  3022   apply (cut_tac x = xa and y = y in linorder_less_linear, auto)
  3023   apply (subgoal_tac [2] "\<exists>z. y < z & z < xa & DERIV tan z :> 0")
  3024   apply (subgoal_tac "\<exists>z. xa < z & z < y & DERIV tan z :> 0")
  3025   apply (rule_tac [4] Rolle)
  3026   apply (rule_tac [2] Rolle)
  3027   apply (auto del: exI intro!: DERIV_tan DERIV_isCont exI
  3028               simp add: differentiable_def)
  3029   txt{*Now, simulate TRYALL*}
  3030   apply (rule_tac [!] DERIV_tan asm_rl)
  3031   apply (auto dest!: DERIV_unique [OF _ DERIV_tan]
  3032               simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym])
  3033   done
  3034 
  3035 lemma tan_monotone:
  3036   assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
  3037   shows "tan y < tan x"
  3038 proof -
  3039   have "\<forall>x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse ((cos x')\<^sup>2)"
  3040   proof (rule allI, rule impI)
  3041     fix x' :: real
  3042     assume "y \<le> x' \<and> x' \<le> x"
  3043     hence "-(pi/2) < x'" and "x' < pi/2" using assms by auto
  3044     from cos_gt_zero_pi[OF this]
  3045     have "cos x' \<noteq> 0" by auto
  3046     thus "DERIV tan x' :> inverse ((cos x')\<^sup>2)" by (rule DERIV_tan)
  3047   qed
  3048   from MVT2[OF `y < x` this]
  3049   obtain z where "y < z" and "z < x"
  3050     and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<^sup>2)" by auto
  3051   hence "- (pi / 2) < z" and "z < pi / 2" using assms by auto
  3052   hence "0 < cos z" using cos_gt_zero_pi by auto
  3053   hence inv_pos: "0 < inverse ((cos z)\<^sup>2)" by auto
  3054   have "0 < x - y" using `y < x` by auto
  3055   from mult_pos_pos [OF this inv_pos]
  3056   have "0 < tan x - tan y" unfolding tan_diff by auto
  3057   thus ?thesis by auto
  3058 qed
  3059 
  3060 lemma tan_monotone':
  3061   assumes "- (pi / 2) < y"
  3062     and "y < pi / 2"
  3063     and "- (pi / 2) < x"
  3064     and "x < pi / 2"
  3065   shows "(y < x) = (tan y < tan x)"
  3066 proof
  3067   assume "y < x"
  3068   thus "tan y < tan x"
  3069     using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
  3070 next
  3071   assume "tan y < tan x"
  3072   show "y < x"
  3073   proof (rule ccontr)
  3074     assume "\<not> y < x" hence "x \<le> y" by auto
  3075     hence "tan x \<le> tan y"
  3076     proof (cases "x = y")
  3077       case True thus ?thesis by auto
  3078     next
  3079       case False hence "x < y" using `x \<le> y` by auto
  3080       from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
  3081     qed
  3082     thus False using `tan y < tan x` by auto
  3083   qed
  3084 qed
  3085 
  3086 lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)"
  3087   unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
  3088 
  3089 lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
  3090   by (simp add: tan_def)
  3091 
  3092 lemma tan_periodic_nat[simp]:
  3093   fixes n :: nat
  3094   shows "tan (x + real n * pi) = tan x"
  3095 proof (induct n arbitrary: x)
  3096   case 0
  3097   then show ?case by simp
  3098 next
  3099   case (Suc n)
  3100   have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi"
  3101     unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right by auto
  3102   show ?case unfolding split_pi_off using Suc by auto
  3103 qed
  3104 
  3105 lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
  3106 proof (cases "0 \<le> i")
  3107   case True
  3108   hence i_nat: "real i = real (nat i)" by auto
  3109   show ?thesis unfolding i_nat by auto
  3110 next
  3111   case False
  3112   hence i_nat: "real i = - real (nat (-i))" by auto
  3113   have "tan x = tan (x + real i * pi - real i * pi)"
  3114     by auto
  3115   also have "\<dots> = tan (x + real i * pi)"
  3116     unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
  3117   finally show ?thesis by auto
  3118 qed
  3119 
  3120 lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
  3121   using tan_periodic_int[of _ "numeral n" ] unfolding real_numeral .
  3122 
  3123 subsection {* Inverse Trigonometric Functions *}
  3124 
  3125 definition arcsin :: "real => real"
  3126   where "arcsin y = (THE x. -(pi/2) \<le> x & x \<le> pi/2 & sin x = y)"
  3127 
  3128 definition arccos :: "real => real"
  3129   where "arccos y = (THE x. 0 \<le> x & x \<le> pi & cos x = y)"
  3130 
  3131 definition arctan :: "real => real"
  3132   where "arctan y = (THE x. -(pi/2) < x & x < pi/2 & tan x = y)"
  3133 
  3134 lemma arcsin:
  3135   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow>
  3136     -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2 & sin(arcsin y) = y"
  3137   unfolding arcsin_def by (rule theI' [OF sin_total])
  3138 
  3139 lemma arcsin_pi:
  3140   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi & sin(arcsin y) = y"
  3141   apply (drule (1) arcsin)
  3142   apply (force intro: order_trans)
  3143   done
  3144 
  3145 lemma sin_arcsin [simp]: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> sin(arcsin y) = y"
  3146   by (blast dest: arcsin)
  3147 
  3148 lemma arcsin_bounded: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2"
  3149   by (blast dest: arcsin)
  3150 
  3151 lemma arcsin_lbound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y"
  3152   by (blast dest: arcsin)
  3153 
  3154 lemma arcsin_ubound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin y \<le> pi/2"
  3155   by (blast dest: arcsin)
  3156 
  3157 lemma arcsin_lt_bounded:
  3158      "[| -1 < y; y < 1 |] ==> -(pi/2) < arcsin y & arcsin y < pi/2"
  3159   apply (frule order_less_imp_le)
  3160   apply (frule_tac y = y in order_less_imp_le)
  3161   apply (frule arcsin_bounded)
  3162   apply (safe, simp)
  3163   apply (drule_tac y = "arcsin y" in order_le_imp_less_or_eq)
  3164   apply (drule_tac [2] y = "pi/2" in order_le_imp_less_or_eq, safe)
  3165   apply (drule_tac [!] f = sin in arg_cong, auto)
  3166   done
  3167 
  3168 lemma arcsin_sin: "[|-(pi/2) \<le> x; x \<le> pi/2 |] ==> arcsin(sin x) = x"
  3169   apply (unfold arcsin_def)
  3170   apply (rule the1_equality)
  3171   apply (rule sin_total, auto)
  3172   done
  3173 
  3174 lemma arccos:
  3175      "[| -1 \<le> y; y \<le> 1 |]
  3176       ==> 0 \<le> arccos y & arccos y \<le> pi & cos(arccos y) = y"
  3177   unfolding arccos_def by (rule theI' [OF cos_total])
  3178 
  3179 lemma cos_arccos [simp]: "[| -1 \<le> y; y \<le> 1 |] ==> cos(arccos y) = y"
  3180   by (blast dest: arccos)
  3181 
  3182 lemma arccos_bounded: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y & arccos y \<le> pi"
  3183   by (blast dest: arccos)
  3184 
  3185 lemma arccos_lbound: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y"
  3186   by (blast dest: arccos)
  3187 
  3188 lemma arccos_ubound: "[| -1 \<le> y; y \<le> 1 |] ==> arccos y \<le> pi"
  3189   by (blast dest: arccos)
  3190 
  3191 lemma arccos_lt_bounded:
  3192      "[| -1 < y; y < 1 |]
  3193       ==> 0 < arccos y & arccos y < pi"
  3194   apply (frule order_less_imp_le)
  3195   apply (frule_tac y = y in order_less_imp_le)
  3196   apply (frule arccos_bounded, auto)
  3197   apply (drule_tac y = "arccos y" in order_le_imp_less_or_eq)
  3198   apply (drule_tac [2] y = pi in order_le_imp_less_or_eq, auto)
  3199   apply (drule_tac [!] f = cos in arg_cong, auto)
  3200   done
  3201 
  3202 lemma arccos_cos: "[|0 \<le> x; x \<le> pi |] ==> arccos(cos x) = x"
  3203   apply (simp add: arccos_def)
  3204   apply (auto intro!: the1_equality cos_total)
  3205   done
  3206 
  3207 lemma arccos_cos2: "[|x \<le> 0; -pi \<le> x |] ==> arccos(cos x) = -x"
  3208   apply (simp add: arccos_def)
  3209   apply (auto intro!: the1_equality cos_total)
  3210   done
  3211 
  3212 lemma cos_arcsin: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cos (arcsin x) = sqrt (1 - x\<^sup>2)"
  3213   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3214   apply (rule power2_eq_imp_eq)
  3215   apply (simp add: cos_squared_eq)
  3216   apply (rule cos_ge_zero)
  3217   apply (erule (1) arcsin_lbound)
  3218   apply (erule (1) arcsin_ubound)
  3219   apply simp
  3220   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3221   apply (rule power_mono, simp, simp)
  3222   done
  3223 
  3224 lemma sin_arccos: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> sin (arccos x) = sqrt (1 - x\<^sup>2)"
  3225   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3226   apply (rule power2_eq_imp_eq)
  3227   apply (simp add: sin_squared_eq)
  3228   apply (rule sin_ge_zero)
  3229   apply (erule (1) arccos_lbound)
  3230   apply (erule (1) arccos_ubound)
  3231   apply simp
  3232   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3233   apply (rule power_mono, simp, simp)
  3234   done
  3235 
  3236 lemma arctan [simp]: "- (pi/2) < arctan y  & arctan y < pi/2 & tan (arctan y) = y"
  3237   unfolding arctan_def by (rule theI' [OF tan_total])
  3238 
  3239 lemma tan_arctan: "tan (arctan y) = y"
  3240   by auto
  3241 
  3242 lemma arctan_bounded: "- (pi/2) < arctan y  & arctan y < pi/2"
  3243   by (auto simp only: arctan)
  3244 
  3245 lemma arctan_lbound: "- (pi/2) < arctan y"
  3246   by auto
  3247 
  3248 lemma arctan_ubound: "arctan y < pi/2"
  3249   by (auto simp only: arctan)
  3250 
  3251 lemma arctan_unique:
  3252   assumes "-(pi/2) < x"
  3253     and "x < pi/2"
  3254     and "tan x = y"
  3255   shows "arctan y = x"
  3256   using assms arctan [of y] tan_total [of y] by (fast elim: ex1E)
  3257 
  3258 lemma arctan_tan: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> arctan (tan x) = x"
  3259   by (rule arctan_unique) simp_all
  3260 
  3261 lemma arctan_zero_zero [simp]: "arctan 0 = 0"
  3262   by (rule arctan_unique) simp_all
  3263 
  3264 lemma arctan_minus: "arctan (- x) = - arctan x"
  3265   apply (rule arctan_unique)
  3266   apply (simp only: neg_less_iff_less arctan_ubound)
  3267   apply (metis minus_less_iff arctan_lbound)
  3268   apply simp
  3269   done
  3270 
  3271 lemma cos_arctan_not_zero [simp]: "cos (arctan x) \<noteq> 0"
  3272   by (intro less_imp_neq [symmetric] cos_gt_zero_pi
  3273     arctan_lbound arctan_ubound)
  3274 
  3275 lemma cos_arctan: "cos (arctan x) = 1 / sqrt (1 + x\<^sup>2)"
  3276 proof (rule power2_eq_imp_eq)
  3277   have "0 < 1 + x\<^sup>2" by (simp add: add_pos_nonneg)
  3278   show "0 \<le> 1 / sqrt (1 + x\<^sup>2)" by simp
  3279   show "0 \<le> cos (arctan x)"
  3280     by (intro less_imp_le cos_gt_zero_pi arctan_lbound arctan_ubound)
  3281   have "(cos (arctan x))\<^sup>2 * (1 + (tan (arctan x))\<^sup>2) = 1"
  3282     unfolding tan_def by (simp add: distrib_left power_divide)
  3283   thus "(cos (arctan x))\<^sup>2 = (1 / sqrt (1 + x\<^sup>2))\<^sup>2"
  3284     using `0 < 1 + x\<^sup>2` by (simp add: power_divide eq_divide_eq)
  3285 qed
  3286 
  3287 lemma sin_arctan: "sin (arctan x) = x / sqrt (1 + x\<^sup>2)"
  3288   using add_pos_nonneg [OF zero_less_one zero_le_power2 [of x]]
  3289   using tan_arctan [of x] unfolding tan_def cos_arctan
  3290   by (simp add: eq_divide_eq)
  3291 
  3292 lemma tan_sec: "cos x \<noteq> 0 ==> 1 + (tan x)\<^sup>2 = (inverse (cos x))\<^sup>2"
  3293   apply (rule power_inverse [THEN subst])
  3294   apply (rule_tac c1 = "(cos x)\<^sup>2" in real_mult_right_cancel [THEN iffD1])
  3295   apply (auto dest: field_power_not_zero
  3296           simp add: power_mult_distrib distrib_right power_divide tan_def
  3297                     mult_assoc power_inverse [symmetric])
  3298   done
  3299 
  3300 lemma arctan_less_iff: "arctan x < arctan y \<longleftrightarrow> x < y"
  3301   by (metis tan_monotone' arctan_lbound arctan_ubound tan_arctan)
  3302 
  3303 lemma arctan_le_iff: "arctan x \<le> arctan y \<longleftrightarrow> x \<le> y"
  3304   by (simp only: not_less [symmetric] arctan_less_iff)
  3305 
  3306 lemma arctan_eq_iff: "arctan x = arctan y \<longleftrightarrow> x = y"
  3307   by (simp only: eq_iff [where 'a=real] arctan_le_iff)
  3308 
  3309 lemma zero_less_arctan_iff [simp]: "0 < arctan x \<longleftrightarrow> 0 < x"
  3310   using arctan_less_iff [of 0 x] by simp
  3311 
  3312 lemma arctan_less_zero_iff [simp]: "arctan x < 0 \<longleftrightarrow> x < 0"
  3313   using arctan_less_iff [of x 0] by simp
  3314 
  3315 lemma zero_le_arctan_iff [simp]: "0 \<le> arctan x \<longleftrightarrow> 0 \<le> x"
  3316   using arctan_le_iff [of 0 x] by simp
  3317 
  3318 lemma arctan_le_zero_iff [simp]: "arctan x \<le> 0 \<longleftrightarrow> x \<le> 0"
  3319   using arctan_le_iff [of x 0] by simp
  3320 
  3321 lemma arctan_eq_zero_iff [simp]: "arctan x = 0 \<longleftrightarrow> x = 0"
  3322   using arctan_eq_iff [of x 0] by simp
  3323 
  3324 lemma continuous_on_arcsin': "continuous_on {-1 .. 1} arcsin"
  3325 proof -
  3326   have "continuous_on (sin ` {- pi / 2 .. pi / 2}) arcsin"
  3327     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arcsin_sin)
  3328   also have "sin ` {- pi / 2 .. pi / 2} = {-1 .. 1}"
  3329   proof safe
  3330     fix x :: real
  3331     assume "x \<in> {-1..1}"
  3332     then show "x \<in> sin ` {- pi / 2..pi / 2}"
  3333       using arcsin_lbound arcsin_ubound
  3334       by (intro image_eqI[where x="arcsin x"]) auto
  3335   qed simp
  3336   finally show ?thesis .
  3337 qed
  3338 
  3339 lemma continuous_on_arcsin [continuous_on_intros]:
  3340   "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))"
  3341   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arcsin']]
  3342   by (auto simp: comp_def subset_eq)
  3343 
  3344 lemma isCont_arcsin: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arcsin x"
  3345   using continuous_on_arcsin'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3346   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3347 
  3348 lemma continuous_on_arccos': "continuous_on {-1 .. 1} arccos"
  3349 proof -
  3350   have "continuous_on (cos ` {0 .. pi}) arccos"
  3351     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arccos_cos)
  3352   also have "cos ` {0 .. pi} = {-1 .. 1}"
  3353   proof safe
  3354     fix x :: real
  3355     assume "x \<in> {-1..1}"
  3356     then show "x \<in> cos ` {0..pi}"
  3357       using arccos_lbound arccos_ubound
  3358       by (intro image_eqI[where x="arccos x"]) auto
  3359   qed simp
  3360   finally show ?thesis .
  3361 qed
  3362 
  3363 lemma continuous_on_arccos [continuous_on_intros]:
  3364   "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))"
  3365   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arccos']]
  3366   by (auto simp: comp_def subset_eq)
  3367 
  3368 lemma isCont_arccos: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arccos x"
  3369   using continuous_on_arccos'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3370   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3371 
  3372 lemma isCont_arctan: "isCont arctan x"
  3373   apply (rule arctan_lbound [of x, THEN dense, THEN exE], clarify)
  3374   apply (rule arctan_ubound [of x, THEN dense, THEN exE], clarify)
  3375   apply (subgoal_tac "isCont arctan (tan (arctan x))", simp)
  3376   apply (erule (1) isCont_inverse_function2 [where f=tan])
  3377   apply (metis arctan_tan order_le_less_trans order_less_le_trans)
  3378   apply (metis cos_gt_zero_pi isCont_tan order_less_le_trans less_le)
  3379   done
  3380 
  3381 lemma tendsto_arctan [tendsto_intros]: "(f ---> x) F \<Longrightarrow> ((\<lambda>x. arctan (f x)) ---> arctan x) F"
  3382   by (rule isCont_tendsto_compose [OF isCont_arctan])
  3383 
  3384 lemma continuous_arctan [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arctan (f x))"
  3385   unfolding continuous_def by (rule tendsto_arctan)
  3386 
  3387 lemma continuous_on_arctan [continuous_on_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. arctan (f x))"
  3388   unfolding continuous_on_def by (auto intro: tendsto_arctan)
  3389 
  3390 lemma DERIV_arcsin:
  3391   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arcsin x :> inverse (sqrt (1 - x\<^sup>2))"
  3392   apply (rule DERIV_inverse_function [where f=sin and a="-1" and b="1"])
  3393   apply (rule DERIV_cong [OF DERIV_sin])
  3394   apply (simp add: cos_arcsin)
  3395   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3396   apply (rule power_strict_mono, simp, simp, simp)
  3397   apply assumption
  3398   apply assumption
  3399   apply simp
  3400   apply (erule (1) isCont_arcsin)
  3401   done
  3402 
  3403 lemma DERIV_arccos:
  3404   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arccos x :> inverse (- sqrt (1 - x\<^sup>2))"
  3405   apply (rule DERIV_inverse_function [where f=cos and a="-1" and b="1"])
  3406   apply (rule DERIV_cong [OF DERIV_cos])
  3407   apply (simp add: sin_arccos)
  3408   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3409   apply (rule power_strict_mono, simp, simp, simp)
  3410   apply assumption
  3411   apply assumption
  3412   apply simp
  3413   apply (erule (1) isCont_arccos)
  3414   done
  3415 
  3416 lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
  3417   apply (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
  3418   apply (rule DERIV_cong [OF DERIV_tan])
  3419   apply (rule cos_arctan_not_zero)
  3420   apply (simp add: power_inverse tan_sec [symmetric])
  3421   apply (subgoal_tac "0 < 1 + x\<^sup>2", simp)
  3422   apply (simp add: add_pos_nonneg)
  3423   apply (simp, simp, simp, rule isCont_arctan)
  3424   done
  3425 
  3426 declare
  3427   DERIV_arcsin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3428   DERIV_arccos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3429   DERIV_arctan[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3430 
  3431 lemma filterlim_tan_at_right: "filterlim tan at_bot (at_right (- pi/2))"
  3432   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])
  3433      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3434            intro!: tan_monotone exI[of _ "pi/2"])
  3435 
  3436 lemma filterlim_tan_at_left: "filterlim tan at_top (at_left (pi/2))"
  3437   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])
  3438      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3439            intro!: tan_monotone exI[of _ "pi/2"])
  3440 
  3441 lemma tendsto_arctan_at_top: "(arctan ---> (pi/2)) at_top"
  3442 proof (rule tendstoI)
  3443   fix e :: real
  3444   assume "0 < e"
  3445   def y \<equiv> "pi/2 - min (pi/2) e"
  3446   then have y: "0 \<le> y" "y < pi/2" "pi/2 \<le> e + y"
  3447     using `0 < e` by auto
  3448 
  3449   show "eventually (\<lambda>x. dist (arctan x) (pi / 2) < e) at_top"
  3450   proof (intro eventually_at_top_dense[THEN iffD2] exI allI impI)
  3451     fix x
  3452     assume "tan y < x"
  3453     then have "arctan (tan y) < arctan x"
  3454       by (simp add: arctan_less_iff)
  3455     with y have "y < arctan x"
  3456       by (subst (asm) arctan_tan) simp_all
  3457     with arctan_ubound[of x, arith] y `0 < e`
  3458     show "dist (arctan x) (pi / 2) < e"
  3459       by (simp add: dist_real_def)
  3460   qed
  3461 qed
  3462 
  3463 lemma tendsto_arctan_at_bot: "(arctan ---> - (pi/2)) at_bot"
  3464   unfolding filterlim_at_bot_mirror arctan_minus
  3465   by (intro tendsto_minus tendsto_arctan_at_top)
  3466 
  3467 
  3468 subsection {* More Theorems about Sin and Cos *}
  3469 
  3470 lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
  3471 proof -
  3472   let ?c = "cos (pi / 4)" and ?s = "sin (pi / 4)"
  3473   have nonneg: "0 \<le> ?c"
  3474     by (simp add: cos_ge_zero)
  3475   have "0 = cos (pi / 4 + pi / 4)"
  3476     by simp
  3477   also have "cos (pi / 4 + pi / 4) = ?c\<^sup>2 - ?s\<^sup>2"
  3478     by (simp only: cos_add power2_eq_square)
  3479   also have "\<dots> = 2 * ?c\<^sup>2 - 1"
  3480     by (simp add: sin_squared_eq)
  3481   finally have "?c\<^sup>2 = (sqrt 2 / 2)\<^sup>2"
  3482     by (simp add: power_divide)
  3483   thus ?thesis
  3484     using nonneg by (rule power2_eq_imp_eq) simp
  3485 qed
  3486 
  3487 lemma cos_30: "cos (pi / 6) = sqrt 3 / 2"
  3488 proof -
  3489   let ?c = "cos (pi / 6)" and ?s = "sin (pi / 6)"
  3490   have pos_c: "0 < ?c"
  3491     by (rule cos_gt_zero, simp, simp)
  3492   have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
  3493     by simp
  3494   also have "\<dots> = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
  3495     by (simp only: cos_add sin_add)
  3496   also have "\<dots> = ?c * (?c\<^sup>2 - 3 * ?s\<^sup>2)"
  3497     by (simp add: algebra_simps power2_eq_square)
  3498   finally have "?c\<^sup>2 = (sqrt 3 / 2)\<^sup>2"
  3499     using pos_c by (simp add: sin_squared_eq power_divide)
  3500   thus ?thesis
  3501     using pos_c [THEN order_less_imp_le]
  3502     by (rule power2_eq_imp_eq) simp
  3503 qed
  3504 
  3505 lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
  3506   by (simp add: sin_cos_eq cos_45)
  3507 
  3508 lemma sin_60: "sin (pi / 3) = sqrt 3 / 2"
  3509   by (simp add: sin_cos_eq cos_30)
  3510 
  3511 lemma cos_60: "cos (pi / 3) = 1 / 2"
  3512   apply (rule power2_eq_imp_eq)
  3513   apply (simp add: cos_squared_eq sin_60 power_divide)
  3514   apply (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
  3515   done
  3516 
  3517 lemma sin_30: "sin (pi / 6) = 1 / 2"
  3518   by (simp add: sin_cos_eq cos_60)
  3519 
  3520 lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
  3521   unfolding tan_def by (simp add: sin_30 cos_30)
  3522 
  3523 lemma tan_45: "tan (pi / 4) = 1"
  3524   unfolding tan_def by (simp add: sin_45 cos_45)
  3525 
  3526 lemma tan_60: "tan (pi / 3) = sqrt 3"
  3527   unfolding tan_def by (simp add: sin_60 cos_60)
  3528 
  3529 lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi / 2) = (-1) ^ n"
  3530 proof -
  3531   have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
  3532     by (auto simp add: algebra_simps sin_add)
  3533   thus ?thesis
  3534     by (simp add: real_of_nat_Suc distrib_right add_divide_distrib
  3535                   mult_commute [of pi])
  3536 qed
  3537 
  3538 lemma cos_2npi [simp]: "cos (2 * real (n::nat) * pi) = 1"
  3539   by (simp add: cos_double mult_assoc power_add [symmetric] numeral_2_eq_2)
  3540 
  3541 lemma cos_3over2_pi [simp]: "cos (3 / 2 * pi) = 0"
  3542   apply (subgoal_tac "cos (pi + pi/2) = 0", simp)
  3543   apply (subst cos_add, simp)
  3544   done
  3545 
  3546 lemma sin_2npi [simp]: "sin (2 * real (n::nat) * pi) = 0"
  3547   by (auto simp add: mult_assoc)
  3548 
  3549 lemma sin_3over2_pi [simp]: "sin (3 / 2 * pi) = - 1"
  3550   apply (subgoal_tac "sin (pi + pi/2) = - 1", simp)
  3551   apply (subst sin_add, simp)
  3552   done
  3553 
  3554 lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
  3555   apply (simp only: cos_add sin_add real_of_nat_Suc distrib_right distrib_left add_divide_distrib)
  3556   apply auto
  3557   done
  3558 
  3559 lemma DERIV_cos_add [simp]: "DERIV (\<lambda>x. cos (x + k)) xa :> - sin (xa + k)"
  3560   by (auto intro!: DERIV_intros)
  3561 
  3562 lemma sin_zero_abs_cos_one: "sin x = 0 ==> \<bar>cos x\<bar> = 1"
  3563   by (auto simp add: sin_zero_iff even_mult_two_ex)
  3564 
  3565 lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
  3566   using sin_cos_squared_add3 [where x = x] by auto
  3567 
  3568 
  3569 subsection {* Machins formula *}
  3570 
  3571 lemma arctan_one: "arctan 1 = pi / 4"
  3572   by (rule arctan_unique, simp_all add: tan_45 m2pi_less_pi)
  3573 
  3574 lemma tan_total_pi4:
  3575   assumes "\<bar>x\<bar> < 1"
  3576   shows "\<exists>z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
  3577 proof
  3578   show "- (pi / 4) < arctan x \<and> arctan x < pi / 4 \<and> tan (arctan x) = x"
  3579     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3580     unfolding arctan_less_iff using assms by auto
  3581 qed
  3582 
  3583 lemma arctan_add:
  3584   assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
  3585   shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
  3586 proof (rule arctan_unique [symmetric])
  3587   have "- (pi / 4) \<le> arctan x" and "- (pi / 4) < arctan y"
  3588     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3589     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3590   from add_le_less_mono [OF this]
  3591   show 1: "- (pi / 2) < arctan x + arctan y" by simp
  3592   have "arctan x \<le> pi / 4" and "arctan y < pi / 4"
  3593     unfolding arctan_one [symmetric]
  3594     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3595   from add_le_less_mono [OF this]
  3596   show 2: "arctan x + arctan y < pi / 2" by simp
  3597   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
  3598     using cos_gt_zero_pi [OF 1 2] by (simp add: tan_add)
  3599 qed
  3600 
  3601 theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
  3602 proof -
  3603   have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
  3604   from arctan_add[OF less_imp_le[OF this] this]
  3605   have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
  3606   moreover
  3607   have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
  3608   from arctan_add[OF less_imp_le[OF this] this]
  3609   have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
  3610   moreover
  3611   have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
  3612   from arctan_add[OF this]
  3613   have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
  3614   ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
  3615   thus ?thesis unfolding arctan_one by algebra
  3616 qed
  3617 
  3618 
  3619 subsection {* Introducing the arcus tangens power series *}
  3620 
  3621 lemma monoseq_arctan_series:
  3622   fixes x :: real
  3623   assumes "\<bar>x\<bar> \<le> 1"
  3624   shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
  3625 proof (cases "x = 0")
  3626   case True
  3627   thus ?thesis unfolding monoseq_def One_nat_def by auto
  3628 next
  3629   case False
  3630   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3631   show "monoseq ?a"
  3632   proof -
  3633     {
  3634       fix n
  3635       fix x :: real
  3636       assume "0 \<le> x" and "x \<le> 1"
  3637       have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le>
  3638         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
  3639       proof (rule mult_mono)
  3640         show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))"
  3641           by (rule frac_le) simp_all
  3642         show "0 \<le> 1 / real (Suc (n * 2))"
  3643           by auto
  3644         show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)"
  3645           by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
  3646         show "0 \<le> x ^ Suc (Suc n * 2)"
  3647           by (rule zero_le_power) (simp add: `0 \<le> x`)
  3648       qed
  3649     } note mono = this
  3650 
  3651     show ?thesis
  3652     proof (cases "0 \<le> x")
  3653       case True from mono[OF this `x \<le> 1`, THEN allI]
  3654       show ?thesis unfolding Suc_eq_plus1[symmetric]
  3655         by (rule mono_SucI2)
  3656     next
  3657       case False
  3658       hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
  3659       from mono[OF this]
  3660       have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge>
  3661         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
  3662       thus ?thesis unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI1[OF allI])
  3663     qed
  3664   qed
  3665 qed
  3666 
  3667 lemma zeroseq_arctan_series:
  3668   fixes x :: real
  3669   assumes "\<bar>x\<bar> \<le> 1"
  3670   shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
  3671 proof (cases "x = 0")
  3672   case True
  3673   thus ?thesis
  3674     unfolding One_nat_def by (auto simp add: tendsto_const)
  3675 next
  3676   case False
  3677   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3678   show "?a ----> 0"
  3679   proof (cases "\<bar>x\<bar> < 1")
  3680     case True
  3681     hence "norm x < 1" by auto
  3682     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
  3683     have "(\<lambda>n. 1 / real (n + 1) * x ^ (n + 1)) ----> 0"
  3684       unfolding inverse_eq_divide Suc_eq_plus1 by simp
  3685     then show ?thesis using pos2 by (rule LIMSEQ_linear)
  3686   next
  3687     case False
  3688     hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3689     hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x"
  3690       unfolding One_nat_def by auto
  3691     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] tendsto_const[of x]]
  3692     show ?thesis unfolding n_eq Suc_eq_plus1 by auto
  3693   qed
  3694 qed
  3695 
  3696 lemma summable_arctan_series:
  3697   fixes x :: real and n :: nat
  3698   assumes "\<bar>x\<bar> \<le> 1"
  3699   shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3700   (is "summable (?c x)")
  3701   by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
  3702 
  3703 lemma less_one_imp_sqr_less_one:
  3704   fixes x :: real
  3705   assumes "\<bar>x\<bar> < 1"
  3706   shows "x\<^sup>2 < 1"
  3707 proof -
  3708   from mult_left_mono[OF less_imp_le[OF `\<bar>x\<bar> < 1`] abs_ge_zero[of x]]
  3709   have "\<bar>x\<^sup>2\<bar> < 1" using `\<bar>x\<bar> < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
  3710   thus ?thesis using zero_le_power2 by auto
  3711 qed
  3712 
  3713 lemma DERIV_arctan_series:
  3714   assumes "\<bar> x \<bar> < 1"
  3715   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))"
  3716   (is "DERIV ?arctan _ :> ?Int")
  3717 proof -
  3718   let ?f = "\<lambda>n. if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
  3719 
  3720   have n_even: "\<And>n :: nat. even n \<Longrightarrow> 2 * (n div 2) = n"
  3721     by presburger
  3722   then have if_eq: "\<And>n x'. ?f n * real (Suc n) * x'^n =
  3723     (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)"
  3724     by auto
  3725 
  3726   {
  3727     fix x :: real
  3728     assume "\<bar>x\<bar> < 1"
  3729     hence "x\<^sup>2 < 1" by (rule less_one_imp_sqr_less_one)
  3730     have "summable (\<lambda> n. -1 ^ n * (x\<^sup>2) ^n)"
  3731       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`])
  3732     hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
  3733   } note summable_Integral = this
  3734 
  3735   {
  3736     fix f :: "nat \<Rightarrow> real"
  3737     have "\<And>x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3738     proof
  3739       fix x :: real
  3740       assume "f sums x"
  3741       from sums_if[OF sums_zero this]
  3742       show "(\<lambda>n. if even n then f (n div 2) else 0) sums x"
  3743         by auto
  3744     next
  3745       fix x :: real
  3746       assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3747       from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
  3748       show "f sums x" unfolding sums_def by auto
  3749     qed
  3750     hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
  3751   } note sums_even = this
  3752 
  3753   have Int_eq: "(\<Sum>n. ?f n * real (Suc n) * x^n) = ?Int"
  3754     unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
  3755     by auto
  3756 
  3757   {
  3758     fix x :: real
  3759     have if_eq': "\<And>n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
  3760       (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
  3761       using n_even by auto
  3762     have idx_eq: "\<And>n. n * 2 + 1 = Suc (2 * n)" by auto
  3763     have "(\<Sum>n. ?f n * x^(Suc n)) = ?arctan x"
  3764       unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
  3765       by auto
  3766   } note arctan_eq = this
  3767 
  3768   have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
  3769   proof (rule DERIV_power_series')
  3770     show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
  3771     {
  3772       fix x' :: real
  3773       assume x'_bounds: "x' \<in> {- 1 <..< 1}"
  3774       hence "\<bar>x'\<bar> < 1" by auto
  3775 
  3776       let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
  3777       show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
  3778         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`])
  3779     }
  3780   qed auto
  3781   thus ?thesis unfolding Int_eq arctan_eq .
  3782 qed
  3783 
  3784 lemma arctan_series:
  3785   assumes "\<bar> x \<bar> \<le> 1"
  3786   shows "arctan x = (\<Sum>k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3787   (is "_ = suminf (\<lambda> n. ?c x n)")
  3788 proof -
  3789   let ?c' = "\<lambda>x n. (-1)^n * x^(n*2)"
  3790 
  3791   {
  3792     fix r x :: real
  3793     assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
  3794     have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
  3795     from DERIV_arctan_series[OF this] have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
  3796   } note DERIV_arctan_suminf = this
  3797 
  3798   {
  3799     fix x :: real
  3800     assume "\<bar>x\<bar> \<le> 1"
  3801     note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]]
  3802   } note arctan_series_borders = this
  3803 
  3804   {
  3805     fix x :: real
  3806     assume "\<bar>x\<bar> < 1"
  3807     have "arctan x = (\<Sum>k. ?c x k)"
  3808     proof -
  3809       obtain r where "\<bar>x\<bar> < r" and "r < 1"
  3810         using dense[OF `\<bar>x\<bar> < 1`] by blast
  3811       hence "0 < r" and "-r < x" and "x < r" by auto
  3812 
  3813       have suminf_eq_arctan_bounded: "\<And>x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow>
  3814         suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3815       proof -
  3816         fix x a b
  3817         assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
  3818         hence "\<bar>x\<bar> < r" by auto
  3819         show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3820         proof (rule DERIV_isconst2[of "a" "b"])
  3821           show "a < b" and "a \<le> x" and "x \<le> b"
  3822             using `a < b` `a \<le> x` `x \<le> b` by auto
  3823           have "\<forall>x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3824           proof (rule allI, rule impI)
  3825             fix x
  3826             assume "-r < x \<and> x < r"
  3827             hence "\<bar>x\<bar> < r" by auto
  3828             hence "\<bar>x\<bar> < 1" using `r < 1` by auto
  3829             have "\<bar> - (x\<^sup>2) \<bar> < 1"
  3830               using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
  3831             hence "(\<lambda> n. (- (x\<^sup>2)) ^ n) sums (1 / (1 - (- (x\<^sup>2))))"
  3832               unfolding real_norm_def[symmetric] by (rule geometric_sums)
  3833             hence "(?c' x) sums (1 / (1 - (- (x\<^sup>2))))"
  3834               unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
  3835             hence suminf_c'_eq_geom: "inverse (1 + x\<^sup>2) = suminf (?c' x)"
  3836               using sums_unique unfolding inverse_eq_divide by auto
  3837             have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x\<^sup>2))"
  3838               unfolding suminf_c'_eq_geom
  3839               by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
  3840             from DERIV_add_minus[OF this DERIV_arctan]
  3841             show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3842               by auto
  3843           qed
  3844           hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3845             using `-r < a` `b < r` by auto
  3846           thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3847             using `\<bar>x\<bar> < r` by auto
  3848           show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y"
  3849             using DERIV_in_rball DERIV_isCont by auto
  3850         qed
  3851       qed
  3852 
  3853       have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
  3854         unfolding Suc_eq_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero
  3855         by auto
  3856 
  3857       have "suminf (?c x) - arctan x = 0"
  3858       proof (cases "x = 0")
  3859         case True
  3860         thus ?thesis using suminf_arctan_zero by auto
  3861       next
  3862         case False
  3863         hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
  3864         have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
  3865           by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric])
  3866             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3867         moreover
  3868         have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
  3869           by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"])
  3870             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3871         ultimately
  3872         show ?thesis using suminf_arctan_zero by auto
  3873       qed
  3874       thus ?thesis by auto
  3875     qed
  3876   } note when_less_one = this
  3877 
  3878   show "arctan x = suminf (\<lambda> n. ?c x n)"
  3879   proof (cases "\<bar>x\<bar> < 1")
  3880     case True
  3881     thus ?thesis by (rule when_less_one)
  3882   next
  3883     case False
  3884     hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3885     let ?a = "\<lambda>x n. \<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
  3886     let ?diff = "\<lambda> x n. \<bar> arctan x - (\<Sum> i = 0..<n. ?c x i)\<bar>"
  3887     {
  3888       fix n :: nat
  3889       have "0 < (1 :: real)" by auto
  3890       moreover
  3891       {
  3892         fix x :: real
  3893         assume "0 < x" and "x < 1"
  3894         hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
  3895         from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)"
  3896           by auto
  3897         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]
  3898         have "0 < 1 / real (n*2+1) * x^(n*2+1)"
  3899           by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
  3900         hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)"
  3901           by (rule abs_of_pos)
  3902         have "?diff x n \<le> ?a x n"
  3903         proof (cases "even n")
  3904           case True
  3905           hence sgn_pos: "(-1)^n = (1::real)" by auto
  3906           from `even n` obtain m where "2 * m = n"
  3907             unfolding even_mult_two_ex by auto
  3908           from bounds[of m, unfolded this atLeastAtMost_iff]
  3909           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))"
  3910             by auto
  3911           also have "\<dots> = ?c x n" unfolding One_nat_def by auto
  3912           also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
  3913           finally show ?thesis .
  3914         next
  3915           case False
  3916           hence sgn_neg: "(-1)^n = (-1::real)" by auto
  3917           from `odd n` obtain m where m_def: "2 * m + 1 = n"
  3918             unfolding odd_Suc_mult_two_ex by auto
  3919           hence m_plus: "2 * (m + 1) = n + 1" by auto
  3920           from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
  3921           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))"
  3922             by auto
  3923           also have "\<dots> = - ?c x n" unfolding One_nat_def by auto
  3924           also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
  3925           finally show ?thesis .
  3926         qed
  3927         hence "0 \<le> ?a x n - ?diff x n" by auto
  3928       }
  3929       hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
  3930       moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
  3931         unfolding diff_conv_add_uminus divide_inverse
  3932         by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan
  3933           isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum
  3934           simp del: add_uminus_conv_diff)
  3935       ultimately have "0 \<le> ?a 1 n - ?diff 1 n"
  3936         by (rule LIM_less_bound)
  3937       hence "?diff 1 n \<le> ?a 1 n" by auto
  3938     }
  3939     have "?a 1 ----> 0"
  3940       unfolding tendsto_rabs_zero_iff power_one divide_inverse One_nat_def
  3941       by (auto intro!: tendsto_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
  3942     have "?diff 1 ----> 0"
  3943     proof (rule LIMSEQ_I)
  3944       fix r :: real
  3945       assume "0 < r"
  3946       obtain N :: nat where N_I: "\<And>n. N \<le> n \<Longrightarrow> ?a 1 n < r"
  3947         using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
  3948       {
  3949         fix n
  3950         assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
  3951         have "norm (?diff 1 n - 0) < r" by auto
  3952       }
  3953       thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
  3954     qed
  3955     from this [unfolded tendsto_rabs_zero_iff, THEN tendsto_add [OF _ tendsto_const], of "- arctan 1", THEN tendsto_minus]
  3956     have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
  3957     hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
  3958 
  3959     show ?thesis
  3960     proof (cases "x = 1")
  3961       case True
  3962       then show ?thesis by (simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
  3963     next
  3964       case False
  3965       hence "x = -1" using `\<bar>x\<bar> = 1` by auto
  3966 
  3967       have "- (pi / 2) < 0" using pi_gt_zero by auto
  3968       have "- (2 * pi) < 0" using pi_gt_zero by auto
  3969 
  3970       have c_minus_minus: "\<And>i. ?c (- 1) i = - ?c 1 i"
  3971         unfolding One_nat_def by auto
  3972 
  3973       have "arctan (- 1) = arctan (tan (-(pi / 4)))"
  3974         unfolding tan_45 tan_minus ..
  3975       also have "\<dots> = - (pi / 4)"
  3976         by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
  3977       also have "\<dots> = - (arctan (tan (pi / 4)))"
  3978         unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
  3979       also have "\<dots> = - (arctan 1)"
  3980         unfolding tan_45 ..
  3981       also have "\<dots> = - (\<Sum> i. ?c 1 i)"
  3982         using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
  3983       also have "\<dots> = (\<Sum> i. ?c (- 1) i)"
  3984         using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]]
  3985         unfolding c_minus_minus by auto
  3986       finally show ?thesis using `x = -1` by auto
  3987     qed
  3988   qed
  3989 qed
  3990 
  3991 lemma arctan_half:
  3992   fixes x :: real
  3993   shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x\<^sup>2)))"
  3994 proof -
  3995   obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x"
  3996     using tan_total by blast
  3997   hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2"
  3998     by auto
  3999 
  4000   have divide_nonzero_divide: "\<And>A B C :: real. C \<noteq> 0 \<Longrightarrow> A / B = (A / C) / (B / C)"
  4001     by auto
  4002 
  4003   have "0 < cos y" using cos_gt_zero_pi[OF low high] .
  4004   hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y)\<^sup>2) = cos y"
  4005     by auto
  4006 
  4007   have "1 + (tan y)\<^sup>2 = 1 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  4008     unfolding tan_def power_divide ..
  4009   also have "\<dots> = (cos y)\<^sup>2 / (cos y)\<^sup>2 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  4010     using `cos y \<noteq> 0` by auto
  4011   also have "\<dots> = 1 / (cos y)\<^sup>2"
  4012     unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
  4013   finally have "1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2" .
  4014 
  4015   have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)"
  4016     unfolding tan_def divide_nonzero_divide[OF `cos y \<noteq> 0`, symmetric] ..
  4017   also have "\<dots> = tan y / (1 + 1 / cos y)"
  4018     using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
  4019   also have "\<dots> = tan y / (1 + 1 / sqrt ((cos y)\<^sup>2))"
  4020     unfolding cos_sqrt ..
  4021   also have "\<dots> = tan y / (1 + sqrt (1 / (cos y)\<^sup>2))"
  4022     unfolding real_sqrt_divide by auto
  4023   finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)\<^sup>2))"
  4024     unfolding `1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2` .
  4025 
  4026   have "arctan x = y"
  4027     using arctan_tan low high y_eq by auto
  4028   also have "\<dots> = 2 * (arctan (tan (y/2)))"
  4029     using arctan_tan[OF low2 high2] by auto
  4030   also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))"
  4031     unfolding tan_half by auto
  4032   finally show ?thesis
  4033     unfolding eq `tan y = x` .
  4034 qed
  4035 
  4036 lemma arctan_monotone: "x < y \<Longrightarrow> arctan x < arctan y"
  4037   by (simp only: arctan_less_iff)
  4038 
  4039 lemma arctan_monotone': "x \<le> y \<Longrightarrow> arctan x \<le> arctan y"
  4040   by (simp only: arctan_le_iff)
  4041 
  4042 lemma arctan_inverse:
  4043   assumes "x \<noteq> 0"
  4044   shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
  4045 proof (rule arctan_unique)
  4046   show "- (pi / 2) < sgn x * pi / 2 - arctan x"
  4047     using arctan_bounded [of x] assms
  4048     unfolding sgn_real_def
  4049     apply (auto simp add: algebra_simps)
  4050     apply (drule zero_less_arctan_iff [THEN iffD2])
  4051     apply arith
  4052     done
  4053   show "sgn x * pi / 2 - arctan x < pi / 2"
  4054     using arctan_bounded [of "- x"] assms
  4055     unfolding sgn_real_def arctan_minus
  4056     by (auto simp add: algebra_simps)
  4057   show "tan (sgn x * pi / 2 - arctan x) = 1 / x"
  4058     unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
  4059     unfolding sgn_real_def
  4060     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
  4061 qed
  4062 
  4063 theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
  4064 proof -
  4065   have "pi / 4 = arctan 1" using arctan_one by auto
  4066   also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
  4067   finally show ?thesis by auto
  4068 qed
  4069 
  4070 
  4071 subsection {* Existence of Polar Coordinates *}
  4072 
  4073 lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
  4074   apply (rule power2_le_imp_le [OF _ zero_le_one])
  4075   apply (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
  4076   done
  4077 
  4078 lemma cos_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> cos (arccos y) = y"
  4079   by (simp add: abs_le_iff)
  4080 
  4081 lemma sin_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> sin (arccos y) = sqrt (1 - y\<^sup>2)"
  4082   by (simp add: sin_arccos abs_le_iff)
  4083 
  4084 lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
  4085 
  4086 lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
  4087 
  4088 lemma polar_ex1: "0 < y \<Longrightarrow> \<exists>r a. x = r * cos a & y = r * sin a"
  4089   apply (rule_tac x = "sqrt (x\<^sup>2 + y\<^sup>2)" in exI)
  4090   apply (rule_tac x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))" in exI)
  4091   apply (simp add: cos_arccos_lemma1)
  4092   apply (simp add: sin_arccos_lemma1)
  4093   apply (simp add: power_divide)
  4094   apply (simp add: real_sqrt_mult [symmetric])
  4095   apply (simp add: right_diff_distrib)
  4096   done
  4097 
  4098 lemma polar_ex2: "y < 0 ==> \<exists>r a. x = r * cos a & y = r * sin a"
  4099   using polar_ex1 [where x=x and y="-y"]
  4100   apply simp
  4101   apply clarify
  4102   apply (metis cos_minus minus_minus minus_mult_right sin_minus)
  4103   done
  4104 
  4105 lemma polar_Ex: "\<exists>r a. x = r * cos a & y = r * sin a"
  4106   apply (rule_tac x=0 and y=y in linorder_cases)
  4107   apply (erule polar_ex1)
  4108   apply (rule_tac x=x in exI, rule_tac x=0 in exI, simp)
  4109   apply (erule polar_ex2)
  4110   done
  4111 
  4112 end