src/HOL/Analysis/Harmonic_Numbers.thy
author nipkow
Mon Oct 17 11:46:22 2016 +0200 (2016-10-17)
changeset 64267 b9a1486e79be
parent 63721 492bb53c3420
child 65109 a79c1080f1e9
permissions -rw-r--r--
setsum -> sum
hoelzl@63627
     1
(*  Title:    HOL/Analysis/Harmonic_Numbers.thy
hoelzl@62055
     2
    Author:   Manuel Eberl, TU M√ľnchen
eberlm@62049
     3
*)
hoelzl@62055
     4
hoelzl@62055
     5
section \<open>Harmonic Numbers\<close>
hoelzl@62055
     6
eberlm@62049
     7
theory Harmonic_Numbers
hoelzl@63594
     8
imports
eberlm@62049
     9
  Complex_Transcendental
hoelzl@63594
    10
  Summation_Tests
eberlm@62049
    11
  Integral_Test
eberlm@62049
    12
begin
eberlm@62049
    13
hoelzl@62055
    14
text \<open>
wenzelm@62174
    15
  The definition of the Harmonic Numbers and the Euler-Mascheroni constant.
hoelzl@63594
    16
  Also provides a reasonably accurate approximation of @{term "ln 2 :: real"}
wenzelm@62174
    17
  and the Euler-Mascheroni constant.
hoelzl@62055
    18
\<close>
hoelzl@62055
    19
eberlm@62049
    20
lemma ln_2_less_1: "ln 2 < (1::real)"
eberlm@62049
    21
proof -
eberlm@62049
    22
  have "2 < 5/(2::real)" by simp
eberlm@62049
    23
  also have "5/2 \<le> exp (1::real)" using exp_lower_taylor_quadratic[of 1, simplified] by simp
eberlm@62049
    24
  finally have "exp (ln 2) < exp (1::real)" by simp
eberlm@62049
    25
  thus "ln 2 < (1::real)" by (subst (asm) exp_less_cancel_iff) simp
eberlm@62049
    26
qed
eberlm@62049
    27
nipkow@64267
    28
lemma sum_Suc_diff':
eberlm@62049
    29
  fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
eberlm@62049
    30
  assumes "m \<le> n"
eberlm@62085
    31
  shows "(\<Sum>i = m..<n. f (Suc i) - f i) = f n - f m"
eberlm@62049
    32
using assms by (induct n) (auto simp: le_Suc_eq)
eberlm@62049
    33
eberlm@62049
    34
eberlm@62049
    35
subsection \<open>The Harmonic numbers\<close>
eberlm@62049
    36
eberlm@62049
    37
definition harm :: "nat \<Rightarrow> 'a :: real_normed_field" where
eberlm@62049
    38
  "harm n = (\<Sum>k=1..n. inverse (of_nat k))"
eberlm@62049
    39
eberlm@62049
    40
lemma harm_altdef: "harm n = (\<Sum>k<n. inverse (of_nat (Suc k)))"
eberlm@62049
    41
  unfolding harm_def by (induction n) simp_all
eberlm@62049
    42
eberlm@62049
    43
lemma harm_Suc: "harm (Suc n) = harm n + inverse (of_nat (Suc n))"
eberlm@62049
    44
  by (simp add: harm_def)
eberlm@62049
    45
eberlm@62049
    46
lemma harm_nonneg: "harm n \<ge> (0 :: 'a :: {real_normed_field,linordered_field})"
nipkow@64267
    47
  unfolding harm_def by (intro sum_nonneg) simp_all
eberlm@62049
    48
eberlm@62065
    49
lemma harm_pos: "n > 0 \<Longrightarrow> harm n > (0 :: 'a :: {real_normed_field,linordered_field})"
nipkow@64267
    50
  unfolding harm_def by (intro sum_pos) simp_all
eberlm@62065
    51
eberlm@62049
    52
lemma of_real_harm: "of_real (harm n) = harm n"
eberlm@62049
    53
  unfolding harm_def by simp
hoelzl@63594
    54
eberlm@62049
    55
lemma norm_harm: "norm (harm n) = harm n"
eberlm@62049
    56
  by (subst of_real_harm [symmetric]) (simp add: harm_nonneg)
eberlm@62049
    57
hoelzl@63594
    58
lemma harm_expand:
eberlm@62065
    59
  "harm 0 = 0"
eberlm@62049
    60
  "harm (Suc 0) = 1"
eberlm@62049
    61
  "harm (numeral n) = harm (pred_numeral n) + inverse (numeral n)"
eberlm@62049
    62
proof -
eberlm@62049
    63
  have "numeral n = Suc (pred_numeral n)" by simp
eberlm@62049
    64
  also have "harm \<dots> = harm (pred_numeral n) + inverse (numeral n)"
eberlm@62049
    65
    by (subst harm_Suc, subst numeral_eq_Suc[symmetric]) simp
eberlm@62049
    66
  finally show "harm (numeral n) = harm (pred_numeral n) + inverse (numeral n)" .
eberlm@62065
    67
qed (simp_all add: harm_def)
eberlm@62049
    68
eberlm@62049
    69
lemma not_convergent_harm: "\<not>convergent (harm :: nat \<Rightarrow> 'a :: real_normed_field)"
eberlm@62049
    70
proof -
eberlm@62049
    71
  have "convergent (\<lambda>n. norm (harm n :: 'a)) \<longleftrightarrow>
eberlm@62049
    72
            convergent (harm :: nat \<Rightarrow> real)" by (simp add: norm_harm)
eberlm@62049
    73
  also have "\<dots> \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k=Suc 0..Suc n. inverse (of_nat k) :: real)"
eberlm@62049
    74
    unfolding harm_def[abs_def] by (subst convergent_Suc_iff) simp_all
eberlm@62049
    75
  also have "... \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k\<le>n. inverse (of_nat (Suc k)) :: real)"
nipkow@64267
    76
    by (subst sum_shift_bounds_cl_Suc_ivl) (simp add: atLeast0AtMost)
eberlm@62049
    77
  also have "... \<longleftrightarrow> summable (\<lambda>n. inverse (of_nat n) :: real)"
