src/HOL/Analysis/Summation_Tests.thy
author wenzelm
Sun Sep 18 20:33:48 2016 +0200 (2016-09-18)
changeset 63915 bab633745c7f
parent 63627 6ddb43c6b711
child 63918 6bf55e6e0b75
permissions -rw-r--r--
tuned proofs;
hoelzl@63627
     1
(*  Title:    HOL/Analysis/Summation.thy
hoelzl@62055
     2
    Author:   Manuel Eberl, TU M√ľnchen
hoelzl@62055
     3
*)
hoelzl@63594
     4
eberlm@62085
     5
section \<open>Radius of Convergence and Summation Tests\<close>
hoelzl@62055
     6
hoelzl@63594
     7
theory Summation_Tests
eberlm@62049
     8
imports
eberlm@62049
     9
  Complex_Main
hoelzl@63594
    10
  "~~/src/HOL/Library/Extended_Real"
eberlm@62049
    11
  "~~/src/HOL/Library/Liminf_Limsup"
eberlm@62049
    12
begin
eberlm@62049
    13
hoelzl@62055
    14
text \<open>
hoelzl@63594
    15
  The definition of the radius of convergence of a power series,
hoelzl@62055
    16
  various summability tests, lemmas to compute the radius of convergence etc.
hoelzl@62055
    17
\<close>
eberlm@62049
    18
eberlm@62085
    19
subsection \<open>Rounded dual logarithm\<close>
eberlm@62085
    20
eberlm@62049
    21
(* This is required for the Cauchy condensation criterion *)
eberlm@62049
    22
eberlm@62049
    23
definition "natlog2 n = (if n = 0 then 0 else nat \<lfloor>log 2 (real_of_nat n)\<rfloor>)"
eberlm@62049
    24
eberlm@62049
    25
lemma natlog2_0 [simp]: "natlog2 0 = 0" by (simp add: natlog2_def)
eberlm@62049
    26
lemma natlog2_1 [simp]: "natlog2 1 = 0" by (simp add: natlog2_def)
eberlm@62049
    27
lemma natlog2_eq_0_iff: "natlog2 n = 0 \<longleftrightarrow> n < 2" by (simp add: natlog2_def)
eberlm@62049
    28
eberlm@62049
    29
lemma natlog2_power_of_two [simp]: "natlog2 (2 ^ n) = n"
eberlm@62049
    30
  by (simp add: natlog2_def log_nat_power)
eberlm@62049
    31
eberlm@62049
    32
lemma natlog2_mono: "m \<le> n \<Longrightarrow> natlog2 m \<le> natlog2 n"
eberlm@62049
    33
  unfolding natlog2_def by (simp_all add: nat_mono floor_mono)
eberlm@62049
    34
eberlm@62049
    35
lemma pow_natlog2_le: "n > 0 \<Longrightarrow> 2 ^ natlog2 n \<le> n"
eberlm@62049
    36
proof -
eberlm@62049
    37
  assume n: "n > 0"
eberlm@62049
    38
  from n have "of_nat (2 ^ natlog2 n) = 2 powr real_of_nat (nat \<lfloor>log 2 (real_of_nat n)\<rfloor>)"
eberlm@62049
    39
    by (subst powr_realpow) (simp_all add: natlog2_def)
eberlm@62049
    40
  also have "\<dots> = 2 powr of_int \<lfloor>log 2 (real_of_nat n)\<rfloor>" using n by simp
eberlm@62049
    41
  also have "\<dots> \<le> 2 powr log 2 (real_of_nat n)" by (intro powr_mono) (linarith, simp_all)
eberlm@62049
    42
  also have "\<dots> = of_nat n" using n by simp
eberlm@62049
    43
  finally show ?thesis by simp
eberlm@62049
    44
qed
eberlm@62049
    45
eberlm@62049
    46
lemma pow_natlog2_gt: "n > 0 \<Longrightarrow> 2 * 2 ^ natlog2 n > n"
eberlm@62049
    47
  and pow_natlog2_ge: "n > 0 \<Longrightarrow> 2 * 2 ^ natlog2 n \<ge> n"
eberlm@62049
    48
proof -
eberlm@62049
    49
  assume n: "n > 0"
eberlm@62049
    50
  from n have "of_nat n = 2 powr log 2 (real_of_nat n)" by simp
hoelzl@63594
    51
  also have "\<dots> < 2 powr (1 + of_int \<lfloor>log 2 (real_of_nat n)\<rfloor>)"
eberlm@62049
    52
    by (intro powr_less_mono) (linarith, simp_all)
eberlm@62049
    53
  also from n have "\<dots> = 2 powr (1 + real_of_nat (nat \<lfloor>log 2 (real_of_nat n)\<rfloor>))" by simp
eberlm@62049
    54
  also from n have "\<dots> = of_nat (2 * 2 ^ natlog2 n)"
eberlm@62049
    55
    by (simp_all add: natlog2_def powr_real_of_int powr_add)
eberlm@62049
    56
  finally show "2 * 2 ^ natlog2 n > n" by (rule of_nat_less_imp_less)
eberlm@62049
    57
  thus "2 * 2 ^ natlog2 n \<ge> n" by simp
eberlm@62049
    58
qed
eberlm@62049
    59
eberlm@62049
    60
lemma natlog2_eqI:
eberlm@62049
    61
  assumes "n > 0" "2^k \<le> n" "n < 2 * 2^k"
eberlm@62049
    62
  shows   "natlog2 n = k"
eberlm@62049
    63
proof -
eberlm@62049
    64
  from assms have "of_nat (2 ^ k) \<le> real_of_nat n"  by (subst of_nat_le_iff) simp_all
eberlm@62049
    65
  hence "real_of_int (int k) \<le> log (of_nat 2) (real_of_nat n)"
eberlm@62049
    66
    by (subst le_log_iff) (simp_all add: powr_realpow assms del: of_nat_le_iff)
eberlm@62049
    67
  moreover from assms have "real_of_nat n < of_nat (2 ^ Suc k)" by (subst of_nat_less_iff) simp_all
eberlm@62049
    68
  hence "log 2 (real_of_nat n) < of_nat k + 1"
eberlm@62049
    69
    by (subst log_less_iff) (simp_all add: assms powr_realpow powr_add)
eberlm@62049
    70
  ultimately have "\<lfloor>log 2 (real_of_nat n)\<rfloor> = of_nat k" by (intro floor_unique) simp_all
eberlm@62049
    71
  with assms show ?thesis by (simp add: natlog2_def)
eberlm@62049
    72
qed
eberlm@62049
    73
hoelzl@63594
    74
lemma natlog2_rec:
eberlm@62049
    75
  assumes "n \<ge> 2"
eberlm@62049
    76
  shows   "natlog2 n = 1 + natlog2 (n div 2)"
eberlm@62049
    77
proof (rule natlog2_eqI)
hoelzl@63594
    78
  from assms have "2 ^ (1 + natlog2 (n div 2)) \<le> 2 * (n div 2)"
eberlm@62049
    79
    by (simp add: pow_natlog2_le)
eberlm@62049
    80
  also have "\<dots> \<le> n" by simp
eberlm@62049
    81
  finally show "2 ^ (1 + natlog2 (n div 2)) \<le> n" .
eberlm@62049
    82
next
hoelzl@63594
    83
  from assms have "n < 2 * (n div 2 + 1)" by simp
hoelzl@63594
    84
  also from assms have "(n div 2) < 2 ^ (1 + natlog2 (n div 2))"
eberlm@62049
    85
    by (simp add: pow_natlog2_gt)
hoelzl@63594
    86
  hence "2 * (n div 2 + 1) \<le> 2 * (2 ^ (1 + natlog2 (n div 2)))"
eberlm@62049
    87
    by (intro mult_left_mono) simp_all
eberlm@62049
    88
  finally show "n < 2 * 2 ^ (1 + natlog2 (n div 2))" .
eberlm@62049
    89
qed (insert assms, simp_all)
eberlm@62049
    90
eberlm@62049
    91
fun natlog2_aux where
eberlm@62049
    92
  "natlog2_aux n acc = (if (n::nat) < 2 then acc else natlog2_aux (n div 2) (acc + 1))"
eberlm@62049
    93
eberlm@62049
    94
lemma natlog2_aux_correct:
eberlm@62049
    95
  "natlog2_aux n acc = acc + natlog2 n"
eberlm@62049
    96
  by (induction n acc rule: natlog2_aux.induct) (auto simp: natlog2_rec natlog2_eq_0_iff)
hoelzl@63594
    97
eberlm@62049
    98
lemma natlog2_code [code]: "natlog2 n = natlog2_aux n 0"
eberlm@62049
    99
  by (subst natlog2_aux_correct) simp
eberlm@62049
   100
eberlm@62049
   101
eberlm@62049
   102
subsection \<open>Convergence tests for infinite sums\<close>
eberlm@62049
   103
eberlm@62049
   104
subsubsection \<open>Root test\<close>
eberlm@62049
   105
eberlm@62049
   106
lemma limsup_root_powser:
eberlm@62049
   107
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
hoelzl@63594
   108
  shows "limsup (\<lambda>n. ereal (root n (norm (f n * z ^ n)))) =
eberlm@62049
   109
             limsup (\<lambda>n. ereal (root n (norm (f n)))) * ereal (norm z)"
eberlm@62049
   110
proof -
hoelzl@63594
   111
  have A: "(\<lambda>n. ereal (root n (norm (f n * z ^ n)))) =
eberlm@62049
   112
              (\<lambda>n. ereal (root n (norm (f n))) * ereal (norm z))" (is "?g = ?h")
eberlm@62049
   113
  proof
eberlm@62049
   114
    fix n show "?g n = ?h n"
eberlm@62049
   115
    by (cases "n = 0") (simp_all add: norm_mult real_root_mult real_root_pos2 norm_power)
eberlm@62049
   116
  qed
eberlm@62049
   117
  show ?thesis by (subst A, subst limsup_ereal_mult_right) simp_all
