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