src/HOL/Transcendental.thy
author paulson <lp15@cam.ac.uk>
Tue Mar 17 12:23:56 2015 +0000 (2015-03-17)
changeset 59731 7fccaeec22f0
parent 59730 b7c394c7a619
parent 59688 6c896dfef33b
child 59741 5b762cd73a8e
permissions -rw-r--r--
Merge
wenzelm@32960
     1
(*  Title:      HOL/Transcendental.thy
wenzelm@32960
     2
    Author:     Jacques D. Fleuriot, University of Cambridge, University of Edinburgh
wenzelm@32960
     3
    Author:     Lawrence C Paulson
hoelzl@51527
     4
    Author:     Jeremy Avigad
paulson@12196
     5
*)
paulson@12196
     6
wenzelm@58889
     7
section{*Power Series, Transcendental Functions etc.*}
paulson@15077
     8
nipkow@15131
     9
theory Transcendental
lp15@59669
    10
imports Binomial Series Deriv NthRoot
nipkow@15131
    11
begin
paulson@15077
    12
lp15@59730
    13
lemma of_real_fact [simp]: "of_real (fact n) = fact n"
lp15@59730
    14
  by (metis of_nat_fact of_real_of_nat_eq)
lp15@59730
    15
lp15@59730
    16
lemma real_fact_nat [simp]: "real (fact n :: nat) = fact n"
lp15@59730
    17
  by (simp add: real_of_nat_def)
lp15@59730
    18
lp15@59731
    19
lemma real_fact_int [simp]: "real (fact n :: int) = fact n"
lp15@59731
    20
  by (metis of_int_of_nat_eq of_nat_fact real_of_int_def)
lp15@59731
    21
hoelzl@57025
    22
lemma root_test_convergence:
hoelzl@57025
    23
  fixes f :: "nat \<Rightarrow> 'a::banach"
hoelzl@57025
    24
  assumes f: "(\<lambda>n. root n (norm (f n))) ----> x" -- "could be weakened to lim sup"
hoelzl@57025
    25
  assumes "x < 1"
hoelzl@57025
    26
  shows "summable f"
hoelzl@57025
    27
proof -
hoelzl@57025
    28
  have "0 \<le> x"
hoelzl@57025
    29
    by (rule LIMSEQ_le[OF tendsto_const f]) (auto intro!: exI[of _ 1])
hoelzl@57025
    30
  from `x < 1` obtain z where z: "x < z" "z < 1"
hoelzl@57025
    31
    by (metis dense)
hoelzl@57025
    32
  from f `x < z`
hoelzl@57025
    33
  have "eventually (\<lambda>n. root n (norm (f n)) < z) sequentially"
hoelzl@57025
    34
    by (rule order_tendstoD)
hoelzl@57025
    35
  then have "eventually (\<lambda>n. norm (f n) \<le> z^n) sequentially"
hoelzl@57025
    36
    using eventually_ge_at_top
hoelzl@57025
    37
  proof eventually_elim
hoelzl@57025
    38
    fix n assume less: "root n (norm (f n)) < z" and n: "1 \<le> n"
hoelzl@57025
    39
    from power_strict_mono[OF less, of n] n
hoelzl@57025
    40
    show "norm (f n) \<le> z ^ n"
hoelzl@57025
    41
      by simp
hoelzl@57025
    42
  qed
hoelzl@57025
    43
  then show "summable f"
hoelzl@57025
    44
    unfolding eventually_sequentially
hoelzl@57025
    45
    using z `0 \<le> x` by (auto intro!: summable_comparison_test[OF _  summable_geometric])
hoelzl@57025
    46
qed
hoelzl@57025
    47
huffman@29164
    48
subsection {* Properties of Power Series *}
paulson@15077
    49
huffman@23082
    50
lemma lemma_realpow_diff:
haftmann@31017
    51
  fixes y :: "'a::monoid_mult"
huffman@23082
    52
  shows "p \<le> n \<Longrightarrow> y ^ (Suc n - p) = (y ^ (n - p)) * y"
huffman@23082
    53
proof -
huffman@23082
    54
  assume "p \<le> n"
huffman@23082
    55
  hence "Suc n - p = Suc (n - p)" by (rule Suc_diff_le)
huffman@30273
    56
  thus ?thesis by (simp add: power_commutes)
huffman@23082
    57
qed
paulson@15077
    58
paulson@15229
    59
lemma lemma_realpow_diff_sumr2:
wenzelm@53079
    60
  fixes y :: "'a::{comm_ring,monoid_mult}"
wenzelm@53079
    61
  shows
wenzelm@53079
    62
    "x ^ (Suc n) - y ^ (Suc n) =
hoelzl@56193
    63
      (x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))"
paulson@54573
    64
proof (induct n)
paulson@54573
    65
  case (Suc n)
lp15@59730
    66
  have "x ^ Suc (Suc n) - y ^ Suc (Suc n) = x * (x * x^n) - y * (y * y ^ n)"
paulson@54573
    67
    by simp
lp15@59730
    68
  also have "... = y * (x ^ (Suc n) - y ^ (Suc n)) + (x - y) * (x * x^n)"
paulson@54573
    69
    by (simp add: algebra_simps)
lp15@59730
    70
  also have "... = y * ((x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
paulson@54573
    71
    by (simp only: Suc)
lp15@59730
    72
  also have "... = (x - y) * (y * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
haftmann@57512
    73
    by (simp only: mult.left_commute)
hoelzl@56193
    74
  also have "... = (x - y) * (\<Sum>p<Suc (Suc n). x ^ p * y ^ (Suc n - p))"
hoelzl@56193
    75
    by (simp add: field_simps Suc_diff_le setsum_left_distrib setsum_right_distrib)
paulson@54573
    76
  finally show ?case .
hoelzl@56193
    77
qed simp
paulson@15077
    78
wenzelm@55832
    79
corollary power_diff_sumr2: --{* @{text COMPLEX_POLYFUN} in HOL Light *}
lp15@55734
    80
  fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193
    81
  shows   "x^n - y^n = (x - y) * (\<Sum>i<n. y^(n - Suc i) * x^i)"
lp15@55734
    82
using lemma_realpow_diff_sumr2[of x "n - 1" y]
lp15@55734
    83
by (cases "n = 0") (simp_all add: field_simps)
lp15@55734
    84
paulson@15229
    85
lemma lemma_realpow_rev_sumr:
hoelzl@56193
    86
   "(\<Sum>p<Suc n. (x ^ p) * (y ^ (n - p))) =
hoelzl@56193
    87
    (\<Sum>p<Suc n. (x ^ (n - p)) * (y ^ p))"
hoelzl@57129
    88
  by (subst nat_diff_setsum_reindex[symmetric]) simp
paulson@15077
    89
lp15@55719
    90
lemma power_diff_1_eq:
lp15@55719
    91
  fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193
    92
  shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"
lp15@59669
    93
using lemma_realpow_diff_sumr2 [of x _ 1]
lp15@55719
    94
  by (cases n) auto
lp15@55719
    95
lp15@55719
    96
lemma one_diff_power_eq':
lp15@55719
    97
  fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193
    98
  shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"
lp15@59669
    99
using lemma_realpow_diff_sumr2 [of 1 _ x]
lp15@55719
   100
  by (cases n) auto
lp15@55719
   101
lp15@55719
   102
lemma one_diff_power_eq:
lp15@55719
   103
  fixes x :: "'a::{comm_ring,monoid_mult}"
hoelzl@56193
   104
  shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^i)"
lp15@55719
   105
by (metis one_diff_power_eq' [of n x] nat_diff_setsum_reindex)
lp15@55719
   106
paulson@15077
   107
text{*Power series has a `circle` of convergence, i.e. if it sums for @{term
wenzelm@53079
   108
  x}, then it sums absolutely for @{term z} with @{term "\<bar>z\<bar> < \<bar>x\<bar>"}.*}
paulson@15077
   109
paulson@15077
   110
lemma powser_insidea:
huffman@53599
   111
  fixes x z :: "'a::real_normed_div_algebra"
lp15@59730
   112
  assumes 1: "summable (\<lambda>n. f n * x^n)"
wenzelm@53079
   113
    and 2: "norm z < norm x"
huffman@23082
   114
  shows "summable (\<lambda>n. norm (f n * z ^ n))"
huffman@20849
   115
proof -
huffman@20849
   116
  from 2 have x_neq_0: "x \<noteq> 0" by clarsimp
lp15@59730
   117
  from 1 have "(\<lambda>n. f n * x^n) ----> 0"
huffman@20849
   118
    by (rule summable_LIMSEQ_zero)
lp15@59730
   119
  hence "convergent (\<lambda>n. f n * x^n)"
huffman@20849
   120
    by (rule convergentI)
lp15@59730
   121
  hence "Cauchy (\<lambda>n. f n * x^n)"
huffman@44726
   122
    by (rule convergent_Cauchy)
lp15@59730
   123
  hence "Bseq (\<lambda>n. f n * x^n)"
huffman@20849
   124
    by (rule Cauchy_Bseq)
lp15@59730
   125
  then obtain K where 3: "0 < K" and 4: "\<forall>n. norm (f n * x^n) \<le> K"
huffman@20849
   126
    by (simp add: Bseq_def, safe)
huffman@23082
   127
  have "\<exists>N. \<forall>n\<ge>N. norm (norm (f n * z ^ n)) \<le>
lp15@59730
   128
                   K * norm (z ^ n) * inverse (norm (x^n))"
huffman@20849
   129
  proof (intro exI allI impI)
wenzelm@53079
   130
    fix n::nat
wenzelm@53079
   131
    assume "0 \<le> n"
lp15@59730
   132
    have "norm (norm (f n * z ^ n)) * norm (x^n) =
lp15@59730
   133
          norm (f n * x^n) * norm (z ^ n)"
huffman@23082
   134
      by (simp add: norm_mult abs_mult)
huffman@23082
   135
    also have "\<dots> \<le> K * norm (z ^ n)"
huffman@23082
   136
      by (simp only: mult_right_mono 4 norm_ge_zero)
lp15@59730
   137
    also have "\<dots> = K * norm (z ^ n) * (inverse (norm (x^n)) * norm (x^n))"
huffman@20849
   138
      by (simp add: x_neq_0)
lp15@59730
   139
    also have "\<dots> = K * norm (z ^ n) * inverse (norm (x^n)) * norm (x^n)"
haftmann@57512
   140
      by (simp only: mult.assoc)
huffman@23082
   141
    finally show "norm (norm (f n * z ^ n)) \<le>
lp15@59730
   142
                  K * norm (z ^ n) * inverse (norm (x^n))"
huffman@20849
   143
      by (simp add: mult_le_cancel_right x_neq_0)
huffman@20849
   144
  qed
lp15@59730
   145
  moreover have "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x^n)))"
huffman@20849
   146
  proof -
huffman@23082
   147
    from 2 have "norm (norm (z * inverse x)) < 1"
huffman@23082
   148
      using x_neq_0
huffman@53599
   149
      by (simp add: norm_mult nonzero_norm_inverse divide_inverse [where 'a=real, symmetric])
huffman@23082
   150
    hence "summable (\<lambda>n. norm (z * inverse x) ^ n)"
huffman@20849
   151
      by (rule summable_geometric)
huffman@23082
   152
    hence "summable (\<lambda>n. K * norm (z * inverse x) ^ n)"
huffman@20849
   153
      by (rule summable_mult)
lp15@59730
   154
    thus "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x^n)))"
huffman@23082
   155
      using x_neq_0
huffman@23082
   156
      by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
haftmann@57512
   157
                    power_inverse norm_power mult.assoc)
huffman@20849
   158
  qed
huffman@23082
   159
  ultimately show "summable (\<lambda>n. norm (f n * z ^ n))"
huffman@20849
   160
    by (rule summable_comparison_test)
huffman@20849
   161
qed
paulson@15077
   162
paulson@15229
   163
lemma powser_inside:
huffman@53599
   164
  fixes f :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}"
wenzelm@53079
   165
  shows
lp15@59730
   166
    "summable (\<lambda>n. f n * (x^n)) \<Longrightarrow> norm z < norm x \<Longrightarrow>
wenzelm@53079
   167
      summable (\<lambda>n. f n * (z ^ n))"
wenzelm@53079
   168
  by (rule powser_insidea [THEN summable_norm_cancel])
wenzelm@53079
   169
wenzelm@53079
   170
lemma sum_split_even_odd:
wenzelm@53079
   171
  fixes f :: "nat \<Rightarrow> real"
wenzelm@53079
   172
  shows
hoelzl@56193
   173
    "(\<Sum>i<2 * n. if even i then f i else g i) =
hoelzl@56193
   174
     (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1))"
hoelzl@29803
   175
proof (induct n)
wenzelm@53079
   176
  case 0
wenzelm@53079
   177
  then show ?case by simp
wenzelm@53079
   178
next
hoelzl@29803
   179
  case (Suc n)
hoelzl@56193
   180
  have "(\<Sum>i<2 * Suc n. if even i then f i else g i) =