eberlm@62049
    78
    by (subst summable_Suc_iff [symmetric]) (simp add: summable_iff_convergent')
eberlm@62049
    79
  also have "\<not>..." by (rule not_summable_harmonic)
eberlm@62049
    80
  finally show ?thesis by (blast dest: convergent_norm)
eberlm@62049
    81
qed
eberlm@62049
    82
eberlm@62065
    83
lemma harm_pos_iff [simp]: "harm n > (0 :: 'a :: {real_normed_field,linordered_field}) \<longleftrightarrow> n > 0"
eberlm@62065
    84
  by (rule iffI, cases n, simp add: harm_expand, simp, rule harm_pos)
eberlm@62065
    85
eberlm@62065
    86
lemma ln_diff_le_inverse:
eberlm@62065
    87
  assumes "x \<ge> (1::real)"
eberlm@62065
    88
  shows   "ln (x + 1) - ln x < 1 / x"
eberlm@62065
    89
proof -
eberlm@62065
    90
  from assms have "\<exists>z>x. z < x + 1 \<and> ln (x + 1) - ln x = (x + 1 - x) * inverse z"
eberlm@62065
    91
    by (intro MVT2) (auto intro!: derivative_eq_intros simp: field_simps)
eberlm@62065
    92
  then obtain z where z: "z > x" "z < x + 1" "ln (x + 1) - ln x = inverse z" by auto
eberlm@62065
    93
  have "ln (x + 1) - ln x = inverse z" by fact
eberlm@62065
    94
  also from z(1,2) assms have "\<dots> < 1 / x" by (simp add: field_simps)
eberlm@62065
    95
  finally show ?thesis .
eberlm@62065
    96
qed
eberlm@62065
    97
eberlm@62065
    98
lemma ln_le_harm: "ln (real n + 1) \<le> (harm n :: real)"
eberlm@62065
    99
proof (induction n)
eberlm@62065
   100
  fix n assume IH: "ln (real n + 1) \<le> harm n"
eberlm@62065
   101
  have "ln (real (Suc n) + 1) = ln (real n + 1) + (ln (real n + 2) - ln (real n + 1))" by simp
eberlm@62065
   102
  also have "(ln (real n + 2) - ln (real n + 1)) \<le> 1 / real (Suc n)"
eberlm@62065
   103
    using ln_diff_le_inverse[of "real n + 1"] by (simp add: add_ac)
eberlm@62065
   104
  also note IH
eberlm@62065
   105
  also have "harm n + 1 / real (Suc n) = harm (Suc n)" by (simp add: harm_Suc field_simps)
eberlm@62065
   106
  finally show "ln (real (Suc n) + 1) \<le> harm (Suc n)" by - simp
eberlm@62065
   107
qed (simp_all add: harm_def)
eberlm@62065
   108
eberlm@62049
   109
eberlm@62085
   110
subsection \<open>The Euler--Mascheroni constant\<close>
eberlm@62049
   111
eberlm@62049
   112
text \<open>
eberlm@62049
   113
  The limit of the difference between the partial harmonic sum and the natural logarithm
eberlm@62049
   114
  (approximately 0.577216). This value occurs e.g. in the definition of the Gamma function.
eberlm@62049
   115
 \<close>
eberlm@62049
   116
definition euler_mascheroni :: "'a :: real_normed_algebra_1" where
eberlm@62049
   117
  "euler_mascheroni = of_real (lim (\<lambda>n. harm n - ln (of_nat n)))"
eberlm@62049
   118
eberlm@62049
   119
lemma of_real_euler_mascheroni [simp]: "of_real euler_mascheroni = euler_mascheroni"
eberlm@62049
   120
  by (simp add: euler_mascheroni_def)
eberlm@62049
   121
eberlm@62049
   122
interpretation euler_mascheroni: antimono_fun_sum_integral_diff "\<lambda>x. inverse (x + 1)"
eberlm@62049
   123
  by unfold_locales (auto intro!: continuous_intros)
eberlm@62049
   124
eberlm@62049
   125
lemma euler_mascheroni_sum_integral_diff_series:
eberlm@62049
   126
  "euler_mascheroni.sum_integral_diff_series n = harm (Suc n) - ln (of_nat (Suc n))"
eberlm@62049
   127
proof -
eberlm@62049
   128
  have "harm (Suc n) = (\<Sum>k=0..n. inverse (of_nat k + 1) :: real)" unfolding harm_def
nipkow@64267
   129
    unfolding One_nat_def by (subst sum_shift_bounds_cl_Suc_ivl) (simp add: add_ac)
eberlm@62049
   130
  moreover have "((\<lambda>x. inverse (x + 1) :: real) has_integral ln (of_nat n + 1) - ln (0 + 1))
eberlm@62049
   131
                   {0..of_nat n}"
eberlm@62049
   132
    by (intro fundamental_theorem_of_calculus)
eberlm@62049
   133
       (auto intro!: derivative_eq_intros simp: divide_inverse
eberlm@62049
   134
           has_field_derivative_iff_has_vector_derivative[symmetric])
eberlm@62049
   135
  hence "integral {0..of_nat n} (\<lambda>x. inverse (x + 1) :: real) = ln (of_nat (Suc n))"
eberlm@62049
   136
    by (auto dest!: integral_unique)
hoelzl@63594
   137
  ultimately show ?thesis
eberlm@62049
   138
    by (simp add: euler_mascheroni.sum_integral_diff_series_def atLeast0AtMost)
eberlm@62049
   139
qed
eberlm@62049
   140
eberlm@62049
   141
lemma euler_mascheroni_sequence_decreasing:
eberlm@62049
   142
  "m > 0 \<Longrightarrow> m \<le> n \<Longrightarrow> harm n - ln (of_nat n) \<le> harm m - ln (of_nat m :: real)"
eberlm@62049
   143
  by (cases m, simp, cases n, simp, hypsubst,
eberlm@62049
   144
      subst (1 2) euler_mascheroni_sum_integral_diff_series [symmetric],
eberlm@62049
   145
      rule euler_mascheroni.sum_integral_diff_series_antimono, simp)
eberlm@62049
   146
eberlm@62049
   147
lemma euler_mascheroni_sequence_nonneg:
eberlm@62049
   148
  "n > 0 \<Longrightarrow> harm n - ln (of_nat n) \<ge> (0::real)"
eberlm@62049
   149
  by (cases n, simp, hypsubst, subst euler_mascheroni_sum_integral_diff_series [symmetric],
eberlm@62049
   150
      rule euler_mascheroni.sum_integral_diff_series_nonneg)
eberlm@62049
   151
eberlm@62049
   152
lemma euler_mascheroni_convergent: "convergent (\<lambda>n. harm n - ln (of_nat n) :: real)"
eberlm@62049
   153
proof -
hoelzl@63594
   154
  have A: "(\<lambda>n. harm (Suc n) - ln (of_nat (Suc n))) =
eberlm@62049
   155
             euler_mascheroni.sum_integral_diff_series"
eberlm@62049
   156
    by (subst euler_mascheroni_sum_integral_diff_series [symmetric]) (rule refl)
eberlm@62049
   157
  have "convergent (\<lambda>n. harm (Suc n) - ln (of_nat (Suc n) :: real))"
eberlm@62049
   158
    by (subst A) (fact euler_mascheroni.sum_integral_diff_series_convergent)
eberlm@62049
   159
  thus ?thesis by (subst (asm) convergent_Suc_iff)
eberlm@62049
   160
qed
eberlm@62049
   161
hoelzl@63594
   162
lemma euler_mascheroni_LIMSEQ:
eberlm@62049
   163
  "(\<lambda>n. harm n - ln (of_nat n) :: real) \<longlonglongrightarrow> euler_mascheroni"
eberlm@62049
   164
  unfolding euler_mascheroni_def
eberlm@62049
   165
  by (simp add: convergent_LIMSEQ_iff [symmetric] euler_mascheroni_convergent)
eberlm@62049
   166
hoelzl@63594
   167
lemma euler_mascheroni_LIMSEQ_of_real:
hoelzl@63594
   168
  "(\<lambda>n. of_real (harm n - ln (of_nat n))) \<longlonglongrightarrow>
eberlm@62049
   169
      (euler_mascheroni :: 'a :: {real_normed_algebra_1, topological_space})"
eberlm@62049
   170
proof -
eberlm@62049
   171
  have "(\<lambda>n. of_real (harm n - ln (of_nat n))) \<longlonglongrightarrow> (of_real (euler_mascheroni) :: 'a)"
eberlm@62049
   172
    by (intro tendsto_of_real euler_mascheroni_LIMSEQ)
eberlm@62049
   173
  thus ?thesis by simp
eberlm@62049
   174
qed
eberlm@62049
   175
eberlm@63721
   176
lemma euler_mascheroni_sum_real:
eberlm@62049
   177
  "(\<lambda>n. inverse (of_nat (n+1)) + ln (of_nat (n+1)) - ln (of_nat (n+2)) :: real)
hoelzl@63594
   178
       sums euler_mascheroni"
eberlm@62049
   179
 using sums_add[OF telescope_sums[OF LIMSEQ_Suc[OF euler_mascheroni_LIMSEQ]]
eberlm@62049
   180
                   telescope_sums'[OF LIMSEQ_inverse_real_of_nat]]
eberlm@62049
   181
  by (simp_all add: harm_def algebra_simps)
eberlm@62049
   182
eberlm@63721
   183
lemma euler_mascheroni_sum:
eberlm@63721
   184
  "(\<lambda>n. inverse (of_nat (n+1)) + of_real (ln (of_nat (n+1))) - of_real (ln (of_nat (n+2))))
eberlm@63721
   185
       sums (euler_mascheroni :: 'a :: {banach, real_normed_field})"
eberlm@63721
   186
proof -
eberlm@63721
   187
  have "(\<lambda>n. of_real (inverse (of_nat (n+1)) + ln (of_nat (n+1)) - ln (of_nat (n+2))))
eberlm@63721
   188
       sums (of_real euler_mascheroni :: 'a :: {banach, real_normed_field})"
eberlm@63721
   189
    by (subst sums_of_real_iff) (rule euler_mascheroni_sum_real)
eberlm@63721
   190
  thus ?thesis by simp
eberlm@63721
   191
qed
eberlm@63721
   192
eberlm@62049
   193
lemma alternating_harmonic_series_sums: "(\<lambda>k. (-1)^k / real_of_nat (Suc k)) sums ln 2"
eberlm@62049
   194
proof -
eberlm@62049
   195
  let ?f = "\<lambda>n. harm n - ln (real_of_nat n)"
eberlm@62049
   196
  let ?g = "\<lambda>n. if even n then 0 else (2::real)"
eberlm@62049
   197
  let ?em = "\<lambda>n. harm n - ln (real_of_nat n)"
eberlm@62049
   198
  have "eventually (\<lambda>n. ?em (2*n) - ?em n + ln 2 = (\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k))) at_top"
eberlm@62049
   199
    using eventually_gt_at_top[of "0::nat"]
eberlm@62049
   200
  proof eventually_elim
eberlm@62049
   201
    fix n :: nat assume n: "n > 0"
eberlm@62049
   202
    have "(\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k)) =
eberlm@62049
   203
              (\<Sum>k<2*n. ((-1)^k + ?g k) / of_nat (Suc k)) - (\<Sum>k<2*n. ?g k / of_nat (Suc k))"
nipkow@64267
   204
      by (simp add: sum.distrib algebra_simps divide_inverse)
eberlm@62049
   205
    also have "(\<Sum>k<2*n. ((-1)^k + ?g k) / real_of_nat (Suc k)) = harm (2*n)"
nipkow@64267
   206
      unfolding harm_altdef by (intro sum.cong) (auto simp: field_simps)
eberlm@62049
   207
    also have "(\<Sum>k<2*n. ?g k / real_of_nat (Suc k)) = (\<Sum>k|k<2*n \<and> odd k. ?g k / of_nat (Suc k))"
nipkow@64267
   208
      by (intro sum.mono_neutral_right) auto
eberlm@62049
   209
    also have "\<dots> = (\<Sum>k|k<2*n \<and> odd k. 2 / (real_of_nat (Suc k)))"
nipkow@64267
   210
      by (intro sum.cong) auto
hoelzl@63594
   211
    also have "(\<Sum>k|k<2*n \<and> odd k. 2 / (real_of_nat (Suc k))) = harm n"
eberlm@62049
   212
      unfolding harm_altdef
nipkow@64267
   213
      by (intro sum.reindex_cong[of "\<lambda>n. 2*n+1"]) (auto simp: inj_on_def field_simps elim!: oddE)
eberlm@62049
   214
    also have "harm (2*n) - harm n = ?em (2*n) - ?em n + ln 2" using n
eberlm@62049
   215
      by (simp_all add: algebra_simps ln_mult)
eberlm@62049
   216
    finally show "?em (2*n) - ?em n + ln 2 = (\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k))" ..
eberlm@62049
   217
  qed
hoelzl@63594
   218
  moreover have "(\<lambda>n. ?em (2*n) - ?em n + ln (2::real))
eberlm@62049
   219
                     \<longlonglongrightarrow> euler_mascheroni - euler_mascheroni + ln 2"
eberlm@62049
   220
    by (intro tendsto_intros euler_mascheroni_LIMSEQ filterlim_compose[OF euler_mascheroni_LIMSEQ]
eberlm@62049
   221
              filterlim_subseq) (auto simp: subseq_def)
eberlm@62049
   222
  hence "(\<lambda>n. ?em (2*n) - ?em n + ln (2::real)) \<longlonglongrightarrow> ln 2" by simp
eberlm@62049
   223
  ultimately have "(\<lambda>n. (\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k))) \<longlonglongrightarrow> ln 2"
eberlm@62049
   224
    by (rule Lim_transform_eventually)
hoelzl@63594
   225
eberlm@62049
   226
  moreover have "summable (\<lambda>k. (-1)^k * inverse (real_of_nat (Suc k)))"
eberlm@62049
   227
    using LIMSEQ_inverse_real_of_nat
eberlm@62049
   228
    by (intro summable_Leibniz(1) decseq_imp_monoseq decseq_SucI) simp_all
eberlm@62049
   229
  hence A: "(\<lambda>n. \<Sum>k<n. (-1)^k / real_of_nat (Suc k)) \<longlonglongrightarrow> (\<Sum>k. (-1)^k / real_of_nat (Suc k))"
eberlm@62049
   230
    by (simp add: summable_sums_iff divide_inverse sums_def)
eberlm@62049
   231
  from filterlim_compose[OF this filterlim_subseq[of "op * (2::nat)"]]
eberlm@62049
   232
    have "(\<lambda>n. \<Sum>k<2*n. (-1)^k / real_of_nat (Suc k)) \<longlonglongrightarrow> (\<Sum>k. (-1)^k / real_of_nat (Suc k))"
eberlm@62049
   233
    by (simp add: subseq_def)
eberlm@62049
   234
  ultimately have "(\<Sum>k. (- 1) ^ k / real_of_nat (Suc k)) = ln 2" by (intro LIMSEQ_unique)
eberlm@62049
   235
  with A show ?thesis by (simp add: sums_def)
eberlm@62049
   236
qed
eberlm@62049
   237
hoelzl@63594
   238
lemma alternating_harmonic_series_sums':
eberlm@62049
   239
  "(\<lambda>k. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2))) sums ln 2"
eberlm@62049
   240
unfolding sums_def
eberlm@62049
   241
proof (rule Lim_transform_eventually)
eberlm@62049
   242
  show "(\<lambda>n. \<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) \<longlonglongrightarrow> ln 2"
hoelzl@63594
   243
    using alternating_harmonic_series_sums unfolding sums_def
eberlm@62049
   244
    by (rule filterlim_compose) (rule mult_nat_left_at_top, simp)
eberlm@62049
   245
  show "eventually (\<lambda>n. (\<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) =
eberlm@62049
   246
            (\<Sum>k<n. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2)))) sequentially"
eberlm@62049
   247
  proof (intro always_eventually allI)
eberlm@62049
   248
    fix n :: nat
eberlm@62049
   249
    show "(\<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) =
eberlm@62049
   250
              (\<Sum>k<n. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2)))"
