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