hoelzl@56193
   181
    (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
huffman@30082
   182
    using Suc.hyps unfolding One_nat_def by auto
hoelzl@56193
   183
  also have "\<dots> = (\<Sum>i<Suc n. f (2 * i)) + (\<Sum>i<Suc n. g (2 * i + 1))"
wenzelm@53079
   184
    by auto
hoelzl@29803
   185
  finally show ?case .
wenzelm@53079
   186
qed
wenzelm@53079
   187
wenzelm@53079
   188
lemma sums_if':
wenzelm@53079
   189
  fixes g :: "nat \<Rightarrow> real"
wenzelm@53079
   190
  assumes "g sums x"
hoelzl@29803
   191
  shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
hoelzl@29803
   192
  unfolding sums_def
hoelzl@29803
   193
proof (rule LIMSEQ_I)
wenzelm@53079
   194
  fix r :: real
wenzelm@53079
   195
  assume "0 < r"
hoelzl@29803
   196
  from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
hoelzl@56193
   197
  obtain no where no_eq: "\<And> n. n \<ge> no \<Longrightarrow> (norm (setsum g {..<n} - x) < r)" by blast
hoelzl@56193
   198
hoelzl@56193
   199
  let ?SUM = "\<lambda> m. \<Sum>i<m. if even i then 0 else g ((i - 1) div 2)"
wenzelm@53079
   200
  {
wenzelm@53079
   201
    fix m
wenzelm@53079
   202
    assume "m \<ge> 2 * no"
wenzelm@53079
   203
    hence "m div 2 \<ge> no" by auto
hoelzl@56193
   204
    have sum_eq: "?SUM (2 * (m div 2)) = setsum g {..< m div 2}"
hoelzl@29803
   205
      using sum_split_even_odd by auto
wenzelm@53079
   206
    hence "(norm (?SUM (2 * (m div 2)) - x) < r)"
wenzelm@53079
   207
      using no_eq unfolding sum_eq using `m div 2 \<ge> no` by auto
hoelzl@29803
   208
    moreover
hoelzl@29803
   209
    have "?SUM (2 * (m div 2)) = ?SUM m"
hoelzl@29803
   210
    proof (cases "even m")
wenzelm@53079
   211
      case True
haftmann@58710
   212
      then show ?thesis by (auto simp add: even_two_times_div_two)
hoelzl@29803
   213
    next
wenzelm@53079
   214
      case False
haftmann@58834
   215
      then have eq: "Suc (2 * (m div 2)) = m" by simp
hoelzl@29803
   216
      hence "even (2 * (m div 2))" using `odd m` by auto
hoelzl@29803
   217
      have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
hoelzl@29803
   218
      also have "\<dots> = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
hoelzl@29803
   219
      finally show ?thesis by auto
hoelzl@29803
   220
    qed
hoelzl@29803
   221
    ultimately have "(norm (?SUM m - x) < r)" by auto
hoelzl@29803
   222
  }
hoelzl@29803
   223
  thus "\<exists> no. \<forall> m \<ge> no. norm (?SUM m - x) < r" by blast
hoelzl@29803
   224
qed
hoelzl@29803
   225
wenzelm@53079
   226
lemma sums_if:
wenzelm@53079
   227
  fixes g :: "nat \<Rightarrow> real"
wenzelm@53079
   228
  assumes "g sums x" and "f sums y"
hoelzl@29803
   229
  shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
hoelzl@29803
   230
proof -
hoelzl@29803
   231
  let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
wenzelm@53079
   232
  {
wenzelm@53079
   233
    fix B T E
wenzelm@53079
   234
    have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
wenzelm@53079
   235
      by (cases B) auto
wenzelm@53079
   236
  } note if_sum = this
wenzelm@53079
   237
  have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
wenzelm@53079
   238
    using sums_if'[OF `g sums x`] .
hoelzl@41970
   239
  {
wenzelm@41550
   240
    have if_eq: "\<And>B T E. (if \<not> B then T else E) = (if B then E else T)" by auto
hoelzl@29803
   241
hoelzl@29803
   242
    have "?s sums y" using sums_if'[OF `f sums y`] .
hoelzl@41970
   243
    from this[unfolded sums_def, THEN LIMSEQ_Suc]
hoelzl@29803
   244
    have "(\<lambda> n. if even n then f (n div 2) else 0) sums y"
haftmann@57418
   245
      by (simp add: lessThan_Suc_eq_insert_0 image_iff setsum.reindex if_eq sums_def cong del: if_cong)
wenzelm@53079
   246
  }
wenzelm@53079
   247
  from sums_add[OF g_sums this] show ?thesis unfolding if_sum .
hoelzl@29803
   248
qed
hoelzl@29803
   249
hoelzl@29803
   250
subsection {* Alternating series test / Leibniz formula *}
hoelzl@29803
   251
hoelzl@29803
   252
lemma sums_alternating_upper_lower:
hoelzl@29803
   253
  fixes a :: "nat \<Rightarrow> real"
hoelzl@29803
   254
  assumes mono: "\<And>n. a (Suc n) \<le> a n" and a_pos: "\<And>n. 0 \<le> a n" and "a ----> 0"
haftmann@58410
   255
  shows "\<exists>l. ((\<forall>n. (\<Sum>i<2*n. (- 1)^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i<2*n. (- 1)^i*a i) ----> l) \<and>
haftmann@58410
   256
             ((\<forall>n. l \<le> (\<Sum>i<2*n + 1. (- 1)^i*a i)) \<and> (\<lambda> n. \<Sum>i<2*n + 1. (- 1)^i*a i) ----> l)"
hoelzl@29803
   257
  (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
wenzelm@53079
   258
proof (rule nested_sequence_unique)
huffman@30082
   259
  have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" unfolding One_nat_def by auto
hoelzl@29803
   260
wenzelm@53079
   261
  show "\<forall>n. ?f n \<le> ?f (Suc n)"
wenzelm@53079
   262
  proof
wenzelm@53079
   263
    fix n
wenzelm@53079
   264
    show "?f n \<le> ?f (Suc n)" using mono[of "2*n"] by auto
wenzelm@53079
   265
  qed
wenzelm@53079
   266
  show "\<forall>n. ?g (Suc n) \<le> ?g n"
wenzelm@53079
   267
  proof
wenzelm@53079
   268
    fix n
wenzelm@53079
   269
    show "?g (Suc n) \<le> ?g n" using mono[of "Suc (2*n)"]
wenzelm@53079
   270
      unfolding One_nat_def by auto
wenzelm@53079
   271
  qed
wenzelm@53079
   272
  show "\<forall>n. ?f n \<le> ?g n"
wenzelm@53079
   273
  proof
wenzelm@53079
   274
    fix n
wenzelm@53079
   275
    show "?f n \<le> ?g n" using fg_diff a_pos
wenzelm@53079
   276
      unfolding One_nat_def by auto
hoelzl@29803
   277
  qed
wenzelm@53079
   278
  show "(\<lambda>n. ?f n - ?g n) ----> 0" unfolding fg_diff
wenzelm@53079
   279
  proof (rule LIMSEQ_I)
wenzelm@53079
   280
    fix r :: real
wenzelm@53079
   281
    assume "0 < r"
wenzelm@53079
   282
    with `a ----> 0`[THEN LIMSEQ_D] obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r"
wenzelm@53079
   283
      by auto
wenzelm@53079
   284
    hence "\<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
wenzelm@53079
   285
    thus "\<exists>N. \<forall>n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
wenzelm@53079
   286
  qed
hoelzl@41970
   287
qed
hoelzl@29803
   288
wenzelm@53079
   289
lemma summable_Leibniz':
wenzelm@53079
   290
  fixes a :: "nat \<Rightarrow> real"
wenzelm@53079
   291
  assumes a_zero: "a ----> 0"
wenzelm@53079
   292
    and a_pos: "\<And> n. 0 \<le> a n"
wenzelm@53079
   293
    and a_monotone: "\<And> n. a (Suc n) \<le> a n"
hoelzl@29803
   294
  shows summable: "summable (\<lambda> n. (-1)^n * a n)"
hoelzl@56193
   295
    and "\<And>n. (\<Sum>i<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
hoelzl@56193
   296
    and "(\<lambda>n. \<Sum>i<2*n. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
hoelzl@56193
   297
    and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i<2*n+1. (-1)^i*a i)"
hoelzl@56193
   298
    and "(\<lambda>n. \<Sum>i<2*n+1. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
hoelzl@29803
   299
proof -
wenzelm@53079
   300
  let ?S = "\<lambda>n. (-1)^n * a n"
hoelzl@56193
   301
  let ?P = "\<lambda>n. \<Sum>i<n. ?S i"
wenzelm@53079
   302
  let ?f = "\<lambda>n. ?P (2 * n)"
wenzelm@53079
   303
  let ?g = "\<lambda>n. ?P (2 * n + 1)"
wenzelm@53079
   304
  obtain l :: real
wenzelm@53079
   305
    where below_l: "\<forall> n. ?f n \<le> l"
wenzelm@53079
   306
      and "?f ----> l"
wenzelm@53079
   307
      and above_l: "\<forall> n. l \<le> ?g n"
wenzelm@53079
   308
      and "?g ----> l"
hoelzl@29803
   309
    using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
hoelzl@41970
   310
hoelzl@56193
   311
  let ?Sa = "\<lambda>m. \<Sum>n<m. ?S n"
hoelzl@29803
   312
  have "?Sa ----> l"
hoelzl@29803
   313
  proof (rule LIMSEQ_I)
wenzelm@53079
   314
    fix r :: real
wenzelm@53079
   315
    assume "0 < r"
hoelzl@41970
   316
    with `?f ----> l`[THEN LIMSEQ_D]
hoelzl@29803
   317
    obtain f_no where f: "\<And> n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r" by auto
hoelzl@29803
   318
hoelzl@41970
   319
    from `0 < r` `?g ----> l`[THEN LIMSEQ_D]
hoelzl@29803
   320
    obtain g_no where g: "\<And> n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r" by auto
hoelzl@29803
   321
wenzelm@53079
   322
    {
wenzelm@53079
   323
      fix n :: nat
wenzelm@53079
   324
      assume "n \<ge> (max (2 * f_no) (2 * g_no))"
wenzelm@53079
   325
      hence "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
hoelzl@29803
   326
      have "norm (?Sa n - l) < r"
hoelzl@29803
   327
      proof (cases "even n")
wenzelm@53079
   328
        case True
haftmann@58710
   329
        then have n_eq: "2 * (n div 2) = n" by (simp add: even_two_times_div_two)
wenzelm@53079
   330
        with `n \<ge> 2 * f_no` have "n div 2 \<ge> f_no"
wenzelm@53079
   331
          by auto
wenzelm@53079
   332
        from f[OF this] show ?thesis
wenzelm@53079
   333
          unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
hoelzl@29803
   334
      next
wenzelm@53079
   335
        case False
wenzelm@53079
   336
        hence "even (n - 1)" by simp
haftmann@58710
   337
        then have n_eq: "2 * ((n - 1) div 2) = n - 1"
haftmann@58710
   338
          by (simp add: even_two_times_div_two)
wenzelm@53079
   339
        hence range_eq: "n - 1 + 1 = n"
wenzelm@53079
   340
          using odd_pos[OF False] by auto
wenzelm@53079
   341
wenzelm@53079
   342
        from n_eq `n \<ge> 2 * g_no` have "(n - 1) div 2 \<ge> g_no"
wenzelm@53079
   343
          by auto
wenzelm@53079
   344
        from g[OF this] show ?thesis
hoelzl@56193
   345
          unfolding n_eq range_eq .
hoelzl@29803
   346
      qed
hoelzl@29803
   347
    }
wenzelm@53079
   348
    thus "\<exists>no. \<forall>n \<ge> no. norm (?Sa n - l) < r" by blast
hoelzl@29803
   349
  qed
wenzelm@53079
   350
  hence sums_l: "(\<lambda>i. (-1)^i * a i) sums l"
hoelzl@56193
   351
    unfolding sums_def .
hoelzl@29803
   352
  thus "summable ?S" using summable_def by auto
hoelzl@29803
   353
hoelzl@29803
   354
  have "l = suminf ?S" using sums_unique[OF sums_l] .
hoelzl@29803
   355
wenzelm@53079
   356
  fix n
wenzelm@53079
   357
  show "suminf ?S \<le> ?g n"
wenzelm@53079
   358
    unfolding sums_unique[OF sums_l, symmetric] using above_l by auto
wenzelm@53079
   359
  show "?f n \<le> suminf ?S"
wenzelm@53079
   360
    unfolding sums_unique[OF sums_l, symmetric] using below_l by auto
wenzelm@53079
   361
  show "?g ----> suminf ?S"
wenzelm@53079
   362
    using `?g ----> l` `l = suminf ?S` by auto
wenzelm@53079
   363
  show "?f ----> suminf ?S"
wenzelm@53079
   364
    using `?f ----> l` `l = suminf ?S` by auto
hoelzl@29803
   365
qed
hoelzl@29803
   366
wenzelm@53079
   367
theorem summable_Leibniz:
wenzelm@53079
   368
  fixes a :: "nat \<Rightarrow> real"
hoelzl@29803
   369
  assumes a_zero: "a ----> 0" and "monoseq a"
hoelzl@29803
   370
  shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
wenzelm@53079
   371
    and "0 < a 0 \<longrightarrow>
haftmann@58410
   372
      (\<forall>n. (\<Sum>i. (- 1)^i*a i) \<in> { \<Sum>i<2*n. (- 1)^i * a i .. \<Sum>i<2*n+1. (- 1)^i * a i})" (is "?pos")
wenzelm@53079
   373
    and "a 0 < 0 \<longrightarrow>
haftmann@58410
   374
      (\<forall>n. (\<Sum>i. (- 1)^i*a i) \<in> { \<Sum>i<2*n+1. (- 1)^i * a i .. \<Sum>i<2*n. (- 1)^i * a i})" (is "?neg")
haftmann@58410
   375
    and "(\<lambda>n. \<Sum>i<2*n. (- 1)^i*a i) ----> (\<Sum>i. (- 1)^i*a i)" (is "?f")
haftmann@58410
   376
    and "(\<lambda>n. \<Sum>i<2*n+1. (- 1)^i*a i) ----> (\<Sum>i. (- 1)^i*a i)" (is "?g")
hoelzl@29803
   377
proof -
hoelzl@29803
   378
  have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
hoelzl@29803
   379
  proof (cases "(\<forall> n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
hoelzl@29803
   380
    case True
wenzelm@53079
   381
    hence ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m" and ge0: "\<And> n. 0 \<le> a n"
wenzelm@53079
   382
      by auto
wenzelm@53079
   383
    {
wenzelm@53079
   384
      fix n
wenzelm@53079
   385
      have "a (Suc n) \<le> a n"
wenzelm@53079
   386
        using ord[where n="Suc n" and m=n] by auto
wenzelm@53079
   387
    } note mono = this
wenzelm@53079
   388
    note leibniz = summable_Leibniz'[OF `a ----> 0` ge0]
hoelzl@29803
   389
    from leibniz[OF mono]
hoelzl@29803
   390
    show ?thesis using `0 \<le> a 0` by auto
hoelzl@29803
   391
  next
hoelzl@29803
   392
    let ?a = "\<lambda> n. - a n"
hoelzl@29803
   393
    case False
hoelzl@29803
   394
    with monoseq_le[OF `monoseq a` `a ----> 0`]
hoelzl@29803
   395
    have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
wenzelm@53079
   396
    hence ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n"
wenzelm@53079
   397
      by auto
wenzelm@53079
   398
    {
wenzelm@53079
   399
      fix n
wenzelm@53079
   400
      have "?a (Suc n) \<le> ?a n" using ord[where n="Suc n" and m=n]
wenzelm@53079
   401
        by auto
wenzelm@53079
   402
    } note monotone = this
wenzelm@53079
   403
    note leibniz =
wenzelm@53079
   404
      summable_Leibniz'[OF _ ge0, of "\<lambda>x. x",
wenzelm@53079
   405
        OF tendsto_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
wenzelm@53079
   406
    have "summable (\<lambda> n. (-1)^n * ?a n)"
wenzelm@53079
   407
      using leibniz(1) by auto
wenzelm@53079
   408
    then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l"
wenzelm@53079
   409
      unfolding summable_def by auto
wenzelm@53079
   410
    from this[THEN sums_minus] have "(\<lambda> n. (-1)^n * a n) sums -l"
wenzelm@53079
   411
      by auto
hoelzl@29803
   412
    hence ?summable unfolding summable_def by auto
hoelzl@29803
   413
    moreover
wenzelm@53079
   414
    have "\<And>a b :: real. \<bar>- a - - b\<bar> = \<bar>a - b\<bar>"
wenzelm@53079
   415
      unfolding minus_diff_minus by auto
hoelzl@41970
   416
hoelzl@29803
   417
    from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
haftmann@58410
   418
    have move_minus: "(\<Sum>n. - ((- 1) ^ n * a n)) = - (\<Sum>n. (- 1) ^ n * a n)"
wenzelm@53079
   419
      by auto
hoelzl@29803
   420
hoelzl@29803
   421
    have ?pos using `0 \<le> ?a 0` by auto
wenzelm@53079
   422
    moreover have ?neg
wenzelm@53079
   423
      using leibniz(2,4)
wenzelm@53079
   424
      unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le
wenzelm@53079
   425
      by auto
wenzelm@53079
   426
    moreover have ?f and ?g
wenzelm@53079
   427
      using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN tendsto_minus_cancel]
wenzelm@53079
   428
      by auto
hoelzl@29803
   429
    ultimately show ?thesis by auto
hoelzl@29803
   430
  qed
lp15@59669
   431
  then show ?summable and ?pos and ?neg and ?f and ?g
paulson@54573
   432
    by safe
hoelzl@29803
   433
qed
paulson@15077
   434
huffman@29164
   435
subsection {* Term-by-Term Differentiability of Power Series *}
huffman@23043
   436
hoelzl@56193
   437
definition diffs :: "(nat \<Rightarrow> 'a::ring_1) \<Rightarrow> nat \<Rightarrow> 'a"
hoelzl@56193
   438
  where "diffs c = (\<lambda>n. of_nat (Suc n) * c (Suc n))"
paulson@15077
   439
paulson@15077
   440
text{*Lemma about distributing negation over it*}
wenzelm@53079
   441
lemma diffs_minus: "diffs (\<lambda>n. - c n) = (\<lambda>n. - diffs c n)"
wenzelm@53079
   442
  by (simp add: diffs_def)
paulson@15077
   443
huffman@29163
   444
lemma sums_Suc_imp:
hoelzl@56193
   445
  "(f::nat \<Rightarrow> 'a::real_normed_vector) 0 = 0 \<Longrightarrow> (\<lambda>n. f (Suc n)) sums s \<Longrightarrow> (\<lambda>n. f n) sums s"
hoelzl@56193
   446
  using sums_Suc_iff[of f] by simp
paulson@15077
   447
paulson@15229
   448
lemma diffs_equiv:
hoelzl@41970
   449
  fixes x :: "'a::{real_normed_vector, ring_1}"
hoelzl@56193
   450
  shows "summable (\<lambda>n. diffs c n * x^n) \<Longrightarrow>
hoelzl@56193
   451
      (\<lambda>n. of_nat n * c n * x^(n - Suc 0)) sums (\<Sum>n. diffs c n * x^n)"
wenzelm@53079
   452
  unfolding diffs_def
paulson@54573
   453
  by (simp add: summable_sums sums_Suc_imp)
paulson@15077
   454
paulson@15077
   455
lemma lemma_termdiff1:
haftmann@31017
   456
  fixes z :: "'a :: {monoid_mult,comm_ring}" shows
hoelzl@56193
   457
  "(\<Sum>p<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =
hoelzl@56193
   458
   (\<Sum>p<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
wenzelm@53079
   459
  by (auto simp add: algebra_simps power_add [symmetric])
paulson@15077
   460
huffman@23082
   461
lemma sumr_diff_mult_const2:
hoelzl@56193
   462
  "setsum f {..<n} - of_nat n * (r::'a::ring_1) = (\<Sum>i<n. f i - r)"
wenzelm@53079
   463
  by (simp add: setsum_subtractf)
huffman@23082
   464
paulson@15229
   465
lemma lemma_termdiff2:
haftmann@31017
   466
  fixes h :: "'a :: {field}"
wenzelm@53079
   467
  assumes h: "h \<noteq> 0"
wenzelm@53079
   468
  shows
wenzelm@53079
   469
    "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
hoelzl@56193
   470
     h * (\<Sum>p< n - Suc 0. \<Sum>q< n - Suc 0 - p.
wenzelm@53079
   471
          (z + h) ^ q * z ^ (n - 2 - q))" (is "?lhs = ?rhs")
wenzelm@53079
   472
  apply (subgoal_tac "h * ?lhs = h * ?rhs", simp add: h)
wenzelm@53079
   473
  apply (simp add: right_diff_distrib diff_divide_distrib h)
haftmann@57512
   474
  apply (simp add: mult.assoc [symmetric])
wenzelm@53079
   475
  apply (cases "n", simp)
wenzelm@53079
   476
  apply (simp add: lemma_realpow_diff_sumr2 h
haftmann@57512
   477
                   right_diff_distrib [symmetric] mult.assoc
hoelzl@56193
   478
              del: power_Suc setsum_lessThan_Suc of_nat_Suc)
wenzelm@53079
   479
  apply (subst lemma_realpow_rev_sumr)
wenzelm@53079
   480
  apply (subst sumr_diff_mult_const2)
wenzelm@53079
   481
  apply simp
wenzelm@53079
   482
  apply (simp only: lemma_termdiff1 setsum_right_distrib)
haftmann@57418
   483
  apply (rule setsum.cong [OF refl])
haftmann@54230
   484
  apply (simp add: less_iff_Suc_add)
wenzelm@53079
   485
  apply (clarify)
haftmann@57514
   486
  apply (simp add: setsum_right_distrib lemma_realpow_diff_sumr2 ac_simps
hoelzl@56193
   487
              del: setsum_lessThan_Suc power_Suc)
haftmann@57512
   488
  apply (subst mult.assoc [symmetric], subst power_add [symmetric])
haftmann@57514
   489
  apply (simp add: ac_simps)
wenzelm@53079
   490
  done
huffman@20860
   491
huffman@20860
   492
lemma real_setsum_nat_ivl_bounded2:
haftmann@35028
   493
  fixes K :: "'a::linordered_semidom"
huffman@23082
   494
  assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K"
wenzelm@53079
   495
    and K: "0 \<le> K"
hoelzl@56193
   496
  shows "setsum f {..<n-k} \<le> of_nat n * K"
wenzelm@53079
   497
  apply (rule order_trans [OF setsum_mono])
wenzelm@53079
   498
  apply (rule f, simp)
wenzelm@53079
   499
  apply (simp add: mult_right_mono K)
wenzelm@53079
   500
  done
paulson@15077
   501
paulson@15229
   502
lemma lemma_termdiff3:
haftmann@31017
   503
  fixes h z :: "'a::{real_normed_field}"
huffman@20860
   504
  assumes 1: "h \<noteq> 0"
wenzelm@53079
   505
    and 2: "norm z \<le> K"
wenzelm@53079
   506
    and 3: "norm (z + h) \<le> K"
huffman@23082
   507
  shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0))
huffman@23082
   508
          \<le> of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
huffman@20860
   509
proof -
huffman@23082
   510
  have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
hoelzl@56193
   511
        norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p.
huffman@23082
   512
          (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
haftmann@57512
   513
    by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
huffman@23082
   514
  also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
huffman@23082
   515
  proof (rule mult_right_mono [OF _ norm_ge_zero])
wenzelm@53079
   516
    from norm_ge_zero 2 have K: "0 \<le> K"
wenzelm@53079
   517
      by (rule order_trans)
huffman@23082
   518
    have le_Kn: "\<And>i j n. i + j = n \<Longrightarrow> norm ((z + h) ^ i * z ^ j) \<le> K ^ n"
huffman@20860
   519
      apply (erule subst)
huffman@23082
   520
      apply (simp only: norm_mult norm_power power_add)
huffman@23082
   521
      apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
huffman@20860
   522
      done
hoelzl@56193
   523
    show "norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))
huffman@23082
   524
          \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
huffman@20860
   525
      apply (intro
huffman@23082
   526
         order_trans [OF norm_setsum]
huffman@20860
   527
         real_setsum_nat_ivl_bounded2
huffman@20860
   528
         mult_nonneg_nonneg
huffman@47489
   529
         of_nat_0_le_iff
huffman@20860
   530
         zero_le_power K)
huffman@20860
   531
      apply (rule le_Kn, simp)
huffman@20860
   532
      done
huffman@20860
   533
  qed
huffman@23082
   534
  also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
haftmann@57512
   535
    by (simp only: mult.assoc)
huffman@20860
   536
  finally show ?thesis .
huffman@20860
   537
qed
paulson@15077
   538
huffman@20860
   539
lemma lemma_termdiff4:
huffman@56167
   540
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
huffman@20860
   541
  assumes k: "0 < (k::real)"
wenzelm@53079
   542
    and le: "\<And>h. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (f h) \<le> K * norm h"
huffman@20860
   543
  shows "f -- 0 --> 0"
huffman@56167
   544
proof (rule tendsto_norm_zero_cancel)
huffman@56167
   545
  show "(\<lambda>h. norm (f h)) -- 0 --> 0"
huffman@56167
   546
  proof (rule real_tendsto_sandwich)
huffman@56167
   547
    show "eventually (\<lambda>h. 0 \<le> norm (f h)) (at 0)"
huffman@20860
   548
      by simp
huffman@56167
   549
    show "eventually (\<lambda>h. norm (f h) \<le> K * norm h) (at 0)"
huffman@56167
   550
      using k by (auto simp add: eventually_at dist_norm le)
huffman@56167
   551
    show "(\<lambda>h. 0) -- (0::'a) --> (0::real)"
huffman@56167
   552
      by (rule tendsto_const)
huffman@56167
   553
    have "(\<lambda>h. K * norm h) -- (0::'a) --> K * norm (0::'a)"
huffman@56167
   554
      by (intro tendsto_intros)
huffman@56167
   555
    then show "(\<lambda>h. K * norm h) -- (0::'a) --> 0"
huffman@56167
   556
      by simp
huffman@20860
   557
  qed
huffman@20860
   558
qed
paulson@15077
   559
paulson@15229
   560
lemma lemma_termdiff5:
huffman@56167
   561
  fixes g :: "'a::real_normed_vector \<Rightarrow> nat \<Rightarrow> 'b::banach"
huffman@20860
   562
  assumes k: "0 < (k::real)"
huffman@20860
   563
  assumes f: "summable f"
huffman@23082
   564
  assumes le: "\<And>h n. \<lbrakk>h \<noteq> 0; norm h < k\<rbrakk> \<Longrightarrow> norm (g h n) \<le> f n * norm h"
huffman@20860
   565
  shows "(\<lambda>h. suminf (g h)) -- 0 --> 0"
huffman@20860
   566
proof (rule lemma_termdiff4 [OF k])
wenzelm@53079
   567
  fix h::'a
wenzelm@53079
   568
  assume "h \<noteq> 0" and "norm h < k"
huffman@23082
   569
  hence A: "\<forall>n. norm (g h n) \<le> f n * norm h"
huffman@20860
   570
    by (simp add: le)
huffman@23082
   571
  hence "\<exists>N. \<forall>n\<ge>N. norm (norm (g h n)) \<le> f n * norm h"
huffman@20860
   572
    by simp
huffman@23082
   573
  moreover from f have B: "summable (\<lambda>n. f n * norm h)"
huffman@20860
   574
    by (rule summable_mult2)
huffman@23082
   575
  ultimately have C: "summable (\<lambda>n. norm (g h n))"
huffman@20860
   576
    by (rule summable_comparison_test)
huffman@23082
   577
  hence "norm (suminf (g h)) \<le> (\<Sum>n. norm (g h n))"
huffman@23082
   578
    by (rule summable_norm)
huffman@23082
   579
  also from A C B have "(\<Sum>n. norm (g h n)) \<le> (\<Sum>n. f n * norm h)"
hoelzl@56213
   580
    by (rule suminf_le)
huffman@23082
   581
  also from f have "(\<Sum>n. f n * norm h) = suminf f * norm h"
huffman@20860
   582
    by (rule suminf_mult2 [symmetric])
huffman@23082
   583
  finally show "norm (suminf (g h)) \<le> suminf f * norm h" .
huffman@20860
   584
qed
paulson@15077
   585
paulson@15077
   586
paulson@15077
   587
text{* FIXME: Long proofs*}
paulson@15077
   588
paulson@15077
   589
lemma termdiffs_aux:
haftmann@31017
   590
  fixes x :: "'a::{real_normed_field,banach}"
huffman@20849
   591
  assumes 1: "summable (\<lambda>n. diffs (diffs c) n * K ^ n)"
wenzelm@53079
   592
    and 2: "norm x < norm K"
lp15@59730
   593
  shows "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x^n) / h
huffman@23082
   594
             - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
huffman@20849
   595
proof -
huffman@20860
   596
  from dense [OF 2]
huffman@23082
   597
  obtain r where r1: "norm x < r" and r2: "r < norm K" by fast
huffman@23082
   598
  from norm_ge_zero r1 have r: "0 < r"
huffman@20860
   599
    by (rule order_le_less_trans)
huffman@20860
   600
  hence r_neq_0: "r \<noteq> 0" by simp
huffman@20860
   601
  show ?thesis
huffman@20849
   602
  proof (rule lemma_termdiff5)
huffman@23082
   603
    show "0 < r - norm x" using r1 by simp
huffman@23082
   604
    from r r2 have "norm (of_real r::'a) < norm K"
huffman@23082
   605
      by simp
huffman@23082
   606
    with 1 have "summable (\<lambda>n. norm (diffs (diffs c) n * (of_real r ^ n)))"
huffman@20860
   607
      by (rule powser_insidea)
huffman@23082
   608
    hence "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
huffman@23082
   609
      using r
huffman@23082
   610
      by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
huffman@23082
   611
    hence "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
huffman@20860
   612
      by (rule diffs_equiv [THEN sums_summable])
wenzelm@53079
   613
    also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
wenzelm@53079
   614
      (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
huffman@20849
   615
      apply (rule ext)
huffman@20849
   616
      apply (simp add: diffs_def)
huffman@20849
   617
      apply (case_tac n, simp_all add: r_neq_0)
huffman@20849
   618
      done
hoelzl@41970
   619
    finally have "summable
huffman@23082
   620
      (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
huffman@20860
   621
      by (rule diffs_equiv [THEN sums_summable])
huffman@20860
   622
    also have
huffman@23082
   623
      "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) *
huffman@20860
   624
           r ^ (n - Suc 0)) =
huffman@23082
   625
       (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
huffman@20849
   626
      apply (rule ext)
huffman@20849
   627
      apply (case_tac "n", simp)
blanchet@55417
   628
      apply (rename_tac nat)
huffman@20849
   629
      apply (case_tac "nat", simp)
huffman@20849
   630
      apply (simp add: r_neq_0)
huffman@20849
   631
      done
wenzelm@53079
   632
    finally
wenzelm@53079
   633
    show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
huffman@20849
   634
  next
huffman@23082
   635
    fix h::'a and n::nat
huffman@20860
   636
    assume h: "h \<noteq> 0"
huffman@23082
   637
    assume "norm h < r - norm x"
huffman@23082
   638
    hence "norm x + norm h < r" by simp
huffman@23082
   639
    with norm_triangle_ineq have xh: "norm (x + h) < r"
huffman@20860
   640
      by (rule order_le_less_trans)
lp15@59730
   641
    show "norm (c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0)))
huffman@23082
   642
          \<le> norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
haftmann@57512
   643
      apply (simp only: norm_mult mult.assoc)
huffman@23082
   644
      apply (rule mult_left_mono [OF _ norm_ge_zero])
haftmann@57512
   645
      apply (simp add: mult.assoc [symmetric])
paulson@54575
   646
      apply (metis h lemma_termdiff3 less_eq_real_def r1 xh)
huffman@20860
   647
      done
huffman@20849
   648
  qed
huffman@20849
   649
qed
webertj@20217
   650
huffman@20860
   651
lemma termdiffs:
haftmann@31017
   652
  fixes K x :: "'a::{real_normed_field,banach}"
huffman@20860
   653
  assumes 1: "summable (\<lambda>n. c n * K ^ n)"
paulson@54575
   654
      and 2: "summable (\<lambda>n. (diffs c) n * K ^ n)"
paulson@54575
   655
      and 3: "summable (\<lambda>n. (diffs (diffs c)) n * K ^ n)"
paulson@54575
   656
      and 4: "norm x < norm K"
lp15@59730
   657
  shows "DERIV (\<lambda>x. \<Sum>n. c n * x^n) x :> (\<Sum>n. (diffs c) n * x^n)"
hoelzl@56381
   658
  unfolding DERIV_def
huffman@29163
   659
proof (rule LIM_zero_cancel)
lp15@59730
   660
  show "(\<lambda>h. (suminf (\<lambda>n. c n * (x + h) ^ n) - suminf (\<lambda>n. c n * x^n)) / h
lp15@59730
   661
            - suminf (\<lambda>n. diffs c n * x^n)) -- 0 --> 0"
huffman@20860
   662
  proof (rule LIM_equal2)
huffman@29163
   663
    show "0 < norm K - norm x" using 4 by (simp add: less_diff_eq)
huffman@20860
   664
  next
huffman@23082
   665
    fix h :: 'a
huffman@23082
   666
    assume "norm (h - 0) < norm K - norm x"
huffman@23082
   667
    hence "norm x + norm h < norm K" by simp
huffman@23082
   668
    hence 5: "norm (x + h) < norm K"
huffman@23082
   669
      by (rule norm_triangle_ineq [THEN order_le_less_trans])
lp15@59730
   670
    have "summable (\<lambda>n. c n * x^n)"
huffman@56167
   671
      and "summable (\<lambda>n. c n * (x + h) ^ n)"
lp15@59730
   672
      and "summable (\<lambda>n. diffs c n * x^n)"
huffman@56167
   673
      using 1 2 4 5 by (auto elim: powser_inside)
lp15@59730
   674
    then have "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x^n)) / h - (\<Sum>n. diffs c n * x^n) =
lp15@59730
   675
          (\<Sum>n. (c n * (x + h) ^ n - c n * x^n) / h - of_nat n * c n * x ^ (n - Suc 0))"
huffman@56167
   676
      by (intro sums_unique sums_diff sums_divide diffs_equiv summable_sums)
lp15@59730
   677
    then show "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x^n)) / h - (\<Sum>n. diffs c n * x^n) =
lp15@59730
   678
          (\<Sum>n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0)))"
