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