eberlm@62049
   251
      by (induction n) (simp_all add: inverse_eq_divide)
eberlm@62049
   252
  qed
hoelzl@63594
   253
qed
eberlm@62049
   254
eberlm@62049
   255
eberlm@62085
   256
subsection \<open>Bounds on the Euler--Mascheroni constant\<close>
eberlm@62049
   257
eberlm@62085
   258
(* TODO: Move? *)
eberlm@62049
   259
lemma ln_inverse_approx_le:
eberlm@62049
   260
  assumes "(x::real) > 0" "a > 0"
eberlm@62049
   261
  shows   "ln (x + a) - ln x \<le> a * (inverse x + inverse (x + a))/2" (is "_ \<le> ?A")
eberlm@62049
   262
proof -
wenzelm@63040
   263
  define f' where "f' = (inverse (x + a) - inverse x)/a"
eberlm@62049
   264
  have f'_nonpos: "f' \<le> 0" using assms by (simp add: f'_def divide_simps)
eberlm@62049
   265
  let ?f = "\<lambda>t. (t - x) * f' + inverse x"
eberlm@62049
   266
  let ?F = "\<lambda>t. (t - x)^2 * f' / 2 + t * inverse x"
hoelzl@63594
   267
  have diff: "\<forall>t\<in>{x..x+a}. (?F has_vector_derivative ?f t)
