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