src/HOL/Series.thy
author hoelzl
Mon Feb 08 19:53:49 2016 +0100 (2016-02-08)
changeset 62368 106569399cd6
parent 62217 527488dc8b90
child 62376 85f38d5f8807
permissions -rw-r--r--
add type class for topological monoids
     1 (*  Title       : Series.thy
     2     Author      : Jacques D. Fleuriot
     3     Copyright   : 1998  University of Cambridge
     4 
     5 Converted to Isar and polished by lcp
     6 Converted to setsum and polished yet more by TNN
     7 Additional contributions by Jeremy Avigad
     8 *)
     9 
    10 section \<open>Infinite Series\<close>
    11 
    12 theory Series
    13 imports Limits Inequalities
    14 begin
    15 
    16 subsection \<open>Definition of infinite summability\<close>
    17 
    18 definition
    19   sums :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a \<Rightarrow> bool"
    20   (infixr "sums" 80)
    21 where
    22   "f sums s \<longleftrightarrow> (\<lambda>n. \<Sum>i<n. f i) \<longlonglongrightarrow> s"
    23 
    24 definition summable :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> bool" where
    25    "summable f \<longleftrightarrow> (\<exists>s. f sums s)"
    26 
    27 definition
    28   suminf :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a"
    29   (binder "\<Sum>" 10)
    30 where
    31   "suminf f = (THE s. f sums s)"
    32 
    33 lemma sums_def': "f sums s \<longleftrightarrow> (\<lambda>n. \<Sum>i = 0..n. f i) \<longlonglongrightarrow> s"
    34   apply (simp add: sums_def)
    35   apply (subst LIMSEQ_Suc_iff [symmetric])
    36   apply (simp only: lessThan_Suc_atMost atLeast0AtMost)
    37   done
    38 
    39 subsection \<open>Infinite summability on topological monoids\<close>
    40 
    41 lemma sums_subst[trans]: "f = g \<Longrightarrow> g sums z \<Longrightarrow> f sums z"
    42   by simp
    43 
    44 lemma sums_cong: "(\<And>n. f n = g n) \<Longrightarrow> f sums c \<longleftrightarrow> g sums c"
    45   by (drule ext) simp
    46 
    47 lemma sums_summable: "f sums l \<Longrightarrow> summable f"
    48   by (simp add: sums_def summable_def, blast)
    49 
    50 lemma summable_iff_convergent: "summable f \<longleftrightarrow> convergent (\<lambda>n. \<Sum>i<n. f i)"
    51   by (simp add: summable_def sums_def convergent_def)
    52 
    53 lemma summable_iff_convergent':
    54   "summable f \<longleftrightarrow> convergent (\<lambda>n. setsum f {..n})"
    55   by (simp_all only: summable_iff_convergent convergent_def
    56         lessThan_Suc_atMost [symmetric] LIMSEQ_Suc_iff[of "\<lambda>n. setsum f {..<n}"])
    57 
    58 lemma suminf_eq_lim: "suminf f = lim (\<lambda>n. \<Sum>i<n. f i)"
    59   by (simp add: suminf_def sums_def lim_def)
    60 
    61 lemma sums_zero[simp, intro]: "(\<lambda>n. 0) sums 0"
    62   unfolding sums_def by simp
    63 
    64 lemma summable_zero[simp, intro]: "summable (\<lambda>n. 0)"
    65   by (rule sums_zero [THEN sums_summable])
    66 
    67 lemma sums_group: "f sums s \<Longrightarrow> 0 < k \<Longrightarrow> (\<lambda>n. setsum f {n * k ..< n * k + k}) sums s"
    68   apply (simp only: sums_def setsum_nat_group tendsto_def eventually_sequentially)
    69   apply safe
    70   apply (erule_tac x=S in allE)
    71   apply safe
    72   apply (rule_tac x="N" in exI, safe)
    73   apply (drule_tac x="n*k" in spec)
    74   apply (erule mp)
    75   apply (erule order_trans)
    76   apply simp
    77   done
    78 
    79 lemma suminf_cong: "(\<And>n. f n = g n) \<Longrightarrow> suminf f = suminf g"
    80   by (rule arg_cong[of f g], rule ext) simp
    81 
    82 lemma summable_cong:
    83   assumes "eventually (\<lambda>x. f x = (g x :: 'a :: real_normed_vector)) sequentially"
    84   shows   "summable f = summable g"
    85 proof -
    86   from assms obtain N where N: "\<forall>n\<ge>N. f n = g n" by (auto simp: eventually_at_top_linorder)
    87   def C \<equiv> "(\<Sum>k<N. f k - g k)"
    88   from eventually_ge_at_top[of N]
    89     have "eventually (\<lambda>n. setsum f {..<n} = C + setsum g {..<n}) sequentially"
    90   proof eventually_elim
    91     fix n assume n: "n \<ge> N"
    92     from n have "{..<n} = {..<N} \<union> {N..<n}" by auto
    93     also have "setsum f ... = setsum f {..<N} + setsum f {N..<n}"
    94       by (intro setsum.union_disjoint) auto
    95     also from N have "setsum f {N..<n} = setsum g {N..<n}" by (intro setsum.cong) simp_all
    96     also have "setsum f {..<N} + setsum g {N..<n} = C + (setsum g {..<N} + setsum g {N..<n})"
    97       unfolding C_def by (simp add: algebra_simps setsum_subtractf)
    98     also have "setsum g {..<N} + setsum g {N..<n} = setsum g ({..<N} \<union> {N..<n})"
    99       by (intro setsum.union_disjoint [symmetric]) auto
   100     also from n have "{..<N} \<union> {N..<n} = {..<n}" by auto
   101     finally show "setsum f {..<n} = C + setsum g {..<n}" .
   102   qed
   103   from convergent_cong[OF this] show ?thesis
   104     by (simp add: summable_iff_convergent convergent_add_const_iff)
   105 qed
   106 
   107 lemma sums_finite:
   108   assumes [simp]: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
   109   shows "f sums (\<Sum>n\<in>N. f n)"
   110 proof -
   111   { fix n
   112     have "setsum f {..<n + Suc (Max N)} = setsum f N"
   113     proof cases
   114       assume "N = {}"
   115       with f have "f = (\<lambda>x. 0)" by auto
   116       then show ?thesis by simp
   117     next
   118       assume [simp]: "N \<noteq> {}"
   119       show ?thesis
   120       proof (safe intro!: setsum.mono_neutral_right f)
   121         fix i assume "i \<in> N"
   122         then have "i \<le> Max N" by simp
   123         then show "i < n + Suc (Max N)" by simp
   124       qed
   125     qed }
   126   note eq = this
   127   show ?thesis unfolding sums_def
   128     by (rule LIMSEQ_offset[of _ "Suc (Max N)"])
   129        (simp add: eq atLeast0LessThan del: add_Suc_right)
   130 qed
   131 
   132 corollary sums_0:
   133    "(\<And>n. f n = 0) \<Longrightarrow> (f sums 0)"
   134     by (metis (no_types) finite.emptyI setsum.empty sums_finite)
   135 
   136 lemma summable_finite: "finite N \<Longrightarrow> (\<And>n. n \<notin> N \<Longrightarrow> f n = 0) \<Longrightarrow> summable f"
   137   by (rule sums_summable) (rule sums_finite)
   138 
   139 lemma sums_If_finite_set: "finite A \<Longrightarrow> (\<lambda>r. if r \<in> A then f r else 0) sums (\<Sum>r\<in>A. f r)"
   140   using sums_finite[of A "(\<lambda>r. if r \<in> A then f r else 0)"] by simp
   141 
   142 lemma summable_If_finite_set[simp, intro]: "finite A \<Longrightarrow> summable (\<lambda>r. if r \<in> A then f r else 0)"
   143   by (rule sums_summable) (rule sums_If_finite_set)
   144 
   145 lemma sums_If_finite: "finite {r. P r} \<Longrightarrow> (\<lambda>r. if P r then f r else 0) sums (\<Sum>r | P r. f r)"
   146   using sums_If_finite_set[of "{r. P r}"] by simp
   147 
   148 lemma summable_If_finite[simp, intro]: "finite {r. P r} \<Longrightarrow> summable (\<lambda>r. if P r then f r else 0)"
   149   by (rule sums_summable) (rule sums_If_finite)
   150 
   151 lemma sums_single: "(\<lambda>r. if r = i then f r else 0) sums f i"
   152   using sums_If_finite[of "\<lambda>r. r = i"] by simp
   153 
   154 lemma summable_single[simp, intro]: "summable (\<lambda>r. if r = i then f r else 0)"
   155   by (rule sums_summable) (rule sums_single)
   156 
   157 context
   158   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
   159 begin
   160 
   161 lemma summable_sums[intro]: "summable f \<Longrightarrow> f sums (suminf f)"
   162   by (simp add: summable_def sums_def suminf_def)
   163      (metis convergent_LIMSEQ_iff convergent_def lim_def)
   164 
   165 lemma summable_LIMSEQ: "summable f \<Longrightarrow> (\<lambda>n. \<Sum>i<n. f i) \<longlonglongrightarrow> suminf f"
   166   by (rule summable_sums [unfolded sums_def])
   167 
   168 lemma sums_unique: "f sums s \<Longrightarrow> s = suminf f"
   169   by (metis limI suminf_eq_lim sums_def)
   170 
   171 lemma sums_iff: "f sums x \<longleftrightarrow> summable f \<and> (suminf f = x)"
   172   by (metis summable_sums sums_summable sums_unique)
   173 
   174 lemma summable_sums_iff:
   175   "summable (f :: nat \<Rightarrow> 'a :: {comm_monoid_add,t2_space}) \<longleftrightarrow> f sums suminf f"
   176   by (auto simp: sums_iff summable_sums)
   177 
   178 lemma sums_unique2:
   179   fixes a b :: "'a::{comm_monoid_add,t2_space}"
   180   shows "f sums a \<Longrightarrow> f sums b \<Longrightarrow> a = b"
   181 by (simp add: sums_iff)
   182 
   183 lemma suminf_finite:
   184   assumes N: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
   185   shows "suminf f = (\<Sum>n\<in>N. f n)"
   186   using sums_finite[OF assms, THEN sums_unique] by simp
   187 
   188 end
   189 
   190 lemma suminf_zero[simp]: "suminf (\<lambda>n. 0::'a::{t2_space, comm_monoid_add}) = 0"
   191   by (rule sums_zero [THEN sums_unique, symmetric])
   192 
   193 
   194 subsection \<open>Infinite summability on ordered, topological monoids\<close>
   195 
   196 lemma sums_le:
   197   fixes f g :: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology}"
   198   shows "\<forall>n. f n \<le> g n \<Longrightarrow> f sums s \<Longrightarrow> g sums t \<Longrightarrow> s \<le> t"
   199   by (rule LIMSEQ_le) (auto intro: setsum_mono simp: sums_def)
   200 
   201 context
   202   fixes f :: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology}"
   203 begin
   204 
   205 lemma suminf_le: "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
   206   by (auto dest: sums_summable intro: sums_le)
   207 
   208 lemma setsum_le_suminf: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 \<le> f m \<Longrightarrow> setsum f {..<n} \<le> suminf f"
   209   by (rule sums_le[OF _ sums_If_finite_set summable_sums]) auto
   210 
   211 lemma suminf_nonneg: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 \<le> suminf f"
   212   using setsum_le_suminf[of 0] by simp
   213 
   214 lemma setsum_less_suminf2: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 \<le> f m \<Longrightarrow> n \<le> i \<Longrightarrow> 0 < f i \<Longrightarrow> setsum f {..<n} < suminf f"
   215   using
   216     setsum_le_suminf[of "Suc i"]
   217     add_strict_increasing[of "f i" "setsum f {..<n}" "setsum f {..<i}"]
   218     setsum_mono2[of "{..<i}" "{..<n}" f]
   219   by (auto simp: less_imp_le ac_simps)
   220 
   221 lemma setsum_less_suminf: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 < f m \<Longrightarrow> setsum f {..<n} < suminf f"
   222   using setsum_less_suminf2[of n n] by (simp add: less_imp_le)
   223 
   224 lemma suminf_pos2: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 < f i \<Longrightarrow> 0 < suminf f"
   225   using setsum_less_suminf2[of 0 i] by simp
   226 
   227 lemma suminf_pos: "summable f \<Longrightarrow> \<forall>n. 0 < f n \<Longrightarrow> 0 < suminf f"
   228   using suminf_pos2[of 0] by (simp add: less_imp_le)
   229 
   230 lemma suminf_le_const: "summable f \<Longrightarrow> (\<And>n. setsum f {..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
   231   by (metis LIMSEQ_le_const2 summable_LIMSEQ)
   232 
   233 lemma suminf_eq_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> suminf f = 0 \<longleftrightarrow> (\<forall>n. f n = 0)"
   234 proof
   235   assume "summable f" "suminf f = 0" and pos: "\<forall>n. 0 \<le> f n"
   236   then have f: "(\<lambda>n. \<Sum>i<n. f i) \<longlonglongrightarrow> 0"
   237     using summable_LIMSEQ[of f] by simp
   238   then have "\<And>i. (\<Sum>n\<in>{i}. f n) \<le> 0"
   239   proof (rule LIMSEQ_le_const)
   240     fix i show "\<exists>N. \<forall>n\<ge>N. (\<Sum>n\<in>{i}. f n) \<le> setsum f {..<n}"
   241       using pos by (intro exI[of _ "Suc i"] allI impI setsum_mono2) auto
   242   qed
   243   with pos show "\<forall>n. f n = 0"
   244     by (auto intro!: antisym)
   245 qed (metis suminf_zero fun_eq_iff)
   246 
   247 lemma suminf_pos_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 < suminf f \<longleftrightarrow> (\<exists>i. 0 < f i)"
   248   using setsum_le_suminf[of 0] suminf_eq_zero_iff by (simp add: less_le)
   249 
   250 end
   251 
   252 lemma summableI_nonneg_bounded:
   253   fixes f:: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology, conditionally_complete_linorder}"
   254   assumes pos[simp]: "\<And>n. 0 \<le> f n" and le: "\<And>n. (\<Sum>i<n. f i) \<le> x"
   255   shows "summable f"
   256   unfolding summable_def sums_def[abs_def]
   257 proof (intro exI order_tendstoI)
   258   have [simp, intro]: "bdd_above (range (\<lambda>n. \<Sum>i<n. f i))"
   259     using le by (auto simp: bdd_above_def)
   260   { fix a assume "a < (SUP n. \<Sum>i<n. f i)"
   261     then obtain n where "a < (\<Sum>i<n. f i)"
   262       by (auto simp add: less_cSUP_iff)
   263     then have "\<And>m. n \<le> m \<Longrightarrow> a < (\<Sum>i<m. f i)"
   264       by (rule less_le_trans) (auto intro!: setsum_mono2)
   265     then show "eventually (\<lambda>n. a < (\<Sum>i<n. f i)) sequentially"
   266       by (auto simp: eventually_sequentially) }
   267   { fix a assume "(SUP n. \<Sum>i<n. f i) < a"
   268     moreover have "\<And>n. (\<Sum>i<n. f i) \<le> (SUP n. \<Sum>i<n. f i)"
   269       by (auto intro: cSUP_upper)
   270     ultimately show "eventually (\<lambda>n. (\<Sum>i<n. f i) < a) sequentially"
   271       by (auto intro: le_less_trans simp: eventually_sequentially) }
   272 qed
   273 
   274 subsection \<open>Infinite summability on topological monoids\<close>
   275 
   276 lemma Zero_notin_Suc: "0 \<notin> Suc ` A"
   277   by auto
   278 
   279 context
   280   fixes f g :: "nat \<Rightarrow> 'a :: {t2_space, topological_comm_monoid_add}"
   281 begin
   282 
   283 lemma sums_Suc:
   284   assumes "(\<lambda>n. f (Suc n)) sums l" shows "f sums (l + f 0)"
   285 proof  -
   286   have "(\<lambda>n. (\<Sum>i<n. f (Suc i)) + f 0) \<longlonglongrightarrow> l + f 0"
   287     using assms by (auto intro!: tendsto_add simp: sums_def)
   288   moreover have "(\<Sum>i<n. f (Suc i)) + f 0 = (\<Sum>i<Suc n. f i)" for n
   289     unfolding lessThan_Suc_eq_insert_0 by (simp add: Zero_notin_Suc ac_simps setsum.reindex)
   290   ultimately show ?thesis
   291     by (auto simp add: sums_def simp del: setsum_lessThan_Suc intro: LIMSEQ_Suc_iff[THEN iffD1])
   292 qed
   293 
   294 lemma sums_add: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n + g n) sums (a + b)"
   295   unfolding sums_def by (simp add: setsum.distrib tendsto_add)
   296 
   297 lemma summable_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n + g n)"
   298   unfolding summable_def by (auto intro: sums_add)
   299 
   300 lemma suminf_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f + suminf g = (\<Sum>n. f n + g n)"
   301   by (intro sums_unique sums_add summable_sums)
   302 
   303 end
   304 
   305 context
   306   fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{t2_space, topological_comm_monoid_add}" and I :: "'i set"
   307 begin
   308 
   309 lemma sums_setsum: "(\<And>i. i \<in> I \<Longrightarrow> (f i) sums (x i)) \<Longrightarrow> (\<lambda>n. \<Sum>i\<in>I. f i n) sums (\<Sum>i\<in>I. x i)"
   310   by (induct I rule: infinite_finite_induct) (auto intro!: sums_add)
   311 
   312 lemma suminf_setsum: "(\<And>i. i \<in> I \<Longrightarrow> summable (f i)) \<Longrightarrow> (\<Sum>n. \<Sum>i\<in>I. f i n) = (\<Sum>i\<in>I. \<Sum>n. f i n)"
   313   using sums_unique[OF sums_setsum, OF summable_sums] by simp
   314 
   315 lemma summable_setsum: "(\<And>i. i \<in> I \<Longrightarrow> summable (f i)) \<Longrightarrow> summable (\<lambda>n. \<Sum>i\<in>I. f i n)"
   316   using sums_summable[OF sums_setsum[OF summable_sums]] .
   317 
   318 end
   319 
   320 subsection \<open>Infinite summability on real normed vector spaces\<close>
   321 
   322 context
   323   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   324 begin
   325 
   326 lemma sums_Suc_iff: "(\<lambda>n. f (Suc n)) sums s \<longleftrightarrow> f sums (s + f 0)"
   327 proof -
   328   have "f sums (s + f 0) \<longleftrightarrow> (\<lambda>i. \<Sum>j<Suc i. f j) \<longlonglongrightarrow> s + f 0"
   329     by (subst LIMSEQ_Suc_iff) (simp add: sums_def)
   330   also have "\<dots> \<longleftrightarrow> (\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) \<longlonglongrightarrow> s + f 0"
   331     by (simp add: ac_simps setsum.reindex image_iff lessThan_Suc_eq_insert_0)
   332   also have "\<dots> \<longleftrightarrow> (\<lambda>n. f (Suc n)) sums s"
   333   proof
   334     assume "(\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) \<longlonglongrightarrow> s + f 0"
   335     with tendsto_add[OF this tendsto_const, of "- f 0"]
   336     show "(\<lambda>i. f (Suc i)) sums s"
   337       by (simp add: sums_def)
   338   qed (auto intro: tendsto_add simp: sums_def)
   339   finally show ?thesis ..
   340 qed
   341 
   342 lemma summable_Suc_iff: "summable (\<lambda>n. f (Suc n)) = summable f"
   343 proof
   344   assume "summable f"
   345   hence "f sums suminf f" by (rule summable_sums)
   346   hence "(\<lambda>n. f (Suc n)) sums (suminf f - f 0)" by (simp add: sums_Suc_iff)
   347   thus "summable (\<lambda>n. f (Suc n))" unfolding summable_def by blast
   348 qed (auto simp: sums_Suc_iff summable_def)
   349 
   350 end
   351 
   352 context
   353   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   354 begin
   355 
   356 lemma sums_diff: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n - g n) sums (a - b)"
   357   unfolding sums_def by (simp add: setsum_subtractf tendsto_diff)
   358 
   359 lemma summable_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n - g n)"
   360   unfolding summable_def by (auto intro: sums_diff)
   361 
   362 lemma suminf_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f - suminf g = (\<Sum>n. f n - g n)"
   363   by (intro sums_unique sums_diff summable_sums)
   364 
   365 lemma sums_minus: "f sums a \<Longrightarrow> (\<lambda>n. - f n) sums (- a)"
   366   unfolding sums_def by (simp add: setsum_negf tendsto_minus)
   367 
   368 lemma summable_minus: "summable f \<Longrightarrow> summable (\<lambda>n. - f n)"
   369   unfolding summable_def by (auto intro: sums_minus)
   370 
   371 lemma suminf_minus: "summable f \<Longrightarrow> (\<Sum>n. - f n) = - (\<Sum>n. f n)"
   372   by (intro sums_unique [symmetric] sums_minus summable_sums)
   373 
   374 lemma sums_iff_shift: "(\<lambda>i. f (i + n)) sums s \<longleftrightarrow> f sums (s + (\<Sum>i<n. f i))"
   375 proof (induct n arbitrary: s)
   376   case (Suc n)
   377   moreover have "(\<lambda>i. f (Suc i + n)) sums s \<longleftrightarrow> (\<lambda>i. f (i + n)) sums (s + f n)"
   378     by (subst sums_Suc_iff) simp
   379   ultimately show ?case
   380     by (simp add: ac_simps)
   381 qed simp
   382 
   383 lemma summable_iff_shift: "summable (\<lambda>n. f (n + k)) \<longleftrightarrow> summable f"
   384   by (metis diff_add_cancel summable_def sums_iff_shift[abs_def])
   385 
   386 lemma sums_split_initial_segment: "f sums s \<Longrightarrow> (\<lambda>i. f (i + n)) sums (s - (\<Sum>i<n. f i))"
   387   by (simp add: sums_iff_shift)
   388 
   389 lemma summable_ignore_initial_segment: "summable f \<Longrightarrow> summable (\<lambda>n. f(n + k))"
   390   by (simp add: summable_iff_shift)
   391 
   392 lemma suminf_minus_initial_segment: "summable f \<Longrightarrow> (\<Sum>n. f (n + k)) = (\<Sum>n. f n) - (\<Sum>i<k. f i)"
   393   by (rule sums_unique[symmetric]) (auto simp: sums_iff_shift)
   394 
   395 lemma suminf_split_initial_segment: "summable f \<Longrightarrow> suminf f = (\<Sum>n. f(n + k)) + (\<Sum>i<k. f i)"
   396   by (auto simp add: suminf_minus_initial_segment)
   397 
   398 lemma suminf_split_head: "summable f \<Longrightarrow> (\<Sum>n. f (Suc n)) = suminf f - f 0"
   399   using suminf_split_initial_segment[of 1] by simp
   400 
   401 lemma suminf_exist_split:
   402   fixes r :: real assumes "0 < r" and "summable f"
   403   shows "\<exists>N. \<forall>n\<ge>N. norm (\<Sum>i. f (i + n)) < r"
   404 proof -
   405   from LIMSEQ_D[OF summable_LIMSEQ[OF \<open>summable f\<close>] \<open>0 < r\<close>]
   406   obtain N :: nat where "\<forall> n \<ge> N. norm (setsum f {..<n} - suminf f) < r" by auto
   407   thus ?thesis
   408     by (auto simp: norm_minus_commute suminf_minus_initial_segment[OF \<open>summable f\<close>])
   409 qed
   410 
   411 lemma summable_LIMSEQ_zero: "summable f \<Longrightarrow> f \<longlonglongrightarrow> 0"
   412   apply (drule summable_iff_convergent [THEN iffD1])
   413   apply (drule convergent_Cauchy)
   414   apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
   415   apply (drule_tac x="r" in spec, safe)
   416   apply (rule_tac x="M" in exI, safe)
   417   apply (drule_tac x="Suc n" in spec, simp)
   418   apply (drule_tac x="n" in spec, simp)
   419   done
   420 
   421 lemma summable_imp_convergent: "summable f \<Longrightarrow> convergent f"
   422   by (force dest!: summable_LIMSEQ_zero simp: convergent_def)
   423 
   424 lemma summable_imp_Bseq: "summable f \<Longrightarrow> Bseq f"
   425   by (simp add: convergent_imp_Bseq summable_imp_convergent)
   426 
   427 end
   428 
   429 lemma summable_minus_iff:
   430   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   431   shows "summable (\<lambda>n. - f n) \<longleftrightarrow> summable f"
   432   by (auto dest: summable_minus) \<comment>\<open>used two ways, hence must be outside the context above\<close>
   433 
   434 lemma (in bounded_linear) sums: "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
   435   unfolding sums_def by (drule tendsto, simp only: setsum)
   436 
   437 lemma (in bounded_linear) summable: "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
   438   unfolding summable_def by (auto intro: sums)
   439 
   440 lemma (in bounded_linear) suminf: "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
   441   by (intro sums_unique sums summable_sums)
   442 
   443 lemmas sums_of_real = bounded_linear.sums [OF bounded_linear_of_real]
   444 lemmas summable_of_real = bounded_linear.summable [OF bounded_linear_of_real]
   445 lemmas suminf_of_real = bounded_linear.suminf [OF bounded_linear_of_real]
   446 
   447 lemmas sums_scaleR_left = bounded_linear.sums[OF bounded_linear_scaleR_left]
   448 lemmas summable_scaleR_left = bounded_linear.summable[OF bounded_linear_scaleR_left]
   449 lemmas suminf_scaleR_left = bounded_linear.suminf[OF bounded_linear_scaleR_left]
   450 
   451 lemmas sums_scaleR_right = bounded_linear.sums[OF bounded_linear_scaleR_right]
   452 lemmas summable_scaleR_right = bounded_linear.summable[OF bounded_linear_scaleR_right]
   453 lemmas suminf_scaleR_right = bounded_linear.suminf[OF bounded_linear_scaleR_right]
   454 
   455 lemma summable_const_iff: "summable (\<lambda>_. c) \<longleftrightarrow> (c :: 'a :: real_normed_vector) = 0"
   456 proof -
   457   {
   458     assume "c \<noteq> 0"
   459     hence "filterlim (\<lambda>n. of_nat n * norm c) at_top sequentially"
   460       by (subst mult.commute)
   461          (auto intro!: filterlim_tendsto_pos_mult_at_top filterlim_real_sequentially)
   462     hence "\<not>convergent (\<lambda>n. norm (\<Sum>k<n. c))"
   463       by (intro filterlim_at_infinity_imp_not_convergent filterlim_at_top_imp_at_infinity)
   464          (simp_all add: setsum_constant_scaleR)
   465     hence "\<not>summable (\<lambda>_. c)" unfolding summable_iff_convergent using convergent_norm by blast
   466   }
   467   thus ?thesis by auto
   468 qed
   469 
   470 
   471 subsection \<open>Infinite summability on real normed algebras\<close>
   472 
   473 context
   474   fixes f :: "nat \<Rightarrow> 'a::real_normed_algebra"
   475 begin
   476 
   477 lemma sums_mult: "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
   478   by (rule bounded_linear.sums [OF bounded_linear_mult_right])
   479 
   480 lemma summable_mult: "summable f \<Longrightarrow> summable (\<lambda>n. c * f n)"
   481   by (rule bounded_linear.summable [OF bounded_linear_mult_right])
   482 
   483 lemma suminf_mult: "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f"
   484   by (rule bounded_linear.suminf [OF bounded_linear_mult_right, symmetric])
   485 
   486 lemma sums_mult2: "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
   487   by (rule bounded_linear.sums [OF bounded_linear_mult_left])
   488 
   489 lemma summable_mult2: "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
   490   by (rule bounded_linear.summable [OF bounded_linear_mult_left])
   491 
   492 lemma suminf_mult2: "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
   493   by (rule bounded_linear.suminf [OF bounded_linear_mult_left])
   494 
   495 end
   496 
   497 lemma sums_mult_iff:
   498   assumes "c \<noteq> 0"
   499   shows   "(\<lambda>n. c * f n :: 'a :: {real_normed_algebra,field}) sums (c * d) \<longleftrightarrow> f sums d"
   500   using sums_mult[of f d c] sums_mult[of "\<lambda>n. c * f n" "c * d" "inverse c"]
   501   by (force simp: field_simps assms)
   502 
   503 lemma sums_mult2_iff:
   504   assumes "c \<noteq> (0 :: 'a :: {real_normed_algebra, field})"
   505   shows   "(\<lambda>n. f n * c) sums (d * c) \<longleftrightarrow> f sums d"
   506   using sums_mult_iff[OF assms, of f d] by (simp add: mult.commute)
   507 
   508 lemma sums_of_real_iff:
   509   "(\<lambda>n. of_real (f n) :: 'a :: real_normed_div_algebra) sums of_real c \<longleftrightarrow> f sums c"
   510   by (simp add: sums_def of_real_setsum[symmetric] tendsto_of_real_iff del: of_real_setsum)
   511 
   512 
   513 subsection \<open>Infinite summability on real normed fields\<close>
   514 
   515 context
   516   fixes c :: "'a::real_normed_field"
   517 begin
   518 
   519 lemma sums_divide: "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
   520   by (rule bounded_linear.sums [OF bounded_linear_divide])
   521 
   522 lemma summable_divide: "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
   523   by (rule bounded_linear.summable [OF bounded_linear_divide])
   524 
   525 lemma suminf_divide: "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
   526   by (rule bounded_linear.suminf [OF bounded_linear_divide, symmetric])
   527 
   528 text\<open>Sum of a geometric progression.\<close>
   529 
   530 lemma geometric_sums: "norm c < 1 \<Longrightarrow> (\<lambda>n. c^n) sums (1 / (1 - c))"
   531 proof -
   532   assume less_1: "norm c < 1"
   533   hence neq_1: "c \<noteq> 1" by auto
   534   hence neq_0: "c - 1 \<noteq> 0" by simp
   535   from less_1 have lim_0: "(\<lambda>n. c^n) \<longlonglongrightarrow> 0"
   536     by (rule LIMSEQ_power_zero)
   537   hence "(\<lambda>n. c ^ n / (c - 1) - 1 / (c - 1)) \<longlonglongrightarrow> 0 / (c - 1) - 1 / (c - 1)"
   538     using neq_0 by (intro tendsto_intros)
   539   hence "(\<lambda>n. (c ^ n - 1) / (c - 1)) \<longlonglongrightarrow> 1 / (1 - c)"
   540     by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
   541   thus "(\<lambda>n. c ^ n) sums (1 / (1 - c))"
   542     by (simp add: sums_def geometric_sum neq_1)
   543 qed
   544 
   545 lemma summable_geometric: "norm c < 1 \<Longrightarrow> summable (\<lambda>n. c^n)"
   546   by (rule geometric_sums [THEN sums_summable])
   547 
   548 lemma suminf_geometric: "norm c < 1 \<Longrightarrow> suminf (\<lambda>n. c^n) = 1 / (1 - c)"
   549   by (rule sums_unique[symmetric]) (rule geometric_sums)
   550 
   551 lemma summable_geometric_iff: "summable (\<lambda>n. c ^ n) \<longleftrightarrow> norm c < 1"
   552 proof
   553   assume "summable (\<lambda>n. c ^ n :: 'a :: real_normed_field)"
   554   hence "(\<lambda>n. norm c ^ n) \<longlonglongrightarrow> 0"
   555     by (simp add: norm_power [symmetric] tendsto_norm_zero_iff summable_LIMSEQ_zero)
   556   from order_tendstoD(2)[OF this zero_less_one] obtain n where "norm c ^ n < 1"
   557     by (auto simp: eventually_at_top_linorder)
   558   thus "norm c < 1" using one_le_power[of "norm c" n] by (cases "norm c \<ge> 1") (linarith, simp)
   559 qed (rule summable_geometric)
   560 
   561 end
   562 
   563 lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
   564 proof -
   565   have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
   566     by auto
   567   have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
   568     by (simp add: mult.commute)
   569   thus ?thesis using sums_divide [OF 2, of 2]
   570     by simp
   571 qed
   572 
   573 
   574 subsection \<open>Telescoping\<close>
   575 
   576 lemma telescope_sums:
   577   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   578   shows   "(\<lambda>n. f (Suc n) - f n) sums (c - f 0)"
   579   unfolding sums_def
   580 proof (subst LIMSEQ_Suc_iff [symmetric])
   581   have "(\<lambda>n. \<Sum>k<Suc n. f (Suc k) - f k) = (\<lambda>n. f (Suc n) - f 0)"
   582     by (simp add: lessThan_Suc_atMost atLeast0AtMost [symmetric] setsum_Suc_diff)
   583   also have "\<dots> \<longlonglongrightarrow> c - f 0" by (intro tendsto_diff LIMSEQ_Suc[OF assms] tendsto_const)
   584   finally show "(\<lambda>n. \<Sum>n<Suc n. f (Suc n) - f n) \<longlonglongrightarrow> c - f 0" .
   585 qed
   586 
   587 lemma telescope_sums':
   588   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   589   shows   "(\<lambda>n. f n - f (Suc n)) sums (f 0 - c)"
   590   using sums_minus[OF telescope_sums[OF assms]] by (simp add: algebra_simps)
   591 
   592 lemma telescope_summable:
   593   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   594   shows   "summable (\<lambda>n. f (Suc n) - f n)"
   595   using telescope_sums[OF assms] by (simp add: sums_iff)
   596 
   597 lemma telescope_summable':
   598   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   599   shows   "summable (\<lambda>n. f n - f (Suc n))"
   600   using summable_minus[OF telescope_summable[OF assms]] by (simp add: algebra_simps)
   601 
   602 
   603 subsection \<open>Infinite summability on Banach spaces\<close>
   604 
   605 text\<open>Cauchy-type criterion for convergence of series (c.f. Harrison)\<close>
   606 
   607 lemma summable_Cauchy:
   608   fixes f :: "nat \<Rightarrow> 'a::banach"
   609   shows "summable f \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n. norm (setsum f {m..<n}) < e)"
   610   apply (simp only: summable_iff_convergent Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
   611   apply (drule spec, drule (1) mp)
   612   apply (erule exE, rule_tac x="M" in exI, clarify)
   613   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   614   apply (frule (1) order_trans)
   615   apply (drule_tac x="n" in spec, drule (1) mp)
   616   apply (drule_tac x="m" in spec, drule (1) mp)
   617   apply (simp_all add: setsum_diff [symmetric])
   618   apply (drule spec, drule (1) mp)
   619   apply (erule exE, rule_tac x="N" in exI, clarify)
   620   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   621   apply (subst norm_minus_commute)
   622   apply (simp_all add: setsum_diff [symmetric])
   623   done
   624 
   625 context
   626   fixes f :: "nat \<Rightarrow> 'a::banach"
   627 begin
   628 
   629 text\<open>Absolute convergence imples normal convergence\<close>
   630 
   631 lemma summable_norm_cancel: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
   632   apply (simp only: summable_Cauchy, safe)
   633   apply (drule_tac x="e" in spec, safe)
   634   apply (rule_tac x="N" in exI, safe)
   635   apply (drule_tac x="m" in spec, safe)
   636   apply (rule order_le_less_trans [OF norm_setsum])
   637   apply (rule order_le_less_trans [OF abs_ge_self])
   638   apply simp
   639   done
   640 
   641 lemma summable_norm: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
   642   by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel summable_LIMSEQ norm_setsum)
   643 
   644 text \<open>Comparison tests\<close>
   645 
   646 lemma summable_comparison_test: "\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n \<Longrightarrow> summable g \<Longrightarrow> summable f"
   647   apply (simp add: summable_Cauchy, safe)
   648   apply (drule_tac x="e" in spec, safe)
   649   apply (rule_tac x = "N + Na" in exI, safe)
   650   apply (rotate_tac 2)
   651   apply (drule_tac x = m in spec)
   652   apply (auto, rotate_tac 2, drule_tac x = n in spec)
   653   apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
   654   apply (rule norm_setsum)
   655   apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
   656   apply (auto intro: setsum_mono simp add: abs_less_iff)
   657   done
   658 
   659 lemma summable_comparison_test_ev:
   660   shows "eventually (\<lambda>n. norm (f n) \<le> g n) sequentially \<Longrightarrow> summable g \<Longrightarrow> summable f"
   661   by (rule summable_comparison_test) (auto simp: eventually_at_top_linorder)
   662 
   663 (*A better argument order*)
   664 lemma summable_comparison_test': "summable g \<Longrightarrow> (\<And>n. n \<ge> N \<Longrightarrow> norm(f n) \<le> g n) \<Longrightarrow> summable f"
   665   by (rule summable_comparison_test) auto
   666 
   667 subsection \<open>The Ratio Test\<close>
   668 
   669 lemma summable_ratio_test:
   670   assumes "c < 1" "\<And>n. n \<ge> N \<Longrightarrow> norm (f (Suc n)) \<le> c * norm (f n)"
   671   shows "summable f"
   672 proof cases
   673   assume "0 < c"
   674   show "summable f"
   675   proof (rule summable_comparison_test)
   676     show "\<exists>N'. \<forall>n\<ge>N'. norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   677     proof (intro exI allI impI)
   678       fix n assume "N \<le> n" then show "norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   679       proof (induct rule: inc_induct)
   680         case (step m)
   681         moreover have "norm (f (Suc m)) / c ^ Suc m * c ^ n \<le> norm (f m) / c ^ m * c ^ n"
   682           using \<open>0 < c\<close> \<open>c < 1\<close> assms(2)[OF \<open>N \<le> m\<close>] by (simp add: field_simps)
   683         ultimately show ?case by simp
   684       qed (insert \<open>0 < c\<close>, simp)
   685     qed
   686     show "summable (\<lambda>n. norm (f N) / c ^ N * c ^ n)"
   687       using \<open>0 < c\<close> \<open>c < 1\<close> by (intro summable_mult summable_geometric) simp
   688   qed
   689 next
   690   assume c: "\<not> 0 < c"
   691   { fix n assume "n \<ge> N"
   692     then have "norm (f (Suc n)) \<le> c * norm (f n)"
   693       by fact
   694     also have "\<dots> \<le> 0"
   695       using c by (simp add: not_less mult_nonpos_nonneg)
   696     finally have "f (Suc n) = 0"
   697       by auto }
   698   then show "summable f"
   699     by (intro sums_summable[OF sums_finite, of "{.. Suc N}"]) (auto simp: not_le Suc_less_eq2)
   700 qed
   701 
   702 end
   703 
   704 text\<open>Relations among convergence and absolute convergence for power series.\<close>
   705 
   706 lemma Abel_lemma:
   707   fixes a :: "nat \<Rightarrow> 'a::real_normed_vector"
   708   assumes r: "0 \<le> r" and r0: "r < r0" and M: "\<And>n. norm (a n) * r0^n \<le> M"
   709     shows "summable (\<lambda>n. norm (a n) * r^n)"
   710 proof (rule summable_comparison_test')
   711   show "summable (\<lambda>n. M * (r / r0) ^ n)"
   712     using assms
   713     by (auto simp add: summable_mult summable_geometric)
   714 next
   715   fix n
   716   show "norm (norm (a n) * r ^ n) \<le> M * (r / r0) ^ n"
   717     using r r0 M [of n]
   718     apply (auto simp add: abs_mult field_simps)
   719     apply (cases "r=0", simp)
   720     apply (cases n, auto)
   721     done
   722 qed
   723 
   724 
   725 text\<open>Summability of geometric series for real algebras\<close>
   726 
   727 lemma complete_algebra_summable_geometric:
   728   fixes x :: "'a::{real_normed_algebra_1,banach}"
   729   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   730 proof (rule summable_comparison_test)
   731   show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
   732     by (simp add: norm_power_ineq)
   733   show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
   734     by (simp add: summable_geometric)
   735 qed
   736 
   737 subsection \<open>Cauchy Product Formula\<close>
   738 
   739 text \<open>
   740   Proof based on Analysis WebNotes: Chapter 07, Class 41
   741   @{url "http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm"}
   742 \<close>
   743 
   744 lemma Cauchy_product_sums:
   745   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   746   assumes a: "summable (\<lambda>k. norm (a k))"
   747   assumes b: "summable (\<lambda>k. norm (b k))"
   748   shows "(\<lambda>k. \<Sum>i\<le>k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
   749 proof -
   750   let ?S1 = "\<lambda>n::nat. {..<n} \<times> {..<n}"
   751   let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
   752   have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
   753   have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
   754   have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
   755   have finite_S1: "\<And>n. finite (?S1 n)" by simp
   756   with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
   757 
   758   let ?g = "\<lambda>(i,j). a i * b j"
   759   let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
   760   have f_nonneg: "\<And>x. 0 \<le> ?f x" by (auto)
   761   hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
   762     unfolding real_norm_def
   763     by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
   764 
   765   have "(\<lambda>n. (\<Sum>k<n. a k) * (\<Sum>k<n. b k)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   766     by (intro tendsto_mult summable_LIMSEQ summable_norm_cancel [OF a] summable_norm_cancel [OF b])
   767   hence 1: "(\<lambda>n. setsum ?g (?S1 n)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   768     by (simp only: setsum_product setsum.Sigma [rule_format] finite_lessThan)
   769 
   770   have "(\<lambda>n. (\<Sum>k<n. norm (a k)) * (\<Sum>k<n. norm (b k))) \<longlonglongrightarrow> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   771     using a b by (intro tendsto_mult summable_LIMSEQ)
   772   hence "(\<lambda>n. setsum ?f (?S1 n)) \<longlonglongrightarrow> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   773     by (simp only: setsum_product setsum.Sigma [rule_format] finite_lessThan)
   774   hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
   775     by (rule convergentI)
   776   hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
   777     by (rule convergent_Cauchy)
   778   have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
   779   proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
   780     fix r :: real
   781     assume r: "0 < r"
   782     from CauchyD [OF Cauchy r] obtain N
   783     where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
   784     hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
   785       by (simp only: setsum_diff finite_S1 S1_mono)
   786     hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
   787       by (simp only: norm_setsum_f)
   788     show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
   789     proof (intro exI allI impI)
   790       fix n assume "2 * N \<le> n"
   791       hence n: "N \<le> n div 2" by simp
   792       have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
   793         by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
   794                   Diff_mono subset_refl S1_le_S2)
   795       also have "\<dots> < r"
   796         using n div_le_dividend by (rule N)
   797       finally show "setsum ?f (?S1 n - ?S2 n) < r" .
   798     qed
   799   qed
   800   hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
   801     apply (rule Zfun_le [rule_format])
   802     apply (simp only: norm_setsum_f)
   803     apply (rule order_trans [OF norm_setsum setsum_mono])
   804     apply (auto simp add: norm_mult_ineq)
   805     done
   806   hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) \<longlonglongrightarrow> 0"
   807     unfolding tendsto_Zfun_iff diff_0_right
   808     by (simp only: setsum_diff finite_S1 S2_le_S1)
   809 
   810   with 1 have "(\<lambda>n. setsum ?g (?S2 n)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   811     by (rule Lim_transform2)
   812   thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
   813 qed
   814 
   815 lemma Cauchy_product:
   816   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   817   assumes a: "summable (\<lambda>k. norm (a k))"
   818   assumes b: "summable (\<lambda>k. norm (b k))"
   819   shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i\<le>k. a i * b (k - i))"
   820   using a b
   821   by (rule Cauchy_product_sums [THEN sums_unique])
   822 
   823 lemma summable_Cauchy_product:
   824   assumes "summable (\<lambda>k. norm (a k :: 'a :: {real_normed_algebra,banach}))"
   825           "summable (\<lambda>k. norm (b k))"
   826   shows   "summable (\<lambda>k. \<Sum>i\<le>k. a i * b (k - i))"
   827   using Cauchy_product_sums[OF assms] by (simp add: sums_iff)
   828 
   829 subsection \<open>Series on @{typ real}s\<close>
   830 
   831 lemma summable_norm_comparison_test: "\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. norm (f n))"
   832   by (rule summable_comparison_test) auto
   833 
   834 lemma summable_rabs_comparison_test: "\<lbrakk>\<exists>N. \<forall>n\<ge>N. \<bar>f n\<bar> \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable (\<lambda>n. \<bar>f n :: real\<bar>)"
   835   by (rule summable_comparison_test) auto
   836 
   837 lemma summable_rabs_cancel: "summable (\<lambda>n. \<bar>f n :: real\<bar>) \<Longrightarrow> summable f"
   838   by (rule summable_norm_cancel) simp
   839 
   840 lemma summable_rabs: "summable (\<lambda>n. \<bar>f n :: real\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
   841   by (fold real_norm_def) (rule summable_norm)
   842 
   843 lemma summable_zero_power [simp]: "summable (\<lambda>n. 0 ^ n :: 'a :: {comm_ring_1,topological_space})"
   844 proof -
   845   have "(\<lambda>n. 0 ^ n :: 'a) = (\<lambda>n. if n = 0 then 0^0 else 0)" by (intro ext) (simp add: zero_power)
   846   moreover have "summable \<dots>" by simp
   847   ultimately show ?thesis by simp
   848 qed
   849 
   850 lemma summable_zero_power' [simp]: "summable (\<lambda>n. f n * 0 ^ n :: 'a :: {ring_1,topological_space})"
   851 proof -
   852   have "(\<lambda>n. f n * 0 ^ n :: 'a) = (\<lambda>n. if n = 0 then f 0 * 0^0 else 0)"
   853     by (intro ext) (simp add: zero_power)
   854   moreover have "summable \<dots>" by simp
   855   ultimately show ?thesis by simp
   856 qed
   857 
   858 lemma summable_power_series:
   859   fixes z :: real
   860   assumes le_1: "\<And>i. f i \<le> 1" and nonneg: "\<And>i. 0 \<le> f i" and z: "0 \<le> z" "z < 1"
   861   shows "summable (\<lambda>i. f i * z^i)"
   862 proof (rule summable_comparison_test[OF _ summable_geometric])
   863   show "norm z < 1" using z by (auto simp: less_imp_le)
   864   show "\<And>n. \<exists>N. \<forall>na\<ge>N. norm (f na * z ^ na) \<le> z ^ na"
   865     using z by (auto intro!: exI[of _ 0] mult_left_le_one_le simp: abs_mult nonneg power_abs less_imp_le le_1)
   866 qed
   867 
   868 lemma summable_0_powser:
   869   "summable (\<lambda>n. f n * 0 ^ n :: 'a :: real_normed_div_algebra)"
   870 proof -
   871   have A: "(\<lambda>n. f n * 0 ^ n) = (\<lambda>n. if n = 0 then f n else 0)"
   872     by (intro ext) auto
   873   thus ?thesis by (subst A) simp_all
   874 qed
   875 
   876 lemma summable_powser_split_head:
   877   "summable (\<lambda>n. f (Suc n) * z ^ n :: 'a :: real_normed_div_algebra) = summable (\<lambda>n. f n * z ^ n)"
   878 proof -
   879   have "summable (\<lambda>n. f (Suc n) * z ^ n) \<longleftrightarrow> summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   880   proof
   881     assume "summable (\<lambda>n. f (Suc n) * z ^ n)"
   882     from summable_mult2[OF this, of z] show "summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   883       by (simp add: power_commutes algebra_simps)
   884   next
   885     assume "summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   886     from summable_mult2[OF this, of "inverse z"] show "summable (\<lambda>n. f (Suc n) * z ^ n)"
   887       by (cases "z \<noteq> 0", subst (asm) power_Suc2) (simp_all add: algebra_simps)
   888   qed
   889   also have "\<dots> \<longleftrightarrow> summable (\<lambda>n. f n * z ^ n)" by (rule summable_Suc_iff)
   890   finally show ?thesis .
   891 qed
   892 
   893 lemma powser_split_head:
   894   assumes "summable (\<lambda>n. f n * z ^ n :: 'a :: {real_normed_div_algebra,banach})"
   895   shows   "suminf (\<lambda>n. f n * z ^ n) = f 0 + suminf (\<lambda>n. f (Suc n) * z ^ n) * z"
   896           "suminf (\<lambda>n. f (Suc n) * z ^ n) * z = suminf (\<lambda>n. f n * z ^ n) - f 0"
   897           "summable (\<lambda>n. f (Suc n) * z ^ n)"
   898 proof -
   899   from assms show "summable (\<lambda>n. f (Suc n) * z ^ n)" by (subst summable_powser_split_head)
   900 
   901   from suminf_mult2[OF this, of z]
   902     have "(\<Sum>n. f (Suc n) * z ^ n) * z = (\<Sum>n. f (Suc n) * z ^ Suc n)"
   903     by (simp add: power_commutes algebra_simps)
   904   also from assms have "\<dots> = suminf (\<lambda>n. f n * z ^ n) - f 0"
   905     by (subst suminf_split_head) simp_all
   906   finally show "suminf (\<lambda>n. f n * z ^ n) = f 0 + suminf (\<lambda>n. f (Suc n) * z ^ n) * z" by simp
   907   thus "suminf (\<lambda>n. f (Suc n) * z ^ n) * z = suminf (\<lambda>n. f n * z ^ n) - f 0" by simp
   908 qed
   909 
   910 lemma summable_partial_sum_bound:
   911   fixes f :: "nat \<Rightarrow> 'a :: banach"
   912   assumes summable: "summable f" and e: "e > (0::real)"
   913   obtains N where "\<And>m n. m \<ge> N \<Longrightarrow> norm (\<Sum>k=m..n. f k) < e"
   914 proof -
   915   from summable have "Cauchy (\<lambda>n. \<Sum>k<n. f k)"
   916     by (simp add: Cauchy_convergent_iff summable_iff_convergent)
   917   from CauchyD[OF this e] obtain N
   918     where N: "\<And>m n. m \<ge> N \<Longrightarrow> n \<ge> N \<Longrightarrow> norm ((\<Sum>k<m. f k) - (\<Sum>k<n. f k)) < e" by blast
   919   {
   920     fix m n :: nat assume m: "m \<ge> N"
   921     have "norm (\<Sum>k=m..n. f k) < e"
   922     proof (cases "n \<ge> m")
   923       assume n: "n \<ge> m"
   924       with m have "norm ((\<Sum>k<Suc n. f k) - (\<Sum>k<m. f k)) < e" by (intro N) simp_all
   925       also from n have "(\<Sum>k<Suc n. f k) - (\<Sum>k<m. f k) = (\<Sum>k=m..n. f k)"
   926         by (subst setsum_diff [symmetric]) (simp_all add: setsum_last_plus)
   927       finally show ?thesis .
   928     qed (insert e, simp_all)
   929   }
   930   thus ?thesis by (rule that)
   931 qed
   932 
   933 lemma powser_sums_if:
   934   "(\<lambda>n. (if n = m then (1 :: 'a :: {ring_1,topological_space}) else 0) * z^n) sums z^m"
   935 proof -
   936   have "(\<lambda>n. (if n = m then 1 else 0) * z^n) = (\<lambda>n. if n = m then z^n else 0)"
   937     by (intro ext) auto
   938   thus ?thesis by (simp add: sums_single)
   939 qed
   940 
   941 lemma
   942    fixes f :: "nat \<Rightarrow> real"
   943    assumes "summable f"
   944    and "inj g"
   945    and pos: "\<And>x. 0 \<le> f x"
   946    shows summable_reindex: "summable (f o g)"
   947    and suminf_reindex_mono: "suminf (f o g) \<le> suminf f"
   948    and suminf_reindex: "(\<And>x. x \<notin> range g \<Longrightarrow> f x = 0) \<Longrightarrow> suminf (f \<circ> g) = suminf f"
   949 proof -
   950   from \<open>inj g\<close> have [simp]: "\<And>A. inj_on g A" by(rule subset_inj_on) simp
   951 
   952   have smaller: "\<forall>n. (\<Sum>i<n. (f \<circ> g) i) \<le> suminf f"
   953   proof
   954     fix n
   955     have "\<forall> n' \<in> (g ` {..<n}). n' < Suc (Max (g ` {..<n}))"
   956       by(metis Max_ge finite_imageI finite_lessThan not_le not_less_eq)
   957     then obtain m where n: "\<And>n'. n' < n \<Longrightarrow> g n' < m" by blast
   958 
   959     have "(\<Sum>i<n. f (g i)) = setsum f (g ` {..<n})"
   960       by (simp add: setsum.reindex)
   961     also have "\<dots> \<le> (\<Sum>i<m. f i)"
   962       by (rule setsum_mono3) (auto simp add: pos n[rule_format])
   963     also have "\<dots> \<le> suminf f"
   964       using \<open>summable f\<close>
   965       by (rule setsum_le_suminf) (simp add: pos)
   966     finally show "(\<Sum>i<n. (f \<circ>  g) i) \<le> suminf f" by simp
   967   qed
   968 
   969   have "incseq (\<lambda>n. \<Sum>i<n. (f \<circ> g) i)"
   970     by (rule incseq_SucI) (auto simp add: pos)
   971   then obtain  L where L: "(\<lambda> n. \<Sum>i<n. (f \<circ> g) i) \<longlonglongrightarrow> L"
   972     using smaller by(rule incseq_convergent)
   973   hence "(f \<circ> g) sums L" by (simp add: sums_def)
   974   thus "summable (f o g)" by (auto simp add: sums_iff)
   975 
   976   hence "(\<lambda>n. \<Sum>i<n. (f \<circ> g) i) \<longlonglongrightarrow> suminf (f \<circ> g)"
   977     by(rule summable_LIMSEQ)
   978   thus le: "suminf (f \<circ> g) \<le> suminf f"
   979     by(rule LIMSEQ_le_const2)(blast intro: smaller[rule_format])
   980 
   981   assume f: "\<And>x. x \<notin> range g \<Longrightarrow> f x = 0"
   982 
   983   from \<open>summable f\<close> have "suminf f \<le> suminf (f \<circ> g)"
   984   proof(rule suminf_le_const)
   985     fix n
   986     have "\<forall> n' \<in> (g -` {..<n}). n' < Suc (Max (g -` {..<n}))"
   987       by(auto intro: Max_ge simp add: finite_vimageI less_Suc_eq_le)
   988     then obtain m where n: "\<And>n'. g n' < n \<Longrightarrow> n' < m" by blast
   989 
   990     have "(\<Sum>i<n. f i) = (\<Sum>i\<in>{..<n} \<inter> range g. f i)"
   991       using f by(auto intro: setsum.mono_neutral_cong_right)
   992     also have "\<dots> = (\<Sum>i\<in>g -` {..<n}. (f \<circ> g) i)"
   993       by(rule setsum.reindex_cong[where l=g])(auto)
   994     also have "\<dots> \<le> (\<Sum>i<m. (f \<circ> g) i)"
   995       by(rule setsum_mono3)(auto simp add: pos n)
   996     also have "\<dots> \<le> suminf (f \<circ> g)"
   997       using \<open>summable (f o g)\<close>
   998       by(rule setsum_le_suminf)(simp add: pos)
   999     finally show "setsum f {..<n} \<le> suminf (f \<circ> g)" .
  1000   qed
  1001   with le show "suminf (f \<circ> g) = suminf f" by(rule antisym)
  1002 qed
  1003 
  1004 lemma sums_mono_reindex:
  1005   assumes subseq: "subseq g" and zero: "\<And>n. n \<notin> range g \<Longrightarrow> f n = 0"
  1006   shows   "(\<lambda>n. f (g n)) sums c \<longleftrightarrow> f sums c"
  1007 unfolding sums_def
  1008 proof
  1009   assume lim: "(\<lambda>n. \<Sum>k<n. f k) \<longlonglongrightarrow> c"
  1010   have "(\<lambda>n. \<Sum>k<n. f (g k)) = (\<lambda>n. \<Sum>k<g n. f k)"
  1011   proof
  1012     fix n :: nat
  1013     from subseq have "(\<Sum>k<n. f (g k)) = (\<Sum>k\<in>g`{..<n}. f k)"
  1014       by (subst setsum.reindex) (auto intro: subseq_imp_inj_on)
  1015     also from subseq have "\<dots> = (\<Sum>k<g n. f k)"
  1016       by (intro setsum.mono_neutral_left ballI zero)
  1017          (auto dest: subseq_strict_mono simp: strict_mono_less strict_mono_less_eq)
  1018     finally show "(\<Sum>k<n. f (g k)) = (\<Sum>k<g n. f k)" .
  1019   qed
  1020   also from LIMSEQ_subseq_LIMSEQ[OF lim subseq] have "\<dots> \<longlonglongrightarrow> c" unfolding o_def .
  1021   finally show "(\<lambda>n. \<Sum>k<n. f (g k)) \<longlonglongrightarrow> c" .
  1022 next
  1023   assume lim: "(\<lambda>n. \<Sum>k<n. f (g k)) \<longlonglongrightarrow> c"
  1024   def g_inv \<equiv> "\<lambda>n. LEAST m. g m \<ge> n"
  1025   from filterlim_subseq[OF subseq] have g_inv_ex: "\<exists>m. g m \<ge> n" for n
  1026     by (auto simp: filterlim_at_top eventually_at_top_linorder)
  1027   hence g_inv: "g (g_inv n) \<ge> n" for n unfolding g_inv_def by (rule LeastI_ex)
  1028   have g_inv_least: "m \<ge> g_inv n" if "g m \<ge> n" for m n using that
  1029     unfolding g_inv_def by (rule Least_le)
  1030   have g_inv_least': "g m < n" if "m < g_inv n" for m n using that g_inv_least[of n m] by linarith
  1031   have "(\<lambda>n. \<Sum>k<n. f k) = (\<lambda>n. \<Sum>k<g_inv n. f (g k))"
  1032   proof
  1033     fix n :: nat
  1034     {
  1035       fix k assume k: "k \<in> {..<n} - g`{..<g_inv n}"
  1036       have "k \<notin> range g"
  1037       proof (rule notI, elim imageE)
  1038         fix l assume l: "k = g l"
  1039         have "g l < g (g_inv n)" by (rule less_le_trans[OF _ g_inv]) (insert k l, simp_all)
  1040         with subseq have "l < g_inv n" by (simp add: subseq_strict_mono strict_mono_less)
  1041         with k l show False by simp
  1042       qed
  1043       hence "f k = 0" by (rule zero)
  1044     }
  1045     with g_inv_least' g_inv have "(\<Sum>k<n. f k) = (\<Sum>k\<in>g`{..<g_inv n}. f k)"
  1046       by (intro setsum.mono_neutral_right) auto
  1047     also from subseq have "\<dots> = (\<Sum>k<g_inv n. f (g k))" using subseq_imp_inj_on
  1048       by (subst setsum.reindex) simp_all
  1049     finally show "(\<Sum>k<n. f k) = (\<Sum>k<g_inv n. f (g k))" .
  1050   qed
  1051   also {
  1052     fix K n :: nat assume "g K \<le> n"
  1053     also have "n \<le> g (g_inv n)" by (rule g_inv)
  1054     finally have "K \<le> g_inv n" using subseq by (simp add: strict_mono_less_eq subseq_strict_mono)
  1055   }
  1056   hence "filterlim g_inv at_top sequentially"
  1057     by (auto simp: filterlim_at_top eventually_at_top_linorder)
  1058   from lim and this have "(\<lambda>n. \<Sum>k<g_inv n. f (g k)) \<longlonglongrightarrow> c" by (rule filterlim_compose)
  1059   finally show "(\<lambda>n. \<Sum>k<n. f k) \<longlonglongrightarrow> c" .
  1060 qed
  1061 
  1062 lemma summable_mono_reindex:
  1063   assumes subseq: "subseq g" and zero: "\<And>n. n \<notin> range g \<Longrightarrow> f n = 0"
  1064   shows   "summable (\<lambda>n. f (g n)) \<longleftrightarrow> summable f"
  1065   using sums_mono_reindex[of g f, OF assms] by (simp add: summable_def)
  1066 
  1067 lemma suminf_mono_reindex:
  1068   assumes "subseq g" "\<And>n. n \<notin> range g \<Longrightarrow> f n = (0 :: 'a :: {t2_space,comm_monoid_add})"
  1069   shows   "suminf (\<lambda>n. f (g n)) = suminf f"
  1070 proof (cases "summable f")
  1071   case False
  1072   hence "\<not>(\<exists>c. f sums c)" unfolding summable_def by blast
  1073   hence "suminf f = The (\<lambda>_. False)" by (simp add: suminf_def)
  1074   moreover from False have "\<not>summable (\<lambda>n. f (g n))"
  1075     using summable_mono_reindex[of g f, OF assms] by simp
  1076   hence "\<not>(\<exists>c. (\<lambda>n. f (g n)) sums c)" unfolding summable_def by blast
  1077   hence "suminf (\<lambda>n. f (g n)) = The (\<lambda>_. False)" by (simp add: suminf_def)
  1078   ultimately show ?thesis by simp
  1079 qed (insert sums_mono_reindex[of g f, OF assms] summable_mono_reindex[of g f, OF assms],
  1080      simp_all add: sums_iff)
  1081 
  1082 end