eberlm@62049
   268
                               (at t within {x..x+a})" using assms
hoelzl@63594
   269
    by (auto intro!: derivative_eq_intros
eberlm@62049
   270
             simp: has_field_derivative_iff_has_vector_derivative[symmetric])
eberlm@62049
   271
  from assms have "(?f has_integral (?F (x+a) - ?F x)) {x..x+a}"
eberlm@62049
   272
    by (intro fundamental_theorem_of_calculus[OF _ diff])
eberlm@62049
   273
       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] field_simps
eberlm@62049
   274
             intro!: derivative_eq_intros)
eberlm@62049
   275
  also have "?F (x+a) - ?F x = (a*2 + f'*a\<^sup>2*x) / (2*x)" using assms by (simp add: field_simps)
hoelzl@63594
   276
  also have "f'*a^2 = - (a^2) / (x*(x + a))" using assms
eberlm@62049
   277
    by (simp add: divide_simps f'_def power2_eq_square)
eberlm@62049
   278
  also have "(a*2 + - a\<^sup>2/(x*(x+a))*x) / (2*x) = ?A" using assms
eberlm@62049
   279
    by (simp add: divide_simps power2_eq_square) (simp add: algebra_simps)
eberlm@62049
   280
  finally have int1: "((\<lambda>t. (t - x) * f' + inverse x) has_integral ?A) {x..x + a}" .
eberlm@62049
   281
eberlm@62049
   282
  from assms have int2: "(inverse has_integral (ln (x + a) - ln x)) {x..x+a}"
eberlm@62049
   283
    by (intro fundamental_theorem_of_calculus)
eberlm@62049
   284
       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
eberlm@62049
   285
             intro!: derivative_eq_intros)
