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