eberlm@62049
   118
qed
eberlm@62049
   119
eberlm@62049
   120
lemma limsup_root_limit:
eberlm@62049
   121
  assumes "(\<lambda>n. ereal (root n (norm (f n)))) \<longlonglongrightarrow> l" (is "?g \<longlonglongrightarrow> _")
eberlm@62049
   122
  shows   "limsup (\<lambda>n. ereal (root n (norm (f n)))) = l"
eberlm@62049
   123
proof -
eberlm@62049
   124
  from assms have "convergent ?g" "lim ?g = l"
eberlm@62049
   125
    unfolding convergent_def by (blast intro: limI)+
eberlm@62049
   126
  with convergent_limsup_cl show ?thesis by force
eberlm@62049
   127
qed
eberlm@62049
   128
eberlm@62049
   129
lemma limsup_root_limit':
eberlm@62049
   130
  assumes "(\<lambda>n. root n (norm (f n))) \<longlonglongrightarrow> l"
eberlm@62049
   131
  shows   "limsup (\<lambda>n. ereal (root n (norm (f n)))) = ereal l"
eberlm@62049
   132
  by (intro limsup_root_limit tendsto_ereal assms)
eberlm@62049
   133
eberlm@62049
   134
lemma root_test_convergence':
eberlm@62049
   135
  fixes f :: "nat \<Rightarrow> 'a :: banach"
eberlm@62049
   136
  defines "l \<equiv> limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   137
  assumes l: "l < 1"
eberlm@62049
   138
  shows   "summable f"
eberlm@62049
   139
proof -
eberlm@62049
   140
  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
eberlm@62049
   141
  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   142
  finally have "l \<ge> 0" by simp
eberlm@62049
   143
  with l obtain l' where l': "l = ereal l'" by (cases l) simp_all
eberlm@62049
   144
wenzelm@63040
   145
  define c where "c = (1 - l') / 2"
hoelzl@63594
   146
  from l and \<open>l \<ge> 0\<close> have c: "l + c > l" "l' + c \<ge> 0" "l' + c < 1" unfolding c_def
