src/HOL/Transcendental.thy
author wenzelm
Sun Aug 18 22:44:39 2013 +0200 (2013-08-18)
changeset 53079 ade63ccd6f4e
parent 53076 47c9aff07725
child 53599 78ea983f7987
permissions -rw-r--r--
tuned proofs;
     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_field"
    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: nonzero_norm_divide divide_inverse [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_field,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: diff_minus [symmetric] 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   unfolding diff_minus divide_inverse
  1133   by (simp add: exp_add exp_minus)
  1134 
  1135 
  1136 subsubsection {* Properties of the Exponential Function on Reals *}
  1137 
  1138 text {* Comparisons of @{term "exp x"} with zero. *}
  1139 
  1140 text{*Proof: because every exponential can be seen as a square.*}
  1141 lemma exp_ge_zero [simp]: "0 \<le> exp (x::real)"
  1142 proof -
  1143   have "0 \<le> exp (x/2) * exp (x/2)" by simp
  1144   thus ?thesis by (simp add: exp_add [symmetric])
  1145 qed
  1146 
  1147 lemma exp_gt_zero [simp]: "0 < exp (x::real)"
  1148   by (simp add: order_less_le)
  1149 
  1150 lemma not_exp_less_zero [simp]: "\<not> exp (x::real) < 0"
  1151   by (simp add: not_less)
  1152 
  1153 lemma not_exp_le_zero [simp]: "\<not> exp (x::real) \<le> 0"
  1154   by (simp add: not_le)
  1155 
  1156 lemma abs_exp_cancel [simp]: "\<bar>exp x::real\<bar> = exp x"
  1157   by simp
  1158 
  1159 lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
  1160   by (induct n) (auto simp add: real_of_nat_Suc distrib_left exp_add mult_commute)
  1161 
  1162 text {* Strict monotonicity of exponential. *}
  1163 
  1164 lemma exp_ge_add_one_self_aux: "0 \<le> (x::real) \<Longrightarrow> (1 + x) \<le> exp(x)"
  1165   apply (drule order_le_imp_less_or_eq, auto)
  1166   apply (simp add: exp_def)
  1167   apply (rule order_trans)
  1168   apply (rule_tac [2] n = 2 and f = "(\<lambda>n. inverse (real (fact n)) * x ^ n)" in series_pos_le)
  1169   apply (auto intro: summable_exp simp add: numeral_2_eq_2 zero_le_mult_iff)
  1170   done
  1171 
  1172 lemma exp_gt_one: "0 < (x::real) \<Longrightarrow> 1 < exp x"
  1173 proof -
  1174   assume x: "0 < x"
  1175   hence "1 < 1 + x" by simp
  1176   also from x have "1 + x \<le> exp x"
  1177     by (simp add: exp_ge_add_one_self_aux)
  1178   finally show ?thesis .
  1179 qed
  1180 
  1181 lemma exp_less_mono:
  1182   fixes x y :: real
  1183   assumes "x < y"
  1184   shows "exp x < exp y"
  1185 proof -
  1186   from `x < y` have "0 < y - x" by simp
  1187   hence "1 < exp (y - x)" by (rule exp_gt_one)
  1188   hence "1 < exp y / exp x" by (simp only: exp_diff)
  1189   thus "exp x < exp y" by simp
  1190 qed
  1191 
  1192 lemma exp_less_cancel: "exp (x::real) < exp y \<Longrightarrow> x < y"
  1193   apply (simp add: linorder_not_le [symmetric])
  1194   apply (auto simp add: order_le_less exp_less_mono)
  1195   done
  1196 
  1197 lemma exp_less_cancel_iff [iff]: "exp (x::real) < exp y \<longleftrightarrow> x < y"
  1198   by (auto intro: exp_less_mono exp_less_cancel)
  1199 
  1200 lemma exp_le_cancel_iff [iff]: "exp (x::real) \<le> exp y \<longleftrightarrow> x \<le> y"
  1201   by (auto simp add: linorder_not_less [symmetric])
  1202 
  1203 lemma exp_inj_iff [iff]: "exp (x::real) = exp y \<longleftrightarrow> x = y"
  1204   by (simp add: order_eq_iff)
  1205 
  1206 text {* Comparisons of @{term "exp x"} with one. *}
  1207 
  1208 lemma one_less_exp_iff [simp]: "1 < exp (x::real) \<longleftrightarrow> 0 < x"
  1209   using exp_less_cancel_iff [where x=0 and y=x] by simp
  1210 
  1211 lemma exp_less_one_iff [simp]: "exp (x::real) < 1 \<longleftrightarrow> x < 0"
  1212   using exp_less_cancel_iff [where x=x and y=0] by simp
  1213 
  1214 lemma one_le_exp_iff [simp]: "1 \<le> exp (x::real) \<longleftrightarrow> 0 \<le> x"
  1215   using exp_le_cancel_iff [where x=0 and y=x] by simp
  1216 
  1217 lemma exp_le_one_iff [simp]: "exp (x::real) \<le> 1 \<longleftrightarrow> x \<le> 0"
  1218   using exp_le_cancel_iff [where x=x and y=0] by simp
  1219 
  1220 lemma exp_eq_one_iff [simp]: "exp (x::real) = 1 \<longleftrightarrow> x = 0"
  1221   using exp_inj_iff [where x=x and y=0] by simp
  1222 
  1223 lemma lemma_exp_total: "1 \<le> y \<Longrightarrow> \<exists>x. 0 \<le> x & x \<le> y - 1 & exp(x::real) = y"
  1224 proof (rule IVT)
  1225   assume "1 \<le> y"
  1226   hence "0 \<le> y - 1" by simp
  1227   hence "1 + (y - 1) \<le> exp (y - 1)" by (rule exp_ge_add_one_self_aux)
  1228   thus "y \<le> exp (y - 1)" by simp
  1229 qed (simp_all add: le_diff_eq)
  1230 
  1231 lemma exp_total: "0 < (y::real) \<Longrightarrow> \<exists>x. exp x = y"
  1232 proof (rule linorder_le_cases [of 1 y])
  1233   assume "1 \<le> y"
  1234   thus "\<exists>x. exp x = y" by (fast dest: lemma_exp_total)
  1235 next
  1236   assume "0 < y" and "y \<le> 1"
  1237   hence "1 \<le> inverse y" by (simp add: one_le_inverse_iff)
  1238   then obtain x where "exp x = inverse y" by (fast dest: lemma_exp_total)
  1239   hence "exp (- x) = y" by (simp add: exp_minus)
  1240   thus "\<exists>x. exp x = y" ..
  1241 qed
  1242 
  1243 
  1244 subsection {* Natural Logarithm *}
  1245 
  1246 definition ln :: "real \<Rightarrow> real"
  1247   where "ln x = (THE u. exp u = x)"
  1248 
  1249 lemma ln_exp [simp]: "ln (exp x) = x"
  1250   by (simp add: ln_def)
  1251 
  1252 lemma exp_ln [simp]: "0 < x \<Longrightarrow> exp (ln x) = x"
  1253   by (auto dest: exp_total)
  1254 
  1255 lemma exp_ln_iff [simp]: "exp (ln x) = x \<longleftrightarrow> 0 < x"
  1256   by (metis exp_gt_zero exp_ln)
  1257 
  1258 lemma ln_unique: "exp y = x \<Longrightarrow> ln x = y"
  1259   by (erule subst, rule ln_exp)
  1260 
  1261 lemma ln_one [simp]: "ln 1 = 0"
  1262   by (rule ln_unique) simp
  1263 
  1264 lemma ln_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x * y) = ln x + ln y"
  1265   by (rule ln_unique) (simp add: exp_add)
  1266 
  1267 lemma ln_inverse: "0 < x \<Longrightarrow> ln (inverse x) = - ln x"
  1268   by (rule ln_unique) (simp add: exp_minus)
  1269 
  1270 lemma ln_div: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x / y) = ln x - ln y"
  1271   by (rule ln_unique) (simp add: exp_diff)
  1272 
  1273 lemma ln_realpow: "0 < x \<Longrightarrow> ln (x ^ n) = real n * ln x"
  1274   by (rule ln_unique) (simp add: exp_real_of_nat_mult)
  1275 
  1276 lemma ln_less_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x < ln y \<longleftrightarrow> x < y"
  1277   by (subst exp_less_cancel_iff [symmetric]) simp
  1278 
  1279 lemma ln_le_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x \<le> ln y \<longleftrightarrow> x \<le> y"
  1280   by (simp add: linorder_not_less [symmetric])
  1281 
  1282 lemma ln_inj_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x = ln y \<longleftrightarrow> x = y"
  1283   by (simp add: order_eq_iff)
  1284 
  1285 lemma ln_add_one_self_le_self [simp]: "0 \<le> x \<Longrightarrow> ln (1 + x) \<le> x"
  1286   apply (rule exp_le_cancel_iff [THEN iffD1])
  1287   apply (simp add: exp_ge_add_one_self_aux)
  1288   done
  1289 
  1290 lemma ln_less_self [simp]: "0 < x \<Longrightarrow> ln x < x"
  1291   by (rule order_less_le_trans [where y="ln (1 + x)"]) simp_all
  1292 
  1293 lemma ln_ge_zero [simp]: "1 \<le> x \<Longrightarrow> 0 \<le> ln x"
  1294   using ln_le_cancel_iff [of 1 x] by simp
  1295 
  1296 lemma ln_ge_zero_imp_ge_one: "0 \<le> ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> x"
  1297   using ln_le_cancel_iff [of 1 x] by simp
  1298 
  1299 lemma ln_ge_zero_iff [simp]: "0 < x \<Longrightarrow> 0 \<le> ln x \<longleftrightarrow> 1 \<le> x"
  1300   using ln_le_cancel_iff [of 1 x] by simp
  1301 
  1302 lemma ln_less_zero_iff [simp]: "0 < x \<Longrightarrow> ln x < 0 \<longleftrightarrow> x < 1"
  1303   using ln_less_cancel_iff [of x 1] by simp
  1304 
  1305 lemma ln_gt_zero: "1 < x \<Longrightarrow> 0 < ln x"
  1306   using ln_less_cancel_iff [of 1 x] by simp
  1307 
  1308 lemma ln_gt_zero_imp_gt_one: "0 < ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 < x"
  1309   using ln_less_cancel_iff [of 1 x] by simp
  1310 
  1311 lemma ln_gt_zero_iff [simp]: "0 < x \<Longrightarrow> 0 < ln x \<longleftrightarrow> 1 < x"
  1312   using ln_less_cancel_iff [of 1 x] by simp
  1313 
  1314 lemma ln_eq_zero_iff [simp]: "0 < x \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1"
  1315   using ln_inj_iff [of x 1] by simp
  1316 
  1317 lemma ln_less_zero: "0 < x \<Longrightarrow> x < 1 \<Longrightarrow> ln x < 0"
  1318   by simp
  1319 
  1320 lemma isCont_ln: "0 < x \<Longrightarrow> isCont ln x"
  1321   apply (subgoal_tac "isCont ln (exp (ln x))", simp)
  1322   apply (rule isCont_inverse_function [where f=exp], simp_all)
  1323   done
  1324 
  1325 lemma tendsto_ln [tendsto_intros]:
  1326   "(f ---> a) F \<Longrightarrow> 0 < a \<Longrightarrow> ((\<lambda>x. ln (f x)) ---> ln a) F"
  1327   by (rule isCont_tendsto_compose [OF isCont_ln])
  1328 
  1329 lemma continuous_ln:
  1330   "continuous F f \<Longrightarrow> 0 < f (Lim F (\<lambda>x. x)) \<Longrightarrow> continuous F (\<lambda>x. ln (f x))"
  1331   unfolding continuous_def by (rule tendsto_ln)
  1332 
  1333 lemma isCont_ln' [continuous_intros]:
  1334   "continuous (at x) f \<Longrightarrow> 0 < f x \<Longrightarrow> continuous (at x) (\<lambda>x. ln (f x))"
  1335   unfolding continuous_at by (rule tendsto_ln)
  1336 
  1337 lemma continuous_within_ln [continuous_intros]:
  1338   "continuous (at x within s) f \<Longrightarrow> 0 < f x \<Longrightarrow> continuous (at x within s) (\<lambda>x. ln (f x))"
  1339   unfolding continuous_within by (rule tendsto_ln)
  1340 
  1341 lemma continuous_on_ln [continuous_on_intros]:
  1342   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. 0 < f x) \<Longrightarrow> continuous_on s (\<lambda>x. ln (f x))"
  1343   unfolding continuous_on_def by (auto intro: tendsto_ln)
  1344 
  1345 lemma DERIV_ln: "0 < x \<Longrightarrow> DERIV ln x :> inverse x"
  1346   apply (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
  1347   apply (erule DERIV_cong [OF DERIV_exp exp_ln])
  1348   apply (simp_all add: abs_if isCont_ln)
  1349   done
  1350 
  1351 lemma DERIV_ln_divide: "0 < x \<Longrightarrow> DERIV ln x :> 1 / x"
  1352   by (rule DERIV_ln[THEN DERIV_cong], simp, simp add: divide_inverse)
  1353 
  1354 declare DERIV_ln_divide[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  1355 
  1356 lemma ln_series:
  1357   assumes "0 < x" and "x < 2"
  1358   shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
  1359   (is "ln x = suminf (?f (x - 1))")
  1360 proof -
  1361   let ?f' = "\<lambda>x n. (-1)^n * (x - 1)^n"
  1362 
  1363   have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
  1364   proof (rule DERIV_isconst3[where x=x])
  1365     fix x :: real
  1366     assume "x \<in> {0 <..< 2}"
  1367     hence "0 < x" and "x < 2" by auto
  1368     have "norm (1 - x) < 1"
  1369       using `0 < x` and `x < 2` by auto
  1370     have "1 / x = 1 / (1 - (1 - x))" by auto
  1371     also have "\<dots> = (\<Sum> n. (1 - x)^n)"
  1372       using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
  1373     also have "\<dots> = suminf (?f' x)"
  1374       unfolding power_mult_distrib[symmetric]
  1375       by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
  1376     finally have "DERIV ln x :> suminf (?f' x)"
  1377       using DERIV_ln[OF `0 < x`] unfolding divide_inverse by auto
  1378     moreover
  1379     have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
  1380     have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :>
  1381       (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
  1382     proof (rule DERIV_power_series')
  1383       show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1"
  1384         using `0 < x` `x < 2` by auto
  1385       fix x :: real
  1386       assume "x \<in> {- 1<..<1}"
  1387       hence "norm (-x) < 1" by auto
  1388       show "summable (\<lambda>n. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
  1389         unfolding One_nat_def
  1390         by (auto simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
  1391     qed
  1392     hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)"
  1393       unfolding One_nat_def by auto
  1394     hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)"
  1395       unfolding DERIV_iff repos .
  1396     ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
  1397       by (rule DERIV_diff)
  1398     thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
  1399   qed (auto simp add: assms)
  1400   thus ?thesis by auto
  1401 qed
  1402 
  1403 lemma exp_first_two_terms: "exp x = 1 + x + (\<Sum> n. inverse(fact (n+2)) * (x ^ (n+2)))"
  1404 proof -
  1405   have "exp x = suminf (\<lambda>n. inverse(fact n) * (x ^ n))"
  1406     by (simp add: exp_def)
  1407   also from summable_exp have "... = (\<Sum> n::nat = 0 ..< 2. inverse(fact n) * (x ^ n)) +
  1408       (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2)))" (is "_ = ?a + _")
  1409     by (rule suminf_split_initial_segment)
  1410   also have "?a = 1 + x"
  1411     by (simp add: numeral_2_eq_2)
  1412   finally show ?thesis .
  1413 qed
  1414 
  1415 lemma exp_bound: "0 <= (x::real) \<Longrightarrow> x <= 1 \<Longrightarrow> exp x <= 1 + x + x\<^sup>2"
  1416 proof -
  1417   assume a: "0 <= x"
  1418   assume b: "x <= 1"
  1419   {
  1420     fix n :: nat
  1421     have "2 * 2 ^ n \<le> fact (n + 2)"
  1422       by (induct n) simp_all
  1423     hence "real ((2::nat) * 2 ^ n) \<le> real (fact (n + 2))"
  1424       by (simp only: real_of_nat_le_iff)
  1425     hence "2 * 2 ^ n \<le> real (fact (n + 2))"
  1426       by simp
  1427     hence "inverse (fact (n + 2)) \<le> inverse (2 * 2 ^ n)"
  1428       by (rule le_imp_inverse_le) simp
  1429     hence "inverse (fact (n + 2)) \<le> 1/2 * (1/2)^n"
  1430       by (simp add: power_inverse)
  1431     hence "inverse (fact (n + 2)) * (x^n * x\<^sup>2) \<le> 1/2 * (1/2)^n * (1 * x\<^sup>2)"
  1432       by (rule mult_mono)
  1433         (rule mult_mono, simp_all add: power_le_one a b mult_nonneg_nonneg)
  1434     hence "inverse (fact (n + 2)) * x ^ (n + 2) \<le> (x\<^sup>2/2) * ((1/2)^n)"
  1435       unfolding power_add by (simp add: mult_ac del: fact_Suc) }
  1436   note aux1 = this
  1437   have "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums (x\<^sup>2 / 2 * (1 / (1 - 1 / 2)))"
  1438     by (intro sums_mult geometric_sums, simp)
  1439   hence aux2: "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums x\<^sup>2"
  1440     by simp
  1441   have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <= x\<^sup>2"
  1442   proof -
  1443     have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <=
  1444         suminf (\<lambda>n. (x\<^sup>2/2) * ((1/2)^n))"
  1445       apply (rule summable_le)
  1446       apply (rule allI, rule aux1)
  1447       apply (rule summable_exp [THEN summable_ignore_initial_segment])
  1448       by (rule sums_summable, rule aux2)
  1449     also have "... = x\<^sup>2"
  1450       by (rule sums_unique [THEN sym], rule aux2)
  1451     finally show ?thesis .
  1452   qed
  1453   thus ?thesis unfolding exp_first_two_terms by auto
  1454 qed
  1455 
  1456 lemma ln_one_minus_pos_upper_bound: "0 <= x \<Longrightarrow> x < 1 \<Longrightarrow> ln (1 - x) <= - x"
  1457 proof -
  1458   assume a: "0 <= (x::real)" and b: "x < 1"
  1459   have "(1 - x) * (1 + x + x\<^sup>2) = (1 - x^3)"
  1460     by (simp add: algebra_simps power2_eq_square power3_eq_cube)
  1461   also have "... <= 1"
  1462     by (auto simp add: a)
  1463   finally have "(1 - x) * (1 + x + x\<^sup>2) <= 1" .
  1464   moreover have c: "0 < 1 + x + x\<^sup>2"
  1465     by (simp add: add_pos_nonneg a)
  1466   ultimately have "1 - x <= 1 / (1 + x + x\<^sup>2)"
  1467     by (elim mult_imp_le_div_pos)
  1468   also have "... <= 1 / exp x"
  1469     apply (rule divide_left_mono)
  1470     apply (rule exp_bound, rule a)
  1471     apply (rule b [THEN less_imp_le])
  1472     apply simp
  1473     apply (rule mult_pos_pos)
  1474     apply (rule c)
  1475     apply simp
  1476     done
  1477   also have "... = exp (-x)"
  1478     by (auto simp add: exp_minus divide_inverse)
  1479   finally have "1 - x <= exp (- x)" .
  1480   also have "1 - x = exp (ln (1 - x))"
  1481   proof -
  1482     have "0 < 1 - x"
  1483       by (insert b, auto)
  1484     thus ?thesis
  1485       by (auto simp only: exp_ln_iff [THEN sym])
  1486   qed
  1487   finally have "exp (ln (1 - x)) <= exp (- x)" .
  1488   thus ?thesis by (auto simp only: exp_le_cancel_iff)
  1489 qed
  1490 
  1491 lemma exp_ge_add_one_self [simp]: "1 + (x::real) <= exp x"
  1492   apply (case_tac "0 <= x")
  1493   apply (erule exp_ge_add_one_self_aux)
  1494   apply (case_tac "x <= -1")
  1495   apply (subgoal_tac "1 + x <= 0")
  1496   apply (erule order_trans)
  1497   apply simp
  1498   apply simp
  1499   apply (subgoal_tac "1 + x = exp(ln (1 + x))")
  1500   apply (erule ssubst)
  1501   apply (subst exp_le_cancel_iff)
  1502   apply (subgoal_tac "ln (1 - (- x)) <= - (- x)")
  1503   apply simp
  1504   apply (rule ln_one_minus_pos_upper_bound)
  1505   apply auto
  1506 done
  1507 
  1508 lemma ln_one_plus_pos_lower_bound: "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> x - x\<^sup>2 <= ln (1 + x)"
  1509 proof -
  1510   assume a: "0 <= x" and b: "x <= 1"
  1511   have "exp (x - x\<^sup>2) = exp x / exp (x\<^sup>2)"
  1512     by (rule exp_diff)
  1513   also have "... <= (1 + x + x\<^sup>2) / exp (x \<^sup>2)"
  1514     apply (rule divide_right_mono)
  1515     apply (rule exp_bound)
  1516     apply (rule a, rule b)
  1517     apply simp
  1518     done
  1519   also have "... <= (1 + x + x\<^sup>2) / (1 + x\<^sup>2)"
  1520     apply (rule divide_left_mono)
  1521     apply (simp add: exp_ge_add_one_self_aux)
  1522     apply (simp add: a)
  1523     apply (simp add: mult_pos_pos add_pos_nonneg)
  1524     done
  1525   also from a have "... <= 1 + x"
  1526     by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
  1527   finally have "exp (x - x\<^sup>2) <= 1 + x" .
  1528   also have "... = exp (ln (1 + x))"
  1529   proof -
  1530     from a have "0 < 1 + x" by auto
  1531     thus ?thesis
  1532       by (auto simp only: exp_ln_iff [THEN sym])
  1533   qed
  1534   finally have "exp (x - x\<^sup>2) <= exp (ln (1 + x))" .
  1535   thus ?thesis by (auto simp only: exp_le_cancel_iff)
  1536 qed
  1537 
  1538 lemma aux5: "x < 1 \<Longrightarrow> ln(1 - x) = - ln(1 + x / (1 - x))"
  1539 proof -
  1540   assume a: "x < 1"
  1541   have "ln(1 - x) = - ln(1 / (1 - x))"
  1542   proof -
  1543     have "ln(1 - x) = - (- ln (1 - x))"
  1544       by auto
  1545     also have "- ln(1 - x) = ln 1 - ln(1 - x)"
  1546       by simp
  1547     also have "... = ln(1 / (1 - x))"
  1548       apply (rule ln_div [THEN sym])
  1549       using a apply auto
  1550       done
  1551     finally show ?thesis .
  1552   qed
  1553   also have " 1 / (1 - x) = 1 + x / (1 - x)" using a by(simp add:field_simps)
  1554   finally show ?thesis .
  1555 qed
  1556 
  1557 lemma ln_one_minus_pos_lower_bound:
  1558   "0 <= x \<Longrightarrow> x <= (1 / 2) \<Longrightarrow> - x - 2 * x\<^sup>2 <= ln (1 - x)"
  1559 proof -
  1560   assume a: "0 <= x" and b: "x <= (1 / 2)"
  1561   from b have c: "x < 1" by auto
  1562   then have "ln (1 - x) = - ln (1 + x / (1 - x))"
  1563     by (rule aux5)
  1564   also have "- (x / (1 - x)) <= ..."
  1565   proof -
  1566     have "ln (1 + x / (1 - x)) <= x / (1 - x)"
  1567       apply (rule ln_add_one_self_le_self)
  1568       apply (rule divide_nonneg_pos)
  1569       using a c apply auto
  1570       done
  1571     thus ?thesis
  1572       by auto
  1573   qed
  1574   also have "- (x / (1 - x)) = -x / (1 - x)"
  1575     by auto
  1576   finally have d: "- x / (1 - x) <= ln (1 - x)" .
  1577   have "0 < 1 - x" using a b by simp
  1578   hence e: "-x - 2 * x\<^sup>2 <= - x / (1 - x)"
  1579     using mult_right_le_one_le[of "x*x" "2*x"] a b
  1580     by (simp add: field_simps power2_eq_square)
  1581   from e d show "- x - 2 * x\<^sup>2 <= ln (1 - x)"
  1582     by (rule order_trans)
  1583 qed
  1584 
  1585 lemma ln_add_one_self_le_self2: "-1 < x \<Longrightarrow> ln(1 + x) <= x"
  1586   apply (subgoal_tac "ln (1 + x) \<le> ln (exp x)", simp)
  1587   apply (subst ln_le_cancel_iff)
  1588   apply auto
  1589   done
  1590 
  1591 lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
  1592   "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> abs(ln (1 + x) - x) <= x\<^sup>2"
  1593 proof -
  1594   assume x: "0 <= x"
  1595   assume x1: "x <= 1"
  1596   from x have "ln (1 + x) <= x"
  1597     by (rule ln_add_one_self_le_self)
  1598   then have "ln (1 + x) - x <= 0"
  1599     by simp
  1600   then have "abs(ln(1 + x) - x) = - (ln(1 + x) - x)"
  1601     by (rule abs_of_nonpos)
  1602   also have "... = x - ln (1 + x)"
  1603     by simp
  1604   also have "... <= x\<^sup>2"
  1605   proof -
  1606     from x x1 have "x - x\<^sup>2 <= ln (1 + x)"
  1607       by (intro ln_one_plus_pos_lower_bound)
  1608     thus ?thesis
  1609       by simp
  1610   qed
  1611   finally show ?thesis .
  1612 qed
  1613 
  1614 lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
  1615   "-(1 / 2) <= x \<Longrightarrow> x <= 0 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
  1616 proof -
  1617   assume a: "-(1 / 2) <= x"
  1618   assume b: "x <= 0"
  1619   have "abs(ln (1 + x) - x) = x - ln(1 - (-x))"
  1620     apply (subst abs_of_nonpos)
  1621     apply simp
  1622     apply (rule ln_add_one_self_le_self2)
  1623     using a apply auto
  1624     done
  1625   also have "... <= 2 * x\<^sup>2"
  1626     apply (subgoal_tac "- (-x) - 2 * (-x)\<^sup>2 <= ln (1 - (-x))")
  1627     apply (simp add: algebra_simps)
  1628     apply (rule ln_one_minus_pos_lower_bound)
  1629     using a b apply auto
  1630     done
  1631   finally show ?thesis .
  1632 qed
  1633 
  1634 lemma abs_ln_one_plus_x_minus_x_bound:
  1635     "abs x <= 1 / 2 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
  1636   apply (case_tac "0 <= x")
  1637   apply (rule order_trans)
  1638   apply (rule abs_ln_one_plus_x_minus_x_bound_nonneg)
  1639   apply auto
  1640   apply (rule abs_ln_one_plus_x_minus_x_bound_nonpos)
  1641   apply auto
  1642   done
  1643 
  1644 lemma ln_x_over_x_mono: "exp 1 <= x \<Longrightarrow> x <= y \<Longrightarrow> (ln y / y) <= (ln x / x)"
  1645 proof -
  1646   assume x: "exp 1 <= x" "x <= y"
  1647   moreover have "0 < exp (1::real)" by simp
  1648   ultimately have a: "0 < x" and b: "0 < y"
  1649     by (fast intro: less_le_trans order_trans)+
  1650   have "x * ln y - x * ln x = x * (ln y - ln x)"
  1651     by (simp add: algebra_simps)
  1652   also have "... = x * ln(y / x)"
  1653     by (simp only: ln_div a b)
  1654   also have "y / x = (x + (y - x)) / x"
  1655     by simp
  1656   also have "... = 1 + (y - x) / x"
  1657     using x a by (simp add: field_simps)
  1658   also have "x * ln(1 + (y - x) / x) <= x * ((y - x) / x)"
  1659     apply (rule mult_left_mono)
  1660     apply (rule ln_add_one_self_le_self)
  1661     apply (rule divide_nonneg_pos)
  1662     using x a apply simp_all
  1663     done
  1664   also have "... = y - x" using a by simp
  1665   also have "... = (y - x) * ln (exp 1)" by simp
  1666   also have "... <= (y - x) * ln x"
  1667     apply (rule mult_left_mono)
  1668     apply (subst ln_le_cancel_iff)
  1669     apply fact
  1670     apply (rule a)
  1671     apply (rule x)
  1672     using x apply simp
  1673     done
  1674   also have "... = y * ln x - x * ln x"
  1675     by (rule left_diff_distrib)
  1676   finally have "x * ln y <= y * ln x"
  1677     by arith
  1678   then have "ln y <= (y * ln x) / x" using a by (simp add: field_simps)
  1679   also have "... = y * (ln x / x)" by simp
  1680   finally show ?thesis using b by (simp add: field_simps)
  1681 qed
  1682 
  1683 lemma ln_le_minus_one: "0 < x \<Longrightarrow> ln x \<le> x - 1"
  1684   using exp_ge_add_one_self[of "ln x"] by simp
  1685 
  1686 lemma ln_eq_minus_one:
  1687   assumes "0 < x" "ln x = x - 1"
  1688   shows "x = 1"
  1689 proof -
  1690   let ?l = "\<lambda>y. ln y - y + 1"
  1691   have D: "\<And>x. 0 < x \<Longrightarrow> DERIV ?l x :> (1 / x - 1)"
  1692     by (auto intro!: DERIV_intros)
  1693 
  1694   show ?thesis
  1695   proof (cases rule: linorder_cases)
  1696     assume "x < 1"
  1697     from dense[OF `x < 1`] obtain a where "x < a" "a < 1" by blast
  1698     from `x < a` have "?l x < ?l a"
  1699     proof (rule DERIV_pos_imp_increasing, safe)
  1700       fix y
  1701       assume "x \<le> y" "y \<le> a"
  1702       with `0 < x` `a < 1` have "0 < 1 / y - 1" "0 < y"
  1703         by (auto simp: field_simps)
  1704       with D show "\<exists>z. DERIV ?l y :> z \<and> 0 < z"
  1705         by auto
  1706     qed
  1707     also have "\<dots> \<le> 0"
  1708       using ln_le_minus_one `0 < x` `x < a` by (auto simp: field_simps)
  1709     finally show "x = 1" using assms by auto
  1710   next
  1711     assume "1 < x"
  1712     from dense[OF this] obtain a where "1 < a" "a < x" by blast
  1713     from `a < x` have "?l x < ?l a"
  1714     proof (rule DERIV_neg_imp_decreasing, safe)
  1715       fix y
  1716       assume "a \<le> y" "y \<le> x"
  1717       with `1 < a` have "1 / y - 1 < 0" "0 < y"
  1718         by (auto simp: field_simps)
  1719       with D show "\<exists>z. DERIV ?l y :> z \<and> z < 0"
  1720         by blast
  1721     qed
  1722     also have "\<dots> \<le> 0"
  1723       using ln_le_minus_one `1 < a` by (auto simp: field_simps)
  1724     finally show "x = 1" using assms by auto
  1725   next
  1726     assume "x = 1"
  1727     then show ?thesis by simp
  1728   qed
  1729 qed
  1730 
  1731 lemma exp_at_bot: "(exp ---> (0::real)) at_bot"
  1732   unfolding tendsto_Zfun_iff
  1733 proof (rule ZfunI, simp add: eventually_at_bot_dense)
  1734   fix r :: real assume "0 < r"
  1735   {
  1736     fix x
  1737     assume "x < ln r"
  1738     then have "exp x < exp (ln r)"
  1739       by simp
  1740     with `0 < r` have "exp x < r"
  1741       by simp
  1742   }
  1743   then show "\<exists>k. \<forall>n<k. exp n < r" by auto
  1744 qed
  1745 
  1746 lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
  1747   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="ln"])
  1748      (auto intro: eventually_gt_at_top)
  1749 
  1750 lemma ln_at_0: "LIM x at_right 0. ln x :> at_bot"
  1751   by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
  1752      (auto simp: eventually_at_filter)
  1753 
  1754 lemma ln_at_top: "LIM x at_top. ln x :> at_top"
  1755   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
  1756      (auto intro: eventually_gt_at_top)
  1757 
  1758 lemma tendsto_power_div_exp_0: "((\<lambda>x. x ^ k / exp x) ---> (0::real)) at_top"
  1759 proof (induct k)
  1760   case 0
  1761   show "((\<lambda>x. x ^ 0 / exp x) ---> (0::real)) at_top"
  1762     by (simp add: inverse_eq_divide[symmetric])
  1763        (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
  1764               at_top_le_at_infinity order_refl)
  1765 next
  1766   case (Suc k)
  1767   show ?case
  1768   proof (rule lhospital_at_top_at_top)
  1769     show "eventually (\<lambda>x. DERIV (\<lambda>x. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
  1770       by eventually_elim (intro DERIV_intros, simp, simp)
  1771     show "eventually (\<lambda>x. DERIV exp x :> exp x) at_top"
  1772       by eventually_elim (auto intro!: DERIV_intros)
  1773     show "eventually (\<lambda>x. exp x \<noteq> 0) at_top"
  1774       by auto
  1775     from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
  1776     show "((\<lambda>x. real (Suc k) * x ^ k / exp x) ---> 0) at_top"
  1777       by simp
  1778   qed (rule exp_at_top)
  1779 qed
  1780 
  1781 
  1782 definition powr :: "[real,real] => real"  (infixr "powr" 80)
  1783   -- {*exponentation with real exponent*}
  1784   where "x powr a = exp(a * ln x)"
  1785 
  1786 definition log :: "[real,real] => real"
  1787   -- {*logarithm of @{term x} to base @{term a}*}
  1788   where "log a x = ln x / ln a"
  1789 
  1790 
  1791 lemma tendsto_log [tendsto_intros]:
  1792   "\<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"
  1793   unfolding log_def by (intro tendsto_intros) auto
  1794 
  1795 lemma continuous_log:
  1796   assumes "continuous F f"
  1797     and "continuous F g"
  1798     and "0 < f (Lim F (\<lambda>x. x))"
  1799     and "f (Lim F (\<lambda>x. x)) \<noteq> 1"
  1800     and "0 < g (Lim F (\<lambda>x. x))"
  1801   shows "continuous F (\<lambda>x. log (f x) (g x))"
  1802   using assms unfolding continuous_def by (rule tendsto_log)
  1803 
  1804 lemma continuous_at_within_log[continuous_intros]:
  1805   assumes "continuous (at a within s) f"
  1806     and "continuous (at a within s) g"
  1807     and "0 < f a"
  1808     and "f a \<noteq> 1"
  1809     and "0 < g a"
  1810   shows "continuous (at a within s) (\<lambda>x. log (f x) (g x))"
  1811   using assms unfolding continuous_within by (rule tendsto_log)
  1812 
  1813 lemma isCont_log[continuous_intros, simp]:
  1814   assumes "isCont f a" "isCont g a" "0 < f a" "f a \<noteq> 1" "0 < g a"
  1815   shows "isCont (\<lambda>x. log (f x) (g x)) a"
  1816   using assms unfolding continuous_at by (rule tendsto_log)
  1817 
  1818 lemma continuous_on_log[continuous_on_intros]:
  1819   assumes "continuous_on s f" "continuous_on s g"
  1820     and "\<forall>x\<in>s. 0 < f x" "\<forall>x\<in>s. f x \<noteq> 1" "\<forall>x\<in>s. 0 < g x"
  1821   shows "continuous_on s (\<lambda>x. log (f x) (g x))"
  1822   using assms unfolding continuous_on_def by (fast intro: tendsto_log)
  1823 
  1824 lemma powr_one_eq_one [simp]: "1 powr a = 1"
  1825   by (simp add: powr_def)
  1826 
  1827 lemma powr_zero_eq_one [simp]: "x powr 0 = 1"
  1828   by (simp add: powr_def)
  1829 
  1830 lemma powr_one_gt_zero_iff [simp]: "(x powr 1 = x) = (0 < x)"
  1831   by (simp add: powr_def)
  1832 declare powr_one_gt_zero_iff [THEN iffD2, simp]
  1833 
  1834 lemma powr_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x * y) powr a = (x powr a) * (y powr a)"
  1835   by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)
  1836 
  1837 lemma powr_gt_zero [simp]: "0 < x powr a"
  1838   by (simp add: powr_def)
  1839 
  1840 lemma powr_ge_pzero [simp]: "0 <= x powr y"
  1841   by (rule order_less_imp_le, rule powr_gt_zero)
  1842 
  1843 lemma powr_not_zero [simp]: "x powr a \<noteq> 0"
  1844   by (simp add: powr_def)
  1845 
  1846 lemma powr_divide: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
  1847   apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
  1848   apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
  1849   done
  1850 
  1851 lemma powr_divide2: "x powr a / x powr b = x powr (a - b)"
  1852   apply (simp add: powr_def)
  1853   apply (subst exp_diff [THEN sym])
  1854   apply (simp add: left_diff_distrib)
  1855   done
  1856 
  1857 lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
  1858   by (simp add: powr_def exp_add [symmetric] distrib_right)
  1859 
  1860 lemma powr_mult_base: "0 < x \<Longrightarrow>x * x powr y = x powr (1 + y)"
  1861   using assms by (auto simp: powr_add)
  1862 
  1863 lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
  1864   by (simp add: powr_def)
  1865 
  1866 lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
  1867   by (simp add: powr_powr mult_commute)
  1868 
  1869 lemma powr_minus: "x powr (-a) = inverse (x powr a)"
  1870   by (simp add: powr_def exp_minus [symmetric])
  1871 
  1872 lemma powr_minus_divide: "x powr (-a) = 1/(x powr a)"
  1873   by (simp add: divide_inverse powr_minus)
  1874 
  1875 lemma powr_less_mono: "a < b \<Longrightarrow> 1 < x \<Longrightarrow> x powr a < x powr b"
  1876   by (simp add: powr_def)
  1877 
  1878 lemma powr_less_cancel: "x powr a < x powr b \<Longrightarrow> 1 < x \<Longrightarrow> a < b"
  1879   by (simp add: powr_def)
  1880 
  1881 lemma powr_less_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a < x powr b) = (a < b)"
  1882   by (blast intro: powr_less_cancel powr_less_mono)
  1883 
  1884 lemma powr_le_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a \<le> x powr b) = (a \<le> b)"
  1885   by (simp add: linorder_not_less [symmetric])
  1886 
  1887 lemma log_ln: "ln x = log (exp(1)) x"
  1888   by (simp add: log_def)
  1889 
  1890 lemma DERIV_log:
  1891   assumes "x > 0"
  1892   shows "DERIV (\<lambda>y. log b y) x :> 1 / (ln b * x)"
  1893 proof -
  1894   def lb \<equiv> "1 / ln b"
  1895   moreover have "DERIV (\<lambda>y. lb * ln y) x :> lb / x"
  1896     using `x > 0` by (auto intro!: DERIV_intros)
  1897   ultimately show ?thesis
  1898     by (simp add: log_def)
  1899 qed
  1900 
  1901 lemmas DERIV_log[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  1902 
  1903 lemma powr_log_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> a powr (log a x) = x"
  1904   by (simp add: powr_def log_def)
  1905 
  1906 lemma log_powr_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a (a powr y) = y"
  1907   by (simp add: log_def powr_def)
  1908 
  1909 lemma log_mult:
  1910   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow>
  1911     log a (x * y) = log a x + log a y"
  1912   by (simp add: log_def ln_mult divide_inverse distrib_right)
  1913 
  1914 lemma log_eq_div_ln_mult_log:
  1915   "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow>
  1916     log a x = (ln b/ln a) * log b x"
  1917   by (simp add: log_def divide_inverse)
  1918 
  1919 text{*Base 10 logarithms*}
  1920 lemma log_base_10_eq1: "0 < x \<Longrightarrow> log 10 x = (ln (exp 1) / ln 10) * ln x"
  1921   by (simp add: log_def)
  1922 
  1923 lemma log_base_10_eq2: "0 < x \<Longrightarrow> log 10 x = (log 10 (exp 1)) * ln x"
  1924   by (simp add: log_def)
  1925 
  1926 lemma log_one [simp]: "log a 1 = 0"
  1927   by (simp add: log_def)
  1928 
  1929 lemma log_eq_one [simp]: "[| 0 < a; a \<noteq> 1 |] ==> log a a = 1"
  1930   by (simp add: log_def)
  1931 
  1932 lemma log_inverse: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log a (inverse x) = - log a x"
  1933   apply (rule_tac a1 = "log a x" in add_left_cancel [THEN iffD1])
  1934   apply (simp add: log_mult [symmetric])
  1935   done
  1936 
  1937 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"
  1938   by (simp add: log_mult divide_inverse log_inverse)
  1939 
  1940 lemma log_less_cancel_iff [simp]:
  1941   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x < log a y \<longleftrightarrow> x < y"
  1942   apply safe
  1943   apply (rule_tac [2] powr_less_cancel)
  1944   apply (drule_tac a = "log a x" in powr_less_mono, auto)
  1945   done
  1946 
  1947 lemma log_inj:
  1948   assumes "1 < b"
  1949   shows "inj_on (log b) {0 <..}"
  1950 proof (rule inj_onI, simp)
  1951   fix x y
  1952   assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
  1953   show "x = y"
  1954   proof (cases rule: linorder_cases)
  1955     assume "x = y"
  1956     then show ?thesis by simp
  1957   next
  1958     assume "x < y" hence "log b x < log b y"
  1959       using log_less_cancel_iff[OF `1 < b`] pos by simp
  1960     then show ?thesis using * by simp
  1961   next
  1962     assume "y < x" hence "log b y < log b x"
  1963       using log_less_cancel_iff[OF `1 < b`] pos by simp
  1964     then show ?thesis using * by simp
  1965   qed
  1966 qed
  1967 
  1968 lemma log_le_cancel_iff [simp]:
  1969   "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (log a x \<le> log a y) = (x \<le> y)"
  1970   by (simp add: linorder_not_less [symmetric])
  1971 
  1972 lemma zero_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < log a x \<longleftrightarrow> 1 < x"
  1973   using log_less_cancel_iff[of a 1 x] by simp
  1974 
  1975 lemma zero_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 \<le> log a x \<longleftrightarrow> 1 \<le> x"
  1976   using log_le_cancel_iff[of a 1 x] by simp
  1977 
  1978 lemma log_less_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 0 \<longleftrightarrow> x < 1"
  1979   using log_less_cancel_iff[of a x 1] by simp
  1980 
  1981 lemma log_le_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 0 \<longleftrightarrow> x \<le> 1"
  1982   using log_le_cancel_iff[of a x 1] by simp
  1983 
  1984 lemma one_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 < log a x \<longleftrightarrow> a < x"
  1985   using log_less_cancel_iff[of a a x] by simp
  1986 
  1987 lemma one_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> log a x \<longleftrightarrow> a \<le> x"
  1988   using log_le_cancel_iff[of a a x] by simp
  1989 
  1990 lemma log_less_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 1 \<longleftrightarrow> x < a"
  1991   using log_less_cancel_iff[of a x a] by simp
  1992 
  1993 lemma log_le_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 1 \<longleftrightarrow> x \<le> a"
  1994   using log_le_cancel_iff[of a x a] by simp
  1995 
  1996 lemma powr_realpow: "0 < x ==> x powr (real n) = x^n"
  1997   apply (induct n)
  1998   apply simp
  1999   apply (subgoal_tac "real(Suc n) = real n + 1")
  2000   apply (erule ssubst)
  2001   apply (subst powr_add, simp, simp)
  2002   done
  2003 
  2004 lemma powr_realpow_numeral: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x^(numeral n)"
  2005   unfolding real_of_nat_numeral[symmetric] by (rule powr_realpow)
  2006 
  2007 lemma powr_realpow2: "0 <= x ==> 0 < n ==> x^n = (if (x = 0) then 0 else x powr (real n))"
  2008   apply (case_tac "x = 0", simp, simp)
  2009   apply (rule powr_realpow [THEN sym], simp)
  2010   done
  2011 
  2012 lemma powr_int:
  2013   assumes "x > 0"
  2014   shows "x powr i = (if i \<ge> 0 then x ^ nat i else 1 / x ^ nat (-i))"
  2015 proof (cases "i < 0")
  2016   case True
  2017   have r: "x powr i = 1 / x powr (-i)" by (simp add: powr_minus field_simps)
  2018   show ?thesis using `i < 0` `x > 0` by (simp add: r field_simps powr_realpow[symmetric])
  2019 next
  2020   case False
  2021   then show ?thesis by (simp add: assms powr_realpow[symmetric])
  2022 qed
  2023 
  2024 lemma powr_numeral: "0 < x \<Longrightarrow> x powr numeral n = x^numeral n"
  2025   using powr_realpow[of x "numeral n"] by simp
  2026 
  2027 lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr neg_numeral n = 1 / x^numeral n"
  2028   using powr_int[of x "neg_numeral n"] by simp
  2029 
  2030 lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
  2031   by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
  2032 
  2033 lemma ln_powr: "0 < x ==> 0 < y ==> ln(x powr y) = y * ln x"
  2034   unfolding powr_def by simp
  2035 
  2036 lemma log_powr: "0 < x ==> 0 \<le> y ==> log b (x powr y) = y * log b x"
  2037   apply (cases "y = 0")
  2038   apply force
  2039   apply (auto simp add: log_def ln_powr field_simps)
  2040   done
  2041 
  2042 lemma log_nat_power: "0 < x ==> log b (x^n) = real n * log b x"
  2043   apply (subst powr_realpow [symmetric])
  2044   apply (auto simp add: log_powr)
  2045   done
  2046 
  2047 lemma ln_bound: "1 <= x ==> ln x <= x"
  2048   apply (subgoal_tac "ln(1 + (x - 1)) <= x - 1")
  2049   apply simp
  2050   apply (rule ln_add_one_self_le_self, simp)
  2051   done
  2052 
  2053 lemma powr_mono: "a <= b ==> 1 <= x ==> x powr a <= x powr b"
  2054   apply (cases "x = 1", simp)
  2055   apply (cases "a = b", simp)
  2056   apply (rule order_less_imp_le)
  2057   apply (rule powr_less_mono, auto)
  2058   done
  2059 
  2060 lemma ge_one_powr_ge_zero: "1 <= x ==> 0 <= a ==> 1 <= x powr a"
  2061   apply (subst powr_zero_eq_one [THEN sym])
  2062   apply (rule powr_mono, assumption+)
  2063   done
  2064 
  2065 lemma powr_less_mono2: "0 < a ==> 0 < x ==> x < y ==> x powr a < y powr a"
  2066   apply (unfold powr_def)
  2067   apply (rule exp_less_mono)
  2068   apply (rule mult_strict_left_mono)
  2069   apply (subst ln_less_cancel_iff, assumption)
  2070   apply (rule order_less_trans)
  2071   prefer 2
  2072   apply assumption+
  2073   done
  2074 
  2075 lemma powr_less_mono2_neg: "a < 0 ==> 0 < x ==> x < y ==> y powr a < x powr a"
  2076   apply (unfold powr_def)
  2077   apply (rule exp_less_mono)
  2078   apply (rule mult_strict_left_mono_neg)
  2079   apply (subst ln_less_cancel_iff)
  2080   apply assumption
  2081   apply (rule order_less_trans)
  2082   prefer 2
  2083   apply assumption+
  2084   done
  2085 
  2086 lemma powr_mono2: "0 <= a ==> 0 < x ==> x <= y ==> x powr a <= y powr a"
  2087   apply (case_tac "a = 0", simp)
  2088   apply (case_tac "x = y", simp)
  2089   apply (rule order_less_imp_le)
  2090   apply (rule powr_less_mono2, auto)
  2091   done
  2092 
  2093 lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
  2094   unfolding powr_def exp_inj_iff by simp
  2095 
  2096 lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
  2097   apply (rule mult_imp_le_div_pos)
  2098   apply (assumption)
  2099   apply (subst mult_commute)
  2100   apply (subst ln_powr [THEN sym])
  2101   apply auto
  2102   apply (rule ln_bound)
  2103   apply (erule ge_one_powr_ge_zero)
  2104   apply (erule order_less_imp_le)
  2105   done
  2106 
  2107 lemma ln_powr_bound2:
  2108   assumes "1 < x" and "0 < a"
  2109   shows "(ln x) powr a <= (a powr a) * x"
  2110 proof -
  2111   from assms have "ln x <= (x powr (1 / a)) / (1 / a)"
  2112     apply (intro ln_powr_bound)
  2113     apply (erule order_less_imp_le)
  2114     apply (rule divide_pos_pos)
  2115     apply simp_all
  2116     done
  2117   also have "... = a * (x powr (1 / a))"
  2118     by simp
  2119   finally have "(ln x) powr a <= (a * (x powr (1 / a))) powr a"
  2120     apply (intro powr_mono2)
  2121     apply (rule order_less_imp_le, rule assms)
  2122     apply (rule ln_gt_zero)
  2123     apply (rule assms)
  2124     apply assumption
  2125     done
  2126   also have "... = (a powr a) * ((x powr (1 / a)) powr a)"
  2127     apply (rule powr_mult)
  2128     apply (rule assms)
  2129     apply (rule powr_gt_zero)
  2130     done
  2131   also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
  2132     by (rule powr_powr)
  2133   also have "... = x"
  2134     apply simp
  2135     apply (subgoal_tac "a ~= 0")
  2136     using assms apply auto
  2137     done
  2138   finally show ?thesis .
  2139 qed
  2140 
  2141 lemma tendsto_powr [tendsto_intros]:
  2142   "\<lbrakk>(f ---> a) F; (g ---> b) F; 0 < a\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x powr g x) ---> a powr b) F"
  2143   unfolding powr_def by (intro tendsto_intros)
  2144 
  2145 lemma continuous_powr:
  2146   assumes "continuous F f"
  2147     and "continuous F g"
  2148     and "0 < f (Lim F (\<lambda>x. x))"
  2149   shows "continuous F (\<lambda>x. (f x) powr (g x))"
  2150   using assms unfolding continuous_def by (rule tendsto_powr)
  2151 
  2152 lemma continuous_at_within_powr[continuous_intros]:
  2153   assumes "continuous (at a within s) f"
  2154     and "continuous (at a within s) g"
  2155     and "0 < f a"
  2156   shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
  2157   using assms unfolding continuous_within by (rule tendsto_powr)
  2158 
  2159 lemma isCont_powr[continuous_intros, simp]:
  2160   assumes "isCont f a" "isCont g a" "0 < f a"
  2161   shows "isCont (\<lambda>x. (f x) powr g x) a"
  2162   using assms unfolding continuous_at by (rule tendsto_powr)
  2163 
  2164 lemma continuous_on_powr[continuous_on_intros]:
  2165   assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. 0 < f x"
  2166   shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
  2167   using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
  2168 
  2169 (* FIXME: generalize by replacing d by with g x and g ---> d? *)
  2170 lemma tendsto_zero_powrI:
  2171   assumes "eventually (\<lambda>x. 0 < f x ) F" and "(f ---> 0) F"
  2172     and "0 < d"
  2173   shows "((\<lambda>x. f x powr d) ---> 0) F"
  2174 proof (rule tendstoI)
  2175   fix e :: real assume "0 < e"
  2176   def Z \<equiv> "e powr (1 / d)"
  2177   with `0 < e` have "0 < Z" by simp
  2178   with assms have "eventually (\<lambda>x. 0 < f x \<and> dist (f x) 0 < Z) F"
  2179     by (intro eventually_conj tendstoD)
  2180   moreover
  2181   from assms have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> x powr d < Z powr d"
  2182     by (intro powr_less_mono2) (auto simp: dist_real_def)
  2183   with assms `0 < e` have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> dist (x powr d) 0 < e"
  2184     unfolding dist_real_def Z_def by (auto simp: powr_powr)
  2185   ultimately
  2186   show "eventually (\<lambda>x. dist (f x powr d) 0 < e) F" by (rule eventually_elim1)
  2187 qed
  2188 
  2189 lemma tendsto_neg_powr:
  2190   assumes "s < 0"
  2191     and "LIM x F. f x :> at_top"
  2192   shows "((\<lambda>x. f x powr s) ---> 0) F"
  2193 proof (rule tendstoI)
  2194   fix e :: real assume "0 < e"
  2195   def Z \<equiv> "e powr (1 / s)"
  2196   from assms have "eventually (\<lambda>x. Z < f x) F"
  2197     by (simp add: filterlim_at_top_dense)
  2198   moreover
  2199   from assms have "\<And>x. Z < x \<Longrightarrow> x powr s < Z powr s"
  2200     by (auto simp: Z_def intro!: powr_less_mono2_neg)
  2201   with assms `0 < e` have "\<And>x. Z < x \<Longrightarrow> dist (x powr s) 0 < e"
  2202     by (simp add: powr_powr Z_def dist_real_def)
  2203   ultimately
  2204   show "eventually (\<lambda>x. dist (f x powr s) 0 < e) F" by (rule eventually_elim1)
  2205 qed
  2206 
  2207 subsection {* Sine and Cosine *}
  2208 
  2209 definition sin_coeff :: "nat \<Rightarrow> real" where
  2210   "sin_coeff = (\<lambda>n. if even n then 0 else -1 ^ ((n - Suc 0) div 2) / real (fact n))"
  2211 
  2212 definition cos_coeff :: "nat \<Rightarrow> real" where
  2213   "cos_coeff = (\<lambda>n. if even n then (-1 ^ (n div 2)) / real (fact n) else 0)"
  2214 
  2215 definition sin :: "real \<Rightarrow> real"
  2216   where "sin = (\<lambda>x. \<Sum>n. sin_coeff n * x ^ n)"
  2217 
  2218 definition cos :: "real \<Rightarrow> real"
  2219   where "cos = (\<lambda>x. \<Sum>n. cos_coeff n * x ^ n)"
  2220 
  2221 lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
  2222   unfolding sin_coeff_def by simp
  2223 
  2224 lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
  2225   unfolding cos_coeff_def by simp
  2226 
  2227 lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
  2228   unfolding cos_coeff_def sin_coeff_def
  2229   by (simp del: mult_Suc)
  2230 
  2231 lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
  2232   unfolding cos_coeff_def sin_coeff_def
  2233   by (simp del: mult_Suc, auto simp add: odd_Suc_mult_two_ex)
  2234 
  2235 lemma summable_sin: "summable (\<lambda>n. sin_coeff n * x ^ n)"
  2236   unfolding sin_coeff_def
  2237   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
  2238   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  2239   done
  2240 
  2241 lemma summable_cos: "summable (\<lambda>n. cos_coeff n * x ^ n)"
  2242   unfolding cos_coeff_def
  2243   apply (rule summable_comparison_test [OF _ summable_exp [where x="\<bar>x\<bar>"]])
  2244   apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  2245   done
  2246 
  2247 lemma sin_converges: "(\<lambda>n. sin_coeff n * x ^ n) sums sin(x)"
  2248   unfolding sin_def by (rule summable_sin [THEN summable_sums])
  2249 
  2250 lemma cos_converges: "(\<lambda>n. cos_coeff n * x ^ n) sums cos(x)"
  2251   unfolding cos_def by (rule summable_cos [THEN summable_sums])
  2252 
  2253 lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
  2254   by (simp add: diffs_def sin_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2255 
  2256 lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
  2257   by (simp add: diffs_def cos_coeff_Suc real_of_nat_def del: of_nat_Suc)
  2258 
  2259 text{*Now at last we can get the derivatives of exp, sin and cos*}
  2260 
  2261 lemma DERIV_sin [simp]: "DERIV sin x :> cos(x)"
  2262   unfolding sin_def cos_def
  2263   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2264   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff
  2265     summable_minus summable_sin summable_cos)
  2266   done
  2267 
  2268 declare DERIV_sin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2269 
  2270 lemma DERIV_cos [simp]: "DERIV cos x :> -sin(x)"
  2271   unfolding cos_def sin_def
  2272   apply (rule DERIV_cong, rule termdiffs [where K="1 + \<bar>x\<bar>"])
  2273   apply (simp_all add: diffs_sin_coeff diffs_cos_coeff diffs_minus
  2274     summable_minus summable_sin summable_cos suminf_minus)
  2275   done
  2276 
  2277 declare DERIV_cos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  2278 
  2279 lemma isCont_sin: "isCont sin x"
  2280   by (rule DERIV_sin [THEN DERIV_isCont])
  2281 
  2282 lemma isCont_cos: "isCont cos x"
  2283   by (rule DERIV_cos [THEN DERIV_isCont])
  2284 
  2285 lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
  2286   by (rule isCont_o2 [OF _ isCont_sin])
  2287 
  2288 lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
  2289   by (rule isCont_o2 [OF _ isCont_cos])
  2290 
  2291 lemma tendsto_sin [tendsto_intros]:
  2292   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) ---> sin a) F"
  2293   by (rule isCont_tendsto_compose [OF isCont_sin])
  2294 
  2295 lemma tendsto_cos [tendsto_intros]:
  2296   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) ---> cos a) F"
  2297   by (rule isCont_tendsto_compose [OF isCont_cos])
  2298 
  2299 lemma continuous_sin [continuous_intros]:
  2300   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
  2301   unfolding continuous_def by (rule tendsto_sin)
  2302 
  2303 lemma continuous_on_sin [continuous_on_intros]:
  2304   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
  2305   unfolding continuous_on_def by (auto intro: tendsto_sin)
  2306 
  2307 lemma continuous_cos [continuous_intros]:
  2308   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
  2309   unfolding continuous_def by (rule tendsto_cos)
  2310 
  2311 lemma continuous_on_cos [continuous_on_intros]:
  2312   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
  2313   unfolding continuous_on_def by (auto intro: tendsto_cos)
  2314 
  2315 subsection {* Properties of Sine and Cosine *}
  2316 
  2317 lemma sin_zero [simp]: "sin 0 = 0"
  2318   unfolding sin_def sin_coeff_def by (simp add: powser_zero)
  2319 
  2320 lemma cos_zero [simp]: "cos 0 = 1"
  2321   unfolding cos_def cos_coeff_def by (simp add: powser_zero)
  2322 
  2323 lemma sin_cos_squared_add [simp]: "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
  2324 proof -
  2325   have "\<forall>x. DERIV (\<lambda>x. (sin x)\<^sup>2 + (cos x)\<^sup>2) x :> 0"
  2326     by (auto intro!: DERIV_intros)
  2327   hence "(sin x)\<^sup>2 + (cos x)\<^sup>2 = (sin 0)\<^sup>2 + (cos 0)\<^sup>2"
  2328     by (rule DERIV_isconst_all)
  2329   thus "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1" by simp
  2330 qed
  2331 
  2332 lemma sin_cos_squared_add2 [simp]: "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
  2333   by (subst add_commute, rule sin_cos_squared_add)
  2334 
  2335 lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
  2336   using sin_cos_squared_add2 [unfolded power2_eq_square] .
  2337 
  2338 lemma sin_squared_eq: "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
  2339   unfolding eq_diff_eq by (rule sin_cos_squared_add)
  2340 
  2341 lemma cos_squared_eq: "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
  2342   unfolding eq_diff_eq by (rule sin_cos_squared_add2)
  2343 
  2344 lemma abs_sin_le_one [simp]: "\<bar>sin x\<bar> \<le> 1"
  2345   by (rule power2_le_imp_le, simp_all add: sin_squared_eq)
  2346 
  2347 lemma sin_ge_minus_one [simp]: "-1 \<le> sin x"
  2348   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2349 
  2350 lemma sin_le_one [simp]: "sin x \<le> 1"
  2351   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
  2352 
  2353 lemma abs_cos_le_one [simp]: "\<bar>cos x\<bar> \<le> 1"
  2354   by (rule power2_le_imp_le, simp_all add: cos_squared_eq)
  2355 
  2356 lemma cos_ge_minus_one [simp]: "-1 \<le> cos x"
  2357   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2358 
  2359 lemma cos_le_one [simp]: "cos x \<le> 1"
  2360   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
  2361 
  2362 lemma DERIV_fun_pow: "DERIV g x :> m ==>
  2363       DERIV (\<lambda>x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
  2364   by (auto intro!: DERIV_intros)
  2365 
  2366 lemma DERIV_fun_exp:
  2367      "DERIV g x :> m ==> DERIV (\<lambda>x. exp(g x)) x :> exp(g x) * m"
  2368   by (auto intro!: DERIV_intros)
  2369 
  2370 lemma DERIV_fun_sin:
  2371      "DERIV g x :> m ==> DERIV (\<lambda>x. sin(g x)) x :> cos(g x) * m"
  2372   by (auto intro!: DERIV_intros)
  2373 
  2374 lemma DERIV_fun_cos:
  2375      "DERIV g x :> m ==> DERIV (\<lambda>x. cos(g x)) x :> -sin(g x) * m"
  2376   by (auto intro!: DERIV_intros)
  2377 
  2378 lemma sin_cos_add_lemma:
  2379   "(sin (x + y) - (sin x * cos y + cos x * sin y))\<^sup>2 +
  2380     (cos (x + y) - (cos x * cos y - sin x * sin y))\<^sup>2 = 0"
  2381   (is "?f x = 0")
  2382 proof -
  2383   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2384     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2385   hence "?f x = ?f 0"
  2386     by (rule DERIV_isconst_all)
  2387   thus ?thesis by simp
  2388 qed
  2389 
  2390 lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
  2391   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2392 
  2393 lemma cos_add: "cos (x + y) = cos x * cos y - sin x * sin y"
  2394   using sin_cos_add_lemma unfolding realpow_two_sum_zero_iff by simp
  2395 
  2396 lemma sin_cos_minus_lemma:
  2397   "(sin(-x) + sin(x))\<^sup>2 + (cos(-x) - cos(x))\<^sup>2 = 0" (is "?f x = 0")
  2398 proof -
  2399   have "\<forall>x. DERIV (\<lambda>x. ?f x) x :> 0"
  2400     by (auto intro!: DERIV_intros simp add: algebra_simps)
  2401   hence "?f x = ?f 0"
  2402     by (rule DERIV_isconst_all)
  2403   thus ?thesis by simp
  2404 qed
  2405 
  2406 lemma sin_minus [simp]: "sin (-x) = -sin(x)"
  2407   using sin_cos_minus_lemma [where x=x] by simp
  2408 
  2409 lemma cos_minus [simp]: "cos (-x) = cos(x)"
  2410   using sin_cos_minus_lemma [where x=x] by simp
  2411 
  2412 lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
  2413   by (simp add: diff_minus sin_add)
  2414 
  2415 lemma sin_diff2: "sin (x - y) = cos y * sin x - sin y * cos x"
  2416   by (simp add: sin_diff mult_commute)
  2417 
  2418 lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
  2419   by (simp add: diff_minus cos_add)
  2420 
  2421 lemma cos_diff2: "cos (x - y) = cos y * cos x + sin y * sin x"
  2422   by (simp add: cos_diff mult_commute)
  2423 
  2424 lemma sin_double [simp]: "sin(2 * x) = 2* sin x * cos x"
  2425   using sin_add [where x=x and y=x] by simp
  2426 
  2427 lemma cos_double: "cos(2* x) = ((cos x)\<^sup>2) - ((sin x)\<^sup>2)"
  2428   using cos_add [where x=x and y=x]
  2429   by (simp add: power2_eq_square)
  2430 
  2431 
  2432 subsection {* The Constant Pi *}
  2433 
  2434 definition pi :: real
  2435   where "pi = 2 * (THE x. 0 \<le> (x::real) & x \<le> 2 & cos x = 0)"
  2436 
  2437 text{*Show that there's a least positive @{term x} with @{term "cos(x) = 0"};
  2438    hence define pi.*}
  2439 
  2440 lemma sin_paired:
  2441   "(\<lambda>n. -1 ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) sums  sin x"
  2442 proof -
  2443   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
  2444     by (rule sin_converges [THEN sums_group], simp)
  2445   thus ?thesis unfolding One_nat_def sin_coeff_def by (simp add: mult_ac)
  2446 qed
  2447 
  2448 lemma sin_gt_zero:
  2449   assumes "0 < x" and "x < 2"
  2450   shows "0 < sin x"
  2451 proof -
  2452   let ?f = "\<lambda>n. \<Sum>k = n*2..<n*2+2. -1 ^ k / real (fact (2*k+1)) * x^(2*k+1)"
  2453   have pos: "\<forall>n. 0 < ?f n"
  2454   proof
  2455     fix n :: nat
  2456     let ?k2 = "real (Suc (Suc (4 * n)))"
  2457     let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
  2458     have "x * x < ?k2 * ?k3"
  2459       using assms by (intro mult_strict_mono', simp_all)
  2460     hence "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
  2461       by (intro mult_strict_right_mono zero_less_power `0 < x`)
  2462     thus "0 < ?f n"
  2463       by (simp del: mult_Suc,
  2464         simp add: less_divide_eq mult_pos_pos field_simps del: mult_Suc)
  2465   qed
  2466   have sums: "?f sums sin x"
  2467     by (rule sin_paired [THEN sums_group], simp)
  2468   show "0 < sin x"
  2469     unfolding sums_unique [OF sums]
  2470     using sums_summable [OF sums] pos
  2471     by (rule suminf_gt_zero)
  2472 qed
  2473 
  2474 lemma cos_double_less_one: "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
  2475   using sin_gt_zero [where x = x] by (auto simp add: cos_squared_eq cos_double)
  2476 
  2477 lemma cos_paired: "(\<lambda>n. -1 ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
  2478 proof -
  2479   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
  2480     by (rule cos_converges [THEN sums_group], simp)
  2481   thus ?thesis unfolding cos_coeff_def by (simp add: mult_ac)
  2482 qed
  2483 
  2484 lemma real_mult_inverse_cancel:
  2485      "[|(0::real) < x; 0 < x1; x1 * y < x * u |]
  2486       ==> inverse x * y < inverse x1 * u"
  2487   apply (rule_tac c=x in mult_less_imp_less_left)
  2488   apply (auto simp add: mult_assoc [symmetric])
  2489   apply (simp (no_asm) add: mult_ac)
  2490   apply (rule_tac c=x1 in mult_less_imp_less_right)
  2491   apply (auto simp add: mult_ac)
  2492   done
  2493 
  2494 lemma real_mult_inverse_cancel2:
  2495      "[|(0::real) < x;0 < x1; x1 * y < x * u |] ==> y * inverse x < u * inverse x1"
  2496   by (auto dest: real_mult_inverse_cancel simp add: mult_ac)
  2497 
  2498 lemma realpow_num_eq_if:
  2499   fixes m :: "'a::power"
  2500   shows "m ^ n = (if n=0 then 1 else m * m ^ (n - 1))"
  2501   by (cases n) auto
  2502 
  2503 lemma cos_two_less_zero [simp]: "cos (2) < 0"
  2504   apply (cut_tac x = 2 in cos_paired)
  2505   apply (drule sums_minus)
  2506   apply (rule neg_less_iff_less [THEN iffD1])
  2507   apply (frule sums_unique, auto)
  2508   apply (rule_tac y =
  2509    "\<Sum>n=0..< Suc(Suc(Suc 0)). - (-1 ^ n / (real(fact (2*n))) * 2 ^ (2*n))"
  2510          in order_less_trans)
  2511   apply (simp (no_asm) add: fact_num_eq_if_nat realpow_num_eq_if del: fact_Suc)
  2512   apply (simp (no_asm) add: mult_assoc del: setsum_op_ivl_Suc)
  2513   apply (rule sumr_pos_lt_pair)
  2514   apply (erule sums_summable, safe)
  2515   unfolding One_nat_def
  2516   apply (simp (no_asm) add: divide_inverse real_0_less_add_iff mult_assoc [symmetric]
  2517               del: fact_Suc)
  2518   apply (simp add: inverse_eq_divide less_divide_eq del: fact_Suc)
  2519   apply (subst fact_Suc [of "Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))"])
  2520   apply (simp only: real_of_nat_mult)
  2521   apply (rule mult_strict_mono, force)
  2522     apply (rule_tac [3] real_of_nat_ge_zero)
  2523    prefer 2 apply force
  2524   apply (rule real_of_nat_less_iff [THEN iffD2])
  2525   apply (rule fact_less_mono_nat, auto)
  2526   done
  2527 
  2528 lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
  2529 lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]
  2530 
  2531 lemma cos_is_zero: "EX! x. 0 \<le> x & x \<le> 2 \<and> cos x = 0"
  2532 proof (rule ex_ex1I)
  2533   show "\<exists>x. 0 \<le> x & x \<le> 2 & cos x = 0"
  2534     by (rule IVT2, simp_all)
  2535 next
  2536   fix x y
  2537   assume x: "0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
  2538   assume y: "0 \<le> y \<and> y \<le> 2 \<and> cos y = 0"
  2539   have [simp]: "\<forall>x. cos differentiable x"
  2540     unfolding differentiable_def by (auto intro: DERIV_cos)
  2541   from x y show "x = y"
  2542     apply (cut_tac less_linear [of x y], auto)
  2543     apply (drule_tac f = cos in Rolle)
  2544     apply (drule_tac [5] f = cos in Rolle)
  2545     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2546     apply (metis order_less_le_trans less_le sin_gt_zero)
  2547     apply (metis order_less_le_trans less_le sin_gt_zero)
  2548     done
  2549 qed
  2550 
  2551 lemma pi_half: "pi/2 = (THE x. 0 \<le> x & x \<le> 2 & cos x = 0)"
  2552   by (simp add: pi_def)
  2553 
  2554 lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
  2555   by (simp add: pi_half cos_is_zero [THEN theI'])
  2556 
  2557 lemma pi_half_gt_zero [simp]: "0 < pi / 2"
  2558   apply (rule order_le_neq_trans)
  2559   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2560   apply (rule notI, drule arg_cong [where f=cos], simp)
  2561   done
  2562 
  2563 lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
  2564 lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]
  2565 
  2566 lemma pi_half_less_two [simp]: "pi / 2 < 2"
  2567   apply (rule order_le_neq_trans)
  2568   apply (simp add: pi_half cos_is_zero [THEN theI'])
  2569   apply (rule notI, drule arg_cong [where f=cos], simp)
  2570   done
  2571 
  2572 lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
  2573 lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]
  2574 
  2575 lemma pi_gt_zero [simp]: "0 < pi"
  2576   using pi_half_gt_zero by simp
  2577 
  2578 lemma pi_ge_zero [simp]: "0 \<le> pi"
  2579   by (rule pi_gt_zero [THEN order_less_imp_le])
  2580 
  2581 lemma pi_neq_zero [simp]: "pi \<noteq> 0"
  2582   by (rule pi_gt_zero [THEN less_imp_neq, symmetric])
  2583 
  2584 lemma pi_not_less_zero [simp]: "\<not> pi < 0"
  2585   by (simp add: linorder_not_less)
  2586 
  2587 lemma minus_pi_half_less_zero: "-(pi/2) < 0"
  2588   by simp
  2589 
  2590 lemma m2pi_less_pi: "- (2 * pi) < pi"
  2591   by simp
  2592 
  2593 lemma sin_pi_half [simp]: "sin(pi/2) = 1"
  2594   using sin_cos_squared_add2 [where x = "pi/2"]
  2595   using sin_gt_zero [OF pi_half_gt_zero pi_half_less_two]
  2596   by (simp add: power2_eq_1_iff)
  2597 
  2598 lemma cos_pi [simp]: "cos pi = -1"
  2599   using cos_add [where x = "pi/2" and y = "pi/2"] by simp
  2600 
  2601 lemma sin_pi [simp]: "sin pi = 0"
  2602   using sin_add [where x = "pi/2" and y = "pi/2"] by simp
  2603 
  2604 lemma sin_cos_eq: "sin x = cos (pi/2 - x)"
  2605   by (simp add: cos_diff)
  2606 
  2607 lemma minus_sin_cos_eq: "-sin x = cos (x + pi/2)"
  2608   by (simp add: cos_add)
  2609 
  2610 lemma cos_sin_eq: "cos x = sin (pi/2 - x)"
  2611   by (simp add: sin_diff)
  2612 
  2613 lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
  2614   by (simp add: sin_add)
  2615 
  2616 lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
  2617   by (simp add: sin_add)
  2618 
  2619 lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
  2620   by (simp add: cos_add)
  2621 
  2622 lemma sin_periodic [simp]: "sin (x + 2*pi) = sin x"
  2623   by (simp add: sin_add cos_double)
  2624 
  2625 lemma cos_periodic [simp]: "cos (x + 2*pi) = cos x"
  2626   by (simp add: cos_add cos_double)
  2627 
  2628 lemma cos_npi [simp]: "cos (real n * pi) = -1 ^ n"
  2629   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2630 
  2631 lemma cos_npi2 [simp]: "cos (pi * real n) = -1 ^ n"
  2632 proof -
  2633   have "cos (pi * real n) = cos (real n * pi)" by (simp only: mult_commute)
  2634   also have "... = -1 ^ n" by (rule cos_npi)
  2635   finally show ?thesis .
  2636 qed
  2637 
  2638 lemma sin_npi [simp]: "sin (real (n::nat) * pi) = 0"
  2639   by (induct n) (auto simp add: real_of_nat_Suc distrib_right)
  2640 
  2641 lemma sin_npi2 [simp]: "sin (pi * real (n::nat)) = 0"
  2642   by (simp add: mult_commute [of pi])
  2643 
  2644 lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
  2645   by (simp add: cos_double)
  2646 
  2647 lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  2648   by simp
  2649 
  2650 lemma sin_gt_zero2: "[| 0 < x; x < pi/2 |] ==> 0 < sin x"
  2651   apply (rule sin_gt_zero, assumption)
  2652   apply (rule order_less_trans, assumption)
  2653   apply (rule pi_half_less_two)
  2654   done
  2655 
  2656 lemma sin_less_zero:
  2657   assumes "- pi/2 < x" and "x < 0"
  2658   shows "sin x < 0"
  2659 proof -
  2660   have "0 < sin (- x)" using assms by (simp only: sin_gt_zero2)
  2661   thus ?thesis by simp
  2662 qed
  2663 
  2664 lemma pi_less_4: "pi < 4"
  2665   using pi_half_less_two by auto
  2666 
  2667 lemma cos_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < cos x"
  2668   apply (cut_tac pi_less_4)
  2669   apply (cut_tac f = cos and a = 0 and b = x and y = 0 in IVT2_objl, safe, simp_all)
  2670   apply (cut_tac cos_is_zero, safe)
  2671   apply (rename_tac y z)
  2672   apply (drule_tac x = y in spec)
  2673   apply (drule_tac x = "pi/2" in spec, simp)
  2674   done
  2675 
  2676 lemma cos_gt_zero_pi: "[| -(pi/2) < x; x < pi/2 |] ==> 0 < cos x"
  2677   apply (rule_tac x = x and y = 0 in linorder_cases)
  2678   apply (rule cos_minus [THEN subst])
  2679   apply (rule cos_gt_zero)
  2680   apply (auto intro: cos_gt_zero)
  2681   done
  2682 
  2683 lemma cos_ge_zero: "[| -(pi/2) \<le> x; x \<le> pi/2 |] ==> 0 \<le> cos x"
  2684   apply (auto simp add: order_le_less cos_gt_zero_pi)
  2685   apply (subgoal_tac "x = pi/2", auto)
  2686   done
  2687 
  2688 lemma sin_gt_zero_pi: "[| 0 < x; x < pi  |] ==> 0 < sin x"
  2689   by (simp add: sin_cos_eq cos_gt_zero_pi)
  2690 
  2691 lemma pi_ge_two: "2 \<le> pi"
  2692 proof (rule ccontr)
  2693   assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
  2694   have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
  2695   proof (cases "2 < 2 * pi")
  2696     case True with dense[OF `pi < 2`] show ?thesis by auto
  2697   next
  2698     case False have "pi < 2 * pi" by auto
  2699     from dense[OF this] and False show ?thesis by auto
  2700   qed
  2701   then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
  2702   hence "0 < sin y" using sin_gt_zero by auto
  2703   moreover
  2704   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
  2705   ultimately show False by auto
  2706 qed
  2707 
  2708 lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
  2709   by (auto simp add: order_le_less sin_gt_zero_pi)
  2710 
  2711 text {* FIXME: This proof is almost identical to lemma @{text cos_is_zero}.
  2712   It should be possible to factor out some of the common parts. *}
  2713 
  2714 lemma cos_total: "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. 0 \<le> x & x \<le> pi & (cos x = y)"
  2715 proof (rule ex_ex1I)
  2716   assume y: "-1 \<le> y" "y \<le> 1"
  2717   show "\<exists>x. 0 \<le> x & x \<le> pi & cos x = y"
  2718     by (rule IVT2, simp_all add: y)
  2719 next
  2720   fix a b
  2721   assume a: "0 \<le> a \<and> a \<le> pi \<and> cos a = y"
  2722   assume b: "0 \<le> b \<and> b \<le> pi \<and> cos b = y"
  2723   have [simp]: "\<forall>x. cos differentiable x"
  2724     unfolding differentiable_def by (auto intro: DERIV_cos)
  2725   from a b show "a = b"
  2726     apply (cut_tac less_linear [of a b], auto)
  2727     apply (drule_tac f = cos in Rolle)
  2728     apply (drule_tac [5] f = cos in Rolle)
  2729     apply (auto dest!: DERIV_cos [THEN DERIV_unique])
  2730     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2731     apply (metis order_less_le_trans less_le sin_gt_zero_pi)
  2732     done
  2733 qed
  2734 
  2735 lemma sin_total:
  2736      "[| -1 \<le> y; y \<le> 1 |] ==> EX! x. -(pi/2) \<le> x & x \<le> pi/2 & (sin x = y)"
  2737 apply (rule ccontr)
  2738 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))")
  2739 apply (erule contrapos_np)
  2740 apply simp
  2741 apply (cut_tac y="-y" in cos_total, simp) apply simp
  2742 apply (erule ex1E)
  2743 apply (rule_tac a = "x - (pi/2)" in ex1I)
  2744 apply (simp (no_asm) add: add_assoc)
  2745 apply (rotate_tac 3)
  2746 apply (drule_tac x = "xa + pi/2" in spec, safe, simp_all add: cos_add)
  2747 done
  2748 
  2749 lemma reals_Archimedean4:
  2750      "[| 0 < y; 0 \<le> x |] ==> \<exists>n. real n * y \<le> x & x < real (Suc n) * y"
  2751 apply (auto dest!: reals_Archimedean3)
  2752 apply (drule_tac x = x in spec, clarify)
  2753 apply (subgoal_tac "x < real(LEAST m::nat. x < real m * y) * y")
  2754  prefer 2 apply (erule LeastI)
  2755 apply (case_tac "LEAST m::nat. x < real m * y", simp)
  2756 apply (subgoal_tac "~ x < real nat * y")
  2757  prefer 2 apply (rule not_less_Least, simp, force)
  2758 done
  2759 
  2760 (* Pre Isabelle99-2 proof was simpler- numerals arithmetic
  2761    now causes some unwanted re-arrangements of literals!   *)
  2762 lemma cos_zero_lemma:
  2763      "[| 0 \<le> x; cos x = 0 |] ==>
  2764       \<exists>n::nat. ~even n & x = real n * (pi/2)"
  2765 apply (drule pi_gt_zero [THEN reals_Archimedean4], safe)
  2766 apply (subgoal_tac "0 \<le> x - real n * pi &
  2767                     (x - real n * pi) \<le> pi & (cos (x - real n * pi) = 0) ")
  2768 apply (auto simp add: algebra_simps real_of_nat_Suc)
  2769  prefer 2 apply (simp add: cos_diff)
  2770 apply (simp add: cos_diff)
  2771 apply (subgoal_tac "EX! x. 0 \<le> x & x \<le> pi & cos x = 0")
  2772 apply (rule_tac [2] cos_total, safe)
  2773 apply (drule_tac x = "x - real n * pi" in spec)
  2774 apply (drule_tac x = "pi/2" in spec)
  2775 apply (simp add: cos_diff)
  2776 apply (rule_tac x = "Suc (2 * n)" in exI)
  2777 apply (simp add: real_of_nat_Suc algebra_simps, auto)
  2778 done
  2779 
  2780 lemma sin_zero_lemma:
  2781      "[| 0 \<le> x; sin x = 0 |] ==>
  2782       \<exists>n::nat. even n & x = real n * (pi/2)"
  2783 apply (subgoal_tac "\<exists>n::nat. ~ even n & x + pi/2 = real n * (pi/2) ")
  2784  apply (clarify, rule_tac x = "n - 1" in exI)
  2785  apply (force simp add: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2786 apply (rule cos_zero_lemma)
  2787 apply (simp_all add: cos_add)
  2788 done
  2789 
  2790 
  2791 lemma cos_zero_iff:
  2792      "(cos x = 0) =
  2793       ((\<exists>n::nat. ~even n & (x = real n * (pi/2))) |
  2794        (\<exists>n::nat. ~even n & (x = -(real n * (pi/2)))))"
  2795 apply (rule iffI)
  2796 apply (cut_tac linorder_linear [of 0 x], safe)
  2797 apply (drule cos_zero_lemma, assumption+)
  2798 apply (cut_tac x="-x" in cos_zero_lemma, simp, simp)
  2799 apply (force simp add: minus_equation_iff [of x])
  2800 apply (auto simp only: odd_Suc_mult_two_ex real_of_nat_Suc distrib_right)
  2801 apply (auto simp add: cos_add)
  2802 done
  2803 
  2804 (* ditto: but to a lesser extent *)
  2805 lemma sin_zero_iff:
  2806      "(sin x = 0) =
  2807       ((\<exists>n::nat. even n & (x = real n * (pi/2))) |
  2808        (\<exists>n::nat. even n & (x = -(real n * (pi/2)))))"
  2809 apply (rule iffI)
  2810 apply (cut_tac linorder_linear [of 0 x], safe)
  2811 apply (drule sin_zero_lemma, assumption+)
  2812 apply (cut_tac x="-x" in sin_zero_lemma, simp, simp, safe)
  2813 apply (force simp add: minus_equation_iff [of x])
  2814 apply (auto simp add: even_mult_two_ex)
  2815 done
  2816 
  2817 lemma cos_monotone_0_pi:
  2818   assumes "0 \<le> y" and "y < x" and "x \<le> pi"
  2819   shows "cos x < cos y"
  2820 proof -
  2821   have "- (x - y) < 0" using assms by auto
  2822 
  2823   from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
  2824   obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z"
  2825     by auto
  2826   hence "0 < z" and "z < pi" using assms by auto
  2827   hence "0 < sin z" using sin_gt_zero_pi by auto
  2828   hence "cos x - cos y < 0"
  2829     unfolding cos_diff minus_mult_commute[symmetric]
  2830     using `- (x - y) < 0` by (rule mult_pos_neg2)
  2831   thus ?thesis by auto
  2832 qed
  2833 
  2834 lemma cos_monotone_0_pi':
  2835   assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi"
  2836   shows "cos x \<le> cos y"
  2837 proof (cases "y < x")
  2838   case True
  2839   show ?thesis
  2840     using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
  2841 next
  2842   case False
  2843   hence "y = x" using `y \<le> x` by auto
  2844   thus ?thesis by auto
  2845 qed
  2846 
  2847 lemma cos_monotone_minus_pi_0:
  2848   assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
  2849   shows "cos y < cos x"
  2850 proof -
  2851   have "0 \<le> -x" and "-x < -y" and "-y \<le> pi"
  2852     using assms by auto
  2853   from cos_monotone_0_pi[OF this] show ?thesis
  2854     unfolding cos_minus .
  2855 qed
  2856 
  2857 lemma cos_monotone_minus_pi_0':
  2858   assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0"
  2859   shows "cos y \<le> cos x"
  2860 proof (cases "y < x")
  2861   case True
  2862   show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`]
  2863     by auto
  2864 next
  2865   case False
  2866   hence "y = x" using `y \<le> x` by auto
  2867   thus ?thesis by auto
  2868 qed
  2869 
  2870 lemma sin_monotone_2pi':
  2871   assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2"
  2872   shows "sin y \<le> sin x"
  2873 proof -
  2874   have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi"
  2875     using pi_ge_two and assms by auto
  2876   from cos_monotone_0_pi'[OF this] show ?thesis
  2877     unfolding minus_sin_cos_eq[symmetric] by auto
  2878 qed
  2879 
  2880 
  2881 subsection {* Tangent *}
  2882 
  2883 definition tan :: "real \<Rightarrow> real"
  2884   where "tan = (\<lambda>x. sin x / cos x)"
  2885 
  2886 lemma tan_zero [simp]: "tan 0 = 0"
  2887   by (simp add: tan_def)
  2888 
  2889 lemma tan_pi [simp]: "tan pi = 0"
  2890   by (simp add: tan_def)
  2891 
  2892 lemma tan_npi [simp]: "tan (real (n::nat) * pi) = 0"
  2893   by (simp add: tan_def)
  2894 
  2895 lemma tan_minus [simp]: "tan (-x) = - tan x"
  2896   by (simp add: tan_def)
  2897 
  2898 lemma tan_periodic [simp]: "tan (x + 2*pi) = tan x"
  2899   by (simp add: tan_def)
  2900 
  2901 lemma lemma_tan_add1:
  2902   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> 1 - tan x * tan y = cos (x + y)/(cos x * cos y)"
  2903   by (simp add: tan_def cos_add field_simps)
  2904 
  2905 lemma add_tan_eq:
  2906   "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0\<rbrakk> \<Longrightarrow> tan x + tan y = sin(x + y)/(cos x * cos y)"
  2907   by (simp add: tan_def sin_add field_simps)
  2908 
  2909 lemma tan_add:
  2910      "[| cos x \<noteq> 0; cos y \<noteq> 0; cos (x + y) \<noteq> 0 |]
  2911       ==> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
  2912   by (simp add: add_tan_eq lemma_tan_add1, simp add: tan_def)
  2913 
  2914 lemma tan_double:
  2915      "[| cos x \<noteq> 0; cos (2 * x) \<noteq> 0 |]
  2916       ==> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
  2917   using tan_add [of x x] by (simp add: power2_eq_square)
  2918 
  2919 lemma tan_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < tan x"
  2920   by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
  2921 
  2922 lemma tan_less_zero:
  2923   assumes lb: "- pi/2 < x" and "x < 0"
  2924   shows "tan x < 0"
  2925 proof -
  2926   have "0 < tan (- x)" using assms by (simp only: tan_gt_zero)
  2927   thus ?thesis by simp
  2928 qed
  2929 
  2930 lemma tan_half: "tan x = sin (2 * x) / (cos (2 * x) + 1)"
  2931   unfolding tan_def sin_double cos_double sin_squared_eq
  2932   by (simp add: power2_eq_square)
  2933 
  2934 lemma DERIV_tan [simp]: "cos x \<noteq> 0 \<Longrightarrow> DERIV tan x :> inverse ((cos x)\<^sup>2)"
  2935   unfolding tan_def
  2936   by (auto intro!: DERIV_intros, simp add: divide_inverse power2_eq_square)
  2937 
  2938 lemma isCont_tan: "cos x \<noteq> 0 \<Longrightarrow> isCont tan x"
  2939   by (rule DERIV_tan [THEN DERIV_isCont])
  2940 
  2941 lemma isCont_tan' [simp]:
  2942   "\<lbrakk>isCont f a; cos (f a) \<noteq> 0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. tan (f x)) a"
  2943   by (rule isCont_o2 [OF _ isCont_tan])
  2944 
  2945 lemma tendsto_tan [tendsto_intros]:
  2946   "\<lbrakk>(f ---> a) F; cos a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. tan (f x)) ---> tan a) F"
  2947   by (rule isCont_tendsto_compose [OF isCont_tan])
  2948 
  2949 lemma continuous_tan:
  2950   "continuous F f \<Longrightarrow> cos (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. tan (f x))"
  2951   unfolding continuous_def by (rule tendsto_tan)
  2952 
  2953 lemma isCont_tan'' [continuous_intros]:
  2954   "continuous (at x) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. tan (f x))"
  2955   unfolding continuous_at by (rule tendsto_tan)
  2956 
  2957 lemma continuous_within_tan [continuous_intros]:
  2958   "continuous (at x within s) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
  2959   unfolding continuous_within by (rule tendsto_tan)
  2960 
  2961 lemma continuous_on_tan [continuous_on_intros]:
  2962   "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. cos (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. tan (f x))"
  2963   unfolding continuous_on_def by (auto intro: tendsto_tan)
  2964 
  2965 lemma LIM_cos_div_sin: "(\<lambda>x. cos(x)/sin(x)) -- pi/2 --> 0"
  2966   by (rule LIM_cong_limit, (rule tendsto_intros)+, simp_all)
  2967 
  2968 lemma lemma_tan_total: "0 < y ==> \<exists>x. 0 < x & x < pi/2 & y < tan x"
  2969   apply (cut_tac LIM_cos_div_sin)
  2970   apply (simp only: LIM_eq)
  2971   apply (drule_tac x = "inverse y" in spec, safe, force)
  2972   apply (drule_tac ?d1.0 = s in pi_half_gt_zero [THEN [2] real_lbound_gt_zero], safe)
  2973   apply (rule_tac x = "(pi/2) - e" in exI)
  2974   apply (simp (no_asm_simp))
  2975   apply (drule_tac x = "(pi/2) - e" in spec)
  2976   apply (auto simp add: tan_def sin_diff cos_diff)
  2977   apply (rule inverse_less_iff_less [THEN iffD1])
  2978   apply (auto simp add: divide_inverse)
  2979   apply (rule mult_pos_pos)
  2980   apply (subgoal_tac [3] "0 < sin e & 0 < cos e")
  2981   apply (auto intro: cos_gt_zero sin_gt_zero2 simp add: mult_commute)
  2982   done
  2983 
  2984 lemma tan_total_pos: "0 \<le> y ==> \<exists>x. 0 \<le> x & x < pi/2 & tan x = y"
  2985   apply (frule order_le_imp_less_or_eq, safe)
  2986    prefer 2 apply force
  2987   apply (drule lemma_tan_total, safe)
  2988   apply (cut_tac f = tan and a = 0 and b = x and y = y in IVT_objl)
  2989   apply (auto intro!: DERIV_tan [THEN DERIV_isCont])
  2990   apply (drule_tac y = xa in order_le_imp_less_or_eq)
  2991   apply (auto dest: cos_gt_zero)
  2992   done
  2993 
  2994 lemma lemma_tan_total1: "\<exists>x. -(pi/2) < x & x < (pi/2) & tan x = y"
  2995   apply (cut_tac linorder_linear [of 0 y], safe)
  2996   apply (drule tan_total_pos)
  2997   apply (cut_tac [2] y="-y" in tan_total_pos, safe)
  2998   apply (rule_tac [3] x = "-x" in exI)
  2999   apply (auto del: exI intro!: exI)
  3000   done
  3001 
  3002 lemma tan_total: "EX! x. -(pi/2) < x & x < (pi/2) & tan x = y"
  3003   apply (cut_tac y = y in lemma_tan_total1, auto)
  3004   apply (cut_tac x = xa and y = y in linorder_less_linear, auto)
  3005   apply (subgoal_tac [2] "\<exists>z. y < z & z < xa & DERIV tan z :> 0")
  3006   apply (subgoal_tac "\<exists>z. xa < z & z < y & DERIV tan z :> 0")
  3007   apply (rule_tac [4] Rolle)
  3008   apply (rule_tac [2] Rolle)
  3009   apply (auto del: exI intro!: DERIV_tan DERIV_isCont exI
  3010               simp add: differentiable_def)
  3011   txt{*Now, simulate TRYALL*}
  3012   apply (rule_tac [!] DERIV_tan asm_rl)
  3013   apply (auto dest!: DERIV_unique [OF _ DERIV_tan]
  3014               simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym])
  3015   done
  3016 
  3017 lemma tan_monotone:
  3018   assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
  3019   shows "tan y < tan x"
  3020 proof -
  3021   have "\<forall>x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse ((cos x')\<^sup>2)"
  3022   proof (rule allI, rule impI)
  3023     fix x' :: real
  3024     assume "y \<le> x' \<and> x' \<le> x"
  3025     hence "-(pi/2) < x'" and "x' < pi/2" using assms by auto
  3026     from cos_gt_zero_pi[OF this]
  3027     have "cos x' \<noteq> 0" by auto
  3028     thus "DERIV tan x' :> inverse ((cos x')\<^sup>2)" by (rule DERIV_tan)
  3029   qed
  3030   from MVT2[OF `y < x` this]
  3031   obtain z where "y < z" and "z < x"
  3032     and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<^sup>2)" by auto
  3033   hence "- (pi / 2) < z" and "z < pi / 2" using assms by auto
  3034   hence "0 < cos z" using cos_gt_zero_pi by auto
  3035   hence inv_pos: "0 < inverse ((cos z)\<^sup>2)" by auto
  3036   have "0 < x - y" using `y < x` by auto
  3037   from mult_pos_pos [OF this inv_pos]
  3038   have "0 < tan x - tan y" unfolding tan_diff by auto
  3039   thus ?thesis by auto
  3040 qed
  3041 
  3042 lemma tan_monotone':
  3043   assumes "- (pi / 2) < y"
  3044     and "y < pi / 2"
  3045     and "- (pi / 2) < x"
  3046     and "x < pi / 2"
  3047   shows "(y < x) = (tan y < tan x)"
  3048 proof
  3049   assume "y < x"
  3050   thus "tan y < tan x"
  3051     using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
  3052 next
  3053   assume "tan y < tan x"
  3054   show "y < x"
  3055   proof (rule ccontr)
  3056     assume "\<not> y < x" hence "x \<le> y" by auto
  3057     hence "tan x \<le> tan y"
  3058     proof (cases "x = y")
  3059       case True thus ?thesis by auto
  3060     next
  3061       case False hence "x < y" using `x \<le> y` by auto
  3062       from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
  3063     qed
  3064     thus False using `tan y < tan x` by auto
  3065   qed
  3066 qed
  3067 
  3068 lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)"
  3069   unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
  3070 
  3071 lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
  3072   by (simp add: tan_def)
  3073 
  3074 lemma tan_periodic_nat[simp]:
  3075   fixes n :: nat
  3076   shows "tan (x + real n * pi) = tan x"
  3077 proof (induct n arbitrary: x)
  3078   case 0
  3079   then show ?case by simp
  3080 next
  3081   case (Suc n)
  3082   have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi"
  3083     unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right by auto
  3084   show ?case unfolding split_pi_off using Suc by auto
  3085 qed
  3086 
  3087 lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
  3088 proof (cases "0 \<le> i")
  3089   case True
  3090   hence i_nat: "real i = real (nat i)" by auto
  3091   show ?thesis unfolding i_nat by auto
  3092 next
  3093   case False
  3094   hence i_nat: "real i = - real (nat (-i))" by auto
  3095   have "tan x = tan (x + real i * pi - real i * pi)"
  3096     by auto
  3097   also have "\<dots> = tan (x + real i * pi)"
  3098     unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
  3099   finally show ?thesis by auto
  3100 qed
  3101 
  3102 lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
  3103   using tan_periodic_int[of _ "numeral n" ] unfolding real_numeral .
  3104 
  3105 subsection {* Inverse Trigonometric Functions *}
  3106 
  3107 definition arcsin :: "real => real"
  3108   where "arcsin y = (THE x. -(pi/2) \<le> x & x \<le> pi/2 & sin x = y)"
  3109 
  3110 definition arccos :: "real => real"
  3111   where "arccos y = (THE x. 0 \<le> x & x \<le> pi & cos x = y)"
  3112 
  3113 definition arctan :: "real => real"
  3114   where "arctan y = (THE x. -(pi/2) < x & x < pi/2 & tan x = y)"
  3115 
  3116 lemma arcsin:
  3117   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow>
  3118     -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2 & sin(arcsin y) = y"
  3119   unfolding arcsin_def by (rule theI' [OF sin_total])
  3120 
  3121 lemma arcsin_pi:
  3122   "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi & sin(arcsin y) = y"
  3123   apply (drule (1) arcsin)
  3124   apply (force intro: order_trans)
  3125   done
  3126 
  3127 lemma sin_arcsin [simp]: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> sin(arcsin y) = y"
  3128   by (blast dest: arcsin)
  3129 
  3130 lemma arcsin_bounded: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y & arcsin y \<le> pi/2"
  3131   by (blast dest: arcsin)
  3132 
  3133 lemma arcsin_lbound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> -(pi/2) \<le> arcsin y"
  3134   by (blast dest: arcsin)
  3135 
  3136 lemma arcsin_ubound: "-1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin y \<le> pi/2"
  3137   by (blast dest: arcsin)
  3138 
  3139 lemma arcsin_lt_bounded:
  3140      "[| -1 < y; y < 1 |] ==> -(pi/2) < arcsin y & arcsin y < pi/2"
  3141   apply (frule order_less_imp_le)
  3142   apply (frule_tac y = y in order_less_imp_le)
  3143   apply (frule arcsin_bounded)
  3144   apply (safe, simp)
  3145   apply (drule_tac y = "arcsin y" in order_le_imp_less_or_eq)
  3146   apply (drule_tac [2] y = "pi/2" in order_le_imp_less_or_eq, safe)
  3147   apply (drule_tac [!] f = sin in arg_cong, auto)
  3148   done
  3149 
  3150 lemma arcsin_sin: "[|-(pi/2) \<le> x; x \<le> pi/2 |] ==> arcsin(sin x) = x"
  3151   apply (unfold arcsin_def)
  3152   apply (rule the1_equality)
  3153   apply (rule sin_total, auto)
  3154   done
  3155 
  3156 lemma arccos:
  3157      "[| -1 \<le> y; y \<le> 1 |]
  3158       ==> 0 \<le> arccos y & arccos y \<le> pi & cos(arccos y) = y"
  3159   unfolding arccos_def by (rule theI' [OF cos_total])
  3160 
  3161 lemma cos_arccos [simp]: "[| -1 \<le> y; y \<le> 1 |] ==> cos(arccos y) = y"
  3162   by (blast dest: arccos)
  3163 
  3164 lemma arccos_bounded: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y & arccos y \<le> pi"
  3165   by (blast dest: arccos)
  3166 
  3167 lemma arccos_lbound: "[| -1 \<le> y; y \<le> 1 |] ==> 0 \<le> arccos y"
  3168   by (blast dest: arccos)
  3169 
  3170 lemma arccos_ubound: "[| -1 \<le> y; y \<le> 1 |] ==> arccos y \<le> pi"
  3171   by (blast dest: arccos)
  3172 
  3173 lemma arccos_lt_bounded:
  3174      "[| -1 < y; y < 1 |]
  3175       ==> 0 < arccos y & arccos y < pi"
  3176   apply (frule order_less_imp_le)
  3177   apply (frule_tac y = y in order_less_imp_le)
  3178   apply (frule arccos_bounded, auto)
  3179   apply (drule_tac y = "arccos y" in order_le_imp_less_or_eq)
  3180   apply (drule_tac [2] y = pi in order_le_imp_less_or_eq, auto)
  3181   apply (drule_tac [!] f = cos in arg_cong, auto)
  3182   done
  3183 
  3184 lemma arccos_cos: "[|0 \<le> x; x \<le> pi |] ==> arccos(cos x) = x"
  3185   apply (simp add: arccos_def)
  3186   apply (auto intro!: the1_equality cos_total)
  3187   done
  3188 
  3189 lemma arccos_cos2: "[|x \<le> 0; -pi \<le> x |] ==> arccos(cos x) = -x"
  3190   apply (simp add: arccos_def)
  3191   apply (auto intro!: the1_equality cos_total)
  3192   done
  3193 
  3194 lemma cos_arcsin: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cos (arcsin x) = sqrt (1 - x\<^sup>2)"
  3195   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3196   apply (rule power2_eq_imp_eq)
  3197   apply (simp add: cos_squared_eq)
  3198   apply (rule cos_ge_zero)
  3199   apply (erule (1) arcsin_lbound)
  3200   apply (erule (1) arcsin_ubound)
  3201   apply simp
  3202   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3203   apply (rule power_mono, simp, simp)
  3204   done
  3205 
  3206 lemma sin_arccos: "\<lbrakk>-1 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> sin (arccos x) = sqrt (1 - x\<^sup>2)"
  3207   apply (subgoal_tac "x\<^sup>2 \<le> 1")
  3208   apply (rule power2_eq_imp_eq)
  3209   apply (simp add: sin_squared_eq)
  3210   apply (rule sin_ge_zero)
  3211   apply (erule (1) arccos_lbound)
  3212   apply (erule (1) arccos_ubound)
  3213   apply simp
  3214   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 \<le> 1\<^sup>2", simp)
  3215   apply (rule power_mono, simp, simp)
  3216   done
  3217 
  3218 lemma arctan [simp]: "- (pi/2) < arctan y  & arctan y < pi/2 & tan (arctan y) = y"
  3219   unfolding arctan_def by (rule theI' [OF tan_total])
  3220 
  3221 lemma tan_arctan: "tan (arctan y) = y"
  3222   by auto
  3223 
  3224 lemma arctan_bounded: "- (pi/2) < arctan y  & arctan y < pi/2"
  3225   by (auto simp only: arctan)
  3226 
  3227 lemma arctan_lbound: "- (pi/2) < arctan y"
  3228   by auto
  3229 
  3230 lemma arctan_ubound: "arctan y < pi/2"
  3231   by (auto simp only: arctan)
  3232 
  3233 lemma arctan_unique:
  3234   assumes "-(pi/2) < x"
  3235     and "x < pi/2"
  3236     and "tan x = y"
  3237   shows "arctan y = x"
  3238   using assms arctan [of y] tan_total [of y] by (fast elim: ex1E)
  3239 
  3240 lemma arctan_tan: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> arctan (tan x) = x"
  3241   by (rule arctan_unique) simp_all
  3242 
  3243 lemma arctan_zero_zero [simp]: "arctan 0 = 0"
  3244   by (rule arctan_unique) simp_all
  3245 
  3246 lemma arctan_minus: "arctan (- x) = - arctan x"
  3247   apply (rule arctan_unique)
  3248   apply (simp only: neg_less_iff_less arctan_ubound)
  3249   apply (metis minus_less_iff arctan_lbound)
  3250   apply simp
  3251   done
  3252 
  3253 lemma cos_arctan_not_zero [simp]: "cos (arctan x) \<noteq> 0"
  3254   by (intro less_imp_neq [symmetric] cos_gt_zero_pi
  3255     arctan_lbound arctan_ubound)
  3256 
  3257 lemma cos_arctan: "cos (arctan x) = 1 / sqrt (1 + x\<^sup>2)"
  3258 proof (rule power2_eq_imp_eq)
  3259   have "0 < 1 + x\<^sup>2" by (simp add: add_pos_nonneg)
  3260   show "0 \<le> 1 / sqrt (1 + x\<^sup>2)" by simp
  3261   show "0 \<le> cos (arctan x)"
  3262     by (intro less_imp_le cos_gt_zero_pi arctan_lbound arctan_ubound)
  3263   have "(cos (arctan x))\<^sup>2 * (1 + (tan (arctan x))\<^sup>2) = 1"
  3264     unfolding tan_def by (simp add: distrib_left power_divide)
  3265   thus "(cos (arctan x))\<^sup>2 = (1 / sqrt (1 + x\<^sup>2))\<^sup>2"
  3266     using `0 < 1 + x\<^sup>2` by (simp add: power_divide eq_divide_eq)
  3267 qed
  3268 
  3269 lemma sin_arctan: "sin (arctan x) = x / sqrt (1 + x\<^sup>2)"
  3270   using add_pos_nonneg [OF zero_less_one zero_le_power2 [of x]]
  3271   using tan_arctan [of x] unfolding tan_def cos_arctan
  3272   by (simp add: eq_divide_eq)
  3273 
  3274 lemma tan_sec: "cos x \<noteq> 0 ==> 1 + (tan x)\<^sup>2 = (inverse (cos x))\<^sup>2"
  3275   apply (rule power_inverse [THEN subst])
  3276   apply (rule_tac c1 = "(cos x)\<^sup>2" in real_mult_right_cancel [THEN iffD1])
  3277   apply (auto dest: field_power_not_zero
  3278           simp add: power_mult_distrib distrib_right power_divide tan_def
  3279                     mult_assoc power_inverse [symmetric])
  3280   done
  3281 
  3282 lemma arctan_less_iff: "arctan x < arctan y \<longleftrightarrow> x < y"
  3283   by (metis tan_monotone' arctan_lbound arctan_ubound tan_arctan)
  3284 
  3285 lemma arctan_le_iff: "arctan x \<le> arctan y \<longleftrightarrow> x \<le> y"
  3286   by (simp only: not_less [symmetric] arctan_less_iff)
  3287 
  3288 lemma arctan_eq_iff: "arctan x = arctan y \<longleftrightarrow> x = y"
  3289   by (simp only: eq_iff [where 'a=real] arctan_le_iff)
  3290 
  3291 lemma zero_less_arctan_iff [simp]: "0 < arctan x \<longleftrightarrow> 0 < x"
  3292   using arctan_less_iff [of 0 x] by simp
  3293 
  3294 lemma arctan_less_zero_iff [simp]: "arctan x < 0 \<longleftrightarrow> x < 0"
  3295   using arctan_less_iff [of x 0] by simp
  3296 
  3297 lemma zero_le_arctan_iff [simp]: "0 \<le> arctan x \<longleftrightarrow> 0 \<le> x"
  3298   using arctan_le_iff [of 0 x] by simp
  3299 
  3300 lemma arctan_le_zero_iff [simp]: "arctan x \<le> 0 \<longleftrightarrow> x \<le> 0"
  3301   using arctan_le_iff [of x 0] by simp
  3302 
  3303 lemma arctan_eq_zero_iff [simp]: "arctan x = 0 \<longleftrightarrow> x = 0"
  3304   using arctan_eq_iff [of x 0] by simp
  3305 
  3306 lemma continuous_on_arcsin': "continuous_on {-1 .. 1} arcsin"
  3307 proof -
  3308   have "continuous_on (sin ` {- pi / 2 .. pi / 2}) arcsin"
  3309     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arcsin_sin)
  3310   also have "sin ` {- pi / 2 .. pi / 2} = {-1 .. 1}"
  3311   proof safe
  3312     fix x :: real
  3313     assume "x \<in> {-1..1}"
  3314     then show "x \<in> sin ` {- pi / 2..pi / 2}"
  3315       using arcsin_lbound arcsin_ubound
  3316       by (intro image_eqI[where x="arcsin x"]) auto
  3317   qed simp
  3318   finally show ?thesis .
  3319 qed
  3320 
  3321 lemma continuous_on_arcsin [continuous_on_intros]:
  3322   "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))"
  3323   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arcsin']]
  3324   by (auto simp: comp_def subset_eq)
  3325 
  3326 lemma isCont_arcsin: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arcsin x"
  3327   using continuous_on_arcsin'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3328   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3329 
  3330 lemma continuous_on_arccos': "continuous_on {-1 .. 1} arccos"
  3331 proof -
  3332   have "continuous_on (cos ` {0 .. pi}) arccos"
  3333     by (rule continuous_on_inv) (auto intro: continuous_on_intros simp: arccos_cos)
  3334   also have "cos ` {0 .. pi} = {-1 .. 1}"
  3335   proof safe
  3336     fix x :: real
  3337     assume "x \<in> {-1..1}"
  3338     then show "x \<in> cos ` {0..pi}"
  3339       using arccos_lbound arccos_ubound
  3340       by (intro image_eqI[where x="arccos x"]) auto
  3341   qed simp
  3342   finally show ?thesis .
  3343 qed
  3344 
  3345 lemma continuous_on_arccos [continuous_on_intros]:
  3346   "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))"
  3347   using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arccos']]
  3348   by (auto simp: comp_def subset_eq)
  3349 
  3350 lemma isCont_arccos: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arccos x"
  3351   using continuous_on_arccos'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
  3352   by (auto simp: continuous_on_eq_continuous_at subset_eq)
  3353 
  3354 lemma isCont_arctan: "isCont arctan x"
  3355   apply (rule arctan_lbound [of x, THEN dense, THEN exE], clarify)
  3356   apply (rule arctan_ubound [of x, THEN dense, THEN exE], clarify)
  3357   apply (subgoal_tac "isCont arctan (tan (arctan x))", simp)
  3358   apply (erule (1) isCont_inverse_function2 [where f=tan])
  3359   apply (metis arctan_tan order_le_less_trans order_less_le_trans)
  3360   apply (metis cos_gt_zero_pi isCont_tan order_less_le_trans less_le)
  3361   done
  3362 
  3363 lemma tendsto_arctan [tendsto_intros]: "(f ---> x) F \<Longrightarrow> ((\<lambda>x. arctan (f x)) ---> arctan x) F"
  3364   by (rule isCont_tendsto_compose [OF isCont_arctan])
  3365 
  3366 lemma continuous_arctan [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arctan (f x))"
  3367   unfolding continuous_def by (rule tendsto_arctan)
  3368 
  3369 lemma continuous_on_arctan [continuous_on_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. arctan (f x))"
  3370   unfolding continuous_on_def by (auto intro: tendsto_arctan)
  3371 
  3372 lemma DERIV_arcsin:
  3373   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arcsin x :> inverse (sqrt (1 - x\<^sup>2))"
  3374   apply (rule DERIV_inverse_function [where f=sin and a="-1" and b="1"])
  3375   apply (rule DERIV_cong [OF DERIV_sin])
  3376   apply (simp add: cos_arcsin)
  3377   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3378   apply (rule power_strict_mono, simp, simp, simp)
  3379   apply assumption
  3380   apply assumption
  3381   apply simp
  3382   apply (erule (1) isCont_arcsin)
  3383   done
  3384 
  3385 lemma DERIV_arccos:
  3386   "\<lbrakk>-1 < x; x < 1\<rbrakk> \<Longrightarrow> DERIV arccos x :> inverse (- sqrt (1 - x\<^sup>2))"
  3387   apply (rule DERIV_inverse_function [where f=cos and a="-1" and b="1"])
  3388   apply (rule DERIV_cong [OF DERIV_cos])
  3389   apply (simp add: sin_arccos)
  3390   apply (subgoal_tac "\<bar>x\<bar>\<^sup>2 < 1\<^sup>2", simp)
  3391   apply (rule power_strict_mono, simp, simp, simp)
  3392   apply assumption
  3393   apply assumption
  3394   apply simp
  3395   apply (erule (1) isCont_arccos)
  3396   done
  3397 
  3398 lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
  3399   apply (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
  3400   apply (rule DERIV_cong [OF DERIV_tan])
  3401   apply (rule cos_arctan_not_zero)
  3402   apply (simp add: power_inverse tan_sec [symmetric])
  3403   apply (subgoal_tac "0 < 1 + x\<^sup>2", simp)
  3404   apply (simp add: add_pos_nonneg)
  3405   apply (simp, simp, simp, rule isCont_arctan)
  3406   done
  3407 
  3408 declare
  3409   DERIV_arcsin[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3410   DERIV_arccos[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3411   DERIV_arctan[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
  3412 
  3413 lemma filterlim_tan_at_right: "filterlim tan at_bot (at_right (- pi/2))"
  3414   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])
  3415      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3416            intro!: tan_monotone exI[of _ "pi/2"])
  3417 
  3418 lemma filterlim_tan_at_left: "filterlim tan at_top (at_left (pi/2))"
  3419   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])
  3420      (auto simp: le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
  3421            intro!: tan_monotone exI[of _ "pi/2"])
  3422 
  3423 lemma tendsto_arctan_at_top: "(arctan ---> (pi/2)) at_top"
  3424 proof (rule tendstoI)
  3425   fix e :: real
  3426   assume "0 < e"
  3427   def y \<equiv> "pi/2 - min (pi/2) e"
  3428   then have y: "0 \<le> y" "y < pi/2" "pi/2 \<le> e + y"
  3429     using `0 < e` by auto
  3430 
  3431   show "eventually (\<lambda>x. dist (arctan x) (pi / 2) < e) at_top"
  3432   proof (intro eventually_at_top_dense[THEN iffD2] exI allI impI)
  3433     fix x
  3434     assume "tan y < x"
  3435     then have "arctan (tan y) < arctan x"
  3436       by (simp add: arctan_less_iff)
  3437     with y have "y < arctan x"
  3438       by (subst (asm) arctan_tan) simp_all
  3439     with arctan_ubound[of x, arith] y `0 < e`
  3440     show "dist (arctan x) (pi / 2) < e"
  3441       by (simp add: dist_real_def)
  3442   qed
  3443 qed
  3444 
  3445 lemma tendsto_arctan_at_bot: "(arctan ---> - (pi/2)) at_bot"
  3446   unfolding filterlim_at_bot_mirror arctan_minus
  3447   by (intro tendsto_minus tendsto_arctan_at_top)
  3448 
  3449 
  3450 subsection {* More Theorems about Sin and Cos *}
  3451 
  3452 lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
  3453 proof -
  3454   let ?c = "cos (pi / 4)" and ?s = "sin (pi / 4)"
  3455   have nonneg: "0 \<le> ?c"
  3456     by (simp add: cos_ge_zero)
  3457   have "0 = cos (pi / 4 + pi / 4)"
  3458     by simp
  3459   also have "cos (pi / 4 + pi / 4) = ?c\<^sup>2 - ?s\<^sup>2"
  3460     by (simp only: cos_add power2_eq_square)
  3461   also have "\<dots> = 2 * ?c\<^sup>2 - 1"
  3462     by (simp add: sin_squared_eq)
  3463   finally have "?c\<^sup>2 = (sqrt 2 / 2)\<^sup>2"
  3464     by (simp add: power_divide)
  3465   thus ?thesis
  3466     using nonneg by (rule power2_eq_imp_eq) simp
  3467 qed
  3468 
  3469 lemma cos_30: "cos (pi / 6) = sqrt 3 / 2"
  3470 proof -
  3471   let ?c = "cos (pi / 6)" and ?s = "sin (pi / 6)"
  3472   have pos_c: "0 < ?c"
  3473     by (rule cos_gt_zero, simp, simp)
  3474   have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
  3475     by simp
  3476   also have "\<dots> = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
  3477     by (simp only: cos_add sin_add)
  3478   also have "\<dots> = ?c * (?c\<^sup>2 - 3 * ?s\<^sup>2)"
  3479     by (simp add: algebra_simps power2_eq_square)
  3480   finally have "?c\<^sup>2 = (sqrt 3 / 2)\<^sup>2"
  3481     using pos_c by (simp add: sin_squared_eq power_divide)
  3482   thus ?thesis
  3483     using pos_c [THEN order_less_imp_le]
  3484     by (rule power2_eq_imp_eq) simp
  3485 qed
  3486 
  3487 lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
  3488   by (simp add: sin_cos_eq cos_45)
  3489 
  3490 lemma sin_60: "sin (pi / 3) = sqrt 3 / 2"
  3491   by (simp add: sin_cos_eq cos_30)
  3492 
  3493 lemma cos_60: "cos (pi / 3) = 1 / 2"
  3494   apply (rule power2_eq_imp_eq)
  3495   apply (simp add: cos_squared_eq sin_60 power_divide)
  3496   apply (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
  3497   done
  3498 
  3499 lemma sin_30: "sin (pi / 6) = 1 / 2"
  3500   by (simp add: sin_cos_eq cos_60)
  3501 
  3502 lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
  3503   unfolding tan_def by (simp add: sin_30 cos_30)
  3504 
  3505 lemma tan_45: "tan (pi / 4) = 1"
  3506   unfolding tan_def by (simp add: sin_45 cos_45)
  3507 
  3508 lemma tan_60: "tan (pi / 3) = sqrt 3"
  3509   unfolding tan_def by (simp add: sin_60 cos_60)
  3510 
  3511 lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi / 2) = (-1) ^ n"
  3512 proof -
  3513   have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
  3514     by (auto simp add: algebra_simps sin_add)
  3515   thus ?thesis
  3516     by (simp add: real_of_nat_Suc distrib_right add_divide_distrib
  3517                   mult_commute [of pi])
  3518 qed
  3519 
  3520 lemma cos_2npi [simp]: "cos (2 * real (n::nat) * pi) = 1"
  3521   by (simp add: cos_double mult_assoc power_add [symmetric] numeral_2_eq_2)
  3522 
  3523 lemma cos_3over2_pi [simp]: "cos (3 / 2 * pi) = 0"
  3524   apply (subgoal_tac "cos (pi + pi/2) = 0", simp)
  3525   apply (subst cos_add, simp)
  3526   done
  3527 
  3528 lemma sin_2npi [simp]: "sin (2 * real (n::nat) * pi) = 0"
  3529   by (auto simp add: mult_assoc)
  3530 
  3531 lemma sin_3over2_pi [simp]: "sin (3 / 2 * pi) = - 1"
  3532   apply (subgoal_tac "sin (pi + pi/2) = - 1", simp)
  3533   apply (subst sin_add, simp)
  3534   done
  3535 
  3536 lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
  3537   apply (simp only: cos_add sin_add real_of_nat_Suc distrib_right distrib_left add_divide_distrib)
  3538   apply auto
  3539   done
  3540 
  3541 lemma DERIV_cos_add [simp]: "DERIV (\<lambda>x. cos (x + k)) xa :> - sin (xa + k)"
  3542   by (auto intro!: DERIV_intros)
  3543 
  3544 lemma sin_zero_abs_cos_one: "sin x = 0 ==> \<bar>cos x\<bar> = 1"
  3545   by (auto simp add: sin_zero_iff even_mult_two_ex)
  3546 
  3547 lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
  3548   using sin_cos_squared_add3 [where x = x] by auto
  3549 
  3550 
  3551 subsection {* Machins formula *}
  3552 
  3553 lemma arctan_one: "arctan 1 = pi / 4"
  3554   by (rule arctan_unique, simp_all add: tan_45 m2pi_less_pi)
  3555 
  3556 lemma tan_total_pi4:
  3557   assumes "\<bar>x\<bar> < 1"
  3558   shows "\<exists>z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
  3559 proof
  3560   show "- (pi / 4) < arctan x \<and> arctan x < pi / 4 \<and> tan (arctan x) = x"
  3561     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3562     unfolding arctan_less_iff using assms by auto
  3563 qed
  3564 
  3565 lemma arctan_add:
  3566   assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
  3567   shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
  3568 proof (rule arctan_unique [symmetric])
  3569   have "- (pi / 4) \<le> arctan x" and "- (pi / 4) < arctan y"
  3570     unfolding arctan_one [symmetric] arctan_minus [symmetric]
  3571     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3572   from add_le_less_mono [OF this]
  3573   show 1: "- (pi / 2) < arctan x + arctan y" by simp
  3574   have "arctan x \<le> pi / 4" and "arctan y < pi / 4"
  3575     unfolding arctan_one [symmetric]
  3576     unfolding arctan_le_iff arctan_less_iff using assms by auto
  3577   from add_le_less_mono [OF this]
  3578   show 2: "arctan x + arctan y < pi / 2" by simp
  3579   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
  3580     using cos_gt_zero_pi [OF 1 2] by (simp add: tan_add)
  3581 qed
  3582 
  3583 theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
  3584 proof -
  3585   have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
  3586   from arctan_add[OF less_imp_le[OF this] this]
  3587   have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
  3588   moreover
  3589   have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
  3590   from arctan_add[OF less_imp_le[OF this] this]
  3591   have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
  3592   moreover
  3593   have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
  3594   from arctan_add[OF this]
  3595   have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
  3596   ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
  3597   thus ?thesis unfolding arctan_one by algebra
  3598 qed
  3599 
  3600 
  3601 subsection {* Introducing the arcus tangens power series *}
  3602 
  3603 lemma monoseq_arctan_series:
  3604   fixes x :: real
  3605   assumes "\<bar>x\<bar> \<le> 1"
  3606   shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
  3607 proof (cases "x = 0")
  3608   case True
  3609   thus ?thesis unfolding monoseq_def One_nat_def by auto
  3610 next
  3611   case False
  3612   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3613   show "monoseq ?a"
  3614   proof -
  3615     {
  3616       fix n
  3617       fix x :: real
  3618       assume "0 \<le> x" and "x \<le> 1"
  3619       have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le>
  3620         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
  3621       proof (rule mult_mono)
  3622         show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))"
  3623           by (rule frac_le) simp_all
  3624         show "0 \<le> 1 / real (Suc (n * 2))"
  3625           by auto
  3626         show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)"
  3627           by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
  3628         show "0 \<le> x ^ Suc (Suc n * 2)"
  3629           by (rule zero_le_power) (simp add: `0 \<le> x`)
  3630       qed
  3631     } note mono = this
  3632 
  3633     show ?thesis
  3634     proof (cases "0 \<le> x")
  3635       case True from mono[OF this `x \<le> 1`, THEN allI]
  3636       show ?thesis unfolding Suc_eq_plus1[symmetric]
  3637         by (rule mono_SucI2)
  3638     next
  3639       case False
  3640       hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
  3641       from mono[OF this]
  3642       have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge>
  3643         1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
  3644       thus ?thesis unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI1[OF allI])
  3645     qed
  3646   qed
  3647 qed
  3648 
  3649 lemma zeroseq_arctan_series:
  3650   fixes x :: real
  3651   assumes "\<bar>x\<bar> \<le> 1"
  3652   shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
  3653 proof (cases "x = 0")
  3654   case True
  3655   thus ?thesis
  3656     unfolding One_nat_def by (auto simp add: tendsto_const)
  3657 next
  3658   case False
  3659   have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
  3660   show "?a ----> 0"
  3661   proof (cases "\<bar>x\<bar> < 1")
  3662     case True
  3663     hence "norm x < 1" by auto
  3664     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
  3665     have "(\<lambda>n. 1 / real (n + 1) * x ^ (n + 1)) ----> 0"
  3666       unfolding inverse_eq_divide Suc_eq_plus1 by simp
  3667     then show ?thesis using pos2 by (rule LIMSEQ_linear)
  3668   next
  3669     case False
  3670     hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3671     hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x"
  3672       unfolding One_nat_def by auto
  3673     from tendsto_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] tendsto_const[of x]]
  3674     show ?thesis unfolding n_eq Suc_eq_plus1 by auto
  3675   qed
  3676 qed
  3677 
  3678 lemma summable_arctan_series:
  3679   fixes x :: real and n :: nat
  3680   assumes "\<bar>x\<bar> \<le> 1"
  3681   shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3682   (is "summable (?c x)")
  3683   by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
  3684 
  3685 lemma less_one_imp_sqr_less_one:
  3686   fixes x :: real
  3687   assumes "\<bar>x\<bar> < 1"
  3688   shows "x\<^sup>2 < 1"
  3689 proof -
  3690   from mult_left_mono[OF less_imp_le[OF `\<bar>x\<bar> < 1`] abs_ge_zero[of x]]
  3691   have "\<bar>x\<^sup>2\<bar> < 1" using `\<bar>x\<bar> < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
  3692   thus ?thesis using zero_le_power2 by auto
  3693 qed
  3694 
  3695 lemma DERIV_arctan_series:
  3696   assumes "\<bar> x \<bar> < 1"
  3697   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))"
  3698   (is "DERIV ?arctan _ :> ?Int")
  3699 proof -
  3700   let ?f = "\<lambda>n. if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
  3701 
  3702   have n_even: "\<And>n :: nat. even n \<Longrightarrow> 2 * (n div 2) = n"
  3703     by presburger
  3704   then have if_eq: "\<And>n x'. ?f n * real (Suc n) * x'^n =
  3705     (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)"
  3706     by auto
  3707 
  3708   {
  3709     fix x :: real
  3710     assume "\<bar>x\<bar> < 1"
  3711     hence "x\<^sup>2 < 1" by (rule less_one_imp_sqr_less_one)
  3712     have "summable (\<lambda> n. -1 ^ n * (x\<^sup>2) ^n)"
  3713       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`])
  3714     hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
  3715   } note summable_Integral = this
  3716 
  3717   {
  3718     fix f :: "nat \<Rightarrow> real"
  3719     have "\<And>x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3720     proof
  3721       fix x :: real
  3722       assume "f sums x"
  3723       from sums_if[OF sums_zero this]
  3724       show "(\<lambda>n. if even n then f (n div 2) else 0) sums x"
  3725         by auto
  3726     next
  3727       fix x :: real
  3728       assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
  3729       from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
  3730       show "f sums x" unfolding sums_def by auto
  3731     qed
  3732     hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
  3733   } note sums_even = this
  3734 
  3735   have Int_eq: "(\<Sum>n. ?f n * real (Suc n) * x^n) = ?Int"
  3736     unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
  3737     by auto
  3738 
  3739   {
  3740     fix x :: real
  3741     have if_eq': "\<And>n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
  3742       (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
  3743       using n_even by auto
  3744     have idx_eq: "\<And>n. n * 2 + 1 = Suc (2 * n)" by auto
  3745     have "(\<Sum>n. ?f n * x^(Suc n)) = ?arctan x"
  3746       unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
  3747       by auto
  3748   } note arctan_eq = this
  3749 
  3750   have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
  3751   proof (rule DERIV_power_series')
  3752     show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
  3753     {
  3754       fix x' :: real
  3755       assume x'_bounds: "x' \<in> {- 1 <..< 1}"
  3756       hence "\<bar>x'\<bar> < 1" by auto
  3757 
  3758       let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
  3759       show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
  3760         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`])
  3761     }
  3762   qed auto
  3763   thus ?thesis unfolding Int_eq arctan_eq .
  3764 qed
  3765 
  3766 lemma arctan_series:
  3767   assumes "\<bar> x \<bar> \<le> 1"
  3768   shows "arctan x = (\<Sum>k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
  3769   (is "_ = suminf (\<lambda> n. ?c x n)")
  3770 proof -
  3771   let ?c' = "\<lambda>x n. (-1)^n * x^(n*2)"
  3772 
  3773   {
  3774     fix r x :: real
  3775     assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
  3776     have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
  3777     from DERIV_arctan_series[OF this] have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
  3778   } note DERIV_arctan_suminf = this
  3779 
  3780   {
  3781     fix x :: real
  3782     assume "\<bar>x\<bar> \<le> 1"
  3783     note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]]
  3784   } note arctan_series_borders = this
  3785 
  3786   {
  3787     fix x :: real
  3788     assume "\<bar>x\<bar> < 1"
  3789     have "arctan x = (\<Sum>k. ?c x k)"
  3790     proof -
  3791       obtain r where "\<bar>x\<bar> < r" and "r < 1"
  3792         using dense[OF `\<bar>x\<bar> < 1`] by blast
  3793       hence "0 < r" and "-r < x" and "x < r" by auto
  3794 
  3795       have suminf_eq_arctan_bounded: "\<And>x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow>
  3796         suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3797       proof -
  3798         fix x a b
  3799         assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
  3800         hence "\<bar>x\<bar> < r" by auto
  3801         show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
  3802         proof (rule DERIV_isconst2[of "a" "b"])
  3803           show "a < b" and "a \<le> x" and "x \<le> b"
  3804             using `a < b` `a \<le> x` `x \<le> b` by auto
  3805           have "\<forall>x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3806           proof (rule allI, rule impI)
  3807             fix x
  3808             assume "-r < x \<and> x < r"
  3809             hence "\<bar>x\<bar> < r" by auto
  3810             hence "\<bar>x\<bar> < 1" using `r < 1` by auto
  3811             have "\<bar> - (x\<^sup>2) \<bar> < 1"
  3812               using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
  3813             hence "(\<lambda> n. (- (x\<^sup>2)) ^ n) sums (1 / (1 - (- (x\<^sup>2))))"
  3814               unfolding real_norm_def[symmetric] by (rule geometric_sums)
  3815             hence "(?c' x) sums (1 / (1 - (- (x\<^sup>2))))"
  3816               unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
  3817             hence suminf_c'_eq_geom: "inverse (1 + x\<^sup>2) = suminf (?c' x)"
  3818               using sums_unique unfolding inverse_eq_divide by auto
  3819             have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x\<^sup>2))"
  3820               unfolding suminf_c'_eq_geom
  3821               by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
  3822             from DERIV_add_minus[OF this DERIV_arctan]
  3823             show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
  3824               unfolding diff_minus by auto
  3825           qed
  3826           hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3827             using `-r < a` `b < r` by auto
  3828           thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0"
  3829             using `\<bar>x\<bar> < r` by auto
  3830           show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y"
  3831             using DERIV_in_rball DERIV_isCont by auto
  3832         qed
  3833       qed
  3834 
  3835       have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
  3836         unfolding Suc_eq_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero
  3837         by auto
  3838 
  3839       have "suminf (?c x) - arctan x = 0"
  3840       proof (cases "x = 0")
  3841         case True
  3842         thus ?thesis using suminf_arctan_zero by auto
  3843       next
  3844         case False
  3845         hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
  3846         have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
  3847           by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric])
  3848             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3849         moreover
  3850         have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
  3851           by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"])
  3852             (simp_all only: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>` neg_less_iff_less)
  3853         ultimately
  3854         show ?thesis using suminf_arctan_zero by auto
  3855       qed
  3856       thus ?thesis by auto
  3857     qed
  3858   } note when_less_one = this
  3859 
  3860   show "arctan x = suminf (\<lambda> n. ?c x n)"
  3861   proof (cases "\<bar>x\<bar> < 1")
  3862     case True
  3863     thus ?thesis by (rule when_less_one)
  3864   next
  3865     case False
  3866     hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
  3867     let ?a = "\<lambda>x n. \<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
  3868     let ?diff = "\<lambda> x n. \<bar> arctan x - (\<Sum> i = 0..<n. ?c x i)\<bar>"
  3869     {
  3870       fix n :: nat
  3871       have "0 < (1 :: real)" by auto
  3872       moreover
  3873       {
  3874         fix x :: real
  3875         assume "0 < x" and "x < 1"
  3876         hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
  3877         from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)"
  3878           by auto
  3879         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]
  3880         have "0 < 1 / real (n*2+1) * x^(n*2+1)"
  3881           by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
  3882         hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)"
  3883           by (rule abs_of_pos)
  3884         have "?diff x n \<le> ?a x n"
  3885         proof (cases "even n")
  3886           case True
  3887           hence sgn_pos: "(-1)^n = (1::real)" by auto
  3888           from `even n` obtain m where "2 * m = n"
  3889             unfolding even_mult_two_ex by auto
  3890           from bounds[of m, unfolded this atLeastAtMost_iff]
  3891           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))"
  3892             by auto
  3893           also have "\<dots> = ?c x n" unfolding One_nat_def by auto
  3894           also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
  3895           finally show ?thesis .
  3896         next
  3897           case False
  3898           hence sgn_neg: "(-1)^n = (-1::real)" by auto
  3899           from `odd n` obtain m where m_def: "2 * m + 1 = n"
  3900             unfolding odd_Suc_mult_two_ex by auto
  3901           hence m_plus: "2 * (m + 1) = n + 1" by auto
  3902           from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
  3903           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))"
  3904             by auto
  3905           also have "\<dots> = - ?c x n" unfolding One_nat_def by auto
  3906           also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
  3907           finally show ?thesis .
  3908         qed
  3909         hence "0 \<le> ?a x n - ?diff x n" by auto
  3910       }
  3911       hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
  3912       moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
  3913         unfolding diff_minus divide_inverse
  3914         by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan
  3915           isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum)
  3916       ultimately have "0 \<le> ?a 1 n - ?diff 1 n"
  3917         by (rule LIM_less_bound)
  3918       hence "?diff 1 n \<le> ?a 1 n" by auto
  3919     }
  3920     have "?a 1 ----> 0"
  3921       unfolding tendsto_rabs_zero_iff power_one divide_inverse One_nat_def
  3922       by (auto intro!: tendsto_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
  3923     have "?diff 1 ----> 0"
  3924     proof (rule LIMSEQ_I)
  3925       fix r :: real
  3926       assume "0 < r"
  3927       obtain N :: nat where N_I: "\<And>n. N \<le> n \<Longrightarrow> ?a 1 n < r"
  3928         using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
  3929       {
  3930         fix n
  3931         assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
  3932         have "norm (?diff 1 n - 0) < r" by auto
  3933       }
  3934       thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
  3935     qed
  3936     from this [unfolded tendsto_rabs_zero_iff, THEN tendsto_add [OF _ tendsto_const], of "- arctan 1", THEN tendsto_minus]
  3937     have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
  3938     hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
  3939 
  3940     show ?thesis
  3941     proof (cases "x = 1")
  3942       case True
  3943       then show ?thesis by (simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
  3944     next
  3945       case False
  3946       hence "x = -1" using `\<bar>x\<bar> = 1` by auto
  3947 
  3948       have "- (pi / 2) < 0" using pi_gt_zero by auto
  3949       have "- (2 * pi) < 0" using pi_gt_zero by auto
  3950 
  3951       have c_minus_minus: "\<And>i. ?c (- 1) i = - ?c 1 i"
  3952         unfolding One_nat_def by auto
  3953 
  3954       have "arctan (- 1) = arctan (tan (-(pi / 4)))"
  3955         unfolding tan_45 tan_minus ..
  3956       also have "\<dots> = - (pi / 4)"
  3957         by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
  3958       also have "\<dots> = - (arctan (tan (pi / 4)))"
  3959         unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
  3960       also have "\<dots> = - (arctan 1)"
  3961         unfolding tan_45 ..
  3962       also have "\<dots> = - (\<Sum> i. ?c 1 i)"
  3963         using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
  3964       also have "\<dots> = (\<Sum> i. ?c (- 1) i)"
  3965         using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]]
  3966         unfolding c_minus_minus by auto
  3967       finally show ?thesis using `x = -1` by auto
  3968     qed
  3969   qed
  3970 qed
  3971 
  3972 lemma arctan_half:
  3973   fixes x :: real
  3974   shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x\<^sup>2)))"
  3975 proof -
  3976   obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x"
  3977     using tan_total by blast
  3978   hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2"
  3979     by auto
  3980 
  3981   have divide_nonzero_divide: "\<And>A B C :: real. C \<noteq> 0 \<Longrightarrow> A / B = (A / C) / (B / C)"
  3982     by auto
  3983 
  3984   have "0 < cos y" using cos_gt_zero_pi[OF low high] .
  3985   hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y)\<^sup>2) = cos y"
  3986     by auto
  3987 
  3988   have "1 + (tan y)\<^sup>2 = 1 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  3989     unfolding tan_def power_divide ..
  3990   also have "\<dots> = (cos y)\<^sup>2 / (cos y)\<^sup>2 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
  3991     using `cos y \<noteq> 0` by auto
  3992   also have "\<dots> = 1 / (cos y)\<^sup>2"
  3993     unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
  3994   finally have "1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2" .
  3995 
  3996   have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)"
  3997     unfolding tan_def divide_nonzero_divide[OF `cos y \<noteq> 0`, symmetric] ..
  3998   also have "\<dots> = tan y / (1 + 1 / cos y)"
  3999     using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
  4000   also have "\<dots> = tan y / (1 + 1 / sqrt ((cos y)\<^sup>2))"
  4001     unfolding cos_sqrt ..
  4002   also have "\<dots> = tan y / (1 + sqrt (1 / (cos y)\<^sup>2))"
  4003     unfolding real_sqrt_divide by auto
  4004   finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)\<^sup>2))"
  4005     unfolding `1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2` .
  4006 
  4007   have "arctan x = y"
  4008     using arctan_tan low high y_eq by auto
  4009   also have "\<dots> = 2 * (arctan (tan (y/2)))"
  4010     using arctan_tan[OF low2 high2] by auto
  4011   also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))"
  4012     unfolding tan_half by auto
  4013   finally show ?thesis
  4014     unfolding eq `tan y = x` .
  4015 qed
  4016 
  4017 lemma arctan_monotone: "x < y \<Longrightarrow> arctan x < arctan y"
  4018   by (simp only: arctan_less_iff)
  4019 
  4020 lemma arctan_monotone': "x \<le> y \<Longrightarrow> arctan x \<le> arctan y"
  4021   by (simp only: arctan_le_iff)
  4022 
  4023 lemma arctan_inverse:
  4024   assumes "x \<noteq> 0"
  4025   shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
  4026 proof (rule arctan_unique)
  4027   show "- (pi / 2) < sgn x * pi / 2 - arctan x"
  4028     using arctan_bounded [of x] assms
  4029     unfolding sgn_real_def
  4030     apply (auto simp add: algebra_simps)
  4031     apply (drule zero_less_arctan_iff [THEN iffD2])
  4032     apply arith
  4033     done
  4034   show "sgn x * pi / 2 - arctan x < pi / 2"
  4035     using arctan_bounded [of "- x"] assms
  4036     unfolding sgn_real_def arctan_minus
  4037     by auto
  4038   show "tan (sgn x * pi / 2 - arctan x) = 1 / x"
  4039     unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
  4040     unfolding sgn_real_def
  4041     by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
  4042 qed
  4043 
  4044 theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
  4045 proof -
  4046   have "pi / 4 = arctan 1" using arctan_one by auto
  4047   also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
  4048   finally show ?thesis by auto
  4049 qed
  4050 
  4051 
  4052 subsection {* Existence of Polar Coordinates *}
  4053 
  4054 lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
  4055   apply (rule power2_le_imp_le [OF _ zero_le_one])
  4056   apply (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
  4057   done
  4058 
  4059 lemma cos_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> cos (arccos y) = y"
  4060   by (simp add: abs_le_iff)
  4061 
  4062 lemma sin_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> sin (arccos y) = sqrt (1 - y\<^sup>2)"
  4063   by (simp add: sin_arccos abs_le_iff)
  4064 
  4065 lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
  4066 
  4067 lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
  4068 
  4069 lemma polar_ex1: "0 < y \<Longrightarrow> \<exists>r a. x = r * cos a & y = r * sin a"
  4070   apply (rule_tac x = "sqrt (x\<^sup>2 + y\<^sup>2)" in exI)
  4071   apply (rule_tac x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))" in exI)
  4072   apply (simp add: cos_arccos_lemma1)
  4073   apply (simp add: sin_arccos_lemma1)
  4074   apply (simp add: power_divide)
  4075   apply (simp add: real_sqrt_mult [symmetric])
  4076   apply (simp add: right_diff_distrib)
  4077   done
  4078 
  4079 lemma polar_ex2: "y < 0 ==> \<exists>r a. x = r * cos a & y = r * sin a"
  4080   using polar_ex1 [where x=x and y="-y"]
  4081   apply simp
  4082   apply clarify
  4083   apply (metis cos_minus minus_minus minus_mult_right sin_minus)
  4084   done
  4085 
  4086 lemma polar_Ex: "\<exists>r a. x = r * cos a & y = r * sin a"
  4087   apply (rule_tac x=0 and y=y in linorder_cases)
  4088   apply (erule polar_ex1)
  4089   apply (rule_tac x=x in exI, rule_tac x=0 in exI, simp)
  4090   apply (erule polar_ex2)
  4091   done
  4092 
  4093 end