paulson@54575
   679
      by (simp add: algebra_simps)
huffman@20860
   680
  next
lp15@59730
   681
    show "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
wenzelm@53079
   682
      by (rule termdiffs_aux [OF 3 4])
huffman@20860
   683
  qed
huffman@20860
   684
qed
huffman@20860
   685
paulson@15077
   686
hoelzl@29803
   687
subsection {* Derivability of power series *}
hoelzl@29803
   688
wenzelm@53079
   689
lemma DERIV_series':
wenzelm@53079
   690
  fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
hoelzl@29803
   691
  assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
wenzelm@53079
   692
    and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)" and x0_in_I: "x0 \<in> {a <..< b}"
wenzelm@53079
   693
    and "summable (f' x0)"
wenzelm@53079
   694
    and "summable L"
wenzelm@53079
   695
    and L_def: "\<And>n x y. \<lbrakk> x \<in> { a <..< b} ; y \<in> { a <..< b} \<rbrakk> \<Longrightarrow> \<bar>f x n - f y n\<bar> \<le> L n * \<bar>x - y\<bar>"
hoelzl@29803
   696
  shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
hoelzl@56381
   697
  unfolding DERIV_def
hoelzl@29803
   698
proof (rule LIM_I)
wenzelm@53079
   699
  fix r :: real
wenzelm@53079
   700
  assume "0 < r" hence "0 < r/3" by auto
hoelzl@29803
   701
hoelzl@41970
   702
  obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3"
hoelzl@29803
   703
    using suminf_exist_split[OF `0 < r/3` `summable L`] by auto
hoelzl@29803
   704
hoelzl@41970
   705
  obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3"
hoelzl@29803
   706
    using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto
hoelzl@29803
   707
hoelzl@29803
   708
  let ?N = "Suc (max N_L N_f')"
hoelzl@29803
   709
  have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3") and
hoelzl@29803
   710
    L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto
hoelzl@29803
   711
wenzelm@53079
   712
  let ?diff = "\<lambda>i x. (f (x0 + x) i - f x0 i) / x"
hoelzl@29803
   713
hoelzl@29803
   714
  let ?r = "r / (3 * real ?N)"
nipkow@56541
   715
  from `0 < r` have "0 < ?r" by simp
hoelzl@29803
   716
hoelzl@56193
   717
  let ?s = "\<lambda>n. SOME s. 0 < s \<and> (\<forall> x. x \<noteq> 0 \<and> \<bar> x \<bar> < s \<longrightarrow> \<bar> ?diff n x - f' x0 n \<bar> < ?r)"
hoelzl@56193
   718
  def S' \<equiv> "Min (?s ` {..< ?N })"
hoelzl@29803
   719
hoelzl@29803
   720
  have "0 < S'" unfolding S'_def
hoelzl@29803
   721
  proof (rule iffD2[OF Min_gr_iff])
hoelzl@56193
   722
    show "\<forall>x \<in> (?s ` {..< ?N }). 0 < x"
wenzelm@53079
   723
    proof
wenzelm@53079
   724
      fix x
hoelzl@56193
   725
      assume "x \<in> ?s ` {..<?N}"
hoelzl@56193
   726
      then obtain n where "x = ?s n" and "n \<in> {..<?N}"
wenzelm@53079
   727
        using image_iff[THEN iffD1] by blast
hoelzl@41970
   728
      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def]
wenzelm@53079
   729
      obtain s where s_bound: "0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < s \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r)"
wenzelm@53079
   730
        by auto
wenzelm@53079
   731
      have "0 < ?s n" by (rule someI2[where a=s]) (auto simp add: s_bound)
hoelzl@29803
   732
      thus "0 < x" unfolding `x = ?s n` .
hoelzl@29803
   733
    qed
hoelzl@29803
   734
  qed auto
hoelzl@29803
   735
hoelzl@29803
   736
  def S \<equiv> "min (min (x0 - a) (b - x0)) S'"
wenzelm@53079
   737
  hence "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0"
wenzelm@53079
   738
    and "S \<le> S'" using x0_in_I and `0 < S'`
hoelzl@29803
   739
    by auto
hoelzl@29803
   740
wenzelm@53079
   741
  {
wenzelm@53079
   742
    fix x
wenzelm@53079
   743
    assume "x \<noteq> 0" and "\<bar> x \<bar> < S"
wenzelm@53079
   744
    hence x_in_I: "x0 + x \<in> { a <..< b }"
wenzelm@53079
   745
      using S_a S_b by auto
hoelzl@41970
   746
hoelzl@29803
   747
    note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
hoelzl@29803
   748
    note div_smbl = summable_divide[OF diff_smbl]
hoelzl@29803
   749
    note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
hoelzl@29803
   750
    note ign = summable_ignore_initial_segment[where k="?N"]
hoelzl@29803
   751
    note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
hoelzl@29803
   752
    note div_shft_smbl = summable_divide[OF diff_shft_smbl]
hoelzl@29803
   753
    note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]
hoelzl@29803
   754
hoelzl@56193
   755
    { fix n
hoelzl@41970
   756
      have "\<bar> ?diff (n + ?N) x \<bar> \<le> L (n + ?N) * \<bar> (x0 + x) - x0 \<bar> / \<bar> x \<bar>"
wenzelm@53079
   757
        using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero]
wenzelm@53079
   758
        unfolding abs_divide .
wenzelm@53079
   759
      hence "\<bar> (\<bar>?diff (n + ?N) x \<bar>) \<bar> \<le> L (n + ?N)"
hoelzl@56193
   760
        using `x \<noteq> 0` by auto }
hoelzl@56193
   761
    note 1 = this and 2 = summable_rabs_comparison_test[OF _ ign[OF `summable L`]]
hoelzl@56193
   762
    then have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))"
hoelzl@56213
   763
      by (metis (lifting) abs_idempotent order_trans[OF summable_rabs[OF 2] suminf_le[OF _ 2 ign[OF `summable L`]]])
hoelzl@56193
   764
    then have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> r / 3" (is "?L_part \<le> r/3")
wenzelm@53079
   765
      using L_estimate by auto
wenzelm@53079
   766
hoelzl@56193
   767
    have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n \<bar> \<le> (\<Sum>n<?N. \<bar>?diff n x - f' x0 n \<bar>)" ..
hoelzl@56193
   768
    also have "\<dots> < (\<Sum>n<?N. ?r)"
hoelzl@29803
   769
    proof (rule setsum_strict_mono)
wenzelm@53079
   770
      fix n
hoelzl@56193
   771
      assume "n \<in> {..< ?N}"
wenzelm@53079
   772
      have "\<bar>x\<bar> < S" using `\<bar>x\<bar> < S` .
hoelzl@29803
   773
      also have "S \<le> S'" using `S \<le> S'` .
hoelzl@41970
   774
      also have "S' \<le> ?s n" unfolding S'_def
hoelzl@29803
   775
      proof (rule Min_le_iff[THEN iffD2])
hoelzl@56193
   776
        have "?s n \<in> (?s ` {..<?N}) \<and> ?s n \<le> ?s n"
hoelzl@56193
   777
          using `n \<in> {..< ?N}` by auto
hoelzl@56193
   778
        thus "\<exists> a \<in> (?s ` {..<?N}). a \<le> ?s n" by blast
hoelzl@29803
   779
      qed auto
wenzelm@53079
   780
      finally have "\<bar>x\<bar> < ?s n" .
hoelzl@29803
   781
hoelzl@29803
   782
      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
hoelzl@29803
   783
      have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
wenzelm@53079
   784
      with `x \<noteq> 0` and `\<bar>x\<bar> < ?s n` show "\<bar>?diff n x - f' x0 n\<bar> < ?r"
wenzelm@53079
   785
        by blast
hoelzl@29803
   786
    qed auto
hoelzl@56193
   787
    also have "\<dots> = of_nat (card {..<?N}) * ?r"
wenzelm@53079
   788
      by (rule setsum_constant)
wenzelm@53079
   789
    also have "\<dots> = real ?N * ?r"
wenzelm@53079
   790
      unfolding real_eq_of_nat by auto
hoelzl@29803
   791
    also have "\<dots> = r/3" by auto
hoelzl@56193
   792
    finally have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
hoelzl@29803
   793
hoelzl@29803
   794
    from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
wenzelm@53079
   795
    have "\<bar>(suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0)\<bar> =
wenzelm@53079
   796
        \<bar>\<Sum>n. ?diff n x - f' x0 n\<bar>"
wenzelm@53079
   797
      unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric]
wenzelm@53079
   798
      using suminf_divide[OF diff_smbl, symmetric] by auto
wenzelm@53079
   799
    also have "\<dots> \<le> ?diff_part + \<bar> (\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N)) \<bar>"
wenzelm@53079
   800
      unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"]
wenzelm@53079
   801
      unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]]
haftmann@57512
   802
      apply (subst (5) add.commute)
wenzelm@53079
   803
      by (rule abs_triangle_ineq)
wenzelm@53079
   804
    also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part"
wenzelm@53079
   805
      using abs_triangle_ineq4 by auto
hoelzl@41970
   806
    also have "\<dots> < r /3 + r/3 + r/3"
huffman@36842
   807
      using `?diff_part < r/3` `?L_part \<le> r/3` and `?f'_part < r/3`
huffman@36842
   808
      by (rule add_strict_mono [OF add_less_le_mono])
wenzelm@53079
   809
    finally have "\<bar>(suminf (f (x0 + x)) - suminf (f x0)) / x - suminf (f' x0)\<bar> < r"
hoelzl@29803
   810
      by auto
wenzelm@53079
   811
  }
wenzelm@53079
   812
  thus "\<exists> s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow>
wenzelm@53079
   813
      norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r"
wenzelm@53079
   814
    using `0 < S` unfolding real_norm_def diff_0_right by blast
hoelzl@29803
   815
qed
hoelzl@29803
   816
wenzelm@53079
   817
lemma DERIV_power_series':
wenzelm@53079
   818
  fixes f :: "nat \<Rightarrow> real"
hoelzl@29803
   819
  assumes converges: "\<And> x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda> n. f n * real (Suc n) * x^n)"
wenzelm@53079
   820
    and x0_in_I: "x0 \<in> {-R <..< R}" and "0 < R"
hoelzl@29803
   821
  shows "DERIV (\<lambda> x. (\<Sum> n. f n * x^(Suc n))) x0 :> (\<Sum> n. f n * real (Suc n) * x0^n)"
hoelzl@29803
   822
  (is "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
hoelzl@29803
   823
proof -
wenzelm@53079
   824
  {
wenzelm@53079
   825
    fix R'
wenzelm@53079
   826
    assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
wenzelm@53079
   827
    hence "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}"
wenzelm@53079
   828
      by auto
hoelzl@29803
   829
    have "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
hoelzl@29803
   830
    proof (rule DERIV_series')
hoelzl@29803
   831
      show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
hoelzl@29803
   832
      proof -
wenzelm@53079
   833
        have "(R' + R) / 2 < R" and "0 < (R' + R) / 2"
wenzelm@53079
   834
          using `0 < R'` `0 < R` `R' < R` by auto
wenzelm@53079
   835
        hence in_Rball: "(R' + R) / 2 \<in> {-R <..< R}"
wenzelm@53079
   836
          using `R' < R` by auto
wenzelm@53079
   837
        have "norm R' < norm ((R' + R) / 2)"
wenzelm@53079
   838
          using `0 < R'` `0 < R` `R' < R` by auto
wenzelm@53079
   839
        from powser_insidea[OF converges[OF in_Rball] this] show ?thesis
wenzelm@53079
   840
          by auto
hoelzl@29803
   841
      qed
wenzelm@53079
   842
      {
wenzelm@53079
   843
        fix n x y
wenzelm@53079
   844
        assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
wenzelm@32960
   845
        show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
wenzelm@32960
   846
        proof -
wenzelm@53079
   847
          have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> =
hoelzl@56193
   848
            (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar>"
wenzelm@53079
   849
            unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult
wenzelm@53079
   850
            by auto
hoelzl@41970
   851
          also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)"
wenzelm@32960
   852
          proof (rule mult_left_mono)
hoelzl@56193
   853
            have "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)"
wenzelm@53079
   854
              by (rule setsum_abs)
hoelzl@56193
   855
            also have "\<dots> \<le> (\<Sum>p<Suc n. R' ^ n)"
wenzelm@32960
   856
            proof (rule setsum_mono)
wenzelm@53079
   857
              fix p
hoelzl@56193
   858
              assume "p \<in> {..<Suc n}"
wenzelm@53079
   859
              hence "p \<le> n" by auto
wenzelm@53079
   860
              {
wenzelm@53079
   861
                fix n
wenzelm@53079
   862
                fix x :: real
wenzelm@53079
   863
                assume "x \<in> {-R'<..<R'}"
wenzelm@32960
   864
                hence "\<bar>x\<bar> \<le> R'"  by auto
wenzelm@53079
   865
                hence "\<bar>x^n\<bar> \<le> R'^n"
wenzelm@53079
   866
                  unfolding power_abs by (rule power_mono, auto)
wenzelm@53079
   867
              }
wenzelm@53079
   868
              from mult_mono[OF this[OF `x \<in> {-R'<..<R'}`, of p] this[OF `y \<in> {-R'<..<R'}`, of "n-p"]] `0 < R'`
wenzelm@53079
   869
              have "\<bar>x^p * y^(n-p)\<bar> \<le> R'^p * R'^(n-p)"
wenzelm@53079
   870
                unfolding abs_mult by auto
wenzelm@53079
   871
              thus "\<bar>x^p * y^(n-p)\<bar> \<le> R'^n"
wenzelm@53079
   872
                unfolding power_add[symmetric] using `p \<le> n` by auto
wenzelm@32960
   873
            qed
wenzelm@53079
   874
            also have "\<dots> = real (Suc n) * R' ^ n"
wenzelm@53079
   875
              unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
hoelzl@56193
   876
            finally show "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>"
wenzelm@53079
   877
              unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
wenzelm@53079
   878
            show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>"
wenzelm@53079
   879
              unfolding abs_mult[symmetric] by auto
wenzelm@32960
   880
          qed
wenzelm@53079
   881
          also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>"
haftmann@57512
   882
            unfolding abs_mult mult.assoc[symmetric] by algebra
wenzelm@32960
   883
          finally show ?thesis .
wenzelm@53079
   884
        qed
wenzelm@53079
   885
      }
wenzelm@53079
   886
      {
wenzelm@53079
   887
        fix n
wenzelm@53079
   888
        show "DERIV (\<lambda> x. ?f x n) x0 :> (?f' x0 n)"
hoelzl@56381
   889
          by (auto intro!: derivative_eq_intros simp del: power_Suc simp: real_of_nat_def)
wenzelm@53079
   890
      }
wenzelm@53079
   891
      {
wenzelm@53079
   892
        fix x
wenzelm@53079
   893
        assume "x \<in> {-R' <..< R'}"
wenzelm@53079
   894
        hence "R' \<in> {-R <..< R}" and "norm x < norm R'"
wenzelm@53079
   895
          using assms `R' < R` by auto
wenzelm@32960
   896
        have "summable (\<lambda> n. f n * x^n)"
hoelzl@56193
   897
        proof (rule summable_comparison_test, intro exI allI impI)
wenzelm@32960
   898
          fix n
wenzelm@53079
   899
          have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)"
wenzelm@53079
   900
            by (rule mult_left_mono) auto
lp15@59730
   901
          show "norm (f n * x^n) \<le> norm (f n * real (Suc n) * x^n)"
wenzelm@53079
   902
            unfolding real_norm_def abs_mult
wenzelm@53079
   903
            by (rule mult_right_mono) (auto simp add: le[unfolded mult_1_right])
hoelzl@56193
   904
        qed (rule powser_insidea[OF converges[OF `R' \<in> {-R <..< R}`] `norm x < norm R'`])
haftmann@57512
   905
        from this[THEN summable_mult2[where c=x], unfolded mult.assoc, unfolded mult.commute]
wenzelm@53079
   906
        show "summable (?f x)" by auto
wenzelm@53079
   907
      }
wenzelm@53079
   908
      show "summable (?f' x0)"
wenzelm@53079
   909
        using converges[OF `x0 \<in> {-R <..< R}`] .
wenzelm@53079
   910
      show "x0 \<in> {-R' <..< R'}"
wenzelm@53079
   911
        using `x0 \<in> {-R' <..< R'}` .
hoelzl@29803
   912
    qed
hoelzl@29803
   913
  } note for_subinterval = this
hoelzl@29803
   914
  let ?R = "(R + \<bar>x0\<bar>) / 2"
hoelzl@29803
   915
  have "\<bar>x0\<bar> < ?R" using assms by auto
hoelzl@29803
   916
  hence "- ?R < x0"
hoelzl@29803
   917
  proof (cases "x0 < 0")
hoelzl@29803
   918
    case True
hoelzl@29803
   919
    hence "- x0 < ?R" using `\<bar>x0\<bar> < ?R` by auto
hoelzl@29803
   920
    thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
hoelzl@29803
   921
  next
hoelzl@29803
   922
    case False
hoelzl@29803
   923
    have "- ?R < 0" using assms by auto
hoelzl@41970
   924
    also have "\<dots> \<le> x0" using False by auto
hoelzl@29803
   925
    finally show ?thesis .
hoelzl@29803
   926
  qed
wenzelm@53079
   927
  hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R"
wenzelm@53079
   928
    using assms by auto
hoelzl@29803
   929
  from for_subinterval[OF this]
hoelzl@29803
   930
  show ?thesis .
hoelzl@29803
   931
qed
chaieb@29695
   932
wenzelm@53079
   933
huffman@29164
   934
subsection {* Exponential Function *}
huffman@23043
   935
immler@58656
   936
definition exp :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
lp15@59730
   937
  where "exp = (\<lambda>x. \<Sum>n. x^n /\<^sub>R fact n)"
huffman@23043
   938
huffman@23115
   939
lemma summable_exp_generic:
haftmann@31017
   940
  fixes x :: "'a::{real_normed_algebra_1,banach}"
lp15@59730
   941
  defines S_def: "S \<equiv> \<lambda>n. x^n /\<^sub>R fact n"
huffman@23115
   942
  shows "summable S"
huffman@23115
   943
proof -
lp15@59730
   944
  have S_Suc: "\<And>n. S (Suc n) = (x * S n) /\<^sub>R (Suc n)"
huffman@30273
   945
    unfolding S_def by (simp del: mult_Suc)
huffman@23115
   946
  obtain r :: real where r0: "0 < r" and r1: "r < 1"
huffman@23115
   947
    using dense [OF zero_less_one] by fast
huffman@23115
   948
  obtain N :: nat where N: "norm x < real N * r"
huffman@23115
   949
    using reals_Archimedean3 [OF r0] by fast
huffman@23115
   950
  from r1 show ?thesis
hoelzl@56193
   951
  proof (rule summable_ratio_test [rule_format])
huffman@23115
   952
    fix n :: nat
huffman@23115
   953
    assume n: "N \<le> n"
huffman@23115
   954
    have "norm x \<le> real N * r"
huffman@23115
   955
      using N by (rule order_less_imp_le)
huffman@23115
   956
    also have "real N * r \<le> real (Suc n) * r"
huffman@23115
   957
      using r0 n by (simp add: mult_right_mono)
huffman@23115
   958
    finally have "norm x * norm (S n) \<le> real (Suc n) * r * norm (S n)"
huffman@23115
   959
      using norm_ge_zero by (rule mult_right_mono)
huffman@23115
   960
    hence "norm (x * S n) \<le> real (Suc n) * r * norm (S n)"
huffman@23115
   961
      by (rule order_trans [OF norm_mult_ineq])
huffman@23115
   962
    hence "norm (x * S n) / real (Suc n) \<le> r * norm (S n)"
haftmann@57514
   963
      by (simp add: pos_divide_le_eq ac_simps)
huffman@23115
   964
    thus "norm (S (Suc n)) \<le> r * norm (S n)"
huffman@35216
   965
      by (simp add: S_Suc inverse_eq_divide)
huffman@23115
   966
  qed
huffman@23115
   967
qed
huffman@23115
   968
huffman@23115
   969
lemma summable_norm_exp:
haftmann@31017
   970
  fixes x :: "'a::{real_normed_algebra_1,banach}"
lp15@59730
   971
  shows "summable (\<lambda>n. norm (x^n /\<^sub>R fact n))"
huffman@23115
   972
proof (rule summable_norm_comparison_test [OF exI, rule_format])
lp15@59730
   973
  show "summable (\<lambda>n. norm x^n /\<^sub>R fact n)"
huffman@23115
   974
    by (rule summable_exp_generic)
wenzelm@53079
   975
  fix n
lp15@59730
   976
  show "norm (x^n /\<^sub>R fact n) \<le> norm x^n /\<^sub>R fact n"
huffman@35216
   977
    by (simp add: norm_power_ineq)
huffman@23115
   978
qed
huffman@23115
   979
lp15@59730
   980
lemma summable_exp: 
lp15@59730
   981
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59730
   982
  shows "summable (\<lambda>n. inverse (fact n) * x^n)"
lp15@59730
   983
  using summable_exp_generic [where x=x]
lp15@59730
   984
  by (simp add: scaleR_conv_of_real nonzero_of_real_inverse)
lp15@59730
   985
lp15@59730
   986
lemma exp_converges: "(\<lambda>n. x^n /\<^sub>R fact n) sums exp x"
wenzelm@53079
   987
  unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])