eberlm@62049
   286
  hence "ln (x + a) - ln x = integral {x..x+a} inverse" by (simp add: integral_unique)
eberlm@62049
   287
  also have ineq: "\<forall>xa\<in>{x..x + a}. inverse xa \<le> (xa - x) * f' + inverse x"
eberlm@62049
   288
  proof
eberlm@62049
   289
    fix t assume t': "t \<in> {x..x+a}"
eberlm@62049
   290
    with assms have t: "0 \<le> (t - x) / a" "(t - x) / a \<le> 1" by simp_all
eberlm@62049
   291
    have "inverse t = inverse ((1 - (t - x) / a) *\<^sub>R x + ((t - x) / a) *\<^sub>R (x + a))" (is "_ = ?A")
eberlm@62049
   292
      using assms t' by (simp add: field_simps)
eberlm@62049
   293
    also from assms have "convex_on {x..x+a} inverse" by (intro convex_on_inverse) auto
hoelzl@63594
   294
    from convex_onD_Icc[OF this _ t] assms
eberlm@62049
   295
      have "?A \<le> (1 - (t - x) / a) * inverse x + (t - x) / a * inverse (x + a)" by simp
eberlm@62049
   296
    also have "\<dots> = (t - x) * f' + inverse x" using assms
eberlm@62049
   297
      by (simp add: f'_def divide_simps) (simp add: f'_def field_simps)
eberlm@62049
   298
    finally show "inverse t \<le> (t - x) * f' + inverse x" .
eberlm@62049
   299
  qed
eberlm@62049
   300
  hence "integral {x..x+a} inverse \<le> integral {x..x+a} ?f" using f'_nonpos assms
eberlm@62049
   301
    by (intro integral_le has_integral_integrable[OF int1] has_integral_integrable[OF int2] ineq)
eberlm@62049
   302
  also have "\<dots> = ?A" using int1 by (rule integral_unique)
eberlm@62049
   303
  finally show ?thesis .
eberlm@62049
   304
qed
eberlm@62049
   305
eberlm@62049
   306
lemma ln_inverse_approx_ge:
eberlm@62049
   307
  assumes "(x::real) > 0" "x < y"
eberlm@62049
   308
  shows   "ln y - ln x \<ge> 2 * (y - x) / (x + y)" (is "_ \<ge> ?A")
eberlm@62049
   309
proof -
wenzelm@63040
   310
  define m where "m = (x+y)/2"
wenzelm@63040
   311
  define f' where "f' = -inverse (m^2)"
eberlm@62049
   312
  from assms have m: "m > 0" by (simp add: m_def)
eberlm@62049
   313
  let ?F = "\<lambda>t. (t - m)^2 * f' / 2 + t / m"
eberlm@62049
   314
  from assms have "((\<lambda>t. (t - m) * f' + inverse m) has_integral (?F y - ?F x)) {x..y}"
eberlm@62049
   315
    by (intro fundamental_theorem_of_calculus)
eberlm@62049
   316
       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
eberlm@62049
   317
             intro!: derivative_eq_intros)
hoelzl@63594
   318
  also from m have "?F y - ?F x = ((y - m)^2 - (x - m)^2) * f' / 2 + (y - x) / m"
eberlm@62049
   319
    by (simp add: field_simps)
eberlm@62049
   320
  also have "((y - m)^2 - (x - m)^2) = 0" by (simp add: m_def power2_eq_square field_simps)
eberlm@62049
   321
  also have "0 * f' / 2 + (y - x) / m = ?A" by (simp add: m_def)
eberlm@62049
   322
  finally have int1: "((\<lambda>t. (t - m) * f' + inverse m) has_integral ?A) {x..y}" .
eberlm@62049
   323
eberlm@62049
   324
  from assms have int2: "(inverse has_integral (ln y - ln x)) {x..y}"
eberlm@62049
   325
    by (intro fundamental_theorem_of_calculus)
eberlm@62049
   326
       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
eberlm@62049
   327
             intro!: derivative_eq_intros)
eberlm@62049
   328
  hence "ln y - ln x = integral {x..y} inverse" by (simp add: integral_unique)
eberlm@62049
   329
  also have ineq: "\<forall>xa\<in>{x..y}. inverse xa \<ge> (xa - m) * f' + inverse m"
eberlm@62049
   330
  proof
eberlm@62049
   331
    fix t assume t: "t \<in> {x..y}"
eberlm@62049
   332
    from t assms have "inverse t - inverse m \<ge> f' * (t - m)"
eberlm@62049
   333
      by (intro convex_on_imp_above_tangent[of "{0<..}"] convex_on_inverse)
