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