huffman@23043
   988
hoelzl@41970
   989
lemma exp_fdiffs:
lp15@59730
   990
  fixes XXX :: "'a::{real_normed_field,banach}"
lp15@59730
   991
  shows "diffs (\<lambda>n. inverse (fact n)) = (\<lambda>n. inverse (fact n :: 'a))"
lp15@59730
   992
  by (simp add: diffs_def mult_ac nonzero_inverse_mult_distrib nonzero_of_real_inverse
lp15@59730
   993
           del: mult_Suc of_nat_Suc)
paulson@15077
   994
huffman@23115
   995
lemma diffs_of_real: "diffs (\<lambda>n. of_real (f n)) = (\<lambda>n. of_real (diffs f n))"
wenzelm@53079
   996
  by (simp add: diffs_def)
huffman@23115
   997
paulson@15077
   998
lemma DERIV_exp [simp]: "DERIV exp x :> exp(x)"
wenzelm@53079
   999
  unfolding exp_def scaleR_conv_of_real
wenzelm@53079
  1000
  apply (rule DERIV_cong)
wenzelm@53079
  1001
  apply (rule termdiffs [where K="of_real (1 + norm x)"])
wenzelm@53079
  1002
  apply (simp_all only: diffs_of_real scaleR_conv_of_real exp_fdiffs)
wenzelm@53079
  1003
  apply (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])+
wenzelm@53079
  1004
  apply (simp del: of_real_add)
wenzelm@53079
  1005
  done
paulson@15077
  1006
hoelzl@56381
  1007
declare DERIV_exp[THEN DERIV_chain2, derivative_intros]
hoelzl@51527
  1008
immler@58656
  1009
lemma norm_exp: "norm (exp x) \<le> exp (norm x)"
immler@58656
  1010
proof -
immler@58656
  1011
  from summable_norm[OF summable_norm_exp, of x]
lp15@59730
  1012
  have "norm (exp x) \<le> (\<Sum>n. inverse (fact n) * norm (x^n))"
immler@58656
  1013
    by (simp add: exp_def)
immler@58656
  1014
  also have "\<dots> \<le> exp (norm x)"
immler@58656
  1015
    using summable_exp_generic[of "norm x"] summable_norm_exp[of x]
immler@58656
  1016
    by (auto simp: exp_def intro!: suminf_le norm_power_ineq)
immler@58656
  1017
  finally show ?thesis .
immler@58656
  1018
qed
immler@58656
  1019
immler@58656
  1020
lemma isCont_exp:
immler@58656
  1021
  fixes x::"'a::{real_normed_field,banach}"
immler@58656
  1022
  shows "isCont exp x"
huffman@44311
  1023
  by (rule DERIV_exp [THEN DERIV_isCont])
huffman@44311
  1024
immler@58656
  1025
lemma isCont_exp' [simp]:
lp15@59613
  1026
  fixes f:: "_ \<Rightarrow>'a::{real_normed_field,banach}"
immler@58656
  1027
  shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. exp (f x)) a"
huffman@44311
  1028
  by (rule isCont_o2 [OF _ isCont_exp])
huffman@44311
  1029
huffman@44311
  1030
lemma tendsto_exp [tendsto_intros]:
lp15@59613
  1031
  fixes f:: "_ \<Rightarrow>'a::{real_normed_field,banach}"
immler@58656
  1032
  shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. exp (f x)) ---> exp a) F"
huffman@44311
  1033
  by (rule isCont_tendsto_compose [OF isCont_exp])
huffman@23045
  1034
wenzelm@53079
  1035
lemma continuous_exp [continuous_intros]:
lp15@59613
  1036
  fixes f:: "_ \<Rightarrow>'a::{real_normed_field,banach}"
immler@58656
  1037
  shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. exp (f x))"
hoelzl@51478
  1038
  unfolding continuous_def by (rule tendsto_exp)
hoelzl@51478
  1039
hoelzl@56371
  1040
lemma continuous_on_exp [continuous_intros]:
lp15@59613
  1041
  fixes f:: "_ \<Rightarrow>'a::{real_normed_field,banach}"
immler@58656
  1042
  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. exp (f x))"
hoelzl@51478
  1043
  unfolding continuous_on_def by (auto intro: tendsto_exp)
hoelzl@51478
  1044
wenzelm@53079
  1045
huffman@29167
  1046
subsubsection {* Properties of the Exponential Function *}
paulson@15077
  1047
huffman@23278
  1048
lemma powser_zero:
haftmann@31017
  1049
  fixes f :: "nat \<Rightarrow> 'a::{real_normed_algebra_1}"
huffman@23278
  1050
  shows "(\<Sum>n. f n * 0 ^ n) = f 0"
paulson@15077
  1051
proof -
hoelzl@56193
  1052
  have "(\<Sum>n<1. f n * 0 ^ n) = (\<Sum>n. f n * 0 ^ n)"
hoelzl@56213
  1053
    by (subst suminf_finite[where N="{0}"]) (auto simp: power_0_left)
huffman@30082
  1054
  thus ?thesis unfolding One_nat_def by simp
paulson@15077
  1055
qed
paulson@15077
  1056
huffman@23278
  1057
lemma exp_zero [simp]: "exp 0 = 1"
wenzelm@53079
  1058
  unfolding exp_def by (simp add: scaleR_conv_of_real powser_zero)
huffman@23278
  1059
immler@58656
  1060
lemma exp_series_add_commuting:
immler@58656
  1061
  fixes x y :: "'a::{real_normed_algebra_1, banach}"
lp15@59730
  1062
  defines S_def: "S \<equiv> \<lambda>x n. x^n /\<^sub>R fact n"
immler@58656
  1063
  assumes comm: "x * y = y * x"
hoelzl@56213
  1064
  shows "S (x + y) n = (\<Sum>i\<le>n. S x i * S y (n - i))"
huffman@23115
  1065
proof (induct n)
huffman@23115
  1066
  case 0
huffman@23115
  1067
  show ?case
huffman@23115
  1068
    unfolding S_def by simp
huffman@23115
  1069
next
huffman@23115
  1070
  case (Suc n)
haftmann@25062
  1071
  have S_Suc: "\<And>x n. S x (Suc n) = (x * S x n) /\<^sub>R real (Suc n)"
huffman@30273
  1072
    unfolding S_def by (simp del: mult_Suc)
haftmann@25062
  1073
  hence times_S: "\<And>x n. x * S x n = real (Suc n) *\<^sub>R S x (Suc n)"
huffman@23115
  1074
    by simp
immler@58656
  1075
  have S_comm: "\<And>n. S x n * y = y * S x n"
immler@58656
  1076
    by (simp add: power_commuting_commutes comm S_def)
huffman@23115
  1077
haftmann@25062
  1078
  have "real (Suc n) *\<^sub>R S (x + y) (Suc n) = (x + y) * S (x + y) n"
huffman@23115
  1079
    by (simp only: times_S)
hoelzl@56213
  1080
  also have "\<dots> = (x + y) * (\<Sum>i\<le>n. S x i * S y (n-i))"
huffman@23115
  1081
    by (simp only: Suc)
hoelzl@56213
  1082
  also have "\<dots> = x * (\<Sum>i\<le>n. S x i * S y (n-i))
hoelzl@56213
  1083
                + y * (\<Sum>i\<le>n. S x i * S y (n-i))"
webertj@49962
  1084
    by (rule distrib_right)
immler@58656
  1085
  also have "\<dots> = (\<Sum>i\<le>n. x * S x i * S y (n-i))
immler@58656
  1086
                + (\<Sum>i\<le>n. S x i * y * S y (n-i))"
immler@58656
  1087
    by (simp add: setsum_right_distrib ac_simps S_comm)
immler@58656
  1088
  also have "\<dots> = (\<Sum>i\<le>n. x * S x i * S y (n-i))
hoelzl@56213
  1089
                + (\<Sum>i\<le>n. S x i * (y * S y (n-i)))"
immler@58656
  1090
    by (simp add: ac_simps)
hoelzl@56213
  1091
  also have "\<dots> = (\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i)))
hoelzl@56213
  1092
                + (\<Sum>i\<le>n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
huffman@23115
  1093
    by (simp add: times_S Suc_diff_le)
hoelzl@56213
  1094
  also have "(\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n-i))) =
hoelzl@56213
  1095
             (\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n-i)))"
hoelzl@56213
  1096
    by (subst setsum_atMost_Suc_shift) simp
hoelzl@56213
  1097
  also have "(\<Sum>i\<le>n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
hoelzl@56213
  1098
             (\<Sum>i\<le>Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i)))"
hoelzl@56213
  1099
    by simp
hoelzl@56213
  1100
  also have "(\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n-i))) +
hoelzl@56213
  1101
             (\<Sum>i\<le>Suc n. real (Suc n-i) *\<^sub>R (S x i * S y (Suc n-i))) =
hoelzl@56213
  1102
             (\<Sum>i\<le>Suc n. real (Suc n) *\<^sub>R (S x i * S y (Suc n-i)))"
haftmann@57418
  1103
    by (simp only: setsum.distrib [symmetric] scaleR_left_distrib [symmetric]
hoelzl@56213
  1104
                   real_of_nat_add [symmetric]) simp
hoelzl@56213
  1105
  also have "\<dots> = real (Suc n) *\<^sub>R (\<Sum>i\<le>Suc n. S x i * S y (Suc n-i))"
huffman@23127
  1106
    by (simp only: scaleR_right.setsum)
huffman@23115
  1107
  finally show
hoelzl@56213
  1108
    "S (x + y) (Suc n) = (\<Sum>i\<le>Suc n. S x i * S y (Suc n - i))"
huffman@35216
  1109
    by (simp del: setsum_cl_ivl_Suc)
huffman@23115
  1110
qed
huffman@23115
  1111
immler@58656
  1112
lemma exp_add_commuting: "x * y = y * x \<Longrightarrow> exp (x + y) = exp x * exp y"
wenzelm@53079
  1113
  unfolding exp_def
immler@58656
  1114
  by (simp only: Cauchy_product summable_norm_exp exp_series_add_commuting)
immler@58656
  1115
immler@58656
  1116
lemma exp_add:
immler@58656
  1117
  fixes x y::"'a::{real_normed_field,banach}"
immler@58656
  1118
  shows "exp (x + y) = exp x * exp y"
immler@58656
  1119
  by (rule exp_add_commuting) (simp add: ac_simps)
immler@58656
  1120
lp15@59613
  1121
lemma exp_double: "exp(2 * z) = exp z ^ 2"
lp15@59613
  1122
  by (simp add: exp_add_commuting mult_2 power2_eq_square)
lp15@59613
  1123
immler@58656
  1124
lemmas mult_exp_exp = exp_add [symmetric]
huffman@29170
  1125
huffman@23241
  1126
lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
wenzelm@53079
  1127
  unfolding exp_def
wenzelm@53079
  1128
  apply (subst suminf_of_real)
wenzelm@53079
  1129
  apply (rule summable_exp_generic)
wenzelm@53079
  1130
  apply (simp add: scaleR_conv_of_real)
wenzelm@53079
  1131
  done
huffman@23241
  1132
huffman@29170
  1133
lemma exp_not_eq_zero [simp]: "exp x \<noteq> 0"
huffman@29170
  1134
proof
immler@58656
  1135
  have "exp x * exp (- x) = 1" by (simp add: exp_add_commuting[symmetric])
huffman@29170
  1136
  also assume "exp x = 0"
huffman@29170
  1137
  finally show "False" by simp
paulson@15077
  1138
qed
paulson@15077
  1139
immler@58656
  1140
lemma exp_minus_inverse:
immler@58656
  1141
  shows "exp x * exp (- x) = 1"
immler@58656
  1142
  by (simp add: exp_add_commuting[symmetric])
immler@58656
  1143
immler@58656
  1144
lemma exp_minus:
immler@58656
  1145
  fixes x :: "'a::{real_normed_field, banach}"
immler@58656
  1146
  shows "exp (- x) = inverse (exp x)"
immler@58656
  1147
  by (intro inverse_unique [symmetric] exp_minus_inverse)
immler@58656
  1148
immler@58656
  1149
lemma exp_diff:
immler@58656
  1150
  fixes x :: "'a::{real_normed_field, banach}"
immler@58656
  1151
  shows "exp (x - y) = exp x / exp y"
haftmann@54230
  1152
  using exp_add [of x "- y"] by (simp add: exp_minus divide_inverse)
paulson@15077
  1153
lp15@59613
  1154
lemma exp_of_nat_mult:
lp15@59613
  1155
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59613
  1156
  shows "exp(of_nat n * x) = exp(x) ^ n"
lp15@59613
  1157
    by (induct n) (auto simp add: distrib_left exp_add mult.commute)
lp15@59613
  1158
lp15@59613
  1159
lemma exp_setsum: "finite I \<Longrightarrow> exp(setsum f I) = setprod (\<lambda>x. exp(f x)) I"
lp15@59613
  1160
  by (induction I rule: finite_induct) (auto simp: exp_add_commuting mult.commute)
lp15@59613
  1161
huffman@29167
  1162
huffman@29167
  1163
subsubsection {* Properties of the Exponential Function on Reals *}
huffman@29167
  1164
huffman@29170
  1165
text {* Comparisons of @{term "exp x"} with zero. *}
huffman@29167
  1166
huffman@29167
  1167
text{*Proof: because every exponential can be seen as a square.*}
huffman@29167
  1168
lemma exp_ge_zero [simp]: "0 \<le> exp (x::real)"
huffman@29167
  1169
proof -
huffman@29167
  1170
  have "0 \<le> exp (x/2) * exp (x/2)" by simp
huffman@29167
  1171
  thus ?thesis by (simp add: exp_add [symmetric])
huffman@29167
  1172
qed
huffman@29167
  1173
huffman@23115
  1174
lemma exp_gt_zero [simp]: "0 < exp (x::real)"
wenzelm@53079
  1175
  by (simp add: order_less_le)
paulson@15077
  1176
huffman@29170
  1177
lemma not_exp_less_zero [simp]: "\<not> exp (x::real) < 0"
wenzelm@53079
  1178
  by (simp add: not_less)
huffman@29170
  1179
huffman@29170
  1180
lemma not_exp_le_zero [simp]: "\<not> exp (x::real) \<le> 0"
wenzelm@53079
  1181
  by (simp add: not_le)
paulson@15077
  1182
huffman@23115
  1183
lemma abs_exp_cancel [simp]: "\<bar>exp x::real\<bar> = exp x"
wenzelm@53079
  1184
  by simp
paulson@15077
  1185
lp15@59669
  1186
(*FIXME: superseded by exp_of_nat_mult*)
lp15@59669
  1187
lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
haftmann@57512
  1188
  by (induct n) (auto simp add: real_of_nat_Suc distrib_left exp_add mult.commute)
lp15@59669
  1189
huffman@29170
  1190
text {* Strict monotonicity of exponential. *}
huffman@29170
  1191
lp15@59669
  1192
lemma exp_ge_add_one_self_aux:
paulson@54575
  1193
  assumes "0 \<le> (x::real)" shows "1+x \<le> exp(x)"
paulson@54575
  1194
using order_le_imp_less_or_eq [OF assms]
lp15@59669
  1195
proof
paulson@54575
  1196
  assume "0 < x"
lp15@59730
  1197
  have "1+x \<le> (\<Sum>n<2. inverse (fact n) * x^n)"
paulson@54575
  1198
    by (auto simp add: numeral_2_eq_2)
lp15@59730
  1199
  also have "... \<le> (\<Sum>n. inverse (fact n) * x^n)"
hoelzl@56213
  1200
    apply (rule setsum_le_suminf [OF summable_exp])
paulson@54575
  1201
    using `0 < x`
paulson@54575
  1202
    apply (auto  simp add:  zero_le_mult_iff)
paulson@54575
  1203
    done
lp15@59669
  1204
  finally show "1+x \<le> exp x"
paulson@54575
  1205
    by (simp add: exp_def)
paulson@54575
  1206
next
paulson@54575
  1207
  assume "0 = x"
paulson@54575
  1208
  then show "1 + x \<le> exp x"
paulson@54575
  1209
    by auto
paulson@54575
  1210
qed
huffman@29170
  1211
huffman@29170
  1212
lemma exp_gt_one: "0 < (x::real) \<Longrightarrow> 1 < exp x"
huffman@29170
  1213
proof -
huffman@29170
  1214
  assume x: "0 < x"
huffman@29170
  1215
  hence "1 < 1 + x" by simp
huffman@29170
  1216
  also from x have "1 + x \<le> exp x"
huffman@29170
  1217
    by (simp add: exp_ge_add_one_self_aux)
huffman@29170
  1218
  finally show ?thesis .
huffman@29170
  1219
qed
huffman@29170
  1220
paulson@15077
  1221
lemma exp_less_mono:
huffman@23115
  1222
  fixes x y :: real
wenzelm@53079
  1223
  assumes "x < y"
wenzelm@53079
  1224
  shows "exp x < exp y"
paulson@15077
  1225
proof -
huffman@29165
  1226
  from `x < y` have "0 < y - x" by simp
huffman@29165
  1227
  hence "1 < exp (y - x)" by (rule exp_gt_one)
huffman@29165
  1228
  hence "1 < exp y / exp x" by (simp only: exp_diff)
huffman@29165
  1229
  thus "exp x < exp y" by simp
paulson@15077
  1230
qed
paulson@15077
  1231
wenzelm@53079
  1232
lemma exp_less_cancel: "exp (x::real) < exp y \<Longrightarrow> x < y"
paulson@54575
  1233
  unfolding linorder_not_le [symmetric]
paulson@54575
  1234
  by (auto simp add: order_le_less exp_less_mono)
paulson@15077
  1235
huffman@29170
  1236
lemma exp_less_cancel_iff [iff]: "exp (x::real) < exp y \<longleftrightarrow> x < y"
wenzelm@53079
  1237
  by (auto intro: exp_less_mono exp_less_cancel)
paulson@15077
  1238
huffman@29170
  1239
lemma exp_le_cancel_iff [iff]: "exp (x::real) \<le> exp y \<longleftrightarrow> x \<le> y"
wenzelm@53079
  1240
  by (auto simp add: linorder_not_less [symmetric])
paulson@15077
  1241
huffman@29170
  1242
lemma exp_inj_iff [iff]: "exp (x::real) = exp y \<longleftrightarrow> x = y"
wenzelm@53079
  1243
  by (simp add: order_eq_iff)
paulson@15077
  1244
huffman@29170
  1245
text {* Comparisons of @{term "exp x"} with one. *}
huffman@29170
  1246
huffman@29170
  1247
lemma one_less_exp_iff [simp]: "1 < exp (x::real) \<longleftrightarrow> 0 < x"
huffman@29170
  1248
  using exp_less_cancel_iff [where x=0 and y=x] by simp
huffman@29170
  1249
huffman@29170
  1250
lemma exp_less_one_iff [simp]: "exp (x::real) < 1 \<longleftrightarrow> x < 0"
huffman@29170
  1251
  using exp_less_cancel_iff [where x=x and y=0] by simp
huffman@29170
  1252
huffman@29170
  1253
lemma one_le_exp_iff [simp]: "1 \<le> exp (x::real) \<longleftrightarrow> 0 \<le> x"
huffman@29170
  1254
  using exp_le_cancel_iff [where x=0 and y=x] by simp
huffman@29170
  1255
huffman@29170
  1256
lemma exp_le_one_iff [simp]: "exp (x::real) \<le> 1 \<longleftrightarrow> x \<le> 0"
huffman@29170
  1257
  using exp_le_cancel_iff [where x=x and y=0] by simp
