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