eberlm@62049
   334
         (auto simp: m_def interior_open f'_def power2_eq_square intro!: derivative_eq_intros)
eberlm@62049
   335
    thus "(t - m) * f' + inverse m \<le> inverse t" by (simp add: algebra_simps)
eberlm@62049
   336
  qed
eberlm@62049
   337
  hence "integral {x..y} inverse \<ge> integral {x..y} (\<lambda>t. (t - m) * f' + inverse m)"
eberlm@62049
   338
    using int1 int2 by (intro integral_le has_integral_integrable)
eberlm@62049
   339
  also have "integral {x..y} (\<lambda>t. (t - m) * f' + inverse m) = ?A"
eberlm@62049
   340
    using integral_unique[OF int1] by simp
eberlm@62049
   341
  finally show ?thesis .
eberlm@62049
   342
qed
eberlm@62049
   343
eberlm@62049
   344
hoelzl@63594
   345
lemma euler_mascheroni_lower:
eberlm@62049
   346
        "euler_mascheroni \<ge> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 2))"
eberlm@62049
   347
  and euler_mascheroni_upper:
eberlm@62049
   348
        "euler_mascheroni \<le> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 1))"
eberlm@62049
   349
proof -
wenzelm@63040
   350
  define D :: "_ \<Rightarrow> real"
wenzelm@63040
   351
    where "D n = inverse (of_nat (n+1)) + ln (of_nat (n+1)) - ln (of_nat (n+2))" for n
eberlm@62049
   352
  let ?g = "\<lambda>n. ln (of_nat (n+2)) - ln (of_nat (n+1)) - inverse (of_nat (n+1)) :: real"
wenzelm@63040
   353
  define inv where [abs_def]: "inv n = inverse (real_of_nat n)" for n
eberlm@62049
   354
  fix n :: nat
eberlm@63721
   355
  note summable = sums_summable[OF euler_mascheroni_sum_real, folded D_def]
eberlm@62049
   356
  have sums: "(\<lambda>k. (inv (Suc (k + (n+1))) - inv (Suc (Suc k + (n+1))))/2) sums ((inv (Suc (0 + (n+1))) - 0)/2)"
eberlm@62049
   357
    unfolding inv_def
eberlm@62049
   358
    by (intro sums_divide telescope_sums' LIMSEQ_ignore_initial_segment LIMSEQ_inverse_real_of_nat)
eberlm@62049
   359
  have sums': "(\<lambda>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2) sums ((inv (Suc (0 + n)) - 0)/2)"
eberlm@62049
   360
    unfolding inv_def
eberlm@62049
   361
    by (intro sums_divide telescope_sums' LIMSEQ_ignore_initial_segment LIMSEQ_inverse_real_of_nat)
eberlm@63721
   362
  from euler_mascheroni_sum_real have "euler_mascheroni = (\<Sum>k. D k)"
eberlm@62049
   363
    by (simp add: sums_iff D_def)
eberlm@62049
   364
  also have "\<dots> = (\<Sum>k. D (k + Suc n)) + (\<Sum>k\<le>n. D k)"
eberlm@63721
   365
    by (subst suminf_split_initial_segment[OF summable, of "Suc n"], 
eberlm@63721
   366
        subst lessThan_Suc_atMost) simp
eberlm@62049
   367
  finally have sum: "(\<Sum>k\<le>n. D k) - euler_mascheroni = -(\<Sum>k. D (k + Suc n))" by simp
eberlm@62049
   368
eberlm@62049
   369
  note sum
eberlm@62049
   370
  also have "\<dots> \<le> -(\<Sum>k. (inv (k + Suc n + 1) - inv (k + Suc n + 2)) / 2)"
eberlm@62049
   371
  proof (intro le_imp_neg_le suminf_le allI summable_ignore_initial_segment[OF summable])
eberlm@62049
   372
    fix k' :: nat
wenzelm@63040
   373
    define k where "k = k' + Suc n"
eberlm@62049
   374
    hence k: "k > 0" by (simp add: k_def)
eberlm@62049
   375
    have "real_of_nat (k+1) > 0" by (simp add: k_def)
eberlm@62049
   376
    with ln_inverse_approx_le[OF this zero_less_one]
eberlm@62049
   377
      have "ln (of_nat k + 2) - ln (of_nat k + 1) \<le> (inv (k+1) + inv (k+2))/2"
eberlm@62049
   378
      by (simp add: inv_def add_ac)
eberlm@62049
   379
    hence "(inv (k+1) - inv (k+2))/2 \<le> inv (k+1) + ln (of_nat (k+1)) - ln (of_nat (k+2))"
eberlm@62049
   380
      by (simp add: field_simps)
eberlm@62049
   381
    also have "\<dots> = D k" unfolding D_def inv_def ..
eberlm@62049
   382
    finally show "D (k' + Suc n) \<ge> (inv (k' + Suc n + 1) - inv (k' + Suc n + 2)) / 2"
eberlm@62049
   383
      by (simp add: k_def)
hoelzl@63594
   384
    from sums_summable[OF sums]
eberlm@62049
   385
      show "summable (\<lambda>k. (inv (k + Suc n + 1) - inv (k + Suc n + 2))/2)" by simp
eberlm@62049
   386
  qed
eberlm@62049
   387
  also from sums have "\<dots> = -inv (n+2) / 2" by (simp add: sums_iff)
hoelzl@63594
   388
  finally have "euler_mascheroni \<ge> (\<Sum>k\<le>n. D k) + 1 / (of_nat (2 * (n+2)))"
eberlm@62085
   389
    by (simp add: inv_def field_simps)
eberlm@62049
   390
  also have "(\<Sum>k\<le>n. D k) = harm (Suc n) - (\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1)))"
nipkow@64267
   391
    unfolding harm_altdef D_def by (subst lessThan_Suc_atMost) (simp add:  sum.distrib sum_subtractf)
eberlm@62049
   392
  also have "(\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1))) = ln (of_nat (n+2))"
nipkow@64267
   393
    by (subst atLeast0AtMost [symmetric], subst sum_Suc_diff) simp_all
eberlm@62049
   394
  finally show "euler_mascheroni \<ge> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 2))"
eberlm@62049
   395
    by simp
hoelzl@63594
   396
eberlm@62049
   397
  note sum
eberlm@62049
   398
  also have "-(\<Sum>k. D (k + Suc n)) \<ge> -(\<Sum>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2)"
eberlm@62049
   399
  proof (intro le_imp_neg_le suminf_le allI summable_ignore_initial_segment[OF summable])
eberlm@62049
   400
    fix k' :: nat
wenzelm@63040
   401
    define k where "k = k' + Suc n"
eberlm@62049
   402
    hence k: "k > 0" by (simp add: k_def)
eberlm@62049
   403
    have "real_of_nat (k+1) > 0" by (simp add: k_def)
eberlm@62049
   404
    from ln_inverse_approx_ge[of "of_nat k + 1" "of_nat k + 2"]
eberlm@62049
   405
      have "2 / (2 * real_of_nat k + 3) \<le> ln (of_nat (k+2)) - ln (real_of_nat (k+1))"
eberlm@62049
   406
      by (simp add: add_ac)
hoelzl@63594
   407
    hence "D k \<le> 1 / real_of_nat (k+1) - 2 / (2 * real_of_nat k + 3)"
eberlm@62049
   408
      by (simp add: D_def inverse_eq_divide inv_def)
eberlm@62049
   409
    also have "\<dots> = inv ((k+1)*(2*k+3))" unfolding inv_def by (simp add: field_simps)
eberlm@62049
   410
    also have "\<dots> \<le> inv (2*k*(k+1))" unfolding inv_def using k
hoelzl@63594
   411
      by (intro le_imp_inverse_le)
eberlm@62049
   412
         (simp add: algebra_simps, simp del: of_nat_add)
eberlm@62049
   413
    also have "\<dots> = (inv k - inv (k+1))/2" unfolding inv_def using k
eberlm@62049
   414
      by (simp add: divide_simps del: of_nat_mult) (simp add: algebra_simps)
eberlm@62049
   415
    finally show "D k \<le> (inv (Suc (k' + n)) - inv (Suc (Suc k' + n)))/2" unfolding k_def by simp
eberlm@62049
   416
  next
hoelzl@63594
   417
    from sums_summable[OF sums']
eberlm@62049
   418
      show "summable (\<lambda>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2)" by simp
eberlm@62049
   419
  qed
eberlm@62049
   420
  also from sums' have "(\<Sum>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2) = inv (n+1)/2"
eberlm@62049
   421
    by (simp add: sums_iff)
hoelzl@63594
   422
  finally have "euler_mascheroni \<le> (\<Sum>k\<le>n. D k) + 1 / of_nat (2 * (n+1))"
eberlm@62049
   423
    by (simp add: inv_def field_simps)
eberlm@62049
   424
  also have "(\<Sum>k\<le>n. D k) = harm (Suc n) - (\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1)))"
nipkow@64267
   425
    unfolding harm_altdef D_def by (subst lessThan_Suc_atMost) (simp add:  sum.distrib sum_subtractf)
eberlm@62049
   426
  also have "(\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1))) = ln (of_nat (n+2))"
nipkow@64267
   427
    by (subst atLeast0AtMost [symmetric], subst sum_Suc_diff) simp_all
eberlm@62049
   428
  finally show "euler_mascheroni \<le> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 1))"
eberlm@62049
   429
    by simp
eberlm@62049
   430
qed
eberlm@62049
   431
eberlm@62049
   432
lemma euler_mascheroni_pos: "euler_mascheroni > (0::real)"
eberlm@62049
   433
  using euler_mascheroni_lower[of 0] ln_2_less_1 by (simp add: harm_def)
eberlm@62049
   434
eberlm@62085
   435
context
eberlm@62085
   436
begin
eberlm@62085
   437
eberlm@62085
   438
private lemma ln_approx_aux:
eberlm@62049
   439
  fixes n :: nat and x :: real
eberlm@62049
   440
  defines "y \<equiv> (x-1)/(x+1)"
eberlm@62049
   441
  assumes x: "x > 0" "x \<noteq> 1"
hoelzl@63594
   442
  shows "inverse (2*y^(2*n+1)) * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))) \<in>
eberlm@62049
   443
            {0..(1 / (1 - y^2) / of_nat (2*n+1))}"
eberlm@62049
   444
proof -
eberlm@62049
   445
  from x have norm_y: "norm y < 1" unfolding y_def by simp
eberlm@62049
   446
  from power_strict_mono[OF this, of 2] have norm_y': "norm y^2 < 1" by simp
eberlm@62049
   447
eberlm@62049
   448
  let ?f = "\<lambda>k. 2 * y ^ (2*k+1) / of_nat (2*k+1)"
eberlm@62049
   449
  note sums = ln_series_quadratic[OF x(1)]
wenzelm@63040
   450
  define c where "c = inverse (2*y^(2*n+1))"
eberlm@62049
   451
  let ?d = "c * (ln x - (\<Sum>k<n. ?f k))"
eberlm@62049
   452
  have "\<forall>k. y\<^sup>2^k / of_nat (2*(k+n)+1) \<le> y\<^sup>2 ^ k / of_nat (2*n+1)"
eberlm@62049
   453
    by (intro allI divide_left_mono mult_right_mono mult_pos_pos zero_le_power[of "y^2"]) simp_all
eberlm@62049
   454
  moreover {
eberlm@62049
   455
    have "(\<lambda>k. ?f (k + n)) sums (ln x - (\<Sum>k<n. ?f k))"
eberlm@62049
   456
      using sums_split_initial_segment[OF sums] by (simp add: y_def)
eberlm@62049
   457
    hence "(\<lambda>k. c * ?f (k + n)) sums ?d" by (rule sums_mult)
eberlm@62049
   458
    also have "(\<lambda>k. c * (2*y^(2*(k+n)+1) / of_nat (2*(k+n)+1))) =
eberlm@62049
   459
                   (\<lambda>k. (c * (2*y^(2*n+1))) * ((y^2)^k / of_nat (2*(k+n)+1)))"
eberlm@62049
   460
      by (simp only: ring_distribs power_add power_mult) (simp add: mult_ac)
eberlm@62049
   461
    also from x have "c * (2*y^(2*n+1)) = 1" by (simp add: c_def y_def)
eberlm@62049
   462
    finally have "(\<lambda>k. (y^2)^k / of_nat (2*(k+n)+1)) sums ?d" by simp
eberlm@62049
   463
  } note sums' = this
eberlm@62049
   464
  moreover from norm_y' have "(\<lambda>k. (y^2)^k / of_nat (2*n+1)) sums (1 / (1 - y^2) / of_nat (2*n+1))"
eberlm@62049
   465
    by (intro sums_divide geometric_sums) (simp_all add: norm_power)
eberlm@62049
   466
  ultimately have "?d \<le> (1 / (1 - y^2) / of_nat (2*n+1))" by (rule sums_le)
eberlm@62049
   467
  moreover have "c * (ln x - (\<Sum>k<n. 2 * y ^ (2 * k + 1) / real_of_nat (2 * k + 1))) \<ge> 0"
eberlm@62049
   468
    by (intro sums_le[OF _ sums_zero sums']) simp_all
eberlm@62049
   469
  ultimately show ?thesis unfolding c_def by simp
eberlm@62049
   470
qed
eberlm@62049
   471
eberlm@62049
   472
lemma
eberlm@62049
   473
  fixes n :: nat and x :: real
eberlm@62049
   474
  defines "y \<equiv> (x-1)/(x+1)"
eberlm@62049
   475
  defines "approx \<equiv> (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))"
eberlm@62049
   476
  defines "d \<equiv> y^(2*n+1) / (1 - y^2) / of_nat (2*n+1)"
eberlm@62049
   477
  assumes x: "x > 1"
eberlm@62049
   478
  shows   ln_approx_bounds: "ln x \<in> {approx..approx + 2*d}"
eberlm@62049
   479
  and     ln_approx_abs:    "abs (ln x - (approx + d)) \<le> d"
eberlm@62049
   480
proof -
wenzelm@63040
   481
  define c where "c = 2*y^(2*n+1)"
hoelzl@63594
   482
  from x have c_pos: "c > 0" unfolding c_def y_def
eberlm@62049
   483
    by (intro mult_pos_pos zero_less_power) simp_all
eberlm@62049
   484
  have A: "inverse c * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))) \<in>
eberlm@62049
   485
              {0.. (1 / (1 - y^2) / of_nat (2*n+1))}" using assms unfolding y_def c_def
eberlm@62049
   486
    by (intro ln_approx_aux) simp_all
eberlm@62049
   487
  hence "inverse c * (ln x - (\<Sum>k<n. 2*y^(2*k+1)/of_nat (2*k+1))) \<le> (1 / (1-y^2) / of_nat (2*n+1))"
eberlm@62049
   488
    by simp
hoelzl@63594
   489
  hence "(ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))) / c \<le> (1 / (1 - y^2) / of_nat (2*n+1))"
eberlm@62049
   490
    by (auto simp add: divide_simps)
eberlm@62049
   491
  with c_pos have "ln x \<le> c / (1 - y^2) / of_nat (2*n+1) + approx"
eberlm@62049
   492
    by (subst (asm) pos_divide_le_eq) (simp_all add: mult_ac approx_def)
eberlm@62049
   493
  moreover {
eberlm@62049
   494
    from A c_pos have "0 \<le> c * (inverse c * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))))"
eberlm@62049
   495
      by (intro mult_nonneg_nonneg[of c]) simp_all
hoelzl@63594
   496
    also have "\<dots> = (c * inverse c) * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1)))"
eberlm@62049
   497
      by (simp add: mult_ac)
eberlm@62049
   498
    also from c_pos have "c * inverse c = 1" by simp
eberlm@62049
   499
    finally have "ln x \<ge> approx" by (simp add: approx_def)
eberlm@62049
   500
  }
eberlm@62049
   501
  ultimately show "ln x \<in> {approx..approx + 2*d}" by (simp add: c_def d_def)
eberlm@62049
   502
  thus "abs (ln x - (approx + d)) \<le> d" by auto
eberlm@62049
   503
qed
eberlm@62049
   504
eberlm@62049
   505
end
eberlm@62049
   506
eberlm@62049
   507
lemma euler_mascheroni_bounds:
eberlm@62049
   508
  fixes n :: nat assumes "n \<ge> 1" defines "t \<equiv> harm n - ln (of_nat (Suc n)) :: real"
eberlm@62049
   509
  shows "euler_mascheroni \<in> {t + inverse (of_nat (2*(n+1)))..t + inverse (of_nat (2*n))}"
eberlm@62049
   510
  using assms euler_mascheroni_upper[of "n-1"] euler_mascheroni_lower[of "n-1"]
eberlm@62085
   511
  unfolding t_def by (cases n) (simp_all add: harm_Suc t_def inverse_eq_divide)
eberlm@62049
   512
eberlm@62049
   513
lemma euler_mascheroni_bounds':
eberlm@62049
   514
  fixes n :: nat assumes "n \<ge> 1" "ln (real_of_nat (Suc n)) \<in> {l<..<u}"
hoelzl@63594
   515
  shows "euler_mascheroni \<in>
eberlm@62049
   516
           {harm n - u + inverse (of_nat (2*(n+1)))<..<harm n - l + inverse (of_nat (2*n))}"
eberlm@62049
   517
  using euler_mascheroni_bounds[OF assms(1)] assms(2) by auto
eberlm@62049
   518
eberlm@62085
   519
eberlm@62085
   520
text \<open>
eberlm@62085
   521
  Approximation of @{term "ln 2"}. The lower bound is accurate to about 0.03; the upper
eberlm@62085
   522
  bound is accurate to about 0.0015.
eberlm@62085
   523
\<close>
hoelzl@63594
   524
lemma ln2_ge_two_thirds: "2/3 \<le> ln (2::real)"
eberlm@62085
   525
  and ln2_le_25_over_36: "ln (2::real) \<le> 25/36"
eberlm@62085
   526
  using ln_approx_bounds[of 2 1, simplified, simplified eval_nat_numeral, simplified] by simp_all
eberlm@62085
   527
eberlm@62085
   528
eberlm@62085
   529
text \<open>
hoelzl@63594
   530
  Approximation of the Euler--Mascheroni constant. The lower bound is accurate to about 0.0015;
eberlm@62085
   531
  the upper bound is accurate to about 0.015.
eberlm@62085
   532
\<close>
eberlm@62085
   533
lemma euler_mascheroni_gt_19_over_33: "(euler_mascheroni :: real) > 19/33" (is ?th1)
eberlm@62085
   534
  and euler_mascheroni_less_13_over_22: "(euler_mascheroni :: real) < 13/22" (is ?th2)
eberlm@62049
   535
proof -
eberlm@62085
   536
  have "ln (real (Suc 7)) = 3 * ln 2" by (simp add: ln_powr [symmetric] powr_numeral)
eberlm@62085
   537
  also from ln_approx_bounds[of 2 3] have "\<dots> \<in> {3*307/443<..<3*4615/6658}"
eberlm@62085
   538
    by (simp add: eval_nat_numeral)
eberlm@62085
   539
  finally have "ln (real (Suc 7)) \<in> \<dots>" .
eberlm@62085
   540
  from euler_mascheroni_bounds'[OF _ this] have "?th1 \<and> ?th2" by (simp_all add: harm_expand)
eberlm@62085
   541
  thus ?th1 ?th2 by blast+
eberlm@62049
   542
qed
eberlm@62049
   543
eberlm@62085
   544
end