huffman@29170
  1258
huffman@29170
  1259
lemma exp_eq_one_iff [simp]: "exp (x::real) = 1 \<longleftrightarrow> x = 0"
huffman@29170
  1260
  using exp_inj_iff [where x=x and y=0] by simp
huffman@29170
  1261
wenzelm@53079
  1262
lemma lemma_exp_total: "1 \<le> y \<Longrightarrow> \<exists>x. 0 \<le> x & x \<le> y - 1 & exp(x::real) = y"
huffman@44755
  1263
proof (rule IVT)
huffman@44755
  1264
  assume "1 \<le> y"
huffman@44755
  1265
  hence "0 \<le> y - 1" by simp
huffman@44755
  1266
  hence "1 + (y - 1) \<le> exp (y - 1)" by (rule exp_ge_add_one_self_aux)
huffman@44755
  1267
  thus "y \<le> exp (y - 1)" by simp
huffman@44755
  1268
qed (simp_all add: le_diff_eq)
paulson@15077
  1269
wenzelm@53079
  1270
lemma exp_total: "0 < (y::real) \<Longrightarrow> \<exists>x. exp x = y"
huffman@44755
  1271
proof (rule linorder_le_cases [of 1 y])
wenzelm@53079
  1272
  assume "1 \<le> y"
wenzelm@53079
  1273
  thus "\<exists>x. exp x = y" by (fast dest: lemma_exp_total)
huffman@44755
  1274
next
huffman@44755
  1275
  assume "0 < y" and "y \<le> 1"
huffman@44755
  1276
  hence "1 \<le> inverse y" by (simp add: one_le_inverse_iff)
huffman@44755
  1277
  then obtain x where "exp x = inverse y" by (fast dest: lemma_exp_total)
huffman@44755
  1278
  hence "exp (- x) = y" by (simp add: exp_minus)
huffman@44755
  1279
  thus "\<exists>x. exp x = y" ..
huffman@44755
  1280
qed
paulson@15077
  1281
paulson@15077
  1282
huffman@29164
  1283
subsection {* Natural Logarithm *}
paulson@15077
  1284
wenzelm@53079
  1285
definition ln :: "real \<Rightarrow> real"
wenzelm@53079
  1286
  where "ln x = (THE u. exp u = x)"
huffman@23043
  1287
huffman@23043
  1288
lemma ln_exp [simp]: "ln (exp x) = x"
huffman@44308
  1289
  by (simp add: ln_def)
paulson@15077
  1290
huffman@22654
  1291
lemma exp_ln [simp]: "0 < x \<Longrightarrow> exp (ln x) = x"
huffman@44308
  1292
  by (auto dest: exp_total)
huffman@22654
  1293
huffman@29171
  1294
lemma exp_ln_iff [simp]: "exp (ln x) = x \<longleftrightarrow> 0 < x"
huffman@44308
  1295
  by (metis exp_gt_zero exp_ln)
paulson@15077
  1296
huffman@29171
  1297
lemma ln_unique: "exp y = x \<Longrightarrow> ln x = y"
huffman@44308
  1298
  by (erule subst, rule ln_exp)
huffman@29171
  1299
huffman@29171
  1300
lemma ln_one [simp]: "ln 1 = 0"
wenzelm@53079
  1301
  by (rule ln_unique) simp
wenzelm@53079
  1302
wenzelm@53079
  1303
lemma ln_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x * y) = ln x + ln y"
wenzelm@53079
  1304
  by (rule ln_unique) (simp add: exp_add)
huffman@29171
  1305
huffman@29171
  1306
lemma ln_inverse: "0 < x \<Longrightarrow> ln (inverse x) = - ln x"
wenzelm@53079
  1307
  by (rule ln_unique) (simp add: exp_minus)
wenzelm@53079
  1308
wenzelm@53079
  1309
lemma ln_div: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x / y) = ln x - ln y"
wenzelm@53079
  1310
  by (rule ln_unique) (simp add: exp_diff)
paulson@15077
  1311
lp15@59730
  1312
lemma ln_realpow: "0 < x \<Longrightarrow> ln (x^n) = real n * ln x"
wenzelm@53079
  1313
  by (rule ln_unique) (simp add: exp_real_of_nat_mult)
wenzelm@53079
  1314
wenzelm@53079
  1315
lemma ln_less_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x < ln y \<longleftrightarrow> x < y"
wenzelm@53079
  1316
  by (subst exp_less_cancel_iff [symmetric]) simp
wenzelm@53079
  1317
wenzelm@53079
  1318
lemma ln_le_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x \<le> ln y \<longleftrightarrow> x \<le> y"
huffman@44308
  1319
  by (simp add: linorder_not_less [symmetric])
huffman@29171
  1320
wenzelm@53079
  1321
lemma ln_inj_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x = ln y \<longleftrightarrow> x = y"
huffman@44308
  1322
  by (simp add: order_eq_iff)
huffman@29171
  1323
huffman@29171
  1324
lemma ln_add_one_self_le_self [simp]: "0 \<le> x \<Longrightarrow> ln (1 + x) \<le> x"
huffman@44308
  1325
  apply (rule exp_le_cancel_iff [THEN iffD1])
huffman@44308
  1326
  apply (simp add: exp_ge_add_one_self_aux)
huffman@44308
  1327
  done
paulson@15077
  1328
huffman@29171
  1329
lemma ln_less_self [simp]: "0 < x \<Longrightarrow> ln x < x"
huffman@44308
  1330
  by (rule order_less_le_trans [where y="ln (1 + x)"]) simp_all
huffman@44308
  1331
huffman@44308
  1332
lemma ln_ge_zero [simp]: "1 \<le> x \<Longrightarrow> 0 \<le> ln x"
huffman@44308
  1333
  using ln_le_cancel_iff [of 1 x] by simp
huffman@44308
  1334
wenzelm@53079
  1335
lemma ln_ge_zero_imp_ge_one: "0 \<le> ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> x"
huffman@44308
  1336
  using ln_le_cancel_iff [of 1 x] by simp
huffman@44308
  1337
wenzelm@53079
  1338
lemma ln_ge_zero_iff [simp]: "0 < x \<Longrightarrow> 0 \<le> ln x \<longleftrightarrow> 1 \<le> x"
huffman@44308
  1339
  using ln_le_cancel_iff [of 1 x] by simp
huffman@44308
  1340
wenzelm@53079
  1341
lemma ln_less_zero_iff [simp]: "0 < x \<Longrightarrow> ln x < 0 \<longleftrightarrow> x < 1"
huffman@44308
  1342
  using ln_less_cancel_iff [of x 1] by simp
huffman@44308
  1343
huffman@44308
  1344
lemma ln_gt_zero: "1 < x \<Longrightarrow> 0 < ln x"
huffman@44308
  1345
  using ln_less_cancel_iff [of 1 x] by simp
huffman@44308
  1346
wenzelm@53079
  1347
lemma ln_gt_zero_imp_gt_one: "0 < ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 < x"
huffman@44308
  1348
  using ln_less_cancel_iff [of 1 x] by simp
huffman@44308
  1349
wenzelm@53079
  1350
lemma ln_gt_zero_iff [simp]: "0 < x \<Longrightarrow> 0 < ln x \<longleftrightarrow> 1 < x"
huffman@44308
  1351
  using ln_less_cancel_iff [of 1 x] by simp
huffman@44308
  1352
wenzelm@53079
  1353
lemma ln_eq_zero_iff [simp]: "0 < x \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1"
huffman@44308
  1354
  using ln_inj_iff [of x 1] by simp
huffman@44308
  1355
wenzelm@53079
  1356
lemma ln_less_zero: "0 < x \<Longrightarrow> x < 1 \<Longrightarrow> ln x < 0"
huffman@44308
  1357
  by simp
paulson@15077
  1358
hoelzl@57275
  1359
lemma ln_neg_is_const: "x \<le> 0 \<Longrightarrow> ln x = (THE x. False)"
hoelzl@57275
  1360
  by (auto simp add: ln_def intro!: arg_cong[where f=The])
hoelzl@57275
  1361
hoelzl@57275
  1362
lemma isCont_ln: assumes "x \<noteq> 0" shows "isCont ln x"
hoelzl@57275
  1363
proof cases
hoelzl@57275
  1364
  assume "0 < x"
hoelzl@57275
  1365
  moreover then have "isCont ln (exp (ln x))"
hoelzl@57275
  1366
    by (intro isCont_inv_fun[where d="\<bar>x\<bar>" and f=exp]) auto
hoelzl@57275
  1367
  ultimately show ?thesis
hoelzl@57275
  1368
    by simp
hoelzl@57275
  1369
next
hoelzl@57275
  1370
  assume "\<not> 0 < x" with `x \<noteq> 0` show "isCont ln x"
hoelzl@57275
  1371
    unfolding isCont_def
hoelzl@57275
  1372
    by (subst filterlim_cong[OF _ refl, of _ "nhds (ln 0)" _ "\<lambda>_. ln 0"])
hoelzl@57275
  1373
       (auto simp: ln_neg_is_const not_less eventually_at dist_real_def
hoelzl@58729
  1374
                intro!: exI[of _ "\<bar>x\<bar>"])
hoelzl@57275
  1375
qed
huffman@23045
  1376
huffman@45915
  1377
lemma tendsto_ln [tendsto_intros]:
hoelzl@57275
  1378
  "(f ---> a) F \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. ln (f x)) ---> ln a) F"
huffman@45915
  1379
  by (rule isCont_tendsto_compose [OF isCont_ln])
huffman@45915
  1380
hoelzl@51478
  1381
lemma continuous_ln:
hoelzl@57275
  1382
  "continuous F f \<Longrightarrow> f (Lim F (\<lambda>x. x)) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. ln (f x))"
hoelzl@51478
  1383
  unfolding continuous_def by (rule tendsto_ln)
hoelzl@51478
  1384
hoelzl@51478
  1385
lemma isCont_ln' [continuous_intros]:
hoelzl@57275
  1386
  "continuous (at x) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. ln (f x))"
hoelzl@51478
  1387
  unfolding continuous_at by (rule tendsto_ln)
hoelzl@51478
  1388
hoelzl@51478
  1389
lemma continuous_within_ln [continuous_intros]:
hoelzl@57275
  1390
  "continuous (at x within s) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. ln (f x))"
hoelzl@51478
  1391
  unfolding continuous_within by (rule tendsto_ln)
hoelzl@51478
  1392
hoelzl@56371
  1393
lemma continuous_on_ln [continuous_intros]:
hoelzl@57275
  1394
  "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. f x \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. ln (f x))"
hoelzl@51478
  1395
  unfolding continuous_on_def by (auto intro: tendsto_ln)
hoelzl@51478
  1396
huffman@23045
  1397
lemma DERIV_ln: "0 < x \<Longrightarrow> DERIV ln x :> inverse x"
huffman@44308
  1398
  apply (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
paulson@54576
  1399
  apply (auto intro: DERIV_cong [OF DERIV_exp exp_ln] isCont_ln)
huffman@44308
  1400
  done
huffman@23045
  1401
wenzelm@53079
  1402
lemma DERIV_ln_divide: "0 < x \<Longrightarrow> DERIV ln x :> 1 / x"
paulson@33667
  1403
  by (rule DERIV_ln[THEN DERIV_cong], simp, simp add: divide_inverse)
paulson@33667
  1404
hoelzl@56381
  1405
declare DERIV_ln_divide[THEN DERIV_chain2, derivative_intros]
hoelzl@51527
  1406
wenzelm@53079
  1407
lemma ln_series:
wenzelm@53079
  1408
  assumes "0 < x" and "x < 2"
wenzelm@53079
  1409
  shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
wenzelm@53079
  1410
  (is "ln x = suminf (?f (x - 1))")
hoelzl@29803
  1411
proof -
wenzelm@53079
  1412
  let ?f' = "\<lambda>x n. (-1)^n * (x - 1)^n"
hoelzl@29803
  1413
hoelzl@29803
  1414
  have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
hoelzl@29803
  1415
  proof (rule DERIV_isconst3[where x=x])
wenzelm@53079
  1416
    fix x :: real
wenzelm@53079
  1417
    assume "x \<in> {0 <..< 2}"
wenzelm@53079
  1418
    hence "0 < x" and "x < 2" by auto
wenzelm@53079
  1419
    have "norm (1 - x) < 1"
wenzelm@53079
  1420
      using `0 < x` and `x < 2` by auto
hoelzl@29803
  1421
    have "1 / x = 1 / (1 - (1 - x))" by auto
wenzelm@53079
  1422
    also have "\<dots> = (\<Sum> n. (1 - x)^n)"
wenzelm@53079
  1423
      using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
wenzelm@53079
  1424
    also have "\<dots> = suminf (?f' x)"
wenzelm@53079
  1425
      unfolding power_mult_distrib[symmetric]
wenzelm@53079
  1426
      by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
wenzelm@53079
  1427
    finally have "DERIV ln x :> suminf (?f' x)"
wenzelm@53079
  1428
      using DERIV_ln[OF `0 < x`] unfolding divide_inverse by auto
hoelzl@29803
  1429
    moreover
hoelzl@29803
  1430
    have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
wenzelm@53079
  1431
    have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :>
wenzelm@53079
  1432
      (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
hoelzl@29803
  1433
    proof (rule DERIV_power_series')
wenzelm@53079
  1434
      show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1"
wenzelm@53079
  1435
        using `0 < x` `x < 2` by auto
wenzelm@53079
  1436
      fix x :: real
wenzelm@53079
  1437
      assume "x \<in> {- 1<..<1}"
wenzelm@53079
  1438
      hence "norm (-x) < 1" by auto
lp15@59730
  1439
      show "summable (\<lambda>n. (- 1) ^ n * (1 / real (n + 1)) * real (Suc n) * x^n)"
wenzelm@53079
  1440
        unfolding One_nat_def
wenzelm@53079
  1441
        by (auto simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
hoelzl@29803
  1442
    qed
wenzelm@53079
  1443
    hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)"
wenzelm@53079
  1444
      unfolding One_nat_def by auto
wenzelm@53079
  1445
    hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)"
hoelzl@56381
  1446
      unfolding DERIV_def repos .
hoelzl@29803
  1447
    ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
hoelzl@29803
  1448
      by (rule DERIV_diff)
hoelzl@29803
  1449
    thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
hoelzl@29803
  1450
  qed (auto simp add: assms)
huffman@44289
  1451
  thus ?thesis by auto
hoelzl@29803
  1452
qed
paulson@15077
  1453
lp15@59730
  1454
lemma exp_first_two_terms:
lp15@59730
  1455
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59730
  1456
  shows "exp x = 1 + x + (\<Sum> n. inverse(fact (n+2)) * (x ^ (n+2)))"
hoelzl@50326
  1457
proof -
lp15@59730
  1458
  have "exp x = suminf (\<lambda>n. inverse(fact n) * (x^n))"
lp15@59730
  1459
    by (simp add: exp_def scaleR_conv_of_real nonzero_of_real_inverse)
lp15@59669
  1460
  also from summable_exp have "... = (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2))) +
lp15@59730
  1461
    (\<Sum> n::nat<2. inverse(fact n) * (x^n))" (is "_ = _ + ?a")
hoelzl@50326
  1462
    by (rule suminf_split_initial_segment)
hoelzl@50326
  1463
  also have "?a = 1 + x"
hoelzl@50326
  1464
    by (simp add: numeral_2_eq_2)
hoelzl@56193
  1465
  finally show ?thesis
hoelzl@56193
  1466
    by simp
hoelzl@50326
  1467
qed
hoelzl@50326
  1468
wenzelm@53079
  1469
lemma exp_bound: "0 <= (x::real) \<Longrightarrow> x <= 1 \<Longrightarrow> exp x <= 1 + x + x\<^sup>2"
hoelzl@50326
  1470
proof -
hoelzl@50326
  1471
  assume a: "0 <= x"
hoelzl@50326
  1472
  assume b: "x <= 1"
wenzelm@53079
  1473
  {
wenzelm@53079
  1474
    fix n :: nat
lp15@59730
  1475
    have "(2::nat) * 2 ^ n \<le> fact (n + 2)"
wenzelm@53079
  1476
      by (induct n) simp_all
lp15@59730
  1477
    hence "real ((2::nat) * 2 ^ n) \<le> real_of_nat (fact (n + 2))"
hoelzl@50326
  1478
      by (simp only: real_of_nat_le_iff)
lp15@59730
  1479
    hence "((2::real) * 2 ^ n) \<le> fact (n + 2)"
lp15@59730
  1480
      unfolding of_nat_fact real_of_nat_def
lp15@59730
  1481
      by (simp add: of_nat_mult of_nat_power)
lp15@59730
  1482
    hence "inverse (fact (n + 2)) \<le> inverse ((2::real) * 2 ^ n)"
hoelzl@50326
  1483
      by (rule le_imp_inverse_le) simp
lp15@59730
  1484
    hence "inverse (fact (n + 2)) \<le> 1/(2::real) * (1/2)^n"
wenzelm@53079
  1485
      by (simp add: power_inverse)
wenzelm@53015
  1486
    hence "inverse (fact (n + 2)) * (x^n * x\<^sup>2) \<le> 1/2 * (1/2)^n * (1 * x\<^sup>2)"
hoelzl@50326
  1487
      by (rule mult_mono)
nipkow@56536
  1488
        (rule mult_mono, simp_all add: power_le_one a b)
wenzelm@53015
  1489
    hence "inverse (fact (n + 2)) * x ^ (n + 2) \<le> (x\<^sup>2/2) * ((1/2)^n)"
lp15@59730
  1490
      unfolding power_add by (simp add: ac_simps del: fact.simps) }
hoelzl@50326
  1491
  note aux1 = this
wenzelm@53015
  1492
  have "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums (x\<^sup>2 / 2 * (1 / (1 - 1 / 2)))"
hoelzl@50326
  1493
    by (intro sums_mult geometric_sums, simp)
wenzelm@53076
  1494
  hence aux2: "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums x\<^sup>2"
hoelzl@50326
  1495
    by simp
wenzelm@53079
  1496
  have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <= x\<^sup>2"
hoelzl@50326
  1497
  proof -
wenzelm@53079
  1498
    have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n+2))) <=
wenzelm@53079
  1499
        suminf (\<lambda>n. (x\<^sup>2/2) * ((1/2)^n))"
hoelzl@56213
  1500
      apply (rule suminf_le)
hoelzl@50326
  1501
      apply (rule allI, rule aux1)
hoelzl@50326
  1502
      apply (rule summable_exp [THEN summable_ignore_initial_segment])
hoelzl@50326
  1503
      by (rule sums_summable, rule aux2)
wenzelm@53076
  1504
    also have "... = x\<^sup>2"
hoelzl@50326
  1505
      by (rule sums_unique [THEN sym], rule aux2)
hoelzl@50326
  1506
    finally show ?thesis .
hoelzl@50326
  1507
  qed
hoelzl@50326
  1508
  thus ?thesis unfolding exp_first_two_terms by auto
hoelzl@50326
  1509
qed
hoelzl@50326
  1510
lp15@59613
  1511
corollary exp_half_le2: "exp(1/2) \<le> (2::real)"
lp15@59613
  1512
  using exp_bound [of "1/2"]
lp15@59613
  1513
  by (simp add: field_simps)
lp15@59613
  1514
lp15@59613
  1515
lemma exp_bound_half: "norm(z) \<le> 1/2 \<Longrightarrow> norm(exp z) \<le> 2"
lp15@59613
  1516
  by (blast intro: order_trans intro!: exp_half_le2 norm_exp)
lp15@59613
  1517
lp15@59613
  1518
lemma exp_bound_lemma:
lp15@59613
  1519
  assumes "norm(z) \<le> 1/2" shows "norm(exp z) \<le> 1 + 2 * norm(z)"
lp15@59613
  1520
proof -
lp15@59613
  1521
  have n: "(norm z)\<^sup>2 \<le> norm z * 1"
lp15@59613
  1522
    unfolding power2_eq_square
lp15@59613
  1523
    apply (rule mult_left_mono)
lp15@59613
  1524
    using assms
lp15@59613
  1525
    apply (auto simp: )
lp15@59613
  1526
    done
lp15@59613
  1527
  show ?thesis
lp15@59613
  1528
    apply (rule order_trans [OF norm_exp])
lp15@59613
  1529
    apply (rule order_trans [OF exp_bound])
lp15@59613
  1530
    using assms n
lp15@59613
  1531
    apply (auto simp: )
lp15@59613
  1532
    done
lp15@59613
  1533
qed
lp15@59613
  1534
lp15@59613
  1535
lemma real_exp_bound_lemma:
lp15@59613
  1536
  fixes x :: real
lp15@59613
  1537
  shows "0 \<le> x \<Longrightarrow> x \<le> 1/2 \<Longrightarrow> exp(x) \<le> 1 + 2 * x"
lp15@59613
  1538
using exp_bound_lemma [of x]
lp15@59613
  1539
by simp
lp15@59613
  1540
wenzelm@53079
  1541
lemma ln_one_minus_pos_upper_bound: "0 <= x \<Longrightarrow> x < 1 \<Longrightarrow> ln (1 - x) <= - x"
hoelzl@50326
  1542
proof -
hoelzl@50326
  1543
  assume a: "0 <= (x::real)" and b: "x < 1"
wenzelm@53076
  1544
  have "(1 - x) * (1 + x + x\<^sup>2) = (1 - x^3)"
hoelzl@50326
  1545
    by (simp add: algebra_simps power2_eq_square power3_eq_cube)
hoelzl@50326
  1546
  also have "... <= 1"
hoelzl@50326
  1547
    by (auto simp add: a)
wenzelm@53076
  1548
  finally have "(1 - x) * (1 + x + x\<^sup>2) <= 1" .
wenzelm@53015
  1549
  moreover have c: "0 < 1 + x + x\<^sup>2"
hoelzl@50326
  1550
    by (simp add: add_pos_nonneg a)
wenzelm@53076
  1551
  ultimately have "1 - x <= 1 / (1 + x + x\<^sup>2)"
hoelzl@50326
  1552
    by (elim mult_imp_le_div_pos)
hoelzl@50326
  1553
  also have "... <= 1 / exp x"
