src/HOL/Analysis/Gamma_Function.thy
changeset 71633 07bec530f02e
parent 71189 954ee5acaae0
child 72318 bc97bd4c0474
equal deleted inserted replaced
71629:2e8f861d21d4 71633:07bec530f02e
   393   define N where "N = max 2 (max (nat \<lceil>2 * (norm z + d)\<rceil>) M)"
   393   define N where "N = max 2 (max (nat \<lceil>2 * (norm z + d)\<rceil>) M)"
   394   {
   394   {
   395     from d have "\<lceil>2 * (cmod z + d)\<rceil> \<ge> \<lceil>0::real\<rceil>"
   395     from d have "\<lceil>2 * (cmod z + d)\<rceil> \<ge> \<lceil>0::real\<rceil>"
   396       by (intro ceiling_mono mult_nonneg_nonneg add_nonneg_nonneg) simp_all
   396       by (intro ceiling_mono mult_nonneg_nonneg add_nonneg_nonneg) simp_all
   397     hence "2 * (norm z + d) \<le> of_nat (nat \<lceil>2 * (norm z + d)\<rceil>)" unfolding N_def
   397     hence "2 * (norm z + d) \<le> of_nat (nat \<lceil>2 * (norm z + d)\<rceil>)" unfolding N_def
   398       by (simp_all add: le_of_int_ceiling)
   398       by (simp_all)
   399     also have "... \<le> of_nat N" unfolding N_def
   399     also have "... \<le> of_nat N" unfolding N_def
   400       by (subst of_nat_le_iff) (rule max.coboundedI2, rule max.cobounded1)
   400       by (subst of_nat_le_iff) (rule max.coboundedI2, rule max.cobounded1)
   401     finally have "of_nat N \<ge> 2 * (norm z + d)" .
   401     finally have "of_nat N \<ge> 2 * (norm z + d)" .
   402     moreover have "N \<ge> 2" "N \<ge> M" unfolding N_def by simp_all
   402     moreover have "N \<ge> 2" "N \<ge> M" unfolding N_def by simp_all
   403     moreover have "(\<Sum>k=m..n. 1/(of_nat k)\<^sup>2) < e'" if "m \<ge> N" for m n
   403     moreover have "(\<Sum>k=m..n. 1/(of_nat k)\<^sup>2) < e'" if "m \<ge> N" for m n
   538       by (subst atLeast0LessThan [symmetric], subst sum.shift_bounds_Suc_ivl [symmetric],
   538       by (subst atLeast0LessThan [symmetric], subst sum.shift_bounds_Suc_ivl [symmetric],
   539           subst atLeastLessThanSuc_atLeastAtMost) simp_all
   539           subst atLeastLessThanSuc_atLeastAtMost) simp_all
   540     also have "\<dots> = z * of_real (harm n) - (\<Sum>k=1..n. ln (1 + z / of_nat k))"
   540     also have "\<dots> = z * of_real (harm n) - (\<Sum>k=1..n. ln (1 + z / of_nat k))"
   541       by (simp add: harm_def sum_subtractf sum_distrib_left divide_inverse)
   541       by (simp add: harm_def sum_subtractf sum_distrib_left divide_inverse)
   542     also from n have "\<dots> - ?g n = 0"
   542     also from n have "\<dots> - ?g n = 0"
   543       by (simp add: ln_Gamma_series_def sum_subtractf algebra_simps Ln_of_nat)
   543       by (simp add: ln_Gamma_series_def sum_subtractf algebra_simps)
   544     finally show "(\<Sum>k<n. ?f k) - ?g n = 0" .
   544     finally show "(\<Sum>k<n. ?f k) - ?g n = 0" .
   545   qed
   545   qed
   546   show "(\<lambda>n. (\<Sum>k<n. ?f k) - ?g n) \<longlonglongrightarrow> 0" by (subst tendsto_cong[OF A]) simp_all
   546   show "(\<lambda>n. (\<Sum>k<n. ?f k) - ?g n) \<longlonglongrightarrow> 0" by (subst tendsto_cong[OF A]) simp_all
   547 qed
   547 qed
   548 
   548 
   581     hence t': "t \<noteq> -of_nat (Suc n)" by (intro notI) (simp del: of_nat_Suc)
   581     hence t': "t \<noteq> -of_nat (Suc n)" by (intro notI) (simp del: of_nat_Suc)
   582 
   582 
   583     with t_nz have "?f n t = 1 / (of_nat (Suc n) * (1 + of_nat (Suc n)/t))"
   583     with t_nz have "?f n t = 1 / (of_nat (Suc n) * (1 + of_nat (Suc n)/t))"
   584       by (simp add: field_split_simps eq_neg_iff_add_eq_0 del: of_nat_Suc)
   584       by (simp add: field_split_simps eq_neg_iff_add_eq_0 del: of_nat_Suc)
   585     also have "norm \<dots> = inverse (of_nat (Suc n)) * inverse (norm (of_nat (Suc n)/t + 1))"
   585     also have "norm \<dots> = inverse (of_nat (Suc n)) * inverse (norm (of_nat (Suc n)/t + 1))"
   586       by (simp add: norm_divide norm_mult field_split_simps add_ac del: of_nat_Suc)
   586       by (simp add: norm_divide norm_mult field_split_simps del: of_nat_Suc)
   587     also {
   587     also {
   588       from z t_nz ball[OF t] have "of_nat (Suc n) / (4 * norm z) \<le> of_nat (Suc n) / (2 * norm t)"
   588       from z t_nz ball[OF t] have "of_nat (Suc n) / (4 * norm z) \<le> of_nat (Suc n) / (2 * norm t)"
   589         by (intro divide_left_mono mult_pos_pos) simp_all
   589         by (intro divide_left_mono mult_pos_pos) simp_all
   590       also have "\<dots> < norm (of_nat (Suc n) / t) - norm (1 :: 'a)"
   590       also have "\<dots> < norm (of_nat (Suc n) / t) - norm (1 :: 'a)"
   591         using t_nz n by (simp add: field_simps norm_divide del: of_nat_Suc)
   591         using t_nz n by (simp add: field_simps norm_divide del: of_nat_Suc)
  1108     fix n :: nat assume n: "n > 0"
  1108     fix n :: nat assume n: "n > 0"
  1109     hence "z * rGamma_series (z + 1) n = inverse (of_nat n) *
  1109     hence "z * rGamma_series (z + 1) n = inverse (of_nat n) *
  1110              pochhammer z (Suc (Suc n)) / (fact n * exp (z * of_real (ln (of_nat n))))"
  1110              pochhammer z (Suc (Suc n)) / (fact n * exp (z * of_real (ln (of_nat n))))"
  1111       by (subst pochhammer_rec) (simp add: rGamma_series_def field_simps exp_add exp_of_real)
  1111       by (subst pochhammer_rec) (simp add: rGamma_series_def field_simps exp_add exp_of_real)
  1112     also from n have "\<dots> = ?f n * rGamma_series z n"
  1112     also from n have "\<dots> = ?f n * rGamma_series z n"
  1113       by (subst pochhammer_rec') (simp_all add: field_split_simps rGamma_series_def add_ac)
  1113       by (subst pochhammer_rec') (simp_all add: field_split_simps rGamma_series_def)
  1114     finally show "?f n * rGamma_series z n = z * rGamma_series (z + 1) n" ..
  1114     finally show "?f n * rGamma_series z n = z * rGamma_series (z + 1) n" ..
  1115   qed
  1115   qed
  1116   moreover have "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> ((z+1) * 0 + 1) * rGamma z"
  1116   moreover have "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> ((z+1) * 0 + 1) * rGamma z"
  1117     by (intro tendsto_intros lim_inverse_n)
  1117     by (intro tendsto_intros lim_inverse_n)
  1118   hence "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> rGamma z" by simp
  1118   hence "(\<lambda>n. ?f n * rGamma_series z n) \<longlonglongrightarrow> rGamma z" by simp
  1173   shows   "g \<longlonglongrightarrow> Gamma z"
  1173   shows   "g \<longlonglongrightarrow> Gamma z"
  1174 proof (rule Lim_transform_eventually)
  1174 proof (rule Lim_transform_eventually)
  1175   have "1/2 > (0::real)" by simp
  1175   have "1/2 > (0::real)" by simp
  1176   from tendstoD[OF assms, OF this]
  1176   from tendstoD[OF assms, OF this]
  1177     show "eventually (\<lambda>n. g n / Gamma_series z n * Gamma_series z n = g n) sequentially"
  1177     show "eventually (\<lambda>n. g n / Gamma_series z n * Gamma_series z n = g n) sequentially"
  1178     by (force elim!: eventually_mono simp: dist_real_def dist_0_norm)
  1178     by (force elim!: eventually_mono simp: dist_real_def)
  1179   from assms have "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> 1 * Gamma z"
  1179   from assms have "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> 1 * Gamma z"
  1180     by (intro tendsto_intros)
  1180     by (intro tendsto_intros)
  1181   thus "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> Gamma z" by simp
  1181   thus "(\<lambda>n. g n / Gamma_series z n * Gamma_series z n) \<longlonglongrightarrow> Gamma z" by simp
  1182 qed
  1182 qed
  1183 
  1183 
  1187   assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
  1187   assumes "z \<notin> \<int>\<^sub>\<le>\<^sub>0"
  1188   shows   "g \<longlonglongrightarrow> Gamma z"
  1188   shows   "g \<longlonglongrightarrow> Gamma z"
  1189 proof (rule Lim_transform_eventually)
  1189 proof (rule Lim_transform_eventually)
  1190   have "1/2 > (0::real)" by simp
  1190   have "1/2 > (0::real)" by simp
  1191   from tendstoD[OF assms(2), OF this] show "eventually (\<lambda>n. g n * f n / f n = g n) sequentially"
  1191   from tendstoD[OF assms(2), OF this] show "eventually (\<lambda>n. g n * f n / f n = g n) sequentially"
  1192     by (force elim!: eventually_mono simp: dist_real_def dist_0_norm)
  1192     by (force elim!: eventually_mono simp: dist_real_def)
  1193   from assms have "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> 1 / rGamma z"
  1193   from assms have "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> 1 / rGamma z"
  1194     by (intro tendsto_divide assms) (simp_all add: rGamma_eq_zero_iff)
  1194     by (intro tendsto_divide assms) (simp_all add: rGamma_eq_zero_iff)
  1195   thus "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> Gamma z" by (simp add: Gamma_def divide_inverse)
  1195   thus "(\<lambda>n. g n * f n / f n) \<longlonglongrightarrow> Gamma z" by (simp add: Gamma_def divide_inverse)
  1196 qed
  1196 qed
  1197 
  1197 
  1407   from has_field_derivative_rGamma_complex_no_nonpos_Int[OF this]
  1407   from has_field_derivative_rGamma_complex_no_nonpos_Int[OF this]
  1408     show "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (z + of_nat k))
  1408     show "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (z + of_nat k))
  1409                        \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma z +
  1409                        \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma z +
  1410               rGamma z * d * (y - z)) /\<^sub>R  cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  1410               rGamma z * d * (y - z)) /\<^sub>R  cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  1411       by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  1411       by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  1412                     netlimit_at of_real_def[symmetric] suminf_def)
  1412                     of_real_def[symmetric] suminf_def)
  1413 next
  1413 next
  1414   fix n :: nat
  1414   fix n :: nat
  1415   from has_field_derivative_rGamma_complex_nonpos_Int[of n]
  1415   from has_field_derivative_rGamma_complex_nonpos_Int[of n]
  1416   show "let z = - of_nat n in (\<lambda>y. (rGamma y - rGamma z - (- 1) ^ n * prod of_nat {1..n} *
  1416   show "let z = - of_nat n in (\<lambda>y. (rGamma y - rGamma z - (- 1) ^ n * prod of_nat {1..n} *
  1417                   (y - z)) /\<^sub>R cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  1417                   (y - z)) /\<^sub>R cmod (y - z)) \<midarrow>z\<rightarrow> 0"
  1418     by (simp add: has_field_derivative_def has_derivative_def fact_prod netlimit_at Let_def)
  1418     by (simp add: has_field_derivative_def has_derivative_def fact_prod Let_def)
  1419 next
  1419 next
  1420   fix z :: complex
  1420   fix z :: complex
  1421   from rGamma_series_complex_converges[of z] have "rGamma_series z \<longlonglongrightarrow> rGamma z"
  1421   from rGamma_series_complex_converges[of z] have "rGamma_series z \<longlonglongrightarrow> rGamma z"
  1422     by (simp add: convergent_LIMSEQ_iff rGamma_complex_def)
  1422     by (simp add: convergent_LIMSEQ_iff rGamma_complex_def)
  1423   thus "let fact' = \<lambda>n. prod of_nat {1..n};
  1423   thus "let fact' = \<lambda>n. prod of_nat {1..n};
  1533   using eventually_gt_at_top[of "0 :: nat"]
  1533   using eventually_gt_at_top[of "0 :: nat"]
  1534 proof eventually_elim
  1534 proof eventually_elim
  1535   fix n :: nat assume n: "n > 0"
  1535   fix n :: nat assume n: "n > 0"
  1536   have "Re (rGamma_series (of_real x) n) =
  1536   have "Re (rGamma_series (of_real x) n) =
  1537           Re (of_real (pochhammer x (Suc n)) / (fact n * exp (of_real (x * ln (real_of_nat n)))))"
  1537           Re (of_real (pochhammer x (Suc n)) / (fact n * exp (of_real (x * ln (real_of_nat n)))))"
  1538     using n by (simp add: rGamma_series_def powr_def Ln_of_nat pochhammer_of_real)
  1538     using n by (simp add: rGamma_series_def powr_def pochhammer_of_real)
  1539   also from n have "\<dots> = Re (of_real ((pochhammer x (Suc n)) /
  1539   also from n have "\<dots> = Re (of_real ((pochhammer x (Suc n)) /
  1540                               (fact n * (exp (x * ln (real_of_nat n))))))"
  1540                               (fact n * (exp (x * ln (real_of_nat n))))))"
  1541     by (subst exp_of_real) simp
  1541     by (subst exp_of_real) simp
  1542   also from n have "\<dots> = rGamma_series x n"
  1542   also from n have "\<dots> = rGamma_series x n"
  1543     by (subst Re_complex_of_real) (simp add: rGamma_series_def powr_def)
  1543     by (subst Re_complex_of_real) (simp add: rGamma_series_def powr_def)
  1567                   simp: Polygamma_of_real rGamma_real_def [abs_def])
  1567                   simp: Polygamma_of_real rGamma_real_def [abs_def])
  1568   thus "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (x + of_nat k))
  1568   thus "let d = (THE d. (\<lambda>n. \<Sum>k<n. inverse (of_nat (Suc k)) - inverse (x + of_nat k))
  1569                        \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma x +
  1569                        \<longlonglongrightarrow> d) - euler_mascheroni *\<^sub>R 1 in  (\<lambda>y. (rGamma y - rGamma x +
  1570               rGamma x * d * (y - x)) /\<^sub>R  norm (y - x)) \<midarrow>x\<rightarrow> 0"
  1570               rGamma x * d * (y - x)) /\<^sub>R  norm (y - x)) \<midarrow>x\<rightarrow> 0"
  1571       by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  1571       by (simp add: has_field_derivative_def has_derivative_def Digamma_def sums_def [abs_def]
  1572                     netlimit_at of_real_def[symmetric] suminf_def)
  1572                     of_real_def[symmetric] suminf_def)
  1573 next
  1573 next
  1574   fix n :: nat
  1574   fix n :: nat
  1575   have "(rGamma has_field_derivative (-1)^n * fact n) (at (- of_nat n :: real))"
  1575   have "(rGamma has_field_derivative (-1)^n * fact n) (at (- of_nat n :: real))"
  1576     by (fastforce intro!: derivative_eq_intros has_vector_derivative_real_field
  1576     by (fastforce intro!: derivative_eq_intros has_vector_derivative_real_field
  1577                   simp: Polygamma_of_real rGamma_real_def [abs_def])
  1577                   simp: Polygamma_of_real rGamma_real_def [abs_def])
  1578   thus "let x = - of_nat n in (\<lambda>y. (rGamma y - rGamma x - (- 1) ^ n * prod of_nat {1..n} *
  1578   thus "let x = - of_nat n in (\<lambda>y. (rGamma y - rGamma x - (- 1) ^ n * prod of_nat {1..n} *
  1579                   (y - x)) /\<^sub>R norm (y - x)) \<midarrow>x::real\<rightarrow> 0"
  1579                   (y - x)) /\<^sub>R norm (y - x)) \<midarrow>x::real\<rightarrow> 0"
  1580     by (simp add: has_field_derivative_def has_derivative_def fact_prod netlimit_at Let_def)
  1580     by (simp add: has_field_derivative_def has_derivative_def fact_prod Let_def)
  1581 next
  1581 next
  1582   fix x :: real
  1582   fix x :: real
  1583   have "rGamma_series x \<longlonglongrightarrow> rGamma x"
  1583   have "rGamma_series x \<longlonglongrightarrow> rGamma x"
  1584   proof (rule Lim_transform_eventually)
  1584   proof (rule Lim_transform_eventually)
  1585     show "(\<lambda>n. Re (rGamma_series (of_real x) n)) \<longlonglongrightarrow> rGamma x" unfolding rGamma_real_def
  1585     show "(\<lambda>n. Re (rGamma_series (of_real x) n)) \<longlonglongrightarrow> rGamma x" unfolding rGamma_real_def
  1617   "x > 0 \<Longrightarrow> n > 0 \<Longrightarrow> ln_Gamma_series (complex_of_real x) n = of_real (ln_Gamma_series x n)"
  1617   "x > 0 \<Longrightarrow> n > 0 \<Longrightarrow> ln_Gamma_series (complex_of_real x) n = of_real (ln_Gamma_series x n)"
  1618 proof -
  1618 proof -
  1619   assume xn: "x > 0" "n > 0"
  1619   assume xn: "x > 0" "n > 0"
  1620   have "Ln (complex_of_real x / of_nat k + 1) = of_real (ln (x / of_nat k + 1))" if "k \<ge> 1" for k
  1620   have "Ln (complex_of_real x / of_nat k + 1) = of_real (ln (x / of_nat k + 1))" if "k \<ge> 1" for k
  1621     using that xn by (subst Ln_of_real [symmetric]) (auto intro!: add_nonneg_pos simp: field_simps)
  1621     using that xn by (subst Ln_of_real [symmetric]) (auto intro!: add_nonneg_pos simp: field_simps)
  1622   with xn show ?thesis by (simp add: ln_Gamma_series_def Ln_of_nat Ln_of_real)
  1622   with xn show ?thesis by (simp add: ln_Gamma_series_def Ln_of_real)
  1623 qed
  1623 qed
  1624 
  1624 
  1625 lemma ln_Gamma_real_converges:
  1625 lemma ln_Gamma_real_converges:
  1626   assumes "(x::real) > 0"
  1626   assumes "(x::real) > 0"
  1627   shows   "convergent (ln_Gamma_series x)"
  1627   shows   "convergent (ln_Gamma_series x)"
  2059   from assms double_in_nonpos_Ints_imp[of z] have z': "2 * z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto
  2059   from assms double_in_nonpos_Ints_imp[of z] have z': "2 * z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto
  2060   from fraction_not_in_ints[of 2 1] have "(1/2 :: 'a) \<notin> \<int>\<^sub>\<le>\<^sub>0"
  2060   from fraction_not_in_ints[of 2 1] have "(1/2 :: 'a) \<notin> \<int>\<^sub>\<le>\<^sub>0"
  2061     by (intro not_in_Ints_imp_not_in_nonpos_Ints) simp_all
  2061     by (intro not_in_Ints_imp_not_in_nonpos_Ints) simp_all
  2062   with lim[of "1/2 :: 'a"] have "?h \<longlonglongrightarrow> 2 * Gamma (1/2 :: 'a)" by (simp add: exp_of_real)
  2062   with lim[of "1/2 :: 'a"] have "?h \<longlonglongrightarrow> 2 * Gamma (1/2 :: 'a)" by (simp add: exp_of_real)
  2063   from LIMSEQ_unique[OF this lim[OF assms]] z' show ?thesis
  2063   from LIMSEQ_unique[OF this lim[OF assms]] z' show ?thesis
  2064     by (simp add: field_split_simps Gamma_eq_zero_iff ring_distribs exp_diff exp_of_real ac_simps)
  2064     by (simp add: field_split_simps Gamma_eq_zero_iff ring_distribs exp_diff exp_of_real)
  2065 qed
  2065 qed
  2066 
  2066 
  2067 text \<open>
  2067 text \<open>
  2068   The following lemma is somewhat annoying. With a little bit of complex analysis
  2068   The following lemma is somewhat annoying. With a little bit of complex analysis
  2069   (Cauchy's integral theorem, to be exact), this would be completely trivial. However,
  2069   (Cauchy's integral theorem, to be exact), this would be completely trivial. However,
  2124     "h2' z = a * (POWSER g (a*z) * POWSER' f (a*z) - POWSER f (a*z) * POWSER' g (a*z)) /
  2124     "h2' z = a * (POWSER g (a*z) * POWSER' f (a*z) - POWSER f (a*z) * POWSER' g (a*z)) /
  2125       (POWSER g (a*z))^2 + Polygamma 1 (1 + z) + Polygamma 1 (1 - z)" for z
  2125       (POWSER g (a*z))^2 + Polygamma 1 (1 + z) + Polygamma 1 (1 - z)" for z
  2126 
  2126 
  2127   have h_eq: "h t = h2 t" if "abs (Re t) < 1" for t
  2127   have h_eq: "h t = h2 t" if "abs (Re t) < 1" for t
  2128   proof -
  2128   proof -
  2129     from that have t: "t \<in> \<int> \<longleftrightarrow> t = 0" by (auto elim!: Ints_cases simp: dist_0_norm)
  2129     from that have t: "t \<in> \<int> \<longleftrightarrow> t = 0" by (auto elim!: Ints_cases)
  2130     hence "h t = a*cot (a*t) - 1/t + Digamma (1 + t) - Digamma (1 - t)"
  2130     hence "h t = a*cot (a*t) - 1/t + Digamma (1 + t) - Digamma (1 - t)"
  2131       unfolding h_def using Digamma_plus1[of t] by (force simp: field_simps a_def)
  2131       unfolding h_def using Digamma_plus1[of t] by (force simp: field_simps a_def)
  2132     also have "a*cot (a*t) - 1/t = (F t) / (G t)"
  2132     also have "a*cot (a*t) - 1/t = (F t) / (G t)"
  2133       using t by (auto simp add: divide_simps sin_eq_0 cot_def a_def F_def G_def)
  2133       using t by (auto simp add: divide_simps sin_eq_0 cot_def a_def F_def G_def)
  2134     also have "\<dots> = (\<Sum>n. f n * (a*t)^n) / (\<Sum>n. g n * (a*t)^n)"
  2134     also have "\<dots> = (\<Sum>n. f n * (a*t)^n) / (\<Sum>n. g n * (a*t)^n)"
  2135       using sums[of t] that by (simp add: sums_iff dist_0_norm)
  2135       using sums[of t] that by (simp add: sums_iff)
  2136     finally show "h t = h2 t" by (simp only: h2_def)
  2136     finally show "h t = h2 t" by (simp only: h2_def)
  2137   qed
  2137   qed
  2138 
  2138 
  2139   let ?A = "{z. abs (Re z) < 1}"
  2139   let ?A = "{z. abs (Re z) < 1}"
  2140   have "open ({z. Re z < 1} \<inter> {z. Re z > -1})"
  2140   have "open ({z. Re z < 1} \<inter> {z. Re z > -1})"
  2165     fix z :: complex assume z: "abs (Re z) < 1"
  2165     fix z :: complex assume z: "abs (Re z) < 1"
  2166     define d where "d = \<i> * of_real (norm z + 1)"
  2166     define d where "d = \<i> * of_real (norm z + 1)"
  2167     have d: "abs (Re d) < 1" "norm z < norm d" by (simp_all add: d_def norm_mult del: of_real_add)
  2167     have d: "abs (Re d) < 1" "norm z < norm d" by (simp_all add: d_def norm_mult del: of_real_add)
  2168     have "eventually (\<lambda>z. h z = h2 z) (nhds z)"
  2168     have "eventually (\<lambda>z. h z = h2 z) (nhds z)"
  2169       using eventually_nhds_in_nhd[of z ?A] using h_eq z
  2169       using eventually_nhds_in_nhd[of z ?A] using h_eq z
  2170       by (auto elim!: eventually_mono simp: dist_0_norm)
  2170       by (auto elim!: eventually_mono)
  2171 
  2171 
  2172     moreover from sums(2)[OF z] z have nz: "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
  2172     moreover from sums(2)[OF z] z have nz: "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
  2173       unfolding G_def by (auto simp: sums_iff sin_eq_0 a_def)
  2173       unfolding G_def by (auto simp: sums_iff sin_eq_0 a_def)
  2174     have A: "z \<in> \<int> \<longleftrightarrow> z = 0" using z by (auto elim!: Ints_cases)
  2174     have A: "z \<in> \<int> \<longleftrightarrow> z = 0" using z by (auto elim!: Ints_cases)
  2175     have no_int: "1 + z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of "1+z" 1] A
  2175     have no_int: "1 + z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of "1+z" 1] A
  2324       proof eventually_elim
  2324       proof eventually_elim
  2325         fix z :: complex assume z: "z \<in> ball 0 1"
  2325         fix z :: complex assume z: "z \<in> ball 0 1"
  2326         show "g z = of_real pi * Gamma (1 + z) * Gamma (1 - z) * ?t z"
  2326         show "g z = of_real pi * Gamma (1 + z) * Gamma (1 - z) * ?t z"
  2327         proof (cases "z = 0")
  2327         proof (cases "z = 0")
  2328           assume z': "z \<noteq> 0"
  2328           assume z': "z \<noteq> 0"
  2329           with z have z'': "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z \<notin> \<int>" by (auto elim!: Ints_cases simp: dist_0_norm)
  2329           with z have z'': "z \<notin> \<int>\<^sub>\<le>\<^sub>0" "z \<notin> \<int>" by (auto elim!: Ints_cases)
  2330           from Gamma_plus1[OF this(1)] have "Gamma z = Gamma (z + 1) / z" by simp
  2330           from Gamma_plus1[OF this(1)] have "Gamma z = Gamma (z + 1) / z" by simp
  2331           with z'' z' show ?thesis by (simp add: g_def ac_simps)
  2331           with z'' z' show ?thesis by (simp add: g_def ac_simps)
  2332         qed (simp add: g_def)
  2332         qed (simp add: g_def)
  2333       qed
  2333       qed
  2334       have "(?t has_field_derivative (0 * of_real pi)) (at 0)"
  2334       have "(?t has_field_derivative (0 * of_real pi)) (at 0)"
  2469       thus "B \<noteq> {}" by auto
  2469       thus "B \<noteq> {}" by auto
  2470     next
  2470     next
  2471       show "\<forall>z\<in>B. norm (h' z) \<le> M/2"
  2471       show "\<forall>z\<in>B. norm (h' z) \<le> M/2"
  2472       proof
  2472       proof
  2473         fix t :: complex assume t: "t \<in> B"
  2473         fix t :: complex assume t: "t \<in> B"
  2474         from h'_eq[of t] t have "h' t = (h' (t/2) + h' ((t+1)/2)) / 4" by (simp add: dist_0_norm)
  2474         from h'_eq[of t] t have "h' t = (h' (t/2) + h' ((t+1)/2)) / 4" by (simp)
  2475         also have "norm \<dots> = norm (h' (t/2) + h' ((t+1)/2)) / 4" by simp
  2475         also have "norm \<dots> = norm (h' (t/2) + h' ((t+1)/2)) / 4" by simp
  2476         also have "norm (h' (t/2) + h' ((t+1)/2)) \<le> norm (h' (t/2)) + norm (h' ((t+1)/2))"
  2476         also have "norm (h' (t/2) + h' ((t+1)/2)) \<le> norm (h' (t/2)) + norm (h' ((t+1)/2))"
  2477           by (rule norm_triangle_ineq)
  2477           by (rule norm_triangle_ineq)
  2478         also from t have "abs (Re ((t + 1)/2)) \<le> m" unfolding m_def B_def by auto
  2478         also from t have "abs (Re ((t + 1)/2)) \<le> m" unfolding m_def B_def by auto
  2479         with t have "t/2 \<in> B" "(t+1)/2 \<in> B" unfolding B_def by auto
  2479         with t have "t/2 \<in> B" "(t+1)/2 \<in> B" unfolding B_def by auto
  2480         hence "norm (h' (t/2)) + norm (h' ((t+1)/2)) \<le> M + M" unfolding M_def
  2480         hence "norm (h' (t/2)) + norm (h' ((t+1)/2)) \<le> M + M" unfolding M_def
  2481           by (intro add_mono cSUP_upper bdd) (auto simp: B_def)
  2481           by (intro add_mono cSUP_upper bdd) (auto simp: B_def)
  2482         also have "(M + M) / 4 = M / 2" by simp
  2482         also have "(M + M) / 4 = M / 2" by simp
  2483         finally show "norm (h' t) \<le> M/2" by - simp_all
  2483         finally show "norm (h' t) \<le> M/2" by - simp_all
  2484       qed
  2484       qed
  2485     qed (insert bdd, auto simp: cball_eq_empty)
  2485     qed (insert bdd, auto)
  2486     hence "M \<le> 0" by simp
  2486     hence "M \<le> 0" by simp
  2487     finally show "h' z = 0" by simp
  2487     finally show "h' z = 0" by simp
  2488   qed
  2488   qed
  2489   have h_h'_2: "(h has_field_derivative 0) (at z)" for z
  2489   have h_h'_2: "(h has_field_derivative 0) (at z)" for z
  2490     using h_h'[of z] h'_zero[of z] by simp
  2490     using h_h'[of z] h'_zero[of z] by simp
  2510     have "Re c = 0" by (simp add: complex_is_Real_iff g.of_1)
  2510     have "Re c = 0" by (simp add: complex_is_Real_iff g.of_1)
  2511   with h_real[of 0] c[of 0] have "c = 0" by (auto elim!: Reals_cases)
  2511   with h_real[of 0] c[of 0] have "c = 0" by (auto elim!: Reals_cases)
  2512   with c have A: "h z * g z = 0" for z by simp
  2512   with c have A: "h z * g z = 0" for z by simp
  2513   hence "(g has_field_derivative 0) (at z)" for z using g_g'[of z] by simp
  2513   hence "(g has_field_derivative 0) (at z)" for z using g_g'[of z] by simp
  2514   hence "\<exists>c'. \<forall>z\<in>UNIV. g z = c'" by (intro has_field_derivative_zero_constant) simp_all
  2514   hence "\<exists>c'. \<forall>z\<in>UNIV. g z = c'" by (intro has_field_derivative_zero_constant) simp_all
  2515   then obtain c' where c: "\<And>z. g z = c'" by (force simp: dist_0_norm)
  2515   then obtain c' where c: "\<And>z. g z = c'" by (force)
  2516   from this[of 0] have "c' = pi" unfolding g_def by simp
  2516   from this[of 0] have "c' = pi" unfolding g_def by simp
  2517   with c have "g z = pi" by simp
  2517   with c have "g z = pi" by simp
  2518 
  2518 
  2519   show ?thesis
  2519   show ?thesis
  2520   proof (cases "z \<in> \<int>")
  2520   proof (cases "z \<in> \<int>")
  2545   finally show ?thesis by simp
  2545   finally show ?thesis by simp
  2546 qed
  2546 qed
  2547 
  2547 
  2548 lemma Gamma_reflection_complex':
  2548 lemma Gamma_reflection_complex':
  2549   "Gamma z * Gamma (- z :: complex) = - of_real pi / (z * sin (of_real pi * z))"
  2549   "Gamma z * Gamma (- z :: complex) = - of_real pi / (z * sin (of_real pi * z))"
  2550   using rGamma_reflection_complex'[of z] by (force simp add: Gamma_def field_split_simps mult_ac)
  2550   using rGamma_reflection_complex'[of z] by (force simp add: Gamma_def field_split_simps)
  2551 
  2551 
  2552 
  2552 
  2553 
  2553 
  2554 lemma Gamma_one_half_real: "Gamma (1/2 :: real) = sqrt pi"
  2554 lemma Gamma_one_half_real: "Gamma (1/2 :: real) = sqrt pi"
  2555 proof -
  2555 proof -
  2673   let ?r = "\<lambda>n. ?f n / Gamma_series z n"
  2673   let ?r = "\<lambda>n. ?f n / Gamma_series z n"
  2674   let ?r' = "\<lambda>n. exp (z * of_real (ln (of_nat (Suc n) / of_nat n)))"
  2674   let ?r' = "\<lambda>n. exp (z * of_real (ln (of_nat (Suc n) / of_nat n)))"
  2675   from z have z': "z \<noteq> 0" by auto
  2675   from z have z': "z \<noteq> 0" by auto
  2676 
  2676 
  2677   have "eventually (\<lambda>n. ?r' n = ?r n) sequentially"
  2677   have "eventually (\<lambda>n. ?r' n = ?r n) sequentially"
  2678     using z by (auto simp: field_split_simps Gamma_series_def ring_distribs exp_diff ln_div add_ac
  2678     using z by (auto simp: field_split_simps Gamma_series_def ring_distribs exp_diff ln_div
  2679                      intro: eventually_mono eventually_gt_at_top[of "0::nat"] dest: pochhammer_eq_0_imp_nonpos_Int)
  2679                      intro: eventually_mono eventually_gt_at_top[of "0::nat"] dest: pochhammer_eq_0_imp_nonpos_Int)
  2680   moreover have "?r' \<longlonglongrightarrow> exp (z * of_real (ln 1))"
  2680   moreover have "?r' \<longlonglongrightarrow> exp (z * of_real (ln 1))"
  2681     by (intro tendsto_intros LIMSEQ_Suc_n_over_n) simp_all
  2681     by (intro tendsto_intros LIMSEQ_Suc_n_over_n) simp_all
  2682   ultimately show "?r \<longlonglongrightarrow> 1" by (force intro: Lim_transform_eventually)
  2682   ultimately show "?r \<longlonglongrightarrow> 1" by (force intro: Lim_transform_eventually)
  2683 
  2683 
  2817   "(\<lambda>n. of_nat ((k + n) choose n) / of_nat (n ^ k) :: 'a :: Gamma) \<longlonglongrightarrow> 1 / fact k"
  2817   "(\<lambda>n. of_nat ((k + n) choose n) / of_nat (n ^ k) :: 'a :: Gamma) \<longlonglongrightarrow> 1 / fact k"
  2818 proof (rule Lim_transform_eventually)
  2818 proof (rule Lim_transform_eventually)
  2819   have "(\<lambda>n. of_nat ((k + n) choose n) / of_real (exp (of_nat k * ln (real_of_nat n))))
  2819   have "(\<lambda>n. of_nat ((k + n) choose n) / of_real (exp (of_nat k * ln (real_of_nat n))))
  2820             \<longlonglongrightarrow> 1 / Gamma (of_nat (Suc k) :: 'a)" (is "?f \<longlonglongrightarrow> _")
  2820             \<longlonglongrightarrow> 1 / Gamma (of_nat (Suc k) :: 'a)" (is "?f \<longlonglongrightarrow> _")
  2821     using Gamma_gbinomial[of "of_nat k :: 'a"]
  2821     using Gamma_gbinomial[of "of_nat k :: 'a"]
  2822     by (simp add: binomial_gbinomial add_ac Gamma_def field_split_simps exp_of_real [symmetric] exp_minus)
  2822     by (simp add: binomial_gbinomial Gamma_def field_split_simps exp_of_real [symmetric] exp_minus)
  2823   also have "Gamma (of_nat (Suc k)) = fact k" by (simp add: Gamma_fact)
  2823   also have "Gamma (of_nat (Suc k)) = fact k" by (simp add: Gamma_fact)
  2824   finally show "?f \<longlonglongrightarrow> 1 / fact k" .
  2824   finally show "?f \<longlonglongrightarrow> 1 / fact k" .
  2825 
  2825 
  2826   show "eventually (\<lambda>n. ?f n = of_nat ((k + n) choose n) / of_nat (n ^ k)) sequentially"
  2826   show "eventually (\<lambda>n. ?f n = of_nat ((k + n) choose n) / of_nat (n ^ k)) sequentially"
  2827     using eventually_gt_at_top[of "0::nat"]
  2827     using eventually_gt_at_top[of "0::nat"]
  2860       with \<open>z = 0\<close> show ?thesis
  2860       with \<open>z = 0\<close> show ?thesis
  2861         by (simp add: Beta_def Gamma_eq_zero_iff Gamma_plus1 [symmetric])
  2861         by (simp add: Beta_def Gamma_eq_zero_iff Gamma_plus1 [symmetric])
  2862     next
  2862     next
  2863       case False
  2863       case False
  2864       with \<open>z = 0\<close> show ?thesis
  2864       with \<open>z = 0\<close> show ?thesis
  2865         by (simp_all add: Beta_pole1 one_minus_of_nat_in_nonpos_Ints_iff gbinomial_1)
  2865         by (simp_all add: Beta_pole1 one_minus_of_nat_in_nonpos_Ints_iff)
  2866     qed
  2866     qed
  2867   next
  2867   next
  2868     case False
  2868     case False
  2869     have "(z gchoose (Suc n)) = ((z - 1 + 1) gchoose (Suc n))" by simp
  2869     have "(z gchoose (Suc n)) = ((z - 1 + 1) gchoose (Suc n))" by simp
  2870     also have "\<dots> = (z - 1 gchoose n) * ((z - 1) + 1) / of_nat (Suc n)"
  2870     also have "\<dots> = (z - 1 gchoose n) * ((z - 1) + 1) / of_nat (Suc n)"
  2883   shows   "(z gchoose n) = Gamma (z + 1) / (fact n * Gamma (z - of_nat n + 1))"
  2883   shows   "(z gchoose n) = Gamma (z + 1) / (fact n * Gamma (z - of_nat n + 1))"
  2884 proof -
  2884 proof -
  2885   have "(z gchoose n) = Gamma (z + 2) / (z + 1) / (fact n * Gamma (z - of_nat n + 1))"
  2885   have "(z gchoose n) = Gamma (z + 2) / (z + 1) / (fact n * Gamma (z - of_nat n + 1))"
  2886     by (subst gbinomial_Beta[OF assms]) (simp_all add: Beta_def Gamma_fact [symmetric] add_ac)
  2886     by (subst gbinomial_Beta[OF assms]) (simp_all add: Beta_def Gamma_fact [symmetric] add_ac)
  2887   also from assms have "Gamma (z + 2) / (z + 1) = Gamma (z + 1)"
  2887   also from assms have "Gamma (z + 2) / (z + 1) = Gamma (z + 1)"
  2888     using Gamma_plus1[of "z+1"] by (auto simp add: field_split_simps mult_ac add_ac)
  2888     using Gamma_plus1[of "z+1"] by (auto simp add: field_split_simps)
  2889   finally show ?thesis .
  2889   finally show ?thesis .
  2890 qed
  2890 qed
  2891 
  2891 
  2892 
  2892 
  2893 subsubsection \<open>Integral form\<close>
  2893 subsubsection \<open>Integral form\<close>
  3071   qed
  3071   qed
  3072 
  3072 
  3073   have "eventually (\<lambda>n. Gamma_series z n =
  3073   have "eventually (\<lambda>n. Gamma_series z n =
  3074           of_nat n powr z * fact n / pochhammer z (n+1)) sequentially"
  3074           of_nat n powr z * fact n / pochhammer z (n+1)) sequentially"
  3075     using eventually_gt_at_top[of "0::nat"]
  3075     using eventually_gt_at_top[of "0::nat"]
  3076     by eventually_elim (simp add: powr_def algebra_simps Ln_of_nat Gamma_series_def)
  3076     by eventually_elim (simp add: powr_def algebra_simps Gamma_series_def)
  3077   from this and Gamma_series_LIMSEQ[of z]
  3077   from this and Gamma_series_LIMSEQ[of z]
  3078     have C: "(\<lambda>k. of_nat k powr z * fact k / pochhammer z (k+1)) \<longlonglongrightarrow> Gamma z"
  3078     have C: "(\<lambda>k. of_nat k powr z * fact k / pochhammer z (k+1)) \<longlonglongrightarrow> Gamma z"
  3079     by (blast intro: Lim_transform_eventually)
  3079     by (blast intro: Lim_transform_eventually)
  3080   {
  3080   {
  3081     fix x :: real assume x: "x \<ge> 0"
  3081     fix x :: real assume x: "x \<ge> 0"
  3439     show "uniform_limit (ball 0 1) (\<lambda>n x. \<Sum>k<n. P x k / of_nat (Suc k)^2) f sequentially"
  3439     show "uniform_limit (ball 0 1) (\<lambda>n x. \<Sum>k<n. P x k / of_nat (Suc k)^2) f sequentially"
  3440     proof (unfold f_def, rule Weierstrass_m_test)
  3440     proof (unfold f_def, rule Weierstrass_m_test)
  3441       fix n :: nat and x :: real assume x: "x \<in> ball 0 1"
  3441       fix n :: nat and x :: real assume x: "x \<in> ball 0 1"
  3442       {
  3442       {
  3443         fix k :: nat assume k: "k \<ge> 1"
  3443         fix k :: nat assume k: "k \<ge> 1"
  3444         from x have "x^2 < 1" by (auto simp: dist_0_norm abs_square_less_1)
  3444         from x have "x^2 < 1" by (auto simp: abs_square_less_1)
  3445         also from k have "\<dots> \<le> of_nat k^2" by simp
  3445         also from k have "\<dots> \<le> of_nat k^2" by simp
  3446         finally have "(1 - x^2 / of_nat k^2) \<in> {0..1}" using k
  3446         finally have "(1 - x^2 / of_nat k^2) \<in> {0..1}" using k
  3447           by (simp_all add: field_simps del: of_nat_Suc)
  3447           by (simp_all add: field_simps del: of_nat_Suc)
  3448       }
  3448       }
  3449       hence "(\<Prod>k=1..n. abs (1 - x^2 / of_nat k^2)) \<le> (\<Prod>k=1..n. 1)" by (intro prod_mono) simp
  3449       hence "(\<Prod>k=1..n. abs (1 - x^2 / of_nat k^2)) \<le> (\<Prod>k=1..n. 1)" by (intro prod_mono) simp
  3468       with sums_split_initial_segment[OF sums_minus[OF sin_converges], of 3 "pi*x"]
  3468       with sums_split_initial_segment[OF sums_minus[OF sin_converges], of 3 "pi*x"]
  3469       have "(\<lambda>n. - (sin_coeff (n+3) * (pi*x)^(n+3))) sums (pi * x - sin (pi*x))"
  3469       have "(\<lambda>n. - (sin_coeff (n+3) * (pi*x)^(n+3))) sums (pi * x - sin (pi*x))"
  3470         by (simp add: eval_nat_numeral)
  3470         by (simp add: eval_nat_numeral)
  3471       from sums_divide[OF this, of "x^3 * pi"] x
  3471       from sums_divide[OF this, of "x^3 * pi"] x
  3472         have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums ((1 - sin (pi*x) / (pi*x)) / x^2)"
  3472         have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums ((1 - sin (pi*x) / (pi*x)) / x^2)"
  3473         by (simp add: field_split_simps eval_nat_numeral power_mult_distrib mult_ac)
  3473         by (simp add: field_split_simps eval_nat_numeral)
  3474       with x have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums (g x / x^2)"
  3474       with x have "(\<lambda>n. - (sin_coeff (n+3) * pi^(n+2) * x^n)) sums (g x / x^2)"
  3475         by (simp add: g_def)
  3475         by (simp add: g_def)
  3476       hence "f' x = g x / x^2" by (simp add: sums_iff f'_def)
  3476       hence "f' x = g x / x^2" by (simp add: sums_iff f'_def)
  3477       also have "\<dots> = f x" using sums[of x] x by (simp add: sums_iff g_def f_def)
  3477       also have "\<dots> = f x" using sums[of x] x by (simp add: sums_iff g_def f_def)
  3478       finally show "f' x = f x" .
  3478       finally show "f' x = f x" .
  3483       fix x :: real show "summable (\<lambda>n. -sin_coeff (n+3) * pi^(n+2) * x^n)"
  3483       fix x :: real show "summable (\<lambda>n. -sin_coeff (n+3) * pi^(n+2) * x^n)"
  3484       proof (cases "x = 0")
  3484       proof (cases "x = 0")
  3485         assume x: "x \<noteq> 0"
  3485         assume x: "x \<noteq> 0"
  3486         from summable_divide[OF sums_summable[OF sums_split_initial_segment[OF
  3486         from summable_divide[OF sums_summable[OF sums_split_initial_segment[OF
  3487                sin_converges[of "pi*x"]], of 3], of "-pi*x^3"] x
  3487                sin_converges[of "pi*x"]], of 3], of "-pi*x^3"] x
  3488           show ?thesis by (simp add: mult_ac power_mult_distrib field_split_simps eval_nat_numeral)
  3488           show ?thesis by (simp add: field_split_simps eval_nat_numeral)
  3489       qed (simp only: summable_0_powser)
  3489       qed (simp only: summable_0_powser)
  3490     qed
  3490     qed
  3491     hence "f' \<midarrow> 0 \<rightarrow> f' 0" by (simp add: isCont_def)
  3491     hence "f' \<midarrow> 0 \<rightarrow> f' 0" by (simp add: isCont_def)
  3492     also have "f' 0 = pi * pi / fact 3" unfolding f'_def
  3492     also have "f' 0 = pi * pi / fact 3" unfolding f'_def
  3493       by (subst powser_zero) (simp add: sin_coeff_def)
  3493       by (subst powser_zero) (simp add: sin_coeff_def)