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