lp15@59669
  1554
    by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs
paulson@54576
  1555
              real_sqrt_pow2_iff real_sqrt_power)
hoelzl@50326
  1556
  also have "... = exp (-x)"
hoelzl@50326
  1557
    by (auto simp add: exp_minus divide_inverse)
hoelzl@50326
  1558
  finally have "1 - x <= exp (- x)" .
hoelzl@50326
  1559
  also have "1 - x = exp (ln (1 - x))"
paulson@54576
  1560
    by (metis b diff_0 exp_ln_iff less_iff_diff_less_0 minus_diff_eq)
hoelzl@50326
  1561
  finally have "exp (ln (1 - x)) <= exp (- x)" .
hoelzl@50326
  1562
  thus ?thesis by (auto simp only: exp_le_cancel_iff)
hoelzl@50326
  1563
qed
hoelzl@50326
  1564
hoelzl@50326
  1565
lemma exp_ge_add_one_self [simp]: "1 + (x::real) <= exp x"
hoelzl@50326
  1566
  apply (case_tac "0 <= x")
hoelzl@50326
  1567
  apply (erule exp_ge_add_one_self_aux)
hoelzl@50326
  1568
  apply (case_tac "x <= -1")
hoelzl@50326
  1569
  apply (subgoal_tac "1 + x <= 0")
hoelzl@50326
  1570
  apply (erule order_trans)
hoelzl@50326
  1571
  apply simp
hoelzl@50326
  1572
  apply simp
hoelzl@50326
  1573
  apply (subgoal_tac "1 + x = exp(ln (1 + x))")
hoelzl@50326
  1574
  apply (erule ssubst)
hoelzl@50326
  1575
  apply (subst exp_le_cancel_iff)
hoelzl@50326
  1576
  apply (subgoal_tac "ln (1 - (- x)) <= - (- x)")
hoelzl@50326
  1577
  apply simp
hoelzl@50326
  1578
  apply (rule ln_one_minus_pos_upper_bound)
hoelzl@50326
  1579
  apply auto
hoelzl@50326
  1580
done
hoelzl@50326
  1581
wenzelm@53079
  1582
lemma ln_one_plus_pos_lower_bound: "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> x - x\<^sup>2 <= ln (1 + x)"
hoelzl@51527
  1583
proof -
hoelzl@51527
  1584
  assume a: "0 <= x" and b: "x <= 1"
wenzelm@53076
  1585
  have "exp (x - x\<^sup>2) = exp x / exp (x\<^sup>2)"
hoelzl@51527
  1586
    by (rule exp_diff)
wenzelm@53076
  1587
  also have "... <= (1 + x + x\<^sup>2) / exp (x \<^sup>2)"
paulson@54576
  1588
    by (metis a b divide_right_mono exp_bound exp_ge_zero)
wenzelm@53076
  1589
  also have "... <= (1 + x + x\<^sup>2) / (1 + x\<^sup>2)"
nipkow@56544
  1590
    by (simp add: a divide_left_mono add_pos_nonneg)
hoelzl@51527
  1591
  also from a have "... <= 1 + x"
hoelzl@51527
  1592
    by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
wenzelm@53076
  1593
  finally have "exp (x - x\<^sup>2) <= 1 + x" .
hoelzl@51527
  1594
  also have "... = exp (ln (1 + x))"
hoelzl@51527
  1595
  proof -
hoelzl@51527
  1596
    from a have "0 < 1 + x" by auto
hoelzl@51527
  1597
    thus ?thesis
hoelzl@51527
  1598
      by (auto simp only: exp_ln_iff [THEN sym])
hoelzl@51527
  1599
  qed
wenzelm@53076
  1600
  finally have "exp (x - x\<^sup>2) <= exp (ln (1 + x))" .
paulson@54576
  1601
  thus ?thesis
lp15@59669
  1602
    by (metis exp_le_cancel_iff)
hoelzl@51527
  1603
qed
hoelzl@51527
  1604
wenzelm@53079
  1605
lemma ln_one_minus_pos_lower_bound:
wenzelm@53079
  1606
  "0 <= x \<Longrightarrow> x <= (1 / 2) \<Longrightarrow> - x - 2 * x\<^sup>2 <= ln (1 - x)"
hoelzl@51527
  1607
proof -
hoelzl@51527
  1608
  assume a: "0 <= x" and b: "x <= (1 / 2)"
wenzelm@53079
  1609
  from b have c: "x < 1" by auto
hoelzl@51527
  1610
  then have "ln (1 - x) = - ln (1 + x / (1 - x))"
paulson@54576
  1611
    apply (subst ln_inverse [symmetric])
paulson@54576
  1612
    apply (simp add: field_simps)
paulson@54576
  1613
    apply (rule arg_cong [where f=ln])
paulson@54576
  1614
    apply (simp add: field_simps)
paulson@54576
  1615
    done
hoelzl@51527
  1616
  also have "- (x / (1 - x)) <= ..."
wenzelm@53079
  1617
  proof -
hoelzl@51527
  1618
    have "ln (1 + x / (1 - x)) <= x / (1 - x)"
hoelzl@56571
  1619
      using a c by (intro ln_add_one_self_le_self) auto
hoelzl@51527
  1620
    thus ?thesis
hoelzl@51527
  1621
      by auto
hoelzl@51527
  1622
  qed
hoelzl@51527
  1623
  also have "- (x / (1 - x)) = -x / (1 - x)"
hoelzl@51527
  1624
    by auto
hoelzl@51527
  1625
  finally have d: "- x / (1 - x) <= ln (1 - x)" .
hoelzl@51527
  1626
  have "0 < 1 - x" using a b by simp
wenzelm@53076
  1627
  hence e: "-x - 2 * x\<^sup>2 <= - x / (1 - x)"
hoelzl@51527
  1628
    using mult_right_le_one_le[of "x*x" "2*x"] a b
wenzelm@53079
  1629
    by (simp add: field_simps power2_eq_square)
wenzelm@53076
  1630
  from e d show "- x - 2 * x\<^sup>2 <= ln (1 - x)"
hoelzl@51527
  1631
    by (rule order_trans)
hoelzl@51527
  1632
qed
hoelzl@51527
  1633
wenzelm@53079
  1634
lemma ln_add_one_self_le_self2: "-1 < x \<Longrightarrow> ln(1 + x) <= x"
hoelzl@51527
  1635
  apply (subgoal_tac "ln (1 + x) \<le> ln (exp x)", simp)
hoelzl@51527
  1636
  apply (subst ln_le_cancel_iff)
hoelzl@51527
  1637
  apply auto
wenzelm@53079
  1638
  done
hoelzl@51527
  1639
hoelzl@51527
  1640
lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
wenzelm@53079
  1641
  "0 <= x \<Longrightarrow> x <= 1 \<Longrightarrow> abs(ln (1 + x) - x) <= x\<^sup>2"
hoelzl@51527
  1642
proof -
hoelzl@51527
  1643
  assume x: "0 <= x"
hoelzl@51527
  1644
  assume x1: "x <= 1"
hoelzl@51527
  1645
  from x have "ln (1 + x) <= x"
hoelzl@51527
  1646
    by (rule ln_add_one_self_le_self)
wenzelm@53079
  1647
  then have "ln (1 + x) - x <= 0"
hoelzl@51527
  1648
    by simp
hoelzl@51527
  1649
  then have "abs(ln(1 + x) - x) = - (ln(1 + x) - x)"
hoelzl@51527
  1650
    by (rule abs_of_nonpos)
wenzelm@53079
  1651
  also have "... = x - ln (1 + x)"
hoelzl@51527
  1652
    by simp
wenzelm@53076
  1653
  also have "... <= x\<^sup>2"
hoelzl@51527
  1654
  proof -
wenzelm@53076
  1655
    from x x1 have "x - x\<^sup>2 <= ln (1 + x)"
hoelzl@51527
  1656
      by (intro ln_one_plus_pos_lower_bound)
hoelzl@51527
  1657
    thus ?thesis
hoelzl@51527
  1658
      by simp
hoelzl@51527
  1659
  qed
hoelzl@51527
  1660
  finally show ?thesis .
hoelzl@51527
  1661
qed
hoelzl@51527
  1662
hoelzl@51527
  1663
lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
wenzelm@53079
  1664
  "-(1 / 2) <= x \<Longrightarrow> x <= 0 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
hoelzl@51527
  1665
proof -
hoelzl@51527
  1666
  assume a: "-(1 / 2) <= x"
hoelzl@51527
  1667
  assume b: "x <= 0"
wenzelm@53079
  1668
  have "abs(ln (1 + x) - x) = x - ln(1 - (-x))"
hoelzl@51527
  1669
    apply (subst abs_of_nonpos)
hoelzl@51527
  1670
    apply simp
hoelzl@51527
  1671
    apply (rule ln_add_one_self_le_self2)
hoelzl@51527
  1672
    using a apply auto
hoelzl@51527
  1673
    done
wenzelm@53076
  1674
  also have "... <= 2 * x\<^sup>2"
wenzelm@53076
  1675
    apply (subgoal_tac "- (-x) - 2 * (-x)\<^sup>2 <= ln (1 - (-x))")
hoelzl@51527
  1676
    apply (simp add: algebra_simps)
hoelzl@51527
  1677
    apply (rule ln_one_minus_pos_lower_bound)
hoelzl@51527
  1678
    using a b apply auto
hoelzl@51527
  1679
    done
hoelzl@51527
  1680
  finally show ?thesis .
hoelzl@51527
  1681
qed
hoelzl@51527
  1682
hoelzl@51527
  1683
lemma abs_ln_one_plus_x_minus_x_bound:
wenzelm@53079
  1684
    "abs x <= 1 / 2 \<Longrightarrow> abs(ln (1 + x) - x) <= 2 * x\<^sup>2"
hoelzl@51527
  1685
  apply (case_tac "0 <= x")
hoelzl@51527
  1686
  apply (rule order_trans)
hoelzl@51527
  1687
  apply (rule abs_ln_one_plus_x_minus_x_bound_nonneg)
hoelzl@51527
  1688
  apply auto
hoelzl@51527
  1689
  apply (rule abs_ln_one_plus_x_minus_x_bound_nonpos)
hoelzl@51527
  1690
  apply auto
wenzelm@53079
  1691
  done
wenzelm@53079
  1692
wenzelm@53079
  1693
lemma ln_x_over_x_mono: "exp 1 <= x \<Longrightarrow> x <= y \<Longrightarrow> (ln y / y) <= (ln x / x)"
hoelzl@51527
  1694
proof -
hoelzl@51527
  1695
  assume x: "exp 1 <= x" "x <= y"
hoelzl@51527
  1696
  moreover have "0 < exp (1::real)" by simp
hoelzl@51527
  1697
  ultimately have a: "0 < x" and b: "0 < y"
hoelzl@51527
  1698
    by (fast intro: less_le_trans order_trans)+
hoelzl@51527
  1699
  have "x * ln y - x * ln x = x * (ln y - ln x)"
hoelzl@51527
  1700
    by (simp add: algebra_simps)
hoelzl@51527
  1701
  also have "... = x * ln(y / x)"
hoelzl@51527
  1702
    by (simp only: ln_div a b)
hoelzl@51527
  1703
  also have "y / x = (x + (y - x)) / x"
hoelzl@51527
  1704
    by simp
hoelzl@51527
  1705
  also have "... = 1 + (y - x) / x"
hoelzl@51527
  1706
    using x a by (simp add: field_simps)
hoelzl@51527
  1707
  also have "x * ln(1 + (y - x) / x) <= x * ((y - x) / x)"
lp15@59669
  1708
    using x a
hoelzl@56571
  1709
    by (intro mult_left_mono ln_add_one_self_le_self) simp_all
hoelzl@51527
  1710
  also have "... = y - x" using a by simp
hoelzl@51527
  1711
  also have "... = (y - x) * ln (exp 1)" by simp
hoelzl@51527
  1712
  also have "... <= (y - x) * ln x"
hoelzl@51527
  1713
    apply (rule mult_left_mono)
hoelzl@51527
  1714
    apply (subst ln_le_cancel_iff)
hoelzl@51527
  1715
    apply fact
hoelzl@51527
  1716
    apply (rule a)
hoelzl@51527
  1717
    apply (rule x)
hoelzl@51527
  1718
    using x apply simp
hoelzl@51527
  1719
    done
hoelzl@51527
  1720
  also have "... = y * ln x - x * ln x"
hoelzl@51527
  1721
    by (rule left_diff_distrib)
hoelzl@51527
  1722
  finally have "x * ln y <= y * ln x"
hoelzl@51527
  1723
    by arith
hoelzl@51527
  1724
  then have "ln y <= (y * ln x) / x" using a by (simp add: field_simps)
hoelzl@51527
  1725
  also have "... = y * (ln x / x)" by simp
hoelzl@51527
  1726
  finally show ?thesis using b by (simp add: field_simps)
hoelzl@51527
  1727
qed
hoelzl@51527
  1728
wenzelm@53079
  1729
lemma ln_le_minus_one: "0 < x \<Longrightarrow> ln x \<le> x - 1"
hoelzl@51527
  1730
  using exp_ge_add_one_self[of "ln x"] by simp
hoelzl@51527
  1731
hoelzl@51527
  1732
lemma ln_eq_minus_one:
wenzelm@53079
  1733
  assumes "0 < x" "ln x = x - 1"
wenzelm@53079
  1734
  shows "x = 1"
hoelzl@51527
  1735
proof -
wenzelm@53079
  1736
  let ?l = "\<lambda>y. ln y - y + 1"
hoelzl@51527
  1737
  have D: "\<And>x. 0 < x \<Longrightarrow> DERIV ?l x :> (1 / x - 1)"
hoelzl@56381
  1738
    by (auto intro!: derivative_eq_intros)
hoelzl@51527
  1739
hoelzl@51527
  1740
  show ?thesis
hoelzl@51527
  1741
  proof (cases rule: linorder_cases)
hoelzl@51527
  1742
    assume "x < 1"
hoelzl@51527
  1743
    from dense[OF `x < 1`] obtain a where "x < a" "a < 1" by blast
hoelzl@51527
  1744
    from `x < a` have "?l x < ?l a"
hoelzl@51527
  1745
    proof (rule DERIV_pos_imp_increasing, safe)
wenzelm@53079
  1746
      fix y
wenzelm@53079
  1747
      assume "x \<le> y" "y \<le> a"
hoelzl@51527
  1748
      with `0 < x` `a < 1` have "0 < 1 / y - 1" "0 < y"
hoelzl@51527
  1749
        by (auto simp: field_simps)
hoelzl@51527
  1750
      with D show "\<exists>z. DERIV ?l y :> z \<and> 0 < z"
hoelzl@51527
  1751
        by auto
hoelzl@51527
  1752
    qed
hoelzl@51527
  1753
    also have "\<dots> \<le> 0"
hoelzl@51527
  1754
      using ln_le_minus_one `0 < x` `x < a` by (auto simp: field_simps)
hoelzl@51527
  1755
    finally show "x = 1" using assms by auto
hoelzl@51527
  1756
  next
hoelzl@51527
  1757
    assume "1 < x"
wenzelm@53079
  1758
    from dense[OF this] obtain a where "1 < a" "a < x" by blast
hoelzl@51527
  1759
    from `a < x` have "?l x < ?l a"
hoelzl@51527
  1760
    proof (rule DERIV_neg_imp_decreasing, safe)
wenzelm@53079
  1761
      fix y
wenzelm@53079
  1762
      assume "a \<le> y" "y \<le> x"
hoelzl@51527
  1763
      with `1 < a` have "1 / y - 1 < 0" "0 < y"
hoelzl@51527
  1764
        by (auto simp: field_simps)
hoelzl@51527
  1765
      with D show "\<exists>z. DERIV ?l y :> z \<and> z < 0"
hoelzl@51527
  1766
        by blast
hoelzl@51527
  1767
    qed
hoelzl@51527
  1768
    also have "\<dots> \<le> 0"
hoelzl@51527
  1769
      using ln_le_minus_one `1 < a` by (auto simp: field_simps)
hoelzl@51527
  1770
    finally show "x = 1" using assms by auto
wenzelm@53079
  1771
  next
wenzelm@53079
  1772
    assume "x = 1"
wenzelm@53079
  1773
    then show ?thesis by simp
wenzelm@53079
  1774
  qed
hoelzl@51527
  1775
qed
hoelzl@51527
  1776
hoelzl@50326
  1777
lemma exp_at_bot: "(exp ---> (0::real)) at_bot"
hoelzl@50326
  1778
  unfolding tendsto_Zfun_iff
hoelzl@50326
  1779
proof (rule ZfunI, simp add: eventually_at_bot_dense)
hoelzl@50326
  1780
  fix r :: real assume "0 < r"
wenzelm@53079
  1781
  {
wenzelm@53079
  1782
    fix x
wenzelm@53079
  1783
    assume "x < ln r"
hoelzl@50326
  1784
    then have "exp x < exp (ln r)"
hoelzl@50326
  1785
      by simp
hoelzl@50326
  1786
    with `0 < r` have "exp x < r"
wenzelm@53079
  1787
      by simp
wenzelm@53079
  1788
  }
hoelzl@50326
  1789
  then show "\<exists>k. \<forall>n<k. exp n < r" by auto
hoelzl@50326
  1790
qed
hoelzl@50326
  1791
hoelzl@50326
  1792
lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
hoelzl@50346
  1793
  by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="ln"])
hoelzl@50346
  1794
     (auto intro: eventually_gt_at_top)
hoelzl@50326
  1795
lp15@59613
  1796
lemma lim_exp_minus_1:
lp15@59613
  1797
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59613
  1798
  shows "((\<lambda>z::'a. (exp(z) - 1) / z) ---> 1) (at 0)"
lp15@59613
  1799
proof -
lp15@59613
  1800
  have "((\<lambda>z::'a. exp(z) - 1) has_field_derivative 1) (at 0)"
lp15@59613
  1801
    by (intro derivative_eq_intros | simp)+
lp15@59613
  1802
  then show ?thesis
lp15@59613
  1803
    by (simp add: Deriv.DERIV_iff2)
lp15@59613
  1804
qed
lp15@59613
  1805
hoelzl@50326
  1806
lemma ln_at_0: "LIM x at_right 0. ln x :> at_bot"
hoelzl@50346
  1807
  by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
hoelzl@51641
  1808
     (auto simp: eventually_at_filter)
hoelzl@50326
  1809
hoelzl@50326
  1810
lemma ln_at_top: "LIM x at_top. ln x :> at_top"
hoelzl@50346
  1811
  by (rule filterlim_at_top_at_top[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g="exp"])
hoelzl@50346
  1812
     (auto intro: eventually_gt_at_top)
hoelzl@50326
  1813
hoelzl@50347
  1814
lemma tendsto_power_div_exp_0: "((\<lambda>x. x ^ k / exp x) ---> (0::real)) at_top"
hoelzl@50347
  1815
proof (induct k)
wenzelm@53079
  1816
  case 0
hoelzl@50347
  1817
  show "((\<lambda>x. x ^ 0 / exp x) ---> (0::real)) at_top"
hoelzl@50347
  1818
    by (simp add: inverse_eq_divide[symmetric])
hoelzl@50347
  1819
       (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
hoelzl@50347
  1820
              at_top_le_at_infinity order_refl)
hoelzl@50347
  1821
next
hoelzl@50347
  1822
  case (Suc k)
hoelzl@50347
  1823
  show ?case
hoelzl@50347
  1824
  proof (rule lhospital_at_top_at_top)
hoelzl@50347
  1825
    show "eventually (\<lambda>x. DERIV (\<lambda>x. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
hoelzl@56381
  1826
      by eventually_elim (intro derivative_eq_intros, auto)
hoelzl@50347
  1827
    show "eventually (\<lambda>x. DERIV exp x :> exp x) at_top"
hoelzl@56381
  1828
      by eventually_elim auto
hoelzl@50347
  1829
    show "eventually (\<lambda>x. exp x \<noteq> 0) at_top"
hoelzl@50347
  1830
      by auto
hoelzl@50347
  1831
    from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
hoelzl@50347
  1832
    show "((\<lambda>x. real (Suc k) * x ^ k / exp x) ---> 0) at_top"
hoelzl@50347
  1833
      by simp
hoelzl@50347
  1834
  qed (rule exp_at_top)
hoelzl@50347
  1835
qed
hoelzl@50347
  1836
hoelzl@51527
  1837
wenzelm@53079
  1838
definition powr :: "[real,real] => real"  (infixr "powr" 80)
wenzelm@53079
  1839
  -- {*exponentation with real exponent*}
wenzelm@53079
  1840
  where "x powr a = exp(a * ln x)"
wenzelm@53079
  1841
wenzelm@53079
  1842
definition log :: "[real,real] => real"
wenzelm@53079
  1843
  -- {*logarithm of @{term x} to base @{term a}*}
wenzelm@53079
  1844
  where "log a x = ln x / ln a"
hoelzl@51527
  1845
hoelzl@51527
  1846
hoelzl@51527
  1847
lemma tendsto_log [tendsto_intros]:
hoelzl@51527
  1848
  "\<lbrakk>(f ---> a) F; (g ---> b) F; 0 < a; a \<noteq> 1; 0 < b\<rbrakk> \<Longrightarrow> ((\<lambda>x. log (f x) (g x)) ---> log a b) F"
hoelzl@51527
  1849
  unfolding log_def by (intro tendsto_intros) auto
hoelzl@51527
  1850
hoelzl@51527
  1851
lemma continuous_log:
wenzelm@53079
  1852
  assumes "continuous F f"
wenzelm@53079
  1853
    and "continuous F g"
wenzelm@53079
  1854
    and "0 < f (Lim F (\<lambda>x. x))"
wenzelm@53079
  1855
    and "f (Lim F (\<lambda>x. x)) \<noteq> 1"
wenzelm@53079
  1856
    and "0 < g (Lim F (\<lambda>x. x))"
hoelzl@51527
  1857
  shows "continuous F (\<lambda>x. log (f x) (g x))"
hoelzl@51527
  1858
  using assms unfolding continuous_def by (rule tendsto_log)
hoelzl@51527
  1859
hoelzl@51527
  1860
