Added lots of material on infinite sums, convergence radii, harmonic numbers, Gamma function
authoreberlm
Mon Jan 04 17:45:36 2016 +0100 (2016-01-04)
changeset 62049b0f941e207cf
parent 62048 fefd79f6b232
child 62054 cff7114e4572
Added lots of material on infinite sums, convergence radii, harmonic numbers, Gamma function
src/HOL/Library/Extended_Real.thy
src/HOL/Library/Liminf_Limsup.thy
src/HOL/Library/Nonpos_Ints.thy
src/HOL/Library/Periodic_Fun.thy
src/HOL/Multivariate_Analysis/Complex_Transcendental.thy
src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy
src/HOL/Multivariate_Analysis/Gamma.thy
src/HOL/Multivariate_Analysis/Generalised_Binomial_Theorem.thy
src/HOL/Multivariate_Analysis/Harmonic_Numbers.thy
src/HOL/Multivariate_Analysis/Integral_Test.thy
src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy
src/HOL/Multivariate_Analysis/Summation.thy
src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
src/HOL/Real_Vector_Spaces.thy
src/HOL/Series.thy
src/HOL/Topological_Spaces.thy
src/HOL/Transcendental.thy
     1.1 --- a/src/HOL/Library/Extended_Real.thy	Sun Jan 03 21:45:34 2016 +0100
     1.2 +++ b/src/HOL/Library/Extended_Real.thy	Mon Jan 04 17:45:36 2016 +0100
     1.3 @@ -3,6 +3,7 @@
     1.4      Author:     Robert Himmelmann, TU München
     1.5      Author:     Armin Heller, TU München
     1.6      Author:     Bogdan Grechuk, University of Edinburgh
     1.7 +    Author:     Manuel Eberl, TU München
     1.8  *)
     1.9  
    1.10  section \<open>Extended real number line\<close>
    1.11 @@ -1629,6 +1630,14 @@
    1.12    by (cases a b c rule: ereal3_cases)
    1.13       (auto simp: field_simps zero_less_mult_iff)
    1.14  
    1.15 +lemma ereal_inverse_real: "\<bar>z\<bar> \<noteq> \<infinity> \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> ereal (inverse (real_of_ereal z)) = inverse z"
    1.16 +  by (cases z) simp_all
    1.17 +
    1.18 +lemma ereal_inverse_mult:
    1.19 +  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse (a * (b::ereal)) = inverse a * inverse b"
    1.20 +  by (cases a; cases b) auto
    1.21 +
    1.22 +     
    1.23  subsection "Complete lattice"
    1.24  
    1.25  instantiation ereal :: lattice
    1.26 @@ -2472,6 +2481,21 @@
    1.27      by (auto simp: order_tendsto_iff)
    1.28  qed
    1.29  
    1.30 +lemma tendsto_PInfty': "(f \<longlongrightarrow> \<infinity>) F = (\<forall>r>c. eventually (\<lambda>x. ereal r < f x) F)"
    1.31 +proof (subst tendsto_PInfty, intro iffI allI impI)
    1.32 +  assume A: "\<forall>r>c. eventually (\<lambda>x. ereal r < f x) F"
    1.33 +  fix r :: real
    1.34 +  from A have A: "eventually (\<lambda>x. ereal r < f x) F" if "r > c" for r using that by blast
    1.35 +  show "eventually (\<lambda>x. ereal r < f x) F"
    1.36 +  proof (cases "r > c")
    1.37 +    case False
    1.38 +    hence B: "ereal r \<le> ereal (c + 1)" by simp
    1.39 +    have "c < c + 1" by simp
    1.40 +    from A[OF this] show "eventually (\<lambda>x. ereal r < f x) F"
    1.41 +      by eventually_elim (rule le_less_trans[OF B])
    1.42 +  qed (simp add: A)
    1.43 +qed simp
    1.44 +
    1.45  lemma tendsto_PInfty_eq_at_top:
    1.46    "((\<lambda>z. ereal (f z)) \<longlongrightarrow> \<infinity>) F \<longleftrightarrow> (LIM z F. f z :> at_top)"
    1.47    unfolding tendsto_PInfty filterlim_at_top_dense by simp
    1.48 @@ -2495,6 +2519,21 @@
    1.49      by auto
    1.50  qed
    1.51  
    1.52 +lemma tendsto_MInfty': "(f \<longlongrightarrow> -\<infinity>) F = (\<forall>r<c. eventually (\<lambda>x. ereal r > f x) F)"
    1.53 +proof (subst tendsto_MInfty, intro iffI allI impI)
    1.54 +  assume A: "\<forall>r<c. eventually (\<lambda>x. ereal r > f x) F"
    1.55 +  fix r :: real
    1.56 +  from A have A: "eventually (\<lambda>x. ereal r > f x) F" if "r < c" for r using that by blast
    1.57 +  show "eventually (\<lambda>x. ereal r > f x) F"
    1.58 +  proof (cases "r < c")
    1.59 +    case False
    1.60 +    hence B: "ereal r \<ge> ereal (c - 1)" by simp
    1.61 +    have "c > c - 1" by simp
    1.62 +    from A[OF this] show "eventually (\<lambda>x. ereal r > f x) F"
    1.63 +      by eventually_elim (erule less_le_trans[OF _ B])
    1.64 +  qed (simp add: A)
    1.65 +qed simp
    1.66 +
    1.67  lemma Lim_PInfty: "f \<longlonglongrightarrow> \<infinity> \<longleftrightarrow> (\<forall>B. \<exists>N. \<forall>n\<ge>N. f n \<ge> ereal B)"
    1.68    unfolding tendsto_PInfty eventually_sequentially
    1.69  proof safe
    1.70 @@ -2527,6 +2566,19 @@
    1.71  lemma Lim_bounded_MInfty: "f \<longlonglongrightarrow> l \<Longrightarrow> (\<And>n. ereal B \<le> f n) \<Longrightarrow> l \<noteq> -\<infinity>"
    1.72    using LIMSEQ_le_const[of f l "ereal B"] by auto
    1.73  
    1.74 +lemma tendsto_zero_erealI:
    1.75 +  assumes "\<And>e. e > 0 \<Longrightarrow> eventually (\<lambda>x. \<bar>f x\<bar> < ereal e) F"
    1.76 +  shows   "(f \<longlongrightarrow> 0) F"
    1.77 +proof (subst filterlim_cong[OF refl refl])
    1.78 +  from assms[OF zero_less_one] show "eventually (\<lambda>x. f x = ereal (real_of_ereal (f x))) F"
    1.79 +    by eventually_elim (auto simp: ereal_real)
    1.80 +  hence "eventually (\<lambda>x. abs (real_of_ereal (f x)) < e) F" if "e > 0" for e using assms[OF that]
    1.81 +    by eventually_elim (simp add: real_less_ereal_iff that)
    1.82 +  hence "((\<lambda>x. real_of_ereal (f x)) \<longlongrightarrow> 0) F" unfolding tendsto_iff 
    1.83 +    by (auto simp: tendsto_iff dist_real_def)
    1.84 +  thus "((\<lambda>x. ereal (real_of_ereal (f x))) \<longlongrightarrow> 0) F" by (simp add: zero_ereal_def)
    1.85 +qed
    1.86 +
    1.87  lemma tendsto_explicit:
    1.88    "f \<longlonglongrightarrow> f0 \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> f0 \<in> S \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. f n \<in> S))"
    1.89    unfolding tendsto_def eventually_sequentially by auto
    1.90 @@ -3678,6 +3730,126 @@
    1.91      by auto
    1.92  qed
    1.93  
    1.94 +lemma continuous_uminus_ereal [continuous_intros]: "continuous_on (A :: ereal set) uminus"
    1.95 +  unfolding continuous_on_def
    1.96 +  by (intro ballI tendsto_uminus_ereal[of "\<lambda>x. x::ereal"]) simp
    1.97 +
    1.98 +lemma ereal_uminus_atMost [simp]: "uminus ` {..(a::ereal)} = {-a..}"
    1.99 +proof (intro equalityI subsetI)
   1.100 +  fix x :: ereal assume "x \<in> {-a..}"
   1.101 +  hence "-(-x) \<in> uminus ` {..a}" by (intro imageI) (simp add: ereal_uminus_le_reorder)
   1.102 +  thus "x \<in> uminus ` {..a}" by simp
   1.103 +qed auto
   1.104 +
   1.105 +lemma continuous_on_inverse_ereal [continuous_intros]: 
   1.106 +  "continuous_on {0::ereal ..} inverse"
   1.107 +  unfolding continuous_on_def
   1.108 +proof clarsimp
   1.109 +  fix x :: ereal assume "0 \<le> x"
   1.110 +  moreover have "at 0 within {0 ..} = at_right (0::ereal)"
   1.111 +    by (auto simp: filter_eq_iff eventually_at_filter le_less)
   1.112 +  moreover have "at x within {0 ..} = at x" if "0 < x"
   1.113 +    using that by (intro at_within_nhd[of _ "{0<..}"]) auto
   1.114 +  ultimately show "(inverse \<longlongrightarrow> inverse x) (at x within {0..})"
   1.115 +    by (auto simp: le_less inverse_ereal_tendsto_at_right_0 inverse_ereal_tendsto_pos)
   1.116 +qed
   1.117 +  
   1.118 +lemma continuous_inverse_ereal_nonpos: "continuous_on ({..<0} :: ereal set) inverse"
   1.119 +proof (subst continuous_on_cong[OF refl]) 
   1.120 +  have "continuous_on {(0::ereal)<..} inverse"
   1.121 +    by (rule continuous_on_subset[OF continuous_on_inverse_ereal]) auto
   1.122 +  thus "continuous_on {..<(0::ereal)} (uminus \<circ> inverse \<circ> uminus)"
   1.123 +    by (intro continuous_intros) simp_all
   1.124 +qed simp
   1.125 +
   1.126 +lemma tendsto_inverse_ereal:
   1.127 +  assumes "(f \<longlongrightarrow> (c :: ereal)) F"
   1.128 +  assumes "eventually (\<lambda>x. f x \<ge> 0) F"
   1.129 +  shows   "((\<lambda>x. inverse (f x)) \<longlongrightarrow> inverse c) F"
   1.130 +  by (cases "F = bot") 
   1.131 +     (auto intro!: tendsto_le_const[of F] assms 
   1.132 +                   continuous_on_tendsto_compose[OF continuous_on_inverse_ereal])
   1.133 +
   1.134 +
   1.135 +subsubsection \<open>liminf and limsup\<close>
   1.136 +
   1.137 +lemma Limsup_ereal_mult_right: 
   1.138 +  assumes "F \<noteq> bot" "(c::real) \<ge> 0"
   1.139 +  shows   "Limsup F (\<lambda>n. f n * ereal c) = Limsup F f * ereal c"
   1.140 +proof (rule Limsup_compose_continuous_mono)
   1.141 +  from assms show "continuous_on UNIV (\<lambda>a. a * ereal c)"
   1.142 +    using tendsto_cmult_ereal[of "ereal c" "\<lambda>x. x" ]
   1.143 +    by (force simp: continuous_on_def mult_ac)
   1.144 +qed (insert assms, auto simp: mono_def ereal_mult_right_mono)
   1.145 +
   1.146 +lemma Liminf_ereal_mult_right: 
   1.147 +  assumes "F \<noteq> bot" "(c::real) \<ge> 0"
   1.148 +  shows   "Liminf F (\<lambda>n. f n * ereal c) = Liminf F f * ereal c"
   1.149 +proof (rule Liminf_compose_continuous_mono)
   1.150 +  from assms show "continuous_on UNIV (\<lambda>a. a * ereal c)"
   1.151 +    using tendsto_cmult_ereal[of "ereal c" "\<lambda>x. x" ]
   1.152 +    by (force simp: continuous_on_def mult_ac)
   1.153 +qed (insert assms, auto simp: mono_def ereal_mult_right_mono)
   1.154 +
   1.155 +lemma Limsup_ereal_mult_left: 
   1.156 +  assumes "F \<noteq> bot" "(c::real) \<ge> 0"
   1.157 +  shows   "Limsup F (\<lambda>n. ereal c * f n) = ereal c * Limsup F f"
   1.158 +  using Limsup_ereal_mult_right[OF assms] by (subst (1 2) mult.commute)
   1.159 +
   1.160 +lemma limsup_ereal_mult_right: 
   1.161 +  "(c::real) \<ge> 0 \<Longrightarrow> limsup (\<lambda>n. f n * ereal c) = limsup f * ereal c"
   1.162 +  by (rule Limsup_ereal_mult_right) simp_all
   1.163 +
   1.164 +lemma limsup_ereal_mult_left: 
   1.165 +  "(c::real) \<ge> 0 \<Longrightarrow> limsup (\<lambda>n. ereal c * f n) = ereal c * limsup f"
   1.166 +  by (subst (1 2) mult.commute, rule limsup_ereal_mult_right) simp_all
   1.167 +
   1.168 +lemma Limsup_add_ereal_right: 
   1.169 +  "F \<noteq> bot \<Longrightarrow> abs c \<noteq> \<infinity> \<Longrightarrow> Limsup F (\<lambda>n. g n + (c :: ereal)) = Limsup F g + c"
   1.170 +  by (rule Limsup_compose_continuous_mono) (auto simp: mono_def ereal_add_mono continuous_on_def)
   1.171 +
   1.172 +lemma Limsup_add_ereal_left: 
   1.173 +  "F \<noteq> bot \<Longrightarrow> abs c \<noteq> \<infinity> \<Longrightarrow> Limsup F (\<lambda>n. (c :: ereal) + g n) = c + Limsup F g"
   1.174 +  by (subst (1 2) add.commute) (rule Limsup_add_ereal_right)
   1.175 +
   1.176 +lemma Liminf_add_ereal_right: 
   1.177 +  "F \<noteq> bot \<Longrightarrow> abs c \<noteq> \<infinity> \<Longrightarrow> Liminf F (\<lambda>n. g n + (c :: ereal)) = Liminf F g + c"
   1.178 +  by (rule Liminf_compose_continuous_mono) (auto simp: mono_def ereal_add_mono continuous_on_def)
   1.179 +
   1.180 +lemma Liminf_add_ereal_left: 
   1.181 +  "F \<noteq> bot \<Longrightarrow> abs c \<noteq> \<infinity> \<Longrightarrow> Liminf F (\<lambda>n. (c :: ereal) + g n) = c + Liminf F g"
   1.182 +  by (subst (1 2) add.commute) (rule Liminf_add_ereal_right)
   1.183 +
   1.184 +lemma 
   1.185 +  assumes "F \<noteq> bot"
   1.186 +  assumes nonneg: "eventually (\<lambda>x. f x \<ge> (0::ereal)) F"
   1.187 +  shows   Liminf_inverse_ereal: "Liminf F (\<lambda>x. inverse (f x)) = inverse (Limsup F f)"
   1.188 +  and     Limsup_inverse_ereal: "Limsup F (\<lambda>x. inverse (f x)) = inverse (Liminf F f)"
   1.189 +proof -
   1.190 +  def inv \<equiv> "\<lambda>x. if x \<le> 0 then \<infinity> else inverse x :: ereal"
   1.191 +  have "continuous_on ({..0} \<union> {0..}) inv" unfolding inv_def
   1.192 +    by (intro continuous_on_If) (auto intro!: continuous_intros)
   1.193 +  also have "{..0} \<union> {0..} = (UNIV :: ereal set)" by auto
   1.194 +  finally have cont: "continuous_on UNIV inv" .
   1.195 +  have antimono: "antimono inv" unfolding inv_def antimono_def
   1.196 +    by (auto intro!: ereal_inverse_antimono)
   1.197 +  
   1.198 +  have "Liminf F (\<lambda>x. inverse (f x)) = Liminf F (\<lambda>x. inv (f x))" using nonneg
   1.199 +    by (auto intro!: Liminf_eq elim!: eventually_mono simp: inv_def)
   1.200 +  also have "... = inv (Limsup F f)"
   1.201 +    by (simp add: assms(1) Liminf_compose_continuous_antimono[OF cont antimono])
   1.202 +  also from assms have "Limsup F f \<ge> 0" by (intro le_Limsup) simp_all
   1.203 +  hence "inv (Limsup F f) = inverse (Limsup F f)" by (simp add: inv_def)
   1.204 +  finally show "Liminf F (\<lambda>x. inverse (f x)) = inverse (Limsup F f)" .
   1.205 +
   1.206 +  have "Limsup F (\<lambda>x. inverse (f x)) = Limsup F (\<lambda>x. inv (f x))" using nonneg
   1.207 +    by (auto intro!: Limsup_eq elim!: eventually_mono simp: inv_def)
   1.208 +  also have "... = inv (Liminf F f)"
   1.209 +    by (simp add: assms(1) Limsup_compose_continuous_antimono[OF cont antimono])
   1.210 +  also from assms have "Liminf F f \<ge> 0" by (intro Liminf_bounded) simp_all
   1.211 +  hence "inv (Liminf F f) = inverse (Liminf F f)" by (simp add: inv_def)
   1.212 +  finally show "Limsup F (\<lambda>x. inverse (f x)) = inverse (Liminf F f)" .
   1.213 +qed
   1.214  
   1.215  subsubsection \<open>Tests for code generator\<close>
   1.216  
     2.1 --- a/src/HOL/Library/Liminf_Limsup.thy	Sun Jan 03 21:45:34 2016 +0100
     2.2 +++ b/src/HOL/Library/Liminf_Limsup.thy	Mon Jan 04 17:45:36 2016 +0100
     2.3 @@ -1,5 +1,6 @@
     2.4  (*  Title:      HOL/Library/Liminf_Limsup.thy
     2.5      Author:     Johannes Hölzl, TU München
     2.6 +    Author:     Manuel Eberl, TU München
     2.7  *)
     2.8  
     2.9  section \<open>Liminf and Limsup on complete lattices\<close>
    2.10 @@ -189,6 +190,42 @@
    2.11      unfolding Liminf_def le_SUP_iff by auto
    2.12  qed
    2.13  
    2.14 +lemma Limsup_le_iff:
    2.15 +  fixes X :: "_ \<Rightarrow> _ :: complete_linorder"
    2.16 +  shows "C \<ge> Limsup F X \<longleftrightarrow> (\<forall>y>C. eventually (\<lambda>x. y > X x) F)"
    2.17 +proof -
    2.18 +  { fix y P assume "eventually P F" "y > SUPREMUM (Collect P) X"
    2.19 +    then have "eventually (\<lambda>x. y > X x) F"
    2.20 +      by (auto elim!: eventually_mono dest: SUP_lessD) }
    2.21 +  moreover
    2.22 +  { fix y P assume "y > C" and y: "\<forall>y>C. eventually (\<lambda>x. y > X x) F"
    2.23 +    have "\<exists>P. eventually P F \<and> y > SUPREMUM (Collect P) X"
    2.24 +    proof (cases "\<exists>z. C < z \<and> z < y")
    2.25 +      case True
    2.26 +      then obtain z where z: "C < z \<and> z < y" ..
    2.27 +      moreover from z have "z \<ge> SUPREMUM {x. z > X x} X"
    2.28 +        by (auto intro!: SUP_least)
    2.29 +      ultimately show ?thesis
    2.30 +        using y by (intro exI[of _ "\<lambda>x. z > X x"]) auto
    2.31 +    next
    2.32 +      case False
    2.33 +      then have "C \<ge> SUPREMUM {x. y > X x} X"
    2.34 +        by (intro SUP_least) (auto simp: not_less)
    2.35 +      with \<open>y > C\<close> show ?thesis
    2.36 +        using y by (intro exI[of _ "\<lambda>x. y > X x"]) auto
    2.37 +    qed }
    2.38 +  ultimately show ?thesis
    2.39 +    unfolding Limsup_def INF_le_iff by auto
    2.40 +qed
    2.41 +
    2.42 +lemma less_LiminfD:
    2.43 +  "y < Liminf F (f :: _ \<Rightarrow> 'a :: complete_linorder) \<Longrightarrow> eventually (\<lambda>x. f x > y) F"
    2.44 +  using le_Liminf_iff[of "Liminf F f" F f] by simp
    2.45 +
    2.46 +lemma Limsup_lessD:
    2.47 +  "y > Limsup F (f :: _ \<Rightarrow> 'a :: complete_linorder) \<Longrightarrow> eventually (\<lambda>x. f x < y) F"
    2.48 +  using Limsup_le_iff[of F f "Limsup F f"] by simp
    2.49 +
    2.50  lemma lim_imp_Liminf:
    2.51    fixes f :: "'a \<Rightarrow> _ :: {complete_linorder,linorder_topology}"
    2.52    assumes ntriv: "\<not> trivial_limit F"
    2.53 @@ -327,6 +364,56 @@
    2.54    unfolding continuous_on_eq_continuous_within
    2.55    by (auto simp: continuous_within intro: tendsto_within_subset)
    2.56  
    2.57 +lemma Liminf_compose_continuous_mono:
    2.58 +  fixes f :: "'a::{complete_linorder, linorder_topology} \<Rightarrow> 'b::{complete_linorder, linorder_topology}"
    2.59 +  assumes c: "continuous_on UNIV f" and am: "mono f" and F: "F \<noteq> bot"
    2.60 +  shows "Liminf F (\<lambda>n. f (g n)) = f (Liminf F g)"
    2.61 +proof -
    2.62 +  { fix P assume "eventually P F"
    2.63 +    have "\<exists>x. P x"
    2.64 +    proof (rule ccontr)
    2.65 +      assume "\<not> (\<exists>x. P x)" then have "P = (\<lambda>x. False)"
    2.66 +        by auto
    2.67 +      with \<open>eventually P F\<close> F show False
    2.68 +        by auto
    2.69 +    qed }
    2.70 +  note * = this
    2.71 +
    2.72 +  have "f (Liminf F g) = (SUP P : {P. eventually P F}. f (Inf (g ` Collect P)))"
    2.73 +    unfolding Liminf_def SUP_def
    2.74 +    by (subst continuous_at_Sup_mono[OF am continuous_on_imp_continuous_within[OF c]])
    2.75 +       (auto intro: eventually_True)
    2.76 +  also have "\<dots> = (SUP P : {P. eventually P F}. INFIMUM (g ` Collect P) f)"
    2.77 +    by (intro SUP_cong refl continuous_at_Inf_mono[OF am continuous_on_imp_continuous_within[OF c]])
    2.78 +       (auto dest!: eventually_happens simp: F)
    2.79 +  finally show ?thesis by (auto simp: Liminf_def)
    2.80 +qed
    2.81 +
    2.82 +lemma Limsup_compose_continuous_mono:
    2.83 +  fixes f :: "'a::{complete_linorder, linorder_topology} \<Rightarrow> 'b::{complete_linorder, linorder_topology}"
    2.84 +  assumes c: "continuous_on UNIV f" and am: "mono f" and F: "F \<noteq> bot"
    2.85 +  shows "Limsup F (\<lambda>n. f (g n)) = f (Limsup F g)"
    2.86 +proof -
    2.87 +  { fix P assume "eventually P F"
    2.88 +    have "\<exists>x. P x"
    2.89 +    proof (rule ccontr)
    2.90 +      assume "\<not> (\<exists>x. P x)" then have "P = (\<lambda>x. False)"
    2.91 +        by auto
    2.92 +      with \<open>eventually P F\<close> F show False
    2.93 +        by auto
    2.94 +    qed }
    2.95 +  note * = this
    2.96 +
    2.97 +  have "f (Limsup F g) = (INF P : {P. eventually P F}. f (Sup (g ` Collect P)))"
    2.98 +    unfolding Limsup_def INF_def
    2.99 +    by (subst continuous_at_Inf_mono[OF am continuous_on_imp_continuous_within[OF c]])
   2.100 +       (auto intro: eventually_True)
   2.101 +  also have "\<dots> = (INF P : {P. eventually P F}. SUPREMUM (g ` Collect P) f)"
   2.102 +    by (intro INF_cong refl continuous_at_Sup_mono[OF am continuous_on_imp_continuous_within[OF c]])
   2.103 +       (auto dest!: eventually_happens simp: F)
   2.104 +  finally show ?thesis by (auto simp: Limsup_def)
   2.105 +qed
   2.106 +
   2.107  lemma Liminf_compose_continuous_antimono:
   2.108    fixes f :: "'a::{complete_linorder,linorder_topology} \<Rightarrow> 'b::{complete_linorder,linorder_topology}"
   2.109    assumes c: "continuous_on UNIV f"
   2.110 @@ -351,6 +438,34 @@
   2.111    finally show ?thesis
   2.112      by (auto simp: Liminf_def)
   2.113  qed
   2.114 +
   2.115 +lemma Limsup_compose_continuous_antimono:
   2.116 +  fixes f :: "'a::{complete_linorder, linorder_topology} \<Rightarrow> 'b::{complete_linorder, linorder_topology}"
   2.117 +  assumes c: "continuous_on UNIV f" and am: "antimono f" and F: "F \<noteq> bot"
   2.118 +  shows "Limsup F (\<lambda>n. f (g n)) = f (Liminf F g)"
   2.119 +proof -
   2.120 +  { fix P assume "eventually P F"
   2.121 +    have "\<exists>x. P x"
   2.122 +    proof (rule ccontr)
   2.123 +      assume "\<not> (\<exists>x. P x)" then have "P = (\<lambda>x. False)"
   2.124 +        by auto
   2.125 +      with \<open>eventually P F\<close> F show False
   2.126 +        by auto
   2.127 +    qed }
   2.128 +  note * = this
   2.129 +
   2.130 +  have "f (Liminf F g) = (INF P : {P. eventually P F}. f (Inf (g ` Collect P)))"
   2.131 +    unfolding Liminf_def SUP_def
   2.132 +    by (subst continuous_at_Sup_antimono[OF am continuous_on_imp_continuous_within[OF c]])
   2.133 +       (auto intro: eventually_True)
   2.134 +  also have "\<dots> = (INF P : {P. eventually P F}. SUPREMUM (g ` Collect P) f)"
   2.135 +    by (intro INF_cong refl continuous_at_Inf_antimono[OF am continuous_on_imp_continuous_within[OF c]])
   2.136 +       (auto dest!: eventually_happens simp: F)
   2.137 +  finally show ?thesis
   2.138 +    by (auto simp: Limsup_def)
   2.139 +qed
   2.140 +
   2.141 +
   2.142  subsection \<open>More Limits\<close>
   2.143  
   2.144  lemma convergent_limsup_cl:
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Library/Nonpos_Ints.thy	Mon Jan 04 17:45:36 2016 +0100
     3.3 @@ -0,0 +1,141 @@
     3.4 +(*
     3.5 +  Title:    HOL/Library/Nonpos_Ints.thy
     3.6 +  Author:   Manuel Eberl, TU München
     3.7 +  
     3.8 +  The set of non-positive integers on a ring. (in analogy to the set of non-negative
     3.9 +  integers @{term "\<nat>"}) This is useful e.g. for the Gamma function.
    3.10 +*)
    3.11 +theory Nonpos_Ints
    3.12 +imports Complex_Main
    3.13 +begin
    3.14 +
    3.15 +subsection \<open>Non-positive integers\<close>
    3.16 +
    3.17 +definition nonpos_Ints ("\<int>\<^sub>\<le>\<^sub>0") where "\<int>\<^sub>\<le>\<^sub>0 = {of_int n |n. n \<le> 0}"
    3.18 +
    3.19 +lemma zero_in_nonpos_Ints [simp,intro]: "0 \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.20 +  unfolding nonpos_Ints_def by (auto intro!: exI[of _ "0::int"])
    3.21 +
    3.22 +lemma neg_one_in_nonpos_Ints [simp,intro]: "-1 \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.23 +  unfolding nonpos_Ints_def by (auto intro!: exI[of _ "-1::int"])
    3.24 +
    3.25 +lemma neg_numeral_in_nonpos_Ints [simp,intro]: "-numeral n \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.26 +  unfolding nonpos_Ints_def by (auto intro!: exI[of _ "-numeral n::int"])
    3.27 +
    3.28 +lemma one_notin_nonpos_Ints [simp]: "(1 :: 'a :: ring_char_0) \<notin> \<int>\<^sub>\<le>\<^sub>0"
    3.29 +  by (auto simp: nonpos_Ints_def)
    3.30 +
    3.31 +lemma numeral_notin_nonpos_Ints [simp]: "(numeral n :: 'a :: ring_char_0) \<notin> \<int>\<^sub>\<le>\<^sub>0"
    3.32 +  by (auto simp: nonpos_Ints_def)
    3.33 +
    3.34 +
    3.35 +lemma minus_of_nat_in_nonpos_Ints [simp, intro]: "- of_nat n \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.36 +proof -
    3.37 +  have "- of_nat n = of_int (-int n)" by simp
    3.38 +  also have "-int n \<le> 0" by simp
    3.39 +  hence "of_int (-int n) \<in> \<int>\<^sub>\<le>\<^sub>0" unfolding nonpos_Ints_def by blast
    3.40 +  finally show ?thesis .
    3.41 +qed
    3.42 +
    3.43 +lemma of_nat_in_nonpos_Ints_iff: "(of_nat n :: 'a :: {ring_1,ring_char_0}) \<in> \<int>\<^sub>\<le>\<^sub>0 \<longleftrightarrow> n = 0"
    3.44 +proof
    3.45 +  assume "(of_nat n :: 'a) \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.46 +  then obtain m where "of_nat n = (of_int m :: 'a)" "m \<le> 0" by (auto simp: nonpos_Ints_def)
    3.47 +  hence "(of_int m :: 'a) = of_nat n" by simp
    3.48 +  also have "... = of_int (int n)" by simp
    3.49 +  finally have "m = int n" by (subst (asm) of_int_eq_iff)
    3.50 +  with `m \<le> 0` show "n = 0" by auto
    3.51 +qed simp
    3.52 +
    3.53 +lemma nonpos_Ints_of_int: "n \<le> 0 \<Longrightarrow> of_int n \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.54 +  unfolding nonpos_Ints_def by blast
    3.55 +
    3.56 +lemma nonpos_IntsI: 
    3.57 +  "x \<in> \<int> \<Longrightarrow> x \<le> 0 \<Longrightarrow> (x :: 'a :: linordered_idom) \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.58 +  using assms unfolding nonpos_Ints_def Ints_def by auto
    3.59 +
    3.60 +lemma nonpos_Ints_subset_Ints: "\<int>\<^sub>\<le>\<^sub>0 \<subseteq> \<int>"
    3.61 +  unfolding nonpos_Ints_def Ints_def by blast
    3.62 +
    3.63 +lemma nonpos_Ints_nonpos [dest]: "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x \<le> (0 :: 'a :: linordered_idom)"
    3.64 +  unfolding nonpos_Ints_def by auto
    3.65 +
    3.66 +lemma nonpos_Ints_Int [dest]: "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x \<in> \<int>"
    3.67 +  unfolding nonpos_Ints_def Ints_def by blast
    3.68 +
    3.69 +lemma nonpos_Ints_cases:
    3.70 +  assumes "x \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.71 +  obtains n where "x = of_int n" "n \<le> 0"
    3.72 +  using assms unfolding nonpos_Ints_def by (auto elim!: Ints_cases)
    3.73 +
    3.74 +lemma nonpos_Ints_cases':
    3.75 +  assumes "x \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.76 +  obtains n where "x = -of_nat n"
    3.77 +proof -
    3.78 +  from assms obtain m where "x = of_int m" and m: "m \<le> 0" by (auto elim!: nonpos_Ints_cases)
    3.79 +  hence "x = - of_int (-m)" by auto
    3.80 +  also from m have "(of_int (-m) :: 'a) = of_nat (nat (-m))" by simp_all
    3.81 +  finally show ?thesis by (rule that)
    3.82 +qed
    3.83 +
    3.84 +lemma of_real_in_nonpos_Ints_iff: "(of_real x :: 'a :: real_algebra_1) \<in> \<int>\<^sub>\<le>\<^sub>0 \<longleftrightarrow> x \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.85 +proof
    3.86 +  assume "of_real x \<in> (\<int>\<^sub>\<le>\<^sub>0 :: 'a set)"
    3.87 +  then obtain n where "(of_real x :: 'a) = of_int n" "n \<le> 0" by (erule nonpos_Ints_cases)
    3.88 +  note `of_real x = of_int n`
    3.89 +  also have "of_int n = of_real (of_int n)" by (rule of_real_of_int_eq [symmetric])
    3.90 +  finally have "x = of_int n" by (subst (asm) of_real_eq_iff)
    3.91 +  with `n \<le> 0` show "x \<in> \<int>\<^sub>\<le>\<^sub>0" by (simp add: nonpos_Ints_of_int)
    3.92 +qed (auto elim!: nonpos_Ints_cases intro!: nonpos_Ints_of_int)
    3.93 +
    3.94 +lemma nonpos_Ints_altdef: "\<int>\<^sub>\<le>\<^sub>0 = {n \<in> \<int>. (n :: 'a :: linordered_idom) \<le> 0}"
    3.95 +  by (auto intro!: nonpos_IntsI elim!: nonpos_Ints_cases)
    3.96 +
    3.97 +lemma uminus_in_Nats_iff: "-x \<in> \<nat> \<longleftrightarrow> x \<in> \<int>\<^sub>\<le>\<^sub>0"
    3.98 +proof
    3.99 +  assume "-x \<in> \<nat>"
   3.100 +  then obtain n where "n \<ge> 0" "-x = of_int n" by (auto simp: Nats_altdef1)
   3.101 +  hence "-n \<le> 0" "x = of_int (-n)" by (simp_all add: eq_commute minus_equation_iff[of x])
   3.102 +  thus "x \<in> \<int>\<^sub>\<le>\<^sub>0" unfolding nonpos_Ints_def by blast
   3.103 +next
   3.104 +  assume "x \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.105 +  then obtain n where "n \<le> 0" "x = of_int n" by (auto simp: nonpos_Ints_def)
   3.106 +  hence "-n \<ge> 0" "-x = of_int (-n)" by (simp_all add: eq_commute minus_equation_iff[of x])
   3.107 +  thus "-x \<in> \<nat>" unfolding Nats_altdef1 by blast
   3.108 +qed
   3.109 +
   3.110 +lemma uminus_in_nonpos_Ints_iff: "-x \<in> \<int>\<^sub>\<le>\<^sub>0 \<longleftrightarrow> x \<in> \<nat>"
   3.111 +  using uminus_in_Nats_iff[of "-x"] by simp
   3.112 +
   3.113 +lemma nonpos_Ints_mult: "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> y \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x * y \<in> \<nat>"
   3.114 +  using Nats_mult[of "-x" "-y"] by (simp add: uminus_in_Nats_iff)
   3.115 +
   3.116 +lemma Nats_mult_nonpos_Ints: "x \<in> \<nat> \<Longrightarrow> y \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x * y \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.117 +  using Nats_mult[of x "-y"] by (simp add: uminus_in_Nats_iff)
   3.118 +
   3.119 +lemma nonpos_Ints_mult_Nats:
   3.120 +  "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> y \<in> \<nat> \<Longrightarrow> x * y \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.121 +  using Nats_mult[of "-x" y] by (simp add: uminus_in_Nats_iff)
   3.122 +
   3.123 +lemma nonpos_Ints_add:
   3.124 +  "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> y \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x + y \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.125 +  using Nats_add[of "-x" "-y"] uminus_in_Nats_iff[of "y+x", simplified minus_add] 
   3.126 +  by (simp add: uminus_in_Nats_iff add.commute)
   3.127 +
   3.128 +lemma nonpos_Ints_diff_Nats:
   3.129 +  "x \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> y \<in> \<nat> \<Longrightarrow> x - y \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.130 +  using Nats_add[of "-x" "y"] uminus_in_Nats_iff[of "x-y", simplified minus_add] 
   3.131 +  by (simp add: uminus_in_Nats_iff add.commute)
   3.132 +
   3.133 +lemma Nats_diff_nonpos_Ints:
   3.134 +  "x \<in> \<nat> \<Longrightarrow> y \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> x - y \<in> \<nat>"
   3.135 +  using Nats_add[of "x" "-y"] by (simp add: uminus_in_Nats_iff add.commute)
   3.136 +
   3.137 +lemma plus_of_nat_eq_0_imp: "z + of_nat n = 0 \<Longrightarrow> z \<in> \<int>\<^sub>\<le>\<^sub>0"
   3.138 +proof -
   3.139 +  assume "z + of_nat n = 0"
   3.140 +  hence A: "z = - of_nat n" by (simp add: eq_neg_iff_add_eq_0)
   3.141 +  show "z \<in> \<int>\<^sub>\<le>\<^sub>0" by (subst A) simp
   3.142 +qed
   3.143 +
   3.144 +end
   3.145 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Library/Periodic_Fun.thy	Mon Jan 04 17:45:36 2016 +0100
     4.3 @@ -0,0 +1,130 @@
     4.4 +(*
     4.5 +  Title:    HOL/Library/Periodic_Fun.thy
     4.6 +  Author:   Manuel Eberl, TU München
     4.7 +  
     4.8 +  A locale for periodic functions. The idea is that one proves $f(x + p) = f(x)$
     4.9 +  for some period $p$ and gets derived results like $f(x - p) = f(x)$ and $f(x + 2p) = f(x)$
    4.10 +  for free.
    4.11 +*)
    4.12 +theory Periodic_Fun
    4.13 +imports Complex_Main
    4.14 +begin
    4.15 +
    4.16 +text \<open>
    4.17 +  @{term g} and @{term gm} are ``plus/minus k periods'' functions. 
    4.18 +  @{term g1} and @{term gn1} are ``plus/minus one period'' functions.
    4.19 +  This is useful e.g. if the period is one; the lemmas one gets are then 
    4.20 +  @{term "f (x + 1) = f x"} instead of @{term "f (x + 1 * 1) = f x"} etc.
    4.21 +\<close>
    4.22 +locale periodic_fun = 
    4.23 +  fixes f :: "('a :: {ring_1}) \<Rightarrow> 'b" and g gm :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" and g1 gn1 :: "'a \<Rightarrow> 'a"
    4.24 +  assumes plus_1: "f (g1 x) = f x"
    4.25 +  assumes periodic_arg_plus_0: "g x 0 = x"
    4.26 +  assumes periodic_arg_plus_distrib: "g x (of_int (m + n)) = g (g x (of_int n)) (of_int m)"
    4.27 +  assumes plus_1_eq: "g x 1 = g1 x" and minus_1_eq: "g x (-1) = gn1 x" 
    4.28 +          and minus_eq: "g x (-y) = gm x y"
    4.29 +begin
    4.30 +
    4.31 +lemma plus_of_nat: "f (g x (of_nat n)) = f x"
    4.32 +  by (induction n) (insert periodic_arg_plus_distrib[of _ 1 "int n" for n], 
    4.33 +                    simp_all add: plus_1 periodic_arg_plus_0 plus_1_eq)
    4.34 +
    4.35 +lemma minus_of_nat: "f (gm x (of_nat n)) = f x"
    4.36 +proof -
    4.37 +  have "f (g x (- of_nat n)) = f (g (g x (- of_nat n)) (of_nat n))"
    4.38 +    by (rule plus_of_nat[symmetric])
    4.39 +  also have "\<dots> = f (g (g x (of_int (- of_nat n))) (of_int (of_nat n)))" by simp
    4.40 +  also have "\<dots> = f x" 
    4.41 +    by (subst periodic_arg_plus_distrib [symmetric]) (simp add: periodic_arg_plus_0)
    4.42 +  finally show ?thesis by (simp add: minus_eq)
    4.43 +qed
    4.44 +
    4.45 +lemma plus_of_int: "f (g x (of_int n)) = f x"
    4.46 +  by (induction n) (simp_all add: plus_of_nat minus_of_nat minus_eq del: of_nat_Suc)
    4.47 +
    4.48 +lemma minus_of_int: "f (gm x (of_int n)) = f x"
    4.49 +  using plus_of_int[of x "of_int (-n)"] by (simp add: minus_eq)
    4.50 +
    4.51 +lemma plus_numeral: "f (g x (numeral n)) = f x"
    4.52 +  by (subst of_nat_numeral[symmetric], subst plus_of_nat) (rule refl)
    4.53 +
    4.54 +lemma minus_numeral: "f (gm x (numeral n)) = f x"
    4.55 +  by (subst of_nat_numeral[symmetric], subst minus_of_nat) (rule refl)
    4.56 +
    4.57 +lemma minus_1: "f (gn1 x) = f x"
    4.58 +  using minus_of_nat[of x 1] by (simp add: minus_1_eq minus_eq[symmetric])
    4.59 +
    4.60 +lemmas periodic_simps = plus_of_nat minus_of_nat plus_of_int minus_of_int 
    4.61 +                        plus_numeral minus_numeral plus_1 minus_1
    4.62 +
    4.63 +end
    4.64 +
    4.65 +
    4.66 +text \<open>
    4.67 +  Specialised case of the @{term periodic_fun} locale for periods that are not 1.
    4.68 +  Gives lemmas @{term "f (x - period) = f x"} etc.
    4.69 +\<close>
    4.70 +locale periodic_fun_simple = 
    4.71 +  fixes f :: "('a :: {ring_1}) \<Rightarrow> 'b" and period :: 'a
    4.72 +  assumes plus_period: "f (x + period) = f x"
    4.73 +begin
    4.74 +sublocale periodic_fun f "\<lambda>z x. z + x * period" "\<lambda>z x. z - x * period" 
    4.75 +  "\<lambda>z. z + period" "\<lambda>z. z - period"
    4.76 +  by standard (simp_all add: ring_distribs plus_period)
    4.77 +end
    4.78 +
    4.79 +
    4.80 +text \<open>
    4.81 +  Specialised case of the @{term periodic_fun} locale for period 1.
    4.82 +  Gives lemmas @{term "f (x - 1) = f x"} etc.
    4.83 +\<close>
    4.84 +locale periodic_fun_simple' = 
    4.85 +  fixes f :: "('a :: {ring_1}) \<Rightarrow> 'b"
    4.86 +  assumes plus_period: "f (x + 1) = f x"
    4.87 +begin
    4.88 +sublocale periodic_fun f "\<lambda>z x. z + x" "\<lambda>z x. z - x" "\<lambda>z. z + 1" "\<lambda>z. z - 1"
    4.89 +  by standard (simp_all add: ring_distribs plus_period)
    4.90 +
    4.91 +lemma of_nat: "f (of_nat n) = f 0" using plus_of_nat[of 0 n] by simp
    4.92 +lemma uminus_of_nat: "f (-of_nat n) = f 0" using minus_of_nat[of 0 n] by simp
    4.93 +lemma of_int: "f (of_int n) = f 0" using plus_of_int[of 0 n] by simp
    4.94 +lemma uminus_of_int: "f (-of_int n) = f 0" using minus_of_int[of 0 n] by simp
    4.95 +lemma of_numeral: "f (numeral n) = f 0" using plus_numeral[of 0 n] by simp
    4.96 +lemma of_neg_numeral: "f (-numeral n) = f 0" using minus_numeral[of 0 n] by simp
    4.97 +lemma of_1: "f 1 = f 0" using plus_of_nat[of 0 1] by simp
    4.98 +lemma of_neg_1: "f (-1) = f 0" using minus_of_nat[of 0 1] by simp
    4.99 +
   4.100 +lemmas periodic_simps' = 
   4.101 +  of_nat uminus_of_nat of_int uminus_of_int of_numeral of_neg_numeral of_1 of_neg_1
   4.102 +
   4.103 +end
   4.104 +
   4.105 +lemma sin_plus_pi: "sin ((z :: 'a :: {real_normed_field,banach}) + of_real pi) = - sin z"
   4.106 +  by (simp add: sin_add)
   4.107 +  
   4.108 +lemma cos_plus_pi: "cos ((z :: 'a :: {real_normed_field,banach}) + of_real pi) = - cos z"
   4.109 +  by (simp add: cos_add)
   4.110 +
   4.111 +interpretation sin: periodic_fun_simple sin "2 * of_real pi :: 'a :: {real_normed_field,banach}"
   4.112 +proof
   4.113 +  fix z :: 'a
   4.114 +  have "sin (z + 2 * of_real pi) = sin (z + of_real pi + of_real pi)" by (simp add: ac_simps)
   4.115 +  also have "\<dots> = sin z" by (simp only: sin_plus_pi) simp
   4.116 +  finally show "sin (z + 2 * of_real pi) = sin z" .
   4.117 +qed
   4.118 +
   4.119 +interpretation cos: periodic_fun_simple cos "2 * of_real pi :: 'a :: {real_normed_field,banach}"
   4.120 +proof
   4.121 +  fix z :: 'a
   4.122 +  have "cos (z + 2 * of_real pi) = cos (z + of_real pi + of_real pi)" by (simp add: ac_simps)
   4.123 +  also have "\<dots> = cos z" by (simp only: cos_plus_pi) simp
   4.124 +  finally show "cos (z + 2 * of_real pi) = cos z" .
   4.125 +qed
   4.126 +
   4.127 +interpretation tan: periodic_fun_simple tan "2 * of_real pi :: 'a :: {real_normed_field,banach}"
   4.128 +  by standard (simp only: tan_def [abs_def] sin.plus_1 cos.plus_1)
   4.129 +
   4.130 +interpretation cot: periodic_fun_simple cot "2 * of_real pi :: 'a :: {real_normed_field,banach}"
   4.131 +  by standard (simp only: cot_def [abs_def] sin.plus_1 cos.plus_1)
   4.132 +  
   4.133 +end
   4.134 \ No newline at end of file
     5.1 --- a/src/HOL/Multivariate_Analysis/Complex_Transcendental.thy	Sun Jan 03 21:45:34 2016 +0100
     5.2 +++ b/src/HOL/Multivariate_Analysis/Complex_Transcendental.thy	Mon Jan 04 17:45:36 2016 +0100
     5.3 @@ -3,9 +3,32 @@
     5.4  text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
     5.5  
     5.6  theory Complex_Transcendental
     5.7 -imports Complex_Analysis_Basics
     5.8 +imports 
     5.9 +  Complex_Analysis_Basics
    5.10 +  Summation
    5.11  begin
    5.12  
    5.13 +(* TODO: Figure out what to do with Möbius transformations *)
    5.14 +definition "moebius a b c d = (\<lambda>z. (a*z+b) / (c*z+d :: 'a :: field))"
    5.15 +
    5.16 +lemma moebius_inverse: 
    5.17 +  assumes "a * d \<noteq> b * c" "c * z + d \<noteq> 0"
    5.18 +  shows   "moebius d (-b) (-c) a (moebius a b c d z) = z"
    5.19 +proof -
    5.20 +  from assms have "(-c) * moebius a b c d z + a \<noteq> 0" unfolding moebius_def
    5.21 +    by (simp add: field_simps)
    5.22 +  with assms show ?thesis
    5.23 +    unfolding moebius_def by (simp add: moebius_def divide_simps) (simp add: algebra_simps)?
    5.24 +qed
    5.25 +
    5.26 +lemma moebius_inverse': 
    5.27 +  assumes "a * d \<noteq> b * c" "c * z - a \<noteq> 0"
    5.28 +  shows   "moebius a b c d (moebius d (-b) (-c) a z) = z"
    5.29 +  using assms moebius_inverse[of d a "-b" "-c" z]
    5.30 +  by (auto simp: algebra_simps)
    5.31 +
    5.32 +
    5.33 +
    5.34  lemma cmod_add_real_less:
    5.35    assumes "Im z \<noteq> 0" "r\<noteq>0"
    5.36      shows "cmod (z + r) < cmod z + \<bar>r\<bar>"
    5.37 @@ -1328,6 +1351,88 @@
    5.38      done
    5.39  qed
    5.40  
    5.41 +lemma Ln_series:
    5.42 +  fixes z :: complex
    5.43 +  assumes "norm z < 1"
    5.44 +  shows   "(\<lambda>n. (-1)^Suc n / of_nat n * z^n) sums ln (1 + z)" (is "(\<lambda>n. ?f n * z^n) sums _")
    5.45 +proof -
    5.46 +  let ?F = "\<lambda>z. \<Sum>n. ?f n * z^n" and ?F' = "\<lambda>z. \<Sum>n. diffs ?f n * z^n"
    5.47 +  have r: "conv_radius ?f = 1"
    5.48 +    by (intro conv_radius_ratio_limit_nonzero[of _ 1])
    5.49 +       (simp_all add: norm_divide LIMSEQ_Suc_n_over_n del: of_nat_Suc)
    5.50 +
    5.51 +  have "\<exists>c. \<forall>z\<in>ball 0 1. ln (1 + z) - ?F z = c"
    5.52 +  proof (rule has_field_derivative_zero_constant)
    5.53 +    fix z :: complex assume z': "z \<in> ball 0 1"
    5.54 +    hence z: "norm z < 1" by (simp add: dist_0_norm)
    5.55 +    def t \<equiv> "of_real (1 + norm z) / 2 :: complex"
    5.56 +    from z have t: "norm z < norm t" "norm t < 1" unfolding t_def
    5.57 +      by (simp_all add: field_simps norm_divide del: of_real_add)
    5.58 +
    5.59 +    have "Re (-z) \<le> norm (-z)" by (rule complex_Re_le_cmod)
    5.60 +    also from z have "... < 1" by simp
    5.61 +    finally have "((\<lambda>z. ln (1 + z)) has_field_derivative inverse (1+z)) (at z)"
    5.62 +      by (auto intro!: derivative_eq_intros)
    5.63 +    moreover have "(?F has_field_derivative ?F' z) (at z)" using t r
    5.64 +      by (intro termdiffs_strong[of _ t] summable_in_conv_radius) simp_all
    5.65 +    ultimately have "((\<lambda>z. ln (1 + z) - ?F z) has_field_derivative (inverse (1 + z) - ?F' z)) 
    5.66 +                       (at z within ball 0 1)"
    5.67 +      by (intro derivative_intros) (simp_all add: at_within_open[OF z'])
    5.68 +    also have "(\<lambda>n. of_nat n * ?f n * z ^ (n - Suc 0)) sums ?F' z" using t r
    5.69 +      by (intro diffs_equiv termdiff_converges[OF t(1)] summable_in_conv_radius) simp_all
    5.70 +    from sums_split_initial_segment[OF this, of 1]
    5.71 +      have "(\<lambda>i. (-z) ^ i) sums ?F' z" by (simp add: power_minus[of z] del: of_nat_Suc)
    5.72 +    hence "?F' z = inverse (1 + z)" using z by (simp add: sums_iff suminf_geometric divide_inverse)
    5.73 +    also have "inverse (1 + z) - inverse (1 + z) = 0" by simp
    5.74 +    finally show "((\<lambda>z. ln (1 + z) - ?F z) has_field_derivative 0) (at z within ball 0 1)" .
    5.75 +  qed simp_all
    5.76 +  then obtain c where c: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> ln (1 + z) - ?F z = c" by blast
    5.77 +  from c[of 0] have "c = 0" by (simp only: powser_zero) simp
    5.78 +  with c[of z] assms have "ln (1 + z) = ?F z" by (simp add: dist_0_norm)
    5.79 +  moreover have "summable (\<lambda>n. ?f n * z^n)" using assms r
    5.80 +    by (intro summable_in_conv_radius) simp_all
    5.81 +  ultimately show ?thesis by (simp add: sums_iff)
    5.82 +qed
    5.83 +
    5.84 +lemma Ln_approx_linear:
    5.85 +  fixes z :: complex
    5.86 +  assumes "norm z < 1"
    5.87 +  shows   "norm (ln (1 + z) - z) \<le> norm z^2 / (1 - norm z)"
    5.88 +proof -
    5.89 +  let ?f = "\<lambda>n. (-1)^Suc n / of_nat n"
    5.90 +  from assms have "(\<lambda>n. ?f n * z^n) sums ln (1 + z)" using Ln_series by simp
    5.91 +  moreover have "(\<lambda>n. (if n = 1 then 1 else 0) * z^n) sums z" using powser_sums_if[of 1] by simp
    5.92 +  ultimately have "(\<lambda>n. (?f n - (if n = 1 then 1 else 0)) * z^n) sums (ln (1 + z) - z)"
    5.93 +    by (subst left_diff_distrib, intro sums_diff) simp_all
    5.94 +  from sums_split_initial_segment[OF this, of "Suc 1"]
    5.95 +    have "(\<lambda>i. (-(z^2)) * inverse (2 + of_nat i) * (- z)^i) sums (Ln (1 + z) - z)"
    5.96 +    by (simp add: power2_eq_square mult_ac power_minus[of z] divide_inverse)
    5.97 +  hence "(Ln (1 + z) - z) = (\<Sum>i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i)"
    5.98 +    by (simp add: sums_iff)
    5.99 +  also have A: "summable (\<lambda>n. norm z^2 * (inverse (real_of_nat (Suc (Suc n))) * cmod z ^ n))"
   5.100 +    by (rule summable_mult, rule summable_comparison_test_ev[OF _ summable_geometric[of "norm z"]])
   5.101 +       (auto simp: assms field_simps intro!: always_eventually)
   5.102 +  hence "norm (\<Sum>i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i) \<le> 
   5.103 +             (\<Sum>i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i))"
   5.104 +    by (intro summable_norm)
   5.105 +       (auto simp: norm_power norm_inverse norm_mult mult_ac simp del: of_nat_add of_nat_Suc)
   5.106 +  also have "norm ((-z)^2 * (-z)^i) * inverse (of_nat (i+2)) \<le> norm ((-z)^2 * (-z)^i) * 1" for i
   5.107 +    by (intro mult_left_mono) (simp_all add: divide_simps)
   5.108 +  hence "(\<Sum>i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i)) \<le> 
   5.109 +           (\<Sum>i. norm (-(z^2) * (-z)^i))" using A assms
   5.110 +    apply (simp_all only: norm_power norm_inverse norm_divide norm_mult)
   5.111 +    apply (intro suminf_le summable_mult summable_geometric)
   5.112 +    apply (auto simp: norm_power field_simps simp del: of_nat_add of_nat_Suc)
   5.113 +    done
   5.114 +  also have "... = norm z^2 * (\<Sum>i. norm z^i)" using assms
   5.115 +    by (subst suminf_mult [symmetric]) (auto intro!: summable_geometric simp: norm_mult norm_power)
   5.116 +  also have "(\<Sum>i. norm z^i) = inverse (1 - norm z)" using assms
   5.117 +    by (subst suminf_geometric) (simp_all add: divide_inverse)
   5.118 +  also have "norm z^2 * ... = norm z^2 / (1 - norm z)" by (simp add: divide_inverse)
   5.119 +  finally show ?thesis .
   5.120 +qed
   5.121 +
   5.122 +
   5.123  text\<open>Relation between Arg and arctangent in upper halfplane\<close>
   5.124  lemma Arg_arctan_upperhalf:
   5.125    assumes "0 < Im z"
   5.126 @@ -1806,6 +1911,23 @@
   5.127  definition Arctan :: "complex \<Rightarrow> complex" where
   5.128      "Arctan \<equiv> \<lambda>z. (\<i>/2) * Ln((1 - \<i>*z) / (1 + \<i>*z))"
   5.129  
   5.130 +lemma Arctan_def_moebius: "Arctan z = \<i>/2 * Ln (moebius (-\<i>) 1 \<i> 1 z)"
   5.131 +  by (simp add: Arctan_def moebius_def add_ac)
   5.132 +
   5.133 +lemma Ln_conv_Arctan:
   5.134 +  assumes "z \<noteq> -1"
   5.135 +  shows   "Ln z = -2*\<i> * Arctan (moebius 1 (- 1) (- \<i>) (- \<i>) z)"
   5.136 +proof -
   5.137 +  have "Arctan (moebius 1 (- 1) (- \<i>) (- \<i>) z) =
   5.138 +             \<i>/2 * Ln (moebius (- \<i>) 1 \<i> 1 (moebius 1 (- 1) (- \<i>) (- \<i>) z))"
   5.139 +    by (simp add: Arctan_def_moebius)
   5.140 +  also from assms have "\<i> * z \<noteq> \<i> * (-1)" by (subst mult_left_cancel) simp
   5.141 +  hence "\<i> * z - -\<i> \<noteq> 0" by (simp add: eq_neg_iff_add_eq_0)
   5.142 +  from moebius_inverse'[OF _ this, of 1 1]
   5.143 +    have "moebius (- \<i>) 1 \<i> 1 (moebius 1 (- 1) (- \<i>) (- \<i>) z) = z" by simp
   5.144 +  finally show ?thesis by (simp add: field_simps)
   5.145 +qed
   5.146 +
   5.147  lemma Arctan_0 [simp]: "Arctan 0 = 0"
   5.148    by (simp add: Arctan_def)
   5.149  
   5.150 @@ -1930,6 +2052,108 @@
   5.151      "(\<And>z. z \<in> s \<Longrightarrow> Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> Arctan holomorphic_on s"
   5.152    by (simp add: complex_differentiable_within_Arctan holomorphic_on_def)
   5.153  
   5.154 +lemma Arctan_series:
   5.155 +  assumes z: "norm (z :: complex) < 1"
   5.156 +  defines "g \<equiv> \<lambda>n. if odd n then -\<i>*\<i>^n / n else 0"
   5.157 +  defines "h \<equiv> \<lambda>z n. (-1)^n / of_nat (2*n+1) * (z::complex)^(2*n+1)"
   5.158 +  shows   "(\<lambda>n. g n * z^n) sums Arctan z"
   5.159 +  and     "h z sums Arctan z"
   5.160 +proof -
   5.161 +  def G \<equiv> "\<lambda>z. (\<Sum>n. g n * z^n)"
   5.162 +  have summable: "summable (\<lambda>n. g n * u^n)" if "norm u < 1" for u
   5.163 +  proof (cases "u = 0")
   5.164 +    assume u: "u \<noteq> 0"
   5.165 +    have "(\<lambda>n. ereal (norm (h u n) / norm (h u (Suc n)))) = (\<lambda>n. ereal (inverse (norm u)^2) * 
   5.166 +              ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n)))))"
   5.167 +    proof
   5.168 +      fix n
   5.169 +      have "ereal (norm (h u n) / norm (h u (Suc n))) = 
   5.170 +             ereal (inverse (norm u)^2) * ereal ((of_nat (2*Suc n+1) / of_nat (Suc n)) / 
   5.171 +                 (of_nat (2*Suc n-1) / of_nat (Suc n)))"
   5.172 +      by (simp add: h_def norm_mult norm_power norm_divide divide_simps 
   5.173 +                    power2_eq_square eval_nat_numeral del: of_nat_add of_nat_Suc)
   5.174 +      also have "of_nat (2*Suc n+1) / of_nat (Suc n) = (2::real) + inverse (real (Suc n))"
   5.175 +        by (auto simp: divide_simps simp del: of_nat_Suc) simp_all?
   5.176 +      also have "of_nat (2*Suc n-1) / of_nat (Suc n) = (2::real) - inverse (real (Suc n))"
   5.177 +        by (auto simp: divide_simps simp del: of_nat_Suc) simp_all?      
   5.178 +      finally show "ereal (norm (h u n) / norm (h u (Suc n))) = ereal (inverse (norm u)^2) * 
   5.179 +              ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n))))" .
   5.180 +    qed
   5.181 +    also have "\<dots> \<longlonglongrightarrow> ereal (inverse (norm u)^2) * ereal ((2 + 0) / (2 - 0))"
   5.182 +      by (intro tendsto_intros LIMSEQ_inverse_real_of_nat) simp_all
   5.183 +    finally have "liminf (\<lambda>n. ereal (cmod (h u n) / cmod (h u (Suc n)))) = inverse (norm u)^2"
   5.184 +      by (intro lim_imp_Liminf) simp_all
   5.185 +    moreover from power_strict_mono[OF that, of 2] u have "inverse (norm u)^2 > 1"
   5.186 +      by (simp add: divide_simps)
   5.187 +    ultimately have A: "liminf (\<lambda>n. ereal (cmod (h u n) / cmod (h u (Suc n)))) > 1" by simp
   5.188 +    from u have "summable (h u)"
   5.189 +      by (intro summable_norm_cancel[OF ratio_test_convergence[OF _ A]])
   5.190 +         (auto simp: h_def norm_divide norm_mult norm_power simp del: of_nat_Suc 
   5.191 +               intro!: mult_pos_pos divide_pos_pos always_eventually)
   5.192 +    thus "summable (\<lambda>n. g n * u^n)"
   5.193 +      by (subst summable_mono_reindex[of "\<lambda>n. 2*n+1", symmetric])
   5.194 +         (auto simp: power_mult subseq_def g_def h_def elim!: oddE)
   5.195 +  qed (simp add: h_def)
   5.196 +
   5.197 +  have "\<exists>c. \<forall>u\<in>ball 0 1. Arctan u - G u = c"
   5.198 +  proof (rule has_field_derivative_zero_constant)
   5.199 +    fix u :: complex assume "u \<in> ball 0 1"
   5.200 +    hence u: "norm u < 1" by (simp add: dist_0_norm)
   5.201 +    def K \<equiv> "(norm u + 1) / 2"
   5.202 +    from u and abs_Im_le_cmod[of u] have Im_u: "\<bar>Im u\<bar> < 1" by linarith
   5.203 +    from u have K: "0 \<le> K" "norm u < K" "K < 1" by (simp_all add: K_def)
   5.204 +    hence "(G has_field_derivative (\<Sum>n. diffs g n * u ^ n)) (at u)" unfolding G_def
   5.205 +      by (intro termdiffs_strong[of _ "of_real K"] summable) simp_all
   5.206 +    also have "(\<lambda>n. diffs g n * u^n) = (\<lambda>n. if even n then (\<i>*u)^n else 0)"
   5.207 +      by (intro ext) (simp_all del: of_nat_Suc add: g_def diffs_def power_mult_distrib)
   5.208 +    also have "suminf \<dots> = (\<Sum>n. (-(u^2))^n)"
   5.209 +      by (subst suminf_mono_reindex[of "\<lambda>n. 2*n", symmetric]) 
   5.210 +         (auto elim!: evenE simp: subseq_def power_mult power_mult_distrib)
   5.211 +    also from u have "norm u^2 < 1^2" by (intro power_strict_mono) simp_all
   5.212 +    hence "(\<Sum>n. (-(u^2))^n) = inverse (1 + u^2)" 
   5.213 +      by (subst suminf_geometric) (simp_all add: norm_power inverse_eq_divide)
   5.214 +    finally have "(G has_field_derivative inverse (1 + u\<^sup>2)) (at u)" .
   5.215 +    from DERIV_diff[OF has_field_derivative_Arctan this] Im_u u
   5.216 +      show "((\<lambda>u. Arctan u - G u) has_field_derivative 0) (at u within ball 0 1)"
   5.217 +      by (simp_all add: dist_0_norm at_within_open[OF _ open_ball])
   5.218 +  qed simp_all
   5.219 +  then obtain c where c: "\<And>u. norm u < 1 \<Longrightarrow> Arctan u - G u = c" by (auto simp: dist_0_norm)
   5.220 +  from this[of 0] have "c = 0" by (simp add: G_def g_def powser_zero)
   5.221 +  with c z have "Arctan z = G z" by simp
   5.222 +  with summable[OF z] show "(\<lambda>n. g n * z^n) sums Arctan z" unfolding G_def by (simp add: sums_iff)
   5.223 +  thus "h z sums Arctan z" by (subst (asm) sums_mono_reindex[of "\<lambda>n. 2*n+1", symmetric])
   5.224 +                              (auto elim!: oddE simp: subseq_def power_mult g_def h_def)
   5.225 +qed
   5.226 +
   5.227 +text \<open>A quickly-converging series for the logarithm, based on the arctangent.\<close>
   5.228 +lemma ln_series_quadratic:
   5.229 +  assumes x: "x > (0::real)"
   5.230 +  shows "(\<lambda>n. (2*((x - 1) / (x + 1)) ^ (2*n+1) / of_nat (2*n+1))) sums ln x"
   5.231 +proof -
   5.232 +  def y \<equiv> "of_real ((x-1)/(x+1)) :: complex"
   5.233 +  from x have x': "complex_of_real x \<noteq> of_real (-1)"  by (subst of_real_eq_iff) auto
   5.234 +  from x have "\<bar>x - 1\<bar> < \<bar>x + 1\<bar>" by linarith
   5.235 +  hence "norm (complex_of_real (x - 1) / complex_of_real (x + 1)) < 1"
   5.236 +    by (simp add: norm_divide del: of_real_add of_real_diff)
   5.237 +  hence "norm (\<i> * y) < 1" unfolding y_def by (subst norm_mult) simp
   5.238 +  hence "(\<lambda>n. (-2*\<i>) * ((-1)^n / of_nat (2*n+1) * (\<i>*y)^(2*n+1))) sums ((-2*\<i>) * Arctan (\<i>*y))"
   5.239 +    by (intro Arctan_series sums_mult) simp_all
   5.240 +  also have "(\<lambda>n. (-2*\<i>) * ((-1)^n / of_nat (2*n+1) * (\<i>*y)^(2*n+1))) = 
   5.241 +                 (\<lambda>n. (-2*\<i>) * ((-1)^n * (\<i>*y*(-y\<^sup>2)^n)/of_nat (2*n+1)))"
   5.242 +    by (intro ext) (simp_all add: power_mult power_mult_distrib)
   5.243 +  also have "\<dots> = (\<lambda>n. 2*y* ((-1) * (-y\<^sup>2))^n/of_nat (2*n+1))"
   5.244 +    by (intro ext, subst power_mult_distrib) (simp add: algebra_simps power_mult)
   5.245 +  also have "\<dots> = (\<lambda>n. 2*y^(2*n+1) / of_nat (2*n+1))" 
   5.246 +    by (subst power_add, subst power_mult) (simp add: mult_ac)
   5.247 +  also have "\<dots> = (\<lambda>n. of_real (2*((x-1)/(x+1))^(2*n+1) / of_nat (2*n+1)))"
   5.248 +    by (intro ext) (simp add: y_def)
   5.249 +  also have "\<i> * y = (of_real x - 1) / (-\<i> * (of_real x + 1))"
   5.250 +    by (subst divide_divide_eq_left [symmetric]) (simp add: y_def)
   5.251 +  also have "\<dots> = moebius 1 (-1) (-\<i>) (-\<i>) (of_real x)" by (simp add: moebius_def algebra_simps)
   5.252 +  also from x' have "-2*\<i>*Arctan \<dots> = Ln (of_real x)" by (intro Ln_conv_Arctan [symmetric]) simp_all
   5.253 +  also from x have "\<dots> = ln x" by (rule Ln_of_real)
   5.254 +  finally show ?thesis by (subst (asm) sums_of_real_iff)
   5.255 +qed
   5.256  
   5.257  subsection \<open>Real arctangent\<close>
   5.258  
     6.1 --- a/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Sun Jan 03 21:45:34 2016 +0100
     6.2 +++ b/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Mon Jan 04 17:45:36 2016 +0100
     6.3 @@ -316,46 +316,6 @@
     6.4    apply (metis INF_absorb centre_in_ball)
     6.5    done
     6.6  
     6.7 -lemma continuous_on_inverse_ereal: "continuous_on {0::ereal ..} inverse"
     6.8 -  unfolding continuous_on_def
     6.9 -proof clarsimp
    6.10 -  fix x :: ereal assume "0 \<le> x"
    6.11 -  moreover have "at 0 within {0 ..} = at_right (0::ereal)"
    6.12 -    by (auto simp: filter_eq_iff eventually_at_filter le_less)
    6.13 -  moreover have "0 < x \<Longrightarrow> at x within {0 ..} = at x"
    6.14 -    using at_within_interior[of x "{0 ..}"] by (simp add: interior_Ici[of "- \<infinity>"])
    6.15 -  ultimately show "(inverse \<longlongrightarrow> inverse x) (at x within {0..})"
    6.16 -    by (auto simp: le_less inverse_ereal_tendsto_at_right_0 inverse_ereal_tendsto_pos)
    6.17 -qed
    6.18 -
    6.19 -
    6.20 -lemma Liminf_inverse_ereal:
    6.21 -  assumes nneg: "\<forall>\<^sub>F x in F. f x \<ge> (0 :: ereal)" and "F \<noteq> bot"
    6.22 -  shows "Liminf F (\<lambda>n. inverse (f n)) = inverse (Limsup F f)"
    6.23 -proof -
    6.24 -  def I \<equiv> "\<lambda>x::ereal. if x \<le> 0 then \<infinity> else inverse x"
    6.25 -  have "Liminf F (\<lambda>n. I (f n)) = I (Limsup F f)"
    6.26 -  proof (rule Liminf_compose_continuous_antimono)
    6.27 -    have "continuous_on ({.. 0} \<union> {0 ..}) I"
    6.28 -      unfolding I_def by (intro continuous_on_cases) (auto intro: continuous_on_const continuous_on_inverse_ereal)
    6.29 -    also have "{.. 0} \<union> {0::ereal ..} = UNIV"
    6.30 -      by auto
    6.31 -    finally show "continuous_on UNIV I" .
    6.32 -    show "antimono I"
    6.33 -      unfolding antimono_def I_def by (auto intro: ereal_inverse_antimono)
    6.34 -  qed fact
    6.35 -  also have "Liminf F (\<lambda>n. I (f n)) = Liminf F (\<lambda>n. inverse (f n))"
    6.36 -  proof (rule Liminf_eq)
    6.37 -    show "\<forall>\<^sub>F x in F. I (f x) = inverse (f x)"
    6.38 -      using nneg by eventually_elim (auto simp: I_def)
    6.39 -  qed
    6.40 -  also have "0 \<le> Limsup F f"
    6.41 -    by (intro le_Limsup) fact+
    6.42 -  then have "I (Limsup F f) = inverse (Limsup F f)"
    6.43 -    by (auto simp: I_def)
    6.44 -  finally show ?thesis .
    6.45 -qed
    6.46 -
    6.47  subsection \<open>monoset\<close>
    6.48  
    6.49  definition (in order) mono_set:
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/HOL/Multivariate_Analysis/Gamma.thy	Mon Jan 04 17:45:36 2016 +0100
     7.3 @@ -0,0 +1,2640 @@
     7.4 +(*
     7.5 +  Title:    HOL/Multivariate_Analysis/Gamma.thy
     7.6 +  Author:   Manuel Eberl, TU München
     7.7 +  
     7.8 +  Several equivalent definitions of the Gamma function and its 
     7.9 +  most important properties. Also contains the definition and some properties
    7.10 +  of the log-Gamma function and the Digamma function and the other Polygamma functions.
    7.11 +  
    7.12 +  Based on the Gamma function, we also prove the Weierstraß product form of the
    7.13 +  sin function and, based on this, the solution of the Basel problem (the 
    7.14 +  sum over all @{term "1 / (n::nat)^2"}.
    7.15 +*)
    7.16 +theory Gamma
    7.17 +imports 
    7.18 +  Complex_Transcendental
    7.19 +  Summation
    7.20 +  Harmonic_Numbers
    7.21 +  "~~/src/HOL/Library/Nonpos_Ints"
    7.22 +  "~~/src/HOL/Library/Periodic_Fun"
    7.23 +begin
    7.24 +
    7.25 +lemma pochhammer_eq_0_imp_nonpos_Int: 
    7.26 +  "pochhammer (x::'a::field_char_0) n = 0 \<Longrightarrow> x \<in> \<int>\<^sub>\<le>\<^sub>0"
    7.27 +  by (auto simp: pochhammer_eq_0_iff)
    7.28 +  
    7.29 +lemma closed_nonpos_Ints [simp]: "closed (\<int>\<^sub>\<le>\<^sub>0 :: 'a :: real_normed_algebra_1 set)"
    7.30 +proof -
    7.31 +  have "\<int>\<^sub>\<le>\<^sub>0 = (of_int ` {n. n \<le> 0} :: 'a set)" 
    7.32 +    by (auto elim!: nonpos_Ints_cases intro!: nonpos_Ints_of_int)
    7.33 +  also have "closed \<dots>" by (rule closed_of_int_image)
    7.34 +  finally show ?thesis .
    7.35 +qed
    7.36 +
    7.37 +lemma plus_one_in_nonpos_Ints_imp: "z + 1 \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> z \<in> \<int>\<^sub>\<le>\<^sub>0"
    7.38 +  using nonpos_Ints_diff_Nats[of "z+1" "1"] by simp_all
    7.39 +
    7.40 +lemma fraction_not_in_ints:
    7.41 +  assumes "\<not>(n dvd m)" "n \<noteq> 0"
    7.42 +  shows   "of_int m / of_int n \<notin> (\<int> :: 'a :: {division_ring,ring_char_0} set)"
    7.43 +proof
    7.44 +  assume "of_int m / (of_int n :: 'a) \<in> \<int>"
    7.45 +  then obtain k where "of_int m / of_int n = (of_int k :: 'a)" by (elim Ints_cases)
    7.46 +  with assms have "of_int m = (of_int (k * n) :: 'a)" by (auto simp add: divide_simps)
    7.47 +  hence "m = k * n" by (subst (asm) of_int_eq_iff)
    7.48 +  hence "n dvd m" by simp
    7.49 +  with assms(1) show False by contradiction
    7.50 +qed
    7.51 +
    7.52 +lemma not_in_Ints_imp_not_in_nonpos_Ints: "z \<notin> \<int> \<Longrightarrow> z \<notin> \<int>\<^sub>\<le>\<^sub>0"
    7.53 +  by (auto simp: Ints_def nonpos_Ints_def)
    7.54 +
    7.55 +lemma double_in_nonpos_Ints_imp:
    7.56 +  assumes "2 * (z :: 'a :: field_char_0) \<in> \<int>\<^sub>\<le>\<^sub>0"
    7.57 +  shows   "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<or> z + 1/2 \<in> \<int>\<^sub>\<le>\<^sub>0"
    7.58 +proof-
    7.59 +  from assms obtain k where k: "2 * z = - of_nat k" by (elim nonpos_Ints_cases')
    7.60 +  thus ?thesis by (cases "even k") (auto elim!: evenE oddE simp: field_simps)
    7.61 +qed
    7.62 +
    7.63 +
    7.64 +lemma sin_series: "(\<lambda>n. ((-1)^n / fact (2*n+1)) *\<^sub>R z^(2*n+1)) sums sin z"
    7.65 +proof -
    7.66 +  from sin_converges[of z] have "(\<lambda>n. sin_coeff n *\<^sub>R z^n) sums sin z" .
    7.67 +  also have "(\<lambda>n. sin_coeff n *\<^sub>R z^n) sums sin z \<longleftrightarrow> 
    7.68 +                 (\<lambda>n. ((-1)^n / fact (2*n+1)) *\<^sub>R z^(2*n+1)) sums sin z"
    7.69 +    by (subst sums_mono_reindex[of "\<lambda>n. 2*n+1", symmetric])
    7.70 +       (auto simp: sin_coeff_def subseq_def ac_simps elim!: oddE)
    7.71 +  finally show ?thesis .
    7.72 +qed
    7.73 +
    7.74 +lemma cos_series: "(\<lambda>n. ((-1)^n / fact (2*n)) *\<^sub>R z^(2*n)) sums cos z"
    7.75 +proof -
    7.76 +  from cos_converges[of z] have "(\<lambda>n. cos_coeff n *\<^sub>R z^n) sums cos z" .
    7.77 +  also have "(\<lambda>n. cos_coeff n *\<^sub>R z^n) sums cos z \<longleftrightarrow> 
    7.78 +                 (\<lambda>n. ((-1)^n / fact (2*n)) *\<^sub>R z^(2*n)) sums cos z"
    7.79 +    by (subst sums_mono_reindex[of "\<lambda>n. 2*n", symmetric])
    7.80 +       (auto simp: cos_coeff_def subseq_def ac_simps elim!: evenE)
    7.81 +  finally show ?thesis .
    7.82 +qed
    7.83 +
    7.84 +lemma sin_z_over_z_series:
    7.85 +  fixes z :: "'a :: {real_normed_field,banach}"
    7.86 +  assumes "z \<noteq> 0"
    7.87 +  shows   "(\<lambda>n. (-1)^n / fact (2*n+1) * z^(2*n)) sums (sin z / z)"
    7.88 +proof -
    7.89 +  from sin_series[of z] have "(\<lambda>n. z * ((-1)^n / fact (2*n+1)) * z^(2*n)) sums sin z"
    7.90 +    by (simp add: field_simps scaleR_conv_of_real)
    7.91 +  from sums_mult[OF this, of "inverse z"] and assms show ?thesis
    7.92 +    by (simp add: field_simps)
    7.93 +qed
    7.94 +
    7.95 +lemma sin_z_over_z_series':
    7.96 +  fixes z :: "'a :: {real_normed_field,banach}"
    7.97 +  assumes "z \<noteq> 0"
    7.98 +  shows   "(\<lambda>n. sin_coeff (n+1) *\<^sub>R z^n) sums (sin z / z)"
    7.99 +proof -
   7.100 +  from sums_split_initial_segment[OF sin_converges[of z], of 1] 
   7.101 +    have "(\<lambda>n. z * (sin_coeff (n+1) *\<^sub>R z ^ n)) sums sin z" by simp
   7.102 +  from sums_mult[OF this, of "inverse z"] assms show ?thesis by (simp add: field_simps)
   7.103 +qed
   7.104 +
   7.105 +lemma has_field_derivative_sin_z_over_z:
   7.106 +  fixes A :: "'a :: {real_normed_field,banach} set"
   7.107 +  shows "((\<lambda>z. if z = 0 then 1 else sin z / z) has_field_derivative 0) (at 0 within A)"
   7.108 +      (is "(?f has_field_derivative ?f') _")
   7.109 +proof (rule has_field_derivative_at_within)
   7.110 +  have "((\<lambda>z::'a. \<Sum>n. of_real (sin_coeff (n+1)) * z^n) 
   7.111 +            has_field_derivative (\<Sum>n. diffs (\<lambda>n. of_real (sin_coeff (n+1))) n * 0^n)) (at 0)"
   7.112 +  proof (rule termdiffs_strong)
   7.113 +    from summable_ignore_initial_segment[OF sums_summable[OF sin_converges[of "1::'a"]], of 1]
   7.114 +      show "summable (\<lambda>n. of_real (sin_coeff (n+1)) * (1::'a)^n)" by (simp add: of_real_def)
   7.115 +  qed simp
   7.116 +  also have "(\<lambda>z::'a. \<Sum>n. of_real (sin_coeff (n+1)) * z^n) = ?f"
   7.117 +  proof
   7.118 +    fix z
   7.119 +    show "(\<Sum>n. of_real (sin_coeff (n+1)) * z^n)  = ?f z"
   7.120 +      by (cases "z = 0") (insert sin_z_over_z_series'[of z], 
   7.121 +            simp_all add: scaleR_conv_of_real sums_iff powser_zero sin_coeff_def)
   7.122 +  qed
   7.123 +  also have "(\<Sum>n. diffs (\<lambda>n. of_real (sin_coeff (n + 1))) n * (0::'a) ^ n) = 
   7.124 +                 diffs (\<lambda>n. of_real (sin_coeff (Suc n))) 0" by (simp add: powser_zero)
   7.125 +  also have "\<dots> = 0" by (simp add: sin_coeff_def diffs_def)
   7.126 +  finally show "((\<lambda>z::'a. if z = 0 then 1 else sin z / z) has_field_derivative 0) (at 0)" .
   7.127 +qed
   7.128 +
   7.129 + 
   7.130 +lemma tendsto_complex_of_real_iff: "((\<lambda>x. complex_of_real (f x)) \<longlongrightarrow> of_real c) F = (f \<longlongrightarrow> c) F"
   7.131 +  by (rule tendsto_of_real_iff)
   7.132 +
   7.133 +lemma round_Re_minimises_norm:
   7.134 +  "norm ((z::complex) - of_int m) \<ge> norm (z - of_int (round (Re z)))"
   7.135 +proof -
   7.136 +  let ?n = "round (Re z)"
   7.137 +  have "norm (z - of_int ?n) = sqrt ((Re z - of_int ?n)\<^sup>2 + (Im z)\<^sup>2)"
   7.138 +    by (simp add: cmod_def)
   7.139 +  also have "\<bar>Re z - of_int ?n\<bar> \<le> \<bar>Re z - of_int m\<bar>" by (rule round_diff_minimal)
   7.140 +  hence "sqrt ((Re z - of_int ?n)\<^sup>2 + (Im z)\<^sup>2) \<le> sqrt ((Re z - of_int m)\<^sup>2 + (Im z)\<^sup>2)"
   7.141 +    by (intro real_sqrt_le_mono add_mono) (simp_all add: abs_le_square_iff)
   7.142 +  also have "\<dots> = norm (z - of_int m)" by (simp add: cmod_def)
   7.143 +  finally show ?thesis .
   7.144 +qed
   7.145 +
   7.146 +lemma Re_pos_in_ball:
   7.147 +  assumes "Re z > 0" "t \<in> ball z (Re z/2)"
   7.148 +  shows   "Re t > 0"
   7.149 +proof -
   7.150 +  have "Re (z - t) \<le> norm (z - t)" by (rule complex_Re_le_cmod)
   7.151 +  also from assms have "\<dots> < Re z / 2" by (simp add: dist_complex_def)
   7.152 +  finally show "Re t > 0" using assms by simp
   7.153 +qed
   7.154 +
   7.155 +lemma no_nonpos_Int_in_ball_complex:
   7.156 +  assumes "Re z > 0" "t \<in> ball z (Re z/2)"
   7.157 +  shows   "t \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.158 +  using Re_pos_in_ball[OF assms] by (force elim!: nonpos_Ints_cases)
   7.159 +
   7.160 +lemma no_nonpos_Int_in_ball: 
   7.161 +  assumes "t \<in> ball z (dist z (round (Re z)))"
   7.162 +  shows   "t \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.163 +proof
   7.164 +  assume "t \<in> \<int>\<^sub>\<le>\<^sub>0"
   7.165 +  then obtain n where "t = of_int n" by (auto elim!: nonpos_Ints_cases)
   7.166 +  have "dist z (of_int n) \<le> dist z t + dist t (of_int n)" by (rule dist_triangle)
   7.167 +  also from assms have "dist z t < dist z (round (Re z))" by simp
   7.168 +  also have "\<dots> \<le> dist z (of_int n)"
   7.169 +    using round_Re_minimises_norm[of z] by (simp add: dist_complex_def)
   7.170 +  finally have "dist t (of_int n) > 0" by simp
   7.171 +  with `t = of_int n` show False by simp
   7.172 +qed
   7.173 +
   7.174 +lemma no_nonpos_Int_in_ball':
   7.175 +  assumes "(z :: 'a :: {euclidean_space,real_normed_algebra_1}) \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.176 +  obtains d where "d > 0" "\<And>t. t \<in> ball z d \<Longrightarrow> t \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.177 +proof (rule that)
   7.178 +  from assms show "setdist {z} \<int>\<^sub>\<le>\<^sub>0 > 0" by (subst setdist_gt_0_compact_closed) auto
   7.179 +next
   7.180 +  fix t assume "t \<in> ball z (setdist {z} \<int>\<^sub>\<le>\<^sub>0)"
   7.181 +  thus "t \<notin> \<int>\<^sub>\<le>\<^sub>0" using setdist_le_dist[of z "{z}" t "\<int>\<^sub>\<le>\<^sub>0"] by force
   7.182 +qed
   7.183 +
   7.184 +lemma Re_pos_or_Im_nz_in_ball:
   7.185 +  assumes "Re z > 0 \<or> Im z \<noteq> 0" "t \<in> ball z (if Im z = 0 then Re z / 2 else abs (Im z) / 2)"
   7.186 +  shows   "Re t > 0 \<or> Im t \<noteq> 0"
   7.187 +using assms(1)
   7.188 +proof (cases "Im z = 0")
   7.189 +  assume A: "Im z = 0"
   7.190 +  with assms(1) have "Re z > 0" by blast
   7.191 +  with assms(2) A Re_pos_in_ball[of z t] show ?thesis by simp
   7.192 +next
   7.193 +  assume A: "Im z \<noteq> 0"
   7.194 +  have "abs (Im z) - abs (Im t) \<le> abs (Im z - Im t)" by linarith
   7.195 +  also have "\<dots> = abs (Im (z - t))" by simp
   7.196 +  also have "\<dots> \<le> norm (z - t)" by (rule abs_Im_le_cmod)
   7.197 +  also from A assms(2) have "\<dots> \<le> abs (Im z) / 2" by (simp add: dist_complex_def)
   7.198 +  finally have "abs (Im t) > 0" using A by simp
   7.199 +  thus ?thesis by force
   7.200 +qed
   7.201 +
   7.202 +
   7.203 +subsection \<open>Definitions\<close>
   7.204 +
   7.205 +text \<open>
   7.206 +  We define the Gamma function by first defining its multiplicative inverse @{term "Gamma_inv"}. 
   7.207 +  This is more convenient because @{term "Gamma_inv"} is entire, which makes proofs of its 
   7.208 +  properties more convenient because one does not have to watch out for discontinuities.
   7.209 +  (e.g. @{term "Gamma_inv"} fulfils @{term "rGamma z = z * rGamma (z + 1)"} everywhere,
   7.210 +  whereas @{term "Gamma"} does not fulfil the analogous equation on the non-positive integers)
   7.211 +  
   7.212 +  We define the Gamma function (resp. its inverse) in the Euler form. This form has the advantage 
   7.213 +  that it is a relatively simple limit that converges everywhere. The limit at the poles is 0 
   7.214 +  (due to division by 0). The functional equation @{term "Gamma (z + 1) = z * Gamma z"} follows 
   7.215 +  immediately from the definition.
   7.216 +\<close>
   7.217 +
   7.218 +definition Gamma_series :: "('a :: {banach,real_normed_field}) \<Rightarrow> nat \<Rightarrow> 'a" where
   7.219 +  "Gamma_series z n = fact n * exp (z * of_real (ln (of_nat n))) / pochhammer z (n+1)"
   7.220 +
   7.221 +definition Gamma_series' :: "('a :: {banach,real_normed_field}) \<Rightarrow> nat \<Rightarrow> 'a" where
   7.222 +  "Gamma_series' z n = fact (n - 1) * exp (z * of_real (ln (of_nat n))) / pochhammer z n"
   7.223 +
   7.224 +definition rGamma_series :: "('a :: {banach,real_normed_field}) \<Rightarrow> nat \<Rightarrow> 'a" where
   7.225 +  "rGamma_series z n = pochhammer z (n+1) / (fact n * exp (z * of_real (ln (of_nat n))))"
   7.226 +
   7.227 +lemma Gamma_series_altdef: "Gamma_series z n = inverse (rGamma_series z n)"
   7.228 +  and rGamma_series_altdef: "rGamma_series z n = inverse (Gamma_series z n)"
   7.229 +  unfolding Gamma_series_def rGamma_series_def by simp_all
   7.230 +  
   7.231 +lemma rGamma_series_minus_of_nat:
   7.232 +  "eventually (\<lambda>n. rGamma_series (- of_nat k) n = 0) sequentially"
   7.233 +  using eventually_ge_at_top[of k]
   7.234 +  by eventually_elim (auto simp: rGamma_series_def pochhammer_of_nat_eq_0_iff)  
   7.235 +
   7.236 +lemma Gamma_series_minus_of_nat:
   7.237 +  "eventually (\<lambda>n. Gamma_series (- of_nat k) n = 0) sequentially"
   7.238 +  using eventually_ge_at_top[of k]
   7.239 +  by eventually_elim (auto simp: Gamma_series_def pochhammer_of_nat_eq_0_iff)  
   7.240 +
   7.241 +lemma Gamma_series'_minus_of_nat:
   7.242 +  "eventually (\<lambda>n. Gamma_series' (- of_nat k) n = 0) sequentially"
   7.243 +  using eventually_gt_at_top[of k]
   7.244 +  by eventually_elim (auto simp: Gamma_series'_def pochhammer_of_nat_eq_0_iff)
   7.245 +  
   7.246 +lemma rGamma_series_nonpos_Ints_LIMSEQ: "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> rGamma_series z \<longlonglongrightarrow> 0"
   7.247 +  by (elim nonpos_Ints_cases', hypsubst, subst tendsto_cong, rule rGamma_series_minus_of_nat, simp)
   7.248 +
   7.249 +lemma Gamma_series_nonpos_Ints_LIMSEQ: "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma_series z \<longlonglongrightarrow> 0"
   7.250 +  by (elim nonpos_Ints_cases', hypsubst, subst tendsto_cong, rule Gamma_series_minus_of_nat, simp)
   7.251 +
   7.252 +lemma Gamma_series'_nonpos_Ints_LIMSEQ: "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma_series' z \<longlonglongrightarrow> 0"
   7.253 +  by (elim nonpos_Ints_cases', hypsubst, subst tendsto_cong, rule Gamma_series'_minus_of_nat, simp)
   7.254 +  
   7.255 +lemma Gamma_series_Gamma_series': 
   7.256 +  assumes z: "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.257 +  shows   "(\<lambda>n. Gamma_series' z n / Gamma_series z n) \<longlonglongrightarrow> 1"
   7.258 +proof (rule Lim_transform_eventually)
   7.259 +  from eventually_gt_at_top[of "0::nat"]
   7.260 +    show "eventually (\<lambda>n. z / of_nat n + 1 = Gamma_series' z n / Gamma_series z n) sequentially"
   7.261 +  proof eventually_elim
   7.262 +    fix n :: nat assume n: "n > 0"
   7.263 +    from n z have "Gamma_series' z n / Gamma_series z n = (z + of_nat n) / of_nat n"
   7.264 +      by (cases n, simp)
   7.265 +         (auto simp add: Gamma_series_def Gamma_series'_def pochhammer_rec'
   7.266 +               dest: pochhammer_eq_0_imp_nonpos_Int plus_of_nat_eq_0_imp)
   7.267 +    also from n have "\<dots> = z / of_nat n + 1" by (simp add: divide_simps)
   7.268 +    finally show "z / of_nat n + 1 = Gamma_series' z n / Gamma_series z n" ..
   7.269 +  qed
   7.270 +  have "(\<lambda>x. z / of_nat x) \<longlonglongrightarrow> 0"
   7.271 +    by (rule tendsto_norm_zero_cancel)
   7.272 +       (insert tendsto_mult[OF tendsto_const[of "norm z"] lim_inverse_n], 
   7.273 +        simp add:  norm_divide inverse_eq_divide)
   7.274 +  from tendsto_add[OF this tendsto_const[of 1]] show "(\<lambda>n. z / of_nat n + 1) \<longlonglongrightarrow> 1" by simp
   7.275 +qed
   7.276 +
   7.277 +
   7.278 +subsection \<open>Convergence of the Euler series form\<close>
   7.279 +
   7.280 +text \<open>
   7.281 +  We now show that the series that defines the Gamma function in the Euler form converges 
   7.282 +  and that the function defined by it is continuous on the complex halfspace with positive 
   7.283 +  real part.
   7.284 +  
   7.285 +  We do this by showing that the logarithm of the Euler series is continuous and converges 
   7.286 +  locally uniformly, which means that the log-Gamma function defined by its limit is also 
   7.287 +  continuous.
   7.288 +  
   7.289 +  This will later allow us to lift holomorphicity and continuity from the log-Gamma 
   7.290 +  function to the inverse Gamma function, and from that to the Gamma function itself.
   7.291 +\<close>
   7.292 +
   7.293 +definition ln_Gamma_series :: "('a :: {banach,real_normed_field,ln}) \<Rightarrow> nat \<Rightarrow> 'a" where
   7.294 +  "ln_Gamma_series z n = z * ln (of_nat n) - ln z - (\<Sum>k=1..n. ln (z / of_nat k + 1))"
   7.295 +
   7.296 +definition ln_Gamma_series' :: "('a :: {banach,real_normed_field,ln}) \<Rightarrow> nat \<Rightarrow> 'a" where
   7.297 +  "ln_Gamma_series' z n =
   7.298 +     - euler_mascheroni*z - ln z + (\<Sum>k=1..n. z / of_nat n - ln (z / of_nat k + 1))"
   7.299 +
   7.300 +definition ln_Gamma :: "('a :: {banach,real_normed_field,ln}) \<Rightarrow> 'a" where
   7.301 +  "ln_Gamma z = lim (ln_Gamma_series z)"
   7.302 +
   7.303 +
   7.304 +text \<open>
   7.305 +  We now show that the log-Gamma series converges locally uniformly for all complex numbers except 
   7.306 +  the non-positive integers. We do this by proving that the series is locally Cauchy, adapting this
   7.307 +  proof:
   7.308 +  http://math.stackexchange.com/questions/887158/convergence-of-gammaz-lim-n-to-infty-fracnzn-prod-m-0nzm
   7.309 +\<close>
   7.310 +
   7.311 +context
   7.312 +begin
   7.313 +
   7.314 +private lemma ln_Gamma_series_complex_converges_aux:
   7.315 +  fixes z :: complex and k :: nat
   7.316 +  assumes z: "z \<noteq> 0" and k: "of_nat k \<ge> 2*norm z" "k \<ge> 2"
   7.317 +  shows "norm (z * ln (1 - 1/of_nat k) + ln (z/of_nat k + 1)) \<le> 2*(norm z + norm z^2) / of_nat k^2"
   7.318 +proof -
   7.319 +  let ?k = "of_nat k :: complex" and ?z = "norm z"
   7.320 +  have "z *ln (1 - 1/?k) + ln (z/?k+1) = z*(ln (1 - 1/?k :: complex) + 1/?k) + (ln (1+z/?k) - z/?k)"
   7.321 +    by (simp add: algebra_simps)
   7.322 +  also have "norm ... \<le> ?z * norm (ln (1-1/?k) + 1/?k :: complex) + norm (ln (1+z/?k) - z/?k)"
   7.323 +    by (subst norm_mult [symmetric], rule norm_triangle_ineq)
   7.324 +  also have "norm (Ln (1 + -1/?k) - (-1/?k)) \<le> (norm (-1/?k))\<^sup>2 / (1 - norm(-1/?k))"
   7.325 +    using k by (intro Ln_approx_linear) (simp add: norm_divide)
   7.326 +  hence "?z * norm (ln (1-1/?k) + 1/?k) \<le> ?z * ((norm (1/?k))^2 / (1 - norm (1/?k)))" 
   7.327 +    by (intro mult_left_mono) simp_all
   7.328 +  also have "... \<le> (?z * (of_nat k / (of_nat k - 1))) / of_nat k^2" using k
   7.329 +    by (simp add: field_simps power2_eq_square norm_divide)
   7.330 +  also have "... \<le> (?z * 2) / of_nat k^2" using k
   7.331 +    by (intro divide_right_mono mult_left_mono) (simp_all add: field_simps)
   7.332 +  also have "norm (ln (1+z/?k) - z/?k) \<le> norm (z/?k)^2 / (1 - norm (z/?k))" using k
   7.333 +    by (intro Ln_approx_linear) (simp add: norm_divide)
   7.334 +  hence "norm (ln (1+z/?k) - z/?k) \<le> ?z^2 / of_nat k^2 / (1 - ?z / of_nat k)" 
   7.335 +    by (simp add: field_simps norm_divide)
   7.336 +  also have "... \<le> (?z^2 * (of_nat k / (of_nat k - ?z))) / of_nat k^2" using k
   7.337 +    by (simp add: field_simps power2_eq_square)
   7.338 +  also have "... \<le> (?z^2 * 2) / of_nat k^2" using k
   7.339 +    by (intro divide_right_mono mult_left_mono) (simp_all add: field_simps)
   7.340 +  also note add_divide_distrib [symmetric]
   7.341 +  finally show ?thesis by (simp only: distrib_left mult.commute)
   7.342 +qed
   7.343 +
   7.344 +lemma ln_Gamma_series_complex_converges:
   7.345 +  assumes z: "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.346 +  assumes d: "d > 0" "\<And>n. n \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> norm (z - of_int n) > d"
   7.347 +  shows "uniformly_convergent_on (ball z d) (\<lambda>n z. ln_Gamma_series z n :: complex)"
   7.348 +proof (intro Cauchy_uniformly_convergent uniformly_Cauchy_onI')
   7.349 +  fix e :: real assume e: "e > 0"
   7.350 +  def e'' \<equiv> "SUP t:ball z d. norm t + norm t^2"
   7.351 +  def e' \<equiv> "e / (2*e'')"
   7.352 +  have "bounded ((\<lambda>t. norm t + norm t^2) ` cball z d)"
   7.353 +    by (intro compact_imp_bounded compact_continuous_image) (auto intro!: continuous_intros)
   7.354 +  hence "bounded ((\<lambda>t. norm t + norm t^2) ` ball z d)" by (rule bounded_subset) auto
   7.355 +  hence bdd: "bdd_above ((\<lambda>t. norm t + norm t^2) ` ball z d)" by (rule bounded_imp_bdd_above)
   7.356 +
   7.357 +  with z d(1) d(2)[of "-1"] have e''_pos: "e'' > 0" unfolding e''_def
   7.358 +    by (subst less_cSUP_iff) (auto intro!: add_pos_nonneg bexI[of _ z])
   7.359 +  have e'': "norm t + norm t^2 \<le> e''" if "t \<in> ball z d" for t unfolding e''_def using that
   7.360 +    by (rule cSUP_upper[OF _ bdd])
   7.361 +  from e z e''_pos have e': "e' > 0" unfolding e'_def 
   7.362 +    by (intro divide_pos_pos mult_pos_pos add_pos_pos) (simp_all add: field_simps)
   7.363 +
   7.364 +  have "summable (\<lambda>k. inverse ((real_of_nat k)^2))"
   7.365 +    by (rule inverse_power_summable) simp
   7.366 +  from summable_partial_sum_bound[OF this e'] guess M . note M = this
   7.367 +
   7.368 +  def N \<equiv> "max 2 (max (nat \<lceil>2 * (norm z + d)\<rceil>) M)"
   7.369 +  {
   7.370 +    from d have "\<lceil>2 * (cmod z + d)\<rceil> \<ge> \<lceil>0::real\<rceil>" 
   7.371 +      by (intro ceiling_mono mult_nonneg_nonneg add_nonneg_nonneg) simp_all
   7.372 +    hence "2 * (norm z + d) \<le> of_nat (nat \<lceil>2 * (norm z + d)\<rceil>)" unfolding N_def
   7.373 +      by (simp_all add: le_of_int_ceiling)
   7.374 +    also have "... \<le> of_nat N" unfolding N_def 
   7.375 +      by (subst of_nat_le_iff) (rule max.coboundedI2, rule max.cobounded1)
   7.376 +    finally have "of_nat N \<ge> 2 * (norm z + d)" .
   7.377 +    moreover have "N \<ge> 2" "N \<ge> M" unfolding N_def by simp_all
   7.378 +    moreover have "(\<Sum>k=m..n. 1/(of_nat k)\<^sup>2) < e'" if "m \<ge> N" for m n
   7.379 +      using M[OF order.trans[OF `N \<ge> M` that]] unfolding real_norm_def
   7.380 +      by (subst (asm) abs_of_nonneg) (auto intro: setsum_nonneg simp: divide_simps)
   7.381 +    moreover note calculation
   7.382 +  } note N = this
   7.383 +
   7.384 +  show "\<exists>M. \<forall>t\<in>ball z d. \<forall>m\<ge>M. \<forall>n>m. dist (ln_Gamma_series t m) (ln_Gamma_series t n) < e"
   7.385 +    unfolding dist_complex_def
   7.386 +  proof (intro exI[of _ N] ballI allI impI)
   7.387 +    fix t m n assume t: "t \<in> ball z d" and mn: "m \<ge> N" "n > m"
   7.388 +    from d(2)[of 0] t have "0 < dist z 0 - dist z t" by (simp add: field_simps dist_complex_def)
   7.389 +    also have "dist z 0 - dist z t \<le> dist 0 t" using dist_triangle[of 0 z t]
   7.390 +      by (simp add: dist_commute)
   7.391 +    finally have t_nz: "t \<noteq> 0" by auto
   7.392 +
   7.393 +    have "norm t \<le> norm z + norm (t - z)" by (rule norm_triangle_sub)
   7.394 +    also from t have "norm (t - z) < d" by (simp add: dist_complex_def norm_minus_commute)
   7.395 +    also have "2 * (norm z + d) \<le> of_nat N" by (rule N)
   7.396 +    also have "N \<le> m" by (rule mn)
   7.397 +    finally have norm_t: "2 * norm t < of_nat m" by simp
   7.398 +
   7.399 +    have "ln_Gamma_series t m - ln_Gamma_series t n = 
   7.400 +              (-(t * Ln (of_nat n)) - (-(t * Ln (of_nat m)))) +
   7.401 +              ((\<Sum>k=1..n. Ln (t / of_nat k + 1)) - (\<Sum>k=1..m. Ln (t / of_nat k + 1)))"
   7.402 +      by (simp add: ln_Gamma_series_def algebra_simps)
   7.403 +    also have "(\<Sum>k=1..n. Ln (t / of_nat k + 1)) - (\<Sum>k=1..m. Ln (t / of_nat k + 1)) =
   7.404 +                 (\<Sum>k\<in>{1..n}-{1..m}. Ln (t / of_nat k + 1))" using mn
   7.405 +      by (simp add: setsum_diff)
   7.406 +    also from mn have "{1..n}-{1..m} = {Suc m..n}" by fastforce
   7.407 +    also have "-(t * Ln (of_nat n)) - (-(t * Ln (of_nat m))) = 
   7.408 +                   (\<Sum>k = Suc m..n. t * Ln (of_nat (k - 1)) - t * Ln (of_nat k))" using mn
   7.409 +      by (subst setsum_telescope'' [symmetric]) simp_all
   7.410 +    also have "... = (\<Sum>k = Suc m..n. t * Ln (of_nat (k - 1) / of_nat k))" using mn N
   7.411 +      by (intro setsum_cong_Suc)
   7.412 +         (simp_all del: of_nat_Suc add: field_simps Ln_of_nat Ln_of_nat_over_of_nat)
   7.413 +    also have "of_nat (k - 1) / of_nat k = 1 - 1 / (of_nat k :: complex)" if "k \<in> {Suc m..n}" for k
   7.414 +      using that of_nat_eq_0_iff[of "Suc i" for i] by (cases k) (simp_all add: divide_simps)
   7.415 +    hence "(\<Sum>k = Suc m..n. t * Ln (of_nat (k - 1) / of_nat k)) =
   7.416 +             (\<Sum>k = Suc m..n. t * Ln (1 - 1 / of_nat k))" using mn N
   7.417 +      by (intro setsum.cong) simp_all
   7.418 +    also note setsum.distrib [symmetric]
   7.419 +    also have "norm (\<Sum>k=Suc m..n. t * Ln (1 - 1/of_nat k) + Ln (t/of_nat k + 1)) \<le> 
   7.420 +      (\<Sum>k=Suc m..n. 2 * (norm t + (norm t)\<^sup>2) / (real_of_nat k)\<^sup>2)" using t_nz N(2) mn norm_t
   7.421 +      by (intro order.trans[OF norm_setsum setsum_mono[OF ln_Gamma_series_complex_converges_aux]]) simp_all
   7.422 +    also have "... \<le> 2 * (norm t + norm t^2) * (\<Sum>k=Suc m..n. 1 / (of_nat k)\<^sup>2)"
   7.423 +      by (simp add: setsum_right_distrib)
   7.424 +    also have "... < 2 * (norm t + norm t^2) * e'" using mn z t_nz
   7.425 +      by (intro mult_strict_left_mono N mult_pos_pos add_pos_pos) simp_all
   7.426 +    also from e''_pos have "... = e * ((cmod t + (cmod t)\<^sup>2) / e'')" 
   7.427 +      by (simp add: e'_def field_simps power2_eq_square)
   7.428 +    also from e''[OF t] e''_pos e 
   7.429 +      have "\<dots> \<le> e * 1" by (intro mult_left_mono) (simp_all add: field_simps)
   7.430 +    finally show "norm (ln_Gamma_series t m - ln_Gamma_series t n) < e" by simp
   7.431 +  qed
   7.432 +qed
   7.433 +
   7.434 +end
   7.435 +
   7.436 +lemma ln_Gamma_series_complex_converges':
   7.437 +  assumes z: "(z :: complex) \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.438 +  shows "\<exists>d>0. uniformly_convergent_on (ball z d) (\<lambda>n z. ln_Gamma_series z n)"
   7.439 +proof -
   7.440 +  def d' \<equiv> "Re z"
   7.441 +  def d \<equiv> "if d' > 0 then d' / 2 else norm (z - of_int (round d')) / 2"
   7.442 +  have "of_int (round d') \<in> \<int>\<^sub>\<le>\<^sub>0" if "d' \<le> 0" using that
   7.443 +    by (intro nonpos_Ints_of_int) (simp_all add: round_def)
   7.444 +  with assms have d_pos: "d > 0" unfolding d_def by (force simp: not_less)
   7.445 +
   7.446 +  have "d < cmod (z - of_int n)" if "n \<in> \<int>\<^sub>\<le>\<^sub>0" for n
   7.447 +  proof (cases "Re z > 0")
   7.448 +    case True
   7.449 +    from nonpos_Ints_nonpos[OF that] have n: "n \<le> 0" by simp
   7.450 +    from True have "d = Re z/2" by (simp add: d_def d'_def)
   7.451 +    also from n True have "\<dots> < Re (z - of_int n)" by simp
   7.452 +    also have "\<dots> \<le> norm (z - of_int n)" by (rule complex_Re_le_cmod)
   7.453 +    finally show ?thesis .
   7.454 +  next
   7.455 +    case False
   7.456 +    with assms nonpos_Ints_of_int[of "round (Re z)"] 
   7.457 +      have "z \<noteq> of_int (round d')" by (auto simp: not_less)
   7.458 +    with False have "d < norm (z - of_int (round d'))" by (simp add: d_def d'_def)
   7.459 +    also have "\<dots> \<le> norm (z - of_int n)" unfolding d'_def by (rule round_Re_minimises_norm)
   7.460 +    finally show ?thesis .
   7.461 +  qed
   7.462 +  hence conv: "uniformly_convergent_on (ball z d) (\<lambda>n z. ln_Gamma_series z n)"
   7.463 +    by (intro ln_Gamma_series_complex_converges d_pos z) simp_all
   7.464 +  from d_pos conv show ?thesis by blast
   7.465 +qed
   7.466 +
   7.467 +lemma ln_Gamma_series_complex_converges'': "(z :: complex) \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> convergent (ln_Gamma_series z)"
   7.468 +  by (drule ln_Gamma_series_complex_converges') (auto intro: uniformly_convergent_imp_convergent)
   7.469 +
   7.470 +lemma ln_Gamma_complex_LIMSEQ: "(z :: complex) \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> ln_Gamma_series z \<longlonglongrightarrow> ln_Gamma z"
   7.471 +  using ln_Gamma_series_complex_converges'' by (simp add: convergent_LIMSEQ_iff ln_Gamma_def)
   7.472 +
   7.473 +lemma exp_ln_Gamma_series_complex:
   7.474 +  assumes "n > 0" "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.475 +  shows   "exp (ln_Gamma_series z n :: complex) = Gamma_series z n"
   7.476 +proof -
   7.477 +  from assms have "z \<noteq> 0" by (intro notI) auto
   7.478 +  with assms have "exp (ln_Gamma_series z n) = 
   7.479 +          (of_nat n) powr z / (z * (\<Prod>k=1..n. exp (Ln (z / of_nat k + 1))))"
   7.480 +    unfolding ln_Gamma_series_def powr_def by (simp add: exp_diff exp_setsum)
   7.481 +  also from assms have "(\<Prod>k=1..n. exp (Ln (z / of_nat k + 1))) = (\<Prod>k=1..n. z / of_nat k + 1)"
   7.482 +    by (intro setprod.cong[OF refl], subst exp_Ln) (auto simp: field_simps plus_of_nat_eq_0_imp)
   7.483 +  also have "... = (\<Prod>k=1..n. z + k) / fact n" unfolding fact_altdef
   7.484 +    by (subst setprod_dividef [symmetric]) (simp_all add: field_simps)
   7.485 +  also from assms have "z * ... = (\<Prod>k=0..n. z + k) / fact n"
   7.486 +    by (cases n) (simp_all add: setprod_nat_ivl_1_Suc)
   7.487 +  also have "(\<Prod>k=0..n. z + k) = pochhammer z (Suc n)" unfolding pochhammer_def by simp
   7.488 +  also have "of_nat n powr z / (pochhammer z (Suc n) / fact n) = Gamma_series z n"
   7.489 +    unfolding Gamma_series_def using assms by (simp add: divide_simps powr_def Ln_of_nat)
   7.490 +  finally show ?thesis .
   7.491 +qed
   7.492 +
   7.493 +
   7.494 +lemma ln_Gamma_series'_aux:
   7.495 +  assumes "(z::complex) \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.496 +  shows   "(\<lambda>k. z / of_nat (Suc k) - ln (1 + z / of_nat (Suc k))) sums 
   7.497 +              (ln_Gamma z + euler_mascheroni * z + ln z)" (is "?f sums ?s")
   7.498 +unfolding sums_def
   7.499 +proof (rule Lim_transform)
   7.500 +  show "(\<lambda>n. ln_Gamma_series z n + of_real (harm n - ln (of_nat n)) * z + ln z) \<longlonglongrightarrow> ?s"
   7.501 +    (is "?g \<longlonglongrightarrow> _") 
   7.502 +    by (intro tendsto_intros ln_Gamma_complex_LIMSEQ euler_mascheroni_LIMSEQ_of_real assms)
   7.503 +
   7.504 +  have A: "eventually (\<lambda>n. (\<Sum>k<n. ?f k) - ?g n = 0) sequentially"
   7.505 +    using eventually_gt_at_top[of "0::nat"]
   7.506 +  proof eventually_elim
   7.507 +    fix n :: nat assume n: "n > 0"
   7.508 +    have "(\<Sum>k<n. ?f k) = (\<Sum>k=1..n. z / of_nat k - ln (1 + z / of_nat k))"
   7.509 +      by (subst atLeast0LessThan [symmetric], subst setsum_shift_bounds_Suc_ivl [symmetric],
   7.510 +          subst atLeastLessThanSuc_atLeastAtMost) simp_all
   7.511 +    also have "\<dots> = z * of_real (harm n) - (\<Sum>k=1..n. ln (1 + z / of_nat k))"
   7.512 +      by (simp add: harm_def setsum_subtractf setsum_right_distrib divide_inverse)
   7.513 +    also from n have "\<dots> - ?g n = 0"
   7.514 +      by (simp add: ln_Gamma_series_def setsum_subtractf algebra_simps Ln_of_nat) 
   7.515 +    finally show "(\<Sum>k<n. ?f k) - ?g n = 0" .
   7.516 +  qed
   7.517 +  show "(\<lambda>n. (\<Sum>k<n. ?f k) - ?g n) \<longlonglongrightarrow> 0" by (subst tendsto_cong[OF A]) simp_all
   7.518 +qed
   7.519 +
   7.520 +
   7.521 +lemma uniformly_summable_deriv_ln_Gamma:
   7.522 +  assumes z: "(z :: 'a :: {real_normed_field,banach}) \<noteq> 0" and d: "d > 0" "d \<le> norm z/2"
   7.523 +  shows "uniformly_convergent_on (ball z d)
   7.524 +            (\<lambda>k z. \<Sum>i<k. inverse (of_nat (Suc i)) - inverse (z + of_nat (Suc i)))"
   7.525 +           (is "uniformly_convergent_on _ (\<lambda>k z. \<Sum>i<k. ?f i z)")
   7.526 +proof (rule weierstrass_m_test'_ev)
   7.527 +  {
   7.528 +    fix t assume t: "t \<in> ball z d"
   7.529 +    have "norm z = norm (t + (z - t))" by simp
   7.530 +    have "norm (t + (z - t)) \<le> norm t + norm (z - t)" by (rule norm_triangle_ineq)
   7.531 +    also from t d have "norm (z - t) < norm z / 2" by (simp add: dist_norm)
   7.532 +    finally have A: "norm t > norm z / 2" using z by (simp add: field_simps)
   7.533 +    
   7.534 +    have "norm t = norm (z + (t - z))" by simp
   7.535 +    also have "\<dots> \<le> norm z + norm (t - z)" by (rule norm_triangle_ineq)
   7.536 +    also from t d have "norm (t - z) \<le> norm z / 2" by (simp add: dist_norm norm_minus_commute)
   7.537 +    also from z have "\<dots> < norm z" by simp
   7.538 +    finally have B: "norm t < 2 * norm z" by simp
   7.539 +    note A B
   7.540 +  } note ball = this
   7.541 +
   7.542 +  show "eventually (\<lambda>n. \<forall>t\<in>ball z d. norm (?f n t) \<le> 4 * norm z * inverse (of_nat (Suc n)^2)) sequentially"
   7.543 +    using eventually_gt_at_top apply eventually_elim
   7.544 +  proof safe
   7.545 +    fix t :: 'a assume t: "t \<in> ball z d"
   7.546 +    from z ball[OF t] have t_nz: "t \<noteq> 0" by auto
   7.547 +    fix n :: nat assume n: "n > nat \<lceil>4 * norm z\<rceil>"
   7.548 +    from ball[OF t] t_nz have "4 * norm z > 2 * norm t" by simp
   7.549 +    also from n have "\<dots>  < of_nat n" by linarith
   7.550 +    finally have n: "of_nat n > 2 * norm t" .
   7.551 +    hence "of_nat n > norm t" by simp
   7.552 +    hence t': "t \<noteq> -of_nat (Suc n)" by (intro notI) (simp del: of_nat_Suc)
   7.553 +
   7.554 +    with t_nz have "?f n t = 1 / (of_nat (Suc n) * (1 + of_nat (Suc n)/t))"
   7.555 +      by (simp add: divide_simps eq_neg_iff_add_eq_0 del: of_nat_Suc)
   7.556 +    also have "norm \<dots> = inverse (of_nat (Suc n)) * inverse (norm (of_nat (Suc n)/t + 1))"
   7.557 +      by (simp add: norm_divide norm_mult divide_simps add_ac del: of_nat_Suc)
   7.558 +    also {
   7.559 +      from z t_nz ball[OF t] have "of_nat (Suc n) / (4 * norm z) \<le> of_nat (Suc n) / (2 * norm t)"
   7.560 +        by (intro divide_left_mono mult_pos_pos) simp_all
   7.561 +      also have "\<dots> < norm (of_nat (Suc n) / t) - norm (1 :: 'a)" 
   7.562 +        using t_nz n by (simp add: field_simps norm_divide del: of_nat_Suc)
   7.563 +      also have "\<dots> \<le> norm (of_nat (Suc n)/t + 1)" by (rule norm_diff_ineq)
   7.564 +      finally have "inverse (norm (of_nat (Suc n)/t + 1)) \<le> 4 * norm z / of_nat (Suc n)"
   7.565 +        using z by (simp add: divide_simps norm_divide mult_ac del: of_nat_Suc)
   7.566 +    }
   7.567 +    also have "inverse (real_of_nat (Suc n)) * (4 * norm z / real_of_nat (Suc n)) =
   7.568 +                 4 * norm z * inverse (of_nat (Suc n)^2)" 
   7.569 +                 by (simp add: divide_simps power2_eq_square del: of_nat_Suc)
   7.570 +    finally show "norm (?f n t) \<le> 4 * norm z * inverse (of_nat (Suc n)^2)" 
   7.571 +      by (simp del: of_nat_Suc)
   7.572 +  qed
   7.573 +next
   7.574 +  show "summable (\<lambda>n. 4 * norm z * inverse ((of_nat (Suc n))^2))"
   7.575 +    by (subst summable_Suc_iff) (simp add: summable_mult inverse_power_summable)
   7.576 +qed
   7.577 +
   7.578 +lemma summable_deriv_ln_Gamma:
   7.579 +  "z \<noteq> (0 :: 'a :: {real_normed_field,banach}) \<Longrightarrow> 
   7.580 +     summable (\<lambda>n. inverse (of_nat (Suc n)) - inverse (z + of_nat (Suc n)))"
   7.581 +  unfolding summable_iff_convergent
   7.582 +  by (rule uniformly_convergent_imp_convergent, 
   7.583 +      rule uniformly_summable_deriv_ln_Gamma[of z "norm z/2"]) simp_all
   7.584 +
   7.585 +
   7.586 +definition Polygamma :: "nat \<Rightarrow> ('a :: {real_normed_field,banach}) \<Rightarrow> 'a" where
   7.587 +  "Polygamma n z = (if n = 0 then 
   7.588 +      (\<Sum>k. inverse (of_nat (Suc k)) - inverse (z + of_nat k)) - euler_mascheroni else 
   7.589 +      (-1)^Suc n * fact n * (\<Sum>k. inverse ((z + of_nat k)^Suc n)))"
   7.590 +
   7.591 +abbreviation Digamma :: "('a :: {real_normed_field,banach}) \<Rightarrow> 'a" where
   7.592 +  "Digamma \<equiv> Polygamma 0"
   7.593 +
   7.594 +lemma Digamma_def: 
   7.595 +  "Digamma z = (\<Sum>k. inverse (of_nat (Suc k)) - inverse (z + of_nat k)) - euler_mascheroni"
   7.596 +  by (simp add: Polygamma_def)
   7.597 +
   7.598 +
   7.599 +lemma summable_Digamma: 
   7.600 +  assumes "(z :: 'a :: {real_normed_field,banach}) \<noteq> 0"
   7.601 +  shows   "summable (\<lambda>n. inverse (of_nat (Suc n)) - inverse (z + of_nat n))"
   7.602 +proof -
   7.603 +  have sums: "(\<lambda>n. inverse (z + of_nat (Suc n)) - inverse (z + of_nat n)) sums 
   7.604 +                       (0 - inverse (z + of_nat 0))"
   7.605 +    by (intro telescope_sums filterlim_compose[OF tendsto_inverse_0]
   7.606 +              tendsto_add_filterlim_at_infinity[OF tendsto_const] tendsto_of_nat)
   7.607 +  from summable_add[OF summable_deriv_ln_Gamma[OF assms] sums_summable[OF sums]]
   7.608 +    show "summable (\<lambda>n. inverse (of_nat (Suc n)) - inverse (z + of_nat n))" by simp
   7.609 +qed
   7.610 +
   7.611 +lemma summable_offset:
   7.612 +  assumes "summable (\<lambda>n. f (n + k) :: 'a :: real_normed_vector)"
   7.613 +  shows   "summable f"
   7.614 +proof -
   7.615 +  from assms have "convergent (\<lambda>m. \<Sum>n<m. f (n + k))" by (simp add: summable_iff_convergent)
   7.616 +  hence "convergent (\<lambda>m. (\<Sum>n<k. f n) + (\<Sum>n<m. f (n + k)))" 
   7.617 +    by (intro convergent_add convergent_const)
   7.618 +  also have "(\<lambda>m. (\<Sum>n<k. f n) + (\<Sum>n<m. f (n + k))) = (\<lambda>m. \<Sum>n<m+k. f n)"
   7.619 +  proof
   7.620 +    fix m :: nat
   7.621 +    have "{..<m+k} = {..<k} \<union> {k..<m+k}" by auto
   7.622 +    also have "(\<Sum>n\<in>\<dots>. f n) = (\<Sum>n<k. f n) + (\<Sum>n=k..<m+k. f n)" 
   7.623 +      by (rule setsum.union_disjoint) auto
   7.624 +    also have "(\<Sum>n=k..<m+k. f n) = (\<Sum>n=0..<m+k-k. f (n + k))"
   7.625 +      by (intro setsum.reindex_cong[of "\<lambda>n. n + k"])
   7.626 +         (simp, subst image_add_atLeastLessThan, auto)
   7.627 +    finally show "(\<Sum>n<k. f n) + (\<Sum>n<m. f (n + k)) = (\<Sum>n<m+k. f n)" by (simp add: atLeast0LessThan)
   7.628 +  qed
   7.629 +  finally have "(\<lambda>a. setsum f {..<a}) \<longlonglongrightarrow> lim (\<lambda>m. setsum f {..<m + k})"
   7.630 +    by (auto simp: convergent_LIMSEQ_iff dest: LIMSEQ_offset)
   7.631 +  thus ?thesis by (auto simp: summable_iff_convergent convergent_def)
   7.632 +qed
   7.633 +
   7.634 +lemma Polygamma_converges:
   7.635 +  fixes z :: "'a :: {real_normed_field,banach}"
   7.636 +  assumes z: "z \<noteq> 0" and n: "n \<ge> 2"
   7.637 +  shows "uniformly_convergent_on (ball z d) (\<lambda>k z. \<Sum>i<k. inverse ((z + of_nat i)^n))"
   7.638 +proof (rule weierstrass_m_test'_ev)
   7.639 +  def e \<equiv> "(1 + d / norm z)"
   7.640 +  def m \<equiv> "nat \<lceil>norm z * e\<rceil>"
   7.641 +  {
   7.642 +    fix t assume t: "t \<in> ball z d"
   7.643 +    have "norm t = norm (z + (t - z))" by simp
   7.644 +    also have "\<dots> \<le> norm z + norm (t - z)" by (rule norm_triangle_ineq)
   7.645 +    also from t have "norm (t - z) < d" by (simp add: dist_norm norm_minus_commute)
   7.646 +    finally have "norm t < norm z * e" using z by (simp add: divide_simps e_def)
   7.647 +  } note ball = this
   7.648 +  
   7.649 +  show "eventually (\<lambda>k. \<forall>t\<in>ball z d. norm (inverse ((t + of_nat k)^n)) \<le> 
   7.650 +            inverse (of_nat (k - m)^n)) sequentially"
   7.651 +    using eventually_gt_at_top[of m] apply eventually_elim
   7.652 +  proof (intro ballI)
   7.653 +    fix k :: nat and t :: 'a assume k: "k > m" and t: "t \<in> ball z d"
   7.654 +    from k have "real_of_nat (k - m) = of_nat k - of_nat m" by (simp add: of_nat_diff)
   7.655 +    also have "\<dots> \<le> norm (of_nat k :: 'a) - norm z * e" 
   7.656 +      unfolding m_def by (subst norm_of_nat) linarith
   7.657 +    also from ball[OF t] have "\<dots> \<le> norm (of_nat k :: 'a) - norm t" by simp
   7.658 +    also have "\<dots> \<le> norm (of_nat k + t)" by (rule norm_diff_ineq)
   7.659 +    finally have "inverse ((norm (t + of_nat k))^n) \<le> inverse (real_of_nat (k - m)^n)" using k n
   7.660 +      by (intro le_imp_inverse_le power_mono) (simp_all add: add_ac del: of_nat_Suc)
   7.661 +    thus "norm (inverse ((t + of_nat k)^n)) \<le> inverse (of_nat (k - m)^n)"
   7.662 +      by (simp add: norm_inverse norm_power power_inverse)
   7.663 +  qed
   7.664 +  
   7.665 +  have "summable (\<lambda>k. inverse ((real_of_nat k)^n))" 
   7.666 +    using inverse_power_summable[of n] n by simp
   7.667 +  hence "summable (\<lambda>k. inverse ((real_of_nat (k + m - m))^n))" by simp
   7.668 +  thus "summable (\<lambda>k. inverse ((real_of_nat (k - m))^n))" by (rule summable_offset)
   7.669 +qed
   7.670 +
   7.671 +lemma Polygamma_converges':
   7.672 +  fixes z :: "'a :: {real_normed_field,banach}"
   7.673 +  assumes z: "z \<noteq> 0" and n: "n \<ge> 2"
   7.674 +  shows "summable (\<lambda>k. inverse ((z + of_nat k)^n))"
   7.675 +  using uniformly_convergent_imp_convergent[OF Polygamma_converges[OF assms, of 1], of z]
   7.676 +  by (simp add: summable_iff_convergent)
   7.677 +
   7.678 +lemma has_field_derivative_ln_Gamma_complex [derivative_intros]:
   7.679 +  assumes z: "Re z > 0 \<or> Im z \<noteq> 0"
   7.680 +  shows   "(ln_Gamma has_field_derivative Digamma z) (at z)"
   7.681 +proof -
   7.682 +  have not_nonpos_Int [simp]: "t \<notin> \<int>\<^sub>\<le>\<^sub>0" if "Re t > 0" for t 
   7.683 +    using that by (auto elim!: nonpos_Ints_cases')
   7.684 +  from z have z': "z \<notin> \<int>\<^sub>\<le>\<^sub>0" and z'': "z \<noteq> 0" by (auto elim!: nonpos_Ints_cases)
   7.685 +  let ?f' = "\<lambda>z k. inverse (of_nat (Suc k)) - inverse (z + of_nat (Suc k))"
   7.686 +  let ?f = "\<lambda>z k. z / of_nat (Suc k) - ln (1 + z / of_nat (Suc k))" and ?F' = "\<lambda>z. \<Sum>n. ?f' z n"
   7.687 +  def d \<equiv> "min (norm z/2) (if Im z = 0 then Re z / 2 else abs (Im z) / 2)"
   7.688 +  from z have d: "d > 0" "norm z/2 \<ge> d" by (auto simp add: d_def)
   7.689 +  have ball: "Im t = 0 \<longrightarrow> Re t > 0" if "dist z t < d" for t 
   7.690 +    using Re_pos_or_Im_nz_in_ball[OF z, of t] that unfolding d_def by auto
   7.691 +  have sums: "(\<lambda>n. inverse (z + of_nat (Suc n)) - inverse (z + of_nat n)) sums 
   7.692 +                       (0 - inverse (z + of_nat 0))"
   7.693 +    by (intro telescope_sums filterlim_compose[OF tendsto_inverse_0]
   7.694 +              tendsto_add_filterlim_at_infinity[OF tendsto_const] tendsto_of_nat)
   7.695 +
   7.696 +  have "((\<lambda>z. \<Sum>n. ?f z n) has_field_derivative ?F' z) (at z)"
   7.697 +    using d z ln_Gamma_series'_aux[OF z']
   7.698 +    by (intro has_field_derivative_series'(2)[of "ball z d" _ _ z] uniformly_summable_deriv_ln_Gamma)
   7.699 +       (auto intro!: derivative_eq_intros add_pos_pos mult_pos_pos dest!: ball
   7.700 +             simp: field_simps Re_complex_div_gt_0 sums_iff Im_divide_of_nat
   7.701 +             simp del: of_nat_Suc)
   7.702 +  with z have "((\<lambda>z. (\<Sum>k. ?f z k) - euler_mascheroni * z - Ln z) has_field_derivative 
   7.703 +                   ?F' z - euler_mascheroni - inverse z) (at z)" 
   7.704 +    by (force intro!: derivative_eq_intros simp: Digamma_def)
   7.705 +  also have "?F' z - euler_mascheroni - inverse z = (?F' z + -inverse z) - euler_mascheroni" by simp
   7.706 +  also from sums have "-inverse z = (\<Sum>n. inverse (z + of_nat (Suc n)) - inverse (z + of_nat n))"
   7.707 +    by (simp add: sums_iff)
   7.708 +  also from sums summable_deriv_ln_Gamma[OF z''] 
   7.709 +    have "?F' z + \<dots> =  (\<Sum>n. inverse (of_nat (Suc n)) - inverse (z + of_nat n))"
   7.710 +    by (subst suminf_add) (simp_all add: add_ac sums_iff)
   7.711 +  also have "\<dots> - euler_mascheroni = Digamma z" by (simp add: Digamma_def)
   7.712 +  finally have "((\<lambda>z. (\<Sum>k. ?f z k) - euler_mascheroni * z - Ln z) 
   7.713 +                    has_field_derivative Digamma z) (at z)" .
   7.714 +  moreover from eventually_nhds_ball[OF d(1), of z]
   7.715 +    have "eventually (\<lambda>z. ln_Gamma z = (\<Sum>k. ?f z k) - euler_mascheroni * z - Ln z) (nhds z)"
   7.716 +  proof eventually_elim
   7.717 +    fix t assume "t \<in> ball z d"
   7.718 +    hence "t \<notin> \<int>\<^sub>\<le>\<^sub>0" by (auto dest!: ball elim!: nonpos_Ints_cases)
   7.719 +    from ln_Gamma_series'_aux[OF this] 
   7.720 +      show "ln_Gamma t = (\<Sum>k. ?f t k) - euler_mascheroni * t - Ln t" by (simp add: sums_iff)
   7.721 +  qed
   7.722 +  ultimately show ?thesis by (subst DERIV_cong_ev[OF refl _ refl])
   7.723 +qed
   7.724 +
   7.725 +declare has_field_derivative_ln_Gamma_complex[THEN DERIV_chain2, derivative_intros]
   7.726 +
   7.727 +
   7.728 +lemma Digamma_1 [simp]: "Digamma (1 :: 'a :: {real_normed_field,banach}) = - euler_mascheroni"
   7.729 +  by (simp add: Digamma_def)
   7.730 +  
   7.731 +lemma Digamma_plus1:
   7.732 +  assumes "z \<noteq> 0"
   7.733 +  shows   "Digamma (z+1) = Digamma z + 1/z"
   7.734 +proof -
   7.735 +  have sums: "(\<lambda>k. inverse (z + of_nat k) - inverse (z + of_nat (Suc k))) 
   7.736 +                  sums (inverse (z + of_nat 0) - 0)"
   7.737 +    by (intro telescope_sums'[OF filterlim_compose[OF tendsto_inverse_0]]
   7.738 +              tendsto_add_filterlim_at_infinity[OF tendsto_const] tendsto_of_nat)
   7.739 +  have "Digamma (z+1) = (\<Sum>k. inverse (of_nat (Suc k)) - inverse (z + of_nat (Suc k))) - 
   7.740 +          euler_mascheroni" (is "_ = suminf ?f - _") by (simp add: Digamma_def add_ac)
   7.741 +  also have "suminf ?f = (\<Sum>k. inverse (of_nat (Suc k)) - inverse (z + of_nat k)) +
   7.742 +                         (\<Sum>k. inverse (z + of_nat k) - inverse (z + of_nat (Suc k)))"
   7.743 +    using summable_Digamma[OF assms] sums by (subst suminf_add) (simp_all add: add_ac sums_iff)
   7.744 +  also have "(\<Sum>k. inverse (z + of_nat k) - inverse (z + of_nat (Suc k))) = 1/z"
   7.745 +    using sums by (simp add: sums_iff inverse_eq_divide)
   7.746 +  finally show ?thesis by (simp add: Digamma_def[of z])
   7.747 +qed
   7.748 +
   7.749 +lemma Polygamma_plus1:
   7.750 +  assumes "z \<noteq> 0"
   7.751 +  shows   "Polygamma n (z + 1) = Polygamma n z + (-1)^n * fact n / (z ^ Suc n)"
   7.752 +proof (cases "n = 0")
   7.753 +  assume n: "n \<noteq> 0"
   7.754 +  let ?f = "\<lambda>k. inverse ((z + of_nat k) ^ Suc n)"
   7.755 +  have "Polygamma n (z + 1) = (-1) ^ Suc n * fact n * (\<Sum>k. ?f (k+1))"
   7.756 +    using n by (simp add: Polygamma_def add_ac)
   7.757 +  also have "(\<Sum>k. ?f (k+1)) + (\<Sum>k<1. ?f k) = (\<Sum>k. ?f k)"
   7.758 +    using Polygamma_converges'[OF assms, of "Suc n"] n
   7.759 +    by (subst suminf_split_initial_segment [symmetric]) simp_all
   7.760 +  hence "(\<Sum>k. ?f (k+1)) = (\<Sum>k. ?f k) - inverse (z ^ Suc n)" by (simp add: algebra_simps)
   7.761 +  also have "(-1) ^ Suc n * fact n * ((\<Sum>k. ?f k) - inverse (z ^ Suc n)) = 
   7.762 +               Polygamma n z + (-1)^n * fact n / (z ^ Suc n)" using n
   7.763 +    by (simp add: inverse_eq_divide algebra_simps Polygamma_def)
   7.764 +  finally show ?thesis .
   7.765 +qed (insert assms, simp add: Digamma_plus1 inverse_eq_divide)
   7.766 +
   7.767 +lemma Digamma_of_nat: 
   7.768 +  "Digamma (of_nat (Suc n) :: 'a :: {real_normed_field,banach}) = harm n - euler_mascheroni"
   7.769 +proof (induction n)
   7.770 +  case (Suc n)
   7.771 +  have "Digamma (of_nat (Suc (Suc n)) :: 'a) = Digamma (of_nat (Suc n) + 1)" by simp
   7.772 +  also have "\<dots> = Digamma (of_nat (Suc n)) + inverse (of_nat (Suc n))" 
   7.773 +    by (subst Digamma_plus1) (simp_all add: inverse_eq_divide del: of_nat_Suc)
   7.774 +  also have "Digamma (of_nat (Suc n) :: 'a) = harm n - euler_mascheroni " by (rule Suc)
   7.775 +  also have "\<dots> + inverse (of_nat (Suc n)) = harm (Suc n) - euler_mascheroni"
   7.776 +    by (simp add: harm_Suc)
   7.777 +  finally show ?case .
   7.778 +qed (simp add: harm_def)
   7.779 +
   7.780 +lemma Digamma_numeral: "Digamma (numeral n) = harm (pred_numeral n) - euler_mascheroni"
   7.781 +  by (subst of_nat_numeral[symmetric], subst numeral_eq_Suc, subst Digamma_of_nat) (rule refl)
   7.782 +
   7.783 +lemma Polygamma_of_real: "x \<noteq> 0 \<Longrightarrow> Polygamma n (of_real x) = of_real (Polygamma n x)"
   7.784 +  unfolding Polygamma_def using summable_Digamma[of x] Polygamma_converges'[of x "Suc n"]
   7.785 +  by (simp_all add: suminf_of_real)
   7.786 +
   7.787 +lemma Polygamma_Real: "z \<in> \<real> \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> Polygamma n z \<in> \<real>"
   7.788 +  by (elim Reals_cases, hypsubst, subst Polygamma_of_real) simp_all
   7.789 +
   7.790 +lemma Digamma_half_integer:
   7.791 +  "Digamma (of_nat n + 1/2 :: 'a :: {real_normed_field,banach}) = 
   7.792 +      (\<Sum>k<n. 2 / (of_nat (2*k+1))) - euler_mascheroni - of_real (2 * ln 2)"
   7.793 +proof (induction n)
   7.794 +  case 0
   7.795 +  have "Digamma (1/2 :: 'a) = of_real (Digamma (1/2))" by (simp add: Polygamma_of_real [symmetric])
   7.796 +  also have "Digamma (1/2::real) = 
   7.797 +               (\<Sum>k. inverse (of_nat (Suc k)) - inverse (of_nat k + 1/2)) - euler_mascheroni" 
   7.798 +    by (simp add: Digamma_def add_ac)
   7.799 +  also have "(\<Sum>k. inverse (of_nat (Suc k) :: real) - inverse (of_nat k + 1/2)) =
   7.800 +             (\<Sum>k. inverse (1/2) * (inverse (2 * of_nat (Suc k)) - inverse (2 * of_nat k + 1)))"
   7.801 +    by (simp_all add: add_ac inverse_mult_distrib[symmetric] ring_distribs del: inverse_divide)
   7.802 +  also have "\<dots> = - 2 * ln 2" using sums_minus[OF alternating_harmonic_series_sums']
   7.803 +    by (subst suminf_mult) (simp_all add: algebra_simps sums_iff)
   7.804 +  finally show ?case by simp
   7.805 +next
   7.806 +  case (Suc n)
   7.807 +  have nz: "2 * of_nat n + (1:: 'a) \<noteq> 0"
   7.808 +     using of_nat_neq_0[of "2*n"] by (simp only: of_nat_Suc) (simp add: add_ac)
   7.809 +  hence nz': "of_nat n + (1/2::'a) \<noteq> 0" by (simp add: field_simps)
   7.810 +  have "Digamma (of_nat (Suc n) + 1/2 :: 'a) = Digamma (of_nat n + 1/2 + 1)" by simp
   7.811 +  also from nz' have "\<dots> = Digamma (of_nat n + 1 / 2) + 1 / (of_nat n + 1 / 2)"
   7.812 +    by (rule Digamma_plus1)
   7.813 +  also from nz nz' have "1 / (of_nat n + 1 / 2 :: 'a) = 2 / (2 * of_nat n + 1)"
   7.814 +    by (subst divide_eq_eq) simp_all
   7.815 +  also note Suc
   7.816 +  finally show ?case by (simp add: add_ac)
   7.817 +qed
   7.818 +
   7.819 +lemma Digamma_one_half: "Digamma (1/2) = - euler_mascheroni - of_real (2 * ln 2)"
   7.820 +  using Digamma_half_integer[of 0] by simp
   7.821 +
   7.822 +lemma Digamma_real_three_halves_pos: "Digamma (3/2 :: real) > 0"
   7.823 +proof -
   7.824 +  have "-Digamma (3/2 :: real) = -Digamma (of_nat 1 + 1/2)" by simp
   7.825 +  also have "\<dots> = 2 * ln 2 + euler_mascheroni - 2" by (subst Digamma_half_integer) simp
   7.826 +  also from euler_mascheroni_approx have "euler_mascheroni \<le> (0.58::real)" 
   7.827 +    by (simp add: abs_real_def split: split_if_asm)
   7.828 +  also from ln_2_bounds have "ln 2 < (0.7 :: real)" by simp
   7.829 +  finally show ?thesis by simp
   7.830 +qed
   7.831 +
   7.832 +
   7.833 +lemma has_field_derivative_Polygamma [derivative_intros]:
   7.834 +  fixes z :: "'a :: {real_normed_field,euclidean_space}"
   7.835 +  assumes z: "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
   7.836 +  shows "(Polygamma n has_field_derivative Polygamma (Suc n) z) (at z within A)"
   7.837 +proof (rule has_field_derivative_at_within, cases "n = 0")
   7.838 +  assume n: "n = 0"
   7.839 +  let ?f = "\<lambda>k z. inverse (of_nat (Suc k)) - inverse (z + of_nat k)"
   7.840 +  let ?F = "\<lambda>z. \<Sum>k. ?f k z" and ?f' = "\<lambda>k z. inverse ((z + of_nat k)\<^sup>2)"
   7.841 +  from no_nonpos_Int_in_ball'[OF z] guess d . note d = this
   7.842 +  from z have summable: "summable (\<lambda>k. inverse (of_nat (Suc k)) - inverse (z + of_nat k))"
   7.843 +    by (intro summable_Digamma) force
   7.844 +  from z have conv: "uniformly_convergent_on (ball z d) (\<lambda>k z. \<Sum>i<k. inverse ((z + of_nat i)\<^sup>2))"
   7.845 +    by (intro Polygamma_converges) auto
   7.846 +  with d have "summable (\<lambda>k. inverse ((z + of_nat k)\<^sup>2))" unfolding summable_iff_convergent
   7.847 +    by (auto dest!: uniformly_convergent_imp_convergent simp: summable_iff_convergent )
   7.848 +
   7.849 +  have "(?F has_field_derivative (\<Sum>k. ?f' k z)) (at z)"
   7.850 +  proof (rule has_field_derivative_series'[of "ball z d" _ _ z])
   7.851 +    fix k :: nat and t :: 'a assume t: "t \<in> ball z d"
   7.852 +    from t d(2)[of t] show "((\<lambda>z. ?f k z) has_field_derivative ?f' k t) (at t within ball z d)" 
   7.853 +      by (auto intro!: derivative_eq_intros simp: power2_eq_square simp del: of_nat_Suc 
   7.854 +               dest!: plus_of_nat_eq_0_imp elim!: nonpos_Ints_cases)
   7.855 +  qed (insert d(1) summable conv, (assumption|simp)+)
   7.856 +  with z show "(Polygamma n has_field_derivative Polygamma (Suc n) z) (at z)"
   7.857 +    unfolding Digamma_def [abs_def] Polygamma_def [abs_def] using n
   7.858 +    by (force simp: power2_eq_square intro!: derivative_eq_intros)
   7.859 +next
   7.860 +  assume n: "n \<noteq> 0"
   7.861 +  from z have z': "z \<noteq> 0" by auto
   7.862 +  from no_nonpos_Int_in_ball'[OF z] guess d . note d = this
   7.863 +  def n' \<equiv> "Suc n"
   7.864 +  from n have n': "n' \<ge> 2" by (simp add: n'_def)
   7.865 +  have "((\<lambda>z. \<Sum>k. inverse ((z + of_nat k) ^ n')) has_field_derivative
   7.866 +                (\<Sum>k. - of_nat n' * inverse ((z + of_nat k) ^ (n'+1)))) (at z)"
   7.867 +  proof (rule has_field_derivative_series'[of "ball z d" _ _ z])
   7.868 +    fix k :: nat and t :: 'a assume t: "t \<in> ball z d"
   7.869 +    with d have t': "t \<notin> \<int>\<^sub>\<le>\<^sub>0" "t \<noteq> 0" by auto
   7.870 +    show "((\<lambda>a. inverse ((a + of_nat k) ^ n')) has_field_derivative
   7.871 +                - of_nat n' * inverse ((t + of_nat k) ^ (n'+1))) (at t within ball z d)" using t'
   7.872 +      by (fastforce intro!: derivative_eq_intros simp: divide_simps power_diff dest: plus_of_nat_eq_0_imp)
   7.873 +  next
   7.874 +    have "uniformly_convergent_on (ball z d) 
   7.875 +              (\<lambda>k z. (- of_nat n' :: 'a) * (\<Sum>i<k. inverse ((z + of_nat i) ^ (n'+1))))" 
   7.876 +      using z' n by (intro uniformly_convergent_mult Polygamma_converges) (simp_all add: n'_def)
   7.877 +    thus "uniformly_convergent_on (ball z d) 
   7.878 +              (\<lambda>k z. \<Sum>i<k. - of_nat n' * inverse ((z + of_nat i :: 'a) ^ (n'+1)))"
   7.879 +      by (subst (asm) setsum_right_distrib) simp
   7.880 +  qed (insert Polygamma_converges'[OF z' n'] d, simp_all)
   7.881 +  also have "(\<Sum>k. - of_nat n' * inverse ((z + of_nat k) ^ (n' + 1))) =
   7.882 +               (- of_nat n') * (\<Sum>k. inverse ((z + of_nat k) ^ (n' + 1)))"
   7.883 +    using Polygamma_converges'[OF z', of "n'+1"] n' by (subst suminf_mult) simp_all
   7.884 +  finally have "((\<lambda>z. \<Sum>k. inverse ((z + of_nat k) ^ n')) has_field_derivative
   7.885 +                    - of_nat n' * (\<Sum>k. inverse ((z + of_nat k) ^ (n' + 1)))) (at z)" .
   7.886 +  from DERIV_cmult[OF this, of "(-1)^Suc n * fact n :: 'a"]
   7.887 +    show "(Polygamma n has_field_derivative Polygamma (Suc n) z) (at z)"
   7.888 +    unfolding n'_def Polygamma_def[abs_def] using n by (simp add: algebra_simps)
   7.889 +qed
   7.890 +
   7.891 +declare has_field_derivative_Polygamma[THEN DERIV_chain2, derivative_intros]
   7.892 +
   7.893 +lemma isCont_Polygamma [continuous_intros]: 
   7.894 +  fixes f :: "_ \<Rightarrow> 'a :: {real_normed_field,euclidean_space}"
   7.895 +  shows "isCont f z \<Longrightarrow> f z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> isCont (\<lambda>x. Polygamma n (f x)) z"
   7.896 +  by (rule isCont_o2[OF _  DERIV_isCont[OF has_field_derivative_Polygamma]])
   7.897 +
   7.898 +lemma continuous_on_Polygamma: 
   7.899 +  "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> continuous_on A (Polygamma n :: _ \<Rightarrow> 'a :: {real_normed_field,euclidean_space})"
   7.900 +  by (intro continuous_at_imp_continuous_on isCont_Polygamma[OF continuous_ident] ballI) blast
   7.901 +
   7.902 +lemma isCont_ln_Gamma_complex [continuous_intros]: 
   7.903 +  "isCont f z \<Longrightarrow> Re (f z) > 0 \<or> Im (f z) \<noteq> 0 \<Longrightarrow> isCont (\<lambda>z. ln_Gamma (f z)) z"
   7.904 +  by (rule isCont_o2[OF _  DERIV_isCont[OF has_field_derivative_ln_Gamma_complex]])
   7.905 +
   7.906 +lemma continuous_on_ln_Gamma_complex [continuous_intros]:
   7.907 +   "A \<inter> {z. Re z \<le> 0 \<and> Im z = 0} = {} \<Longrightarrow> continuous_on A ln_Gamma"
   7.908 +  by (intro continuous_at_imp_continuous_on ballI isCont_ln_Gamma_complex[OF continuous_ident]) 
   7.909 +     fastforce
   7.910 +
   7.911 +
   7.912 +text \<open>
   7.913 +  We define a type class that captures all the fundamental properties of the inverse Gamma function 
   7.914 +  and defines the Gamma function upon that. This allows us to instantiate the type class both for 
   7.915 +  the reals and for the complex numbers with a minimal amount of proof duplication. 
   7.916 +\<close>
   7.917 +
   7.918 +class Gamma = real_normed_field + complete_space +
   7.919 +  fixes rGamma :: "'a \<Rightarrow> 'a"
   7.920 +  assumes rGamma_eq_zero_iff_aux: "rGamma z = 0 \<longleftrightarrow> (\<exists>n. z = - of_nat n)"
   7.921 +  assumes differentiable_rGamma_aux1: 
   7.922 +    "(\<And>n. z \<noteq> - of_nat n) \<Longrightarrow>
   7.923 +     let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (z + of_nat k)) 
   7.924 +               \<longlonglongrightarrow> d) - scaleR euler_mascheroni 1
   7.925 +     in  filterlim (\<lambda>y. (rGamma y - rGamma z + rGamma z * d * (y - z)) /\<^sub>R 
   7.926 +                        norm (y - z)) (nhds 0) (at z)"
   7.927 +  assumes differentiable_rGamma_aux2: 
   7.928 +    "let z = - of_nat n
   7.929 +     in  filterlim (\<lambda>y. (rGamma y - rGamma z - (-1)^n * (setprod of_nat {1..n}) * (y - z)) /\<^sub>R 
   7.930 +                        norm (y - z)) (nhds 0) (at z)"
   7.931 +  assumes rGamma_series_aux: "(\<And>n. z \<noteq> - of_nat n) \<Longrightarrow> 
   7.932 +             let fact' = (\<lambda>n. setprod of_nat {1..n});
   7.933 +                 exp = (\<lambda>x. THE e. (\<lambda>n. \<Sum>k<n. x^k /\<^sub>R fact k) \<longlonglongrightarrow> e);
   7.934 +                 pochhammer' = (\<lambda>a n. (\<Prod>n = 0..n. a + of_nat n))
   7.935 +             in  filterlim (\<lambda>n. pochhammer' z n / (fact' n * exp (z * (ln (of_nat n) *\<^sub>R 1)))) 
   7.936 +                     (nhds (rGamma z)) sequentially"
   7.937 +begin
   7.938 +subclass banach ..
   7.939 +end
   7.940 +
   7.941 +definition "Gamma z = inverse (rGamma z)"
   7.942 +
   7.943 +
   7.944 +subsection \<open>Basic properties\<close>
   7.945 +
   7.946 +lemma Gamma_nonpos_Int: "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma z = 0"
   7.947 +  and rGamma_nonpos_Int: "z \<in> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> rGamma z = 0"
   7.948 +  using rGamma_eq_zero_iff_aux[of z] unfolding Gamma_def by (auto elim!: nonpos_Ints_cases')
   7.949 +
   7.950 +lemma Gamma_nonzero: "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma z \<noteq> 0"
   7.951 +  and rGamma_nonzero: "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> rGamma z \<noteq> 0"
   7.952 +  using rGamma_eq_zero_iff_aux[of z] unfolding Gamma_def by (auto elim!: nonpos_Ints_cases')
   7.953 +
   7.954 +lemma Gamma_eq_zero_iff: "Gamma z = 0 \<longleftrightarrow> z \<in> \<int>\<^sub>\<le>\<^sub>0" 
   7.955 +  and rGamma_eq_zero_iff: "rGamma z = 0 \<longleftrightarrow> z \<in> \<int>\<^sub>\<le>\<^sub>0"
   7.956 +  using rGamma_eq_zero_iff_aux[of z] unfolding Gamma_def by (auto elim!: nonpos_Ints_cases')
   7.957 +
   7.958 +lemma rGamma_inverse_Gamma: "rGamma z = inverse (Gamma z)"
   7.959 +  unfolding Gamma_def by simp
   7.960 +
   7.961 +lemma rGamma_series_LIMSEQ [tendsto_intros]:
   7.962 +  "rGamma_series z \<longlonglongrightarrow> rGamma z"
   7.963 +proof (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0")
   7.964 +  case False
   7.965 +  hence "z \<noteq> - of_nat n" for n by auto
   7.966 +  from rGamma_series_aux[OF this] show ?thesis
   7.967 +    by (simp add: rGamma_series_def[abs_def] fact_altdef pochhammer_Suc_setprod
   7.968 +                  exp_def of_real_def[symmetric] suminf_def sums_def[abs_def])
   7.969 +qed (insert rGamma_eq_zero_iff[of z], simp_all add: rGamma_series_nonpos_Ints_LIMSEQ)
   7.970 +
   7.971 +lemma Gamma_series_LIMSEQ [tendsto_intros]: 
   7.972 +  "Gamma_series z \<longlonglongrightarrow> Gamma z"
   7.973 +proof (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0")
   7.974 +  case False
   7.975 +  hence "(\<lambda>n. inverse (rGamma_series z n)) \<longlonglongrightarrow> inverse (rGamma z)"
   7.976 +    by (intro tendsto_intros) (simp_all add: rGamma_eq_zero_iff)
   7.977 +  also have "(\<lambda>n. inverse (rGamma_series z n)) = Gamma_series z" 
   7.978 +    by (simp add: rGamma_series_def Gamma_series_def[abs_def])
   7.979 +  finally show ?thesis by (simp add: Gamma_def)
   7.980 +qed (insert Gamma_eq_zero_iff[of z], simp_all add: Gamma_series_nonpos_Ints_LIMSEQ)
   7.981 +
   7.982 +lemma Gamma_altdef: "Gamma z = lim (Gamma_series z)"
   7.983 +  using Gamma_series_LIMSEQ[of z] by (simp add: limI)
   7.984 +
   7.985 +lemma rGamma_1 [simp]: "rGamma 1 = 1"
   7.986 +proof -
   7.987 +  have A: "eventually (\<lambda>n. rGamma_series 1 n = of_nat (Suc n) / of_nat n) sequentially"
   7.988 +    using eventually_gt_at_top[of "0::nat"]
   7.989 +    by (force elim!: eventually_mono simp: rGamma_series_def exp_of_real pochhammer_fact 
   7.990 +                    divide_simps pochhammer_rec' dest!: pochhammer_eq_0_imp_nonpos_Int)
   7.991 +  have "rGamma_series 1 \<longlonglongrightarrow> 1" by (subst tendsto_cong[OF A]) (rule LIMSEQ_Suc_n_over_n)
   7.992 +  moreover have "rGamma_series 1 \<longlonglongrightarrow> rGamma 1" by (rule tendsto_intros)
   7.993 +  ultimately show ?thesis by (intro LIMSEQ_unique)
   7.994 +qed
   7.995 +
   7.996 +lemma rGamma_plus1: "z * rGamma (z + 1) = rGamma z"
   7.997 +proof -
   7.998 +  let ?f = "\<lambda>n. (z + 1) * inverse (of_nat n) + 1"
   7.999 +  have "eventually (\<lambda>n. ?f n * rGamma_series z n = z * rGamma_series (z + 1) n) sequentially"
  7.1000 +    using eventually_gt_at_top[of "0::nat"]
  7.1001 +  proof eventually_elim
  7.1002 +    fix n :: nat assume n: "n > 0"
  7.1003 +    hence "z * rGamma_series (z + 1) n = inverse (of_nat n) *
  7.1004 +             pochhammer z (Suc (Suc n)) / (fact n * exp (z * of_real (ln (of_nat n))))"
  7.1005 +      by (subst pochhammer_rec) (simp add: rGamma_series_def field_simps exp_add exp_of_real)
  7.1006 +    also from n have "\<dots> = ?f n * rGamma_series z n"
  7.1007 +      by (subst pochhammer_rec') (simp_all add: divide_simps rGamma_series_def add_ac)
  7.1008 +    finally show "?f n * rGamma_series z n = z * rGamma_series (z + 1) n" ..
  7.1009 +  qed
  7.1010 +  moreover have "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> ((z+1) * 0 + 1) * rGamma z"
  7.1011 +    by (intro tendsto_intros lim_inverse_n)
  7.1012 +  hence "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> rGamma z" by simp
  7.1013 +  ultimately have "(\<lambda>n. z * rGamma_series (z + 1) n) \<longlonglongrightarrow> rGamma z"
  7.1014 +    by (rule Lim_transform_eventually)
  7.1015 +  moreover have "(\<lambda>n. z * rGamma_series (z + 1) n) \<longlonglongrightarrow> z * rGamma (z + 1)"
  7.1016 +    by (intro tendsto_intros)
  7.1017 +  ultimately show "z * rGamma (z + 1) = rGamma z" using LIMSEQ_unique by blast
  7.1018 +qed
  7.1019 +
  7.1020 +
  7.1021 +lemma pochhammer_rGamma: "rGamma z = pochhammer z n * rGamma (z + of_nat n)"
  7.1022 +proof (induction n arbitrary: z)
  7.1023 +  case (Suc n z)
  7.1024 +  have "rGamma z = pochhammer z n * rGamma (z + of_nat n)" by (rule Suc.IH)
  7.1025 +  also note rGamma_plus1 [symmetric]
  7.1026 +  finally show ?case by (simp add: add_ac pochhammer_rec')
  7.1027 +qed simp_all
  7.1028 +
  7.1029 +lemma Gamma_plus1: "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma (z + 1) = z * Gamma z"
  7.1030 +  using rGamma_plus1[of z] by (simp add: rGamma_inverse_Gamma field_simps Gamma_eq_zero_iff)
  7.1031 +
  7.1032 +lemma pochhammer_Gamma: "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> pochhammer z n = Gamma (z + of_nat n) / Gamma z"
  7.1033 +  using pochhammer_rGamma[of z] 
  7.1034 +  by (simp add: rGamma_inverse_Gamma Gamma_eq_zero_iff field_simps)
  7.1035 +
  7.1036 +lemma Gamma_0 [simp]: "Gamma 0 = 0" 
  7.1037 +  and rGamma_0 [simp]: "rGamma 0 = 0"
  7.1038 +  and Gamma_neg_1 [simp]: "Gamma (- 1) = 0"
  7.1039 +  and rGamma_neg_1 [simp]: "rGamma (- 1) = 0"
  7.1040 +  and Gamma_neg_numeral [simp]: "Gamma (- numeral n) = 0"
  7.1041 +  and rGamma_neg_numeral [simp]: "rGamma (- numeral n) = 0"
  7.1042 +  and Gamma_neg_of_nat [simp]: "Gamma (- of_nat m) = 0"
  7.1043 +  and rGamma_neg_of_nat [simp]: "rGamma (- of_nat m) = 0"
  7.1044 +  by (simp_all add: rGamma_eq_zero_iff Gamma_eq_zero_iff)
  7.1045 +
  7.1046 +lemma Gamma_1 [simp]: "Gamma 1 = 1" unfolding Gamma_def by simp
  7.1047 +
  7.1048 +lemma Gamma_fact: "Gamma (of_nat (Suc n)) = fact n"
  7.1049 +  by (simp add: pochhammer_fact pochhammer_Gamma of_nat_in_nonpos_Ints_iff)
  7.1050 +
  7.1051 +lemma Gamma_numeral: "Gamma (numeral n) = fact (pred_numeral n)"
  7.1052 +  by (subst of_nat_numeral[symmetric], subst numeral_eq_Suc, subst Gamma_fact) (rule refl)
  7.1053 +
  7.1054 +lemma Gamma_of_int: "Gamma (of_int n) = (if n > 0 then fact (nat (n - 1)) else 0)"
  7.1055 +proof (cases "n > 0")
  7.1056 +  case True
  7.1057 +  hence "Gamma (of_int n) = Gamma (of_nat (Suc (nat (n - 1))))" by (subst of_nat_Suc) simp_all
  7.1058 +  with True show ?thesis by (subst (asm) Gamma_fact) simp
  7.1059 +qed (simp_all add: Gamma_eq_zero_iff nonpos_Ints_of_int)
  7.1060 +
  7.1061 +lemma rGamma_of_int: "rGamma (of_int n) = (if n > 0 then inverse (fact (nat (n - 1))) else 0)"
  7.1062 +  by (simp add: Gamma_of_int rGamma_inverse_Gamma)
  7.1063 +
  7.1064 +lemma Gamma_seriesI:
  7.1065 +  assumes "(\<lambda>n. g n / Gamma_series z n) \<longlonglongrightarrow> 1"
  7.1066 +  shows   "g \<longlonglongrightarrow> Gamma z"
  7.1067 +proof (rule Lim_transform_eventually)
  7.1068 +  have "1/2 > (0::real)" by simp
  7.1069 +  from tendstoD[OF assms, OF this] 
  7.1070 +    show "eventually (\<lambda>n. g n / Gamma_series z n * Gamma_series z n = g n) sequentially"
  7.1071 +    by (force elim!: eventually_mono simp: dist_real_def dist_0_norm)
  7.1072 +  from assms have "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> 1 * Gamma z"
  7.1073 +    by (intro tendsto_intros)
  7.1074 +  thus "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> Gamma z" by simp
  7.1075 +qed
  7.1076 +
  7.1077 +lemma Gamma_seriesI':
  7.1078 +  assumes "f \<longlonglongrightarrow> rGamma z"
  7.1079 +  assumes "(\<lambda>n. g n * f n) \<longlonglongrightarrow> 1"
  7.1080 +  assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1081 +  shows   "g \<longlonglongrightarrow> Gamma z"
  7.1082 +proof (rule Lim_transform_eventually)
  7.1083 +  have "1/2 > (0::real)" by simp
  7.1084 +  from tendstoD[OF assms(2), OF this] show "eventually (\<lambda>n. g n * f n / f n = g n) sequentially"
  7.1085 +    by (force elim!: eventually_mono simp: dist_real_def dist_0_norm)
  7.1086 +  from assms have "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> 1 / rGamma z"
  7.1087 +    by (intro tendsto_divide assms) (simp_all add: rGamma_eq_zero_iff)
  7.1088 +  thus "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> Gamma z" by (simp add: Gamma_def divide_inverse)
  7.1089 +qed
  7.1090 +
  7.1091 +lemma Gamma_series'_LIMSEQ: "Gamma_series' z \<longlonglongrightarrow> Gamma z"
  7.1092 +  by (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0") (simp_all add: Gamma_nonpos_Int Gamma_seriesI[OF Gamma_series_Gamma_series'] 
  7.1093 +                                      Gamma_series'_nonpos_Ints_LIMSEQ[of z])
  7.1094 +
  7.1095 +
  7.1096 +subsection \<open>Differentiability\<close>
  7.1097 +
  7.1098 +lemma has_field_derivative_rGamma_no_nonpos_int: 
  7.1099 +  assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1100 +  shows   "(rGamma has_field_derivative -rGamma z * Digamma z) (at z within A)"
  7.1101 +proof (rule has_field_derivative_at_within)
  7.1102 +  from assms have "z \<noteq> - of_nat n" for n by auto
  7.1103 +  from differentiable_rGamma_aux1[OF this] 
  7.1104 +    show "(rGamma has_field_derivative -rGamma z * Digamma z) (at z)"
  7.1105 +         unfolding Digamma_def suminf_def sums_def[abs_def]
  7.1106 +                   has_field_derivative_def has_derivative_def netlimit_at
  7.1107 +    by (simp add: Let_def bounded_linear_mult_right mult_ac of_real_def [symmetric])
  7.1108 +qed
  7.1109 +
  7.1110 +lemma has_field_derivative_rGamma_nonpos_int: 
  7.1111 +  "(rGamma has_field_derivative (-1)^n * fact n) (at (- of_nat n) within A)" 
  7.1112 +  apply (rule has_field_derivative_at_within)
  7.1113 +  using differentiable_rGamma_aux2[of n]
  7.1114 +  unfolding Let_def has_field_derivative_def has_derivative_def netlimit_at
  7.1115 +  by (simp only: bounded_linear_mult_right mult_ac of_real_def [symmetric] fact_altdef)
  7.1116 +
  7.1117 +lemma has_field_derivative_rGamma [derivative_intros]: 
  7.1118 +  "(rGamma has_field_derivative (if z \<in> \<int>\<^sub>\<le>\<^sub>0 then (-1)^(nat \<lfloor>norm z\<rfloor>) * fact (nat \<lfloor>norm z\<rfloor>)
  7.1119 +      else -rGamma z * Digamma z)) (at z within A)"
  7.1120 +using has_field_derivative_rGamma_no_nonpos_int[of z A]
  7.1121 +      has_field_derivative_rGamma_nonpos_int[of "nat \<lfloor>norm z\<rfloor>" A]
  7.1122 +  by (auto elim!: nonpos_Ints_cases')
  7.1123 +
  7.1124 +declare has_field_derivative_rGamma_no_nonpos_int [THEN DERIV_chain2, derivative_intros]
  7.1125 +declare has_field_derivative_rGamma [THEN DERIV_chain2, derivative_intros]
  7.1126 +declare has_field_derivative_rGamma_nonpos_int [derivative_intros]
  7.1127 +declare has_field_derivative_rGamma_no_nonpos_int [derivative_intros]
  7.1128 +declare has_field_derivative_rGamma [derivative_intros]
  7.1129 +
  7.1130 +
  7.1131 +lemma has_field_derivative_Gamma [derivative_intros]: 
  7.1132 +  "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> (Gamma has_field_derivative Gamma z * Digamma z) (at z within A)"
  7.1133 +  unfolding Gamma_def [abs_def]
  7.1134 +  by (fastforce intro!: derivative_eq_intros simp: rGamma_eq_zero_iff)
  7.1135 +
  7.1136 +declare has_field_derivative_Gamma[THEN DERIV_chain2, derivative_intros]
  7.1137 +
  7.1138 +(* TODO: Hide ugly facts properly *)
  7.1139 +hide_fact rGamma_eq_zero_iff_aux differentiable_rGamma_aux1 differentiable_rGamma_aux2
  7.1140 +          differentiable_rGamma_aux2 rGamma_series_aux Gamma_class.rGamma_eq_zero_iff_aux
  7.1141 +
  7.1142 +
  7.1143 +
  7.1144 +(* TODO: differentiable etc. *)
  7.1145 +
  7.1146 +
  7.1147 +subsection \<open>Continuity\<close>
  7.1148 +
  7.1149 +lemma continuous_on_rGamma [continuous_intros]: "continuous_on A rGamma"
  7.1150 +  by (rule DERIV_continuous_on has_field_derivative_rGamma)+
  7.1151 +
  7.1152 +lemma continuous_on_Gamma [continuous_intros]: "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> continuous_on A Gamma"
  7.1153 +  by (rule DERIV_continuous_on has_field_derivative_Gamma)+ blast
  7.1154 +
  7.1155 +lemma isCont_rGamma [continuous_intros]: 
  7.1156 +  "isCont f z \<Longrightarrow> isCont (\<lambda>x. rGamma (f x)) z"
  7.1157 +  by (rule isCont_o2[OF _  DERIV_isCont[OF has_field_derivative_rGamma]])
  7.1158 +
  7.1159 +lemma isCont_Gamma [continuous_intros]: 
  7.1160 +  "isCont f z \<Longrightarrow> f z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> isCont (\<lambda>x. Gamma (f x)) z"
  7.1161 +  by (rule isCont_o2[OF _  DERIV_isCont[OF has_field_derivative_Gamma]])
  7.1162 +
  7.1163 +
  7.1164 +
  7.1165 +text \<open>The complex Gamma function\<close>
  7.1166 +
  7.1167 +instantiation complex :: Gamma
  7.1168 +begin
  7.1169 +
  7.1170 +definition rGamma_complex :: "complex \<Rightarrow> complex" where
  7.1171 +  "rGamma_complex z = lim (rGamma_series z)"
  7.1172 +
  7.1173 +lemma rGamma_series_complex_converges: 
  7.1174 +        "convergent (rGamma_series (z :: complex))" (is "?thesis1")
  7.1175 +  and rGamma_complex_altdef:
  7.1176 +        "rGamma z = (if z \<in> \<int>\<^sub>\<le>\<^sub>0 then 0 else exp (-ln_Gamma z))" (is "?thesis2")
  7.1177 +proof -
  7.1178 +  have "?thesis1 \<and> ?thesis2"
  7.1179 +  proof (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0")
  7.1180 +    case False
  7.1181 +    have "rGamma_series z \<longlonglongrightarrow> exp (- ln_Gamma z)"
  7.1182 +    proof (rule Lim_transform_eventually)
  7.1183 +      from ln_Gamma_series_complex_converges'[OF False] guess d by (elim exE conjE)
  7.1184 +      from this(1) uniformly_convergent_imp_convergent[OF this(2), of z] 
  7.1185 +        have "ln_Gamma_series z \<longlonglongrightarrow> lim (ln_Gamma_series z)" by (simp add: convergent_LIMSEQ_iff)
  7.1186 +      thus "(\<lambda>n. exp (-ln_Gamma_series z n)) \<longlonglongrightarrow> exp (- ln_Gamma z)"
  7.1187 +        unfolding convergent_def ln_Gamma_def by (intro tendsto_exp tendsto_minus)
  7.1188 +      from eventually_gt_at_top[of "0::nat"] exp_ln_Gamma_series_complex False
  7.1189 +        show "eventually (\<lambda>n. exp (-ln_Gamma_series z n) = rGamma_series z n) sequentially"
  7.1190 +        by (force elim!: eventually_mono simp: exp_minus Gamma_series_def rGamma_series_def)
  7.1191 +    qed
  7.1192 +    with False show ?thesis
  7.1193 +      by (auto simp: convergent_def rGamma_complex_def intro!: limI)
  7.1194 +  next
  7.1195 +    case True
  7.1196 +    then obtain k where "z = - of_nat k" by (erule nonpos_Ints_cases')
  7.1197 +    also have "rGamma_series \<dots> \<longlonglongrightarrow> 0"
  7.1198 +      by (subst tendsto_cong[OF rGamma_series_minus_of_nat]) (simp_all add: convergent_const)
  7.1199 +    finally show ?thesis using True
  7.1200 +      by (auto simp: rGamma_complex_def convergent_def intro!: limI)
  7.1201 +  qed
  7.1202 +  thus "?thesis1" "?thesis2" by blast+
  7.1203 +qed
  7.1204 +
  7.1205 +context
  7.1206 +begin
  7.1207 +
  7.1208 +(* TODO: duplication *)
  7.1209 +private lemma rGamma_complex_plus1: "z * rGamma (z + 1) = rGamma (z :: complex)"
  7.1210 +proof -
  7.1211 +  let ?f = "\<lambda>n. (z + 1) * inverse (of_nat n) + 1"
  7.1212 +  have "eventually (\<lambda>n. ?f n * rGamma_series z n = z * rGamma_series (z + 1) n) sequentially"
  7.1213 +    using eventually_gt_at_top[of "0::nat"]
  7.1214 +  proof eventually_elim
  7.1215 +    fix n :: nat assume n: "n > 0"
  7.1216 +    hence "z * rGamma_series (z + 1) n = inverse (of_nat n) *
  7.1217 +             pochhammer z (Suc (Suc n)) / (fact n * exp (z * of_real (ln (of_nat n))))"
  7.1218 +      by (subst pochhammer_rec) (simp add: rGamma_series_def field_simps exp_add exp_of_real)
  7.1219 +    also from n have "\<dots> = ?f n * rGamma_series z n"
  7.1220 +      by (subst pochhammer_rec') (simp_all add: divide_simps rGamma_series_def add_ac)
  7.1221 +    finally show "?f n * rGamma_series z n = z * rGamma_series (z + 1) n" ..
  7.1222 +  qed
  7.1223 +  moreover have "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> ((z+1) * 0 + 1) * rGamma z"
  7.1224 +    using rGamma_series_complex_converges
  7.1225 +    by (intro tendsto_intros lim_inverse_n) 
  7.1226 +       (simp_all add: convergent_LIMSEQ_iff rGamma_complex_def)
  7.1227 +  hence "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> rGamma z" by simp
  7.1228 +  ultimately have "(\<lambda>n. z * rGamma_series (z + 1) n) \<longlonglongrightarrow> rGamma z"
  7.1229 +    by (rule Lim_transform_eventually)
  7.1230 +  moreover have "(\<lambda>n. z * rGamma_series (z + 1) n) \<longlonglongrightarrow> z * rGamma (z + 1)"
  7.1231 +    using rGamma_series_complex_converges 
  7.1232 +    by (auto intro!: tendsto_mult simp: rGamma_complex_def convergent_LIMSEQ_iff)
  7.1233 +  ultimately show "z * rGamma (z + 1) = rGamma z" using LIMSEQ_unique by blast
  7.1234 +qed
  7.1235 +
  7.1236 +private lemma has_field_derivative_rGamma_complex_no_nonpos_Int:
  7.1237 +  assumes "(z :: complex) \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1238 +  shows   "(rGamma has_field_derivative - rGamma z * Digamma z) (at z)"
  7.1239 +proof -
  7.1240 +  have diff: "(rGamma has_field_derivative - rGamma z * Digamma z) (at z)" if "Re z > 0" for z 
  7.1241 +  proof (subst DERIV_cong_ev[OF refl _ refl])
  7.1242 +    from that have "eventually (\<lambda>t. t \<in> ball z (Re z/2)) (nhds z)" 
  7.1243 +      by (intro eventually_nhds_in_nhd) simp_all
  7.1244 +    thus "eventually (\<lambda>t. rGamma t = exp (- ln_Gamma t)) (nhds z)"
  7.1245 +      using no_nonpos_Int_in_ball_complex[OF that]
  7.1246 +      by (auto elim!: eventually_mono simp: rGamma_complex_altdef)
  7.1247 +  next
  7.1248 +    show "((\<lambda>t. exp (- ln_Gamma t)) has_field_derivative (-rGamma z * Digamma z)) (at z)"
  7.1249 +      using that by (force elim!: nonpos_Ints_cases intro!: derivative_eq_intros 
  7.1250 +                           simp: rGamma_complex_altdef)
  7.1251 +  qed
  7.1252 +  
  7.1253 +  from assms show "(rGamma has_field_derivative - rGamma z * Digamma z) (at z)"
  7.1254 +  proof (induction "nat \<lfloor>1 - Re z\<rfloor>" arbitrary: z)
  7.1255 +    case (Suc n z)
  7.1256 +    from Suc.prems have z: "z \<noteq> 0" by auto
  7.1257 +    from Suc.hyps have "n = nat \<lfloor>- Re z\<rfloor>" by linarith
  7.1258 +    hence A: "n = nat \<lfloor>1 - Re (z + 1)\<rfloor>" by simp
  7.1259 +    from Suc.prems have B: "z + 1 \<notin> \<int>\<^sub>\<le>\<^sub>0" by (force dest: plus_one_in_nonpos_Ints_imp)
  7.1260 +    
  7.1261 +    have "((\<lambda>z. z * (rGamma \<circ> (\<lambda>z. z + 1)) z) has_field_derivative 
  7.1262 +      -rGamma (z + 1) * (Digamma (z + 1) * z - 1)) (at z)"
  7.1263 +      by (rule derivative_eq_intros DERIV_chain Suc refl A B)+ (simp add: algebra_simps)
  7.1264 +    also have "(\<lambda>z. z * (rGamma \<circ> (\<lambda>z. z + 1 :: complex)) z) = rGamma" 
  7.1265 +      by (simp add: rGamma_complex_plus1)
  7.1266 +    also from z have "Digamma (z + 1) * z - 1 = z * Digamma z"
  7.1267 +      by (subst Digamma_plus1) (simp_all add: field_simps)
  7.1268 +    also have "-rGamma (z + 1) * (z * Digamma z) = -rGamma z * Digamma z"
  7.1269 +      by (simp add: rGamma_complex_plus1[of z, symmetric])
  7.1270 +    finally show ?case .
  7.1271 +  qed (intro diff, simp)
  7.1272 +qed
  7.1273 +
  7.1274 +private lemma rGamma_complex_1: "rGamma (1 :: complex) = 1"
  7.1275 +proof -
  7.1276 +  have A: "eventually (\<lambda>n. rGamma_series 1 n = of_nat (Suc n) / of_nat n) sequentially"
  7.1277 +    using eventually_gt_at_top[of "0::nat"]
  7.1278 +    by (force elim!: eventually_mono simp: rGamma_series_def exp_of_real pochhammer_fact 
  7.1279 +                    divide_simps pochhammer_rec' dest!: pochhammer_eq_0_imp_nonpos_Int)
  7.1280 +  have "rGamma_series 1 \<longlonglongrightarrow> 1" by (subst tendsto_cong[OF A]) (rule LIMSEQ_Suc_n_over_n)
  7.1281 +  thus "rGamma 1 = (1 :: complex)" unfolding rGamma_complex_def by (rule limI)
  7.1282 +qed
  7.1283 +
  7.1284 +private lemma has_field_derivative_rGamma_complex_nonpos_Int:
  7.1285 +  "(rGamma has_field_derivative (-1)^n * fact n) (at (- of_nat n :: complex))"
  7.1286 +proof (induction n)
  7.1287 +  case 0
  7.1288 +  have A: "(0::complex) + 1 \<notin> \<int>\<^sub>\<le>\<^sub>0" by simp
  7.1289 +  have "((\<lambda>z. z * (rGamma \<circ> (\<lambda>z. z + 1 :: complex)) z) has_field_derivative 1) (at 0)"
  7.1290 +    by (rule derivative_eq_intros DERIV_chain refl
  7.1291 +             has_field_derivative_rGamma_complex_no_nonpos_Int A)+ (simp add: rGamma_complex_1)
  7.1292 +    thus ?case by (simp add: rGamma_complex_plus1)
  7.1293 +next
  7.1294 +  case (Suc n)
  7.1295 +  hence A: "(rGamma has_field_derivative (-1)^n * fact n)  
  7.1296 +                (at (- of_nat (Suc n) + 1 :: complex))" by simp
  7.1297 +   have "((\<lambda>z. z * (rGamma \<circ> (\<lambda>z. z + 1 :: complex)) z) has_field_derivative 
  7.1298 +             (- 1) ^ Suc n * fact (Suc n)) (at (- of_nat (Suc n)))"
  7.1299 +     by (rule derivative_eq_intros refl A DERIV_chain)+ 
  7.1300 +        (simp add: algebra_simps rGamma_complex_altdef)
  7.1301 +  thus ?case by (simp add: rGamma_complex_plus1)
  7.1302 +qed
  7.1303 +
  7.1304 +instance proof
  7.1305 +  fix z :: complex show "(rGamma z = 0) \<longleftrightarrow> (\<exists>n. z = - of_nat n)"
  7.1306 +    by (auto simp: rGamma_complex_altdef elim!: nonpos_Ints_cases')
  7.1307 +next
  7.1308 +  fix z :: complex assume "\<And>n. z \<noteq> - of_nat n"
  7.1309 +  hence "z \<notin> \<int>\<^sub>\<le>\<^sub>0" by (auto elim!: nonpos_Ints_cases')
  7.1310 +  from has_field_derivative_rGamma_complex_no_nonpos_Int[OF this]
  7.1311 +    show "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (z + of_nat k))
  7.1312 +                       \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma z + 
  7.1313 +              rGamma z * d * (y - z)) /\<^sub>R  cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  7.1314 +      by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  7.1315 +                    netlimit_at of_real_def[symmetric] suminf_def)
  7.1316 +next
  7.1317 +  fix n :: nat
  7.1318 +  from has_field_derivative_rGamma_complex_nonpos_Int[of n]
  7.1319 +  show "let z = - of_nat n in (\<lambda>y. (rGamma y - rGamma z - (- 1) ^ n * setprod of_nat {1..n} *
  7.1320 +                  (y - z)) /\<^sub>R cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  7.1321 +    by (simp add: has_field_derivative_def has_derivative_def fact_altdef netlimit_at Let_def)
  7.1322 +next
  7.1323 +  fix z :: complex
  7.1324 +  from rGamma_series_complex_converges[of z] have "rGamma_series z \<longlonglongrightarrow> rGamma z"
  7.1325 +    by (simp add: convergent_LIMSEQ_iff rGamma_complex_def)
  7.1326 +  thus "let fact' = \<lambda>n. setprod of_nat {1..n}; 
  7.1327 +            exp = \<lambda>x. THE e. (\<lambda>n. \<Sum>k<n. x ^ k /\<^sub>R fact k) \<longlonglongrightarrow> e;
  7.1328 +            pochhammer' = \<lambda>a n. \<Prod>n = 0..n. a + of_nat n
  7.1329 +        in  (\<lambda>n. pochhammer' z n / (fact' n * exp (z * ln (real_of_nat n) *\<^sub>R 1))) \<longlonglongrightarrow> rGamma z"
  7.1330 +    by (simp add: fact_altdef pochhammer_Suc_setprod rGamma_series_def [abs_def] exp_def
  7.1331 +                  of_real_def [symmetric] suminf_def sums_def [abs_def])
  7.1332 +qed
  7.1333 +
  7.1334 +end
  7.1335 +end
  7.1336 +
  7.1337 +
  7.1338 +lemma Gamma_complex_altdef: 
  7.1339 +  "Gamma z = (if z \<in> \<int>\<^sub>\<le>\<^sub>0 then 0 else exp (ln_Gamma (z :: complex)))"
  7.1340 +  unfolding Gamma_def rGamma_complex_altdef by (simp add: exp_minus)
  7.1341 +
  7.1342 +lemma cnj_rGamma: "cnj (rGamma z) = rGamma (cnj z)"
  7.1343 +proof -
  7.1344 +  have "rGamma_series (cnj z) = (\<lambda>n. cnj (rGamma_series z n))"
  7.1345 +    by (intro ext) (simp_all add: rGamma_series_def exp_cnj)
  7.1346 +  also have "... \<longlonglongrightarrow> cnj (rGamma z)" by (intro tendsto_cnj tendsto_intros)
  7.1347 +  finally show ?thesis unfolding rGamma_complex_def by (intro sym[OF limI])
  7.1348 +qed
  7.1349 +
  7.1350 +lemma cnj_Gamma: "cnj (Gamma z) = Gamma (cnj z)"
  7.1351 +  unfolding Gamma_def by (simp add: cnj_rGamma)
  7.1352 +  
  7.1353 +lemma Gamma_complex_real: 
  7.1354 +  "z \<in> \<real> \<Longrightarrow> Gamma z \<in> (\<real> :: complex set)" and rGamma_complex_real: "z \<in> \<real> \<Longrightarrow> rGamma z \<in> \<real>"
  7.1355 +  by (simp_all add: Reals_cnj_iff cnj_Gamma cnj_rGamma)
  7.1356 +
  7.1357 +lemma complex_differentiable_rGamma: "rGamma complex_differentiable (at z within A)"
  7.1358 +  using has_field_derivative_rGamma[of z] unfolding complex_differentiable_def by blast
  7.1359 +
  7.1360 +lemma holomorphic_on_rGamma: "rGamma holomorphic_on A"
  7.1361 +  unfolding holomorphic_on_def by (auto intro!: complex_differentiable_rGamma)
  7.1362 +
  7.1363 +lemma analytic_on_rGamma: "rGamma analytic_on A"
  7.1364 +  unfolding analytic_on_def by (auto intro!: exI[of _ 1] holomorphic_on_rGamma)
  7.1365 +
  7.1366 +
  7.1367 +lemma complex_differentiable_Gamma: "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Gamma complex_differentiable (at z within A)"
  7.1368 +  using has_field_derivative_Gamma[of z] unfolding complex_differentiable_def by auto
  7.1369 +
  7.1370 +lemma holomorphic_on_Gamma: "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> Gamma holomorphic_on A"
  7.1371 +  unfolding holomorphic_on_def by (auto intro!: complex_differentiable_Gamma)
  7.1372 +
  7.1373 +lemma analytic_on_Gamma: "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> Gamma analytic_on A"
  7.1374 +  by (rule analytic_on_subset[of _ "UNIV - \<int>\<^sub>\<le>\<^sub>0"], subst analytic_on_open)
  7.1375 +     (auto intro!: holomorphic_on_Gamma)
  7.1376 +
  7.1377 +lemma has_field_derivative_rGamma_complex' [derivative_intros]:
  7.1378 +  "(rGamma has_field_derivative (if z \<in> \<int>\<^sub>\<le>\<^sub>0 then (-1)^(nat \<lfloor>-Re z\<rfloor>) * fact (nat \<lfloor>-Re z\<rfloor>) else 
  7.1379 +        -rGamma z * Digamma z)) (at z within A)"
  7.1380 +  using has_field_derivative_rGamma[of z] by (auto elim!: nonpos_Ints_cases')
  7.1381 +
  7.1382 +declare has_field_derivative_rGamma_complex'[THEN DERIV_chain2, derivative_intros]
  7.1383 +
  7.1384 +
  7.1385 +lemma complex_differentiable_Polygamma:
  7.1386 +  "z \<notin> \<int>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Polygamma n complex_differentiable (at z within A)"
  7.1387 +  using has_field_derivative_Polygamma[of z n] unfolding complex_differentiable_def by auto
  7.1388 +
  7.1389 +lemma holomorphic_on_Polygamma: "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> Polygamma n holomorphic_on A"
  7.1390 +  unfolding holomorphic_on_def by (auto intro!: complex_differentiable_Polygamma)
  7.1391 +
  7.1392 +lemma analytic_on_Polygamma: "A \<inter> \<int>\<^sub>\<le>\<^sub>0 = {} \<Longrightarrow> Polygamma n analytic_on A"
  7.1393 +  by (rule analytic_on_subset[of _ "UNIV - \<int>\<^sub>\<le>\<^sub>0"], subst analytic_on_open)
  7.1394 +     (auto intro!: holomorphic_on_Polygamma)
  7.1395 +
  7.1396 +
  7.1397 +
  7.1398 +text \<open>The real Gamma function\<close>
  7.1399 +
  7.1400 +lemma rGamma_series_real:
  7.1401 +  "eventually (\<lambda>n. rGamma_series x n = Re (rGamma_series (of_real x) n)) sequentially"
  7.1402 +  using eventually_gt_at_top[of "0 :: nat"]
  7.1403 +proof eventually_elim
  7.1404 +  fix n :: nat assume n: "n > 0"
  7.1405 +  have "Re (rGamma_series (of_real x) n) =
  7.1406 +          Re (of_real (pochhammer x (Suc n)) / (fact n * exp (of_real (x * ln (real_of_nat n)))))"
  7.1407 +    using n by (simp add: rGamma_series_def powr_def Ln_of_nat pochhammer_of_real)
  7.1408 +  also from n have "\<dots> = Re (of_real ((pochhammer x (Suc n)) /
  7.1409 +                              (fact n * (exp (x * ln (real_of_nat n))))))"
  7.1410 +    by (subst exp_of_real) simp
  7.1411 +  also from n have "\<dots> = rGamma_series x n"
  7.1412 +    by (subst Re_complex_of_real) (simp add: rGamma_series_def powr_def)
  7.1413 +  finally show "rGamma_series x n = Re (rGamma_series (of_real x) n)" ..
  7.1414 +qed
  7.1415 +
  7.1416 +instantiation real :: Gamma
  7.1417 +begin
  7.1418 +
  7.1419 +definition "rGamma_real x = Re (rGamma (of_real x :: complex))"
  7.1420 +
  7.1421 +instance proof
  7.1422 +  fix x :: real
  7.1423 +  have "rGamma x = Re (rGamma (of_real x))" by (simp add: rGamma_real_def)
  7.1424 +  also have "of_real \<dots> = rGamma (of_real x :: complex)"
  7.1425 +    by (intro of_real_Re rGamma_complex_real) simp_all
  7.1426 +  also have "\<dots> = 0 \<longleftrightarrow> x \<in> \<int>\<^sub>\<le>\<^sub>0" by (simp add: rGamma_eq_zero_iff of_real_in_nonpos_Ints_iff)
  7.1427 +  also have "\<dots> \<longleftrightarrow> (\<exists>n. x = - of_nat n)" by (auto elim!: nonpos_Ints_cases')
  7.1428 +  finally show "(rGamma x) = 0 \<longleftrightarrow> (\<exists>n. x = - real_of_nat n)" by simp
  7.1429 +next
  7.1430 +  fix x :: real assume "\<And>n. x \<noteq> - of_nat n"
  7.1431 +  hence "complex_of_real x \<notin> \<int>\<^sub>\<le>\<^sub>0" 
  7.1432 +    by (subst of_real_in_nonpos_Ints_iff) (auto elim!: nonpos_Ints_cases')
  7.1433 +  moreover from this have "x \<noteq> 0" by auto
  7.1434 +  ultimately have "(rGamma has_field_derivative - rGamma x * Digamma x) (at x)"
  7.1435 +    by (fastforce intro!: derivative_eq_intros has_vector_derivative_real_complex 
  7.1436 +                  simp: Polygamma_of_real rGamma_real_def [abs_def])
  7.1437 +  thus "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (x + of_nat k))
  7.1438 +                       \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma x + 
  7.1439 +              rGamma x * d * (y - x)) /\<^sub>R  norm (y - x)) \<midarrow>x\<rightarrow> 0"
  7.1440 +      by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  7.1441 +                    netlimit_at of_real_def[symmetric] suminf_def)
  7.1442 +next
  7.1443 +  fix n :: nat
  7.1444 +  have "(rGamma has_field_derivative (-1)^n * fact n) (at (- of_nat n :: real))"
  7.1445 +    by (fastforce intro!: derivative_eq_intros has_vector_derivative_real_complex 
  7.1446 +                  simp: Polygamma_of_real rGamma_real_def [abs_def])
  7.1447 +  thus "let x = - of_nat n in (\<lambda>y. (rGamma y - rGamma x - (- 1) ^ n * setprod of_nat {1..n} *
  7.1448 +                  (y - x)) /\<^sub>R norm (y - x)) \<midarrow>x::real\<rightarrow> 0"
  7.1449 +    by (simp add: has_field_derivative_def has_derivative_def fact_altdef netlimit_at Let_def)
  7.1450 +next
  7.1451 +  fix x :: real
  7.1452 +  have "rGamma_series x \<longlonglongrightarrow> rGamma x"
  7.1453 +  proof (rule Lim_transform_eventually)
  7.1454 +    show "(\<lambda>n. Re (rGamma_series (of_real x) n)) \<longlonglongrightarrow> rGamma x" unfolding rGamma_real_def
  7.1455 +      by (intro tendsto_intros)
  7.1456 +  qed (insert rGamma_series_real, simp add: eq_commute)
  7.1457 +  thus "let fact' = \<lambda>n. setprod of_nat {1..n}; 
  7.1458 +            exp = \<lambda>x. THE e. (\<lambda>n. \<Sum>k<n. x ^ k /\<^sub>R fact k) \<longlonglongrightarrow> e;
  7.1459 +            pochhammer' = \<lambda>a n. \<Prod>n = 0..n. a + of_nat n
  7.1460 +        in  (\<lambda>n. pochhammer' x n / (fact' n * exp (x * ln (real_of_nat n) *\<^sub>R 1))) \<longlonglongrightarrow> rGamma x"
  7.1461 +    by (simp add: fact_altdef pochhammer_Suc_setprod rGamma_series_def [abs_def] exp_def
  7.1462 +                  of_real_def [symmetric] suminf_def sums_def [abs_def])
  7.1463 +qed
  7.1464 +
  7.1465 +end
  7.1466 +
  7.1467 +
  7.1468 +lemma rGamma_complex_of_real: "rGamma (complex_of_real x) = complex_of_real (rGamma x)"
  7.1469 +  unfolding rGamma_real_def using rGamma_complex_real by simp
  7.1470 +
  7.1471 +lemma Gamma_complex_of_real: "Gamma (complex_of_real x) = complex_of_real (Gamma x)"
  7.1472 +  unfolding Gamma_def by (simp add: rGamma_complex_of_real)
  7.1473 +
  7.1474 +lemma rGamma_real_altdef: "rGamma x = lim (rGamma_series (x :: real))"
  7.1475 +  by (rule sym, rule limI, rule tendsto_intros)
  7.1476 +
  7.1477 +lemma Gamma_real_altdef1: "Gamma x = lim (Gamma_series (x :: real))"
  7.1478 +  by (rule sym, rule limI, rule tendsto_intros)
  7.1479 +
  7.1480 +lemma Gamma_real_altdef2: "Gamma x = Re (Gamma (of_real x))"
  7.1481 +  using rGamma_complex_real[OF Reals_of_real[of x]]
  7.1482 +  by (elim Reals_cases) 
  7.1483 +     (simp only: Gamma_def rGamma_real_def of_real_inverse[symmetric] Re_complex_of_real)
  7.1484 +
  7.1485 +lemma ln_Gamma_series_complex_of_real: 
  7.1486 +  "x > 0 \<Longrightarrow> n > 0 \<Longrightarrow> ln_Gamma_series (complex_of_real x) n = of_real (ln_Gamma_series x n)"
  7.1487 +proof -
  7.1488 +  assume xn: "x > 0" "n > 0"
  7.1489 +  have "Ln (complex_of_real x / of_nat k + 1) = of_real (ln (x / of_nat k + 1))" if "k \<ge> 1" for k
  7.1490 +    using that xn by (subst Ln_of_real [symmetric]) (auto intro!: add_nonneg_pos simp: field_simps)
  7.1491 +  with xn show ?thesis by (simp add: ln_Gamma_series_def Ln_of_nat Ln_of_real)
  7.1492 +qed
  7.1493 +
  7.1494 +lemma ln_Gamma_real_converges: 
  7.1495 +  assumes "(x::real) > 0" 
  7.1496 +  shows   "convergent (ln_Gamma_series x)"
  7.1497 +proof -
  7.1498 +  have "(\<lambda>n. ln_Gamma_series (complex_of_real x) n) \<longlonglongrightarrow> ln_Gamma (of_real x)" using assms
  7.1499 +    by (intro ln_Gamma_complex_LIMSEQ) (auto simp: of_real_in_nonpos_Ints_iff)
  7.1500 +  moreover from eventually_gt_at_top[of "0::nat"]
  7.1501 +    have "eventually (\<lambda>n. complex_of_real (ln_Gamma_series x n) = 
  7.1502 +            ln_Gamma_series (complex_of_real x) n) sequentially" 
  7.1503 +    by eventually_elim (simp add: ln_Gamma_series_complex_of_real assms)
  7.1504 +  ultimately have "(\<lambda>n. complex_of_real (ln_Gamma_series x n)) \<longlonglongrightarrow> ln_Gamma (of_real x)"
  7.1505 +    by (subst tendsto_cong) assumption+
  7.1506 +  from tendsto_Re[OF this] show ?thesis by (auto simp: convergent_def)
  7.1507 +qed
  7.1508 +
  7.1509 +lemma ln_Gamma_real_LIMSEQ: "(x::real) > 0 \<Longrightarrow> ln_Gamma_series x \<longlonglongrightarrow> ln_Gamma x"
  7.1510 +  using ln_Gamma_real_converges[of x] unfolding ln_Gamma_def by (simp add: convergent_LIMSEQ_iff)
  7.1511 +
  7.1512 +lemma ln_Gamma_complex_of_real: "x > 0 \<Longrightarrow> ln_Gamma (complex_of_real x) = of_real (ln_Gamma x)"
  7.1513 +proof (unfold ln_Gamma_def, rule limI, rule Lim_transform_eventually)
  7.1514 +  assume x: "x > 0"
  7.1515 +  show "eventually (\<lambda>n. of_real (ln_Gamma_series x n) = 
  7.1516 +            ln_Gamma_series (complex_of_real x) n) sequentially" 
  7.1517 +    using eventually_gt_at_top[of "0::nat"] 
  7.1518 +    by eventually_elim (simp add: ln_Gamma_series_complex_of_real x)
  7.1519 +qed (intro tendsto_of_real, insert ln_Gamma_real_LIMSEQ[of x], simp add: ln_Gamma_def)
  7.1520 +     
  7.1521 +lemma Gamma_real_pos_exp: "x > (0 :: real) \<Longrightarrow> Gamma x = exp (ln_Gamma x)"
  7.1522 +  by (auto simp: Gamma_real_altdef2 Gamma_complex_altdef of_real_in_nonpos_Ints_iff 
  7.1523 +                 ln_Gamma_complex_of_real exp_of_real)
  7.1524 +
  7.1525 +lemma ln_Gamma_real_pos: "x > 0 \<Longrightarrow> ln_Gamma x = ln (Gamma x :: real)"
  7.1526 +  unfolding Gamma_real_pos_exp by simp
  7.1527 +
  7.1528 +lemma Gamma_real_pos: "x > (0::real) \<Longrightarrow> Gamma x > 0"
  7.1529 +  by (simp add: Gamma_real_pos_exp)
  7.1530 +  
  7.1531 +lemma has_field_derivative_ln_Gamma_real [derivative_intros]:
  7.1532 +  assumes x: "x > (0::real)"
  7.1533 +  shows "(ln_Gamma has_field_derivative Digamma x) (at x)"
  7.1534 +proof (subst DERIV_cong_ev[OF refl _ refl])
  7.1535 +  from assms show "((Re \<circ> ln_Gamma \<circ> complex_of_real) has_field_derivative Digamma x) (at x)"
  7.1536 +    by (auto intro!: derivative_eq_intros has_vector_derivative_real_complex
  7.1537 +             simp: Polygamma_of_real o_def)
  7.1538 +  from eventually_nhds_in_nhd[of x "{0<..}"] assms
  7.1539 +    show "eventually (\<lambda>y. ln_Gamma y = (Re \<circ> ln_Gamma \<circ> of_real) y) (nhds x)"
  7.1540 +    by (auto elim!: eventually_mono simp: ln_Gamma_complex_of_real interior_open)
  7.1541 +qed
  7.1542 +
  7.1543 +declare has_field_derivative_ln_Gamma_real[THEN DERIV_chain2, derivative_intros]
  7.1544 +
  7.1545 +
  7.1546 +lemma has_field_derivative_rGamma_real' [derivative_intros]:
  7.1547 +  "(rGamma has_field_derivative (if x \<in> \<int>\<^sub>\<le>\<^sub>0 then (-1)^(nat \<lfloor>-x\<rfloor>) * fact (nat \<lfloor>-x\<rfloor>) else 
  7.1548 +        -rGamma x * Digamma x)) (at x within A)"
  7.1549 +  using has_field_derivative_rGamma[of x] by (force elim!: nonpos_Ints_cases')
  7.1550 +
  7.1551 +declare has_field_derivative_rGamma_real'[THEN DERIV_chain2, derivative_intros]
  7.1552 +
  7.1553 +lemma Polygamma_real_odd_pos:
  7.1554 +  assumes "(x::real) \<notin> \<int>\<^sub>\<le>\<^sub>0" "odd n"
  7.1555 +  shows   "Polygamma n x > 0"
  7.1556 +proof -
  7.1557 +  from assms have "x \<noteq> 0" by auto
  7.1558 +  with assms show ?thesis
  7.1559 +    unfolding Polygamma_def using Polygamma_converges'[of x "Suc n"]
  7.1560 +    by (auto simp: zero_less_power_eq simp del: power_Suc 
  7.1561 +             dest: plus_of_nat_eq_0_imp intro!: mult_pos_pos suminf_pos)
  7.1562 +qed
  7.1563 +
  7.1564 +lemma Polygamma_real_even_neg:
  7.1565 +  assumes "(x::real) > 0" "n > 0" "even n"
  7.1566 +  shows   "Polygamma n x < 0"
  7.1567 +  using assms unfolding Polygamma_def using Polygamma_converges'[of x "Suc n"]
  7.1568 +  by (auto intro!: mult_pos_pos suminf_pos)
  7.1569 +  
  7.1570 +lemma Polygamma_real_strict_mono:
  7.1571 +  assumes "x > 0" "x < (y::real)" "even n"
  7.1572 +  shows   "Polygamma n x < Polygamma n y"
  7.1573 +proof -
  7.1574 +  have "\<exists>\<xi>. x < \<xi> \<and> \<xi> < y \<and> Polygamma n y - Polygamma n x = (y - x) * Polygamma (Suc n) \<xi>"
  7.1575 +    using assms by (intro MVT2 derivative_intros impI allI) (auto elim!: nonpos_Ints_cases)
  7.1576 +  then guess \<xi> by (elim exE conjE) note \<xi> = this
  7.1577 +  note \<xi>(3)
  7.1578 +  also from \<xi>(1,2) assms have "(y - x) * Polygamma (Suc n) \<xi> > 0" 
  7.1579 +    by (intro mult_pos_pos Polygamma_real_odd_pos) (auto elim!: nonpos_Ints_cases)
  7.1580 +  finally show ?thesis by simp
  7.1581 +qed
  7.1582 +
  7.1583 +lemma Polygamma_real_strict_antimono:
  7.1584 +  assumes "x > 0" "x < (y::real)" "odd n"
  7.1585 +  shows   "Polygamma n x > Polygamma n y"
  7.1586 +proof -
  7.1587 +  have "\<exists>\<xi>. x < \<xi> \<and> \<xi> < y \<and> Polygamma n y - Polygamma n x = (y - x) * Polygamma (Suc n) \<xi>"
  7.1588 +    using assms by (intro MVT2 derivative_intros impI allI) (auto elim!: nonpos_Ints_cases)
  7.1589 +  then guess \<xi> by (elim exE conjE) note \<xi> = this
  7.1590 +  note \<xi>(3)
  7.1591 +  also from \<xi>(1,2) assms have "(y - x) * Polygamma (Suc n) \<xi> < 0" 
  7.1592 +    by (intro mult_pos_neg Polygamma_real_even_neg) simp_all
  7.1593 +  finally show ?thesis by simp
  7.1594 +qed
  7.1595 +
  7.1596 +lemma Polygamma_real_mono:
  7.1597 +  assumes "x > 0" "x \<le> (y::real)" "even n"
  7.1598 +  shows   "Polygamma n x \<le> Polygamma n y"
  7.1599 +  using Polygamma_real_strict_mono[OF assms(1) _ assms(3), of y] assms(2) 
  7.1600 +  by (cases "x = y") simp_all
  7.1601 +
  7.1602 +lemma Digamma_real_ge_three_halves_pos:
  7.1603 +  assumes "x \<ge> 3/2"
  7.1604 +  shows   "Digamma (x :: real) > 0"
  7.1605 +proof -
  7.1606 +  have "0 < Digamma (3/2 :: real)" by (fact Digamma_real_three_halves_pos)
  7.1607 +  also from assms have "\<dots> \<le> Digamma x" by (intro Polygamma_real_mono) simp_all
  7.1608 +  finally show ?thesis .
  7.1609 +qed
  7.1610 +
  7.1611 +lemma ln_Gamma_real_strict_mono:
  7.1612 +  assumes "x \<ge> 3/2" "x < y"
  7.1613 +  shows   "ln_Gamma (x :: real) < ln_Gamma y"
  7.1614 +proof -
  7.1615 +  have "\<exists>\<xi>. x < \<xi> \<and> \<xi> < y \<and> ln_Gamma y - ln_Gamma x = (y - x) * Digamma \<xi>"
  7.1616 +    using assms by (intro MVT2 derivative_intros impI allI) (auto elim!: nonpos_Ints_cases)
  7.1617 +  then guess \<xi> by (elim exE conjE) note \<xi> = this
  7.1618 +  note \<xi>(3)
  7.1619 +  also from \<xi>(1,2) assms have "(y - x) * Digamma \<xi> > 0" 
  7.1620 +    by (intro mult_pos_pos Digamma_real_ge_three_halves_pos) simp_all
  7.1621 +  finally show ?thesis by simp
  7.1622 +qed  
  7.1623 +  
  7.1624 +lemma Gamma_real_strict_mono:
  7.1625 +  assumes "x \<ge> 3/2" "x < y"
  7.1626 +  shows   "Gamma (x :: real) < Gamma y"
  7.1627 +proof -
  7.1628 +  from Gamma_real_pos_exp[of x] assms have "Gamma x = exp (ln_Gamma x)" by simp
  7.1629 +  also have "\<dots> < exp (ln_Gamma y)" by (intro exp_less_mono ln_Gamma_real_strict_mono assms)
  7.1630 +  also from Gamma_real_pos_exp[of y] assms have "\<dots> = Gamma y" by simp
  7.1631 +  finally show ?thesis .
  7.1632 +qed
  7.1633 +
  7.1634 +lemma log_convex_Gamma_real: "convex_on {0<..} (ln \<circ> Gamma :: real \<Rightarrow> real)"
  7.1635 +  by (rule convex_on_realI[of _ _ Digamma])
  7.1636 +     (auto intro!: derivative_eq_intros Polygamma_real_mono Gamma_real_pos 
  7.1637 +           simp: o_def Gamma_eq_zero_iff elim!: nonpos_Ints_cases')
  7.1638 +
  7.1639 +
  7.1640 +subsection \<open>Beta function\<close>
  7.1641 +
  7.1642 +definition Beta where "Beta a b = Gamma a * Gamma b / Gamma (a + b)"
  7.1643 +
  7.1644 +lemma Beta_altdef: "Beta a b = Gamma a * Gamma b * rGamma (a + b)"
  7.1645 +  by (simp add: inverse_eq_divide Beta_def Gamma_def)
  7.1646 +
  7.1647 +lemma Beta_commute: "Beta a b = Beta b a"
  7.1648 +  unfolding Beta_def by (simp add: ac_simps)
  7.1649 +
  7.1650 +lemma has_field_derivative_Beta1 [derivative_intros]:
  7.1651 +  assumes "x \<notin> \<int>\<^sub>\<le>\<^sub>0" "x + y \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1652 +  shows   "((\<lambda>x. Beta x y) has_field_derivative (Beta x y * (Digamma x - Digamma (x + y)))) 
  7.1653 +               (at x within A)" unfolding Beta_altdef
  7.1654 +  by (rule DERIV_cong, (rule derivative_intros assms)+) (simp add: algebra_simps)
  7.1655 +
  7.1656 +lemma has_field_derivative_Beta2 [derivative_intros]:
  7.1657 +  assumes "y \<notin> \<int>\<^sub>\<le>\<^sub>0" "x + y \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1658 +  shows   "((\<lambda>y. Beta x y) has_field_derivative (Beta x y * (Digamma y - Digamma (x + y)))) 
  7.1659 +               (at y within A)"
  7.1660 +  using has_field_derivative_Beta1[of y x A] assms by (simp add: Beta_commute add_ac)
  7.1661 +
  7.1662 +lemma Beta_plus1_plus1: 
  7.1663 +  assumes "x \<notin> \<int>\<^sub>\<le>\<^sub>0" "y \<notin> \<int>\<^sub>\<le>\<^sub>0" 
  7.1664 +  shows   "Beta (x + 1) y + Beta x (y + 1) = Beta x y"
  7.1665 +proof -
  7.1666 +  have "Beta (x + 1) y + Beta x (y + 1) = 
  7.1667 +            (Gamma (x + 1) * Gamma y + Gamma x * Gamma (y + 1)) * rGamma ((x + y) + 1)"
  7.1668 +    by (simp add: Beta_altdef add_divide_distrib algebra_simps)
  7.1669 +  also have "\<dots> = (Gamma x * Gamma y) * ((x + y) * rGamma ((x + y) + 1))"
  7.1670 +    by (subst assms[THEN Gamma_plus1])+ (simp add: algebra_simps)
  7.1671 +  also from assms have "\<dots> = Beta x y" unfolding Beta_altdef by (subst rGamma_plus1) simp
  7.1672 +  finally show ?thesis .
  7.1673 +qed
  7.1674 +
  7.1675 +lemma Beta_plus1_left: 
  7.1676 +  assumes "x \<notin> \<int>\<^sub>\<le>\<^sub>0" "y \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1677 +  shows   "(x + y) * Beta (x + 1) y = x * Beta x y"
  7.1678 +proof -
  7.1679 +  have "(x + y) * Beta (x + 1) y = Gamma (x + 1) * Gamma y * ((x + y) * rGamma ((x + y) + 1))"
  7.1680 +    unfolding Beta_altdef by (simp only: ac_simps)
  7.1681 +  also have "\<dots> = x * Beta x y" unfolding Beta_altdef
  7.1682 +     by (subst assms[THEN Gamma_plus1] rGamma_plus1)+ (simp only: ac_simps)
  7.1683 +  finally show ?thesis .
  7.1684 +qed
  7.1685 +
  7.1686 +lemma Beta_plus1_right: 
  7.1687 +  assumes "x \<notin> \<int>\<^sub>\<le>\<^sub>0" "y \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1688 +  shows   "(x + y) * Beta x (y + 1) = y * Beta x y"
  7.1689 +  using Beta_plus1_left[of y x] assms by (simp add: Beta_commute add.commute)
  7.1690 +
  7.1691 +lemma Gamma_Gamma_Beta:
  7.1692 +  assumes "x \<notin> \<int>\<^sub>\<le>\<^sub>0" "y \<notin> \<int>\<^sub>\<le>\<^sub>0" "x + y \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1693 +  shows   "Gamma x * Gamma y = Beta x y * Gamma (x + y)"
  7.1694 +  unfolding Beta_altdef using assms Gamma_eq_zero_iff[of "x+y"]
  7.1695 +  by (simp add: rGamma_inverse_Gamma)
  7.1696 +
  7.1697 +
  7.1698 +
  7.1699 +subsection \<open>Legendre duplication theorem\<close>
  7.1700 +
  7.1701 +context
  7.1702 +begin
  7.1703 +
  7.1704 +private lemma Gamma_legendre_duplication_aux:
  7.1705 +  fixes z :: "'a :: Gamma"
  7.1706 +  assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z + 1/2 \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1707 +  shows "Gamma z * Gamma (z + 1/2) = exp ((1 - 2*z) * of_real (ln 2)) * Gamma (1/2) * Gamma (2*z)"
  7.1708 +proof -
  7.1709 +  let ?powr = "\<lambda>b a. exp (a * of_real (ln (of_nat b)))"
  7.1710 +  let ?h = "\<lambda>n. (fact (n-1))\<^sup>2 / fact (2*n-1) * of_nat (2^(2*n)) * 
  7.1711 +                exp (1/2 * of_real (ln (real_of_nat n)))"
  7.1712 +  {
  7.1713 +    fix z :: 'a assume z: "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z + 1/2 \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1714 +    let ?g = "\<lambda>n. ?powr 2 (2*z) * Gamma_series' z n * Gamma_series' (z + 1/2) n / 
  7.1715 +                      Gamma_series' (2*z) (2*n)"
  7.1716 +    have "eventually (\<lambda>n. ?g n = ?h n) sequentially" using eventually_gt_at_top
  7.1717 +    proof eventually_elim
  7.1718 +      fix n :: nat assume n: "n > 0"
  7.1719 +      let ?f = "fact (n - 1) :: 'a" and ?f' = "fact (2*n - 1) :: 'a"
  7.1720 +      have A: "exp t * exp t = exp (2*t :: 'a)" for t by (subst exp_add [symmetric]) simp
  7.1721 +      have A: "Gamma_series' z n * Gamma_series' (z + 1/2) n = ?f^2 * ?powr n (2*z + 1/2) /
  7.1722 +                (pochhammer z n * pochhammer (z + 1/2) n)"
  7.1723 +        by (simp add: Gamma_series'_def exp_add ring_distribs power2_eq_square A mult_ac)
  7.1724 +      have B: "Gamma_series' (2*z) (2*n) = 
  7.1725 +                       ?f' * ?powr 2 (2*z) * ?powr n (2*z) / 
  7.1726 +                       (of_nat (2^(2*n)) * pochhammer z n * pochhammer (z+1/2) n)" using n
  7.1727 +        by (simp add: Gamma_series'_def ln_mult exp_add ring_distribs pochhammer_double)
  7.1728 +      from z have "pochhammer z n \<noteq> 0" by (auto dest: pochhammer_eq_0_imp_nonpos_Int)
  7.1729 +      moreover from z have "pochhammer (z + 1/2) n \<noteq> 0" by (auto dest: pochhammer_eq_0_imp_nonpos_Int)
  7.1730 +      ultimately have "?powr 2 (2*z) * (Gamma_series' z n * Gamma_series' (z + 1/2) n) / Gamma_series' (2*z) (2*n) = 
  7.1731 +         ?f^2 / ?f' * of_nat (2^(2*n)) * (?powr n ((4*z + 1)/2) * ?powr n (-2*z))"
  7.1732 +        using n unfolding A B by (simp add: divide_simps exp_minus)
  7.1733 +      also have "?powr n ((4*z + 1)/2) * ?powr n (-2*z) = ?powr n (1/2)"
  7.1734 +        by (simp add: algebra_simps exp_add[symmetric] add_divide_distrib)
  7.1735 +      finally show "?g n = ?h n" by (simp only: mult_ac)
  7.1736 +    qed
  7.1737 +    
  7.1738 +    moreover from z double_in_nonpos_Ints_imp[of z] have "2 * z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto     
  7.1739 +    hence "?g \<longlonglongrightarrow> ?powr 2 (2*z) * Gamma z * Gamma (z+1/2) / Gamma (2*z)"
  7.1740 +      using lim_subseq[of "op * 2", OF _ Gamma_series'_LIMSEQ, of "2*z"]
  7.1741 +      by (intro tendsto_intros Gamma_series'_LIMSEQ)
  7.1742 +         (simp_all add: o_def subseq_def Gamma_eq_zero_iff)
  7.1743 +    ultimately have "?h \<longlonglongrightarrow> ?powr 2 (2*z) * Gamma z * Gamma (z+1/2) / Gamma (2*z)"
  7.1744 +      by (rule Lim_transform_eventually)
  7.1745 +  } note lim = this
  7.1746 +  
  7.1747 +  from assms double_in_nonpos_Ints_imp[of z] have z': "2 * z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto  
  7.1748 +  from fraction_not_in_ints[of 2 1] have "(1/2 :: 'a) \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.1749 +    by (intro not_in_Ints_imp_not_in_nonpos_Ints) simp_all
  7.1750 +  with lim[of "1/2 :: 'a"] have "?h \<longlonglongrightarrow> 2 * Gamma (1 / 2 :: 'a)" by (simp add: exp_of_real)
  7.1751 +  from LIMSEQ_unique[OF this lim[OF assms]] z' show ?thesis
  7.1752 +    by (simp add: divide_simps Gamma_eq_zero_iff ring_distribs exp_diff exp_of_real ac_simps)
  7.1753 +qed
  7.1754 +
  7.1755 +(* TODO: perhaps this is unnecessary once we have the fact that a holomorphic function is 
  7.1756 +   infinitely differentiable *)
  7.1757 +private lemma Gamma_reflection_aux:
  7.1758 +  defines "h \<equiv> \<lambda>z::complex. if z \<in> \<int> then 0 else 
  7.1759 +                 (of_real pi * cot (of_real pi*z) + Digamma z - Digamma (1 - z))"
  7.1760 +  defines "a \<equiv> complex_of_real pi"
  7.1761 +  obtains h' where "continuous_on UNIV h'" "\<And>z. (h has_field_derivative (h' z)) (at z)"
  7.1762 +proof -
  7.1763 +  def f \<equiv> "\<lambda>n. a * of_real (cos_coeff (n+1) - sin_coeff (n+2))"
  7.1764 +  def F \<equiv> "\<lambda>z. if z = 0 then 0 else (cos (a*z) - sin (a*z)/(a*z)) / z"
  7.1765 +  def g \<equiv> "\<lambda>n. complex_of_real (sin_coeff (n+1))"
  7.1766 +  def G \<equiv> "\<lambda>z. if z = 0 then 1 else sin (a*z)/(a*z)"
  7.1767 +  have a_nz: "a \<noteq> 0" unfolding a_def by simp
  7.1768 +
  7.1769 +  have "(\<lambda>n. f n * (a*z)^n) sums (F z) \<and> (\<lambda>n. g n * (a*z)^n) sums (G z)" 
  7.1770 +    if "abs (Re z) < 1" for z
  7.1771 +  proof (cases "z = 0"; rule conjI)
  7.1772 +    assume "z \<noteq> 0"
  7.1773 +    note z = this that
  7.1774 +
  7.1775 +    from z have sin_nz: "sin (a*z) \<noteq> 0" unfolding a_def by (auto simp: sin_eq_0)
  7.1776 +    have "(\<lambda>n. of_real (sin_coeff n) * (a*z)^n) sums (sin (a*z))" using sin_converges[of "a*z"]
  7.1777 +      by (simp add: scaleR_conv_of_real)
  7.1778 +    from sums_split_initial_segment[OF this, of 1] 
  7.1779 +      have "(\<lambda>n. (a*z) * of_real (sin_coeff (n+1)) * (a*z)^n) sums (sin (a*z))" by (simp add: mult_ac)
  7.1780 +    from sums_mult[OF this, of "inverse (a*z)"] z a_nz
  7.1781 +      have A: "(\<lambda>n. g n * (a*z)^n) sums (sin (a*z)/(a*z))"
  7.1782 +      by (simp add: field_simps g_def)
  7.1783 +    with z show "(\<lambda>n. g n * (a*z)^n) sums (G z)" by (simp add: G_def)
  7.1784 +    from A z a_nz sin_nz have g_nz: "(\<Sum>n. g n * (a*z)^n) \<noteq> 0" by (simp add: sums_iff g_def)
  7.1785 +  
  7.1786 +    have [simp]: "sin_coeff (Suc 0) = 1" by (simp add: sin_coeff_def)
  7.1787 +    from sums_split_initial_segment[OF sums_diff[OF cos_converges[of "a*z"] A], of 1]
  7.1788 +    have "(\<lambda>n. z * f n * (a*z)^n) sums (cos (a*z) - sin (a*z) / (a*z))"
  7.1789 +      by (simp add: mult_ac scaleR_conv_of_real ring_distribs f_def g_def)
  7.1790 +    from sums_mult[OF this, of "inverse z"] z assms
  7.1791 +      show "(\<lambda>n. f n * (a*z)^n) sums (F z)" by (simp add: divide_simps mult_ac f_def F_def)
  7.1792 +  next
  7.1793 +    assume z: "z = 0"
  7.1794 +    have "(\<lambda>n. f n * (a * z) ^ n) sums f 0" using powser_sums_zero[of f] z by simp
  7.1795 +    with z show "(\<lambda>n. f n * (a * z) ^ n) sums (F z)" 
  7.1796 +      by (simp add: f_def F_def sin_coeff_def cos_coeff_def)
  7.1797 +    have "(\<lambda>n. g n * (a * z) ^ n) sums g 0" using powser_sums_zero[of g] z by simp
  7.1798 +    with z show "(\<lambda>n. g n * (a * z) ^ n) sums (G z)" 
  7.1799 +      by (simp add: g_def G_def sin_coeff_def cos_coeff_def)
  7.1800 +  qed
  7.1801 +  note sums = conjunct1[OF this] conjunct2[OF this]
  7.1802 +  
  7.1803 +  def h2 \<equiv> "\<lambda>z. (\<Sum>n. f n * (a*z)^n) / (\<Sum>n. g n * (a*z)^n) + 
  7.1804 +            Digamma (1 + z) - Digamma (1 - z)"
  7.1805 +  def POWSER \<equiv> "\<lambda>f z. (\<Sum>n. f n * (z^n :: complex))"
  7.1806 +  def POWSER' \<equiv> "\<lambda>f z. (\<Sum>n. diffs f n * (z^n :: complex))"
  7.1807 +
  7.1808 +  def h2' \<equiv> "\<lambda>z. a * (POWSER g (a*z) * POWSER' f (a*z) - POWSER f (a*z) * POWSER' g (a*z)) / 
  7.1809 +                     (POWSER g (a*z))^2 + Polygamma 1 (1 + z) + Polygamma 1 (1 - z)" 
  7.1810 +  
  7.1811 +  have h_eq: "h t = h2 t" if "abs (Re t) < 1" for t
  7.1812 +  proof -
  7.1813 +    from that have t: "t \<in> \<int> \<longleftrightarrow> t = 0" by (auto elim!: Ints_cases simp: dist_0_norm)
  7.1814 +    hence "h t = a*cot (a*t) - 1/t + Digamma (1 + t) - Digamma (1 - t)" 
  7.1815 +      unfolding h_def using Digamma_plus1[of t] by (force simp: field_simps a_def)
  7.1816 +    also have "a*cot (a*t) - 1/t = (F t) / (G t)"
  7.1817 +      using t by (auto simp add: divide_simps sin_eq_0 cot_def a_def F_def G_def)
  7.1818 +    also have "\<dots> = (\<Sum>n. f n * (a*t)^n) / (\<Sum>n. g n * (a*t)^n)"
  7.1819 +      using sums[of t] that by (simp add: sums_iff dist_0_norm)
  7.1820 +    finally show "h t = h2 t" by (simp only: h2_def)
  7.1821 +  qed
  7.1822 +  
  7.1823 +  let ?A = "{z. abs (Re z) < 1}"  
  7.1824 +  have "open ({z. Re z < 1} \<inter> {z. Re z > -1})"
  7.1825 +    using open_halfspace_Re_gt open_halfspace_Re_lt by auto
  7.1826 +  also have "({z. Re z < 1} \<inter> {z. Re z > -1}) = {z. abs (Re z) < 1}" by auto
  7.1827 +  finally have open_A: "open ?A" .
  7.1828 +  hence [simp]: "interior ?A = ?A" by (simp add: interior_open)
  7.1829 +  
  7.1830 +  have summable_f: "summable (\<lambda>n. f n * z^n)" for z
  7.1831 +    by (rule powser_inside, rule sums_summable, rule sums[of "\<i> * of_real (norm z + 1) / a"])
  7.1832 +       (simp_all add: norm_mult a_def del: of_real_add)
  7.1833 +  have summable_g: "summable (\<lambda>n. g n * z^n)" for z
  7.1834 +    by (rule powser_inside, rule sums_summable, rule sums[of "\<i> * of_real (norm z + 1) / a"])
  7.1835 +       (simp_all add: norm_mult a_def del: of_real_add)
  7.1836 +  have summable_fg': "summable (\<lambda>n. diffs f n * z^n)" "summable (\<lambda>n. diffs g n * z^n)" for z
  7.1837 +    by (intro termdiff_converges_all summable_f summable_g)+
  7.1838 +  have "(POWSER f has_field_derivative (POWSER' f z)) (at z)"
  7.1839 +               "(POWSER g has_field_derivative (POWSER' g z)) (at z)" for z
  7.1840 +    unfolding POWSER_def POWSER'_def 
  7.1841 +    by (intro termdiffs_strong_converges_everywhere summable_f summable_g)+
  7.1842 +  note derivs = this[THEN DERIV_chain2[OF _ DERIV_cmult[OF DERIV_ident]], unfolded POWSER_def]
  7.1843 +  have "isCont (POWSER f) z" "isCont (POWSER g) z" "isCont (POWSER' f) z" "isCont (POWSER' g) z" 
  7.1844 +    for z unfolding POWSER_def POWSER'_def
  7.1845 +    by (intro isCont_powser_converges_everywhere summable_f summable_g summable_fg')+
  7.1846 +  note cont = this[THEN isCont_o2[rotated], unfolded POWSER_def POWSER'_def]
  7.1847 +
  7.1848 +  {
  7.1849 +    fix z :: complex assume z: "abs (Re z) < 1"
  7.1850 +    def d \<equiv> "\<i> * of_real (norm z + 1)"
  7.1851 +    have d: "abs (Re d) < 1" "norm z < norm d" by (simp_all add: d_def norm_mult del: of_real_add)
  7.1852 +    have "eventually (\<lambda>z. h z = h2 z) (nhds z)"
  7.1853 +      using eventually_nhds_in_nhd[of z ?A] using h_eq z 
  7.1854 +      by (auto elim!: eventually_mono simp: dist_0_norm)
  7.1855 +    
  7.1856 +    moreover from sums(2)[OF z] z have nz: "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
  7.1857 +      unfolding G_def by (auto simp: sums_iff sin_eq_0 a_def)
  7.1858 +    have A: "z \<in> \<int> \<longleftrightarrow> z = 0" using z by (auto elim!: Ints_cases)
  7.1859 +    have no_int: "1 + z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of "1+z" 1] A
  7.1860 +      by (auto elim!: nonpos_Ints_cases)
  7.1861 +    have no_int': "1 - z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of 1 "1-z"] A
  7.1862 +      by (auto elim!: nonpos_Ints_cases)
  7.1863 +    from no_int no_int' have no_int: "1 - z \<notin> \<int>\<^sub>\<le>\<^sub>0" "1 + z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto
  7.1864 +    have "(h2 has_field_derivative h2' z) (at z)" unfolding h2_def
  7.1865 +      by (rule DERIV_cong, (rule derivative_intros refl derivs[unfolded POWSER_def] nz no_int)+)
  7.1866 +         (auto simp: h2'_def POWSER_def field_simps power2_eq_square)
  7.1867 +    ultimately have deriv: "(h has_field_derivative h2' z) (at z)" 
  7.1868 +      by (subst DERIV_cong_ev[OF refl _ refl])
  7.1869 +    
  7.1870 +    from sums(2)[OF z] z have "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
  7.1871 +      unfolding G_def by (auto simp: sums_iff a_def sin_eq_0)
  7.1872 +    hence "isCont h2' z" using no_int unfolding h2'_def[abs_def] POWSER_def POWSER'_def
  7.1873 +      by (intro continuous_intros cont 
  7.1874 +            continuous_on_compose2[OF _ continuous_on_Polygamma[of "{z. Re z > 0}"]]) auto
  7.1875 +    note deriv and this
  7.1876 +  } note A = this
  7.1877 +
  7.1878 +  interpret h: periodic_fun_simple' h
  7.1879 +  proof
  7.1880 +    fix z :: complex
  7.1881 +    show "h (z + 1) = h z"
  7.1882 +    proof (cases "z \<in> \<int>")
  7.1883 +      assume z: "z \<notin> \<int>"
  7.1884 +      hence A: "z + 1 \<notin> \<int>" "z \<noteq> 0" using Ints_diff[of "z+1" 1] by auto
  7.1885 +      hence "Digamma (z + 1) - Digamma (-z) = Digamma z - Digamma (-z + 1)"
  7.1886 +        by (subst (1 2) Digamma_plus1) simp_all
  7.1887 +      with A z show "h (z + 1) = h z" 
  7.1888 +        by (simp add: h_def sin_plus_pi cos_plus_pi ring_distribs cot_def)
  7.1889 +    qed (simp add: h_def)
  7.1890 +  qed
  7.1891 +  
  7.1892 +  have h2'_eq: "h2' (z - 1) = h2' z" if z: "Re z > 0" "Re z < 1" for z
  7.1893 +  proof -
  7.1894 +    have "((\<lambda>z. h (z - 1)) has_field_derivative h2' (z - 1)) (at z)"
  7.1895 +      by (rule DERIV_cong, rule DERIV_chain'[OF _ A(1)])
  7.1896 +         (insert z, auto intro!: derivative_eq_intros)
  7.1897 +    hence "(h has_field_derivative h2' (z - 1)) (at z)" by (subst (asm) h.minus_1)
  7.1898 +    moreover from z have "(h has_field_derivative h2' z) (at z)" by (intro A) simp_all
  7.1899 +    ultimately show "h2' (z - 1) = h2' z" by (rule DERIV_unique)
  7.1900 +  qed
  7.1901 +
  7.1902 +  def h2'' \<equiv> "\<lambda>z. h2' (z - of_int \<lfloor>Re z\<rfloor>)"
  7.1903 +  have deriv: "(h has_field_derivative h2'' z) (at z)" for z
  7.1904 +  proof -
  7.1905 +    fix z :: complex
  7.1906 +    have B: "\<bar>Re z - real_of_int \<lfloor>Re z\<rfloor>\<bar> < 1" by linarith
  7.1907 +    have "((\<lambda>t. h (t - of_int \<lfloor>Re z\<rfloor>)) has_field_derivative h2'' z) (at z)"
  7.1908 +      unfolding h2''_def by (rule DERIV_cong, rule DERIV_chain'[OF _ A(1)])
  7.1909 +                            (insert B, auto intro!: derivative_intros)
  7.1910 +    thus "(h has_field_derivative h2'' z) (at z)" by (simp add: h.minus_of_int)
  7.1911 +  qed
  7.1912 +  
  7.1913 +  have cont: "continuous_on UNIV h2''"
  7.1914 +  proof (intro continuous_at_imp_continuous_on ballI)
  7.1915 +    fix z :: complex
  7.1916 +    def r \<equiv> "\<lfloor>Re z\<rfloor>" 
  7.1917 +    def A \<equiv> "{t. of_int r - 1 < Re t \<and> Re t < of_int r + 1}"
  7.1918 +    have "continuous_on A (\<lambda>t. h2' (t - of_int r))" unfolding A_def
  7.1919 +      by (intro continuous_at_imp_continuous_on isCont_o2[OF _ A(2)] ballI continuous_intros)
  7.1920 +         (simp_all add: abs_real_def)
  7.1921 +    moreover have "h2'' t = h2' (t - of_int r)" if t: "t \<in> A" for t
  7.1922 +    proof (cases "Re t \<ge> of_int r")
  7.1923 +      case True
  7.1924 +      from t have "of_int r - 1 < Re t" "Re t < of_int r + 1" by (simp_all add: A_def) 
  7.1925 +      with True have "\<lfloor>Re t\<rfloor> = \<lfloor>Re z\<rfloor>" unfolding r_def by linarith
  7.1926 +      thus ?thesis by (auto simp: r_def h2''_def)
  7.1927 +    next
  7.1928 +      case False
  7.1929 +      from t have t: "of_int r - 1 < Re t" "Re t < of_int r + 1" by (simp_all add: A_def)
  7.1930 +      with False have t': "\<lfloor>Re t\<rfloor> = \<lfloor>Re z\<rfloor> - 1" unfolding r_def by linarith
  7.1931 +      moreover from t False have "h2' (t - of_int r + 1 - 1) = h2' (t - of_int r + 1)"
  7.1932 +        by (intro h2'_eq) simp_all
  7.1933 +      ultimately show ?thesis by (auto simp: r_def h2''_def algebra_simps t')
  7.1934 +    qed
  7.1935 +    ultimately have "continuous_on A h2''" by (subst continuous_on_cong[OF refl])
  7.1936 +    moreover {
  7.1937 +      have "open ({t. of_int r - 1 < Re t} \<inter> {t. of_int r + 1 > Re t})"
  7.1938 +        by (intro open_Int open_halfspace_Re_gt open_halfspace_Re_lt)
  7.1939 +      also have "{t. of_int r - 1 < Re t} \<inter> {t. of_int r + 1 > Re t} = A"
  7.1940 +        unfolding A_def by blast
  7.1941 +      finally have "open A" .
  7.1942 +    }
  7.1943 +    ultimately have C: "isCont h2'' t" if "t \<in> A" for t using that
  7.1944 +      by (subst (asm) continuous_on_eq_continuous_at) auto
  7.1945 +    have "of_int r - 1 < Re z" "Re z  < of_int r + 1" unfolding r_def by linarith+
  7.1946 +    thus "isCont h2'' z" by (intro C) (simp_all add: A_def)
  7.1947 +  qed
  7.1948 +  
  7.1949 +  from that[OF cont deriv] show ?thesis .
  7.1950 +qed
  7.1951 +
  7.1952 +lemma Gamma_reflection_complex: 
  7.1953 +  fixes z :: complex
  7.1954 +  shows "Gamma z * Gamma (1 - z) = of_real pi / sin (of_real pi * z)"
  7.1955 +proof -
  7.1956 +  let ?g = "\<lambda>z::complex. Gamma z * Gamma (1 - z) * sin (of_real pi * z)"
  7.1957 +  def g \<equiv> "\<lambda>z::complex. if z \<in> \<int> then of_real pi else ?g z"
  7.1958 +  let ?h = "\<lambda>z::complex. (of_real pi * cot (of_real pi*z) + Digamma z - Digamma (1 - z))"
  7.1959 +  def h \<equiv> "\<lambda>z::complex. if z \<in> \<int> then 0 else ?h z"
  7.1960 +
  7.1961 +  -- \<open>@{term g} is periodic with period 1.\<close>
  7.1962 +  interpret g: periodic_fun_simple' g
  7.1963 +  proof
  7.1964 +    fix z :: complex
  7.1965 +    show "g (z + 1) = g z"
  7.1966 +    proof (cases "z \<in> \<int>")
  7.1967 +      case False
  7.1968 +      hence "z * g z = z * Beta z (- z + 1) * sin (of_real pi * z)" by (simp add: g_def Beta_def)
  7.1969 +      also have "z * Beta z (- z + 1) = (z + 1 + -z) * Beta (z + 1) (- z + 1)" 
  7.1970 +        using False Ints_diff[of 1 "1 - z"] nonpos_Ints_subset_Ints
  7.1971 +        by (subst Beta_plus1_left [symmetric]) auto
  7.1972 +      also have "\<dots> * sin (of_real pi * z) = z * (Beta (z + 1) (-z) * sin (of_real pi * (z + 1)))"
  7.1973 +        using False Ints_diff[of "z+1" 1] Ints_minus[of "-z"] nonpos_Ints_subset_Ints
  7.1974 +        by (subst Beta_plus1_right) (auto simp: ring_distribs sin_plus_pi)
  7.1975 +      also from False have "Beta (z + 1) (-z) * sin (of_real pi * (z + 1)) = g (z + 1)" 
  7.1976 +        using Ints_diff[of "z+1" 1] by (auto simp: g_def Beta_def)
  7.1977 +      finally show "g (z + 1) = g z" using False by (subst (asm) mult_left_cancel) auto
  7.1978 +    qed (simp add: g_def)
  7.1979 +  qed
  7.1980 +
  7.1981 +  -- \<open>@{term g} is entire.\<close>
  7.1982 +  have g_g': "(g has_field_derivative (h z * g z)) (at z)" for z :: complex
  7.1983 +  proof (cases "z \<in> \<int>")
  7.1984 +    let ?h' = "\<lambda>z. Beta z (1 - z) * ((Digamma z - Digamma (1 - z)) * sin (z * of_real pi) +
  7.1985 +                     of_real pi * cos (z * of_real pi))"
  7.1986 +    case False
  7.1987 +    from False have "eventually (\<lambda>t. t \<in> UNIV - \<int>) (nhds z)"
  7.1988 +      by (intro eventually_nhds_in_open) (auto simp: open_Diff)
  7.1989 +    hence "eventually (\<lambda>t. g t = ?g t) (nhds z)" by eventually_elim (simp add: g_def)
  7.1990 +    moreover {
  7.1991 +      from False Ints_diff[of 1 "1-z"] have "1 - z \<notin> \<int>" by auto 
  7.1992 +      hence "(?g has_field_derivative ?h' z) (at z)" using nonpos_Ints_subset_Ints
  7.1993 +        by (auto intro!: derivative_eq_intros simp: algebra_simps Beta_def)
  7.1994 +      also from False have "sin (of_real pi * z) \<noteq> 0" by (subst sin_eq_0) auto
  7.1995 +      hence "?h' z = h z * g z"
  7.1996 +        using False unfolding g_def h_def cot_def by (simp add: field_simps Beta_def)
  7.1997 +      finally have "(?g has_field_derivative (h z * g z)) (at z)" .
  7.1998 +    }
  7.1999 +    ultimately show ?thesis by (subst DERIV_cong_ev[OF refl _ refl])
  7.2000 +  next
  7.2001 +    case True
  7.2002 +    then obtain n where z: "z = of_int n" by (auto elim!: Ints_cases)
  7.2003 +    let ?t = "(\<lambda>z::complex. if z = 0 then 1 else sin z / z) \<circ> (\<lambda>z. of_real pi * z)"
  7.2004 +    have deriv_0: "(g has_field_derivative 0) (at 0)"
  7.2005 +    proof (subst DERIV_cong_ev[OF refl _ refl])
  7.2006 +      show "eventually (\<lambda>z. g z = of_real pi * Gamma (1 + z) * Gamma (1 - z) * ?t z) (nhds 0)"
  7.2007 +        using eventually_nhds_ball[OF zero_less_one, of "0::complex"]
  7.2008 +      proof eventually_elim
  7.2009 +        fix z :: complex assume z: "z \<in> ball 0 1"
  7.2010 +        show "g z = of_real pi * Gamma (1 + z) * Gamma (1 - z) * ?t z"
  7.2011 +        proof (cases "z = 0")
  7.2012 +          assume z': "z \<noteq> 0"
  7.2013 +          with z have z'': "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z \<notin> \<int>" by (auto elim!: Ints_cases simp: dist_0_norm)
  7.2014 +          from Gamma_plus1[OF this(1)] have "Gamma z = Gamma (z + 1) / z" by simp
  7.2015 +          with z'' z' show ?thesis by (simp add: g_def ac_simps)
  7.2016 +        qed (simp add: g_def)
  7.2017 +      qed
  7.2018 +      have "(?t has_field_derivative (0 * of_real pi)) (at 0)"
  7.2019 +        using has_field_derivative_sin_z_over_z[of "UNIV :: complex set"] 
  7.2020 +        by (intro DERIV_chain) simp_all
  7.2021 +      thus "((\<lambda>z. of_real pi * Gamma (1 + z) * Gamma (1 - z) * ?t z) has_field_derivative 0) (at 0)"
  7.2022 +        by (auto intro!: derivative_eq_intros simp: o_def)
  7.2023 +    qed
  7.2024 +    
  7.2025 +    have "((g \<circ> (\<lambda>x. x - of_int n)) has_field_derivative 0 * 1) (at (of_int n))"
  7.2026 +      using deriv_0 by (intro DERIV_chain) (auto intro!: derivative_eq_intros)
  7.2027 +    also have "g \<circ> (\<lambda>x. x - of_int n) = g" by (intro ext) (simp add: g.minus_of_int)
  7.2028 +    finally show "(g has_field_derivative (h z * g z)) (at z)" by (simp add: z h_def)
  7.2029 +  qed
  7.2030 +
  7.2031 +  have g_eq: "g (z/2) * g ((z+1)/2) = Gamma (1/2)^2 * g z" if "Re z > -1" "Re z < 2" for z
  7.2032 +  proof (cases "z \<in> \<int>")
  7.2033 +    case True
  7.2034 +    with that have "z = 0 \<or> z = 1" by (force elim!: Ints_cases)
  7.2035 +    moreover have "g 0 * g (1/2) = Gamma (1/2)^2 * g 0"
  7.2036 +      using fraction_not_in_ints[where 'a = complex, of 2 1] by (simp add: g_def power2_eq_square)
  7.2037 +    moreover have "g (1/2) * g 1 = Gamma (1/2)^2 * g 1"
  7.2038 +        using fraction_not_in_ints[where 'a = complex, of 2 1]
  7.2039 +        by (simp add: g_def power2_eq_square Beta_def algebra_simps)
  7.2040 +    ultimately show ?thesis by force
  7.2041 +  next
  7.2042 +    case False
  7.2043 +    hence z: "z/2 \<notin> \<int>" "(z+1)/2 \<notin> \<int>" using Ints_diff[of "z+1" 1] by (auto elim!: Ints_cases)
  7.2044 +    hence z': "z/2 \<notin> \<int>\<^sub>\<le>\<^sub>0" "(z+1)/2 \<notin> \<int>\<^sub>\<le>\<^sub>0" by (auto elim!: nonpos_Ints_cases)
  7.2045 +    from z have "1-z/2 \<notin> \<int>" "1-((z+1)/2) \<notin> \<int>" 
  7.2046 +      using Ints_diff[of 1 "1-z/2"] Ints_diff[of 1 "1-((z+1)/2)"] by auto
  7.2047 +    hence z'': "1-z/2 \<notin> \<int>\<^sub>\<le>\<^sub>0" "1-((z+1)/2) \<notin> \<int>\<^sub>\<le>\<^sub>0" by (auto elim!: nonpos_Ints_cases)
  7.2048 +    from z have "g (z/2) * g ((z+1)/2) = 
  7.2049 +      (Gamma (z/2) * Gamma ((z+1)/2)) * (Gamma (1-z/2) * Gamma (1-((z+1)/2))) *
  7.2050 +      (sin (of_real pi * z/2) * sin (of_real pi * (z+1)/2))"
  7.2051 +      by (simp add: g_def)
  7.2052 +    also from z' Gamma_legendre_duplication_aux[of "z/2"] 
  7.2053 +      have "Gamma (z/2) * Gamma ((z+1)/2) = exp ((1-z) * of_real (ln 2)) * Gamma (1/2) * Gamma z"
  7.2054 +      by (simp add: add_divide_distrib)
  7.2055 +    also from z'' Gamma_legendre_duplication_aux[of "1-(z+1)/2"]
  7.2056 +      have "Gamma (1-z/2) * Gamma (1-(z+1)/2) = 
  7.2057 +              Gamma (1-z) * Gamma (1/2) * exp (z * of_real (ln 2))"
  7.2058 +      by (simp add: add_divide_distrib ac_simps)
  7.2059 +    finally have "g (z/2) * g ((z+1)/2) = Gamma (1/2)^2 * (Gamma z * Gamma (1-z) *
  7.2060 +                    (2 * (sin (of_real pi*z/2) * sin (of_real pi*(z+1)/2))))"
  7.2061 +      by (simp add: add_ac power2_eq_square exp_add ring_distribs exp_diff exp_of_real)
  7.2062 +    also have "sin (of_real pi*(z+1)/2) = cos (of_real pi*z/2)"
  7.2063 +      using cos_sin_eq[of "- of_real pi * z/2", symmetric]
  7.2064 +      by (simp add: ring_distribs add_divide_distrib ac_simps)
  7.2065 +    also have "2 * (sin (of_real pi*z/2) * cos (of_real pi*z/2)) = sin (of_real pi * z)"
  7.2066 +      by (subst sin_times_cos) (simp add: field_simps)
  7.2067 +    also have "Gamma z * Gamma (1 - z) * sin (complex_of_real pi * z) = g z"
  7.2068 +      using \<open>z \<notin> \<int>\<close> by (simp add: g_def)
  7.2069 +    finally show ?thesis .
  7.2070 +  qed
  7.2071 +  have g_eq: "g (z/2) * g ((z+1)/2) = Gamma (1/2)^2 * g z" for z
  7.2072 +  proof -
  7.2073 +    def r \<equiv> "\<lfloor>Re z / 2\<rfloor>"
  7.2074 +    have "Gamma (1/2)^2 * g z = Gamma (1/2)^2 * g (z - of_int (2*r))" by (simp only: g.minus_of_int)
  7.2075 +    also have "of_int (2*r) = 2 * of_int r" by simp
  7.2076 +    also have "Re z - 2 * of_int r > -1" "Re z - 2 * of_int r < 2" unfolding r_def by linarith+
  7.2077 +    hence "Gamma (1/2)^2 * g (z - 2 * of_int r) = 
  7.2078 +                   g ((z - 2 * of_int r)/2) * g ((z - 2 * of_int r + 1)/2)" 
  7.2079 +      unfolding r_def by (intro g_eq[symmetric]) simp_all
  7.2080 +    also have "(z - 2 * of_int r) / 2 = z/2 - of_int r" by simp
  7.2081 +    also have "g \<dots> = g (z/2)" by (rule g.minus_of_int)
  7.2082 +    also have "(z - 2 * of_int r + 1) / 2 = (z + 1)/2 - of_int r" by simp
  7.2083 +    also have "g \<dots> = g ((z+1)/2)" by (rule g.minus_of_int)
  7.2084 +    finally show ?thesis ..
  7.2085 +  qed
  7.2086 +    
  7.2087 +  have g_nz [simp]: "g z \<noteq> 0" for z :: complex
  7.2088 +  unfolding g_def using Ints_diff[of 1 "1 - z"]
  7.2089 +    by (auto simp: Gamma_eq_zero_iff sin_eq_0 dest!: nonpos_Ints_Int)
  7.2090 +  
  7.2091 +  have h_eq: "h z = (h (z/2) + h ((z+1)/2)) / 2" for z
  7.2092 +  proof -
  7.2093 +    have "((\<lambda>t. g (t/2) * g ((t+1)/2)) has_field_derivative
  7.2094 +                       (g (z/2) * g ((z+1)/2)) * ((h (z/2) + h ((z+1)/2)) / 2)) (at z)"
  7.2095 +      by (auto intro!: derivative_eq_intros g_g'[THEN DERIV_chain2] simp: field_simps)
  7.2096 +    hence "((\<lambda>t. Gamma (1/2)^2 * g t) has_field_derivative
  7.2097 +              Gamma (1/2)^2 * g z * ((h (z/2) + h ((z+1)/2)) / 2)) (at z)"
  7.2098 +      by (subst (1 2) g_eq[symmetric]) simp
  7.2099 +    from DERIV_cmult[OF this, of "inverse ((Gamma (1/2))^2)"]
  7.2100 +      have "(g has_field_derivative (g z * ((h (z/2) + h ((z+1)/2))/2))) (at z)"
  7.2101 +      using fraction_not_in_ints[where 'a = complex, of 2 1]
  7.2102 +      by (simp add: divide_simps Gamma_eq_zero_iff not_in_Ints_imp_not_in_nonpos_Ints)
  7.2103 +    moreover have "(g has_field_derivative (g z * h z)) (at z)"
  7.2104 +      using g_g'[of z] by (simp add: ac_simps)
  7.2105 +    ultimately have "g z * h z = g z * ((h (z/2) + h ((z+1)/2))/2)"
  7.2106 +      by (intro DERIV_unique)
  7.2107 +    thus "h z = (h (z/2) + h ((z+1)/2)) / 2" by simp
  7.2108 +  qed
  7.2109 +  
  7.2110 +  obtain h' where h'_cont: "continuous_on UNIV h'" and
  7.2111 +                  h_h': "\<And>z. (h has_field_derivative h' z) (at z)"
  7.2112 +     unfolding h_def by (erule Gamma_reflection_aux) 
  7.2113 +  
  7.2114 +  have h'_eq: "h' z = (h' (z/2) + h' ((z+1)/2)) / 4" for z
  7.2115 +  proof -
  7.2116 +    have "((\<lambda>t. (h (t/2) + h ((t+1)/2)) / 2) has_field_derivative
  7.2117 +                       ((h' (z/2) + h' ((z+1)/2)) / 4)) (at z)" 
  7.2118 +      by (fastforce intro!: derivative_eq_intros h_h'[THEN DERIV_chain2])
  7.2119 +    hence "(h has_field_derivative ((h' (z/2) + h' ((z+1)/2))/4)) (at z)"
  7.2120 +      by (subst (asm) h_eq[symmetric])
  7.2121 +    from h_h' and this show "h' z = (h' (z/2) + h' ((z+1)/2)) / 4" by (rule DERIV_unique)
  7.2122 +  qed
  7.2123 +  
  7.2124 +  have h'_zero: "h' z = 0" for z
  7.2125 +  proof -
  7.2126 +    def m \<equiv> "max 1 \<bar>Re z\<bar>"
  7.2127 +    def B \<equiv> "{t. abs (Re t) \<le> m \<and> abs (Im t) \<le> abs (Im z)}"
  7.2128 +    have "closed ({t. Re t \<ge> -m} \<inter> {t. Re t \<le> m} \<inter> 
  7.2129 +                  {t. Im t \<ge> -\<bar>Im z\<bar>} \<inter> {t. Im t \<le> \<bar>Im z\<bar>})"
  7.2130 +      (is "closed ?B") by (intro closed_Int closed_halfspace_Re_ge closed_halfspace_Re_le 
  7.2131 +                                 closed_halfspace_Im_ge closed_halfspace_Im_le)
  7.2132 +    also have "?B = B" unfolding B_def by fastforce
  7.2133 +    finally have "closed B" .
  7.2134 +    moreover have "bounded B" unfolding bounded_iff
  7.2135 +    proof (intro ballI exI)
  7.2136 +      fix t assume t: "t \<in> B"
  7.2137 +      have "norm t \<le> \<bar>Re t\<bar> + \<bar>Im t\<bar>" by (rule cmod_le)
  7.2138 +      also from t have "\<bar>Re t\<bar> \<le> m" unfolding B_def by blast
  7.2139 +      also from t have "\<bar>Im t\<bar> \<le> \<bar>Im z\<bar>" unfolding B_def by blast
  7.2140 +      finally show "norm t \<le> m + \<bar>Im z\<bar>" by - simp
  7.2141 +    qed
  7.2142 +    ultimately have compact: "compact B" by (subst compact_eq_bounded_closed) blast
  7.2143 +    
  7.2144 +    def M \<equiv> "SUP z:B. norm (h' z)"
  7.2145 +    have "compact (h' ` B)"
  7.2146 +      by (intro compact_continuous_image continuous_on_subset[OF h'_cont] compact) blast+
  7.2147 +    hence bdd: "bdd_above ((\<lambda>z. norm (h' z)) ` B)"
  7.2148 +      using bdd_above_norm[of "h' ` B"] by (simp add: image_comp o_def compact_imp_bounded)
  7.2149 +    have "norm (h' z) \<le> M" unfolding M_def by (intro cSUP_upper bdd) (simp_all add: B_def m_def)
  7.2150 +    also have "M \<le> M/2"
  7.2151 +    proof (subst M_def, subst cSUP_le_iff)
  7.2152 +      have "z \<in> B" unfolding B_def m_def by simp
  7.2153 +      thus "B \<noteq> {}" by auto
  7.2154 +    next
  7.2155 +      show "\<forall>z\<in>B. norm (h' z) \<le> M/2"
  7.2156 +      proof
  7.2157 +        fix t :: complex assume t: "t \<in> B"
  7.2158 +        from h'_eq[of t] t have "h' t = (h' (t/2) + h' ((t+1)/2)) / 4" by (simp add: dist_0_norm)
  7.2159 +        also have "norm \<dots> = norm (h' (t/2) + h' ((t+1)/2)) / 4" by simp
  7.2160 +        also have "norm (h' (t/2) + h' ((t+1)/2)) \<le> norm (h' (t/2)) + norm (h' ((t+1)/2))"
  7.2161 +          by (rule norm_triangle_ineq)
  7.2162 +        also from t have "abs (Re ((t + 1)/2)) \<le> m" unfolding m_def B_def by auto
  7.2163 +        with t have "t/2 \<in> B" "(t+1)/2 \<in> B" unfolding B_def by auto        
  7.2164 +        hence "norm (h' (t/2)) + norm (h' ((t+1)/2)) \<le> M + M" unfolding M_def 
  7.2165 +          by (intro add_mono cSUP_upper bdd) (auto simp: B_def)
  7.2166 +        also have "(M + M) / 4 = M / 2" by simp
  7.2167 +        finally show "norm (h' t) \<le> M/2" by - simp_all
  7.2168 +      qed
  7.2169 +    qed (insert bdd, auto simp: cball_eq_empty)
  7.2170 +    hence "M \<le> 0" by simp
  7.2171 +    finally show "h' z = 0" by simp
  7.2172 +  qed
  7.2173 +  have h_h'_2: "(h has_field_derivative 0) (at z)" for z
  7.2174 +    using h_h'[of z] h'_zero[of z] by simp
  7.2175 +  
  7.2176 +  have g_real: "g z \<in> \<real>" if "z \<in> \<real>" for z
  7.2177 +    unfolding g_def using that by (auto intro!: Reals_mult Gamma_complex_real)
  7.2178 +  have h_real: "h z \<in> \<real>" if "z \<in> \<real>" for z
  7.2179 +    unfolding h_def using that by (auto intro!: Reals_mult Reals_add Reals_diff Polygamma_Real)
  7.2180 +  have g_nz: "g z \<noteq> 0" for z unfolding g_def using Ints_diff[of 1 "1-z"]
  7.2181 +    by (auto simp: Gamma_eq_zero_iff sin_eq_0)
  7.2182 +  
  7.2183 +  from h'_zero h_h'_2 have "\<exists>c. \<forall>z\<in>UNIV. h z = c"
  7.2184 +    by (intro has_field_derivative_zero_constant) (simp_all add: dist_0_norm)
  7.2185 +  then obtain c where c: "\<And>z. h z = c" by auto
  7.2186 +  have "\<exists>u. u \<in> closed_segment 0 1 \<and> Re (g 1) - Re (g 0) = Re (h u * g u * (1 - 0))"
  7.2187 +    by (intro complex_mvt_line g_g')
  7.2188 +    find_theorems name:deriv Reals
  7.2189 +  then guess u by (elim exE conjE) note u = this
  7.2190 +  from u(1) have u': "u \<in> \<real>" unfolding closed_segment_def 
  7.2191 +    by (auto simp: scaleR_conv_of_real)
  7.2192 +  from u' g_real[of u] g_nz[of u] have "Re (g u) \<noteq> 0" by (auto elim!: Reals_cases)
  7.2193 +  with u(2) c[of u] g_real[of u] g_nz[of u] u'
  7.2194 +    have "Re c = 0" by (simp add: complex_is_Real_iff g.of_1)
  7.2195 +  with h_real[of 0] c[of 0] have "c = 0" by (auto elim!: Reals_cases)
  7.2196 +  with c have A: "h z * g z = 0" for z by simp
  7.2197 +  hence "(g has_field_derivative 0) (at z)" for z using g_g'[of z] by simp
  7.2198 +  hence "\<exists>c'. \<forall>z\<in>UNIV. g z = c'" by (intro has_field_derivative_zero_constant) simp_all
  7.2199 +  then obtain c' where c: "\<And>z. g z = c'" by (force simp: dist_0_norm)
  7.2200 +  moreover from this[of 0] have "c' = pi" unfolding g_def by simp
  7.2201 +  ultimately have "g z = pi" by simp
  7.2202 +  
  7.2203 +  show ?thesis
  7.2204 +  proof (cases "z \<in> \<int>")
  7.2205 +    case False
  7.2206 +    with `g z = pi` show ?thesis by (auto simp: g_def divide_simps)
  7.2207 +  next
  7.2208 +    case True
  7.2209 +    then obtain n where n: "z = of_int n" by (elim Ints_cases)
  7.2210 +    with sin_eq_0[of "of_real pi * z"] have "sin (of_real pi * z) = 0" by force
  7.2211 +    moreover have "of_int (1 - n) \<in> \<int>\<^sub>\<le>\<^sub>0" if "n > 0" using that by (intro nonpos_Ints_of_int) simp
  7.2212 +    ultimately show ?thesis using n
  7.2213 +      by (cases "n \<le> 0") (auto simp: Gamma_eq_zero_iff nonpos_Ints_of_int)
  7.2214 +  qed
  7.2215 +qed
  7.2216 +
  7.2217 +lemma rGamma_reflection_complex: 
  7.2218 +  "rGamma z * rGamma (1 - z :: complex) = sin (of_real pi * z) / of_real pi"
  7.2219 +  using Gamma_reflection_complex[of z]
  7.2220 +    by (simp add: Gamma_def divide_simps split: split_if_asm)
  7.2221 +
  7.2222 +lemma rGamma_reflection_complex': 
  7.2223 +  "rGamma z * rGamma (- z :: complex) = -z * sin (of_real pi * z) / of_real pi"
  7.2224 +proof -
  7.2225 +  have "rGamma z * rGamma (-z) = -z * (rGamma z * rGamma (1 - z))"
  7.2226 +    using rGamma_plus1[of "-z", symmetric] by simp
  7.2227 +  also have "rGamma z * rGamma (1 - z) = sin (of_real pi * z) / of_real pi"
  7.2228 +    by (rule rGamma_reflection_complex)
  7.2229 +  finally show ?thesis by simp
  7.2230 +qed
  7.2231 +
  7.2232 +lemma Gamma_reflection_complex': 
  7.2233 +  "Gamma z * Gamma (- z :: complex) = - of_real pi / (z * sin (of_real pi * z))"
  7.2234 +  using rGamma_reflection_complex'[of z] by (force simp add: Gamma_def divide_simps mult_ac)
  7.2235 +
  7.2236 +
  7.2237 +
  7.2238 +lemma Gamma_one_half_real: "Gamma (1/2 :: real) = sqrt pi"
  7.2239 +proof -
  7.2240 +  from Gamma_reflection_complex[of "1/2"] fraction_not_in_ints[where 'a = complex, of 2 1]
  7.2241 +    have "Gamma (1/2 :: complex)^2 = of_real pi" by (simp add: power2_eq_square)
  7.2242 +  hence "of_real pi = Gamma (complex_of_real (1/2))^2" by simp
  7.2243 +  also have "\<dots> = of_real ((Gamma (1/2))^2)" by (subst Gamma_complex_of_real) simp_all
  7.2244 +  finally have "Gamma (1/2)^2 = pi" by (subst (asm) of_real_eq_iff) simp_all
  7.2245 +  moreover have "Gamma (1/2 :: real) \<ge> 0" using Gamma_real_pos[of "1/2"] by simp
  7.2246 +  ultimately show ?thesis by (rule real_sqrt_unique [symmetric])
  7.2247 +qed
  7.2248 +
  7.2249 +lemma Gamma_one_half_complex: "Gamma (1/2 :: complex) = of_real (sqrt pi)"
  7.2250 +proof -
  7.2251 +  have "Gamma (1/2 :: complex) = Gamma (of_real (1/2))" by simp
  7.2252 +  also have "\<dots> = of_real (sqrt pi)" by (simp only: Gamma_complex_of_real Gamma_one_half_real)
  7.2253 +  finally show ?thesis .
  7.2254 +qed
  7.2255 +
  7.2256 +lemma Gamma_legendre_duplication:
  7.2257 +  fixes z :: complex
  7.2258 +  assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z + 1/2 \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.2259 +  shows "Gamma z * Gamma (z + 1/2) = 
  7.2260 +             exp ((1 - 2*z) * of_real (ln 2)) * of_real (sqrt pi) * Gamma (2*z)"
  7.2261 +  using Gamma_legendre_duplication_aux[OF assms] by (simp add: Gamma_one_half_complex)
  7.2262 +
  7.2263 +end
  7.2264 +
  7.2265 +
  7.2266 +subsection \<open>Limits and residues\<close>
  7.2267 +
  7.2268 +text \<open>
  7.2269 +  The inverse Gamma function has simple zeros:
  7.2270 +\<close>
  7.2271 +
  7.2272 +lemma rGamma_zeros: 
  7.2273 +  "(\<lambda>z. rGamma z / (z + of_nat n)) \<midarrow> (- of_nat n) \<rightarrow> ((-1)^n * fact n :: 'a :: Gamma)"
  7.2274 +proof (subst tendsto_cong)
  7.2275 +  let ?f = "\<lambda>z. pochhammer z n * rGamma (z + of_nat (Suc n)) :: 'a"
  7.2276 +  from eventually_at_ball'[OF zero_less_one, of "- of_nat n :: 'a" UNIV]
  7.2277 +    show "eventually (\<lambda>z. rGamma z / (z + of_nat n) = ?f z) (at (- of_nat n))" 
  7.2278 +    by (subst pochhammer_rGamma[of _ "Suc n"])
  7.2279 +       (auto elim!: eventually_mono simp: divide_simps pochhammer_rec' eq_neg_iff_add_eq_0)
  7.2280 +  have "isCont ?f (- of_nat n)" by (intro continuous_intros)
  7.2281 +  thus "?f \<midarrow> (- of_nat n) \<rightarrow> (- 1) ^ n * fact n" unfolding isCont_def
  7.2282 +    by (simp add: pochhammer_same)
  7.2283 +qed
  7.2284 +
  7.2285 +
  7.2286 +text \<open>
  7.2287 +  The simple zeros of the inverse Gamma function correspond to simple poles of the Gamma function, 
  7.2288 +  and their residues can easily be computed from the limit we have just proven:
  7.2289 +\<close>
  7.2290 +
  7.2291 +lemma Gamma_poles: "filterlim Gamma at_infinity (at (- of_nat n :: 'a :: Gamma))"
  7.2292 +proof -
  7.2293 +  from eventually_at_ball'[OF zero_less_one, of "- of_nat n :: 'a" UNIV]
  7.2294 +    have "eventually (\<lambda>z. rGamma z \<noteq> (0 :: 'a)) (at (- of_nat n))"
  7.2295 +    by (auto elim!: eventually_mono nonpos_Ints_cases'
  7.2296 +             simp: rGamma_eq_zero_iff dist_of_nat dist_minus)
  7.2297 +  with isCont_rGamma[of "- of_nat n :: 'a", OF continuous_ident] 
  7.2298 +    have "filterlim (\<lambda>z. inverse (rGamma z) :: 'a) at_infinity (at (- of_nat n))"
  7.2299 +    unfolding isCont_def by (intro filterlim_compose[OF filterlim_inverse_at_infinity])
  7.2300 +                            (simp_all add: filterlim_at)
  7.2301 +  moreover have "(\<lambda>z. inverse (rGamma z) :: 'a) = Gamma" 
  7.2302 +    by (intro ext) (simp add: rGamma_inverse_Gamma)
  7.2303 +  ultimately show ?thesis by (simp only: )
  7.2304 +qed
  7.2305 +
  7.2306 +lemma Gamma_residues: 
  7.2307 +  "(\<lambda>z. Gamma z * (z + of_nat n)) \<midarrow> (- of_nat n) \<rightarrow> ((-1)^n / fact n :: 'a :: Gamma)"
  7.2308 +proof (subst tendsto_cong)
  7.2309 +  let ?c = "(- 1) ^ n / fact n :: 'a"
  7.2310 +  from eventually_at_ball'[OF zero_less_one, of "- of_nat n :: 'a" UNIV]
  7.2311 +    show "eventually (\<lambda>z. Gamma z * (z + of_nat n) = inverse (rGamma z / (z + of_nat n))) 
  7.2312 +            (at (- of_nat n))" 
  7.2313 +    by (auto elim!: eventually_mono simp: divide_simps rGamma_inverse_Gamma)
  7.2314 +  have "(\<lambda>z. inverse (rGamma z / (z + of_nat n))) \<midarrow> (- of_nat n) \<rightarrow> 
  7.2315 +          inverse ((- 1) ^ n * fact n :: 'a)"
  7.2316 +    by (intro tendsto_intros rGamma_zeros) simp_all
  7.2317 +  also have "inverse ((- 1) ^ n * fact n) = ?c" 
  7.2318 +    by (simp_all add: field_simps power_mult_distrib [symmetric] del: power_mult_distrib)
  7.2319 +  finally show "(\<lambda>z. inverse (rGamma z / (z + of_nat n))) \<midarrow> (- of_nat n) \<rightarrow> ?c" .
  7.2320 +qed
  7.2321 +
  7.2322 +
  7.2323 +
  7.2324 +subsection \<open>Alternative definitions\<close>
  7.2325 +
  7.2326 +
  7.2327 +subsubsection \<open>Variant of the Euler form\<close>
  7.2328 +
  7.2329 +
  7.2330 +definition Gamma_series_euler' where
  7.2331 +  "Gamma_series_euler' z n = 
  7.2332 +     inverse z * (\<Prod>k=1..n. exp (z * of_real (ln (1 + inverse (of_nat k)))) / (1 + z / of_nat k))"
  7.2333 +
  7.2334 +context
  7.2335 +begin
  7.2336 +private lemma Gamma_euler'_aux1:
  7.2337 +  fixes z :: "'a :: {real_normed_field,banach}"
  7.2338 +  assumes n: "n > 0"
  7.2339 +  shows "exp (z * of_real (ln (of_nat n + 1))) = (\<Prod>k=1..n. exp (z * of_real (ln (1 + 1 / of_nat k))))"
  7.2340 +proof -
  7.2341 +  have "(\<Prod>k=1..n. exp (z * of_real (ln (1 + 1 / of_nat k)))) = 
  7.2342 +          exp (z * of_real (\<Sum>k = 1..n. ln (1 + 1 / real_of_nat k)))"
  7.2343 +    by (subst exp_setsum [symmetric]) (simp_all add: setsum_right_distrib)
  7.2344 +  also have "(\<Sum>k=1..n. ln (1 + 1 / of_nat k) :: real) = ln (\<Prod>k=1..n. 1 + 1 / real_of_nat k)"
  7.2345 +    by (subst ln_setprod [symmetric]) (auto intro!: add_pos_nonneg)
  7.2346 +  also have "(\<Prod>k=1..n. 1 + 1 / of_nat k :: real) = (\<Prod>k=1..n. (of_nat k + 1) / of_nat k)"
  7.2347 +    by (intro setprod.cong) (simp_all add: divide_simps)
  7.2348 +  also have "(\<Prod>k=1..n. (of_nat k + 1) / of_nat k :: real) = of_nat n + 1"
  7.2349 +    by (induction n) (simp_all add: setprod_nat_ivl_Suc' divide_simps)
  7.2350 +  finally show ?thesis ..
  7.2351 +qed
  7.2352 +
  7.2353 +lemma Gamma_series_euler':
  7.2354 +  assumes z: "(z :: 'a :: Gamma) \<notin> \<int>\<^sub>\<le>\<^sub>0"
  7.2355 +  shows "(\<lambda>n. Gamma_series_euler' z n) \<longlonglongrightarrow> Gamma z"
  7.2356 +proof (rule Gamma_seriesI, rule Lim_transform_eventually)
  7.2357 +  let ?f = "\<lambda>n. fact n * exp (z * of_real (ln (of_nat n + 1))) / pochhammer z (n + 1)"
  7.2358 +  let ?r = "\<lambda>n. ?f n / Gamma_series z n"
  7.2359 +  let ?r' = "\<lambda>n. exp (z * of_real (ln (of_nat (Suc n) / of_nat n)))"
  7.2360 +  from z have z': "z \<noteq> 0" by auto
  7.2361 +
  7.2362 +  have "eventually (\<lambda>n. ?r' n = ?r n) sequentially" using eventually_gt_at_top[of "0::nat"]
  7.2363 +    using z by (auto simp: divide_simps Gamma_series_def ring_distribs exp_diff ln_div add_ac
  7.2364 +                     elim!: eventually_mono dest: pochhammer_eq_0_imp_nonpos_Int)
  7.2365 +  moreover have "?r' \<longlonglongrightarrow> exp (z * of_real (ln 1))"
  7.2366 +    by (intro tendsto_intros LIMSEQ_Suc_n_over_n) simp_all
  7.2367 +  ultimately show "?r \<longlonglongrightarrow> 1" by (force dest!: Lim_transform_eventually)
  7.2368 +  
  7.2369 +  from eventually_gt_at_top[of "0::nat"]
  7.2370 +    show "eventually (\<lambda>n. ?r n = Gamma_series_euler' z n / Gamma_series z n) sequentially"
  7.2371 +  proof eventually_elim
  7.2372 +    fix n :: nat assume n: "n > 0"
  7.2373 +    from n z' have "Gamma_series_euler' z n = 
  7.2374 +      exp (z * of_real (ln (of_nat n + 1))) / (z * (\<Prod>k=1..n. (1 + z / of_nat k)))"
  7.2375 +      by (subst Gamma_euler'_aux1) 
  7.2376 +         (simp_all add: Gamma_series_euler'_def setprod.distrib 
  7.2377 +                        setprod_inversef[symmetric] divide_inverse)
  7.2378 +    also have "(\<Prod>k=1..n. (1 + z / of_nat k)) = pochhammer (z + 1) n / fact n"
  7.2379 +      by (cases n) (simp_all add: pochhammer_def fact_altdef setprod_shift_bounds_cl_Suc_ivl 
  7.2380 +                                  setprod_dividef[symmetric] divide_simps add_ac)
  7.2381 +    also have "z * \<dots> = pochhammer z (Suc n) / fact n" by (simp add: pochhammer_rec)
  7.2382 +    finally show "?r n = Gamma_series_euler' z n / Gamma_series z n" by simp
  7.2383 +  qed
  7.2384 +qed
  7.2385 +
  7.2386 +end
  7.2387 +
  7.2388 +
  7.2389 +
  7.2390 +subsubsection \<open>Weierstrass form\<close>
  7.2391 +
  7.2392 +definition Gamma_series_weierstrass :: "'a :: {banach,real_normed_field} \<Rightarrow> nat \<Rightarrow> 'a" where
  7.2393 +  "Gamma_series_weierstrass z n = 
  7.2394 +     exp (-euler_mascheroni * z) / z * (\<Prod>k=1..n. exp (z / of_nat k) / (1 + z / of_nat k))"
  7.2395 +
  7.2396 +definition rGamma_series_weierstrass :: "'a :: {banach,real_normed_field} \<Rightarrow> nat \<Rightarrow> 'a" where
  7.2397 +  "rGamma_series_weierstrass z n = 
  7.2398 +     exp (euler_mascheroni * z) * z * (\<Prod>k=1..n. (1 + z / of_nat k) * exp (-z / of_nat k))"
  7.2399 +
  7.2400 +lemma Gamma_series_weierstrass_nonpos_Ints:
  7.2401 +  "eventually (\<lambda>k. Gamma_series_weierstrass (- of_nat n) k = 0) sequentially"
  7.2402 +  using eventually_ge_at_top[of n] by eventually_elim (auto simp: Gamma_series_weierstrass_def)
  7.2403 +
  7.2404 +lemma rGamma_series_weierstrass_nonpos_Ints:
  7.2405 +  "eventually (\<lambda>k. rGamma_series_weierstrass (- of_nat n) k = 0) sequentially"
  7.2406 +  using eventually_ge_at_top[of n] by eventually_elim (auto simp: rGamma_series_weierstrass_def)
  7.2407 +  
  7.2408 +lemma Gamma_weierstrass_complex: "Gamma_series_weierstrass z \<longlonglongrightarrow> Gamma (z :: complex)"
  7.2409 +proof (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0")
  7.2410 +  case True
  7.2411 +  then obtain n where "z = - of_nat n" by (elim nonpos_Ints_cases')
  7.2412 +  also from True have "Gamma_series_weierstrass \<dots> \<longlonglongrightarrow> Gamma z"
  7.2413 +    by (simp add: tendsto_cong[OF Gamma_series_weierstrass_nonpos_Ints] Gamma_nonpos_Int)
  7.2414 +  finally show ?thesis .
  7.2415 +next
  7.2416 +  case False
  7.2417 +  hence z: "z \<noteq> 0" by auto
  7.2418 +  let ?f = "(\<lambda>x. \<Prod>x = Suc 0..x. exp (z / of_nat x) / (1 + z / of_nat x))"
  7.2419 +  have A: "exp (ln (1 + z / of_nat n)) = (1 + z / of_nat n)" if "n \<ge> 1" for n :: nat
  7.2420 +    using False that by (subst exp_Ln) (auto simp: field_simps dest!: plus_of_nat_eq_0_imp)
  7.2421 +  have "(\<lambda>n. \<Sum>k=1..n. z / of_nat k - ln (1 + z / of_nat k)) \<longlonglongrightarrow> ln_Gamma z + euler_mascheroni * z + ln z"
  7.2422 +    using ln_Gamma_series'_aux[OF False]
  7.2423 +    by (simp only: atLeastLessThanSuc_atLeastAtMost [symmetric] One_nat_def 
  7.2424 +                   setsum_shift_bounds_Suc_ivl sums_def atLeast0LessThan)
  7.2425 +  from tendsto_exp[OF this] False z have "?f \<longlonglongrightarrow> z * exp (euler_mascheroni * z) * Gamma z"
  7.2426 +    by (simp add: exp_add exp_setsum exp_diff mult_ac Gamma_complex_altdef A)
  7.2427 +  from tendsto_mult[OF tendsto_const[of "exp (-euler_mascheroni * z) / z"] this] z
  7.2428 +    show "Gamma_series_weierstrass z \<longlonglongrightarrow> Gamma z" 
  7.2429 +    by (simp add: exp_minus divide_simps Gamma_series_weierstrass_def [abs_def])
  7.2430 +qed
  7.2431 +
  7.2432 +lemma Gamma_weierstrass_real: "Gamma_series_weierstrass x \<longlonglongrightarrow> Gamma (x :: real)"
  7.2433 +  using Gamma_weierstrass_complex[of "of_real x"] unfolding Gamma_series_weierstrass_def[abs_def]
  7.2434 +  by (subst tendsto_complex_of_real_iff [symmetric])
  7.2435 +     (simp_all add: exp_of_real[symmetric] Gamma_complex_of_real)
  7.2436 +
  7.2437 +lemma rGamma_weierstrass_complex: "rGamma_series_weierstrass z \<longlonglongrightarrow> rGamma (z :: complex)"
  7.2438 +proof (cases "z \<in> \<int>\<^sub>\<le>\<^sub>0")
  7.2439 +  case True
  7.2440 +  then obtain n where "z = - of_nat n" by (elim nonpos_Ints_cases')
  7.2441 +  also from True have "rGamma_series_weierstrass \<dots> \<longlonglongrightarrow> rGamma z"
  7.2442 +    by (simp add: tendsto_cong[OF rGamma_series_weierstrass_nonpos_Ints] rGamma_nonpos_Int)
  7.2443 +  finally show ?thesis .
  7.2444 +next
  7.2445 +  case False
  7.2446 +  have "rGamma_series_weierstrass z = (\<lambda>n. inverse (Gamma_series_weierstrass z n))"
  7.2447 +    by (simp add: rGamma_series_weierstrass_def[abs_def] Gamma_series_weierstrass_def 
  7.2448 +                  exp_minus divide_inverse setprod_inversef[symmetric] mult_ac)
  7.2449 +  also from False have "\<dots> \<longlonglongrightarrow> inverse (Gamma z)"
  7.2450 +    by (intro tendsto_intros Gamma_weierstrass_complex) (simp add: Gamma_eq_zero_iff)
  7.2451 +  finally show ?thesis by (simp add: Gamma_def)
  7.2452 +qed
  7.2453 +
  7.2454 +lemma sin_product_formula_complex:
  7.2455 +  fixes z :: complex
  7.2456 +  shows "(\<lambda>n. of_real pi * z * (\<Prod>k=1..n. 1 - z^2 / of_nat k^2)) \<longlonglongrightarrow> sin (of_real pi * z)"
  7.2457 +proof -
  7.2458 +  let ?f = "rGamma_series_weierstrass"
  7.2459 +  have "(\<lambda>n. (- of_real pi * inverse z) * (?f z n * ?f (- z) n))
  7.2460 +            \<longlonglongrightarrow> (- of_real pi * inverse z) * (rGamma z * rGamma (- z))"
  7.2461 +    by (intro tendsto_intros rGamma_weierstrass_complex)
  7.2462 +  also have "(\<lambda>n. (- of_real pi * inverse z) * (?f z n * ?f (-z) n)) =
  7.2463 +                    (\<lambda>n. of_real pi * z * (\<Prod>k=1..n. 1 - z^2 / of_nat k ^ 2))"
  7.2464 +  proof
  7.2465 +    fix n :: nat
  7.2466 +    have "(- of_real pi * inverse z) * (?f z n * ?f (-z) n) = 
  7.2467 +              of_real pi * z * (\<Prod>k=1..n. (of_nat k - z) * (of_nat k + z) / of_nat k ^ 2)"
  7.2468 +      by (simp add: rGamma_series_weierstrass_def mult_ac exp_minus
  7.2469 +                    divide_simps setprod.distrib[symmetric] power2_eq_square)
  7.2470 +    also have "(\<Prod>k=1..n. (of_nat k - z) * (of_nat k + z) / of_nat k ^ 2) =
  7.2471 +                 (\<Prod>k=1..n. 1 - z^2 / of_nat k ^ 2)"
  7.2472 +      by (intro setprod.cong) (simp_all add: power2_eq_square field_simps)
  7.2473 +    finally show "(- of_real pi * inverse z) * (?f z n * ?f (-z) n) = of_real pi * z * \<dots>" 
  7.2474 +      by (simp add: divide_simps)
  7.2475 +  qed
  7.2476 +  also have "(- of_real pi * inverse z) * (rGamma z * rGamma (- z)) = sin (of_real pi * z)"
  7.2477 +    by (subst rGamma_reflection_complex') (simp add: divide_simps)
  7.2478 +  finally show ?thesis .
  7.2479 +qed
  7.2480 +
  7.2481 +lemma sin_product_formula_real:
  7.2482 +  "(\<lambda>n. pi * (x::real) * (\<Prod>k=1..n. 1 - x^2 / of_nat k^2)) \<longlonglongrightarrow> sin (pi * x)"
  7.2483 +proof -
  7.2484 +  from sin_product_formula_complex[of "of_real x"]
  7.2485 +    have "(\<lambda>n. of_real pi * of_real x * (\<Prod>k=1..n. 1 - (of_real x)^2 / (of_nat k)^2))
  7.2486 +              \<longlonglongrightarrow> sin (of_real pi * of_real x :: complex)" (is "?f \<longlonglongrightarrow> ?y") .
  7.2487 +  also have "?f = (\<lambda>n. of_real (pi * x * (\<Prod>k=1..n. 1 - x^2 / (of_nat k^2))))" by simp
  7.2488 +  also have "?y = of_real (sin (pi * x))" by (simp only: sin_of_real [symmetric] of_real_mult)
  7.2489 +  finally show ?thesis by (subst (asm) tendsto_of_real_iff)
  7.2490 +qed
  7.2491 +
  7.2492 +lemma sin_product_formula_real':
  7.2493 +  assumes "x \<noteq> (0::real)"
  7.2494 +  shows   "(\<lambda>n. (\<Prod>k=1..n. 1 - x^2 / of_nat k^2)) \<longlonglongrightarrow> sin (pi * x) / (pi * x)" 
  7.2495 +  using tendsto_divide[OF sin_product_formula_real[of x] tendsto_const[of "pi * x"]] assms
  7.2496 +  by simp
  7.2497 +
  7.2498 +theorem inverse_squares_sums: "(\<lambda>n. 1 / (n + 1)\<^sup>2) sums (pi\<^sup>2 / 6)"
  7.2499 +proof -
  7.2500 +  def P \<equiv> "\<lambda>x n. (\<Prod>k=1..n. 1 - x^2 / of_nat k^2 :: real)"
  7.2501 +  def K \<equiv> "\<Sum>n. inverse (real_of_nat (Suc n))^2"
  7.2502 +  def f \<equiv> "\<lambda>x. \<Sum>n. P x n / of_nat (Suc n)^2"
  7.2503 +  def g \<equiv> "\<lambda>x. (1 - sin (pi * x) / (pi * x))"
  7.2504 +  
  7.2505 +  have sums: "(\<lambda>n. P x n / of_nat (Suc n)^2) sums (if x = 0 then K else g x / x^2)" for x
  7.2506 +  proof (cases "x = 0")
  7.2507 +    assume x: "x = 0"
  7.2508 +    have "summable (\<lambda>n. inverse ((real_of_nat (Suc n))\<^sup>2))"
  7.2509 +      using inverse_power_summable[of 2] by (subst summable_Suc_iff) simp
  7.2510 +    thus ?thesis by (simp add: x g_def P_def K_def inverse_eq_divide power_divide summable_sums)
  7.2511 +  next
  7.2512 +    assume x: "x \<noteq> 0"
  7.2513 +    have "(\<lambda>n. P x n - P x (Suc n)) sums (P x 0 - sin (pi * x) / (pi * x))"
  7.2514 +      unfolding P_def using x by (intro telescope_sums' sin_product_formula_real')
  7.2515 +    also have "(\<lambda>n. P x n - P x (Suc n)) = (\<lambda>n. (x^2 / of_nat (Suc n)^2) * P x n)"
  7.2516 +      unfolding P_def by (simp add: setprod_nat_ivl_Suc' algebra_simps)
  7.2517 +    also have "P x 0 = 1" by (simp add: P_def)
  7.2518 +    finally have "(\<lambda>n. x\<^sup>2 / (of_nat (Suc n))\<^sup>2 * P x n) sums (1 - sin (pi * x) / (pi * x))" .
  7.2519 +    from sums_divide[OF this, of "x^2"] x show ?thesis unfolding g_def by simp
  7.2520 +  qed
  7.2521 +  
  7.2522 +  have "continuous_on (ball 0 1) f"
  7.2523 +  proof (rule uniform_limit_theorem; (intro always_eventually allI)?)
  7.2524 +    show "uniform_limit (ball 0 1) (\<lambda>n x. \<Sum>k<n. P x k / of_nat (Suc k)^2) f sequentially"
  7.2525 +    proof (unfold f_def, rule weierstrass_m_test)
  7.2526 +      fix n :: nat and x :: real assume x: "x \<in> ball 0 1"
  7.2527 +      {
  7.2528 +        fix k :: nat assume k: "k \<ge> 1"
  7.2529 +        from x have "x^2 < 1" by (auto simp: dist_0_norm abs_square_less_1)
  7.2530 +        also from k have "\<dots> \<le> of_nat k^2" by simp
  7.2531 +        finally have "(1 - x^2 / of_nat k^2) \<in> {0..1}" using k 
  7.2532 +          by (simp_all add: field_simps del: of_nat_Suc)
  7.2533 +      }
  7.2534 +      hence "(\<Prod>k=1..n. abs (1 - x^2 / of_nat k^2)) \<le> (\<Prod>k=1..n. 1)" by (intro setprod_mono) simp
  7.2535 +      thus "norm (P x n / (of_nat (Suc n)^2)) \<le> 1 / of_nat (Suc n)^2"
  7.2536 +        unfolding P_def by (simp add: field_simps abs_setprod del: of_nat_Suc)
  7.2537 +    qed (subst summable_Suc_iff, insert inverse_power_summable[of 2], simp add: inverse_eq_divide)
  7.2538 +  qed (auto simp: P_def intro!: continuous_intros)
  7.2539 +  hence "isCont f 0" by (subst (asm) continuous_on_eq_continuous_at) simp_all
  7.2540 +  hence "(f \<midarrow> 0 \<rightarrow> f 0)" by (simp add: isCont_def)
  7.2541 +  also have "f 0 = K" unfolding f_def P_def K_def by (simp add: inverse_eq_divide power_divide)
  7.2542 +  finally have "f \<midarrow> 0 \<rightarrow> K" . 
  7.2543 +  
  7.2544 +  moreover have "f \<midarrow> 0 \<rightarrow> pi^2 / 6"
  7.2545 +  proof (rule Lim_transform_eventually)
  7.2546 +    def f' \<equiv> "\<lambda>x. \<Sum>n. - sin_coeff (n+3) * pi ^ (n+2) * x^n"
  7.2547 +    have "eventually (\<lambda>x. x \<noteq> (0::real)) (at 0)" 
  7.2548 +      by (auto simp add: eventually_at intro!: exI[of _ 1])
  7.2549 +    thus "eventually (\<lambda>x. f' x = f x) (at 0)"
  7.2550 +    proof eventually_elim
  7.2551 +      fix x :: real assume x: "x \<noteq> 0"
  7.2552 +      have "sin_coeff 1 = (1 :: real)" "sin_coeff 2 = (0::real)" by (simp_all add: sin_coeff_def)
  7.2553 +      with sums_split_initial_segment[OF sums_minus[OF sin_converges], of 3 "pi*x"]
  7.2554 +      have "(\<lambda>n. - (sin_coeff (n+3) * (pi*x)^(n+3))) sums (pi * x - sin (pi*x))"
  7.2555 +        by (simp add: eval_nat_numeral)
  7.2556 +      from sums_divide[OF this, of "x^3 * pi"] x
  7.2557 +        have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums ((1 - sin (pi*x) / (pi*x)) / x^2)"
  7.2558 +        by (simp add: divide_simps eval_nat_numeral power_mult_distrib mult_ac)
  7.2559 +      with x have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums (g x / x^2)"
  7.2560 +        by (simp add: g_def)
  7.2561 +      hence "f' x = g x / x^2" by (simp add: sums_iff f'_def)
  7.2562 +      also have "\<dots> = f x" using sums[of x] x by (simp add: sums_iff g_def f_def)
  7.2563 +      finally show "f' x = f x" .
  7.2564 +    qed
  7.2565 +    
  7.2566 +    have "isCont f' 0" unfolding f'_def
  7.2567 +    proof (intro isCont_powser_converges_everywhere) 
  7.2568 +      fix x :: real show "summable (\<lambda>n. -sin_coeff (n+3) * pi^(n+2) * x^n)"
  7.2569 +      proof (cases "x = 0")
  7.2570 +        assume x: "x \<noteq> 0"
  7.2571 +        from summable_divide[OF sums_summable[OF sums_split_initial_segment[OF 
  7.2572 +               sin_converges[of "pi*x"]], of 3], of "-pi*x^3"] x
  7.2573 +          show ?thesis by (simp add: mult_ac power_mult_distrib divide_simps eval_nat_numeral)
  7.2574 +      qed (simp only: summable_0_powser)
  7.2575 +    qed
  7.2576 +    hence "f' \<midarrow> 0 \<rightarrow> f' 0" by (simp add: isCont_def)
  7.2577 +    also have "f' 0 = pi * pi / fact 3" unfolding f'_def 
  7.2578 +      by (subst powser_zero) (simp add: sin_coeff_def)
  7.2579 +    finally show "f' \<midarrow> 0 \<rightarrow> pi^2 / 6" by (simp add: eval_nat_numeral)
  7.2580 +  qed
  7.2581 +  
  7.2582 +  ultimately have "K = pi^2 / 6" by (rule LIM_unique)
  7.2583 +  moreover from inverse_power_summable[of 2]
  7.2584 +    have "summable (\<lambda>n. (inverse (real_of_nat (Suc n)))\<^sup>2)"
  7.2585 +    by (subst summable_Suc_iff) (simp add: power_inverse)
  7.2586 +  ultimately show ?thesis unfolding K_def 
  7.2587 +    by (auto simp add: sums_iff power_divide inverse_eq_divide)
  7.2588 +qed
  7.2589 +
  7.2590 +
  7.2591 +
  7.2592 +subsection \<open>Binomial coefficient form\<close>
  7.2593 +
  7.2594 +lemma Gamma_binomial:
  7.2595 +  "(\<lambda>n. ((z + of_nat n) gchoose n) * exp (-z * of_real (ln (of_nat n)))) \<longlonglongrightarrow> rGamma (z+1)"
  7.2596 +proof (cases "z = 0")
  7.2597 +  case False
  7.2598 +  show ?thesis
  7.2599 +  proof (rule Lim_transform_eventually)
  7.2600 +    let ?powr = "\<lambda>a b. exp (b * of_real (ln (of_nat a)))"
  7.2601 +    show "eventually (\<lambda>n. rGamma_series z n / z = 
  7.2602 +            ((z + of_nat n) gchoose n) * ?powr n (-z)) sequentially"
  7.2603 +    proof (intro always_eventually allI)
  7.2604 +      fix n :: nat
  7.2605 +      from False have "((z + of_nat n) gchoose n) = pochhammer z (Suc n) / z / fact n"
  7.2606 +        by (simp add: gbinomial_pochhammer' pochhammer_rec)
  7.2607 +      also have "pochhammer z (Suc n) / z / fact n * ?powr n (-z) = rGamma_series z n / z"
  7.2608 +        by (simp add: rGamma_series_def divide_simps exp_minus)
  7.2609 +      finally show "rGamma_series z n / z = ((z + of_nat n) gchoose n) * ?powr n (-z)" ..
  7.2610 +    qed
  7.2611 +  
  7.2612 +    from False have "(\<lambda>n. rGamma_series z n / z) \<longlonglongrightarrow> rGamma z / z" by (intro tendsto_intros)
  7.2613 +    also from False have "rGamma z / z = rGamma (z + 1)" using rGamma_plus1[of z] 
  7.2614 +      by (simp add: field_simps)
  7.2615 +    finally show "(\<lambda>n. rGamma_series z n / z) \<longlonglongrightarrow> rGamma (z+1)" .
  7.2616 +  qed
  7.2617 +qed (simp_all add: binomial_gbinomial [symmetric])
  7.2618 +
  7.2619 +lemma fact_binomial_limit: 
  7.2620 +  "(\<lambda>n. of_nat ((k + n) choose n) / of_nat (n ^ k) :: 'a :: Gamma) \<longlonglongrightarrow> 1 / fact k"
  7.2621 +proof (rule Lim_transform_eventually)
  7.2622 +  have "(\<lambda>n. of_nat ((k + n) choose n) / of_real (exp (of_nat k * ln (real_of_nat n))))
  7.2623 +            \<longlonglongrightarrow> 1 / Gamma (of_nat (Suc k) :: 'a)" (is "?f \<longlonglongrightarrow> _")
  7.2624 +    using Gamma_binomial[of "of_nat k :: 'a"] 
  7.2625 +    by (simp add: binomial_gbinomial add_ac Gamma_def divide_simps exp_of_real [symmetric] exp_minus)
  7.2626 +  also have "Gamma (of_nat (Suc k)) = fact k" by (rule Gamma_fact)
  7.2627 +  finally show "?f \<longlonglongrightarrow> 1 / fact k" .
  7.2628 +
  7.2629 +  show "eventually (\<lambda>n. ?f n = of_nat ((k + n) choose n) / of_nat (n ^ k)) sequentially"
  7.2630 +    using eventually_gt_at_top[of "0::nat"]
  7.2631 +  proof eventually_elim
  7.2632 +    fix n :: nat assume n: "n > 0"
  7.2633 +    from n have "exp (real_of_nat k * ln (real_of_nat n)) = real_of_nat (n^k)"
  7.2634 +      by (simp add: exp_of_nat_mult)
  7.2635 +    thus "?f n = of_nat ((k + n) choose n) / of_nat (n ^ k)" by simp
  7.2636 +  qed
  7.2637 +qed
  7.2638 +
  7.2639 +lemma binomial_asymptotic: 
  7.2640 +  "(\<lambda>n. of_nat ((k + n) choose n) / (of_nat (n ^ k) / fact k) :: 'a :: Gamma) \<longlonglongrightarrow> 1"
  7.2641 +  using tendsto_mult[OF fact_binomial_limit[of k] tendsto_const[of "fact k :: 'a"]] by simp
  7.2642 +
  7.2643 +end
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/HOL/Multivariate_Analysis/Generalised_Binomial_Theorem.thy	Mon Jan 04 17:45:36 2016 +0100
     8.3 @@ -0,0 +1,255 @@
     8.4 +(*
     8.5 +  Title:    HOL/Multivariate_Analysis/Generalised_Binomial_Theorem.thy
     8.6 +  Author:   Manuel Eberl, TU München
     8.7 +  
     8.8 +  The proof of the Generalised Binomial Theorem and related results.
     8.9 +*)
    8.10 +theory Generalised_Binomial_Theorem
    8.11 +imports 
    8.12 +  Complex_Main 
    8.13 +  Complex_Transcendental
    8.14 +  Summation
    8.15 +begin
    8.16 +
    8.17 +subsection \<open>The generalised binomial theorem\<close>
    8.18 +
    8.19 +text \<open>
    8.20 +  We prove the generalised binomial theorem for complex numbers, following the proof at:
    8.21 +  https://proofwiki.org/wiki/Binomial_Theorem/General_Binomial_Theorem
    8.22 +\<close>
    8.23 +
    8.24 +lemma gbinomial_ratio_limit:
    8.25 +  fixes a :: "'a :: real_normed_field"
    8.26 +  assumes "a \<notin> \<nat>"
    8.27 +  shows "(\<lambda>n. (a gchoose n) / (a gchoose Suc n)) \<longlonglongrightarrow> -1"
    8.28 +proof (rule Lim_transform_eventually)
    8.29 +  let ?f = "\<lambda>n. inverse (a / of_nat (Suc n) - of_nat n / of_nat (Suc n))"
    8.30 +  from eventually_gt_at_top[of "0::nat"]
    8.31 +    show "eventually (\<lambda>n. ?f n = (a gchoose n) /(a gchoose Suc n)) sequentially"
    8.32 +  proof eventually_elim
    8.33 +    fix n :: nat assume n: "n > 0"
    8.34 +    let ?P = "\<Prod>i = 0..n - 1. a - of_nat i"
    8.35 +    from n have "(a gchoose n) / (a gchoose Suc n) = (of_nat (Suc n) :: 'a) *
    8.36 +                   (?P / (\<Prod>i = 0..n. a - of_nat i))" by (simp add: gbinomial_def)
    8.37 +    also from n have "(\<Prod>i = 0..n. a - of_nat i) = ?P * (a - of_nat n)"
    8.38 +      by (cases n) (simp_all add: setprod_nat_ivl_Suc)
    8.39 +    also have "?P / \<dots> = (?P / ?P) / (a - of_nat n)" by (rule divide_divide_eq_left[symmetric])
    8.40 +    also from assms have "?P / ?P = 1" by auto
    8.41 +    also have "of_nat (Suc n) * (1 / (a - of_nat n)) = 
    8.42 +                   inverse (inverse (of_nat (Suc n)) * (a - of_nat n))" by (simp add: field_simps)
    8.43 +    also have "inverse (of_nat (Suc n)) * (a - of_nat n) = a / of_nat (Suc n) - of_nat n / of_nat (Suc n)"
    8.44 +      by (simp add: field_simps del: of_nat_Suc)
    8.45 +    finally show "?f n = (a gchoose n) / (a gchoose Suc n)" by simp
    8.46 +  qed
    8.47 +
    8.48 +  have "(\<lambda>n. norm a / (of_nat (Suc n))) \<longlonglongrightarrow> 0" 
    8.49 +    unfolding divide_inverse
    8.50 +    by (intro tendsto_mult_right_zero LIMSEQ_inverse_real_of_nat)
    8.51 +  hence "(\<lambda>n. a / of_nat (Suc n)) \<longlonglongrightarrow> 0"
    8.52 +    by (subst tendsto_norm_zero_iff[symmetric]) (simp add: norm_divide del: of_nat_Suc)
    8.53 +  hence "?f \<longlonglongrightarrow> inverse (0 - 1)"
    8.54 +    by (intro tendsto_inverse tendsto_diff LIMSEQ_n_over_Suc_n) simp_all
    8.55 +  thus "?f \<longlonglongrightarrow> -1" by simp
    8.56 +qed
    8.57 +
    8.58 +lemma conv_radius_gchoose:
    8.59 +  fixes a :: "'a :: {real_normed_field,banach}"
    8.60 +  shows "conv_radius (\<lambda>n. a gchoose n) = (if a \<in> \<nat> then \<infinity> else 1)"
    8.61 +proof (cases "a \<in> \<nat>")
    8.62 +  assume a: "a \<in> \<nat>"
    8.63 +  have "eventually (\<lambda>n. (a gchoose n) = 0) sequentially"
    8.64 +    using eventually_gt_at_top[of "nat \<lfloor>norm a\<rfloor>"]
    8.65 +    by eventually_elim (insert a, auto elim!: Nats_cases simp: binomial_gbinomial[symmetric])
    8.66 +  from conv_radius_cong[OF this] a show ?thesis by simp
    8.67 +next
    8.68 +  assume a: "a \<notin> \<nat>"
    8.69 +  from tendsto_norm[OF gbinomial_ratio_limit[OF this]]
    8.70 +    have "conv_radius (\<lambda>n. a gchoose n) = 1"
    8.71 +    by (intro conv_radius_ratio_limit_nonzero[of _ 1]) (simp_all add: norm_divide)
    8.72 +  with a show ?thesis by simp
    8.73 +qed
    8.74 +
    8.75 +lemma gen_binomial_complex:
    8.76 +  fixes z :: complex
    8.77 +  assumes "norm z < 1"
    8.78 +  shows   "(\<lambda>n. (a gchoose n) * z^n) sums (1 + z) powr a"
    8.79 +proof -
    8.80 +  def K \<equiv> "1 - (1 - norm z) / 2"
    8.81 +  from assms have K: "K > 0" "K < 1" "norm z < K"
    8.82 +     unfolding K_def by (auto simp: field_simps intro!: add_pos_nonneg)
    8.83 +  let ?f = "\<lambda>n. a gchoose n" and ?f' = "diffs (\<lambda>n. a gchoose n)"
    8.84 +  have summable_strong: "summable (\<lambda>n. ?f n * z ^ n)" if "norm z < 1" for z using that
    8.85 +    by (intro summable_in_conv_radius) (simp_all add: conv_radius_gchoose)
    8.86 +  with K have summable: "summable (\<lambda>n. ?f n * z ^ n)" if "norm z < K" for z using that by auto
    8.87 +  hence summable': "summable (\<lambda>n. ?f' n * z ^ n)" if "norm z < K" for z using that
    8.88 +    by (intro termdiff_converges[of _ K]) simp_all
    8.89 +  
    8.90 +  def f \<equiv> "\<lambda>z. \<Sum>n. ?f n * z ^ n" and f' \<equiv> "\<lambda>z. \<Sum>n. ?f' n * z ^ n"
    8.91 +  {
    8.92 +    fix z :: complex assume z: "norm z < K"
    8.93 +    from summable_mult2[OF summable'[OF z], of z]
    8.94 +      have summable1: "summable (\<lambda>n. ?f' n * z ^ Suc n)" by (simp add: mult_ac)
    8.95 +    hence summable2: "summable (\<lambda>n. of_nat n * ?f n * z^n)" 
    8.96 +      unfolding diffs_def by (subst (asm) summable_Suc_iff)
    8.97 +
    8.98 +    have "(1 + z) * f' z = (\<Sum>n. ?f' n * z^n) + (\<Sum>n. ?f' n * z^Suc n)"
    8.99 +      unfolding f'_def using summable' z by (simp add: algebra_simps suminf_mult)
   8.100 +    also have "(\<Sum>n. ?f' n * z^n) = (\<Sum>n. of_nat (Suc n) * ?f (Suc n) * z^n)"
   8.101 +      by (intro suminf_cong) (simp add: diffs_def)
   8.102 +    also have "(\<Sum>n. ?f' n * z^Suc n) = (\<Sum>n. of_nat n * ?f n * z ^ n)" 
   8.103 +      using summable1 suminf_split_initial_segment[OF summable1] unfolding diffs_def
   8.104 +      by (subst suminf_split_head, subst (asm) summable_Suc_iff) simp_all
   8.105 +    also have "(\<Sum>n. of_nat (Suc n) * ?f (Suc n) * z^n) + (\<Sum>n. of_nat n * ?f n * z^n) =
   8.106 +                 (\<Sum>n. a * ?f n * z^n)"
   8.107 +      by (subst gbinomial_mult_1, subst suminf_add)
   8.108 +         (insert summable'[OF z] summable2, 
   8.109 +          simp_all add: summable_powser_split_head algebra_simps diffs_def)
   8.110 +    also have "\<dots> = a * f z" unfolding f_def
   8.111 +      by (subst suminf_mult[symmetric]) (simp_all add: summable[OF z] mult_ac)
   8.112 +    finally have "a * f z = (1 + z) * f' z" by simp
   8.113 +  } note deriv = this
   8.114 +
   8.115 +  have [derivative_intros]: "(f has_field_derivative f' z) (at z)" if "norm z < of_real K" for z
   8.116 +    unfolding f_def f'_def using K that
   8.117 +    by (intro termdiffs_strong[of "?f" K z] summable_strong) simp_all
   8.118 +  have "f 0 = (\<Sum>n. if n = 0 then 1 else 0)" unfolding f_def by (intro suminf_cong) simp
   8.119 +  also have "\<dots> = 1" using sums_single[of 0 "\<lambda>_. 1::complex"] unfolding sums_iff by simp
   8.120 +  finally have [simp]: "f 0 = 1" .
   8.121 +
   8.122 +  have "\<exists>c. \<forall>z\<in>ball 0 K. f z * (1 + z) powr (-a) = c"
   8.123 +  proof (rule has_field_derivative_zero_constant)
   8.124 +    fix z :: complex assume z': "z \<in> ball 0 K"
   8.125 +    hence z: "norm z < K" by (simp add: dist_0_norm)
   8.126 +    with K have nz: "1 + z \<noteq> 0" by (auto dest!: minus_unique)
   8.127 +    from z K have "norm z < 1" by simp
   8.128 +    hence "Im (1 + z) \<noteq> 0 \<or> Re (1 + z) > 0" by (cases z) auto
   8.129 +    hence "((\<lambda>z. f z * (1 + z) powr (-a)) has_field_derivative 
   8.130 +              f' z * (1 + z) powr (-a) - a * f z * (1 + z) powr (-a-1)) (at z)" using z
   8.131 +      by (auto intro!: derivative_eq_intros)
   8.132 +    also from z have "a * f z = (1 + z) * f' z" by (rule deriv)
   8.133 +    finally show "((\<lambda>z. f z * (1 + z) powr (-a)) has_field_derivative 0) (at z within ball 0 K)" 
   8.134 +      using nz by (simp add: field_simps powr_diff_complex at_within_open[OF z'])
   8.135 +  qed simp_all
   8.136 +  then obtain c where c: "\<And>z. z \<in> ball 0 K \<Longrightarrow> f z * (1 + z) powr (-a) = c" by blast
   8.137 +  from c[of 0] and K have "c = 1" by simp
   8.138 +  with c[of z] have "f z = (1 + z) powr a" using K 
   8.139 +    by (simp add: powr_minus_complex field_simps dist_complex_def)
   8.140 +  with summable K show ?thesis unfolding f_def by (simp add: sums_iff)
   8.141 +qed
   8.142 +
   8.143 +lemma gen_binomial_complex':
   8.144 +  fixes x y :: real and a :: complex
   8.145 +  assumes "\<bar>x\<bar> < \<bar>y\<bar>"
   8.146 +  shows   "(\<lambda>n. (a gchoose n) * of_real x^n * of_real y powr (a - of_nat n)) sums 
   8.147 +               of_real (x + y) powr a" (is "?P x y")
   8.148 +proof -
   8.149 +  {
   8.150 +    fix x y :: real assume xy: "\<bar>x\<bar> < \<bar>y\<bar>" "y \<ge> 0"
   8.151 +    hence "y > 0" by simp
   8.152 +    note xy = xy this
   8.153 +    from xy have "(\<lambda>n. (a gchoose n) * of_real (x / y) ^ n) sums (1 + of_real (x / y)) powr a"
   8.154 +        by (intro gen_binomial_complex) (simp add: norm_divide)
   8.155 +    hence "(\<lambda>n. (a gchoose n) * of_real (x / y) ^ n * y powr a) sums 
   8.156 +               ((1 + of_real (x / y)) powr a * y powr a)"
   8.157 +      by (rule sums_mult2)
   8.158 +    also have "(1 + complex_of_real (x / y)) = complex_of_real (1 + x/y)" by simp
   8.159 +    also from xy have "\<dots> powr a * of_real y powr a = (\<dots> * y) powr a"
   8.160 +      by (subst powr_times_real[symmetric]) (simp_all add: field_simps)
   8.161 +    also from xy have "complex_of_real (1 + x / y) * complex_of_real y = of_real (x + y)"
   8.162 +      by (simp add: field_simps)
   8.163 +    finally have "?P x y" using xy by (simp add: field_simps powr_diff_complex powr_nat)
   8.164 +  } note A = this
   8.165 +
   8.166 +  show ?thesis
   8.167 +  proof (cases "y < 0")
   8.168 +    assume y: "y < 0"
   8.169 +    with assms have xy: "x + y < 0" by simp
   8.170 +    with assms have "\<bar>-x\<bar> < \<bar>-y\<bar>" "-y \<ge> 0" by simp_all
   8.171 +    note A[OF this]
   8.172 +    also have "complex_of_real (-x + -y) = - complex_of_real (x + y)" by simp
   8.173 +    also from xy assms have "... powr a = (-1) powr -a * of_real (x + y) powr a"
   8.174 +      by (subst powr_neg_real_complex) (simp add: abs_real_def split: split_if_asm)
   8.175 +    also {
   8.176 +      fix n :: nat
   8.177 +      from y have "(a gchoose n) * of_real (-x) ^ n * of_real (-y) powr (a - of_nat n) = 
   8.178 +                       (a gchoose n) * (-of_real x / -of_real y) ^ n * (- of_real y) powr a"
   8.179 +        by (subst power_divide) (simp add: powr_diff_complex powr_nat)
   8.180 +      also from y have "(- of_real y) powr a = (-1) powr -a * of_real y powr a"
   8.181 +        by (subst powr_neg_real_complex) simp
   8.182 +      also have "-complex_of_real x / -complex_of_real y = complex_of_real x / complex_of_real y"
   8.183 +        by simp
   8.184 +      also have "... ^ n = of_real x ^ n / of_real y ^ n" by (simp add: power_divide)
   8.185 +      also have "(a gchoose n) * ... * ((-1) powr -a * of_real y powr a) = 
   8.186 +                   (-1) powr -a * ((a gchoose n) * of_real x ^ n * of_real y powr (a - n))"
   8.187 +        by (simp add: algebra_simps powr_diff_complex powr_nat)
   8.188 +      finally have "(a gchoose n) * of_real (- x) ^ n * of_real (- y) powr (a - of_nat n) =
   8.189 +                      (-1) powr -a * ((a gchoose n) * of_real x ^ n * of_real y powr (a - of_nat n))" .
   8.190 +    }
   8.191 +    note sums_cong[OF this]
   8.192 +    finally show ?thesis by (simp add: sums_mult_iff)
   8.193 +  qed (insert A[of x y] assms, simp_all add: not_less)
   8.194 +qed
   8.195 +
   8.196 +lemma gen_binomial_complex'':
   8.197 +  fixes x y :: real and a :: complex
   8.198 +  assumes "\<bar>y\<bar> < \<bar>x\<bar>"
   8.199 +  shows   "(\<lambda>n. (a gchoose n) * of_real x powr (a - of_nat n) * of_real y ^ n) sums 
   8.200 +               of_real (x + y) powr a"
   8.201 +  using gen_binomial_complex'[OF assms] by (simp add: mult_ac add.commute)
   8.202 +
   8.203 +lemma gen_binomial_real:
   8.204 +  fixes z :: real
   8.205 +  assumes "\<bar>z\<bar> < 1"
   8.206 +  shows   "(\<lambda>n. (a gchoose n) * z^n) sums (1 + z) powr a"
   8.207 +proof -
   8.208 +  from assms have "norm (of_real z :: complex) < 1" by simp
   8.209 +  from gen_binomial_complex[OF this]
   8.210 +    have "(\<lambda>n. (of_real a gchoose n :: complex) * of_real z ^ n) sums
   8.211 +              (of_real (1 + z)) powr (of_real a)" by simp
   8.212 +  also have "(of_real (1 + z) :: complex) powr (of_real a) = of_real ((1 + z) powr a)"
   8.213 +    using assms by (subst powr_of_real) simp_all
   8.214 +  also have "(of_real a gchoose n :: complex) = of_real (a gchoose n)" for n 
   8.215 +    by (simp add: gbinomial_def)
   8.216 +  hence "(\<lambda>n. (of_real a gchoose n :: complex) * of_real z ^ n) =
   8.217 +           (\<lambda>n. of_real ((a gchoose n) * z ^ n))" by (intro ext) simp
   8.218 +  finally show ?thesis by (simp only: sums_of_real_iff)
   8.219 +qed 
   8.220 +
   8.221 +lemma gen_binomial_real':
   8.222 +  fixes x y a :: real
   8.223 +  assumes "\<bar>x\<bar> < y"
   8.224 +  shows   "(\<lambda>n. (a gchoose n) * x^n * y powr (a - of_nat n)) sums (x + y) powr a"
   8.225 +proof -
   8.226 +  from assms have "y > 0" by simp
   8.227 +  note xy = this assms
   8.228 +  from assms have "\<bar>x / y\<bar> < 1" by simp
   8.229 +  hence "(\<lambda>n. (a gchoose n) * (x / y) ^ n) sums (1 + x / y) powr a"
   8.230 +    by (rule gen_binomial_real)
   8.231 +  hence "(\<lambda>n. (a gchoose n) * (x / y) ^ n * y powr a) sums ((1 + x / y) powr a * y powr a)"
   8.232 +    by (rule sums_mult2)
   8.233 +  with xy show ?thesis 
   8.234 +    by (simp add: field_simps powr_divide powr_divide2[symmetric] powr_realpow)
   8.235 +qed
   8.236 +
   8.237 +lemma one_plus_neg_powr_powser:
   8.238 +  fixes z s :: complex
   8.239 +  assumes "norm (z :: complex) < 1"
   8.240 +  shows "(\<lambda>n. (-1)^n * ((s + n - 1) gchoose n) * z^n) sums (1 + z) powr (-s)"
   8.241 +    using gen_binomial_complex[OF assms, of "-s"] by (simp add: gbinomial_minus)
   8.242 +
   8.243 +lemma gen_binomial_real'':
   8.244 +  fixes x y a :: real
   8.245 +  assumes "\<bar>y\<bar> < x"
   8.246 +  shows   "(\<lambda>n. (a gchoose n) * x powr (a - of_nat n) * y^n) sums (x + y) powr a"
   8.247 +  using gen_binomial_real'[OF assms] by (simp add: mult_ac add.commute)
   8.248 +
   8.249 +lemma sqrt_series':
   8.250 +  "\<bar>z\<bar> < a \<Longrightarrow> (\<lambda>n. ((1/2) gchoose n) * a powr (1/2 - real_of_nat n) * z ^ n) sums 
   8.251 +                  sqrt (a + z :: real)"
   8.252 +  using gen_binomial_real''[of z a "1/2"] by (simp add: powr_half_sqrt)
   8.253 +
   8.254 +lemma sqrt_series:
   8.255 +  "\<bar>z\<bar> < 1 \<Longrightarrow> (\<lambda>n. ((1/2) gchoose n) * z ^ n) sums sqrt (1 + z)"
   8.256 +  using gen_binomial_real[of z "1/2"] by (simp add: powr_half_sqrt)
   8.257 +
   8.258 +end
   8.259 \ No newline at end of file
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/HOL/Multivariate_Analysis/Harmonic_Numbers.thy	Mon Jan 04 17:45:36 2016 +0100
     9.3 @@ -0,0 +1,573 @@
     9.4 +(*
     9.5 +  Title:    HOL/Multivariate_Analysis/Harmonic_Numbers.thy
     9.6 +  Author:   Manuel Eberl, TU München
     9.7 +  
     9.8 +  The definition of the Harmonic Numbers and the Euler–Mascheroni constant.
     9.9 +  Also provides a reasonably accurate approximation of @{term "ln 2 :: real"} 
    9.10 +  and the Euler–Mascheroni constant.
    9.11 +*)
    9.12 +theory Harmonic_Numbers
    9.13 +imports 
    9.14 +  Complex_Transcendental
    9.15 +  Summation
    9.16 +  Integral_Test
    9.17 +begin
    9.18 +
    9.19 +lemma ln_2_less_1: "ln 2 < (1::real)"
    9.20 +proof -
    9.21 +  have "2 < 5/(2::real)" by simp
    9.22 +  also have "5/2 \<le> exp (1::real)" using exp_lower_taylor_quadratic[of 1, simplified] by simp
    9.23 +  finally have "exp (ln 2) < exp (1::real)" by simp
    9.24 +  thus "ln 2 < (1::real)" by (subst (asm) exp_less_cancel_iff) simp
    9.25 +qed
    9.26 +
    9.27 +lemma setsum_Suc_diff':
    9.28 +  fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
    9.29 +  assumes "m \<le> n"
    9.30 +  shows "(\<Sum>i = m..<n. f(Suc i) - f i) = f n - f m"
    9.31 +using assms by (induct n) (auto simp: le_Suc_eq)
    9.32 +
    9.33 +lemma eval_fact:
    9.34 +  "fact 0 = 1"
    9.35 +  "fact (Suc 0) = 1"
    9.36 +  "fact (numeral n) = numeral n * fact (pred_numeral n)"
    9.37 +  by (simp, simp, simp_all only: numeral_eq_Suc fact_Suc,
    9.38 +      simp only: numeral_eq_Suc [symmetric] of_nat_numeral)
    9.39 +
    9.40 +lemma setsum_poly_horner_expand:
    9.41 +  "(\<Sum>k<(numeral n::nat). f k * x^k) = f 0 + (\<Sum>k<pred_numeral n. f (k+1) * x^k) * x"
    9.42 +  "(\<Sum>k<Suc 0. f k * x^k) = (f 0 :: 'a :: semiring_1)"
    9.43 +  "(\<Sum>k<(0::nat). f k * x^k) = 0"
    9.44 +proof -
    9.45 +  {
    9.46 +    fix m :: nat
    9.47 +    have "(\<Sum>k<Suc m. f k * x^k) = f 0 + (\<Sum>k=Suc 0..<Suc m. f k * x^k)"
    9.48 +      by (subst atLeast0LessThan [symmetric], subst setsum_head_upt_Suc) simp_all
    9.49 +    also have "(\<Sum>k=Suc 0..<Suc m. f k * x^k) = (\<Sum>k<m. f (k+1) * x^k) * x"
    9.50 +      by (subst setsum_shift_bounds_Suc_ivl)
    9.51 +         (simp add: setsum_left_distrib algebra_simps atLeast0LessThan power_commutes)
    9.52 +    finally have "(\<Sum>k<Suc m. f k * x ^ k) = f 0 + (\<Sum>k<m. f (k + 1) * x ^ k) * x" .
    9.53 +  }
    9.54 +  from this[of "pred_numeral n"] 
    9.55 +    show "(\<Sum>k<numeral n. f k * x^k) = f 0 + (\<Sum>k<pred_numeral n. f (k+1) * x^k) * x" 
    9.56 +    by (simp add: numeral_eq_Suc)
    9.57 +qed simp_all
    9.58 +
    9.59 +
    9.60 +subsection \<open>The Harmonic numbers\<close>
    9.61 +
    9.62 +definition harm :: "nat \<Rightarrow> 'a :: real_normed_field" where
    9.63 +  "harm n = (\<Sum>k=1..n. inverse (of_nat k))"
    9.64 +
    9.65 +lemma harm_altdef: "harm n = (\<Sum>k<n. inverse (of_nat (Suc k)))"
    9.66 +  unfolding harm_def by (induction n) simp_all
    9.67 +
    9.68 +lemma harm_Suc: "harm (Suc n) = harm n + inverse (of_nat (Suc n))"
    9.69 +  by (simp add: harm_def)
    9.70 +
    9.71 +lemma harm_nonneg: "harm n \<ge> (0 :: 'a :: {real_normed_field,linordered_field})"
    9.72 +  unfolding harm_def by (intro setsum_nonneg) simp_all
    9.73 +
    9.74 +lemma of_real_harm: "of_real (harm n) = harm n"
    9.75 +  unfolding harm_def by simp
    9.76 +  
    9.77 +lemma norm_harm: "norm (harm n) = harm n"
    9.78 +  by (subst of_real_harm [symmetric]) (simp add: harm_nonneg)
    9.79 +
    9.80 +lemma harm_expand: 
    9.81 +  "harm (Suc 0) = 1"
    9.82 +  "harm (numeral n) = harm (pred_numeral n) + inverse (numeral n)"
    9.83 +proof -
    9.84 +  have "numeral n = Suc (pred_numeral n)" by simp
    9.85 +  also have "harm \<dots> = harm (pred_numeral n) + inverse (numeral n)"
    9.86 +    by (subst harm_Suc, subst numeral_eq_Suc[symmetric]) simp
    9.87 +  finally show "harm (numeral n) = harm (pred_numeral n) + inverse (numeral n)" .
    9.88 +qed (simp add: harm_def)
    9.89 +
    9.90 +lemma not_convergent_harm: "\<not>convergent (harm :: nat \<Rightarrow> 'a :: real_normed_field)"
    9.91 +proof -
    9.92 +  have "convergent (\<lambda>n. norm (harm n :: 'a)) \<longleftrightarrow>
    9.93 +            convergent (harm :: nat \<Rightarrow> real)" by (simp add: norm_harm)
    9.94 +  also have "\<dots> \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k=Suc 0..Suc n. inverse (of_nat k) :: real)"
    9.95 +    unfolding harm_def[abs_def] by (subst convergent_Suc_iff) simp_all
    9.96 +  also have "... \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k\<le>n. inverse (of_nat (Suc k)) :: real)"
    9.97 +    by (subst setsum_shift_bounds_cl_Suc_ivl) (simp add: atLeast0AtMost)
    9.98 +  also have "... \<longleftrightarrow> summable (\<lambda>n. inverse (of_nat n) :: real)"
    9.99 +    by (subst summable_Suc_iff [symmetric]) (simp add: summable_iff_convergent')
   9.100 +  also have "\<not>..." by (rule not_summable_harmonic)
   9.101 +  finally show ?thesis by (blast dest: convergent_norm)
   9.102 +qed
   9.103 +
   9.104 +
   9.105 +subsection \<open>The Euler–Mascheroni constant\<close>
   9.106 +
   9.107 +text \<open>
   9.108 +  The limit of the difference between the partial harmonic sum and the natural logarithm
   9.109 +  (approximately 0.577216). This value occurs e.g. in the definition of the Gamma function.
   9.110 + \<close>
   9.111 +definition euler_mascheroni :: "'a :: real_normed_algebra_1" where
   9.112 +  "euler_mascheroni = of_real (lim (\<lambda>n. harm n - ln (of_nat n)))"
   9.113 +
   9.114 +lemma of_real_euler_mascheroni [simp]: "of_real euler_mascheroni = euler_mascheroni"
   9.115 +  by (simp add: euler_mascheroni_def)
   9.116 +
   9.117 +interpretation euler_mascheroni: antimono_fun_sum_integral_diff "\<lambda>x. inverse (x + 1)"
   9.118 +  by unfold_locales (auto intro!: continuous_intros)
   9.119 +
   9.120 +lemma euler_mascheroni_sum_integral_diff_series:
   9.121 +  "euler_mascheroni.sum_integral_diff_series n = harm (Suc n) - ln (of_nat (Suc n))"
   9.122 +proof -
   9.123 +  have "harm (Suc n) = (\<Sum>k=0..n. inverse (of_nat k + 1) :: real)" unfolding harm_def
   9.124 +    unfolding One_nat_def by (subst setsum_shift_bounds_cl_Suc_ivl) (simp add: add_ac)
   9.125 +  moreover have "((\<lambda>x. inverse (x + 1) :: real) has_integral ln (of_nat n + 1) - ln (0 + 1))
   9.126 +                   {0..of_nat n}"
   9.127 +    by (intro fundamental_theorem_of_calculus)
   9.128 +       (auto intro!: derivative_eq_intros simp: divide_inverse
   9.129 +           has_field_derivative_iff_has_vector_derivative[symmetric])
   9.130 +  hence "integral {0..of_nat n} (\<lambda>x. inverse (x + 1) :: real) = ln (of_nat (Suc n))"
   9.131 +    by (auto dest!: integral_unique)
   9.132 +  ultimately show ?thesis 
   9.133 +    by (simp add: euler_mascheroni.sum_integral_diff_series_def atLeast0AtMost)
   9.134 +qed
   9.135 +
   9.136 +lemma euler_mascheroni_sequence_decreasing:
   9.137 +  "m > 0 \<Longrightarrow> m \<le> n \<Longrightarrow> harm n - ln (of_nat n) \<le> harm m - ln (of_nat m :: real)"
   9.138 +  by (cases m, simp, cases n, simp, hypsubst,
   9.139 +      subst (1 2) euler_mascheroni_sum_integral_diff_series [symmetric],
   9.140 +      rule euler_mascheroni.sum_integral_diff_series_antimono, simp)
   9.141 +
   9.142 +lemma euler_mascheroni_sequence_nonneg:
   9.143 +  "n > 0 \<Longrightarrow> harm n - ln (of_nat n) \<ge> (0::real)"
   9.144 +  by (cases n, simp, hypsubst, subst euler_mascheroni_sum_integral_diff_series [symmetric],
   9.145 +      rule euler_mascheroni.sum_integral_diff_series_nonneg)
   9.146 +
   9.147 +lemma euler_mascheroni_convergent: "convergent (\<lambda>n. harm n - ln (of_nat n) :: real)"
   9.148 +proof -
   9.149 +  have A: "(\<lambda>n. harm (Suc n) - ln (of_nat (Suc n))) = 
   9.150 +             euler_mascheroni.sum_integral_diff_series"
   9.151 +    by (subst euler_mascheroni_sum_integral_diff_series [symmetric]) (rule refl)
   9.152 +  have "convergent (\<lambda>n. harm (Suc n) - ln (of_nat (Suc n) :: real))"
   9.153 +    by (subst A) (fact euler_mascheroni.sum_integral_diff_series_convergent)
   9.154 +  thus ?thesis by (subst (asm) convergent_Suc_iff)
   9.155 +qed
   9.156 +
   9.157 +lemma euler_mascheroni_LIMSEQ: 
   9.158 +  "(\<lambda>n. harm n - ln (of_nat n) :: real) \<longlonglongrightarrow> euler_mascheroni"
   9.159 +  unfolding euler_mascheroni_def
   9.160 +  by (simp add: convergent_LIMSEQ_iff [symmetric] euler_mascheroni_convergent)
   9.161 +
   9.162 +lemma euler_mascheroni_LIMSEQ_of_real: 
   9.163 +  "(\<lambda>n. of_real (harm n - ln (of_nat n))) \<longlonglongrightarrow> 
   9.164 +      (euler_mascheroni :: 'a :: {real_normed_algebra_1, topological_space})"
   9.165 +proof -
   9.166 +  have "(\<lambda>n. of_real (harm n - ln (of_nat n))) \<longlonglongrightarrow> (of_real (euler_mascheroni) :: 'a)"
   9.167 +    by (intro tendsto_of_real euler_mascheroni_LIMSEQ)
   9.168 +  thus ?thesis by simp
   9.169 +qed
   9.170 +
   9.171 +lemma euler_mascheroni_sum:
   9.172 +  "(\<lambda>n. inverse (of_nat (n+1)) + ln (of_nat (n+1)) - ln (of_nat (n+2)) :: real)
   9.173 +       sums euler_mascheroni" 
   9.174 + using sums_add[OF telescope_sums[OF LIMSEQ_Suc[OF euler_mascheroni_LIMSEQ]]
   9.175 +                   telescope_sums'[OF LIMSEQ_inverse_real_of_nat]]
   9.176 +  by (simp_all add: harm_def algebra_simps)
   9.177 +
   9.178 +lemma alternating_harmonic_series_sums: "(\<lambda>k. (-1)^k / real_of_nat (Suc k)) sums ln 2"
   9.179 +proof -
   9.180 +  let ?f = "\<lambda>n. harm n - ln (real_of_nat n)"
   9.181 +  let ?g = "\<lambda>n. if even n then 0 else (2::real)"
   9.182 +  let ?em = "\<lambda>n. harm n - ln (real_of_nat n)"
   9.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"
   9.184 +    using eventually_gt_at_top[of "0::nat"]
   9.185 +  proof eventually_elim
   9.186 +    fix n :: nat assume n: "n > 0"
   9.187 +    have "(\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k)) =
   9.188 +              (\<Sum>k<2*n. ((-1)^k + ?g k) / of_nat (Suc k)) - (\<Sum>k<2*n. ?g k / of_nat (Suc k))"
   9.189 +      by (simp add: setsum.distrib algebra_simps divide_inverse)
   9.190 +    also have "(\<Sum>k<2*n. ((-1)^k + ?g k) / real_of_nat (Suc k)) = harm (2*n)"
   9.191 +      unfolding harm_altdef by (intro setsum.cong) (auto simp: field_simps)
   9.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))"
   9.193 +      by (intro setsum.mono_neutral_right) auto
   9.194 +    also have "\<dots> = (\<Sum>k|k<2*n \<and> odd k. 2 / (real_of_nat (Suc k)))"
   9.195 +      by (intro setsum.cong) auto
   9.196 +    also have "(\<Sum>k|k<2*n \<and> odd k. 2 / (real_of_nat (Suc k))) = harm n" 
   9.197 +      unfolding harm_altdef
   9.198 +      by (intro setsum.reindex_cong[of "\<lambda>n. 2*n+1"]) (auto simp: inj_on_def field_simps elim!: oddE)
   9.199 +    also have "harm (2*n) - harm n = ?em (2*n) - ?em n + ln 2" using n
   9.200 +      by (simp_all add: algebra_simps ln_mult)
   9.201 +    finally show "?em (2*n) - ?em n + ln 2 = (\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k))" ..
   9.202 +  qed
   9.203 +  moreover have "(\<lambda>n. ?em (2*n) - ?em n + ln (2::real)) 
   9.204 +                     \<longlonglongrightarrow> euler_mascheroni - euler_mascheroni + ln 2"
   9.205 +    by (intro tendsto_intros euler_mascheroni_LIMSEQ filterlim_compose[OF euler_mascheroni_LIMSEQ]
   9.206 +              filterlim_subseq) (auto simp: subseq_def)
   9.207 +  hence "(\<lambda>n. ?em (2*n) - ?em n + ln (2::real)) \<longlonglongrightarrow> ln 2" by simp
   9.208 +  ultimately have "(\<lambda>n. (\<Sum>k<2*n. (-1)^k / real_of_nat (Suc k))) \<longlonglongrightarrow> ln 2"
   9.209 +    by (rule Lim_transform_eventually)
   9.210 +  
   9.211 +  moreover have "summable (\<lambda>k. (-1)^k * inverse (real_of_nat (Suc k)))"
   9.212 +    using LIMSEQ_inverse_real_of_nat
   9.213 +    by (intro summable_Leibniz(1) decseq_imp_monoseq decseq_SucI) simp_all
   9.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))"
   9.215 +    by (simp add: summable_sums_iff divide_inverse sums_def)
   9.216 +  from filterlim_compose[OF this filterlim_subseq[of "op * (2::nat)"]]
   9.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))"
   9.218 +    by (simp add: subseq_def)
   9.219 +  ultimately have "(\<Sum>k. (- 1) ^ k / real_of_nat (Suc k)) = ln 2" by (intro LIMSEQ_unique)
   9.220 +  with A show ?thesis by (simp add: sums_def)
   9.221 +qed
   9.222 +
   9.223 +lemma alternating_harmonic_series_sums': 
   9.224 +  "(\<lambda>k. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2))) sums ln 2"
   9.225 +unfolding sums_def
   9.226 +proof (rule Lim_transform_eventually)
   9.227 +  show "(\<lambda>n. \<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) \<longlonglongrightarrow> ln 2"
   9.228 +    using alternating_harmonic_series_sums unfolding sums_def 
   9.229 +    by (rule filterlim_compose) (rule mult_nat_left_at_top, simp)
   9.230 +  show "eventually (\<lambda>n. (\<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) =
   9.231 +            (\<Sum>k<n. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2)))) sequentially"
   9.232 +  proof (intro always_eventually allI)
   9.233 +    fix n :: nat
   9.234 +    show "(\<Sum>k<2*n. (-1)^k / (real_of_nat (Suc k))) =
   9.235 +              (\<Sum>k<n. inverse (real_of_nat (2*k+1)) - inverse (real_of_nat (2*k+2)))"
   9.236 +      by (induction n) (simp_all add: inverse_eq_divide)
   9.237 +  qed
   9.238 +qed               
   9.239 +
   9.240 +
   9.241 +subsection \<open>Approximation of the Euler--Mascheroni constant\<close>
   9.242 +
   9.243 +(* FIXME: ugly *)
   9.244 +(* TODO: Move ? *)
   9.245 +lemma ln_inverse_approx_le:
   9.246 +  assumes "(x::real) > 0" "a > 0"
   9.247 +  shows   "ln (x + a) - ln x \<le> a * (inverse x + inverse (x + a))/2" (is "_ \<le> ?A")
   9.248 +proof -
   9.249 +  def f' \<equiv> "(inverse (x + a) - inverse x)/a"
   9.250 +  have f'_nonpos: "f' \<le> 0" using assms by (simp add: f'_def divide_simps)
   9.251 +  let ?f = "\<lambda>t. (t - x) * f' + inverse x"
   9.252 +  let ?F = "\<lambda>t. (t - x)^2 * f' / 2 + t * inverse x"
   9.253 +  have diff: "\<forall>t\<in>{x..x+a}. (?F has_vector_derivative ?f t) 
   9.254 +                               (at t within {x..x+a})" using assms
   9.255 +    by (auto intro!: derivative_eq_intros 
   9.256 +             simp: has_field_derivative_iff_has_vector_derivative[symmetric])
   9.257 +  from assms have "(?f has_integral (?F (x+a) - ?F x)) {x..x+a}"
   9.258 +    by (intro fundamental_theorem_of_calculus[OF _ diff])
   9.259 +       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] field_simps
   9.260 +             intro!: derivative_eq_intros)
   9.261 +  also have "?F (x+a) - ?F x = (a*2 + f'*a\<^sup>2*x) / (2*x)" using assms by (simp add: field_simps)
   9.262 +  also have "f'*a^2 = - (a^2) / (x*(x + a))" using assms 
   9.263 +    by (simp add: divide_simps f'_def power2_eq_square)
   9.264 +  also have "(a*2 + - a\<^sup>2/(x*(x+a))*x) / (2*x) = ?A" using assms
   9.265 +    by (simp add: divide_simps power2_eq_square) (simp add: algebra_simps)
   9.266 +  finally have int1: "((\<lambda>t. (t - x) * f' + inverse x) has_integral ?A) {x..x + a}" .
   9.267 +
   9.268 +  from assms have int2: "(inverse has_integral (ln (x + a) - ln x)) {x..x+a}"
   9.269 +    by (intro fundamental_theorem_of_calculus)
   9.270 +       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
   9.271 +             intro!: derivative_eq_intros)
   9.272 +  hence "ln (x + a) - ln x = integral {x..x+a} inverse" by (simp add: integral_unique)
   9.273 +  also have ineq: "\<forall>xa\<in>{x..x + a}. inverse xa \<le> (xa - x) * f' + inverse x"
   9.274 +  proof
   9.275 +    fix t assume t': "t \<in> {x..x+a}"
   9.276 +    with assms have t: "0 \<le> (t - x) / a" "(t - x) / a \<le> 1" by simp_all
   9.277 +    have "inverse t = inverse ((1 - (t - x) / a) *\<^sub>R x + ((t - x) / a) *\<^sub>R (x + a))" (is "_ = ?A")
   9.278 +      using assms t' by (simp add: field_simps)
   9.279 +    also from assms have "convex_on {x..x+a} inverse" by (intro convex_on_inverse) auto
   9.280 +    from convex_onD_Icc[OF this _ t] assms 
   9.281 +      have "?A \<le> (1 - (t - x) / a) * inverse x + (t - x) / a * inverse (x + a)" by simp
   9.282 +    also have "\<dots> = (t - x) * f' + inverse x" using assms
   9.283 +      by (simp add: f'_def divide_simps) (simp add: f'_def field_simps)
   9.284 +    finally show "inverse t \<le> (t - x) * f' + inverse x" .
   9.285 +  qed
   9.286 +  hence "integral {x..x+a} inverse \<le> integral {x..x+a} ?f" using f'_nonpos assms
   9.287 +    by (intro integral_le has_integral_integrable[OF int1] has_integral_integrable[OF int2] ineq)
   9.288 +  also have "\<dots> = ?A" using int1 by (rule integral_unique)
   9.289 +  finally show ?thesis .
   9.290 +qed
   9.291 +
   9.292 +lemma ln_inverse_approx_ge:
   9.293 +  assumes "(x::real) > 0" "x < y"
   9.294 +  shows   "ln y - ln x \<ge> 2 * (y - x) / (x + y)" (is "_ \<ge> ?A")
   9.295 +proof -
   9.296 +  def m \<equiv> "(x+y)/2"
   9.297 +  def f' \<equiv> "-inverse (m^2)"
   9.298 +  from assms have m: "m > 0" by (simp add: m_def)
   9.299 +  let ?F = "\<lambda>t. (t - m)^2 * f' / 2 + t / m"
   9.300 +  from assms have "((\<lambda>t. (t - m) * f' + inverse m) has_integral (?F y - ?F x)) {x..y}"
   9.301 +    by (intro fundamental_theorem_of_calculus)
   9.302 +       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
   9.303 +             intro!: derivative_eq_intros)
   9.304 +  also from m have "?F y - ?F x = ((y - m)^2 - (x - m)^2) * f' / 2 + (y - x) / m" 
   9.305 +    by (simp add: field_simps)
   9.306 +  also have "((y - m)^2 - (x - m)^2) = 0" by (simp add: m_def power2_eq_square field_simps)
   9.307 +  also have "0 * f' / 2 + (y - x) / m = ?A" by (simp add: m_def)
   9.308 +  finally have int1: "((\<lambda>t. (t - m) * f' + inverse m) has_integral ?A) {x..y}" .
   9.309 +
   9.310 +  from assms have int2: "(inverse has_integral (ln y - ln x)) {x..y}"
   9.311 +    by (intro fundamental_theorem_of_calculus)
   9.312 +       (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric] divide_simps
   9.313 +             intro!: derivative_eq_intros)
   9.314 +  hence "ln y - ln x = integral {x..y} inverse" by (simp add: integral_unique)
   9.315 +  also have ineq: "\<forall>xa\<in>{x..y}. inverse xa \<ge> (xa - m) * f' + inverse m"
   9.316 +  proof
   9.317 +    fix t assume t: "t \<in> {x..y}"
   9.318 +    from t assms have "inverse t - inverse m \<ge> f' * (t - m)"
   9.319 +      by (intro convex_on_imp_above_tangent[of "{0<..}"] convex_on_inverse)
   9.320 +         (auto simp: m_def interior_open f'_def power2_eq_square intro!: derivative_eq_intros)
   9.321 +    thus "(t - m) * f' + inverse m \<le> inverse t" by (simp add: algebra_simps)
   9.322 +  qed
   9.323 +  hence "integral {x..y} inverse \<ge> integral {x..y} (\<lambda>t. (t - m) * f' + inverse m)"
   9.324 +    using int1 int2 by (intro integral_le has_integral_integrable)
   9.325 +  also have "integral {x..y} (\<lambda>t. (t - m) * f' + inverse m) = ?A"
   9.326 +    using integral_unique[OF int1] by simp
   9.327 +  finally show ?thesis .
   9.328 +qed
   9.329 +
   9.330 +
   9.331 +lemma euler_mascheroni_lower: 
   9.332 +        "euler_mascheroni \<ge> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 2))"
   9.333 +  and euler_mascheroni_upper:
   9.334 +        "euler_mascheroni \<le> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 1))"
   9.335 +proof -
   9.336 +  def D \<equiv> "\<lambda>n. inverse (of_nat (n+1)) + ln (of_nat (n+1)) - ln (of_nat (n+2)) :: real"
   9.337 +  let ?g = "\<lambda>n. ln (of_nat (n+2)) - ln (of_nat (n+1)) - inverse (of_nat (n+1)) :: real"
   9.338 +  def inv \<equiv> "\<lambda>n. inverse (real_of_nat n)"
   9.339 +  fix n :: nat
   9.340 +  note summable = sums_summable[OF euler_mascheroni_sum, folded D_def]
   9.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)"
   9.342 +    unfolding inv_def
   9.343 +    by (intro sums_divide telescope_sums' LIMSEQ_ignore_initial_segment LIMSEQ_inverse_real_of_nat)
   9.344 +  have sums': "(\<lambda>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2) sums ((inv (Suc (0 + n)) - 0)/2)"
   9.345 +    unfolding inv_def
   9.346 +    by (intro sums_divide telescope_sums' LIMSEQ_ignore_initial_segment LIMSEQ_inverse_real_of_nat)
   9.347 +  from euler_mascheroni_sum have "euler_mascheroni = (\<Sum>k. D k)"
   9.348 +    by (simp add: sums_iff D_def)
   9.349 +  also have "\<dots> = (\<Sum>k. D (k + Suc n)) + (\<Sum>k\<le>n. D k)"
   9.350 +    by (subst suminf_split_initial_segment[OF summable, of "Suc n"], subst lessThan_Suc_atMost) simp
   9.351 +  finally have sum: "(\<Sum>k\<le>n. D k) - euler_mascheroni = -(\<Sum>k. D (k + Suc n))" by simp
   9.352 +
   9.353 +  note sum
   9.354 +  also have "\<dots> \<le> -(\<Sum>k. (inv (k + Suc n + 1) - inv (k + Suc n + 2)) / 2)"
   9.355 +  proof (intro le_imp_neg_le suminf_le allI summable_ignore_initial_segment[OF summable])
   9.356 +    fix k' :: nat
   9.357 +    def k \<equiv> "k' + Suc n"
   9.358 +    hence k: "k > 0" by (simp add: k_def)
   9.359 +    have "real_of_nat (k+1) > 0" by (simp add: k_def)
   9.360 +    with ln_inverse_approx_le[OF this zero_less_one]
   9.361 +      have "ln (of_nat k + 2) - ln (of_nat k + 1) \<le> (inv (k+1) + inv (k+2))/2"
   9.362 +      by (simp add: inv_def add_ac)
   9.363 +    hence "(inv (k+1) - inv (k+2))/2 \<le> inv (k+1) + ln (of_nat (k+1)) - ln (of_nat (k+2))"
   9.364 +      by (simp add: field_simps)
   9.365 +    also have "\<dots> = D k" unfolding D_def inv_def ..
   9.366 +    finally show "D (k' + Suc n) \<ge> (inv (k' + Suc n + 1) - inv (k' + Suc n + 2)) / 2"
   9.367 +      by (simp add: k_def)
   9.368 +    from sums_summable[OF sums] 
   9.369 +      show "summable (\<lambda>k. (inv (k + Suc n + 1) - inv (k + Suc n + 2))/2)" by simp
   9.370 +  qed
   9.371 +  also from sums have "\<dots> = -inv (n+2) / 2" by (simp add: sums_iff)
   9.372 +  finally have "euler_mascheroni \<ge> (\<Sum>k\<le>n. D k) + 1 / (of_nat (2 * (n+2)))" 
   9.373 +    by (simp add: inv_def field_simps of_nat_mult)
   9.374 +  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)))"
   9.375 +    unfolding harm_altdef D_def by (subst lessThan_Suc_atMost) (simp add:  setsum.distrib setsum_subtractf)
   9.376 +  also have "(\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1))) = ln (of_nat (n+2))"
   9.377 +    by (subst atLeast0AtMost [symmetric], subst setsum_Suc_diff) simp_all
   9.378 +  finally show "euler_mascheroni \<ge> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 2))"
   9.379 +    by simp
   9.380 +  
   9.381 +  note sum
   9.382 +  also have "-(\<Sum>k. D (k + Suc n)) \<ge> -(\<Sum>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2)"
   9.383 +  proof (intro le_imp_neg_le suminf_le allI summable_ignore_initial_segment[OF summable])
   9.384 +    fix k' :: nat
   9.385 +    def k \<equiv> "k' + Suc n"
   9.386 +    hence k: "k > 0" by (simp add: k_def)
   9.387 +    have "real_of_nat (k+1) > 0" by (simp add: k_def)
   9.388 +    from ln_inverse_approx_ge[of "of_nat k + 1" "of_nat k + 2"]
   9.389 +      have "2 / (2 * real_of_nat k + 3) \<le> ln (of_nat (k+2)) - ln (real_of_nat (k+1))"
   9.390 +      by (simp add: add_ac)
   9.391 +    hence "D k \<le> 1 / real_of_nat (k+1) - 2 / (2 * real_of_nat k + 3)" 
   9.392 +      by (simp add: D_def inverse_eq_divide inv_def)
   9.393 +    also have "\<dots> = inv ((k+1)*(2*k+3))" unfolding inv_def by (simp add: field_simps)
   9.394 +    also have "\<dots> \<le> inv (2*k*(k+1))" unfolding inv_def using k
   9.395 +      by (intro le_imp_inverse_le) 
   9.396 +         (simp add: algebra_simps, simp del: of_nat_add)
   9.397 +    also have "\<dots> = (inv k - inv (k+1))/2" unfolding inv_def using k
   9.398 +      by (simp add: divide_simps del: of_nat_mult) (simp add: algebra_simps)
   9.399 +    finally show "D k \<le> (inv (Suc (k' + n)) - inv (Suc (Suc k' + n)))/2" unfolding k_def by simp
   9.400 +  next
   9.401 +    from sums_summable[OF sums'] 
   9.402 +      show "summable (\<lambda>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2)" by simp
   9.403 +  qed
   9.404 +  also from sums' have "(\<Sum>k. (inv (Suc (k + n)) - inv (Suc (Suc k + n)))/2) = inv (n+1)/2"
   9.405 +    by (simp add: sums_iff)
   9.406 +  finally have "euler_mascheroni \<le> (\<Sum>k\<le>n. D k) + 1 / of_nat (2 * (n+1))" 
   9.407 +    by (simp add: inv_def field_simps)
   9.408 +  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)))"
   9.409 +    unfolding harm_altdef D_def by (subst lessThan_Suc_atMost) (simp add:  setsum.distrib setsum_subtractf)
   9.410 +  also have "(\<Sum>k\<le>n. ln (real_of_nat (Suc k+1)) - ln (of_nat (k+1))) = ln (of_nat (n+2))"
   9.411 +    by (subst atLeast0AtMost [symmetric], subst setsum_Suc_diff) simp_all
   9.412 +  finally show "euler_mascheroni \<le> harm (Suc n) - ln (real_of_nat (n + 2)) + 1/real_of_nat (2 * (n + 1))"
   9.413 +    by simp
   9.414 +qed
   9.415 +
   9.416 +lemma euler_mascheroni_pos: "euler_mascheroni > (0::real)"
   9.417 +  using euler_mascheroni_lower[of 0] ln_2_less_1 by (simp add: harm_def)
   9.418 +
   9.419 +lemma ln_approx_aux:
   9.420 +  fixes n :: nat and x :: real
   9.421 +  defines "y \<equiv> (x-1)/(x+1)"
   9.422 +  assumes x: "x > 0" "x \<noteq> 1"
   9.423 +  shows "inverse (2*y^(2*n+1)) * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))) \<in> 
   9.424 +            {0..(1 / (1 - y^2) / of_nat (2*n+1))}"
   9.425 +proof -
   9.426 +  from x have norm_y: "norm y < 1" unfolding y_def by simp
   9.427 +  from power_strict_mono[OF this, of 2] have norm_y': "norm y^2 < 1" by simp
   9.428 +
   9.429 +  let ?f = "\<lambda>k. 2 * y ^ (2*k+1) / of_nat (2*k+1)"
   9.430 +  note sums = ln_series_quadratic[OF x(1)]
   9.431 +  def c \<equiv> "inverse (2*y^(2*n+1))"
   9.432 +  let ?d = "c * (ln x - (\<Sum>k<n. ?f k))"
   9.433 +  have "\<forall>k. y\<^sup>2^k / of_nat (2*(k+n)+1) \<le> y\<^sup>2 ^ k / of_nat (2*n+1)"
   9.434 +    by (intro allI divide_left_mono mult_right_mono mult_pos_pos zero_le_power[of "y^2"]) simp_all
   9.435 +  moreover {
   9.436 +    have "(\<lambda>k. ?f (k + n)) sums (ln x - (\<Sum>k<n. ?f k))"
   9.437 +      using sums_split_initial_segment[OF sums] by (simp add: y_def)
   9.438 +    hence "(\<lambda>k. c * ?f (k + n)) sums ?d" by (rule sums_mult)
   9.439 +    also have "(\<lambda>k. c * (2*y^(2*(k+n)+1) / of_nat (2*(k+n)+1))) =
   9.440 +                   (\<lambda>k. (c * (2*y^(2*n+1))) * ((y^2)^k / of_nat (2*(k+n)+1)))"
   9.441 +      by (simp only: ring_distribs power_add power_mult) (simp add: mult_ac)
   9.442 +    also from x have "c * (2*y^(2*n+1)) = 1" by (simp add: c_def y_def)
   9.443 +    finally have "(\<lambda>k. (y^2)^k / of_nat (2*(k+n)+1)) sums ?d" by simp
   9.444 +  } note sums' = this
   9.445 +  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))"
   9.446 +    by (intro sums_divide geometric_sums) (simp_all add: norm_power)
   9.447 +  ultimately have "?d \<le> (1 / (1 - y^2) / of_nat (2*n+1))" by (rule sums_le)
   9.448 +  moreover have "c * (ln x - (\<Sum>k<n. 2 * y ^ (2 * k + 1) / real_of_nat (2 * k + 1))) \<ge> 0"
   9.449 +    by (intro sums_le[OF _ sums_zero sums']) simp_all
   9.450 +  ultimately show ?thesis unfolding c_def by simp
   9.451 +qed
   9.452 +
   9.453 +lemma
   9.454 +  fixes n :: nat and x :: real
   9.455 +  defines "y \<equiv> (x-1)/(x+1)"
   9.456 +  defines "approx \<equiv> (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))"
   9.457 +  defines "d \<equiv> y^(2*n+1) / (1 - y^2) / of_nat (2*n+1)"
   9.458 +  assumes x: "x > 1"
   9.459 +  shows   ln_approx_bounds: "ln x \<in> {approx..approx + 2*d}"
   9.460 +  and     ln_approx_abs:    "abs (ln x - (approx + d)) \<le> d"
   9.461 +proof -
   9.462 +  def c \<equiv> "2*y^(2*n+1)"
   9.463 +  from x have c_pos: "c > 0" unfolding c_def y_def 
   9.464 +    by (intro mult_pos_pos zero_less_power) simp_all
   9.465 +  have A: "inverse c * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1))) \<in>
   9.466 +              {0.. (1 / (1 - y^2) / of_nat (2*n+1))}" using assms unfolding y_def c_def
   9.467 +    by (intro ln_approx_aux) simp_all
   9.468 +  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))"
   9.469 +    by simp
   9.470 +  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))" 
   9.471 +    by (auto simp add: divide_simps)
   9.472 +  with c_pos have "ln x \<le> c / (1 - y^2) / of_nat (2*n+1) + approx"
   9.473 +    by (subst (asm) pos_divide_le_eq) (simp_all add: mult_ac approx_def)
   9.474 +  moreover {
   9.475 +    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))))"
   9.476 +      by (intro mult_nonneg_nonneg[of c]) simp_all
   9.477 +    also have "\<dots> = (c * inverse c) * (ln x - (\<Sum>k<n. 2*y^(2*k+1) / of_nat (2*k+1)))"  
   9.478 +      by (simp add: mult_ac)
   9.479 +    also from c_pos have "c * inverse c = 1" by simp
   9.480 +    finally have "ln x \<ge> approx" by (simp add: approx_def)
   9.481 +  }
   9.482 +  ultimately show "ln x \<in> {approx..approx + 2*d}" by (simp add: c_def d_def)
   9.483 +  thus "abs (ln x - (approx + d)) \<le> d" by auto
   9.484 +qed
   9.485 +
   9.486 +context
   9.487 +begin
   9.488 +
   9.489 +qualified lemma ln_approx_abs': 
   9.490 +  assumes "x > (1::real)"
   9.491 +  assumes "(x-1)/(x+1) = y"
   9.492 +  assumes "y^2 = ysqr"
   9.493 +  assumes "(\<Sum>k<n. inverse (of_nat (2*k+1)) * ysqr^k) = approx"
   9.494 +  assumes "y*ysqr^n / (1 - ysqr) / of_nat (2*n+1) = d"
   9.495 +  assumes "d \<le> e"
   9.496 +  shows   "abs (ln x - (2*y*approx + d)) \<le> e"
   9.497 +proof -
   9.498 +  note ln_approx_abs[OF assms(1), of n]
   9.499 +  also note assms(2)
   9.500 +  also have "y^(2*n+1) = y*ysqr^n" by (simp add: assms(3)[symmetric] power_mult)
   9.501 +  also note assms(3)
   9.502 +  also note assms(5)
   9.503 +  also note assms(5)
   9.504 +  also note assms(6)
   9.505 +  also have "(\<Sum>k<n. 2*y^(2*k+1) / real_of_nat (2 * k + 1)) = (2*y) * approx"
   9.506 +    apply (subst assms(4)[symmetric], subst setsum_right_distrib)
   9.507 +    apply (simp add: assms(3)[symmetric] power_mult)
   9.508 +    apply (simp add: mult_ac divide_simps)?
   9.509 +    done
   9.510 +  finally show ?thesis .
   9.511 +qed
   9.512 +
   9.513 +lemma ln_2_approx: "\<bar>ln 2 - 0.69314718055\<bar> < inverse (2 ^ 36 :: real)" (is ?thesis1)
   9.514 +  and ln_2_bounds: "ln (2::real) \<in> {0.693147180549..0.693147180561}" (is ?thesis2)
   9.515 +proof -
   9.516 +  def approx \<equiv> "0.69314718055 :: real" and approx' \<equiv> "4465284211343447 / 6442043387911560 :: real"
   9.517 +  def d \<equiv> "inverse (195259926456::real)"
   9.518 +  have "dist (ln 2) approx \<le> dist (ln 2) approx' + dist approx' approx" by (rule dist_triangle)
   9.519 +  also have "\<bar>ln (2::real) - (2 * (1/3) * (651187280816108 / 626309773824735) +
   9.520 +                 inverse 195259926456)\<bar> \<le> inverse 195259926456"
   9.521 +  proof (rule ln_approx_abs'[where n = 10])
   9.522 +    show "(1/3::real)^2 = 1/9" by (simp add: power2_eq_square)
   9.523 +  qed (simp_all add: eval_nat_numeral)
   9.524 +  hence A: "dist (ln 2) approx' \<le> d" by (simp add: dist_real_def approx'_def d_def)
   9.525 +  hence "dist (ln 2) approx' + dist approx' approx \<le> \<dots> + dist approx' approx"
   9.526 +    by (rule add_right_mono)
   9.527 +  also have "\<dots> < inverse (2 ^ 36)" by (simp add: dist_real_def approx'_def approx_def d_def)
   9.528 +  finally show ?thesis1 unfolding dist_real_def approx_def .
   9.529 +  
   9.530 +  from A have "ln 2 \<in> {approx' - d..approx' + d}" 
   9.531 +    by (simp add: dist_real_def abs_real_def split: split_if_asm)
   9.532 +  also have "\<dots> \<subseteq> {0.693147180549..0.693147180561}"
   9.533 +    by (subst atLeastatMost_subset_iff, rule disjI2) (simp add: approx'_def d_def)
   9.534 +  finally show ?thesis2 .
   9.535 +qed
   9.536 +
   9.537 +end
   9.538 +
   9.539 +
   9.540 +lemma euler_mascheroni_bounds:
   9.541 +  fixes n :: nat assumes "n \<ge> 1" defines "t \<equiv> harm n - ln (of_nat (Suc n)) :: real"
   9.542 +  shows "euler_mascheroni \<in> {t + inverse (of_nat (2*(n+1)))..t + inverse (of_nat (2*n))}"
   9.543 +  using assms euler_mascheroni_upper[of "n-1"] euler_mascheroni_lower[of "n-1"]
   9.544 +  unfolding t_def by (cases n) (simp_all add: harm_Suc t_def inverse_eq_divide of_nat_mult)
   9.545 +
   9.546 +lemma euler_mascheroni_bounds':
   9.547 +  fixes n :: nat assumes "n \<ge> 1" "ln (real_of_nat (Suc n)) \<in> {l<..<u}"
   9.548 +  shows "euler_mascheroni \<in> 
   9.549 +           {harm n - u + inverse (of_nat (2*(n+1)))<..<harm n - l + inverse (of_nat (2*n))}"
   9.550 +  using euler_mascheroni_bounds[OF assms(1)] assms(2) by auto
   9.551 +
   9.552 +lemma euler_mascheroni_approx: 
   9.553 +  defines "approx \<equiv> 0.577257 :: real" and "e \<equiv> 0.000063 :: real"
   9.554 +  shows   "abs (euler_mascheroni - approx :: real) < e"
   9.555 +  (is "abs (_ - ?approx) < ?e")
   9.556 +proof -
   9.557 +  def l \<equiv> "47388813395531028639296492901910937/82101866951584879688289000000000000 :: real"
   9.558 +  def u \<equiv> "142196984054132045946501548559032969 / 246305600854754639064867000000000000 :: real"
   9.559 +  have impI: "P \<longrightarrow> Q" if Q for P Q using that by blast
   9.560 +  have hsum_63: "harm 63 = (310559566510213034489743057 / 65681493561267903750631200 ::real)"
   9.561 +    by (simp add: harm_expand)
   9.562 +  from harm_Suc[of 63] have hsum_64: "harm 64 = 
   9.563 +          623171679694215690971693339 / (131362987122535807501262400::real)" 
   9.564 +    by (subst (asm) hsum_63) simp
   9.565 +  have "ln (64::real) = real (6::nat) * ln 2" by (subst ln_realpow[symmetric]) simp_all
   9.566 +  hence "ln (real_of_nat (Suc 63)) \<in> {4.158883083293<..<4.158883083367}" using ln_2_bounds by simp
   9.567 +  from euler_mascheroni_bounds'[OF _ this]
   9.568 +    have "(euler_mascheroni :: real) \<in> {l<..<u}" 
   9.569 +    by (simp add: hsum_63 del: greaterThanLessThan_iff) (simp only: l_def u_def)
   9.570 +  also have "\<dots> \<subseteq> {approx - e<..<approx + e}"
   9.571 +    by (subst greaterThanLessThan_subseteq_greaterThanLessThan, rule impI) 
   9.572 +       (simp add: approx_def e_def u_def l_def)
   9.573 +  finally show ?thesis by (simp add: abs_real_def)
   9.574 +qed
   9.575 +
   9.576 +end
   9.577 \ No newline at end of file
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/HOL/Multivariate_Analysis/Integral_Test.thy	Mon Jan 04 17:45:36 2016 +0100
    10.3 @@ -0,0 +1,111 @@
    10.4 +(*
    10.5 +  Title:    HOL/Multivariate_Analysis/Integral_Test.thy
    10.6 +  Author:   Manuel Eberl, TU München
    10.7 +  
    10.8 +  The integral test for summability. We show here that for a decreasing non-negative 
    10.9 +  function, the infinite sum over that function evaluated at the natural numbers 
   10.10 +  converges iff the corresponding integral converges.
   10.11 +  
   10.12 +  As a useful side result, we also provide some results on the difference between
   10.13 +  the integral and the partial sum. (This is useful e.g. for the definition of the
   10.14 +  Euler–Mascheroni constant)
   10.15 +*)
   10.16 +theory Integral_Test
   10.17 +imports Integration
   10.18 +begin
   10.19 +
   10.20 +subsubsection \<open>Integral test\<close>
   10.21 +
   10.22 +(* TODO: continuous_in \<rightarrow> integrable_on *)
   10.23 +locale antimono_fun_sum_integral_diff =
   10.24 +  fixes f :: "real \<Rightarrow> real"
   10.25 +  assumes dec: "\<And>x y. x \<ge> 0 \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<ge> f y"
   10.26 +  assumes nonneg: "\<And>x. x \<ge> 0 \<Longrightarrow> f x \<ge> 0"
   10.27 +  assumes cont: "continuous_on {0..} f"
   10.28 +begin
   10.29 +
   10.30 +definition "sum_integral_diff_series n = (\<Sum>k\<le>n. f (of_nat k)) - (integral {0..of_nat n} f)"
   10.31 +
   10.32 +lemma sum_integral_diff_series_nonneg:
   10.33 +  "sum_integral_diff_series n \<ge> 0"
   10.34 +proof -
   10.35 +  note int = integrable_continuous_real[OF continuous_on_subset[OF cont]]
   10.36 +  let ?int = "\<lambda>a b. integral {of_nat a..of_nat b} f"
   10.37 +  have "-sum_integral_diff_series n = ?int 0 n - (\<Sum>k\<le>n. f (of_nat k))" 
   10.38 +    by (simp add: sum_integral_diff_series_def)
   10.39 +  also have "?int 0 n = (\<Sum>k<n. ?int k (Suc k))"
   10.40 +  proof (induction n)
   10.41 +    case (Suc n)
   10.42 +    have "?int 0 (Suc n) = ?int 0 n + ?int n (Suc n)"
   10.43 +      by (intro integral_combine[symmetric] int) simp_all
   10.44 +    with Suc show ?case by simp
   10.45 +  qed simp_all
   10.46 +  also have "... \<le> (\<Sum>k<n. integral {of_nat k..of_nat (Suc k)} (\<lambda>_::real. f (of_nat k)))"
   10.47 +    by (intro setsum_mono integral_le int) (auto intro: dec)
   10.48 +  also have "... = (\<Sum>k<n. f (of_nat k))" by simp
   10.49 +  also have "\<dots> - (\<Sum>k\<le>n. f (of_nat k)) = -(\<Sum>k\<in>{..n} - {..<n}. f (of_nat k))"
   10.50 +    by (subst setsum_diff) auto
   10.51 +  also have "\<dots> \<le> 0" by (auto intro!: setsum_nonneg nonneg)
   10.52 +  finally show "sum_integral_diff_series n \<ge> 0" by simp
   10.53 +qed
   10.54 +
   10.55 +lemma sum_integral_diff_series_antimono:
   10.56 +  assumes "m \<le> n"
   10.57 +  shows   "sum_integral_diff_series m \<ge> sum_integral_diff_series n"
   10.58 +proof -
   10.59 +  let ?int = "\<lambda>a b. integral {of_nat a..of_nat b} f"
   10.60 +  note int = integrable_continuous_real[OF continuous_on_subset[OF cont]]
   10.61 +  have d_mono: "sum_integral_diff_series (Suc n) \<le> sum_integral_diff_series n" for n
   10.62 +  proof -
   10.63 +    fix n :: nat
   10.64 +    have "sum_integral_diff_series (Suc n) - sum_integral_diff_series n = 
   10.65 +            f (of_nat (Suc n)) + (?int 0 n - ?int 0 (Suc n))"
   10.66 +      unfolding sum_integral_diff_series_def by (simp add: algebra_simps)
   10.67 +    also have "?int 0 n - ?int 0 (Suc n) = -?int n (Suc n)"
   10.68 +      by (subst integral_combine [symmetric, of "of_nat 0" "of_nat n" "of_nat (Suc n)"])
   10.69 +         (auto intro!: int simp: algebra_simps)
   10.70 +    also have "?int n (Suc n) \<ge> integral {of_nat n..of_nat (Suc n)} (\<lambda>_::real. f (of_nat (Suc n)))"
   10.71 +      by (intro integral_le int) (auto intro: dec)
   10.72 +    hence "f (of_nat (Suc n)) + -?int n (Suc n) \<le> 0" by (simp add: algebra_simps)
   10.73 +    finally show "sum_integral_diff_series (Suc n) \<le> sum_integral_diff_series n" by simp
   10.74 +  qed
   10.75 +  with assms show ?thesis
   10.76 +    by (induction rule: inc_induct) (auto intro: order.trans[OF _ d_mono])
   10.77 +qed
   10.78 +
   10.79 +lemma sum_integral_diff_series_Bseq: "Bseq sum_integral_diff_series"
   10.80 +proof -
   10.81 +  from sum_integral_diff_series_nonneg and sum_integral_diff_series_antimono 
   10.82 +    have "norm (sum_integral_diff_series n) \<le> sum_integral_diff_series 0" for n by simp
   10.83 +  thus "Bseq sum_integral_diff_series" by (rule BseqI')
   10.84 +qed
   10.85 +
   10.86 +lemma sum_integral_diff_series_monoseq: "monoseq sum_integral_diff_series"
   10.87 +  using sum_integral_diff_series_antimono unfolding monoseq_def by blast
   10.88 +
   10.89 +lemma sum_integral_diff_series_convergent: "convergent sum_integral_diff_series"
   10.90 +  using sum_integral_diff_series_Bseq sum_integral_diff_series_monoseq
   10.91 +  by (blast intro!: Bseq_monoseq_convergent)
   10.92 +
   10.93 +lemma integral_test:
   10.94 +  "summable (\<lambda>n. f (of_nat n)) \<longleftrightarrow> convergent (\<lambda>n. integral {0..of_nat n} f)"
   10.95 +proof -
   10.96 +  have "summable (\<lambda>n. f (of_nat n)) \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k\<le>n. f (of_nat k))"
   10.97 +    by (simp add: summable_iff_convergent')
   10.98 +  also have "... \<longleftrightarrow> convergent (\<lambda>n. integral {0..of_nat n} f)"
   10.99 +  proof
  10.100 +    assume "convergent (\<lambda>n. \<Sum>k\<le>n. f (of_nat k))"
  10.101 +    from convergent_diff[OF this sum_integral_diff_series_convergent] 
  10.102 +      show "convergent (\<lambda>n. integral {0..of_nat n} f)" 
  10.103 +        unfolding sum_integral_diff_series_def by simp
  10.104 +  next
  10.105 +    assume "convergent (\<lambda>n. integral {0..of_nat n} f)"
  10.106 +    from convergent_add[OF this sum_integral_diff_series_convergent] 
  10.107 +      show "convergent (\<lambda>n. \<Sum>k\<le>n. f (of_nat k))" unfolding sum_integral_diff_series_def by simp
  10.108 +  qed
  10.109 +  finally show ?thesis by simp
  10.110 +qed
  10.111 +
  10.112 +end
  10.113 +
  10.114 +end
  10.115 \ No newline at end of file
    11.1 --- a/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Sun Jan 03 21:45:34 2016 +0100
    11.2 +++ b/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Mon Jan 04 17:45:36 2016 +0100
    11.3 @@ -8,6 +8,8 @@
    11.4    Bounded_Continuous_Function
    11.5    Weierstrass
    11.6    Cauchy_Integral_Thm
    11.7 +  Generalised_Binomial_Theorem
    11.8 +  Gamma
    11.9  begin
   11.10  
   11.11  end
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/HOL/Multivariate_Analysis/Summation.thy	Mon Jan 04 17:45:36 2016 +0100
    12.3 @@ -0,0 +1,907 @@
    12.4 +(*
    12.5 +  Title:    HOL/Multivariate_Analysis/Summation.thy
    12.6 +  Author:   Manuel Eberl, TU München
    12.7 +  
    12.8 +  The definition of the radius of convergence of a power series, 
    12.9 +  various summability tests, lemmas to compute the radius of convergence etc.
   12.10 +*)
   12.11 +theory Summation
   12.12 +imports
   12.13 +  Complex_Main
   12.14 +  "~~/src/HOL/Library/Extended_Real" 
   12.15 +  "~~/src/HOL/Library/Liminf_Limsup"
   12.16 +begin
   12.17 +
   12.18 +subsection \<open>Rounded dual logarithm\<close>
   12.19 +
   12.20 +(* This is required for the Cauchy condensation criterion *)
   12.21 +
   12.22 +definition "natlog2 n = (if n = 0 then 0 else nat \<lfloor>log 2 (real_of_nat n)\<rfloor>)"
   12.23 +
   12.24 +lemma natlog2_0 [simp]: "natlog2 0 = 0" by (simp add: natlog2_def)
   12.25 +lemma natlog2_1 [simp]: "natlog2 1 = 0" by (simp add: natlog2_def)
   12.26 +lemma natlog2_eq_0_iff: "natlog2 n = 0 \<longleftrightarrow> n < 2" by (simp add: natlog2_def)
   12.27 +
   12.28 +lemma natlog2_power_of_two [simp]: "natlog2 (2 ^ n) = n"
   12.29 +  by (simp add: natlog2_def log_nat_power)
   12.30 +
   12.31 +lemma natlog2_mono: "m \<le> n \<Longrightarrow> natlog2 m \<le> natlog2 n"
   12.32 +  unfolding natlog2_def by (simp_all add: nat_mono floor_mono)
   12.33 +
   12.34 +lemma pow_natlog2_le: "n > 0 \<Longrightarrow> 2 ^ natlog2 n \<le> n"
   12.35 +proof -
   12.36 +  assume n: "n > 0"
   12.37 +  from n have "of_nat (2 ^ natlog2 n) = 2 powr real_of_nat (nat \<lfloor>log 2 (real_of_nat n)\<rfloor>)"
   12.38 +    by (subst powr_realpow) (simp_all add: natlog2_def)
   12.39 +  also have "\<dots> = 2 powr of_int \<lfloor>log 2 (real_of_nat n)\<rfloor>" using n by simp
   12.40 +  also have "\<dots> \<le> 2 powr log 2 (real_of_nat n)" by (intro powr_mono) (linarith, simp_all)
   12.41 +  also have "\<dots> = of_nat n" using n by simp
   12.42 +  finally show ?thesis by simp
   12.43 +qed
   12.44 +
   12.45 +lemma pow_natlog2_gt: "n > 0 \<Longrightarrow> 2 * 2 ^ natlog2 n > n"
   12.46 +  and pow_natlog2_ge: "n > 0 \<Longrightarrow> 2 * 2 ^ natlog2 n \<ge> n"
   12.47 +proof -
   12.48 +  assume n: "n > 0"
   12.49 +  from n have "of_nat n = 2 powr log 2 (real_of_nat n)" by simp
   12.50 +  also have "\<dots> < 2 powr (1 + of_int \<lfloor>log 2 (real_of_nat n)\<rfloor>)" 
   12.51 +    by (intro powr_less_mono) (linarith, simp_all)
   12.52 +  also from n have "\<dots> = 2 powr (1 + real_of_nat (nat \<lfloor>log 2 (real_of_nat n)\<rfloor>))" by simp
   12.53 +  also from n have "\<dots> = of_nat (2 * 2 ^ natlog2 n)"
   12.54 +    by (simp_all add: natlog2_def powr_real_of_int powr_add)
   12.55 +  finally show "2 * 2 ^ natlog2 n > n" by (rule of_nat_less_imp_less)
   12.56 +  thus "2 * 2 ^ natlog2 n \<ge> n" by simp
   12.57 +qed
   12.58 +
   12.59 +lemma natlog2_eqI:
   12.60 +  assumes "n > 0" "2^k \<le> n" "n < 2 * 2^k"
   12.61 +  shows   "natlog2 n = k"
   12.62 +proof -
   12.63 +  from assms have "of_nat (2 ^ k) \<le> real_of_nat n"  by (subst of_nat_le_iff) simp_all
   12.64 +  hence "real_of_int (int k) \<le> log (of_nat 2) (real_of_nat n)"
   12.65 +    by (subst le_log_iff) (simp_all add: powr_realpow assms del: of_nat_le_iff)
   12.66 +  moreover from assms have "real_of_nat n < of_nat (2 ^ Suc k)" by (subst of_nat_less_iff) simp_all
   12.67 +  hence "log 2 (real_of_nat n) < of_nat k + 1"
   12.68 +    by (subst log_less_iff) (simp_all add: assms powr_realpow powr_add)
   12.69 +  ultimately have "\<lfloor>log 2 (real_of_nat n)\<rfloor> = of_nat k" by (intro floor_unique) simp_all
   12.70 +  with assms show ?thesis by (simp add: natlog2_def)
   12.71 +qed
   12.72 +
   12.73 +lemma natlog2_rec: 
   12.74 +  assumes "n \<ge> 2"
   12.75 +  shows   "natlog2 n = 1 + natlog2 (n div 2)"
   12.76 +proof (rule natlog2_eqI)
   12.77 +  from assms have "2 ^ (1 + natlog2 (n div 2)) \<le> 2 * (n div 2)" 
   12.78 +    by (simp add: pow_natlog2_le)
   12.79 +  also have "\<dots> \<le> n" by simp
   12.80 +  finally show "2 ^ (1 + natlog2 (n div 2)) \<le> n" .
   12.81 +next
   12.82 +  from assms have "n < 2 * (n div 2 + 1)" by simp 
   12.83 +  also from assms have "(n div 2) < 2 ^ (1 + natlog2 (n div 2))" 
   12.84 +    by (simp add: pow_natlog2_gt)
   12.85 +  hence "2 * (n div 2 + 1) \<le> 2 * (2 ^ (1 + natlog2 (n div 2)))" 
   12.86 +    by (intro mult_left_mono) simp_all
   12.87 +  finally show "n < 2 * 2 ^ (1 + natlog2 (n div 2))" .
   12.88 +qed (insert assms, simp_all)
   12.89 +
   12.90 +fun natlog2_aux where
   12.91 +  "natlog2_aux n acc = (if (n::nat) < 2 then acc else natlog2_aux (n div 2) (acc + 1))"
   12.92 +
   12.93 +lemma natlog2_aux_correct:
   12.94 +  "natlog2_aux n acc = acc + natlog2 n"
   12.95 +  by (induction n acc rule: natlog2_aux.induct) (auto simp: natlog2_rec natlog2_eq_0_iff)
   12.96 +  
   12.97 +lemma natlog2_code [code]: "natlog2 n = natlog2_aux n 0"
   12.98 +  by (subst natlog2_aux_correct) simp
   12.99 +
  12.100 +
  12.101 +subsection \<open>Convergence tests for infinite sums\<close>
  12.102 +
  12.103 +subsubsection \<open>Root test\<close>
  12.104 +
  12.105 +lemma limsup_root_powser:
  12.106 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.107 +  shows "limsup (\<lambda>n. ereal (root n (norm (f n * z ^ n)))) = 
  12.108 +             limsup (\<lambda>n. ereal (root n (norm (f n)))) * ereal (norm z)"
  12.109 +proof -
  12.110 +  have A: "(\<lambda>n. ereal (root n (norm (f n * z ^ n)))) = 
  12.111 +              (\<lambda>n. ereal (root n (norm (f n))) * ereal (norm z))" (is "?g = ?h")
  12.112 +  proof
  12.113 +    fix n show "?g n = ?h n"
  12.114 +    by (cases "n = 0") (simp_all add: norm_mult real_root_mult real_root_pos2 norm_power)
  12.115 +  qed
  12.116 +  show ?thesis by (subst A, subst limsup_ereal_mult_right) simp_all
  12.117 +qed
  12.118 +
  12.119 +lemma limsup_root_limit:
  12.120 +  assumes "(\<lambda>n. ereal (root n (norm (f n)))) \<longlonglongrightarrow> l" (is "?g \<longlonglongrightarrow> _")
  12.121 +  shows   "limsup (\<lambda>n. ereal (root n (norm (f n)))) = l"
  12.122 +proof -
  12.123 +  from assms have "convergent ?g" "lim ?g = l"
  12.124 +    unfolding convergent_def by (blast intro: limI)+
  12.125 +  with convergent_limsup_cl show ?thesis by force
  12.126 +qed
  12.127 +
  12.128 +lemma limsup_root_limit':
  12.129 +  assumes "(\<lambda>n. root n (norm (f n))) \<longlonglongrightarrow> l"
  12.130 +  shows   "limsup (\<lambda>n. ereal (root n (norm (f n)))) = ereal l"
  12.131 +  by (intro limsup_root_limit tendsto_ereal assms)
  12.132 +
  12.133 +lemma root_test_convergence':
  12.134 +  fixes f :: "nat \<Rightarrow> 'a :: banach"
  12.135 +  defines "l \<equiv> limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.136 +  assumes l: "l < 1"
  12.137 +  shows   "summable f"
  12.138 +proof -
  12.139 +  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
  12.140 +  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
  12.141 +  finally have "l \<ge> 0" by simp
  12.142 +  with l obtain l' where l': "l = ereal l'" by (cases l) simp_all
  12.143 +
  12.144 +  def c \<equiv> "(1 - l') / 2"
  12.145 +  from l and `l \<ge> 0` have c: "l + c > l" "l' + c \<ge> 0" "l' + c < 1" unfolding c_def 
  12.146 +    by (simp_all add: field_simps l')
  12.147 +  have "\<forall>C>l. eventually (\<lambda>n. ereal (root n (norm (f n))) < C) sequentially"
  12.148 +    by (subst Limsup_le_iff[symmetric]) (simp add: l_def)
  12.149 +  with c have "eventually (\<lambda>n. ereal (root n (norm (f n))) < l + ereal c) sequentially" by simp
  12.150 +  with eventually_gt_at_top[of "0::nat"]
  12.151 +    have "eventually (\<lambda>n. norm (f n) \<le> (l' + c) ^ n) sequentially"
  12.152 +  proof eventually_elim
  12.153 +    fix n :: nat assume n: "n > 0" 
  12.154 +    assume "ereal (root n (norm (f n))) < l + ereal c"
  12.155 +    hence "root n (norm (f n)) \<le> l' + c" by (simp add: l')
  12.156 +    with c n have "root n (norm (f n)) ^ n \<le> (l' + c) ^ n"
  12.157 +      by (intro power_mono) (simp_all add: real_root_ge_zero)
  12.158 +    also from n have "root n (norm (f n)) ^ n = norm (f n)" by simp
  12.159 +    finally show "norm (f n) \<le> (l' + c) ^ n" by simp
  12.160 +  qed
  12.161 +  thus ?thesis
  12.162 +    by (rule summable_comparison_test_ev[OF _ summable_geometric]) (simp add: c)
  12.163 +qed
  12.164 +
  12.165 +lemma root_test_divergence:
  12.166 +  fixes f :: "nat \<Rightarrow> 'a :: banach"
  12.167 +  defines "l \<equiv> limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.168 +  assumes l: "l > 1"
  12.169 +  shows   "\<not>summable f"
  12.170 +proof
  12.171 +  assume "summable f"
  12.172 +  hence bounded: "Bseq f" by (simp add: summable_imp_Bseq)
  12.173 +
  12.174 +  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
  12.175 +  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
  12.176 +  finally have l_nonneg: "l \<ge> 0" by simp
  12.177 +
  12.178 +  def c \<equiv> "if l = \<infinity> then 2 else 1 + (real_of_ereal l - 1) / 2"
  12.179 +  from l l_nonneg consider "l = \<infinity>" | "\<exists>l'. l = ereal l'" by (cases l) simp_all
  12.180 +  hence c: "c > 1 \<and> ereal c < l" by cases (insert l, auto simp: c_def field_simps)
  12.181 +
  12.182 +  have unbounded: "\<not>bdd_above {n. root n (norm (f n)) > c}"
  12.183 +  proof
  12.184 +    assume "bdd_above {n. root n (norm (f n)) > c}"
  12.185 +    then obtain N where "\<forall>n. root n (norm (f n)) > c \<longrightarrow> n \<le> N" unfolding bdd_above_def by blast
  12.186 +    hence "\<exists>N. \<forall>n\<ge>N. root n (norm (f n)) \<le> c"
  12.187 +      by (intro exI[of _ "N + 1"]) (force simp: not_less_eq_eq[symmetric])
  12.188 +    hence "eventually (\<lambda>n. root n (norm (f n)) \<le> c) sequentially"
  12.189 +      by (auto simp: eventually_at_top_linorder)
  12.190 +    hence "l \<le> c" unfolding l_def by (intro Limsup_bounded) simp_all
  12.191 +    with c show False by auto
  12.192 +  qed
  12.193 +  
  12.194 +  from bounded obtain K where K: "K > 0" "\<And>n. norm (f n) \<le> K" using BseqE by blast
  12.195 +  def n \<equiv> "nat \<lceil>log c K\<rceil>"
  12.196 +  from unbounded have "\<exists>m>n. c < root m (norm (f m))" unfolding bdd_above_def
  12.197 +    by (auto simp: not_le)
  12.198 +  then guess m by (elim exE conjE) note m = this
  12.199 +  from c K have "K = c powr log c K" by (simp add: powr_def log_def)
  12.200 +  also from c have "c powr log c K \<le> c powr real n" unfolding n_def
  12.201 +    by (intro powr_mono, linarith, simp)
  12.202 +  finally have "K \<le> c ^ n" using c by (simp add: powr_realpow)
  12.203 +  also from c m have "c ^ n < c ^ m" by simp
  12.204 +  also from c m have "c ^ m < root m (norm (f m)) ^ m" by (intro power_strict_mono) simp_all
  12.205 +  also from m have "... = norm (f m)" by simp
  12.206 +  finally show False using K(2)[of m]  by simp
  12.207 +qed
  12.208 +
  12.209 +
  12.210 +subsection \<open>Cauchy's condensation test\<close>
  12.211 +
  12.212 +context
  12.213 +fixes f :: "nat \<Rightarrow> real"
  12.214 +begin
  12.215 +
  12.216 +private lemma condensation_inequality:
  12.217 +  assumes mono: "\<And>m n. 0 < m \<Longrightarrow> m \<le> n \<Longrightarrow> f n \<le> f m"
  12.218 +  shows   "(\<Sum>k=1..<n. f k) \<ge> (\<Sum>k=1..<n. f (2 * 2 ^ natlog2 k))" (is "?thesis1")
  12.219 +          "(\<Sum>k=1..<n. f k) \<le> (\<Sum>k=1..<n. f (2 ^ natlog2 k))" (is "?thesis2")
  12.220 +  by (intro setsum_mono mono pow_natlog2_ge pow_natlog2_le, simp, simp)+
  12.221 +
  12.222 +private lemma condensation_condense1: "(\<Sum>k=1..<2^n. f (2 ^ natlog2 k)) = (\<Sum>k<n. 2^k * f (2 ^ k))"
  12.223 +proof (induction n)
  12.224 +  case (Suc n)
  12.225 +  have "{1..<2^Suc n} = {1..<2^n} \<union> {2^n..<(2^Suc n :: nat)}" by auto  
  12.226 +  also have "(\<Sum>k\<in>\<dots>. f (2 ^ natlog2 k)) = 
  12.227 +                 (\<Sum>k<n. 2^k * f (2^k)) + (\<Sum>k = 2^n..<2^Suc n. f (2^natlog2 k))" 
  12.228 +    by (subst setsum.union_disjoint) (insert Suc, auto)
  12.229 +  also have "natlog2 k = n" if "k \<in> {2^n..<2^Suc n}" for k using that by (intro natlog2_eqI) simp_all
  12.230 +  hence "(\<Sum>k = 2^n..<2^Suc n. f (2^natlog2 k)) = (\<Sum>(_::nat) = 2^n..<2^Suc n. f (2^n))"
  12.231 +    by (intro setsum.cong) simp_all
  12.232 +  also have "\<dots> = 2^n * f (2^n)" by (simp add: of_nat_power)
  12.233 +  finally show ?case by simp
  12.234 +qed simp
  12.235 +
  12.236 +private lemma condensation_condense2: "(\<Sum>k=1..<2^n. f (2 * 2 ^ natlog2 k)) = (\<Sum>k<n. 2^k * f (2 ^ Suc k))"
  12.237 +proof (induction n)
  12.238 +  case (Suc n)
  12.239 +  have "{1..<2^Suc n} = {1..<2^n} \<union> {2^n..<(2^Suc n :: nat)}" by auto  
  12.240 +  also have "(\<Sum>k\<in>\<dots>. f (2 * 2 ^ natlog2 k)) = 
  12.241 +                 (\<Sum>k<n. 2^k * f (2^Suc k)) + (\<Sum>k = 2^n..<2^Suc n. f (2 * 2^natlog2 k))" 
  12.242 +    by (subst setsum.union_disjoint) (insert Suc, auto)
  12.243 +  also have "natlog2 k = n" if "k \<in> {2^n..<2^Suc n}" for k using that by (intro natlog2_eqI) simp_all
  12.244 +  hence "(\<Sum>k = 2^n..<2^Suc n. f (2*2^natlog2 k)) = (\<Sum>(_::nat) = 2^n..<2^Suc n. f (2^Suc n))"
  12.245 +    by (intro setsum.cong) simp_all
  12.246 +  also have "\<dots> = 2^n * f (2^Suc n)" by (simp add: of_nat_power)
  12.247 +  finally show ?case by simp
  12.248 +qed simp
  12.249 +
  12.250 +lemma condensation_test:
  12.251 +  assumes mono: "\<And>m. 0 < m \<Longrightarrow> f (Suc m) \<le> f m"
  12.252 +  assumes nonneg: "\<And>n. f n \<ge> 0"
  12.253 +  shows "summable f \<longleftrightarrow> summable (\<lambda>n. 2^n * f (2^n))"
  12.254 +proof -
  12.255 +  def f' \<equiv> "\<lambda>n. if n = 0 then 0 else f n"
  12.256 +  from mono have mono': "decseq (\<lambda>n. f (Suc n))" by (intro decseq_SucI) simp
  12.257 +  hence mono': "f n \<le> f m" if "m \<le> n" "m > 0" for m n 
  12.258 +    using that decseqD[OF mono', of "m - 1" "n - 1"] by simp
  12.259 +  
  12.260 +  have "(\<lambda>n. f (Suc n)) = (\<lambda>n. f' (Suc n))" by (intro ext) (simp add: f'_def)
  12.261 +  hence "summable f \<longleftrightarrow> summable f'"
  12.262 +    by (subst (1 2) summable_Suc_iff [symmetric]) (simp only:)
  12.263 +  also have "\<dots> \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k<n. f' k)" unfolding summable_iff_convergent ..
  12.264 +  also have "monoseq (\<lambda>n. \<Sum>k<n. f' k)" unfolding f'_def
  12.265 +    by (intro mono_SucI1) (auto intro!: mult_nonneg_nonneg nonneg)
  12.266 +  hence "convergent (\<lambda>n. \<Sum>k<n. f' k) \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k<n. f' k)"
  12.267 +    by (rule monoseq_imp_convergent_iff_Bseq)
  12.268 +  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<n. f' k)" unfolding One_nat_def
  12.269 +    by (subst setsum_shift_lb_Suc0_0_upt) (simp_all add: f'_def atLeast0LessThan)
  12.270 +  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<n. f k)" unfolding f'_def by simp
  12.271 +  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)"
  12.272 +    by (rule nonneg_incseq_Bseq_subseq_iff[symmetric])
  12.273 +       (auto intro!: setsum_nonneg incseq_SucI nonneg simp: subseq_def)
  12.274 +  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k<n. 2^k * f (2^k))"
  12.275 +  proof (intro iffI)
  12.276 +    assume A: "Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)"
  12.277 +    have "eventually (\<lambda>n. norm (\<Sum>k<n. 2^k * f (2^Suc k)) \<le> norm (\<Sum>k=1..<2^n. f k)) sequentially"
  12.278 +    proof (intro always_eventually allI)
  12.279 +      fix n :: nat
  12.280 +      have "norm (\<Sum>k<n. 2^k * f (2^Suc k)) = (\<Sum>k<n. 2^k * f (2^Suc k))" unfolding real_norm_def
  12.281 +        by (intro abs_of_nonneg setsum_nonneg ballI mult_nonneg_nonneg nonneg) simp_all
  12.282 +      also have "\<dots> \<le> (\<Sum>k=1..<2^n. f k)"
  12.283 +        by (subst condensation_condense2 [symmetric]) (intro condensation_inequality mono')
  12.284 +      also have "\<dots> = norm \<dots>" unfolding real_norm_def
  12.285 +        by (intro abs_of_nonneg[symmetric] setsum_nonneg ballI mult_nonneg_nonneg nonneg)
  12.286 +      finally show "norm (\<Sum>k<n. 2 ^ k * f (2 ^ Suc k)) \<le> norm (\<Sum>k=1..<2^n. f k)" .
  12.287 +    qed
  12.288 +    from this and A have "Bseq (\<lambda>n. \<Sum>k<n. 2^k * f (2^Suc k))" by (rule Bseq_eventually_mono)
  12.289 +    from Bseq_mult[OF Bfun_const[of 2] this] have "Bseq (\<lambda>n. \<Sum>k<n. 2^Suc k * f (2^Suc k))"
  12.290 +      by (simp add: setsum_right_distrib setsum_left_distrib mult_ac)
  12.291 +    hence "Bseq (\<lambda>n. (\<Sum>k=Suc 0..<Suc n. 2^k * f (2^k)) + f 1)"
  12.292 +      by (intro Bseq_add, subst setsum_shift_bounds_Suc_ivl) (simp add: atLeast0LessThan)
  12.293 +    hence "Bseq (\<lambda>n. (\<Sum>k=0..<Suc n. 2^k * f (2^k)))"
  12.294 +      by (subst setsum_head_upt_Suc) (simp_all add: add_ac)
  12.295 +    thus "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))" 
  12.296 +      by (subst (asm) Bseq_Suc_iff) (simp add: atLeast0LessThan)
  12.297 +  next
  12.298 +    assume A: "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
  12.299 +    have "eventually (\<lambda>n. norm (\<Sum>k=1..<2^n. f k) \<le> norm (\<Sum>k<n. 2^k * f (2^k))) sequentially"
  12.300 +    proof (intro always_eventually allI)
  12.301 +      fix n :: nat
  12.302 +      have "norm (\<Sum>k=1..<2^n. f k) = (\<Sum>k=1..<2^n. f k)" unfolding real_norm_def
  12.303 +        by (intro abs_of_nonneg setsum_nonneg ballI mult_nonneg_nonneg nonneg)
  12.304 +      also have "\<dots> \<le> (\<Sum>k<n. 2^k * f (2^k))"
  12.305 +        by (subst condensation_condense1 [symmetric]) (intro condensation_inequality mono')
  12.306 +      also have "\<dots> = norm \<dots>" unfolding real_norm_def
  12.307 +        by (intro abs_of_nonneg [symmetric] setsum_nonneg ballI mult_nonneg_nonneg nonneg) simp_all
  12.308 +      finally show "norm (\<Sum>k=1..<2^n. f k) \<le> norm (\<Sum>k<n. 2^k * f (2^k))" .
  12.309 +    qed
  12.310 +    from this and A show "Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)" by (rule Bseq_eventually_mono)
  12.311 +  qed
  12.312 +  also have "monoseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
  12.313 +    by (intro mono_SucI1) (auto intro!: mult_nonneg_nonneg nonneg)
  12.314 +  hence "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k))) \<longleftrightarrow> convergent (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
  12.315 +    by (rule monoseq_imp_convergent_iff_Bseq [symmetric])
  12.316 +  also have "\<dots> \<longleftrightarrow> summable (\<lambda>k. 2^k * f (2^k))" by (simp only: summable_iff_convergent)
  12.317 +  finally show ?thesis .
  12.318 +qed
  12.319 +
  12.320 +end
  12.321 +
  12.322 +
  12.323 +subsection \<open>Summability of powers\<close>
  12.324 +
  12.325 +lemma abs_summable_complex_powr_iff: 
  12.326 +    "summable (\<lambda>n. norm (exp (of_real (ln (of_nat n)) * s))) \<longleftrightarrow> Re s < -1"
  12.327 +proof (cases "Re s \<le> 0")
  12.328 +  let ?l = "\<lambda>n. complex_of_real (ln (of_nat n))"
  12.329 +  case False
  12.330 +  with eventually_gt_at_top[of "0::nat"]
  12.331 +    have "eventually (\<lambda>n. norm (1 :: real) \<le> norm (exp (?l n * s))) sequentially" 
  12.332 +    by (auto intro!: ge_one_powr_ge_zero elim!: eventually_mono)
  12.333 +  from summable_comparison_test_ev[OF this] False show ?thesis by (auto simp: summable_const_iff)
  12.334 +next
  12.335 +  let ?l = "\<lambda>n. complex_of_real (ln (of_nat n))"
  12.336 +  case True
  12.337 +  hence "summable (\<lambda>n. norm (exp (?l n * s))) \<longleftrightarrow> summable (\<lambda>n. 2^n * norm (exp (?l (2^n) * s)))"
  12.338 +    by (intro condensation_test) (auto intro!: mult_right_mono_neg)
  12.339 +  also have "(\<lambda>n. 2^n * norm (exp (?l (2^n) * s))) = (\<lambda>n. (2 powr (Re s + 1)) ^ n)"
  12.340 +  proof
  12.341 +    fix n :: nat
  12.342 +    have "2^n * norm (exp (?l (2^n) * s)) = exp (real n * ln 2) * exp (real n * ln 2 * Re s)"
  12.343 +      using True by (subst exp_of_nat_mult) (simp add: ln_realpow algebra_simps) 
  12.344 +    also have "\<dots> = exp (real n * (ln 2 * (Re s + 1)))"
  12.345 +      by (simp add: algebra_simps exp_add)
  12.346 +    also have "\<dots> = exp (ln 2 * (Re s + 1)) ^ n" by (subst exp_of_nat_mult) simp
  12.347 +    also have "exp (ln 2 * (Re s + 1)) = 2 powr (Re s + 1)" by (simp add: powr_def)
  12.348 +    finally show "2^n * norm (exp (?l (2^n) * s)) = (2 powr (Re s + 1)) ^ n" .
  12.349 +  qed
  12.350 +  also have "summable \<dots> \<longleftrightarrow> 2 powr (Re s + 1) < 2 powr 0"
  12.351 +    by (subst summable_geometric_iff) simp
  12.352 +  also have "\<dots> \<longleftrightarrow> Re s < -1" by (subst powr_less_cancel_iff) (simp, linarith)
  12.353 +  finally show ?thesis .
  12.354 +qed
  12.355 +
  12.356 +lemma summable_complex_powr_iff: 
  12.357 +  assumes "Re s < -1"
  12.358 +  shows   "summable (\<lambda>n. exp (of_real (ln (of_nat n)) * s))"
  12.359 +  by (rule summable_norm_cancel, subst abs_summable_complex_powr_iff) fact
  12.360 +
  12.361 +lemma summable_real_powr_iff: "summable (\<lambda>n. of_nat n powr s :: real) \<longleftrightarrow> s < -1"
  12.362 +proof -
  12.363 +  from eventually_gt_at_top[of "0::nat"]
  12.364 +    have "summable (\<lambda>n. of_nat n powr s) \<longleftrightarrow> summable (\<lambda>n. exp (ln (of_nat n) * s))"
  12.365 +    by (intro summable_cong) (auto elim!: eventually_mono simp: powr_def)
  12.366 +  also have "\<dots> \<longleftrightarrow> s < -1" using abs_summable_complex_powr_iff[of "of_real s"] by simp
  12.367 +  finally show ?thesis .
  12.368 +qed
  12.369 +
  12.370 +lemma inverse_power_summable:
  12.371 +  assumes s: "s \<ge> 2"
  12.372 +  shows "summable (\<lambda>n. inverse (of_nat n ^ s :: 'a :: {real_normed_div_algebra,banach}))"
  12.373 +proof (rule summable_norm_cancel, subst summable_cong)
  12.374 +  from eventually_gt_at_top[of "0::nat"]
  12.375 +    show "eventually (\<lambda>n. norm (inverse (of_nat n ^ s:: 'a)) = real_of_nat n powr (-real s)) at_top"
  12.376 +    by eventually_elim (simp add: norm_inverse norm_power powr_minus powr_realpow)
  12.377 +qed (insert s summable_real_powr_iff[of "-s"], simp_all)
  12.378 +
  12.379 +lemma not_summable_harmonic: "\<not>summable (\<lambda>n. inverse (of_nat n) :: 'a :: real_normed_field)"
  12.380 +proof
  12.381 +  assume "summable (\<lambda>n. inverse (of_nat n) :: 'a)"
  12.382 +  hence "convergent (\<lambda>n. norm (of_real (\<Sum>k<n. inverse (of_nat k)) :: 'a))" 
  12.383 +    by (simp add: summable_iff_convergent convergent_norm)
  12.384 +  hence "convergent (\<lambda>n. abs (\<Sum>k<n. inverse (of_nat k)) :: real)" by (simp only: norm_of_real)
  12.385 +  also have "(\<lambda>n. abs (\<Sum>k<n. inverse (of_nat k)) :: real) = (\<lambda>n. \<Sum>k<n. inverse (of_nat k))"
  12.386 +    by (intro ext abs_of_nonneg setsum_nonneg) auto
  12.387 +  also have "convergent \<dots> \<longleftrightarrow> summable (\<lambda>k. inverse (of_nat k) :: real)"
  12.388 +    by (simp add: summable_iff_convergent)
  12.389 +  finally show False using summable_real_powr_iff[of "-1"] by (simp add: powr_minus)
  12.390 +qed
  12.391 +
  12.392 +
  12.393 +subsection \<open>Kummer's test\<close>
  12.394 +
  12.395 +lemma kummers_test_convergence:
  12.396 +  fixes f p :: "nat \<Rightarrow> real"
  12.397 +  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially" 
  12.398 +  assumes nonneg_p: "eventually (\<lambda>n. p n \<ge> 0) sequentially"
  12.399 +  defines "l \<equiv> liminf (\<lambda>n. ereal (p n * f n / f (Suc n) - p (Suc n)))"
  12.400 +  assumes l: "l > 0"
  12.401 +  shows   "summable f"
  12.402 +  unfolding summable_iff_convergent'
  12.403 +proof -
  12.404 +  def r \<equiv> "(if l = \<infinity> then 1 else real_of_ereal l / 2)"
  12.405 +  from l have "r > 0 \<and> of_real r < l" by (cases l) (simp_all add: r_def)
  12.406 +  hence r: "r > 0" "of_real r < l" by simp_all
  12.407 +  hence "eventually (\<lambda>n. p n * f n / f (Suc n) - p (Suc n) > r) sequentially"
  12.408 +    unfolding l_def by (force dest: less_LiminfD)
  12.409 +  moreover from pos_f have "eventually (\<lambda>n. f (Suc n) > 0) sequentially" 
  12.410 +    by (subst eventually_sequentially_Suc)
  12.411 +  ultimately have "eventually (\<lambda>n. p n * f n - p (Suc n) * f (Suc n) > r * f (Suc n)) sequentially"
  12.412 +    by eventually_elim (simp add: field_simps)
  12.413 +  from eventually_conj[OF pos_f eventually_conj[OF nonneg_p this]]
  12.414 +    obtain m where m: "\<And>n. n \<ge> m \<Longrightarrow> f n > 0" "\<And>n. n \<ge> m \<Longrightarrow> p n \<ge> 0"
  12.415 +        "\<And>n. n \<ge> m \<Longrightarrow> p n * f n - p (Suc n) * f (Suc n) > r * f (Suc n)"
  12.416 +    unfolding eventually_at_top_linorder by blast
  12.417 +
  12.418 +  let ?c = "(norm (\<Sum>k\<le>m. r * f k) + p m * f m) / r"
  12.419 +  have "Bseq (\<lambda>n. (\<Sum>k\<le>n + Suc m. f k))"
  12.420 +  proof (rule BseqI')
  12.421 +    fix k :: nat
  12.422 +    def n \<equiv> "k + Suc m"
  12.423 +    have n: "n > m" by (simp add: n_def)
  12.424 +
  12.425 +    from r have "r * norm (\<Sum>k\<le>n. f k) = norm (\<Sum>k\<le>n. r * f k)"
  12.426 +      by (simp add: setsum_right_distrib[symmetric] abs_mult)
  12.427 +    also from n have "{..n} = {..m} \<union> {Suc m..n}" by auto
  12.428 +    hence "(\<Sum>k\<le>n. r * f k) = (\<Sum>k\<in>{..m} \<union> {Suc m..n}. r * f k)" by (simp only:)
  12.429 +    also have "\<dots> = (\<Sum>k\<le>m. r * f k) + (\<Sum>k=Suc m..n. r * f k)"
  12.430 +      by (subst setsum.union_disjoint) auto
  12.431 +    also have "norm \<dots> \<le> norm (\<Sum>k\<le>m. r * f k) + norm (\<Sum>k=Suc m..n. r * f k)"
  12.432 +      by (rule norm_triangle_ineq)
  12.433 +    also from r less_imp_le[OF m(1)] have "(\<Sum>k=Suc m..n. r * f k) \<ge> 0" 
  12.434 +      by (intro setsum_nonneg) auto
  12.435 +    hence "norm (\<Sum>k=Suc m..n. r * f k) = (\<Sum>k=Suc m..n. r * f k)" by simp
  12.436 +    also have "(\<Sum>k=Suc m..n. r * f k) = (\<Sum>k=m..<n. r * f (Suc k))"
  12.437 +     by (subst setsum_shift_bounds_Suc_ivl [symmetric])
  12.438 +          (simp only: atLeastLessThanSuc_atLeastAtMost)
  12.439 +    also from m have "\<dots> \<le> (\<Sum>k=m..<n. p k * f k - p (Suc k) * f (Suc k))"
  12.440 +      by (intro setsum_mono[OF less_imp_le]) simp_all
  12.441 +    also have "\<dots> = -(\<Sum>k=m..<n. p (Suc k) * f (Suc k) - p k * f k)"
  12.442 +      by (simp add: setsum_negf [symmetric] algebra_simps)
  12.443 +    also from n have "\<dots> = p m * f m - p n * f n"
  12.444 +      by (cases n, simp, simp only: atLeastLessThanSuc_atLeastAtMost, subst setsum_Suc_diff) simp_all
  12.445 +    also from less_imp_le[OF m(1)] m(2) n have "\<dots> \<le> p m * f m" by simp
  12.446 +    finally show "norm (\<Sum>k\<le>n. f k) \<le> (norm (\<Sum>k\<le>m. r * f k) + p m * f m) / r" using r
  12.447 +      by (subst pos_le_divide_eq[OF r(1)]) (simp only: mult_ac)
  12.448 +  qed
  12.449 +  moreover have "(\<Sum>k\<le>n. f k) \<le> (\<Sum>k\<le>n'. f k)" if "Suc m \<le> n" "n \<le> n'" for n n'
  12.450 +    using less_imp_le[OF m(1)] that by (intro setsum_mono2) auto
  12.451 +  ultimately show "convergent (\<lambda>n. \<Sum>k\<le>n. f k)" by (rule Bseq_monoseq_convergent'_inc)
  12.452 +qed
  12.453 +
  12.454 +
  12.455 +lemma kummers_test_divergence:
  12.456 +  fixes f p :: "nat \<Rightarrow> real"
  12.457 +  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially" 
  12.458 +  assumes pos_p: "eventually (\<lambda>n. p n > 0) sequentially"
  12.459 +  assumes divergent_p: "\<not>summable (\<lambda>n. inverse (p n))"
  12.460 +  defines "l \<equiv> limsup (\<lambda>n. ereal (p n * f n / f (Suc n) - p (Suc n)))"
  12.461 +  assumes l: "l < 0"
  12.462 +  shows   "\<not>summable f"
  12.463 +proof
  12.464 +  assume "summable f"
  12.465 +  from eventually_conj[OF pos_f eventually_conj[OF pos_p Limsup_lessD[OF l[unfolded l_def]]]]
  12.466 +    obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> p n > 0" "\<And>n. n \<ge> N \<Longrightarrow> f n > 0"
  12.467 +                      "\<And>n. n \<ge> N \<Longrightarrow> p n * f n / f (Suc n) - p (Suc n) < 0"
  12.468 +    by (auto simp: eventually_at_top_linorder)
  12.469 +  hence A: "p n * f n < p (Suc n) * f (Suc n)" if "n \<ge> N" for n using that N[of n] N[of "Suc n"] 
  12.470 +    by (simp add: field_simps)
  12.471 +  have "p n * f n \<ge> p N * f N" if "n \<ge> N" for n using that and A
  12.472 +      by (induction n rule: dec_induct) (auto intro!: less_imp_le elim!: order.trans)
  12.473 +  from eventually_ge_at_top[of N] N this
  12.474 +    have "eventually (\<lambda>n. norm (p N * f N * inverse (p n)) \<le> f n) sequentially"
  12.475 +    by (auto elim!: eventually_mono simp: field_simps abs_of_pos)
  12.476 +  from this and \<open>summable f\<close> have "summable (\<lambda>n. p N * f N * inverse (p n))"
  12.477 +    by (rule summable_comparison_test_ev)
  12.478 +  from summable_mult[OF this, of "inverse (p N * f N)"] N[OF le_refl] 
  12.479 +    have "summable (\<lambda>n. inverse (p n))" by (simp add: divide_simps)
  12.480 +  with divergent_p show False by contradiction
  12.481 +qed
  12.482 +
  12.483 +
  12.484 +subsection \<open>Ratio test\<close>
  12.485 +
  12.486 +lemma ratio_test_convergence:
  12.487 +  fixes f :: "nat \<Rightarrow> real"
  12.488 +  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially" 
  12.489 +  defines "l \<equiv> liminf (\<lambda>n. ereal (f n / f (Suc n)))"
  12.490 +  assumes l: "l > 1"
  12.491 +  shows   "summable f"
  12.492 +proof (rule kummers_test_convergence[OF pos_f])
  12.493 +  note l
  12.494 +  also have "l = liminf (\<lambda>n. ereal (f n / f (Suc n) - 1)) + 1" 
  12.495 +    by (subst Liminf_add_ereal_right[symmetric]) (simp_all add: minus_ereal_def l_def one_ereal_def)
  12.496 +  finally show "liminf (\<lambda>n. ereal (1 * f n / f (Suc n) - 1)) > 0"
  12.497 +    by (cases "liminf (\<lambda>n. ereal (1 * f n / f (Suc n) - 1))") simp_all
  12.498 +qed simp
  12.499 +
  12.500 +lemma ratio_test_divergence:
  12.501 +  fixes f :: "nat \<Rightarrow> real"
  12.502 +  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially" 
  12.503 +  defines "l \<equiv> limsup (\<lambda>n. ereal (f n / f (Suc n)))"
  12.504 +  assumes l: "l < 1"
  12.505 +  shows   "\<not>summable f"
  12.506 +proof (rule kummers_test_divergence[OF pos_f])
  12.507 +  have "limsup (\<lambda>n. ereal (f n / f (Suc n) - 1)) + 1 = l" 
  12.508 +    by (subst Limsup_add_ereal_right[symmetric]) (simp_all add: minus_ereal_def l_def one_ereal_def)
  12.509 +  also note l
  12.510 +  finally show "limsup (\<lambda>n. ereal (1 * f n / f (Suc n) - 1)) < 0"
  12.511 +    by (cases "limsup (\<lambda>n. ereal (1 * f n / f (Suc n) - 1))") simp_all
  12.512 +qed (simp_all add: summable_const_iff)
  12.513 +
  12.514 +
  12.515 +subsection \<open>Raabe's test\<close>
  12.516 +
  12.517 +lemma raabes_test_convergence:
  12.518 +fixes f :: "nat \<Rightarrow> real"
  12.519 +  assumes pos: "eventually (\<lambda>n. f n > 0) sequentially"
  12.520 +  defines "l \<equiv> liminf (\<lambda>n. ereal (of_nat n * (f n / f (Suc n) - 1)))"
  12.521 +  assumes l: "l > 1"
  12.522 +  shows   "summable f"
  12.523 +proof (rule kummers_test_convergence)
  12.524 +  let ?l' = "liminf (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)))"
  12.525 +  have "1 < l" by fact
  12.526 +  also have "l = liminf (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)) + 1)"
  12.527 +    by (simp add: l_def algebra_simps)
  12.528 +  also have "\<dots> = ?l' + 1" by (subst Liminf_add_ereal_right) simp_all
  12.529 +  finally show "?l' > 0" by (cases ?l') (simp_all add: algebra_simps)
  12.530 +qed (simp_all add: pos)
  12.531 +
  12.532 +lemma raabes_test_divergence:
  12.533 +fixes f :: "nat \<Rightarrow> real"
  12.534 +  assumes pos: "eventually (\<lambda>n. f n > 0) sequentially"
  12.535 +  defines "l \<equiv> limsup (\<lambda>n. ereal (of_nat n * (f n / f (Suc n) - 1)))"
  12.536 +  assumes l: "l < 1"
  12.537 +  shows   "\<not>summable f"
  12.538 +proof (rule kummers_test_divergence)
  12.539 +  let ?l' = "limsup (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)))"
  12.540 +  note l
  12.541 +  also have "l = limsup (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)) + 1)"
  12.542 +    by (simp add: l_def algebra_simps)
  12.543 +  also have "\<dots> = ?l' + 1" by (subst Limsup_add_ereal_right) simp_all
  12.544 +  finally show "?l' < 0" by (cases ?l') (simp_all add: algebra_simps)
  12.545 +qed (insert pos eventually_gt_at_top[of "0::nat"] not_summable_harmonic, simp_all)
  12.546 +
  12.547 +
  12.548 +
  12.549 +subsection \<open>Radius of convergence\<close>
  12.550 +
  12.551 +text \<open>
  12.552 +  The radius of convergence of a power series. This value always exists, ranges from
  12.553 +  @{term "0::ereal"} to @{term "\<infinity>::ereal"}, and the power series is guaranteed to converge for 
  12.554 +  all inputs with a norm that is smaller than that radius and to diverge for all inputs with a
  12.555 +  norm that is greater. 
  12.556 +\<close>
  12.557 +definition conv_radius :: "(nat \<Rightarrow> 'a :: banach) \<Rightarrow> ereal" where
  12.558 +  "conv_radius f = inverse (limsup (\<lambda>n. ereal (root n (norm (f n)))))"
  12.559 +
  12.560 +lemma conv_radius_nonneg: "conv_radius f \<ge> 0"
  12.561 +proof -
  12.562 +  have "0 = limsup (\<lambda>n. 0)" by (subst Limsup_const) simp_all
  12.563 +  also have "\<dots> \<le> limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.564 +    by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
  12.565 +  finally show ?thesis
  12.566 +    unfolding conv_radius_def by (auto simp: ereal_inverse_nonneg_iff)
  12.567 +qed
  12.568 +
  12.569 +lemma conv_radius_zero [simp]: "conv_radius (\<lambda>_. 0) = \<infinity>"
  12.570 +  by (auto simp: conv_radius_def zero_ereal_def [symmetric] Limsup_const)
  12.571 +
  12.572 +lemma conv_radius_cong:
  12.573 +  assumes "eventually (\<lambda>x. f x = g x) sequentially"
  12.574 +  shows   "conv_radius f = conv_radius g"
  12.575 +proof -
  12.576 +  have "eventually (\<lambda>n. ereal (root n (norm (f n))) = ereal (root n (norm (g n)))) sequentially"
  12.577 +    using assms by eventually_elim simp
  12.578 +  from Limsup_eq[OF this] show ?thesis unfolding conv_radius_def by simp
  12.579 +qed
  12.580 +
  12.581 +lemma conv_radius_altdef:
  12.582 +  "conv_radius f = liminf (\<lambda>n. inverse (ereal (root n (norm (f n)))))"
  12.583 +  by (subst Liminf_inverse_ereal) (simp_all add: real_root_ge_zero conv_radius_def)
  12.584 +
  12.585 +
  12.586 +lemma abs_summable_in_conv_radius:
  12.587 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.588 +  assumes "ereal (norm z) < conv_radius f"
  12.589 +  shows   "summable (\<lambda>n. norm (f n * z ^ n))"
  12.590 +proof (rule root_test_convergence')
  12.591 +  def l \<equiv> "limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.592 +  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
  12.593 +  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
  12.594 +  finally have l_nonneg: "l \<ge> 0" .
  12.595 +
  12.596 +  have "limsup (\<lambda>n. root n (norm (f n * z^n))) = l * ereal (norm z)" unfolding l_def
  12.597 +    by (rule limsup_root_powser)
  12.598 +  also from l_nonneg consider "l = 0" | "l = \<infinity>" | "\<exists>l'. l = ereal l' \<and> l' > 0"
  12.599 +    by (cases "l") (auto simp: less_le)
  12.600 +  hence "l * ereal (norm z) < 1"
  12.601 +  proof cases
  12.602 +    assume "l = \<infinity>"
  12.603 +    hence "conv_radius f = 0" unfolding conv_radius_def l_def by simp
  12.604 +    with assms show ?thesis by simp
  12.605 +  next
  12.606 +    assume "\<exists>l'. l = ereal l' \<and> l' > 0"
  12.607 +    then guess l' by (elim exE conjE) note l' = this
  12.608 +    hence "l \<noteq> \<infinity>" by auto
  12.609 +    have "l * ereal (norm z) < l * conv_radius f"
  12.610 +      by (intro ereal_mult_strict_left_mono) (simp_all add: l' assms)
  12.611 +    also have "conv_radius f = inverse l" by (simp add: conv_radius_def l_def)
  12.612 +    also from l' have "l * inverse l = 1" by simp
  12.613 +    finally show ?thesis .
  12.614 +  qed simp_all
  12.615 +  finally show "limsup (\<lambda>n. ereal (root n (norm (norm (f n * z ^ n))))) < 1" by simp
  12.616 +qed
  12.617 +
  12.618 +lemma summable_in_conv_radius:
  12.619 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.620 +  assumes "ereal (norm z) < conv_radius f"
  12.621 +  shows   "summable (\<lambda>n. f n * z ^ n)"
  12.622 +  by (rule summable_norm_cancel, rule abs_summable_in_conv_radius) fact+
  12.623 +
  12.624 +lemma not_summable_outside_conv_radius:
  12.625 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.626 +  assumes "ereal (norm z) > conv_radius f"
  12.627 +  shows   "\<not>summable (\<lambda>n. f n * z ^ n)"
  12.628 +proof (rule root_test_divergence)
  12.629 +  def l \<equiv> "limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.630 +  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
  12.631 +  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
  12.632 +  finally have l_nonneg: "l \<ge> 0" .
  12.633 +  from assms have l_nz: "l \<noteq> 0" unfolding conv_radius_def l_def by auto
  12.634 +
  12.635 +  have "limsup (\<lambda>n. ereal (root n (norm (f n * z^n)))) = l * ereal (norm z)"
  12.636 +    unfolding l_def by (rule limsup_root_powser)
  12.637 +  also have "... > 1"
  12.638 +  proof (cases l)
  12.639 +    assume "l = \<infinity>"
  12.640 +    with assms conv_radius_nonneg[of f] show ?thesis
  12.641 +      by (auto simp: zero_ereal_def[symmetric])
  12.642 +  next
  12.643 +    fix l' assume l': "l = ereal l'"
  12.644 +    from l_nonneg l_nz have "1 = l * inverse l" by (auto simp: l' field_simps)
  12.645 +    also from l_nz have "inverse l = conv_radius f" 
  12.646 +      unfolding l_def conv_radius_def by auto
  12.647 +    also from l' l_nz l_nonneg assms have "l * \<dots> < l * ereal (norm z)"
  12.648 +      by (intro ereal_mult_strict_left_mono) (auto simp: l')
  12.649 +    finally show ?thesis .
  12.650 +  qed (insert l_nonneg, simp_all)
  12.651 +  finally show "limsup (\<lambda>n. ereal (root n (norm (f n * z^n)))) > 1" .
  12.652 +qed
  12.653 +
  12.654 +
  12.655 +lemma conv_radius_geI:
  12.656 +  assumes "summable (\<lambda>n. f n * z ^ n :: 'a :: {banach, real_normed_div_algebra})"
  12.657 +  shows   "conv_radius f \<ge> norm z"
  12.658 +  using not_summable_outside_conv_radius[of f z] assms by (force simp: not_le[symmetric])
  12.659 +
  12.660 +lemma conv_radius_leI:
  12.661 +  assumes "\<not>summable (\<lambda>n. norm (f n * z ^ n :: 'a :: {banach, real_normed_div_algebra}))"
  12.662 +  shows   "conv_radius f \<le> norm z"
  12.663 +  using abs_summable_in_conv_radius[of z f] assms by (force simp: not_le[symmetric])
  12.664 +
  12.665 +lemma conv_radius_leI':
  12.666 +  assumes "\<not>summable (\<lambda>n. f n * z ^ n :: 'a :: {banach, real_normed_div_algebra})"
  12.667 +  shows   "conv_radius f \<le> norm z"
  12.668 +  using summable_in_conv_radius[of z f] assms by (force simp: not_le[symmetric])
  12.669 +
  12.670 +lemma conv_radius_geI_ex:
  12.671 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.672 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
  12.673 +  shows   "conv_radius f \<ge> R"
  12.674 +proof (rule linorder_cases[of "conv_radius f" R])
  12.675 +  assume R: "conv_radius f < R"
  12.676 +  with conv_radius_nonneg[of f] obtain conv_radius' 
  12.677 +    where [simp]: "conv_radius f = ereal conv_radius'"
  12.678 +    by (cases "conv_radius f") simp_all
  12.679 +  def r \<equiv> "if R = \<infinity> then conv_radius' + 1 else (real_of_ereal R + conv_radius') / 2"
  12.680 +  from R conv_radius_nonneg[of f] have "0 < r \<and> ereal r < R \<and> ereal r > conv_radius f" 
  12.681 +    unfolding r_def by (cases R) (auto simp: r_def field_simps)
  12.682 +  with assms(1)[of r] obtain z where "norm z > conv_radius f" "summable (\<lambda>n. f n * z^n)" by auto
  12.683 +  with not_summable_outside_conv_radius[of f z] show ?thesis by simp
  12.684 +qed simp_all
  12.685 +
  12.686 +lemma conv_radius_geI_ex':
  12.687 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.688 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> summable (\<lambda>n. f n * of_real r^n)"
  12.689 +  shows   "conv_radius f \<ge> R"
  12.690 +proof (rule conv_radius_geI_ex)
  12.691 +  fix r assume "0 < r" "ereal r < R"
  12.692 +  with assms[of r] show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z ^ n)"
  12.693 +    by (intro exI[of _ "of_real r :: 'a"]) auto
  12.694 +qed
  12.695 +
  12.696 +lemma conv_radius_leI_ex:
  12.697 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.698 +  assumes "R \<ge> 0"
  12.699 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z^n))"
  12.700 +  shows   "conv_radius f \<le> R"
  12.701 +proof (rule linorder_cases[of "conv_radius f" R])
  12.702 +  assume R: "conv_radius f > R"
  12.703 +  from R assms(1) obtain R' where R': "R = ereal R'" by (cases R) simp_all
  12.704 +  def r \<equiv> "if conv_radius f = \<infinity> then R' + 1 else (R' + real_of_ereal (conv_radius f)) / 2"
  12.705 +  from R conv_radius_nonneg[of f] have "r > R \<and> r < conv_radius f" unfolding r_def
  12.706 +    by (cases "conv_radius f") (auto simp: r_def field_simps R')
  12.707 +  with assms(1) assms(2)[of r] R' 
  12.708 +    obtain z where "norm z < conv_radius f" "\<not>summable (\<lambda>n. norm (f n * z^n))" by auto
  12.709 +  with abs_summable_in_conv_radius[of z f] show ?thesis by auto
  12.710 +qed simp_all
  12.711 +
  12.712 +lemma conv_radius_leI_ex':
  12.713 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.714 +  assumes "R \<ge> 0"
  12.715 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<not>summable (\<lambda>n. f n * of_real r^n)"
  12.716 +  shows   "conv_radius f \<le> R"
  12.717 +proof (rule conv_radius_leI_ex)
  12.718 +  fix r assume "0 < r" "ereal r > R"
  12.719 +  with assms(2)[of r] show "\<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z ^ n))"
  12.720 +    by (intro exI[of _ "of_real r :: 'a"]) (auto dest: summable_norm_cancel)
  12.721 +qed fact+
  12.722 +
  12.723 +lemma conv_radius_eqI:
  12.724 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.725 +  assumes "R \<ge> 0"
  12.726 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
  12.727 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z^n))"
  12.728 +  shows   "conv_radius f = R"
  12.729 +  by (intro antisym conv_radius_geI_ex conv_radius_leI_ex assms)
  12.730 +
  12.731 +lemma conv_radius_eqI':
  12.732 +  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
  12.733 +  assumes "R \<ge> 0"
  12.734 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> summable (\<lambda>n. f n * (of_real r)^n)"
  12.735 +  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<not>summable (\<lambda>n. norm (f n * (of_real r)^n))"
  12.736 +  shows   "conv_radius f = R"
  12.737 +proof (intro conv_radius_eqI[OF assms(1)])
  12.738 +  fix r assume "0 < r" "ereal r < R" with assms(2)[OF this] 
  12.739 +    show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z ^ n)" by force
  12.740 +next
  12.741 +  fix r assume "0 < r" "ereal r > R" with assms(3)[OF this] 
  12.742 +    show "\<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z ^ n))" by force  
  12.743 +qed
  12.744 +
  12.745 +lemma conv_radius_zeroI:
  12.746 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.747 +  assumes "\<And>z. z \<noteq> 0 \<Longrightarrow> \<not>summable (\<lambda>n. f n * z^n)"
  12.748 +  shows   "conv_radius f = 0"
  12.749 +proof (rule ccontr)
  12.750 +  assume "conv_radius f \<noteq> 0"
  12.751 +  with conv_radius_nonneg[of f] have pos: "conv_radius f > 0" by simp
  12.752 +  def r \<equiv> "if conv_radius f = \<infinity> then 1 else real_of_ereal (conv_radius f) / 2"
  12.753 +  from pos have r: "ereal r > 0 \<and> ereal r < conv_radius f" 
  12.754 +    by (cases "conv_radius f") (simp_all add: r_def)
  12.755 +  hence "summable (\<lambda>n. f n * of_real r ^ n)" by (intro summable_in_conv_radius) simp
  12.756 +  moreover from r and assms[of "of_real r"] have "\<not>summable (\<lambda>n. f n * of_real r ^ n)" by simp
  12.757 +  ultimately show False by contradiction
  12.758 +qed
  12.759 +
  12.760 +lemma conv_radius_inftyI':
  12.761 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.762 +  assumes "\<And>r. r > c \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
  12.763 +  shows   "conv_radius f = \<infinity>"
  12.764 +proof -
  12.765 +  {
  12.766 +    fix r :: real
  12.767 +    have "max r (c + 1) > c" by (auto simp: max_def)
  12.768 +    from assms[OF this] obtain z where "norm z = max r (c + 1)" "summable (\<lambda>n. f n * z^n)" by blast
  12.769 +    from conv_radius_geI[OF this(2)] this(1) have "conv_radius f \<ge> r" by simp
  12.770 +  }
  12.771 +  from this[of "real_of_ereal (conv_radius f + 1)"] show "conv_radius f = \<infinity>"
  12.772 +    by (cases "conv_radius f") simp_all
  12.773 +qed
  12.774 +
  12.775 +lemma conv_radius_inftyI:
  12.776 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.777 +  assumes "\<And>r. \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
  12.778 +  shows   "conv_radius f = \<infinity>"
  12.779 +  using assms by (rule conv_radius_inftyI')
  12.780 +
  12.781 +lemma conv_radius_inftyI'':
  12.782 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.783 +  assumes "\<And>z. summable (\<lambda>n. f n * z^n)"
  12.784 +  shows   "conv_radius f = \<infinity>"
  12.785 +proof (rule conv_radius_inftyI')
  12.786 +  fix r :: real assume "r > 0"
  12.787 +  with assms show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
  12.788 +    by (intro exI[of _ "of_real r"]) simp
  12.789 +qed
  12.790 +
  12.791 +lemma conv_radius_ratio_limit_ereal:
  12.792 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.793 +  assumes nz:  "eventually (\<lambda>n. f n \<noteq> 0) sequentially"
  12.794 +  assumes lim: "(\<lambda>n. ereal (norm (f n) / norm (f (Suc n)))) \<longlonglongrightarrow> c"
  12.795 +  shows   "conv_radius f = c"
  12.796 +proof (rule conv_radius_eqI')
  12.797 +  show "c \<ge> 0" by (intro Lim_bounded2_ereal[OF lim]) simp_all
  12.798 +next
  12.799 +  fix r assume r: "0 < r" "ereal r < c"
  12.800 +  let ?l = "liminf (\<lambda>n. ereal (norm (f n * of_real r ^ n) / norm (f (Suc n) * of_real r ^ Suc n)))"
  12.801 +  have "?l = liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n)))) * ereal (inverse r))"
  12.802 +    using r by (simp add: norm_mult norm_power divide_simps)
  12.803 +  also from r have "\<dots> = liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) * ereal (inverse r)"
  12.804 +    by (intro Liminf_ereal_mult_right) simp_all
  12.805 +  also have "liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) = c"
  12.806 +    by (intro lim_imp_Liminf lim) simp
  12.807 +  finally have l: "?l = c * ereal (inverse r)" by simp
  12.808 +  from r have  l': "c * ereal (inverse r) > 1" by (cases c) (simp_all add: field_simps)
  12.809 +  show "summable (\<lambda>n. f n * of_real r^n)"
  12.810 +    by (rule summable_norm_cancel, rule ratio_test_convergence)
  12.811 +       (insert r nz l l', auto elim!: eventually_mono)
  12.812 +next
  12.813 +  fix r assume r: "0 < r" "ereal r > c"
  12.814 +  let ?l = "limsup (\<lambda>n. ereal (norm (f n * of_real r ^ n) / norm (f (Suc n) * of_real r ^ Suc n)))"
  12.815 +  have "?l = limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n)))) * ereal (inverse r))"
  12.816 +    using r by (simp add: norm_mult norm_power divide_simps)
  12.817 +  also from r have "\<dots> = limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) * ereal (inverse r)"
  12.818 +    by (intro Limsup_ereal_mult_right) simp_all
  12.819 +  also have "limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) = c"
  12.820 +    by (intro lim_imp_Limsup lim) simp
  12.821 +  finally have l: "?l = c * ereal (inverse r)" by simp
  12.822 +  from r have  l': "c * ereal (inverse r) < 1" by (cases c) (simp_all add: field_simps)
  12.823 +  show "\<not>summable (\<lambda>n. norm (f n * of_real r^n))"
  12.824 +    by (rule ratio_test_divergence) (insert r nz l l', auto elim!: eventually_mono)
  12.825 +qed
  12.826 +
  12.827 +lemma conv_radius_ratio_limit_ereal_nonzero:
  12.828 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.829 +  assumes nz:  "c \<noteq> 0"
  12.830 +  assumes lim: "(\<lambda>n. ereal (norm (f n) / norm (f (Suc n)))) \<longlonglongrightarrow> c"
  12.831 +  shows   "conv_radius f = c"
  12.832 +proof (rule conv_radius_ratio_limit_ereal[OF _ lim], rule ccontr)
  12.833 +  assume "\<not>eventually (\<lambda>n. f n \<noteq> 0) sequentially"
  12.834 +  hence "frequently (\<lambda>n. f n = 0) sequentially" by (simp add: frequently_def)
  12.835 +  hence "frequently (\<lambda>n. ereal (norm (f n) / norm (f (Suc n))) = 0) sequentially"
  12.836 +    by (force elim!: frequently_elim1)
  12.837 +  hence "c = 0" by (intro limit_frequently_eq[OF _ _ lim]) auto
  12.838 +  with nz show False by contradiction
  12.839 +qed 
  12.840 +
  12.841 +lemma conv_radius_ratio_limit:
  12.842 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.843 +  assumes "c' = ereal c"
  12.844 +  assumes nz:  "eventually (\<lambda>n. f n \<noteq> 0) sequentially"
  12.845 +  assumes lim: "(\<lambda>n. norm (f n) / norm (f (Suc n))) \<longlonglongrightarrow> c"
  12.846 +  shows   "conv_radius f = c'"
  12.847 +  using assms by (intro conv_radius_ratio_limit_ereal) simp_all
  12.848 +  
  12.849 +lemma conv_radius_ratio_limit_nonzero:
  12.850 +  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
  12.851 +  assumes "c' = ereal c"
  12.852 +  assumes nz:  "c \<noteq> 0"
  12.853 +  assumes lim: "(\<lambda>n. norm (f n) / norm (f (Suc n))) \<longlonglongrightarrow> c"
  12.854 +  shows   "conv_radius f = c'"
  12.855 +  using assms by (intro conv_radius_ratio_limit_ereal_nonzero) simp_all
  12.856 +
  12.857 +lemma conv_radius_mult_power: 
  12.858 +  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
  12.859 +  shows   "conv_radius (\<lambda>n. c ^ n * f n) = conv_radius f / ereal (norm c)"
  12.860 +proof - 
  12.861 +  have "limsup (\<lambda>n. ereal (root n (norm (c ^ n * f n)))) =
  12.862 +          limsup (\<lambda>n. ereal (norm c) * ereal (root n (norm (f n))))" 
  12.863 +    using eventually_gt_at_top[of "0::nat"]
  12.864 +    by (intro Limsup_eq) 
  12.865 +       (auto elim!: eventually_mono simp: norm_mult norm_power real_root_mult real_root_power)
  12.866 +  also have "\<dots> = ereal (norm c) * limsup (\<lambda>n. ereal (root n (norm (f n))))"
  12.867 +    using assms by (subst Limsup_ereal_mult_left[symmetric]) simp_all
  12.868 +  finally have A: "limsup (\<lambda>n. ereal (root n (norm (c ^ n * f n)))) = 
  12.869 +                       ereal (norm c) * limsup (\<lambda>n. ereal (root n (norm (f n))))" .
  12.870 +  show ?thesis using assms
  12.871 +    apply (cases "limsup (\<lambda>n. ereal (root n (norm (f n)))) = 0")
  12.872 +    apply (simp add: A conv_radius_def)
  12.873 +    apply (unfold conv_radius_def A divide_ereal_def, simp add: mult.commute ereal_inverse_mult)
  12.874 +    done
  12.875 +qed
  12.876 +
  12.877 +lemma conv_radius_mult_power_right: 
  12.878 +  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
  12.879 +  shows   "conv_radius (\<lambda>n. f n * c ^ n) = conv_radius f / ereal (norm c)"
  12.880 +  using conv_radius_mult_power[OF assms, of f]
  12.881 +  unfolding conv_radius_def by (simp add: mult.commute norm_mult)
  12.882 +
  12.883 +lemma conv_radius_divide_power: 
  12.884 +  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
  12.885 +  shows   "conv_radius (\<lambda>n. f n / c^n) = conv_radius f * ereal (norm c)"
  12.886 +proof - 
  12.887 +  from assms have "inverse c \<noteq> 0" by simp
  12.888 +  from conv_radius_mult_power_right[OF this, of f] show ?thesis
  12.889 +    by (simp add: divide_inverse divide_ereal_def assms norm_inverse power_inverse)
  12.890 +qed
  12.891 +
  12.892 +
  12.893 +lemma conv_radius_add_ge: 
  12.894 +  "min (conv_radius f) (conv_radius g) \<le> 
  12.895 +       conv_radius (\<lambda>x. f x + g x :: 'a :: {banach,real_normed_div_algebra})"
  12.896 +  by (rule conv_radius_geI_ex')
  12.897 +     (auto simp: algebra_simps intro!: summable_add summable_in_conv_radius)
  12.898 +
  12.899 +lemma conv_radius_mult_ge:
  12.900 +  fixes f g :: "nat \<Rightarrow> ('a :: {banach,real_normed_div_algebra})"
  12.901 +  shows "conv_radius (\<lambda>x. \<Sum>i\<le>x. f i * g (x - i)) \<ge> min (conv_radius f) (conv_radius g)"
  12.902 +proof (rule conv_radius_geI_ex')
  12.903 +  fix r assume r: "r > 0" "ereal r < min (conv_radius f) (conv_radius g)"
  12.904 +  from r have "summable (\<lambda>n. (\<Sum>i\<le>n. (f i * of_real r^i) * (g (n - i) * of_real r^(n - i))))"
  12.905 +    by (intro summable_Cauchy_product abs_summable_in_conv_radius) simp_all
  12.906 +  thus "summable (\<lambda>n. (\<Sum>i\<le>n. f i * g (n - i)) * of_real r ^ n)"
  12.907 +    by (simp add: algebra_simps of_real_def scaleR_power power_add [symmetric] scaleR_setsum_right)
  12.908 +qed
  12.909 +
  12.910 +end
    13.1 --- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Sun Jan 03 21:45:34 2016 +0100
    13.2 +++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Jan 04 17:45:36 2016 +0100
    13.3 @@ -5388,23 +5388,6 @@
    13.4      unfolding uniformly_continuous_on_def by blast
    13.5  qed
    13.6  
    13.7 -lemma continuous_on_tendsto_compose:
    13.8 -  assumes f_cont: "continuous_on s f"
    13.9 -  assumes g: "(g \<longlongrightarrow> l) F"
   13.10 -  assumes l: "l \<in> s"
   13.11 -  assumes ev: "\<forall>\<^sub>F x in F. g x \<in> s"
   13.12 -  shows "((\<lambda>x. f (g x)) \<longlongrightarrow> f l) F"
   13.13 -proof -
   13.14 -  from f_cont have f: "(f \<longlongrightarrow> f l) (at l within s)"
   13.15 -    by (auto simp: l continuous_on)
   13.16 -  have i: "((\<lambda>x. if g x = l then f l else f (g x)) \<longlongrightarrow> f l) F"
   13.17 -    by (rule filterlim_If)
   13.18 -      (auto intro!: filterlim_compose[OF f] eventually_conj tendsto_mono[OF _ g]
   13.19 -        simp: filterlim_at eventually_inf_principal eventually_mono[OF ev])
   13.20 -  show ?thesis
   13.21 -    by (rule filterlim_cong[THEN iffD1[OF _ i]]) auto
   13.22 -qed
   13.23 -
   13.24  
   13.25  text\<open>The usual transformation theorems.\<close>
   13.26  
    14.1 --- a/src/HOL/Real_Vector_Spaces.thy	Sun Jan 03 21:45:34 2016 +0100
    14.2 +++ b/src/HOL/Real_Vector_Spaces.thy	Mon Jan 04 17:45:36 2016 +0100
    14.3 @@ -191,7 +191,7 @@
    14.4    fixes x :: "'a::real_vector"
    14.5    shows "scaleR (-1) x = - x"
    14.6    using scaleR_minus_left [of 1 x] by simp
    14.7 -
    14.8 +  
    14.9  class real_algebra = real_vector + ring +
   14.10    assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
   14.11    and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
   14.12 @@ -686,6 +686,10 @@
   14.13  
   14.14  class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   14.15    assumes norm_one [simp]: "norm 1 = 1"
   14.16 +  
   14.17 +lemma (in real_normed_algebra_1) scaleR_power [simp]: 
   14.18 +  "(scaleR x y) ^ n = scaleR (x^n) (y^n)"
   14.19 +  by (induction n) (simp_all add: scaleR_one scaleR_scaleR mult_ac)
   14.20  
   14.21  class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   14.22    assumes norm_mult: "norm (x * y) = norm x * norm y"
    15.1 --- a/src/HOL/Series.thy	Sun Jan 03 21:45:34 2016 +0100
    15.2 +++ b/src/HOL/Series.thy	Mon Jan 04 17:45:36 2016 +0100
    15.3 @@ -791,6 +791,12 @@
    15.4    using a b
    15.5    by (rule Cauchy_product_sums [THEN sums_unique])
    15.6  
    15.7 +lemma summable_Cauchy_product:
    15.8 +  assumes "summable (\<lambda>k. norm (a k :: 'a :: {real_normed_algebra,banach}))" 
    15.9 +          "summable (\<lambda>k. norm (b k))"
   15.10 +  shows   "summable (\<lambda>k. \<Sum>i\<le>k. a i * b (k - i))"
   15.11 +  using Cauchy_product_sums[OF assms] by (simp add: sums_iff)  
   15.12 +
   15.13  subsection \<open>Series on @{typ real}s\<close>
   15.14  
   15.15  lemma summable_norm_comparison_test: "\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. norm (f n))"
    16.1 --- a/src/HOL/Topological_Spaces.thy	Sun Jan 03 21:45:34 2016 +0100
    16.2 +++ b/src/HOL/Topological_Spaces.thy	Mon Jan 04 17:45:36 2016 +0100
    16.3 @@ -1621,6 +1621,23 @@
    16.4  
    16.5  lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
    16.6    unfolding isCont_def by (rule tendsto_compose)
    16.7 +  
    16.8 +lemma continuous_on_tendsto_compose:
    16.9 +  assumes f_cont: "continuous_on s f"
   16.10 +  assumes g: "(g \<longlongrightarrow> l) F"
   16.11 +  assumes l: "l \<in> s"
   16.12 +  assumes ev: "\<forall>\<^sub>F x in F. g x \<in> s"
   16.13 +  shows "((\<lambda>x. f (g x)) \<longlongrightarrow> f l) F"
   16.14 +proof -
   16.15 +  from f_cont l have f: "(f \<longlongrightarrow> f l) (at l within s)"
   16.16 +    by (simp add: continuous_on_def)
   16.17 +  have i: "((\<lambda>x. if g x = l then f l else f (g x)) \<longlongrightarrow> f l) F"
   16.18 +    by (rule filterlim_If)
   16.19 +       (auto intro!: filterlim_compose[OF f] eventually_conj tendsto_mono[OF _ g]
   16.20 +             simp: filterlim_at eventually_inf_principal eventually_mono[OF ev])
   16.21 +  show ?thesis
   16.22 +    by (rule filterlim_cong[THEN iffD1[OF _ i]]) auto
   16.23 +qed
   16.24  
   16.25  lemma continuous_within_compose3:
   16.26    "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
    17.1 --- a/src/HOL/Transcendental.thy	Sun Jan 03 21:45:34 2016 +0100
    17.2 +++ b/src/HOL/Transcendental.thy	Mon Jan 04 17:45:36 2016 +0100
    17.3 @@ -2354,6 +2354,11 @@
    17.4  lemma powr_numeral: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x ^ (numeral n)"
    17.5    by (metis of_nat_numeral powr_realpow)
    17.6  
    17.7 +lemma powr_real_of_int: 
    17.8 +  "x > 0 \<Longrightarrow> x powr real_of_int n = (if n \<ge> 0 then x ^ nat n else inverse (x ^ nat (-n)))"
    17.9 +  using powr_realpow[of x "nat n"] powr_realpow[of x "nat (-n)"]
   17.10 +  by (auto simp: field_simps powr_minus)  
   17.11 +
   17.12  lemma powr2_sqrt[simp]: "0 < x \<Longrightarrow> sqrt x powr 2 = x"
   17.13  by(simp add: powr_numeral)
   17.14