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