eberlm@62049
   147
    by (simp_all add: field_simps l')
eberlm@62049
   148
  have "\<forall>C>l. eventually (\<lambda>n. ereal (root n (norm (f n))) < C) sequentially"
eberlm@62049
   149
    by (subst Limsup_le_iff[symmetric]) (simp add: l_def)
eberlm@62049
   150
  with c have "eventually (\<lambda>n. ereal (root n (norm (f n))) < l + ereal c) sequentially" by simp
eberlm@62049
   151
  with eventually_gt_at_top[of "0::nat"]
eberlm@62049
   152
    have "eventually (\<lambda>n. norm (f n) \<le> (l' + c) ^ n) sequentially"
eberlm@62049
   153
  proof eventually_elim
hoelzl@63594
   154
    fix n :: nat assume n: "n > 0"
eberlm@62049
   155
    assume "ereal (root n (norm (f n))) < l + ereal c"
eberlm@62049
   156
    hence "root n (norm (f n)) \<le> l' + c" by (simp add: l')
eberlm@62049
   157
    with c n have "root n (norm (f n)) ^ n \<le> (l' + c) ^ n"
eberlm@62049
   158
      by (intro power_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   159
    also from n have "root n (norm (f n)) ^ n = norm (f n)" by simp
eberlm@62049
   160
    finally show "norm (f n) \<le> (l' + c) ^ n" by simp
eberlm@62049
   161
  qed
eberlm@62049
   162
  thus ?thesis
eberlm@62049
   163
    by (rule summable_comparison_test_ev[OF _ summable_geometric]) (simp add: c)
eberlm@62049
   164
qed
eberlm@62049
   165
eberlm@62049
   166
lemma root_test_divergence:
eberlm@62049
   167
  fixes f :: "nat \<Rightarrow> 'a :: banach"
eberlm@62049
   168
  defines "l \<equiv> limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   169
  assumes l: "l > 1"
eberlm@62049
   170
  shows   "\<not>summable f"
eberlm@62049
   171
proof
eberlm@62049
   172
  assume "summable f"
eberlm@62049
   173
  hence bounded: "Bseq f" by (simp add: summable_imp_Bseq)
eberlm@62049
   174
eberlm@62049
   175
  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
eberlm@62049
   176
  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   177
  finally have l_nonneg: "l \<ge> 0" by simp
eberlm@62049
   178
wenzelm@63040
   179
  define c where "c = (if l = \<infinity> then 2 else 1 + (real_of_ereal l - 1) / 2)"
eberlm@62049
   180
  from l l_nonneg consider "l = \<infinity>" | "\<exists>l'. l = ereal l'" by (cases l) simp_all
eberlm@62049
   181
  hence c: "c > 1 \<and> ereal c < l" by cases (insert l, auto simp: c_def field_simps)
eberlm@62049
   182
eberlm@62049
   183
  have unbounded: "\<not>bdd_above {n. root n (norm (f n)) > c}"
eberlm@62049
   184
  proof
eberlm@62049
   185
    assume "bdd_above {n. root n (norm (f n)) > c}"
eberlm@62049
   186
    then obtain N where "\<forall>n. root n (norm (f n)) > c \<longrightarrow> n \<le> N" unfolding bdd_above_def by blast
eberlm@62049
   187
    hence "\<exists>N. \<forall>n\<ge>N. root n (norm (f n)) \<le> c"
eberlm@62049
   188
      by (intro exI[of _ "N + 1"]) (force simp: not_less_eq_eq[symmetric])
eberlm@62049
   189
    hence "eventually (\<lambda>n. root n (norm (f n)) \<le> c) sequentially"
eberlm@62049
   190
      by (auto simp: eventually_at_top_linorder)
eberlm@62049
   191
    hence "l \<le> c" unfolding l_def by (intro Limsup_bounded) simp_all
eberlm@62049
   192
    with c show False by auto
eberlm@62049
   193
  qed
hoelzl@63594
   194
eberlm@62049
   195
  from bounded obtain K where K: "K > 0" "\<And>n. norm (f n) \<le> K" using BseqE by blast
wenzelm@63040
   196
  define n where "n = nat \<lceil>log c K\<rceil>"
eberlm@62049
   197
  from unbounded have "\<exists>m>n. c < root m (norm (f m))" unfolding bdd_above_def
eberlm@62049
   198
    by (auto simp: not_le)
eberlm@62049
   199
  then guess m by (elim exE conjE) note m = this
eberlm@62049
   200
  from c K have "K = c powr log c K" by (simp add: powr_def log_def)
eberlm@62049
   201
  also from c have "c powr log c K \<le> c powr real n" unfolding n_def
eberlm@62049
   202
    by (intro powr_mono, linarith, simp)
eberlm@62049
   203
  finally have "K \<le> c ^ n" using c by (simp add: powr_realpow)
eberlm@62049
   204
  also from c m have "c ^ n < c ^ m" by simp
eberlm@62049
   205
  also from c m have "c ^ m < root m (norm (f m)) ^ m" by (intro power_strict_mono) simp_all
eberlm@62049
   206
  also from m have "... = norm (f m)" by simp
eberlm@62049
   207
  finally show False using K(2)[of m]  by simp
eberlm@62049
   208
qed
eberlm@62049
   209
eberlm@62049
   210
eberlm@62085
   211
subsubsection \<open>Cauchy's condensation test\<close>
eberlm@62049
   212
eberlm@62049
   213
context
eberlm@62049
   214
fixes f :: "nat \<Rightarrow> real"
eberlm@62049
   215
begin
eberlm@62049
   216
eberlm@62049
   217
private lemma condensation_inequality:
eberlm@62049
   218
  assumes mono: "\<And>m n. 0 < m \<Longrightarrow> m \<le> n \<Longrightarrow> f n \<le> f m"
eberlm@62049
   219
  shows   "(\<Sum>k=1..<n. f k) \<ge> (\<Sum>k=1..<n. f (2 * 2 ^ natlog2 k))" (is "?thesis1")
eberlm@62049
   220
          "(\<Sum>k=1..<n. f k) \<le> (\<Sum>k=1..<n. f (2 ^ natlog2 k))" (is "?thesis2")
eberlm@62049
   221
  by (intro setsum_mono mono pow_natlog2_ge pow_natlog2_le, simp, simp)+
eberlm@62049
   222
eberlm@62049
   223
private lemma condensation_condense1: "(\<Sum>k=1..<2^n. f (2 ^ natlog2 k)) = (\<Sum>k<n. 2^k * f (2 ^ k))"
eberlm@62049
   224
proof (induction n)
eberlm@62049
   225
  case (Suc n)
hoelzl@63594
   226
  have "{1..<2^Suc n} = {1..<2^n} \<union> {2^n..<(2^Suc n :: nat)}" by auto
hoelzl@63594
   227
  also have "(\<Sum>k\<in>\<dots>. f (2 ^ natlog2 k)) =
hoelzl@63594
   228
                 (\<Sum>k<n. 2^k * f (2^k)) + (\<Sum>k = 2^n..<2^Suc n. f (2^natlog2 k))"
eberlm@62049
   229
    by (subst setsum.union_disjoint) (insert Suc, auto)
eberlm@62049
   230
  also have "natlog2 k = n" if "k \<in> {2^n..<2^Suc n}" for k using that by (intro natlog2_eqI) simp_all
eberlm@62049
   231
  hence "(\<Sum>k = 2^n..<2^Suc n. f (2^natlog2 k)) = (\<Sum>(_::nat) = 2^n..<2^Suc n. f (2^n))"
eberlm@62049
   232
    by (intro setsum.cong) simp_all
eberlm@62049
   233
  also have "\<dots> = 2^n * f (2^n)" by (simp add: of_nat_power)
eberlm@62049
   234
  finally show ?case by simp
eberlm@62049
   235
qed simp
eberlm@62049
   236
eberlm@62049
   237
private lemma condensation_condense2: "(\<Sum>k=1..<2^n. f (2 * 2 ^ natlog2 k)) = (\<Sum>k<n. 2^k * f (2 ^ Suc k))"
eberlm@62049
   238
proof (induction n)
eberlm@62049
   239
  case (Suc n)
hoelzl@63594
   240
  have "{1..<2^Suc n} = {1..<2^n} \<union> {2^n..<(2^Suc n :: nat)}" by auto
hoelzl@63594
   241
  also have "(\<Sum>k\<in>\<dots>. f (2 * 2 ^ natlog2 k)) =
hoelzl@63594
   242
                 (\<Sum>k<n. 2^k * f (2^Suc k)) + (\<Sum>k = 2^n..<2^Suc n. f (2 * 2^natlog2 k))"
eberlm@62049
   243
    by (subst setsum.union_disjoint) (insert Suc, auto)
eberlm@62049
   244
  also have "natlog2 k = n" if "k \<in> {2^n..<2^Suc n}" for k using that by (intro natlog2_eqI) simp_all
eberlm@62049
   245
  hence "(\<Sum>k = 2^n..<2^Suc n. f (2*2^natlog2 k)) = (\<Sum>(_::nat) = 2^n..<2^Suc n. f (2^Suc n))"
eberlm@62049
   246
    by (intro setsum.cong) simp_all
eberlm@62049
   247
  also have "\<dots> = 2^n * f (2^Suc n)" by (simp add: of_nat_power)
eberlm@62049
   248
  finally show ?case by simp
eberlm@62049
   249
qed simp
eberlm@62049
   250
eberlm@62049
   251
lemma condensation_test:
eberlm@62049
   252
  assumes mono: "\<And>m. 0 < m \<Longrightarrow> f (Suc m) \<le> f m"
eberlm@62049
   253
  assumes nonneg: "\<And>n. f n \<ge> 0"
eberlm@62049
   254
  shows "summable f \<longleftrightarrow> summable (\<lambda>n. 2^n * f (2^n))"
eberlm@62049
   255
proof -
wenzelm@63040
   256
  define f' where "f' n = (if n = 0 then 0 else f n)" for n
eberlm@62049
   257
  from mono have mono': "decseq (\<lambda>n. f (Suc n))" by (intro decseq_SucI) simp
hoelzl@63594
   258
  hence mono': "f n \<le> f m" if "m \<le> n" "m > 0" for m n
eberlm@62049
   259
    using that decseqD[OF mono', of "m - 1" "n - 1"] by simp
hoelzl@63594
   260
eberlm@62049
   261
  have "(\<lambda>n. f (Suc n)) = (\<lambda>n. f' (Suc n))" by (intro ext) (simp add: f'_def)
eberlm@62049
   262
  hence "summable f \<longleftrightarrow> summable f'"
eberlm@62049
   263
    by (subst (1 2) summable_Suc_iff [symmetric]) (simp only:)
eberlm@62049
   264
  also have "\<dots> \<longleftrightarrow> convergent (\<lambda>n. \<Sum>k<n. f' k)" unfolding summable_iff_convergent ..
eberlm@62049
   265
  also have "monoseq (\<lambda>n. \<Sum>k<n. f' k)" unfolding f'_def
eberlm@62049
   266
    by (intro mono_SucI1) (auto intro!: mult_nonneg_nonneg nonneg)
eberlm@62049
   267
  hence "convergent (\<lambda>n. \<Sum>k<n. f' k) \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k<n. f' k)"
eberlm@62049
   268
    by (rule monoseq_imp_convergent_iff_Bseq)
eberlm@62049
   269
  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<n. f' k)" unfolding One_nat_def
eberlm@62049
   270
    by (subst setsum_shift_lb_Suc0_0_upt) (simp_all add: f'_def atLeast0LessThan)
eberlm@62049
   271
  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<n. f k)" unfolding f'_def by simp
eberlm@62049
   272
  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)"
eberlm@62049
   273
    by (rule nonneg_incseq_Bseq_subseq_iff[symmetric])
eberlm@62049
   274
       (auto intro!: setsum_nonneg incseq_SucI nonneg simp: subseq_def)
eberlm@62049
   275
  also have "\<dots> \<longleftrightarrow> Bseq (\<lambda>n. \<Sum>k<n. 2^k * f (2^k))"
eberlm@62049
   276
  proof (intro iffI)
eberlm@62049
   277
    assume A: "Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)"
eberlm@62049
   278
    have "eventually (\<lambda>n. norm (\<Sum>k<n. 2^k * f (2^Suc k)) \<le> norm (\<Sum>k=1..<2^n. f k)) sequentially"
eberlm@62049
   279
    proof (intro always_eventually allI)
eberlm@62049
   280
      fix n :: nat
eberlm@62049
   281
      have "norm (\<Sum>k<n. 2^k * f (2^Suc k)) = (\<Sum>k<n. 2^k * f (2^Suc k))" unfolding real_norm_def
eberlm@62049
   282
        by (intro abs_of_nonneg setsum_nonneg ballI mult_nonneg_nonneg nonneg) simp_all
eberlm@62049
   283
      also have "\<dots> \<le> (\<Sum>k=1..<2^n. f k)"
eberlm@62049
   284
        by (subst condensation_condense2 [symmetric]) (intro condensation_inequality mono')
eberlm@62049
   285
      also have "\<dots> = norm \<dots>" unfolding real_norm_def
eberlm@62049
   286
        by (intro abs_of_nonneg[symmetric] setsum_nonneg ballI mult_nonneg_nonneg nonneg)
eberlm@62049
   287
      finally show "norm (\<Sum>k<n. 2 ^ k * f (2 ^ Suc k)) \<le> norm (\<Sum>k=1..<2^n. f k)" .
eberlm@62049
   288
    qed
eberlm@62049
   289
    from this and A have "Bseq (\<lambda>n. \<Sum>k<n. 2^k * f (2^Suc k))" by (rule Bseq_eventually_mono)
eberlm@62049
   290
    from Bseq_mult[OF Bfun_const[of 2] this] have "Bseq (\<lambda>n. \<Sum>k<n. 2^Suc k * f (2^Suc k))"
eberlm@62049
   291
      by (simp add: setsum_right_distrib setsum_left_distrib mult_ac)
eberlm@62049
   292
    hence "Bseq (\<lambda>n. (\<Sum>k=Suc 0..<Suc n. 2^k * f (2^k)) + f 1)"
eberlm@62049
   293
      by (intro Bseq_add, subst setsum_shift_bounds_Suc_ivl) (simp add: atLeast0LessThan)
eberlm@62049
   294
    hence "Bseq (\<lambda>n. (\<Sum>k=0..<Suc n. 2^k * f (2^k)))"
eberlm@62049
   295
      by (subst setsum_head_upt_Suc) (simp_all add: add_ac)
hoelzl@63594
   296
    thus "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
eberlm@62049
   297
      by (subst (asm) Bseq_Suc_iff) (simp add: atLeast0LessThan)
eberlm@62049
   298
  next
eberlm@62049
   299
    assume A: "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
eberlm@62049
   300
    have "eventually (\<lambda>n. norm (\<Sum>k=1..<2^n. f k) \<le> norm (\<Sum>k<n. 2^k * f (2^k))) sequentially"
eberlm@62049
   301
    proof (intro always_eventually allI)
eberlm@62049
   302
      fix n :: nat
eberlm@62049
   303
      have "norm (\<Sum>k=1..<2^n. f k) = (\<Sum>k=1..<2^n. f k)" unfolding real_norm_def
eberlm@62049
   304
        by (intro abs_of_nonneg setsum_nonneg ballI mult_nonneg_nonneg nonneg)
eberlm@62049
   305
      also have "\<dots> \<le> (\<Sum>k<n. 2^k * f (2^k))"
eberlm@62049
   306
        by (subst condensation_condense1 [symmetric]) (intro condensation_inequality mono')
eberlm@62049
   307
      also have "\<dots> = norm \<dots>" unfolding real_norm_def
eberlm@62049
   308
        by (intro abs_of_nonneg [symmetric] setsum_nonneg ballI mult_nonneg_nonneg nonneg) simp_all
eberlm@62049
   309
      finally show "norm (\<Sum>k=1..<2^n. f k) \<le> norm (\<Sum>k<n. 2^k * f (2^k))" .
eberlm@62049
   310
    qed
eberlm@62049
   311
    from this and A show "Bseq (\<lambda>n. \<Sum>k=1..<2^n. f k)" by (rule Bseq_eventually_mono)
eberlm@62049
   312
  qed
eberlm@62049
   313
  also have "monoseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
eberlm@62049
   314
    by (intro mono_SucI1) (auto intro!: mult_nonneg_nonneg nonneg)
eberlm@62049
   315
  hence "Bseq (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k))) \<longleftrightarrow> convergent (\<lambda>n. (\<Sum>k<n. 2^k * f (2^k)))"
eberlm@62049
   316
    by (rule monoseq_imp_convergent_iff_Bseq [symmetric])
eberlm@62049
   317
  also have "\<dots> \<longleftrightarrow> summable (\<lambda>k. 2^k * f (2^k))" by (simp only: summable_iff_convergent)
eberlm@62049
   318
  finally show ?thesis .
eberlm@62049
   319
qed
eberlm@62049
   320
eberlm@62049
   321
end
eberlm@62049
   322
eberlm@62049
   323
eberlm@62085
   324
subsubsection \<open>Summability of powers\<close>
eberlm@62049
   325
hoelzl@63594
   326
lemma abs_summable_complex_powr_iff:
eberlm@62049
   327
    "summable (\<lambda>n. norm (exp (of_real (ln (of_nat n)) * s))) \<longleftrightarrow> Re s < -1"
eberlm@62049
   328
proof (cases "Re s \<le> 0")
eberlm@62049
   329
  let ?l = "\<lambda>n. complex_of_real (ln (of_nat n))"
eberlm@62049
   330
  case False
eberlm@62049
   331
  with eventually_gt_at_top[of "0::nat"]
hoelzl@63594
   332
    have "eventually (\<lambda>n. norm (1 :: real) \<le> norm (exp (?l n * s))) sequentially"
eberlm@62049
   333
    by (auto intro!: ge_one_powr_ge_zero elim!: eventually_mono)
eberlm@62049
   334
  from summable_comparison_test_ev[OF this] False show ?thesis by (auto simp: summable_const_iff)
eberlm@62049
   335
next
eberlm@62049
   336
  let ?l = "\<lambda>n. complex_of_real (ln (of_nat n))"
eberlm@62049
   337
  case True
eberlm@62049
   338
  hence "summable (\<lambda>n. norm (exp (?l n * s))) \<longleftrightarrow> summable (\<lambda>n. 2^n * norm (exp (?l (2^n) * s)))"
eberlm@62049
   339
    by (intro condensation_test) (auto intro!: mult_right_mono_neg)
eberlm@62049
   340
  also have "(\<lambda>n. 2^n * norm (exp (?l (2^n) * s))) = (\<lambda>n. (2 powr (Re s + 1)) ^ n)"
eberlm@62049
   341
  proof
eberlm@62049
   342
    fix n :: nat
eberlm@62049
   343
    have "2^n * norm (exp (?l (2^n) * s)) = exp (real n * ln 2) * exp (real n * ln 2 * Re s)"
hoelzl@63594
   344
      using True by (subst exp_of_nat_mult) (simp add: ln_realpow algebra_simps)
eberlm@62049
   345
    also have "\<dots> = exp (real n * (ln 2 * (Re s + 1)))"
eberlm@62049
   346
      by (simp add: algebra_simps exp_add)
eberlm@62049
   347
    also have "\<dots> = exp (ln 2 * (Re s + 1)) ^ n" by (subst exp_of_nat_mult) simp
eberlm@62049
   348
    also have "exp (ln 2 * (Re s + 1)) = 2 powr (Re s + 1)" by (simp add: powr_def)
eberlm@62049
   349
    finally show "2^n * norm (exp (?l (2^n) * s)) = (2 powr (Re s + 1)) ^ n" .
eberlm@62049
   350
  qed
eberlm@62049
   351
  also have "summable \<dots> \<longleftrightarrow> 2 powr (Re s + 1) < 2 powr 0"
eberlm@62049
   352
    by (subst summable_geometric_iff) simp
eberlm@62049
   353
  also have "\<dots> \<longleftrightarrow> Re s < -1" by (subst powr_less_cancel_iff) (simp, linarith)
eberlm@62049
   354
  finally show ?thesis .
eberlm@62049
   355
qed
eberlm@62049
   356
hoelzl@63594
   357
lemma summable_complex_powr_iff:
eberlm@62049
   358
  assumes "Re s < -1"
eberlm@62049
   359
  shows   "summable (\<lambda>n. exp (of_real (ln (of_nat n)) * s))"
eberlm@62049
   360
  by (rule summable_norm_cancel, subst abs_summable_complex_powr_iff) fact
eberlm@62049
   361
eberlm@62049
   362
lemma summable_real_powr_iff: "summable (\<lambda>n. of_nat n powr s :: real) \<longleftrightarrow> s < -1"
eberlm@62049
   363
proof -
eberlm@62049
   364
  from eventually_gt_at_top[of "0::nat"]
eberlm@62049
   365
    have "summable (\<lambda>n. of_nat n powr s) \<longleftrightarrow> summable (\<lambda>n. exp (ln (of_nat n) * s))"
eberlm@62049
   366
    by (intro summable_cong) (auto elim!: eventually_mono simp: powr_def)
eberlm@62049
   367
  also have "\<dots> \<longleftrightarrow> s < -1" using abs_summable_complex_powr_iff[of "of_real s"] by simp
eberlm@62049
   368
  finally show ?thesis .
eberlm@62049
   369
qed
eberlm@62049
   370
eberlm@62049
   371
lemma inverse_power_summable:
eberlm@62049
   372
  assumes s: "s \<ge> 2"
eberlm@62049
   373
  shows "summable (\<lambda>n. inverse (of_nat n ^ s :: 'a :: {real_normed_div_algebra,banach}))"
eberlm@62049
   374
proof (rule summable_norm_cancel, subst summable_cong)
eberlm@62049
   375
  from eventually_gt_at_top[of "0::nat"]
eberlm@62049
   376
    show "eventually (\<lambda>n. norm (inverse (of_nat n ^ s:: 'a)) = real_of_nat n powr (-real s)) at_top"
eberlm@62049
   377
    by eventually_elim (simp add: norm_inverse norm_power powr_minus powr_realpow)
eberlm@62049
   378
qed (insert s summable_real_powr_iff[of "-s"], simp_all)
eberlm@62049
   379
eberlm@62049
   380
lemma not_summable_harmonic: "\<not>summable (\<lambda>n. inverse (of_nat n) :: 'a :: real_normed_field)"
eberlm@62049
   381
proof
eberlm@62049
   382
  assume "summable (\<lambda>n. inverse (of_nat n) :: 'a)"
hoelzl@63594
   383
  hence "convergent (\<lambda>n. norm (of_real (\<Sum>k<n. inverse (of_nat k)) :: 'a))"
eberlm@62049
   384
    by (simp add: summable_iff_convergent convergent_norm)
eberlm@62049
   385
  hence "convergent (\<lambda>n. abs (\<Sum>k<n. inverse (of_nat k)) :: real)" by (simp only: norm_of_real)
eberlm@62049
   386
  also have "(\<lambda>n. abs (\<Sum>k<n. inverse (of_nat k)) :: real) = (\<lambda>n. \<Sum>k<n. inverse (of_nat k))"
eberlm@62049
   387
    by (intro ext abs_of_nonneg setsum_nonneg) auto
eberlm@62049
   388
  also have "convergent \<dots> \<longleftrightarrow> summable (\<lambda>k. inverse (of_nat k) :: real)"
eberlm@62049
   389
    by (simp add: summable_iff_convergent)
eberlm@62049
   390
  finally show False using summable_real_powr_iff[of "-1"] by (simp add: powr_minus)
eberlm@62049
   391
qed
eberlm@62049
   392
eberlm@62049
   393
eberlm@62085
   394
subsubsection \<open>Kummer's test\<close>
eberlm@62049
   395
eberlm@62049
   396
lemma kummers_test_convergence:
eberlm@62049
   397
  fixes f p :: "nat \<Rightarrow> real"
hoelzl@63594
   398
  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   399
  assumes nonneg_p: "eventually (\<lambda>n. p n \<ge> 0) sequentially"
eberlm@62049
   400
  defines "l \<equiv> liminf (\<lambda>n. ereal (p n * f n / f (Suc n) - p (Suc n)))"
eberlm@62049
   401
  assumes l: "l > 0"
eberlm@62049
   402
  shows   "summable f"
eberlm@62049
   403
  unfolding summable_iff_convergent'
eberlm@62049
   404
proof -
wenzelm@63040
   405
  define r where "r = (if l = \<infinity> then 1 else real_of_ereal l / 2)"
eberlm@62049
   406
  from l have "r > 0 \<and> of_real r < l" by (cases l) (simp_all add: r_def)
eberlm@62049
   407
  hence r: "r > 0" "of_real r < l" by simp_all
eberlm@62049
   408
  hence "eventually (\<lambda>n. p n * f n / f (Suc n) - p (Suc n) > r) sequentially"
eberlm@62049
   409
    unfolding l_def by (force dest: less_LiminfD)
hoelzl@63594
   410
  moreover from pos_f have "eventually (\<lambda>n. f (Suc n) > 0) sequentially"
eberlm@62049
   411
    by (subst eventually_sequentially_Suc)
eberlm@62049
   412
  ultimately have "eventually (\<lambda>n. p n * f n - p (Suc n) * f (Suc n) > r * f (Suc n)) sequentially"
eberlm@62049
   413
    by eventually_elim (simp add: field_simps)
eberlm@62049
   414
  from eventually_conj[OF pos_f eventually_conj[OF nonneg_p this]]
eberlm@62049
   415
    obtain m where m: "\<And>n. n \<ge> m \<Longrightarrow> f n > 0" "\<And>n. n \<ge> m \<Longrightarrow> p n \<ge> 0"
eberlm@62049
   416
        "\<And>n. n \<ge> m \<Longrightarrow> p n * f n - p (Suc n) * f (Suc n) > r * f (Suc n)"
eberlm@62049
   417
    unfolding eventually_at_top_linorder by blast
eberlm@62049
   418
eberlm@62049
   419
  let ?c = "(norm (\<Sum>k\<le>m. r * f k) + p m * f m) / r"
eberlm@62049
   420
  have "Bseq (\<lambda>n. (\<Sum>k\<le>n + Suc m. f k))"
eberlm@62049
   421
  proof (rule BseqI')
eberlm@62049
   422
    fix k :: nat
wenzelm@63040
   423
    define n where "n = k + Suc m"
eberlm@62049
   424
    have n: "n > m" by (simp add: n_def)
eberlm@62049
   425
eberlm@62049
   426
    from r have "r * norm (\<Sum>k\<le>n. f k) = norm (\<Sum>k\<le>n. r * f k)"
eberlm@62049
   427
      by (simp add: setsum_right_distrib[symmetric] abs_mult)
eberlm@62049
   428
    also from n have "{..n} = {..m} \<union> {Suc m..n}" by auto
eberlm@62049
   429
    hence "(\<Sum>k\<le>n. r * f k) = (\<Sum>k\<in>{..m} \<union> {Suc m..n}. r * f k)" by (simp only:)
eberlm@62049
   430
    also have "\<dots> = (\<Sum>k\<le>m. r * f k) + (\<Sum>k=Suc m..n. r * f k)"
eberlm@62049
   431
      by (subst setsum.union_disjoint) auto
eberlm@62049
   432
    also have "norm \<dots> \<le> norm (\<Sum>k\<le>m. r * f k) + norm (\<Sum>k=Suc m..n. r * f k)"
eberlm@62049
   433
      by (rule norm_triangle_ineq)
hoelzl@63594
   434
    also from r less_imp_le[OF m(1)] have "(\<Sum>k=Suc m..n. r * f k) \<ge> 0"
eberlm@62049
   435
      by (intro setsum_nonneg) auto
eberlm@62049
   436
    hence "norm (\<Sum>k=Suc m..n. r * f k) = (\<Sum>k=Suc m..n. r * f k)" by simp
eberlm@62049
   437
    also have "(\<Sum>k=Suc m..n. r * f k) = (\<Sum>k=m..<n. r * f (Suc k))"
eberlm@62049
   438
     by (subst setsum_shift_bounds_Suc_ivl [symmetric])
eberlm@62049
   439
          (simp only: atLeastLessThanSuc_atLeastAtMost)
eberlm@62049
   440
    also from m have "\<dots> \<le> (\<Sum>k=m..<n. p k * f k - p (Suc k) * f (Suc k))"
eberlm@62049
   441
      by (intro setsum_mono[OF less_imp_le]) simp_all
eberlm@62049
   442
    also have "\<dots> = -(\<Sum>k=m..<n. p (Suc k) * f (Suc k) - p k * f k)"
eberlm@62049
   443
      by (simp add: setsum_negf [symmetric] algebra_simps)
eberlm@62049
   444
    also from n have "\<dots> = p m * f m - p n * f n"
eberlm@62049
   445
      by (cases n, simp, simp only: atLeastLessThanSuc_atLeastAtMost, subst setsum_Suc_diff) simp_all
eberlm@62049
   446
    also from less_imp_le[OF m(1)] m(2) n have "\<dots> \<le> p m * f m" by simp
eberlm@62049
   447
    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
eberlm@62049
   448
      by (subst pos_le_divide_eq[OF r(1)]) (simp only: mult_ac)
eberlm@62049
   449
  qed
eberlm@62049
   450
  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'
eberlm@62049
   451
    using less_imp_le[OF m(1)] that by (intro setsum_mono2) auto
eberlm@62049
   452
  ultimately show "convergent (\<lambda>n. \<Sum>k\<le>n. f k)" by (rule Bseq_monoseq_convergent'_inc)
eberlm@62049
   453
qed
eberlm@62049
   454
eberlm@62049
   455
eberlm@62049
   456
lemma kummers_test_divergence:
eberlm@62049
   457
  fixes f p :: "nat \<Rightarrow> real"
hoelzl@63594
   458
  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   459
  assumes pos_p: "eventually (\<lambda>n. p n > 0) sequentially"
eberlm@62049
   460
  assumes divergent_p: "\<not>summable (\<lambda>n. inverse (p n))"
eberlm@62049
   461
  defines "l \<equiv> limsup (\<lambda>n. ereal (p n * f n / f (Suc n) - p (Suc n)))"
eberlm@62049
   462
  assumes l: "l < 0"
eberlm@62049
   463
  shows   "\<not>summable f"
eberlm@62049
   464
proof
eberlm@62049
   465
  assume "summable f"
eberlm@62049
   466
  from eventually_conj[OF pos_f eventually_conj[OF pos_p Limsup_lessD[OF l[unfolded l_def]]]]
eberlm@62049
   467
    obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> p n > 0" "\<And>n. n \<ge> N \<Longrightarrow> f n > 0"
eberlm@62049
   468
                      "\<And>n. n \<ge> N \<Longrightarrow> p n * f n / f (Suc n) - p (Suc n) < 0"
eberlm@62049
   469
    by (auto simp: eventually_at_top_linorder)
hoelzl@63594
   470
  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"]
eberlm@62049
   471
    by (simp add: field_simps)
eberlm@62049
   472
  have "p n * f n \<ge> p N * f N" if "n \<ge> N" for n using that and A
eberlm@62049
   473
      by (induction n rule: dec_induct) (auto intro!: less_imp_le elim!: order.trans)
eberlm@62049
   474
  from eventually_ge_at_top[of N] N this
eberlm@62049
   475
    have "eventually (\<lambda>n. norm (p N * f N * inverse (p n)) \<le> f n) sequentially"
eberlm@62049
   476
    by (auto elim!: eventually_mono simp: field_simps abs_of_pos)
eberlm@62049
   477
  from this and \<open>summable f\<close> have "summable (\<lambda>n. p N * f N * inverse (p n))"
eberlm@62049
   478
    by (rule summable_comparison_test_ev)
hoelzl@63594
   479
  from summable_mult[OF this, of "inverse (p N * f N)"] N[OF le_refl]
eberlm@62049
   480
    have "summable (\<lambda>n. inverse (p n))" by (simp add: divide_simps)
eberlm@62049
   481
  with divergent_p show False by contradiction
eberlm@62049
   482
qed
eberlm@62049
   483
eberlm@62049
   484
eberlm@62085
   485
subsubsection \<open>Ratio test\<close>
eberlm@62049
   486
eberlm@62049
   487
lemma ratio_test_convergence:
eberlm@62049
   488
  fixes f :: "nat \<Rightarrow> real"
hoelzl@63594
   489
  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   490
  defines "l \<equiv> liminf (\<lambda>n. ereal (f n / f (Suc n)))"
eberlm@62049
   491
  assumes l: "l > 1"
eberlm@62049
   492
  shows   "summable f"
eberlm@62049
   493
proof (rule kummers_test_convergence[OF pos_f])
eberlm@62049
   494
  note l
hoelzl@63594
   495
  also have "l = liminf (\<lambda>n. ereal (f n / f (Suc n) - 1)) + 1"
eberlm@62049
   496
    by (subst Liminf_add_ereal_right[symmetric]) (simp_all add: minus_ereal_def l_def one_ereal_def)
eberlm@62049
   497
  finally show "liminf (\<lambda>n. ereal (1 * f n / f (Suc n) - 1)) > 0"
eberlm@62049
   498
    by (cases "liminf (\<lambda>n. ereal (1 * f n / f (Suc n) - 1))") simp_all
eberlm@62049
   499
qed simp
eberlm@62049
   500
eberlm@62049
   501
lemma ratio_test_divergence:
eberlm@62049
   502
  fixes f :: "nat \<Rightarrow> real"
hoelzl@63594
   503
  assumes pos_f: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   504
  defines "l \<equiv> limsup (\<lambda>n. ereal (f n / f (Suc n)))"
eberlm@62049
   505
  assumes l: "l < 1"
eberlm@62049
   506
  shows   "\<not>summable f"
eberlm@62049
   507
proof (rule kummers_test_divergence[OF pos_f])
hoelzl@63594
   508
  have "limsup (\<lambda>n. ereal (f n / f (Suc n) - 1)) + 1 = l"
eberlm@62049
   509
    by (subst Limsup_add_ereal_right[symmetric]) (simp_all add: minus_ereal_def l_def one_ereal_def)
eberlm@62049
   510
  also note l
eberlm@62049
   511
  finally show "limsup (\<lambda>n. ereal (1 * f n / f (Suc n) - 1)) < 0"
eberlm@62049
   512
    by (cases "limsup (\<lambda>n. ereal (1 * f n / f (Suc n) - 1))") simp_all
eberlm@62049
   513
qed (simp_all add: summable_const_iff)
eberlm@62049
   514
eberlm@62049
   515
eberlm@62085
   516
subsubsection \<open>Raabe's test\<close>
eberlm@62049
   517
eberlm@62049
   518
lemma raabes_test_convergence:
eberlm@62049
   519
fixes f :: "nat \<Rightarrow> real"
eberlm@62049
   520
  assumes pos: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   521
  defines "l \<equiv> liminf (\<lambda>n. ereal (of_nat n * (f n / f (Suc n) - 1)))"
eberlm@62049
   522
  assumes l: "l > 1"
eberlm@62049
   523
  shows   "summable f"
eberlm@62049
   524
proof (rule kummers_test_convergence)
eberlm@62049
   525
  let ?l' = "liminf (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)))"
eberlm@62049
   526
  have "1 < l" by fact
eberlm@62049
   527
  also have "l = liminf (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)) + 1)"
eberlm@62049
   528
    by (simp add: l_def algebra_simps)
eberlm@62049
   529
  also have "\<dots> = ?l' + 1" by (subst Liminf_add_ereal_right) simp_all
eberlm@62049
   530
  finally show "?l' > 0" by (cases ?l') (simp_all add: algebra_simps)
eberlm@62049
   531
qed (simp_all add: pos)
eberlm@62049
   532
eberlm@62049
   533
lemma raabes_test_divergence:
eberlm@62049
   534
fixes f :: "nat \<Rightarrow> real"
eberlm@62049
   535
  assumes pos: "eventually (\<lambda>n. f n > 0) sequentially"
eberlm@62049
   536
  defines "l \<equiv> limsup (\<lambda>n. ereal (of_nat n * (f n / f (Suc n) - 1)))"
eberlm@62049
   537
  assumes l: "l < 1"
eberlm@62049
   538
  shows   "\<not>summable f"
eberlm@62049
   539
proof (rule kummers_test_divergence)
eberlm@62049
   540
  let ?l' = "limsup (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)))"
eberlm@62049
   541
  note l
eberlm@62049
   542
  also have "l = limsup (\<lambda>n. ereal (of_nat n * f n / f (Suc n) - of_nat (Suc n)) + 1)"
eberlm@62049
   543
    by (simp add: l_def algebra_simps)
eberlm@62049
   544
  also have "\<dots> = ?l' + 1" by (subst Limsup_add_ereal_right) simp_all
eberlm@62049
   545
  finally show "?l' < 0" by (cases ?l') (simp_all add: algebra_simps)
eberlm@62049
   546
qed (insert pos eventually_gt_at_top[of "0::nat"] not_summable_harmonic, simp_all)
eberlm@62049
   547
eberlm@62049
   548
eberlm@62049
   549
eberlm@62049
   550
subsection \<open>Radius of convergence\<close>
eberlm@62049
   551
eberlm@62049
   552
text \<open>
eberlm@62049
   553
  The radius of convergence of a power series. This value always exists, ranges from
hoelzl@63594
   554
  @{term "0::ereal"} to @{term "\<infinity>::ereal"}, and the power series is guaranteed to converge for
eberlm@62049
   555
  all inputs with a norm that is smaller than that radius and to diverge for all inputs with a
hoelzl@63594
   556
  norm that is greater.
eberlm@62049
   557
\<close>
eberlm@62049
   558
definition conv_radius :: "(nat \<Rightarrow> 'a :: banach) \<Rightarrow> ereal" where
eberlm@62049
   559
  "conv_radius f = inverse (limsup (\<lambda>n. ereal (root n (norm (f n)))))"
eberlm@62049
   560
eberlm@62049
   561
lemma conv_radius_nonneg: "conv_radius f \<ge> 0"
eberlm@62049
   562
proof -
eberlm@62049
   563
  have "0 = limsup (\<lambda>n. 0)" by (subst Limsup_const) simp_all
eberlm@62049
   564
  also have "\<dots> \<le> limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   565
    by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   566
  finally show ?thesis
eberlm@62049
   567
    unfolding conv_radius_def by (auto simp: ereal_inverse_nonneg_iff)
eberlm@62049
   568
qed
eberlm@62049
   569
eberlm@62049
   570
lemma conv_radius_zero [simp]: "conv_radius (\<lambda>_. 0) = \<infinity>"
eberlm@62049
   571
  by (auto simp: conv_radius_def zero_ereal_def [symmetric] Limsup_const)
eberlm@62049
   572
eberlm@62049
   573
lemma conv_radius_cong:
eberlm@62049
   574
  assumes "eventually (\<lambda>x. f x = g x) sequentially"
eberlm@62049
   575
  shows   "conv_radius f = conv_radius g"
eberlm@62049
   576
proof -
eberlm@62049
   577
  have "eventually (\<lambda>n. ereal (root n (norm (f n))) = ereal (root n (norm (g n)))) sequentially"
eberlm@62049
   578
    using assms by eventually_elim simp
eberlm@62049
   579
  from Limsup_eq[OF this] show ?thesis unfolding conv_radius_def by simp
eberlm@62049
   580
qed
eberlm@62049
   581
eberlm@62049
   582
lemma conv_radius_altdef:
eberlm@62049
   583
  "conv_radius f = liminf (\<lambda>n. inverse (ereal (root n (norm (f n)))))"
eberlm@62049
   584
  by (subst Liminf_inverse_ereal) (simp_all add: real_root_ge_zero conv_radius_def)
eberlm@62049
   585
eberlm@62049
   586
lemma abs_summable_in_conv_radius:
eberlm@62049
   587
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   588
  assumes "ereal (norm z) < conv_radius f"
eberlm@62049
   589
  shows   "summable (\<lambda>n. norm (f n * z ^ n))"
eberlm@62049
   590
proof (rule root_test_convergence')
wenzelm@63040
   591
  define l where "l = limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   592
  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
eberlm@62049
   593
  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   594
  finally have l_nonneg: "l \<ge> 0" .
eberlm@62049
   595
eberlm@62049
   596
  have "limsup (\<lambda>n. root n (norm (f n * z^n))) = l * ereal (norm z)" unfolding l_def
eberlm@62049
   597
    by (rule limsup_root_powser)
eberlm@62049
   598
  also from l_nonneg consider "l = 0" | "l = \<infinity>" | "\<exists>l'. l = ereal l' \<and> l' > 0"
eberlm@62049
   599
    by (cases "l") (auto simp: less_le)
eberlm@62049
   600
  hence "l * ereal (norm z) < 1"
eberlm@62049
   601
  proof cases
eberlm@62049
   602
    assume "l = \<infinity>"
eberlm@62049
   603
    hence "conv_radius f = 0" unfolding conv_radius_def l_def by simp
eberlm@62049
   604
    with assms show ?thesis by simp
eberlm@62049
   605
  next
eberlm@62049
   606
    assume "\<exists>l'. l = ereal l' \<and> l' > 0"
eberlm@62049
   607
    then guess l' by (elim exE conjE) note l' = this
eberlm@62049
   608
    hence "l \<noteq> \<infinity>" by auto
eberlm@62049
   609
    have "l * ereal (norm z) < l * conv_radius f"
eberlm@62049
   610
      by (intro ereal_mult_strict_left_mono) (simp_all add: l' assms)
eberlm@62049
   611
    also have "conv_radius f = inverse l" by (simp add: conv_radius_def l_def)
eberlm@62049
   612
    also from l' have "l * inverse l = 1" by simp
eberlm@62049
   613
    finally show ?thesis .
eberlm@62049
   614
  qed simp_all
eberlm@62049
   615
  finally show "limsup (\<lambda>n. ereal (root n (norm (norm (f n * z ^ n))))) < 1" by simp
eberlm@62049
   616
qed
eberlm@62049
   617
eberlm@62049
   618
lemma summable_in_conv_radius:
eberlm@62049
   619
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   620
  assumes "ereal (norm z) < conv_radius f"
eberlm@62049
   621
  shows   "summable (\<lambda>n. f n * z ^ n)"
eberlm@62049
   622
  by (rule summable_norm_cancel, rule abs_summable_in_conv_radius) fact+
eberlm@62049
   623
eberlm@62049
   624
lemma not_summable_outside_conv_radius:
eberlm@62049
   625
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   626
  assumes "ereal (norm z) > conv_radius f"
eberlm@62049
   627
  shows   "\<not>summable (\<lambda>n. f n * z ^ n)"
eberlm@62049
   628
proof (rule root_test_divergence)
wenzelm@63040
   629
  define l where "l = limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   630
  have "0 = limsup (\<lambda>n. 0)" by (simp add: Limsup_const)
eberlm@62049
   631
  also have "... \<le> l" unfolding l_def by (intro Limsup_mono) (simp_all add: real_root_ge_zero)
eberlm@62049
   632
  finally have l_nonneg: "l \<ge> 0" .
eberlm@62049
   633
  from assms have l_nz: "l \<noteq> 0" unfolding conv_radius_def l_def by auto
eberlm@62049
   634
eberlm@62049
   635
  have "limsup (\<lambda>n. ereal (root n (norm (f n * z^n)))) = l * ereal (norm z)"
eberlm@62049
   636
    unfolding l_def by (rule limsup_root_powser)
eberlm@62049
   637
  also have "... > 1"
eberlm@62049
   638
  proof (cases l)
eberlm@62049
   639
    assume "l = \<infinity>"
eberlm@62049
   640
    with assms conv_radius_nonneg[of f] show ?thesis
eberlm@62049
   641
      by (auto simp: zero_ereal_def[symmetric])
eberlm@62049
   642
  next
eberlm@62049
   643
    fix l' assume l': "l = ereal l'"
eberlm@62049
   644
    from l_nonneg l_nz have "1 = l * inverse l" by (auto simp: l' field_simps)
hoelzl@63594
   645
    also from l_nz have "inverse l = conv_radius f"
eberlm@62049
   646
      unfolding l_def conv_radius_def by auto
eberlm@62049
   647
    also from l' l_nz l_nonneg assms have "l * \<dots> < l * ereal (norm z)"
eberlm@62049
   648
      by (intro ereal_mult_strict_left_mono) (auto simp: l')
eberlm@62049
   649
    finally show ?thesis .
eberlm@62049
   650
  qed (insert l_nonneg, simp_all)
eberlm@62049
   651
  finally show "limsup (\<lambda>n. ereal (root n (norm (f n * z^n)))) > 1" .
eberlm@62049
   652
qed
eberlm@62049
   653
eberlm@62049
   654
eberlm@62049
   655
lemma conv_radius_geI:
eberlm@62049
   656
  assumes "summable (\<lambda>n. f n * z ^ n :: 'a :: {banach, real_normed_div_algebra})"
eberlm@62049
   657
  shows   "conv_radius f \<ge> norm z"
eberlm@62049
   658
  using not_summable_outside_conv_radius[of f z] assms by (force simp: not_le[symmetric])
eberlm@62049
   659
eberlm@62049
   660
lemma conv_radius_leI:
eberlm@62049
   661
  assumes "\<not>summable (\<lambda>n. norm (f n * z ^ n :: 'a :: {banach, real_normed_div_algebra}))"
eberlm@62049
   662
  shows   "conv_radius f \<le> norm z"
eberlm@62049
   663
  using abs_summable_in_conv_radius[of z f] assms by (force simp: not_le[symmetric])
eberlm@62049
   664
eberlm@62049
   665
lemma conv_radius_leI':
eberlm@62049
   666
  assumes "\<not>summable (\<lambda>n. f n * z ^ n :: 'a :: {banach, real_normed_div_algebra})"
eberlm@62049
   667
  shows   "conv_radius f \<le> norm z"
eberlm@62049
   668
  using summable_in_conv_radius[of z f] assms by (force simp: not_le[symmetric])
eberlm@62049
   669
eberlm@62049
   670
lemma conv_radius_geI_ex:
eberlm@62049
   671
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   672
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
eberlm@62049
   673
  shows   "conv_radius f \<ge> R"
eberlm@62049
   674
proof (rule linorder_cases[of "conv_radius f" R])
eberlm@62049
   675
  assume R: "conv_radius f < R"
hoelzl@63594
   676
  with conv_radius_nonneg[of f] obtain conv_radius'
eberlm@62049
   677
    where [simp]: "conv_radius f = ereal conv_radius'"
eberlm@62049
   678
    by (cases "conv_radius f") simp_all
wenzelm@63040
   679
  define r where "r = (if R = \<infinity> then conv_radius' + 1 else (real_of_ereal R + conv_radius') / 2)"
hoelzl@63594
   680
  from R conv_radius_nonneg[of f] have "0 < r \<and> ereal r < R \<and> ereal r > conv_radius f"
eberlm@62049
   681
    unfolding r_def by (cases R) (auto simp: r_def field_simps)
eberlm@62049
   682
  with assms(1)[of r] obtain z where "norm z > conv_radius f" "summable (\<lambda>n. f n * z^n)" by auto
eberlm@62049
   683
  with not_summable_outside_conv_radius[of f z] show ?thesis by simp
eberlm@62049
   684
qed simp_all
eberlm@62049
   685
eberlm@62049
   686
lemma conv_radius_geI_ex':
eberlm@62049
   687
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   688
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> summable (\<lambda>n. f n * of_real r^n)"
eberlm@62049
   689
  shows   "conv_radius f \<ge> R"
eberlm@62049
   690
proof (rule conv_radius_geI_ex)
eberlm@62049
   691
  fix r assume "0 < r" "ereal r < R"
eberlm@62049
   692
  with assms[of r] show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z ^ n)"
eberlm@62049
   693
    by (intro exI[of _ "of_real r :: 'a"]) auto
eberlm@62049
   694
qed
eberlm@62049
   695
eberlm@62049
   696
lemma conv_radius_leI_ex:
eberlm@62049
   697
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   698
  assumes "R \<ge> 0"
eberlm@62049
   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))"
eberlm@62049
   700
  shows   "conv_radius f \<le> R"
eberlm@62049
   701
proof (rule linorder_cases[of "conv_radius f" R])
eberlm@62049
   702
  assume R: "conv_radius f > R"
eberlm@62049
   703
  from R assms(1) obtain R' where R': "R = ereal R'" by (cases R) simp_all
wenzelm@63040
   704
  define r where
wenzelm@63040
   705
    "r = (if conv_radius f = \<infinity> then R' + 1 else (R' + real_of_ereal (conv_radius f)) / 2)"
eberlm@62049
   706
  from R conv_radius_nonneg[of f] have "r > R \<and> r < conv_radius f" unfolding r_def
eberlm@62049
   707
    by (cases "conv_radius f") (auto simp: r_def field_simps R')
hoelzl@63594
   708
  with assms(1) assms(2)[of r] R'
eberlm@62049
   709
    obtain z where "norm z < conv_radius f" "\<not>summable (\<lambda>n. norm (f n * z^n))" by auto
eberlm@62049
   710
  with abs_summable_in_conv_radius[of z f] show ?thesis by auto
eberlm@62049
   711
qed simp_all
eberlm@62049
   712
eberlm@62049
   713
lemma conv_radius_leI_ex':
eberlm@62049
   714
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   715
  assumes "R \<ge> 0"
eberlm@62049
   716
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<not>summable (\<lambda>n. f n * of_real r^n)"
eberlm@62049
   717
  shows   "conv_radius f \<le> R"
eberlm@62049
   718
proof (rule conv_radius_leI_ex)
eberlm@62049
   719
  fix r assume "0 < r" "ereal r > R"
eberlm@62049
   720
  with assms(2)[of r] show "\<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z ^ n))"
eberlm@62049
   721
    by (intro exI[of _ "of_real r :: 'a"]) (auto dest: summable_norm_cancel)
eberlm@62049
   722
qed fact+
eberlm@62049
   723
eberlm@62049
   724
lemma conv_radius_eqI:
eberlm@62049
   725
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   726
  assumes "R \<ge> 0"
eberlm@62049
   727
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
eberlm@62049
   728
  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))"
eberlm@62049
   729
  shows   "conv_radius f = R"
eberlm@62049
   730
  by (intro antisym conv_radius_geI_ex conv_radius_leI_ex assms)
eberlm@62049
   731
eberlm@62049
   732
lemma conv_radius_eqI':
eberlm@62049
   733
  fixes f :: "nat \<Rightarrow> 'a :: {banach, real_normed_div_algebra}"
eberlm@62049
   734
  assumes "R \<ge> 0"
eberlm@62049
   735
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r < R \<Longrightarrow> summable (\<lambda>n. f n * (of_real r)^n)"
eberlm@62049
   736
  assumes "\<And>r. 0 < r \<Longrightarrow> ereal r > R \<Longrightarrow> \<not>summable (\<lambda>n. norm (f n * (of_real r)^n))"
eberlm@62049
   737
  shows   "conv_radius f = R"
eberlm@62049
   738
proof (intro conv_radius_eqI[OF assms(1)])
hoelzl@63594
   739
  fix r assume "0 < r" "ereal r < R" with assms(2)[OF this]
eberlm@62049
   740
    show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z ^ n)" by force
eberlm@62049
   741
next
hoelzl@63594
   742
  fix r assume "0 < r" "ereal r > R" with assms(3)[OF this]
hoelzl@63594
   743
    show "\<exists>z. norm z = r \<and> \<not>summable (\<lambda>n. norm (f n * z ^ n))" by force
eberlm@62049
   744
qed
eberlm@62049
   745
eberlm@62049
   746
lemma conv_radius_zeroI:
eberlm@62049
   747
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   748
  assumes "\<And>z. z \<noteq> 0 \<Longrightarrow> \<not>summable (\<lambda>n. f n * z^n)"
eberlm@62049
   749
  shows   "conv_radius f = 0"
eberlm@62049
   750
proof (rule ccontr)
eberlm@62049
   751
  assume "conv_radius f \<noteq> 0"
eberlm@62049
   752
  with conv_radius_nonneg[of f] have pos: "conv_radius f > 0" by simp
wenzelm@63040
   753
  define r where "r = (if conv_radius f = \<infinity> then 1 else real_of_ereal (conv_radius f) / 2)"
hoelzl@63594
   754
  from pos have r: "ereal r > 0 \<and> ereal r < conv_radius f"
eberlm@62049
   755
    by (cases "conv_radius f") (simp_all add: r_def)
eberlm@62049
   756
  hence "summable (\<lambda>n. f n * of_real r ^ n)" by (intro summable_in_conv_radius) simp
eberlm@62049
   757
  moreover from r and assms[of "of_real r"] have "\<not>summable (\<lambda>n. f n * of_real r ^ n)" by simp
eberlm@62049
   758
  ultimately show False by contradiction
eberlm@62049
   759
qed
eberlm@62049
   760
eberlm@62049
   761
lemma conv_radius_inftyI':
eberlm@62049
   762
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   763
  assumes "\<And>r. r > c \<Longrightarrow> \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
eberlm@62049
   764
  shows   "conv_radius f = \<infinity>"
eberlm@62049
   765
proof -
eberlm@62049
   766
  {
eberlm@62049
   767
    fix r :: real
eberlm@62049
   768
    have "max r (c + 1) > c" by (auto simp: max_def)
eberlm@62049
   769
    from assms[OF this] obtain z where "norm z = max r (c + 1)" "summable (\<lambda>n. f n * z^n)" by blast
eberlm@62049
   770
    from conv_radius_geI[OF this(2)] this(1) have "conv_radius f \<ge> r" by simp
eberlm@62049
   771
  }
eberlm@62049
   772
  from this[of "real_of_ereal (conv_radius f + 1)"] show "conv_radius f = \<infinity>"
eberlm@62049
   773
    by (cases "conv_radius f") simp_all
eberlm@62049
   774
qed
eberlm@62049
   775
eberlm@62049
   776
lemma conv_radius_inftyI:
eberlm@62049
   777
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   778
  assumes "\<And>r. \<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
eberlm@62049
   779
  shows   "conv_radius f = \<infinity>"
eberlm@62049
   780
  using assms by (rule conv_radius_inftyI')
eberlm@62049
   781
eberlm@62049
   782
lemma conv_radius_inftyI'':
eberlm@62049
   783
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   784
  assumes "\<And>z. summable (\<lambda>n. f n * z^n)"
eberlm@62049
   785
  shows   "conv_radius f = \<infinity>"
eberlm@62049
   786
proof (rule conv_radius_inftyI')
eberlm@62049
   787
  fix r :: real assume "r > 0"
eberlm@62049
   788
  with assms show "\<exists>z. norm z = r \<and> summable (\<lambda>n. f n * z^n)"
eberlm@62049
   789
    by (intro exI[of _ "of_real r"]) simp
eberlm@62049
   790
qed
eberlm@62049
   791
eberlm@62049
   792
lemma conv_radius_ratio_limit_ereal:
eberlm@62049
   793
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   794
  assumes nz:  "eventually (\<lambda>n. f n \<noteq> 0) sequentially"
eberlm@62049
   795
  assumes lim: "(\<lambda>n. ereal (norm (f n) / norm (f (Suc n)))) \<longlonglongrightarrow> c"
eberlm@62049
   796
  shows   "conv_radius f = c"
eberlm@62049
   797
proof (rule conv_radius_eqI')
eberlm@62049
   798
  show "c \<ge> 0" by (intro Lim_bounded2_ereal[OF lim]) simp_all
eberlm@62049
   799
next
eberlm@62049
   800
  fix r assume r: "0 < r" "ereal r < c"
eberlm@62049
   801
  let ?l = "liminf (\<lambda>n. ereal (norm (f n * of_real r ^ n) / norm (f (Suc n) * of_real r ^ Suc n)))"
eberlm@62049
   802
  have "?l = liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n)))) * ereal (inverse r))"
eberlm@62049
   803
    using r by (simp add: norm_mult norm_power divide_simps)
eberlm@62049
   804
  also from r have "\<dots> = liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) * ereal (inverse r)"
eberlm@62049
   805
    by (intro Liminf_ereal_mult_right) simp_all
eberlm@62049
   806
  also have "liminf (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) = c"
eberlm@62049
   807
    by (intro lim_imp_Liminf lim) simp
eberlm@62049
   808
  finally have l: "?l = c * ereal (inverse r)" by simp
eberlm@62049
   809
  from r have  l': "c * ereal (inverse r) > 1" by (cases c) (simp_all add: field_simps)
eberlm@62049
   810
  show "summable (\<lambda>n. f n * of_real r^n)"
eberlm@62049
   811
    by (rule summable_norm_cancel, rule ratio_test_convergence)
eberlm@62049
   812
       (insert r nz l l', auto elim!: eventually_mono)
eberlm@62049
   813
next
eberlm@62049
   814
  fix r assume r: "0 < r" "ereal r > c"
eberlm@62049
   815
  let ?l = "limsup (\<lambda>n. ereal (norm (f n * of_real r ^ n) / norm (f (Suc n) * of_real r ^ Suc n)))"
eberlm@62049
   816
  have "?l = limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n)))) * ereal (inverse r))"
eberlm@62049
   817
    using r by (simp add: norm_mult norm_power divide_simps)
eberlm@62049
   818
  also from r have "\<dots> = limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) * ereal (inverse r)"
eberlm@62049
   819
    by (intro Limsup_ereal_mult_right) simp_all
eberlm@62049
   820
  also have "limsup (\<lambda>n. ereal (norm (f n) / (norm (f (Suc n))))) = c"
eberlm@62049
   821
    by (intro lim_imp_Limsup lim) simp
eberlm@62049
   822
  finally have l: "?l = c * ereal (inverse r)" by simp
eberlm@62049
   823
  from r have  l': "c * ereal (inverse r) < 1" by (cases c) (simp_all add: field_simps)
eberlm@62049
   824
  show "\<not>summable (\<lambda>n. norm (f n * of_real r^n))"
eberlm@62049
   825
    by (rule ratio_test_divergence) (insert r nz l l', auto elim!: eventually_mono)
eberlm@62049
   826
qed
eberlm@62049
   827
eberlm@62049
   828
lemma conv_radius_ratio_limit_ereal_nonzero:
eberlm@62049
   829
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   830
  assumes nz:  "c \<noteq> 0"
eberlm@62049
   831
  assumes lim: "(\<lambda>n. ereal (norm (f n) / norm (f (Suc n)))) \<longlonglongrightarrow> c"
eberlm@62049
   832
  shows   "conv_radius f = c"
eberlm@62049
   833
proof (rule conv_radius_ratio_limit_ereal[OF _ lim], rule ccontr)
eberlm@62049
   834
  assume "\<not>eventually (\<lambda>n. f n \<noteq> 0) sequentially"
eberlm@62049
   835
  hence "frequently (\<lambda>n. f n = 0) sequentially" by (simp add: frequently_def)
eberlm@62049
   836
  hence "frequently (\<lambda>n. ereal (norm (f n) / norm (f (Suc n))) = 0) sequentially"
eberlm@62049
   837
    by (force elim!: frequently_elim1)
eberlm@62049
   838
  hence "c = 0" by (intro limit_frequently_eq[OF _ _ lim]) auto
eberlm@62049
   839
  with nz show False by contradiction
hoelzl@63594
   840
qed
eberlm@62049
   841
eberlm@62049
   842
lemma conv_radius_ratio_limit:
eberlm@62049
   843
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   844
  assumes "c' = ereal c"
eberlm@62049
   845
  assumes nz:  "eventually (\<lambda>n. f n \<noteq> 0) sequentially"
eberlm@62049
   846
  assumes lim: "(\<lambda>n. norm (f n) / norm (f (Suc n))) \<longlonglongrightarrow> c"
eberlm@62049
   847
  shows   "conv_radius f = c'"
eberlm@62049
   848
  using assms by (intro conv_radius_ratio_limit_ereal) simp_all
hoelzl@63594
   849
eberlm@62049
   850
lemma conv_radius_ratio_limit_nonzero:
eberlm@62049
   851
  fixes f :: "nat \<Rightarrow> 'a :: {banach,real_normed_div_algebra}"
eberlm@62049
   852
  assumes "c' = ereal c"
eberlm@62049
   853
  assumes nz:  "c \<noteq> 0"
eberlm@62049
   854
  assumes lim: "(\<lambda>n. norm (f n) / norm (f (Suc n))) \<longlonglongrightarrow> c"
eberlm@62049
   855
  shows   "conv_radius f = c'"
eberlm@62049
   856
  using assms by (intro conv_radius_ratio_limit_ereal_nonzero) simp_all
eberlm@62049
   857
hoelzl@63594
   858
lemma conv_radius_mult_power:
eberlm@62049
   859
  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
eberlm@62049
   860
  shows   "conv_radius (\<lambda>n. c ^ n * f n) = conv_radius f / ereal (norm c)"
hoelzl@63594
   861
proof -
eberlm@62049
   862
  have "limsup (\<lambda>n. ereal (root n (norm (c ^ n * f n)))) =
hoelzl@63594
   863
          limsup (\<lambda>n. ereal (norm c) * ereal (root n (norm (f n))))"
eberlm@62049
   864
    using eventually_gt_at_top[of "0::nat"]
hoelzl@63594
   865
    by (intro Limsup_eq)
eberlm@62049
   866
       (auto elim!: eventually_mono simp: norm_mult norm_power real_root_mult real_root_power)
eberlm@62049
   867
  also have "\<dots> = ereal (norm c) * limsup (\<lambda>n. ereal (root n (norm (f n))))"
eberlm@62049
   868
    using assms by (subst Limsup_ereal_mult_left[symmetric]) simp_all
hoelzl@63594
   869
  finally have A: "limsup (\<lambda>n. ereal (root n (norm (c ^ n * f n)))) =
eberlm@62049
   870
                       ereal (norm c) * limsup (\<lambda>n. ereal (root n (norm (f n))))" .
eberlm@62049
   871
  show ?thesis using assms
eberlm@62049
   872
    apply (cases "limsup (\<lambda>n. ereal (root n (norm (f n)))) = 0")
eberlm@62049
   873
    apply (simp add: A conv_radius_def)
eberlm@62049
   874
    apply (unfold conv_radius_def A divide_ereal_def, simp add: mult.commute ereal_inverse_mult)
eberlm@62049
   875
    done
eberlm@62049
   876
qed
eberlm@62049
   877
hoelzl@63594
   878
lemma conv_radius_mult_power_right:
eberlm@62049
   879
  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
eberlm@62049
   880
  shows   "conv_radius (\<lambda>n. f n * c ^ n) = conv_radius f / ereal (norm c)"
eberlm@62049
   881
  using conv_radius_mult_power[OF assms, of f]
eberlm@62049
   882
  unfolding conv_radius_def by (simp add: mult.commute norm_mult)
eberlm@62049
   883
hoelzl@63594
   884
lemma conv_radius_divide_power:
eberlm@62049
   885
  assumes "c \<noteq> (0 :: 'a :: {real_normed_div_algebra,banach})"
eberlm@62049
   886
  shows   "conv_radius (\<lambda>n. f n / c^n) = conv_radius f * ereal (norm c)"
hoelzl@63594
   887
proof -
eberlm@62049
   888
  from assms have "inverse c \<noteq> 0" by simp
eberlm@62049
   889
  from conv_radius_mult_power_right[OF this, of f] show ?thesis
eberlm@62049
   890
    by (simp add: divide_inverse divide_ereal_def assms norm_inverse power_inverse)
eberlm@62049
   891
qed
eberlm@62049
   892
eberlm@62049
   893
hoelzl@63594
   894
lemma conv_radius_add_ge:
hoelzl@63594
   895
  "min (conv_radius f) (conv_radius g) \<le>
eberlm@62049
   896
       conv_radius (\<lambda>x. f x + g x :: 'a :: {banach,real_normed_div_algebra})"
eberlm@62049
   897
  by (rule conv_radius_geI_ex')
eberlm@62049
   898
     (auto simp: algebra_simps intro!: summable_add summable_in_conv_radius)
eberlm@62049
   899
eberlm@62049
   900
lemma conv_radius_mult_ge:
eberlm@62049
   901
  fixes f g :: "nat \<Rightarrow> ('a :: {banach,real_normed_div_algebra})"
eberlm@62049
   902
  shows "conv_radius (\<lambda>x. \<Sum>i\<le>x. f i * g (x - i)) \<ge> min (conv_radius f) (conv_radius g)"
eberlm@62049
   903
proof (rule conv_radius_geI_ex')
eberlm@62049
   904
  fix r assume r: "r > 0" "ereal r < min (conv_radius f) (conv_radius g)"
eberlm@62049
   905
  from r have "summable (\<lambda>n. (\<Sum>i\<le>n. (f i * of_real r^i) * (g (n - i) * of_real r^(n - i))))"
eberlm@62049
   906
    by (intro summable_Cauchy_product abs_summable_in_conv_radius) simp_all
eberlm@62049
   907
  thus "summable (\<lambda>n. (\<Sum>i\<le>n. f i * g (n - i)) * of_real r ^ n)"
lp15@62381
   908
    by (simp add: algebra_simps of_real_def power_add [symmetric] scaleR_setsum_right)
eberlm@62049
   909
qed
eberlm@62049
   910
lp15@62381
   911
lemma le_conv_radius_iff:
lp15@62381
   912
  fixes a :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}"
lp15@62381
   913
  shows "r \<le> conv_radius a \<longleftrightarrow> (\<forall>x. norm (x-\<xi>) < r \<longrightarrow> summable (\<lambda>i. a i * (x - \<xi>) ^ i))"
lp15@62381
   914
apply (intro iffI allI impI summable_in_conv_radius conv_radius_geI_ex)
lp15@62381
   915
apply (meson less_ereal.simps(1) not_le order_trans)
lp15@62381
   916
apply (rule_tac x="of_real ra" in exI, simp)
lp15@62381
   917
apply (metis abs_of_nonneg add_diff_cancel_left' less_eq_real_def norm_of_real)
lp15@62381
   918
done
lp15@62381
   919
eberlm@62049
   920
end