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