lemma continuous_at_within_log[continuous_intros]:
wenzelm@53079
  1861
  assumes "continuous (at a within s) f"
wenzelm@53079
  1862
    and "continuous (at a within s) g"
wenzelm@53079
  1863
    and "0 < f a"
wenzelm@53079
  1864
    and "f a \<noteq> 1"
wenzelm@53079
  1865
    and "0 < g a"
hoelzl@51527
  1866
  shows "continuous (at a within s) (\<lambda>x. log (f x) (g x))"
hoelzl@51527
  1867
  using assms unfolding continuous_within by (rule tendsto_log)
hoelzl@51527
  1868
hoelzl@51527
  1869
lemma isCont_log[continuous_intros, simp]:
hoelzl@51527
  1870
  assumes "isCont f a" "isCont g a" "0 < f a" "f a \<noteq> 1" "0 < g a"
hoelzl@51527
  1871
  shows "isCont (\<lambda>x. log (f x) (g x)) a"
hoelzl@51527
  1872
  using assms unfolding continuous_at by (rule tendsto_log)
hoelzl@51527
  1873
hoelzl@56371
  1874
lemma continuous_on_log[continuous_intros]:
wenzelm@53079
  1875
  assumes "continuous_on s f" "continuous_on s g"
wenzelm@53079
  1876
    and "\<forall>x\<in>s. 0 < f x" "\<forall>x\<in>s. f x \<noteq> 1" "\<forall>x\<in>s. 0 < g x"
hoelzl@51527
  1877
  shows "continuous_on s (\<lambda>x. log (f x) (g x))"
hoelzl@51527
  1878
  using assms unfolding continuous_on_def by (fast intro: tendsto_log)
hoelzl@51527
  1879
hoelzl@51527
  1880
lemma powr_one_eq_one [simp]: "1 powr a = 1"
wenzelm@53079
  1881
  by (simp add: powr_def)
hoelzl@51527
  1882
hoelzl@51527
  1883
lemma powr_zero_eq_one [simp]: "x powr 0 = 1"
wenzelm@53079
  1884
  by (simp add: powr_def)
hoelzl@51527
  1885
hoelzl@51527
  1886
lemma powr_one_gt_zero_iff [simp]: "(x powr 1 = x) = (0 < x)"
wenzelm@53079
  1887
  by (simp add: powr_def)
hoelzl@51527
  1888
declare powr_one_gt_zero_iff [THEN iffD2, simp]
hoelzl@51527
  1889
wenzelm@53079
  1890
lemma powr_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x * y) powr a = (x powr a) * (y powr a)"
wenzelm@53079
  1891
  by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)
hoelzl@51527
  1892
hoelzl@51527
  1893
lemma powr_gt_zero [simp]: "0 < x powr a"
wenzelm@53079
  1894
  by (simp add: powr_def)
hoelzl@51527
  1895
hoelzl@51527
  1896
lemma powr_ge_pzero [simp]: "0 <= x powr y"
wenzelm@53079
  1897
  by (rule order_less_imp_le, rule powr_gt_zero)
hoelzl@51527
  1898
hoelzl@51527
  1899
lemma powr_not_zero [simp]: "x powr a \<noteq> 0"
wenzelm@53079
  1900
  by (simp add: powr_def)
wenzelm@53079
  1901
wenzelm@53079
  1902
lemma powr_divide: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
wenzelm@53079
  1903
  apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
wenzelm@53079
  1904
  apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
wenzelm@53079
  1905
  done
hoelzl@51527
  1906
hoelzl@51527
  1907
lemma powr_divide2: "x powr a / x powr b = x powr (a - b)"
hoelzl@51527
  1908
  apply (simp add: powr_def)
hoelzl@51527
  1909
  apply (subst exp_diff [THEN sym])
hoelzl@51527
  1910
  apply (simp add: left_diff_distrib)
wenzelm@53079
  1911
  done
hoelzl@51527
  1912
hoelzl@51527
  1913
lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
wenzelm@53079
  1914
  by (simp add: powr_def exp_add [symmetric] distrib_right)
wenzelm@53079
  1915
wenzelm@53079
  1916
lemma powr_mult_base: "0 < x \<Longrightarrow>x * x powr y = x powr (1 + y)"
wenzelm@53079
  1917
  using assms by (auto simp: powr_add)
hoelzl@51527
  1918
hoelzl@51527
  1919
lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
wenzelm@53079
  1920
  by (simp add: powr_def)
hoelzl@51527
  1921
hoelzl@51527
  1922
lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
haftmann@57512
  1923
  by (simp add: powr_powr mult.commute)
hoelzl@51527
  1924
hoelzl@51527
  1925
lemma powr_minus: "x powr (-a) = inverse (x powr a)"
wenzelm@53079
  1926
  by (simp add: powr_def exp_minus [symmetric])
hoelzl@51527
  1927
hoelzl@51527
  1928
lemma powr_minus_divide: "x powr (-a) = 1/(x powr a)"
wenzelm@53079
  1929
  by (simp add: divide_inverse powr_minus)
wenzelm@53079
  1930
immler@58984
  1931
lemma divide_powr_uminus: "a / b powr c = a * b powr (- c)"
immler@58984
  1932
  by (simp add: powr_minus_divide)
immler@58984
  1933
wenzelm@53079
  1934
lemma powr_less_mono: "a < b \<Longrightarrow> 1 < x \<Longrightarrow> x powr a < x powr b"
wenzelm@53079
  1935
  by (simp add: powr_def)
wenzelm@53079
  1936
wenzelm@53079
  1937
lemma powr_less_cancel: "x powr a < x powr b \<Longrightarrow> 1 < x \<Longrightarrow> a < b"
wenzelm@53079
  1938
  by (simp add: powr_def)
wenzelm@53079
  1939
wenzelm@53079
  1940
lemma powr_less_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a < x powr b) = (a < b)"
wenzelm@53079
  1941
  by (blast intro: powr_less_cancel powr_less_mono)
wenzelm@53079
  1942
wenzelm@53079
  1943
lemma powr_le_cancel_iff [simp]: "1 < x \<Longrightarrow> (x powr a \<le> x powr b) = (a \<le> b)"
wenzelm@53079
  1944
  by (simp add: linorder_not_less [symmetric])
hoelzl@51527
  1945
hoelzl@51527
  1946
lemma log_ln: "ln x = log (exp(1)) x"
wenzelm@53079
  1947
  by (simp add: log_def)
wenzelm@53079
  1948
wenzelm@53079
  1949
lemma DERIV_log:
wenzelm@53079
  1950
  assumes "x > 0"
wenzelm@53079
  1951
  shows "DERIV (\<lambda>y. log b y) x :> 1 / (ln b * x)"
hoelzl@51527
  1952
proof -
hoelzl@51527
  1953
  def lb \<equiv> "1 / ln b"
hoelzl@51527
  1954
  moreover have "DERIV (\<lambda>y. lb * ln y) x :> lb / x"
hoelzl@56381
  1955
    using `x > 0` by (auto intro!: derivative_eq_intros)
hoelzl@51527
  1956
  ultimately show ?thesis
hoelzl@51527
  1957
    by (simp add: log_def)
hoelzl@51527
  1958
qed
hoelzl@51527
  1959
hoelzl@56381
  1960
lemmas DERIV_log[THEN DERIV_chain2, derivative_intros]
hoelzl@51527
  1961
wenzelm@53079
  1962
lemma powr_log_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> a powr (log a x) = x"
wenzelm@53079
  1963
  by (simp add: powr_def log_def)
wenzelm@53079
  1964
wenzelm@53079
  1965
lemma log_powr_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a (a powr y) = y"
wenzelm@53079
  1966
  by (simp add: log_def powr_def)
wenzelm@53079
  1967
wenzelm@53079
  1968
lemma log_mult:
wenzelm@53079
  1969
  "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow>
wenzelm@53079
  1970
    log a (x * y) = log a x + log a y"
wenzelm@53079
  1971
  by (simp add: log_def ln_mult divide_inverse distrib_right)
wenzelm@53079
  1972
wenzelm@53079
  1973
lemma log_eq_div_ln_mult_log:
wenzelm@53079
  1974
  "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow>
wenzelm@53079
  1975
    log a x = (ln b/ln a) * log b x"
wenzelm@53079
  1976
  by (simp add: log_def divide_inverse)
hoelzl@51527
  1977
hoelzl@51527
  1978
text{*Base 10 logarithms*}
wenzelm@53079
  1979
lemma log_base_10_eq1: "0 < x \<Longrightarrow> log 10 x = (ln (exp 1) / ln 10) * ln x"
wenzelm@53079
  1980
  by (simp add: log_def)
wenzelm@53079
  1981
wenzelm@53079
  1982
lemma log_base_10_eq2: "0 < x \<Longrightarrow> log 10 x = (log 10 (exp 1)) * ln x"
wenzelm@53079
  1983
  by (simp add: log_def)
hoelzl@51527
  1984
hoelzl@51527
  1985
lemma log_one [simp]: "log a 1 = 0"
wenzelm@53079
  1986
  by (simp add: log_def)
hoelzl@51527
  1987
hoelzl@51527
  1988
lemma log_eq_one [simp]: "[| 0 < a; a \<noteq> 1 |] ==> log a a = 1"
wenzelm@53079
  1989
  by (simp add: log_def)
wenzelm@53079
  1990
wenzelm@53079
  1991
lemma log_inverse: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log a (inverse x) = - log a x"
wenzelm@53079
  1992
  apply (rule_tac a1 = "log a x" in add_left_cancel [THEN iffD1])
wenzelm@53079
  1993
  apply (simp add: log_mult [symmetric])
wenzelm@53079
  1994
  done
wenzelm@53079
  1995
wenzelm@53079
  1996
lemma log_divide: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a (x/y) = log a x - log a y"
wenzelm@53079
  1997
  by (simp add: log_mult divide_inverse log_inverse)
hoelzl@51527
  1998
immler@58984
  1999
lemma log_add_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log b x + y = log b (x * b powr y)"
immler@58984
  2000
  and add_log_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> y + log b x = log b (b powr y * x)"
immler@58984
  2001
  and log_minus_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log b x - y = log b (x * b powr -y)"
immler@58984
  2002
  and minus_log_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> y - log b x = log b (b powr y / x)"
immler@58984
  2003
  by (simp_all add: log_mult log_divide)
immler@58984
  2004
hoelzl@51527
  2005
lemma log_less_cancel_iff [simp]:
wenzelm@53079
  2006
  "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x < log a y \<longleftrightarrow> x < y"
wenzelm@53079
  2007
  apply safe
wenzelm@53079
  2008
  apply (rule_tac [2] powr_less_cancel)
wenzelm@53079
  2009
  apply (drule_tac a = "log a x" in powr_less_mono, auto)
wenzelm@53079
  2010
  done
wenzelm@53079
  2011
wenzelm@53079
  2012
lemma log_inj:
wenzelm@53079
  2013
  assumes "1 < b"
wenzelm@53079
  2014
  shows "inj_on (log b) {0 <..}"
hoelzl@51527
  2015
proof (rule inj_onI, simp)
wenzelm@53079
  2016
  fix x y
wenzelm@53079
  2017
  assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
hoelzl@51527
  2018
  show "x = y"
hoelzl@51527
  2019
  proof (cases rule: linorder_cases)
wenzelm@53079
  2020
    assume "x = y"
wenzelm@53079
  2021
    then show ?thesis by simp
wenzelm@53079
  2022
  next
hoelzl@51527
  2023
    assume "x < y" hence "log b x < log b y"
hoelzl@51527
  2024
      using log_less_cancel_iff[OF `1 < b`] pos by simp
wenzelm@53079
  2025
    then show ?thesis using * by simp
hoelzl@51527
  2026
  next
hoelzl@51527
  2027
    assume "y < x" hence "log b y < log b x"
hoelzl@51527
  2028
      using log_less_cancel_iff[OF `1 < b`] pos by simp
wenzelm@53079
  2029
    then show ?thesis using * by simp
wenzelm@53079
  2030
  qed
hoelzl@51527
  2031
qed
hoelzl@51527
  2032
hoelzl@51527
  2033
lemma log_le_cancel_iff [simp]:
wenzelm@53079
  2034
  "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (log a x \<le> log a y) = (x \<le> y)"
wenzelm@53079
  2035
  by (simp add: linorder_not_less [symmetric])
hoelzl@51527
  2036
hoelzl@51527
  2037
lemma zero_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < log a x \<longleftrightarrow> 1 < x"
hoelzl@51527
  2038
  using log_less_cancel_iff[of a 1 x] by simp
hoelzl@51527
  2039
hoelzl@51527
  2040
lemma zero_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 \<le> log a x \<longleftrightarrow> 1 \<le> x"
hoelzl@51527
  2041
  using log_le_cancel_iff[of a 1 x] by simp
hoelzl@51527
  2042
hoelzl@51527
  2043
lemma log_less_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 0 \<longleftrightarrow> x < 1"
hoelzl@51527
  2044
  using log_less_cancel_iff[of a x 1] by simp
hoelzl@51527
  2045
hoelzl@51527
  2046
lemma log_le_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 0 \<longleftrightarrow> x \<le> 1"
hoelzl@51527
  2047
  using log_le_cancel_iff[of a x 1] by simp
hoelzl@51527
  2048
hoelzl@51527
  2049
lemma one_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 < log a x \<longleftrightarrow> a < x"
hoelzl@51527
  2050
  using log_less_cancel_iff[of a a x] by simp
hoelzl@51527
  2051
hoelzl@51527
  2052
lemma one_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> log a x \<longleftrightarrow> a \<le> x"
hoelzl@51527
  2053
  using log_le_cancel_iff[of a a x] by simp
hoelzl@51527
  2054
hoelzl@51527
  2055
lemma log_less_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 1 \<longleftrightarrow> x < a"
hoelzl@51527
  2056
  using log_less_cancel_iff[of a x a] by simp
hoelzl@51527
  2057
hoelzl@51527
  2058
lemma log_le_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 1 \<longleftrightarrow> x \<le> a"
hoelzl@51527
  2059
  using log_le_cancel_iff[of a x a] by simp
hoelzl@51527
  2060
immler@58984
  2061
lemma le_log_iff:
immler@58984
  2062
  assumes "1 < b" "x > 0"
immler@58984
  2063
  shows "y \<le> log b x \<longleftrightarrow> b powr y \<le> x"
immler@58984
  2064
  by (metis assms(1) assms(2) dual_order.strict_trans powr_le_cancel_iff powr_log_cancel
immler@58984
  2065
    powr_one_eq_one powr_one_gt_zero_iff)
immler@58984
  2066
immler@58984
  2067
lemma less_log_iff:
immler@58984
  2068
  assumes "1 < b" "x > 0"
immler@58984
  2069
  shows "y < log b x \<longleftrightarrow> b powr y < x"
immler@58984
  2070
  by (metis assms(1) assms(2) dual_order.strict_trans less_irrefl powr_less_cancel_iff
immler@58984
  2071
    powr_log_cancel zero_less_one)
immler@58984
  2072
immler@58984
  2073
lemma
immler@58984
  2074
  assumes "1 < b" "x > 0"
immler@58984
  2075
  shows log_less_iff: "log b x < y \<longleftrightarrow> x < b powr y"
immler@58984
  2076
    and log_le_iff: "log b x \<le> y \<longleftrightarrow> x \<le> b powr y"
immler@58984
  2077
  using le_log_iff[OF assms, of y] less_log_iff[OF assms, of y]
immler@58984
  2078
  by auto
immler@58984
  2079
immler@58984
  2080
lemmas powr_le_iff = le_log_iff[symmetric]
immler@58984
  2081
  and powr_less_iff = le_log_iff[symmetric]
immler@58984
  2082
  and less_powr_iff = log_less_iff[symmetric]
immler@58984
  2083
  and le_powr_iff = log_le_iff[symmetric]
immler@58984
  2084
immler@58984
  2085
lemma
immler@58984
  2086
  floor_log_eq_powr_iff: "x > 0 \<Longrightarrow> b > 1 \<Longrightarrow> \<lfloor>log b x\<rfloor> = k \<longleftrightarrow> b powr k \<le> x \<and> x < b powr (k + 1)"
immler@58984
  2087
  by (auto simp add: floor_eq_iff powr_le_iff less_powr_iff)
immler@58984
  2088
hoelzl@51527
  2089
lemma powr_realpow: "0 < x ==> x powr (real n) = x^n"
wenzelm@53079
  2090
  apply (induct n)
wenzelm@53079
  2091
  apply simp
hoelzl@51527
  2092
  apply (subgoal_tac "real(Suc n) = real n + 1")
hoelzl@51527
  2093
  apply (erule ssubst)
hoelzl@51527
  2094
  apply (subst powr_add, simp, simp)
wenzelm@53079
  2095
  done
hoelzl@51527
  2096
haftmann@54489
  2097
lemma powr_realpow_numeral: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x ^ (numeral n)"
haftmann@54489
  2098
  unfolding real_of_nat_numeral [symmetric] by (rule powr_realpow)
noschinl@52139
  2099
nipkow@57180
  2100
lemma powr2_sqrt[simp]: "0 < x \<Longrightarrow> sqrt x powr 2 = x"
nipkow@57180
  2101
by(simp add: powr_realpow_numeral)
nipkow@57180
  2102
hoelzl@51527
  2103
lemma powr_realpow2: "0 <= x ==> 0 < n ==> x^n = (if (x = 0) then 0 else x powr (real n))"
hoelzl@51527
  2104
  apply (case_tac "x = 0", simp, simp)
hoelzl@51527
  2105
  apply (rule powr_realpow [THEN sym], simp)
wenzelm@53079
  2106
  done
hoelzl@51527
  2107
hoelzl@51527
  2108
lemma powr_int:
hoelzl@51527
  2109
  assumes "x > 0"
hoelzl@51527
  2110
  shows "x powr i = (if i \<ge> 0 then x ^ nat i else 1 / x ^ nat (-i))"
wenzelm@53079
  2111
proof (cases "i < 0")
wenzelm@53079
  2112
  case True
hoelzl@51527
  2113
  have r: "x powr i = 1 / x powr (-i)" by (simp add: powr_minus field_simps)
hoelzl@51527
  2114
  show ?thesis using `i < 0` `x > 0` by (simp add: r field_simps powr_realpow[symmetric])
wenzelm@53079
  2115
next
wenzelm@53079
  2116
  case False
wenzelm@53079
  2117
  then show ?thesis by (simp add: assms powr_realpow[symmetric])
wenzelm@53079
  2118
qed
hoelzl@51527
  2119
immler@58981
  2120
lemma compute_powr[code]:
immler@58981
  2121
  fixes i::real
immler@58981
  2122
  shows "b powr i =
immler@58981
  2123
    (if b \<le> 0 then Code.abort (STR ''op powr with nonpositive base'') (\<lambda>_. b powr i)
nipkow@59587
  2124
    else if floor i = i then (if 0 \<le> i then b ^ nat(floor i) else 1 / b ^ nat(floor (- i)))
immler@58981
  2125
    else Code.abort (STR ''op powr with non-integer exponent'') (\<lambda>_. b powr i))"
nipkow@59587
  2126
  by (auto simp: powr_int)
immler@58981
  2127
haftmann@54489
  2128
lemma powr_one: "0 < x \<Longrightarrow> x powr 1 = x"
haftmann@54489
  2129
  using powr_realpow [of x 1] by simp
haftmann@54489
  2130
haftmann@54489
  2131
lemma powr_numeral: "0 < x \<Longrightarrow> x powr numeral n = x ^ numeral n"
haftmann@54489
  2132
  by (fact powr_realpow_numeral)
haftmann@54489
  2133
haftmann@54489
  2134
lemma powr_neg_one: "0 < x \<Longrightarrow> x powr - 1 = 1 / x"
haftmann@54489
  2135
  using powr_int [of x "- 1"] by simp
haftmann@54489
  2136
haftmann@54489
  2137
lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr - numeral n = 1 / x ^ numeral n"
haftmann@54489
  2138
  using powr_int [of x "- numeral n"] by simp
hoelzl@51527
  2139
wenzelm@53079
  2140
lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
hoelzl@51527
  2141
  by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
hoelzl@51527
  2142
hoelzl@56483
  2143
lemma ln_powr: "ln (x powr y) = y * ln x"
hoelzl@56483
  2144
  by (simp add: powr_def)
hoelzl@56483
  2145
nipkow@56952
  2146
lemma ln_root: "\<lbrakk> n > 0; b > 0 \<rbrakk> \<Longrightarrow> ln (root n b) =  ln b / n"
nipkow@56952
  2147
by(simp add: root_powr_inverse ln_powr)
nipkow@56952
  2148
hoelzl@57275
  2149
lemma ln_sqrt: "0 < x \<Longrightarrow> ln (sqrt x) = ln x / 2"
haftmann@57512
  2150
  by (simp add: ln_powr powr_numeral ln_powr[symmetric] mult.commute)
hoelzl@57275
  2151
nipkow@56952
  2152
lemma log_root: "\<lbrakk> n > 0; a > 0 \<rbrakk> \<Longrightarrow> log b (root n a) =  log b a / n"
nipkow@56952
  2153
by(simp add: log_def ln_root)
nipkow@56952
  2154
hoelzl@56483
  2155
lemma log_powr: "log b (x powr y) = y * log b x"
hoelzl@56483
  2156
  by (simp add: log_def ln_powr)
hoelzl@56483
  2157
lp15@59730
  2158
lemma log_nat_power: "0 < x \<Longrightarrow> log b (x^n) = real n * log b x"
hoelzl@56483
  2159
  by (simp add: log_powr powr_realpow [symmetric])
hoelzl@56483
  2160
hoelzl@56483
  2161
lemma log_base_change: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log b x = log a x / log a b"
hoelzl@56483
  2162
  by (simp add: log_def)
hoelzl@56483
  2163
hoelzl@56483
  2164
lemma log_base_pow: "0 < a \<Longrightarrow> log (a ^ n) x = log a x / n"
hoelzl@56483
  2165
  by (simp add: log_def ln_realpow)
hoelzl@56483
  2166
hoelzl@56483
  2167
lemma log_base_powr: "log (a powr b) x = log a x / b"
hoelzl@56483
  2168
  by (simp add: log_def ln_powr)
hoelzl@51527
  2169
nipkow@56952
  2170
lemma log_base_root: "\<lbrakk> n > 0; b > 0 \<rbrakk> \<Longrightarrow> log (root n b) x = n * (log b x)"
nipkow@56952
  2171
by(simp add: log_def ln_root)
nipkow@56952
  2172
hoelzl@51527
  2173
lemma ln_bound: "1 <= x ==> ln x <= x"
hoelzl@51527
  2174
  apply (subgoal_tac "ln(1 + (x - 1)) <= x - 1")
hoelzl@51527
  2175
  apply simp
hoelzl@51527
  2176
  apply (rule ln_add_one_self_le_self, simp)
wenzelm@53079
  2177
  done
hoelzl@51527
  2178
hoelzl@51527
  2179
lemma powr_mono: "a <= b ==> 1 <= x ==> x powr a <= x powr b"
wenzelm@53079
  2180
  apply (cases "x = 1", simp)
wenzelm@53079
  2181
  apply (cases "a = b", simp)
hoelzl@51527
  2182
  apply (rule order_less_imp_le)
hoelzl@51527
  2183
  apply (rule powr_less_mono, auto)
wenzelm@53079
  2184
  done
hoelzl@51527
  2185
hoelzl@51527
  2186
lemma ge_one_powr_ge_zero: "1 <= x ==> 0 <= a ==> 1 <= x powr a"
hoelzl@51527
  2187
  apply (subst powr_zero_eq_one [THEN sym])
hoelzl@51527
  2188
  apply (rule powr_mono, assumption+)
wenzelm@53079
  2189
  done
wenzelm@53079
  2190
wenzelm@53079
  2191
lemma powr_less_mono2: "0 < a ==> 0 < x ==> x < y ==> x powr a < y powr a"
hoelzl@51527
  2192
  apply (unfold powr_def)
hoelzl@51527
  2193
  apply (rule exp_less_mono)
hoelzl@51527
  2194
  apply (rule mult_strict_left_mono)
hoelzl@51527
  2195
  apply (subst ln_less_cancel_iff, assumption)
hoelzl@51527
  2196
  apply (rule order_less_trans)
hoelzl@51527
  2197
  prefer 2
hoelzl@51527
  2198
  apply assumption+
wenzelm@53079
  2199
  done
wenzelm@53079
  2200
wenzelm@53079
  2201
lemma powr_less_mono2_neg: "a < 0 ==> 0 < x ==> x < y ==> y powr a < x powr a"
hoelzl@51527
  2202
  apply (unfold powr_def)
hoelzl@51527
  2203
  apply (rule exp_less_mono)
hoelzl@51527
  2204
  apply (rule mult_strict_left_mono_neg)
hoelzl@51527
  2205
  apply (subst ln_less_cancel_iff)
hoelzl@51527
  2206
  apply assumption
hoelzl@51527
  2207
  apply (rule order_less_trans)
hoelzl@51527
  2208
  prefer 2
hoelzl@51527
  2209
  apply assumption+
wenzelm@53079
  2210
  done
hoelzl@51527
  2211
hoelzl@51527
  2212
lemma powr_mono2: "0 <= a ==> 0 < x ==> x <= y ==> x powr a <= y powr a"
hoelzl@51527
  2213
  apply (case_tac "a = 0", simp)
hoelzl@51527
  2214
  apply (case_tac "x = y", simp)
paulson@54575
  2215
  apply (metis less_eq_real_def powr_less_mono2)
wenzelm@53079
  2216
  done
wenzelm@53079
  2217
wenzelm@53079
  2218
lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
hoelzl@51527
  2219
  unfolding powr_def exp_inj_iff by simp
hoelzl@51527
  2220
hoelzl@51527
  2221
lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
lp15@59669
  2222
  by (metis less_eq_real_def ln_less_self mult_imp_le_div_pos ln_powr mult.commute
paulson@54575
  2223
            order.strict_trans2 powr_gt_zero zero_less_one)
hoelzl@51527
  2224
hoelzl@51527
  2225
lemma ln_powr_bound2:
hoelzl@51527
  2226
  assumes "1 < x" and "0 < a"
hoelzl@51527
  2227
  shows "(ln x) powr a <= (a powr a) * x"
hoelzl@51527
  2228
proof -
hoelzl@51527
  2229
  from assms have "ln x <= (x powr (1 / a)) / (1 / a)"
paulson@54575
  2230
    by (metis less_eq_real_def ln_powr_bound zero_less_divide_1_iff)
hoelzl@51527
  2231
  also have "... = a * (x powr (1 / a))"
hoelzl@51527
  2232
    by simp
hoelzl@51527
  2233
  finally have "(ln x) powr a <= (a * (x powr (1 / a))) powr a"
paulson@54575
  2234
    by (metis assms less_imp_le ln_gt_zero powr_mono2)
hoelzl@51527
  2235
  also have "... = (a powr a) * ((x powr (1 / a)) powr a)"
paulson@54575
  2236
    by (metis assms(2) powr_mult powr_gt_zero)
hoelzl@51527
  2237
  also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
hoelzl@51527
  2238
    by (rule powr_powr)
paulson@54575
  2239
  also have "... = x" using assms
paulson@54575
  2240
    by auto
hoelzl@51527
  2241
  finally show ?thesis .
hoelzl@51527
  2242
qed
hoelzl@51527
  2243
hoelzl@51527
  2244
lemma tendsto_powr [tendsto_intros]:
hoelzl@57275
  2245
  "\<lbrakk>(f ---> a) F; (g ---> b) F; a \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x powr g x) ---> a powr b) F"
hoelzl@51527
  2246
  unfolding powr_def by (intro tendsto_intros)
hoelzl@51527
  2247
hoelzl@51527
  2248
lemma continuous_powr:
wenzelm@53079
  2249
  assumes "continuous F f"
wenzelm@53079
  2250
    and "continuous F g"
hoelzl@57275
  2251
    and "f (Lim F (\<lambda>x. x)) \<noteq> 0"
hoelzl@51527
  2252
  shows "continuous F (\<lambda>x. (f x) powr (g x))"
hoelzl@51527
  2253
  using assms unfolding continuous_def by (rule tendsto_powr)
hoelzl@51527
  2254
hoelzl@51527
  2255
lemma continuous_at_within_powr[continuous_intros]:
wenzelm@53079
  2256
  assumes "continuous (at a within s) f"
wenzelm@53079
  2257
    and "continuous (at a within s) g"
hoelzl@57275
  2258
    and "f a \<noteq> 0"
hoelzl@51527
  2259
  shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
hoelzl@51527
  2260
  using assms unfolding continuous_within by (rule tendsto_powr)
hoelzl@51527
  2261
hoelzl@51527
  2262
lemma isCont_powr[continuous_intros, simp]:
hoelzl@57275
  2263
  assumes "isCont f a" "isCont g a" "f a \<noteq> 0"
hoelzl@51527
  2264
  shows "isCont (\<lambda>x. (f x) powr g x) a"
hoelzl@51527
  2265
  using assms unfolding continuous_at by (rule tendsto_powr)
hoelzl@51527
  2266
hoelzl@56371
  2267
lemma continuous_on_powr[continuous_intros]:
hoelzl@57275
  2268
  assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. f x \<noteq> 0"
hoelzl@51527
  2269
  shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
hoelzl@51527
  2270
  using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
hoelzl@51527
  2271
hoelzl@51527
  2272
(* FIXME: generalize by replacing d by with g x and g ---> d? *)
hoelzl@51527
  2273
lemma tendsto_zero_powrI:
hoelzl@51527
  2274
  assumes "eventually (\<lambda>x. 0 < f x ) F" and "(f ---> 0) F"
wenzelm@53079
  2275
    and "0 < d"
hoelzl@51527
  2276
  shows "((\<lambda>x. f x powr d) ---> 0) F"
hoelzl@51527
  2277
proof (rule tendstoI)
hoelzl@51527
  2278
  fix e :: real assume "0 < e"
hoelzl@51527
  2279
  def Z \<equiv> "e powr (1 / d)"
hoelzl@51527
  2280
  with `0 < e` have "0 < Z" by simp
hoelzl@51527
  2281
  with assms have "eventually (\<lambda>x. 0 < f x \<and> dist (f x) 0 < Z) F"
hoelzl@51527
  2282
    by (intro eventually_conj tendstoD)
hoelzl@51527
  2283
  moreover
hoelzl@51527
  2284
  from assms have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> x powr d < Z powr d"
hoelzl@51527
  2285
    by (intro powr_less_mono2) (auto simp: dist_real_def)
hoelzl@51527
  2286
  with assms `0 < e` have "\<And>x. 0 < x \<and> dist x 0 < Z \<Longrightarrow> dist (x powr d) 0 < e"
hoelzl@51527
  2287
    unfolding dist_real_def Z_def by (auto simp: powr_powr)
hoelzl@51527
  2288
  ultimately
hoelzl@51527
  2289
  show "eventually (\<lambda>x. dist (f x powr d) 0 < e) F" by (rule eventually_elim1)
hoelzl@51527
  2290
qed
hoelzl@51527
  2291
hoelzl@51527
  2292
lemma tendsto_neg_powr:
wenzelm@53079
  2293
  assumes "s < 0"
wenzelm@53079
  2294
    and "LIM x F. f x :> at_top"
hoelzl@51527
  2295
  shows "((\<lambda>x. f x powr s) ---> 0) F"
hoelzl@51527
  2296
proof (rule tendstoI)
hoelzl@51527
  2297
  fix e :: real assume "0 < e"
hoelzl@51527
  2298
  def Z \<equiv> "e powr (1 / s)"
hoelzl@51527
  2299
  from assms have "eventually (\<lambda>x. Z < f x) F"
hoelzl@51527
  2300
    by (simp add: filterlim_at_top_dense)
hoelzl@51527
  2301
  moreover
hoelzl@51527
  2302
  from assms have "\<And>x. Z < x \<Longrightarrow> x powr s < Z powr s"
hoelzl@51527
  2303
    by (auto simp: Z_def intro!: powr_less_mono2_neg)
hoelzl@51527
  2304
  with assms `0 < e` have "\<And>x. Z < x \<Longrightarrow> dist (x powr s) 0 < e"
hoelzl@51527
  2305
    by (simp add: powr_powr Z_def dist_real_def)
hoelzl@51527
  2306
  ultimately
hoelzl@51527
  2307
  show "eventually (\<lambda>x. dist (f x powr s) 0 < e) F" by (rule eventually_elim1)
hoelzl@51527
  2308
qed
hoelzl@51527
  2309
hoelzl@57275
  2310
(* it is funny that this isn't in the library! It could go in Transcendental *)
hoelzl@57275
  2311
lemma tendsto_exp_limit_at_right:
hoelzl@57275
  2312
  fixes x :: real
hoelzl@57275
  2313
  shows "((\<lambda>y. (1 + x * y) powr (1 / y)) ---> exp x) (at_right 0)"
hoelzl@57275
  2314
proof cases
hoelzl@57275
  2315
  assume "x \<noteq> 0"
hoelzl@57275
  2316
hoelzl@57275
  2317
  have "((\<lambda>y. ln (1 + x * y)::real) has_real_derivative 1 * x) (at 0)"
hoelzl@57275
  2318
    by (auto intro!: derivative_eq_intros)
hoelzl@57275
  2319
  then have "((\<lambda>y. ln (1 + x * y) / y) ---> x) (at 0)"
lp15@59669
  2320
    by (auto simp add: has_field_derivative_def field_has_derivative_at)
hoelzl@57275
  2321
  then have *: "((\<lambda>y. exp (ln (1 + x * y) / y)) ---> exp x) (at 0)"
hoelzl@57275
  2322
    by (rule tendsto_intros)
hoelzl@57275
  2323
  then show ?thesis
hoelzl@57275
  2324
  proof (rule filterlim_mono_eventually)
hoelzl@57275
  2325
    show "eventually (\<lambda>xa. exp (ln (1 + x * xa) / xa) = (1 + x * xa) powr (1 / xa)) (at_right 0)"
hoelzl@57275
  2326
      unfolding eventually_at_right[OF zero_less_one]
hoelzl@57275
  2327
      using `x \<noteq> 0` by (intro exI[of _ "1 / \<bar>x\<bar>"]) (auto simp: field_simps powr_def)
hoelzl@57275
  2328
  qed (simp_all add: at_eq_sup_left_right)
hoelzl@58729
  2329
qed simp
hoelzl@57275
  2330
hoelzl@57275
  2331
lemma tendsto_exp_limit_at_top:
hoelzl@57275
  2332
  fixes x :: real
hoelzl@57275
  2333
  shows "((\<lambda>y. (1 + x / y) powr y) ---> exp x) at_top"
hoelzl@57275
  2334
  apply (subst filterlim_at_top_to_right)
hoelzl@57275
  2335
  apply (simp add: inverse_eq_divide)
hoelzl@57275
  2336
  apply (rule tendsto_exp_limit_at_right)
hoelzl@57275
  2337
  done
hoelzl@57275
  2338
hoelzl@57275
  2339
lemma tendsto_exp_limit_sequentially:
hoelzl@57275
  2340
  fixes x :: real
hoelzl@57275
  2341
  shows "(\<lambda>n. (1 + x / n) ^ n) ----> exp x"
hoelzl@57275
  2342
proof (rule filterlim_mono_eventually)
hoelzl@57275
  2343
  from reals_Archimedean2 [of "abs x"] obtain n :: nat where *: "real n > abs x" ..
hoelzl@57275
  2344
  hence "eventually (\<lambda>n :: nat. 0 < 1 + x / real n) at_top"
hoelzl@57275
  2345
    apply (intro eventually_sequentiallyI [of n])
hoelzl@57275
  2346
    apply (case_tac "x \<ge> 0")
hoelzl@57275
  2347
    apply (rule add_pos_nonneg, auto intro: divide_nonneg_nonneg)
hoelzl@57275
  2348
    apply (subgoal_tac "x / real xa > -1")
hoelzl@57275
  2349
    apply (auto simp add: field_simps)
hoelzl@57275
  2350
    done
hoelzl@57275
  2351
  then show "eventually (\<lambda>n. (1 + x / n) powr n = (1 + x / n) ^ n) at_top"
hoelzl@57275
  2352
    by (rule eventually_elim1) (erule powr_realpow)
hoelzl@57275
  2353
  show "(\<lambda>n. (1 + x / real n) powr real n) ----> exp x"
hoelzl@57275
  2354
    by (rule filterlim_compose [OF tendsto_exp_limit_at_top filterlim_real_sequentially])
hoelzl@57275
  2355
qed auto
hoelzl@57275
  2356
huffman@29164
  2357
subsection {* Sine and Cosine *}
huffman@29164
  2358
huffman@44308
  2359
definition sin_coeff :: "nat \<Rightarrow> real" where
lp15@59730
  2360
  "sin_coeff = (\<lambda>n. if even n then 0 else (- 1) ^ ((n - Suc 0) div 2) / (fact n))"
huffman@31271
  2361
huffman@44308
  2362
definition cos_coeff :: "nat \<Rightarrow> real" where
lp15@59730
  2363
  "cos_coeff = (\<lambda>n. if even n then ((- 1) ^ (n div 2)) / (fact n) else 0)"
huffman@31271
  2364
lp15@59658
  2365
definition sin :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
lp15@59658
  2366
  where "sin = (\<lambda>x. \<Sum>n. sin_coeff n *\<^sub>R x^n)"
lp15@59658
  2367
lp15@59658
  2368
definition cos :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
lp15@59658
  2369
  where "cos = (\<lambda>x. \<Sum>n. cos_coeff n *\<^sub>R x^n)"
huffman@31271
  2370
huffman@44319
  2371
lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
huffman@44319
  2372
  unfolding sin_coeff_def by simp
huffman@44319
  2373
huffman@44319
  2374
lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
huffman@44319
  2375
  unfolding cos_coeff_def by simp
huffman@44319
  2376
huffman@44319
  2377
lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
huffman@44319
  2378
  unfolding cos_coeff_def sin_coeff_def
huffman@44319
  2379
  by (simp del: mult_Suc)
huffman@44319
  2380
huffman@44319
  2381
lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
huffman@44319
  2382
  unfolding cos_coeff_def sin_coeff_def
haftmann@58709
  2383
  by (simp del: mult_Suc) (auto elim: oddE)
huffman@44319
  2384
lp15@59669
  2385
lemma summable_norm_sin:
lp15@59658
  2386
  fixes x :: "'a::{real_normed_algebra_1,banach}"
lp15@59658
  2387
  shows "summable (\<lambda>n. norm (sin_coeff n *\<^sub>R x^n))"
lp15@59669
  2388
  unfolding sin_coeff_def
lp15@59658
  2389
  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
lp15@59658
  2390
  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
wenzelm@53079
  2391
  done
huffman@29164
  2392
lp15@59669
  2393
lemma summable_norm_cos:
lp15@59658
  2394
  fixes x :: "'a::{real_normed_algebra_1,banach}"
lp15@59730
  2395
  shows "summable (\<lambda>n. norm (cos_coeff n *\<^sub>R x^n))"
wenzelm@53079
  2396
  unfolding cos_coeff_def
lp15@59658
  2397
  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
lp15@59658
  2398
  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
wenzelm@53079
  2399
  done
huffman@29164
  2400
lp15@59658
  2401
lemma sin_converges: "(\<lambda>n. sin_coeff n *\<^sub>R x^n) sums sin(x)"
lp15@59658
  2402
unfolding sin_def
lp15@59658
  2403
  by (metis (full_types) summable_norm_cancel summable_norm_sin summable_sums)
lp15@59658
  2404
lp15@59658
  2405
lemma cos_converges: "(\<lambda>n. cos_coeff n *\<^sub>R x^n) sums cos(x)"
lp15@59658
  2406
unfolding cos_def
lp15@59658
  2407
  by (metis (full_types) summable_norm_cancel summable_norm_cos summable_sums)
lp15@59658
  2408
lp15@59658
  2409
lemma sin_of_real:
lp15@59658
  2410
  fixes x::real
lp15@59658
  2411
  shows "sin (of_real x) = of_real (sin x)"
lp15@59658
  2412
proof -
lp15@59658
  2413
  have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R  x^n)) = (\<lambda>n. sin_coeff n *\<^sub>R  (of_real x)^n)"
lp15@59658
  2414
  proof
lp15@59658
  2415
    fix n
lp15@59730
  2416
    show "of_real (sin_coeff n *\<^sub>R  x^n) = sin_coeff n *\<^sub>R of_real x^n"
lp15@59658
  2417
      by (simp add: scaleR_conv_of_real)
lp15@59658
  2418
  qed
lp15@59658
  2419
  also have "... sums (sin (of_real x))"
lp15@59658
  2420
    by (rule sin_converges)
lp15@59658
  2421
  finally have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R x^n)) sums (sin (of_real x))" .
lp15@59658
  2422
  then show ?thesis
lp15@59669
  2423
    using sums_unique2 sums_of_real [OF sin_converges]
lp15@59658
  2424
    by blast
lp15@59658
  2425
qed
lp15@59658
  2426
lp15@59658
  2427
lemma cos_of_real:
lp15@59658
  2428
  fixes x::real
lp15@59658
  2429
  shows "cos (of_real x) = of_real (cos x)"
lp15@59658
  2430
proof -
lp15@59658
  2431
  have "(\<lambda>n. of_real (cos_coeff n *\<^sub>R  x^n)) = (\<lambda>n. cos_coeff n *\<^sub>R  (of_real x)^n)"
lp15@59658
  2432
  proof
lp15@59658
  2433
    fix n
lp15@59730
  2434
    show "of_real (cos_coeff n *\<^sub>R  x^n) = cos_coeff n *\<^sub>R of_real x^n"
lp15@59658
  2435
      by (simp add: scaleR_conv_of_real)
lp15@59658
  2436
  qed
lp15@59658
  2437
  also have "... sums (cos (of_real x))"
lp15@59658
  2438
    by (rule cos_converges)
lp15@59658
  2439
  finally have "(\<lambda>n. of_real (cos_coeff n *\<^sub>R x^n)) sums (cos (of_real x))" .
lp15@59658
  2440
  then show ?thesis
lp15@59669
  2441
    using sums_unique2 sums_of_real [OF cos_converges]
lp15@59658
  2442
    by blast
lp15@59658
  2443
qed
huffman@29164
  2444
huffman@44319
  2445
lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
huffman@44319
  2446
  by (simp add: diffs_def sin_coeff_Suc real_of_nat_def del: of_nat_Suc)
huffman@44319
  2447
huffman@44319
  2448
lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
hoelzl@56479
  2449
  by (simp add: diffs_def cos_coeff_Suc real_of_nat_def del: of_nat_Suc)
huffman@29164
  2450
huffman@29164
  2451
text{*Now at last we can get the derivatives of exp, sin and cos*}
huffman@29164
  2452
lp15@59658
  2453
lemma DERIV_sin [simp]:
lp15@59658
  2454
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59658
  2455
  shows "DERIV sin x :> cos(x)"
lp15@59658
  2456
  unfolding sin_def cos_def scaleR_conv_of_real
lp15@59658
  2457
  apply (rule DERIV_cong)
lp15@59658
  2458
  apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
lp15@59669
  2459
  apply (simp_all add: norm_less_p1 diffs_of_real diffs_sin_coeff diffs_cos_coeff
lp15@59658
  2460
              summable_minus_iff scaleR_conv_of_real [symmetric]
lp15@59658
  2461
              summable_norm_sin [THEN summable_norm_cancel]
lp15@59658
  2462
              summable_norm_cos [THEN summable_norm_cancel])
huffman@44319
  2463
  done
lp15@59669
  2464
hoelzl@56381
  2465
declare DERIV_sin[THEN DERIV_chain2, derivative_intros]
hoelzl@51527
  2466
lp15@59669
  2467
lemma DERIV_cos [simp]:
lp15@59658
  2468
  fixes x :: "'a::{real_normed_field,banach}"
lp15@59658
  2469
  shows "DERIV cos x :> -sin(x)"
lp15@59658
  2470
  unfolding sin_def cos_def scaleR_conv_of_real
lp15@59658
  2471
  apply (rule DERIV_cong)