src/HOL/Series.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62377 ace69956d018
child 62381 a6479cb85944
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     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 suminf_le_const: "summable f \<Longrightarrow> (\<And>n. setsum f {..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
   215   by (metis LIMSEQ_le_const2 summable_LIMSEQ)
   216 
   217 lemma suminf_eq_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> suminf f = 0 \<longleftrightarrow> (\<forall>n. f n = 0)"
   218 proof
   219   assume "summable f" "suminf f = 0" and pos: "\<forall>n. 0 \<le> f n"
   220   then have f: "(\<lambda>n. \<Sum>i<n. f i) \<longlonglongrightarrow> 0"
   221     using summable_LIMSEQ[of f] by simp
   222   then have "\<And>i. (\<Sum>n\<in>{i}. f n) \<le> 0"
   223   proof (rule LIMSEQ_le_const)
   224     fix i show "\<exists>N. \<forall>n\<ge>N. (\<Sum>n\<in>{i}. f n) \<le> setsum f {..<n}"
   225       using pos by (intro exI[of _ "Suc i"] allI impI setsum_mono2) auto
   226   qed
   227   with pos show "\<forall>n. f n = 0"
   228     by (auto intro!: antisym)
   229 qed (metis suminf_zero fun_eq_iff)
   230 
   231 lemma suminf_pos_iff:
   232   "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 < suminf f \<longleftrightarrow> (\<exists>i. 0 < f i)"
   233   using setsum_le_suminf[of 0] suminf_eq_zero_iff by (simp add: less_le)
   234 
   235 lemma suminf_pos2:
   236   assumes "summable f" "\<forall>n. 0 \<le> f n" "0 < f i"
   237   shows "0 < suminf f"
   238 proof -
   239   have "0 < (\<Sum>n<Suc i. f n)"
   240     using assms by (intro setsum_pos2[where i=i]) auto
   241   also have "\<dots> \<le> suminf f"
   242     using assms by (intro setsum_le_suminf) auto
   243   finally show ?thesis .
   244 qed
   245 
   246 lemma suminf_pos: "summable f \<Longrightarrow> \<forall>n. 0 < f n \<Longrightarrow> 0 < suminf f"
   247   by (intro suminf_pos2[where i=0]) (auto intro: less_imp_le)
   248 
   249 end
   250 
   251 context
   252   fixes f :: "nat \<Rightarrow> 'a::{ordered_cancel_comm_monoid_add, linorder_topology}"
   253 begin
   254 
   255 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"
   256   using
   257     setsum_le_suminf[of f "Suc i"]
   258     add_strict_increasing[of "f i" "setsum f {..<n}" "setsum f {..<i}"]
   259     setsum_mono2[of "{..<i}" "{..<n}" f]
   260   by (auto simp: less_imp_le ac_simps)
   261 
   262 lemma setsum_less_suminf: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 < f m \<Longrightarrow> setsum f {..<n} < suminf f"
   263   using setsum_less_suminf2[of n n] by (simp add: less_imp_le)
   264 
   265 end
   266 
   267 lemma summableI_nonneg_bounded:
   268   fixes f:: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology, conditionally_complete_linorder}"
   269   assumes pos[simp]: "\<And>n. 0 \<le> f n" and le: "\<And>n. (\<Sum>i<n. f i) \<le> x"
   270   shows "summable f"
   271   unfolding summable_def sums_def[abs_def]
   272 proof (rule exI LIMSEQ_incseq_SUP)+
   273   show "bdd_above (range (\<lambda>n. setsum f {..<n}))"
   274     using le by (auto simp: bdd_above_def)
   275   show "incseq (\<lambda>n. setsum f {..<n})"
   276     by (auto simp: mono_def intro!: setsum_mono2)
   277 qed
   278 
   279 lemma summableI[intro, simp]:
   280   fixes f:: "nat \<Rightarrow> 'a::{canonically_ordered_monoid_add, linorder_topology, complete_linorder}"
   281   shows "summable f"
   282   by (intro summableI_nonneg_bounded[where x=top] zero_le top_greatest)
   283 
   284 subsection \<open>Infinite summability on topological monoids\<close>
   285 
   286 lemma Zero_notin_Suc: "0 \<notin> Suc ` A"
   287   by auto
   288 
   289 context
   290   fixes f g :: "nat \<Rightarrow> 'a :: {t2_space, topological_comm_monoid_add}"
   291 begin
   292 
   293 lemma sums_Suc:
   294   assumes "(\<lambda>n. f (Suc n)) sums l" shows "f sums (l + f 0)"
   295 proof  -
   296   have "(\<lambda>n. (\<Sum>i<n. f (Suc i)) + f 0) \<longlonglongrightarrow> l + f 0"
   297     using assms by (auto intro!: tendsto_add simp: sums_def)
   298   moreover have "(\<Sum>i<n. f (Suc i)) + f 0 = (\<Sum>i<Suc n. f i)" for n
   299     unfolding lessThan_Suc_eq_insert_0 by (simp add: Zero_notin_Suc ac_simps setsum.reindex)
   300   ultimately show ?thesis
   301     by (auto simp add: sums_def simp del: setsum_lessThan_Suc intro: LIMSEQ_Suc_iff[THEN iffD1])
   302 qed
   303 
   304 lemma sums_add: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n + g n) sums (a + b)"
   305   unfolding sums_def by (simp add: setsum.distrib tendsto_add)
   306 
   307 lemma summable_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n + g n)"
   308   unfolding summable_def by (auto intro: sums_add)
   309 
   310 lemma suminf_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f + suminf g = (\<Sum>n. f n + g n)"
   311   by (intro sums_unique sums_add summable_sums)
   312 
   313 end
   314 
   315 context
   316   fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{t2_space, topological_comm_monoid_add}" and I :: "'i set"
   317 begin
   318 
   319 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)"
   320   by (induct I rule: infinite_finite_induct) (auto intro!: sums_add)
   321 
   322 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)"
   323   using sums_unique[OF sums_setsum, OF summable_sums] by simp
   324 
   325 lemma summable_setsum: "(\<And>i. i \<in> I \<Longrightarrow> summable (f i)) \<Longrightarrow> summable (\<lambda>n. \<Sum>i\<in>I. f i n)"
   326   using sums_summable[OF sums_setsum[OF summable_sums]] .
   327 
   328 end
   329 
   330 subsection \<open>Infinite summability on real normed vector spaces\<close>
   331 
   332 context
   333   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   334 begin
   335 
   336 lemma sums_Suc_iff: "(\<lambda>n. f (Suc n)) sums s \<longleftrightarrow> f sums (s + f 0)"
   337 proof -
   338   have "f sums (s + f 0) \<longleftrightarrow> (\<lambda>i. \<Sum>j<Suc i. f j) \<longlonglongrightarrow> s + f 0"
   339     by (subst LIMSEQ_Suc_iff) (simp add: sums_def)
   340   also have "\<dots> \<longleftrightarrow> (\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) \<longlonglongrightarrow> s + f 0"
   341     by (simp add: ac_simps setsum.reindex image_iff lessThan_Suc_eq_insert_0)
   342   also have "\<dots> \<longleftrightarrow> (\<lambda>n. f (Suc n)) sums s"
   343   proof
   344     assume "(\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) \<longlonglongrightarrow> s + f 0"
   345     with tendsto_add[OF this tendsto_const, of "- f 0"]
   346     show "(\<lambda>i. f (Suc i)) sums s"
   347       by (simp add: sums_def)
   348   qed (auto intro: tendsto_add simp: sums_def)
   349   finally show ?thesis ..
   350 qed
   351 
   352 lemma summable_Suc_iff: "summable (\<lambda>n. f (Suc n)) = summable f"
   353 proof
   354   assume "summable f"
   355   hence "f sums suminf f" by (rule summable_sums)
   356   hence "(\<lambda>n. f (Suc n)) sums (suminf f - f 0)" by (simp add: sums_Suc_iff)
   357   thus "summable (\<lambda>n. f (Suc n))" unfolding summable_def by blast
   358 qed (auto simp: sums_Suc_iff summable_def)
   359 
   360 end
   361 
   362 context
   363   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   364 begin
   365 
   366 lemma sums_diff: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n - g n) sums (a - b)"
   367   unfolding sums_def by (simp add: setsum_subtractf tendsto_diff)
   368 
   369 lemma summable_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n - g n)"
   370   unfolding summable_def by (auto intro: sums_diff)
   371 
   372 lemma suminf_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f - suminf g = (\<Sum>n. f n - g n)"
   373   by (intro sums_unique sums_diff summable_sums)
   374 
   375 lemma sums_minus: "f sums a \<Longrightarrow> (\<lambda>n. - f n) sums (- a)"
   376   unfolding sums_def by (simp add: setsum_negf tendsto_minus)
   377 
   378 lemma summable_minus: "summable f \<Longrightarrow> summable (\<lambda>n. - f n)"
   379   unfolding summable_def by (auto intro: sums_minus)
   380 
   381 lemma suminf_minus: "summable f \<Longrightarrow> (\<Sum>n. - f n) = - (\<Sum>n. f n)"
   382   by (intro sums_unique [symmetric] sums_minus summable_sums)
   383 
   384 lemma sums_iff_shift: "(\<lambda>i. f (i + n)) sums s \<longleftrightarrow> f sums (s + (\<Sum>i<n. f i))"
   385 proof (induct n arbitrary: s)
   386   case (Suc n)
   387   moreover have "(\<lambda>i. f (Suc i + n)) sums s \<longleftrightarrow> (\<lambda>i. f (i + n)) sums (s + f n)"
   388     by (subst sums_Suc_iff) simp
   389   ultimately show ?case
   390     by (simp add: ac_simps)
   391 qed simp
   392 
   393 corollary sums_iff_shift': "(\<lambda>i. f (i + n)) sums (s - (\<Sum>i<n. f i)) \<longleftrightarrow> f sums s"
   394   by (simp add: sums_iff_shift)
   395 
   396 lemma summable_iff_shift: "summable (\<lambda>n. f (n + k)) \<longleftrightarrow> summable f"
   397   by (metis diff_add_cancel summable_def sums_iff_shift[abs_def])
   398 
   399 lemma sums_split_initial_segment: "f sums s \<Longrightarrow> (\<lambda>i. f (i + n)) sums (s - (\<Sum>i<n. f i))"
   400   by (simp add: sums_iff_shift)
   401 
   402 lemma summable_ignore_initial_segment: "summable f \<Longrightarrow> summable (\<lambda>n. f(n + k))"
   403   by (simp add: summable_iff_shift)
   404 
   405 lemma suminf_minus_initial_segment: "summable f \<Longrightarrow> (\<Sum>n. f (n + k)) = (\<Sum>n. f n) - (\<Sum>i<k. f i)"
   406   by (rule sums_unique[symmetric]) (auto simp: sums_iff_shift)
   407 
   408 lemma suminf_split_initial_segment: "summable f \<Longrightarrow> suminf f = (\<Sum>n. f(n + k)) + (\<Sum>i<k. f i)"
   409   by (auto simp add: suminf_minus_initial_segment)
   410 
   411 lemma suminf_split_head: "summable f \<Longrightarrow> (\<Sum>n. f (Suc n)) = suminf f - f 0"
   412   using suminf_split_initial_segment[of 1] by simp
   413 
   414 lemma suminf_exist_split:
   415   fixes r :: real assumes "0 < r" and "summable f"
   416   shows "\<exists>N. \<forall>n\<ge>N. norm (\<Sum>i. f (i + n)) < r"
   417 proof -
   418   from LIMSEQ_D[OF summable_LIMSEQ[OF \<open>summable f\<close>] \<open>0 < r\<close>]
   419   obtain N :: nat where "\<forall> n \<ge> N. norm (setsum f {..<n} - suminf f) < r" by auto
   420   thus ?thesis
   421     by (auto simp: norm_minus_commute suminf_minus_initial_segment[OF \<open>summable f\<close>])
   422 qed
   423 
   424 lemma summable_LIMSEQ_zero: "summable f \<Longrightarrow> f \<longlonglongrightarrow> 0"
   425   apply (drule summable_iff_convergent [THEN iffD1])
   426   apply (drule convergent_Cauchy)
   427   apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
   428   apply (drule_tac x="r" in spec, safe)
   429   apply (rule_tac x="M" in exI, safe)
   430   apply (drule_tac x="Suc n" in spec, simp)
   431   apply (drule_tac x="n" in spec, simp)
   432   done
   433 
   434 lemma summable_imp_convergent: "summable f \<Longrightarrow> convergent f"
   435   by (force dest!: summable_LIMSEQ_zero simp: convergent_def)
   436 
   437 lemma summable_imp_Bseq: "summable f \<Longrightarrow> Bseq f"
   438   by (simp add: convergent_imp_Bseq summable_imp_convergent)
   439 
   440 end
   441 
   442 lemma summable_minus_iff:
   443   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   444   shows "summable (\<lambda>n. - f n) \<longleftrightarrow> summable f"
   445   by (auto dest: summable_minus) \<comment>\<open>used two ways, hence must be outside the context above\<close>
   446 
   447 lemma (in bounded_linear) sums: "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
   448   unfolding sums_def by (drule tendsto, simp only: setsum)
   449 
   450 lemma (in bounded_linear) summable: "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
   451   unfolding summable_def by (auto intro: sums)
   452 
   453 lemma (in bounded_linear) suminf: "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
   454   by (intro sums_unique sums summable_sums)
   455 
   456 lemmas sums_of_real = bounded_linear.sums [OF bounded_linear_of_real]
   457 lemmas summable_of_real = bounded_linear.summable [OF bounded_linear_of_real]
   458 lemmas suminf_of_real = bounded_linear.suminf [OF bounded_linear_of_real]
   459 
   460 lemmas sums_scaleR_left = bounded_linear.sums[OF bounded_linear_scaleR_left]
   461 lemmas summable_scaleR_left = bounded_linear.summable[OF bounded_linear_scaleR_left]
   462 lemmas suminf_scaleR_left = bounded_linear.suminf[OF bounded_linear_scaleR_left]
   463 
   464 lemmas sums_scaleR_right = bounded_linear.sums[OF bounded_linear_scaleR_right]
   465 lemmas summable_scaleR_right = bounded_linear.summable[OF bounded_linear_scaleR_right]
   466 lemmas suminf_scaleR_right = bounded_linear.suminf[OF bounded_linear_scaleR_right]
   467 
   468 lemma summable_const_iff: "summable (\<lambda>_. c) \<longleftrightarrow> (c :: 'a :: real_normed_vector) = 0"
   469 proof -
   470   {
   471     assume "c \<noteq> 0"
   472     hence "filterlim (\<lambda>n. of_nat n * norm c) at_top sequentially"
   473       by (subst mult.commute)
   474          (auto intro!: filterlim_tendsto_pos_mult_at_top filterlim_real_sequentially)
   475     hence "\<not>convergent (\<lambda>n. norm (\<Sum>k<n. c))"
   476       by (intro filterlim_at_infinity_imp_not_convergent filterlim_at_top_imp_at_infinity)
   477          (simp_all add: setsum_constant_scaleR)
   478     hence "\<not>summable (\<lambda>_. c)" unfolding summable_iff_convergent using convergent_norm by blast
   479   }
   480   thus ?thesis by auto
   481 qed
   482 
   483 
   484 subsection \<open>Infinite summability on real normed algebras\<close>
   485 
   486 context
   487   fixes f :: "nat \<Rightarrow> 'a::real_normed_algebra"
   488 begin
   489 
   490 lemma sums_mult: "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
   491   by (rule bounded_linear.sums [OF bounded_linear_mult_right])
   492 
   493 lemma summable_mult: "summable f \<Longrightarrow> summable (\<lambda>n. c * f n)"
   494   by (rule bounded_linear.summable [OF bounded_linear_mult_right])
   495 
   496 lemma suminf_mult: "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f"
   497   by (rule bounded_linear.suminf [OF bounded_linear_mult_right, symmetric])
   498 
   499 lemma sums_mult2: "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
   500   by (rule bounded_linear.sums [OF bounded_linear_mult_left])
   501 
   502 lemma summable_mult2: "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
   503   by (rule bounded_linear.summable [OF bounded_linear_mult_left])
   504 
   505 lemma suminf_mult2: "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
   506   by (rule bounded_linear.suminf [OF bounded_linear_mult_left])
   507 
   508 end
   509 
   510 lemma sums_mult_iff:
   511   assumes "c \<noteq> 0"
   512   shows   "(\<lambda>n. c * f n :: 'a :: {real_normed_algebra,field}) sums (c * d) \<longleftrightarrow> f sums d"
   513   using sums_mult[of f d c] sums_mult[of "\<lambda>n. c * f n" "c * d" "inverse c"]
   514   by (force simp: field_simps assms)
   515 
   516 lemma sums_mult2_iff:
   517   assumes "c \<noteq> (0 :: 'a :: {real_normed_algebra, field})"
   518   shows   "(\<lambda>n. f n * c) sums (d * c) \<longleftrightarrow> f sums d"
   519   using sums_mult_iff[OF assms, of f d] by (simp add: mult.commute)
   520 
   521 lemma sums_of_real_iff:
   522   "(\<lambda>n. of_real (f n) :: 'a :: real_normed_div_algebra) sums of_real c \<longleftrightarrow> f sums c"
   523   by (simp add: sums_def of_real_setsum[symmetric] tendsto_of_real_iff del: of_real_setsum)
   524 
   525 
   526 subsection \<open>Infinite summability on real normed fields\<close>
   527 
   528 context
   529   fixes c :: "'a::real_normed_field"
   530 begin
   531 
   532 lemma sums_divide: "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
   533   by (rule bounded_linear.sums [OF bounded_linear_divide])
   534 
   535 lemma summable_divide: "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
   536   by (rule bounded_linear.summable [OF bounded_linear_divide])
   537 
   538 lemma suminf_divide: "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
   539   by (rule bounded_linear.suminf [OF bounded_linear_divide, symmetric])
   540 
   541 lemma sums_mult_D: "\<lbrakk>(\<lambda>n. c * f n) sums a; c \<noteq> 0\<rbrakk> \<Longrightarrow> f sums (a/c)"
   542   using sums_mult_iff by fastforce
   543 
   544 lemma summable_mult_D: "\<lbrakk>summable (\<lambda>n. c * f n); c \<noteq> 0\<rbrakk> \<Longrightarrow> summable f"
   545   by (auto dest: summable_divide)
   546 
   547 text\<open>Sum of a geometric progression.\<close>
   548 
   549 lemma geometric_sums: "norm c < 1 \<Longrightarrow> (\<lambda>n. c^n) sums (1 / (1 - c))"
   550 proof -
   551   assume less_1: "norm c < 1"
   552   hence neq_1: "c \<noteq> 1" by auto
   553   hence neq_0: "c - 1 \<noteq> 0" by simp
   554   from less_1 have lim_0: "(\<lambda>n. c^n) \<longlonglongrightarrow> 0"
   555     by (rule LIMSEQ_power_zero)
   556   hence "(\<lambda>n. c ^ n / (c - 1) - 1 / (c - 1)) \<longlonglongrightarrow> 0 / (c - 1) - 1 / (c - 1)"
   557     using neq_0 by (intro tendsto_intros)
   558   hence "(\<lambda>n. (c ^ n - 1) / (c - 1)) \<longlonglongrightarrow> 1 / (1 - c)"
   559     by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
   560   thus "(\<lambda>n. c ^ n) sums (1 / (1 - c))"
   561     by (simp add: sums_def geometric_sum neq_1)
   562 qed
   563 
   564 lemma summable_geometric: "norm c < 1 \<Longrightarrow> summable (\<lambda>n. c^n)"
   565   by (rule geometric_sums [THEN sums_summable])
   566 
   567 lemma suminf_geometric: "norm c < 1 \<Longrightarrow> suminf (\<lambda>n. c^n) = 1 / (1 - c)"
   568   by (rule sums_unique[symmetric]) (rule geometric_sums)
   569 
   570 lemma summable_geometric_iff: "summable (\<lambda>n. c ^ n) \<longleftrightarrow> norm c < 1"
   571 proof
   572   assume "summable (\<lambda>n. c ^ n :: 'a :: real_normed_field)"
   573   hence "(\<lambda>n. norm c ^ n) \<longlonglongrightarrow> 0"
   574     by (simp add: norm_power [symmetric] tendsto_norm_zero_iff summable_LIMSEQ_zero)
   575   from order_tendstoD(2)[OF this zero_less_one] obtain n where "norm c ^ n < 1"
   576     by (auto simp: eventually_at_top_linorder)
   577   thus "norm c < 1" using one_le_power[of "norm c" n] by (cases "norm c \<ge> 1") (linarith, simp)
   578 qed (rule summable_geometric)
   579 
   580 end
   581 
   582 lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
   583 proof -
   584   have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
   585     by auto
   586   have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
   587     by (simp add: mult.commute)
   588   thus ?thesis using sums_divide [OF 2, of 2]
   589     by simp
   590 qed
   591 
   592 
   593 subsection \<open>Telescoping\<close>
   594 
   595 lemma telescope_sums:
   596   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   597   shows   "(\<lambda>n. f (Suc n) - f n) sums (c - f 0)"
   598   unfolding sums_def
   599 proof (subst LIMSEQ_Suc_iff [symmetric])
   600   have "(\<lambda>n. \<Sum>k<Suc n. f (Suc k) - f k) = (\<lambda>n. f (Suc n) - f 0)"
   601     by (simp add: lessThan_Suc_atMost atLeast0AtMost [symmetric] setsum_Suc_diff)
   602   also have "\<dots> \<longlonglongrightarrow> c - f 0" by (intro tendsto_diff LIMSEQ_Suc[OF assms] tendsto_const)
   603   finally show "(\<lambda>n. \<Sum>n<Suc n. f (Suc n) - f n) \<longlonglongrightarrow> c - f 0" .
   604 qed
   605 
   606 lemma telescope_sums':
   607   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   608   shows   "(\<lambda>n. f n - f (Suc n)) sums (f 0 - c)"
   609   using sums_minus[OF telescope_sums[OF assms]] by (simp add: algebra_simps)
   610 
   611 lemma telescope_summable:
   612   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   613   shows   "summable (\<lambda>n. f (Suc n) - f n)"
   614   using telescope_sums[OF assms] by (simp add: sums_iff)
   615 
   616 lemma telescope_summable':
   617   assumes "f \<longlonglongrightarrow> (c :: 'a :: real_normed_vector)"
   618   shows   "summable (\<lambda>n. f n - f (Suc n))"
   619   using summable_minus[OF telescope_summable[OF assms]] by (simp add: algebra_simps)
   620 
   621 
   622 subsection \<open>Infinite summability on Banach spaces\<close>
   623 
   624 text\<open>Cauchy-type criterion for convergence of series (c.f. Harrison)\<close>
   625 
   626 lemma summable_Cauchy:
   627   fixes f :: "nat \<Rightarrow> 'a::banach"
   628   shows "summable f \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n. norm (setsum f {m..<n}) < e)"
   629   apply (simp only: summable_iff_convergent Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
   630   apply (drule spec, drule (1) mp)
   631   apply (erule exE, rule_tac x="M" in exI, clarify)
   632   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   633   apply (frule (1) order_trans)
   634   apply (drule_tac x="n" in spec, drule (1) mp)
   635   apply (drule_tac x="m" in spec, drule (1) mp)
   636   apply (simp_all add: setsum_diff [symmetric])
   637   apply (drule spec, drule (1) mp)
   638   apply (erule exE, rule_tac x="N" in exI, clarify)
   639   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   640   apply (subst norm_minus_commute)
   641   apply (simp_all add: setsum_diff [symmetric])
   642   done
   643 
   644 context
   645   fixes f :: "nat \<Rightarrow> 'a::banach"
   646 begin
   647 
   648 text\<open>Absolute convergence imples normal convergence\<close>
   649 
   650 lemma summable_norm_cancel: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
   651   apply (simp only: summable_Cauchy, safe)
   652   apply (drule_tac x="e" in spec, safe)
   653   apply (rule_tac x="N" in exI, safe)
   654   apply (drule_tac x="m" in spec, safe)
   655   apply (rule order_le_less_trans [OF norm_setsum])
   656   apply (rule order_le_less_trans [OF abs_ge_self])
   657   apply simp
   658   done
   659 
   660 lemma summable_norm: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
   661   by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel summable_LIMSEQ norm_setsum)
   662 
   663 text \<open>Comparison tests\<close>
   664 
   665 lemma summable_comparison_test: "\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n \<Longrightarrow> summable g \<Longrightarrow> summable f"
   666   apply (simp add: summable_Cauchy, safe)
   667   apply (drule_tac x="e" in spec, safe)
   668   apply (rule_tac x = "N + Na" in exI, safe)
   669   apply (rotate_tac 2)
   670   apply (drule_tac x = m in spec)
   671   apply (auto, rotate_tac 2, drule_tac x = n in spec)
   672   apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
   673   apply (rule norm_setsum)
   674   apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
   675   apply (auto intro: setsum_mono simp add: abs_less_iff)
   676   done
   677 
   678 lemma summable_comparison_test_ev:
   679   shows "eventually (\<lambda>n. norm (f n) \<le> g n) sequentially \<Longrightarrow> summable g \<Longrightarrow> summable f"
   680   by (rule summable_comparison_test) (auto simp: eventually_at_top_linorder)
   681 
   682 (*A better argument order*)
   683 lemma summable_comparison_test': "summable g \<Longrightarrow> (\<And>n. n \<ge> N \<Longrightarrow> norm(f n) \<le> g n) \<Longrightarrow> summable f"
   684   by (rule summable_comparison_test) auto
   685 
   686 subsection \<open>The Ratio Test\<close>
   687 
   688 lemma summable_ratio_test:
   689   assumes "c < 1" "\<And>n. n \<ge> N \<Longrightarrow> norm (f (Suc n)) \<le> c * norm (f n)"
   690   shows "summable f"
   691 proof cases
   692   assume "0 < c"
   693   show "summable f"
   694   proof (rule summable_comparison_test)
   695     show "\<exists>N'. \<forall>n\<ge>N'. norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   696     proof (intro exI allI impI)
   697       fix n assume "N \<le> n" then show "norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   698       proof (induct rule: inc_induct)
   699         case (step m)
   700         moreover have "norm (f (Suc m)) / c ^ Suc m * c ^ n \<le> norm (f m) / c ^ m * c ^ n"
   701           using \<open>0 < c\<close> \<open>c < 1\<close> assms(2)[OF \<open>N \<le> m\<close>] by (simp add: field_simps)
   702         ultimately show ?case by simp
   703       qed (insert \<open>0 < c\<close>, simp)
   704     qed
   705     show "summable (\<lambda>n. norm (f N) / c ^ N * c ^ n)"
   706       using \<open>0 < c\<close> \<open>c < 1\<close> by (intro summable_mult summable_geometric) simp
   707   qed
   708 next
   709   assume c: "\<not> 0 < c"
   710   { fix n assume "n \<ge> N"
   711     then have "norm (f (Suc n)) \<le> c * norm (f n)"
   712       by fact
   713     also have "\<dots> \<le> 0"
   714       using c by (simp add: not_less mult_nonpos_nonneg)
   715     finally have "f (Suc n) = 0"
   716       by auto }
   717   then show "summable f"
   718     by (intro sums_summable[OF sums_finite, of "{.. Suc N}"]) (auto simp: not_le Suc_less_eq2)
   719 qed
   720 
   721 end
   722 
   723 text\<open>Relations among convergence and absolute convergence for power series.\<close>
   724 
   725 lemma Abel_lemma:
   726   fixes a :: "nat \<Rightarrow> 'a::real_normed_vector"
   727   assumes r: "0 \<le> r" and r0: "r < r0" and M: "\<And>n. norm (a n) * r0^n \<le> M"
   728     shows "summable (\<lambda>n. norm (a n) * r^n)"
   729 proof (rule summable_comparison_test')
   730   show "summable (\<lambda>n. M * (r / r0) ^ n)"
   731     using assms
   732     by (auto simp add: summable_mult summable_geometric)
   733 next
   734   fix n
   735   show "norm (norm (a n) * r ^ n) \<le> M * (r / r0) ^ n"
   736     using r r0 M [of n]
   737     apply (auto simp add: abs_mult field_simps)
   738     apply (cases "r=0", simp)
   739     apply (cases n, auto)
   740     done
   741 qed
   742 
   743 
   744 text\<open>Summability of geometric series for real algebras\<close>
   745 
   746 lemma complete_algebra_summable_geometric:
   747   fixes x :: "'a::{real_normed_algebra_1,banach}"
   748   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   749 proof (rule summable_comparison_test)
   750   show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
   751     by (simp add: norm_power_ineq)
   752   show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
   753     by (simp add: summable_geometric)
   754 qed
   755 
   756 subsection \<open>Cauchy Product Formula\<close>
   757 
   758 text \<open>
   759   Proof based on Analysis WebNotes: Chapter 07, Class 41
   760   @{url "http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm"}
   761 \<close>
   762 
   763 lemma Cauchy_product_sums:
   764   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   765   assumes a: "summable (\<lambda>k. norm (a k))"
   766   assumes b: "summable (\<lambda>k. norm (b k))"
   767   shows "(\<lambda>k. \<Sum>i\<le>k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
   768 proof -
   769   let ?S1 = "\<lambda>n::nat. {..<n} \<times> {..<n}"
   770   let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
   771   have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
   772   have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
   773   have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
   774   have finite_S1: "\<And>n. finite (?S1 n)" by simp
   775   with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
   776 
   777   let ?g = "\<lambda>(i,j). a i * b j"
   778   let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
   779   have f_nonneg: "\<And>x. 0 \<le> ?f x" by (auto)
   780   hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
   781     unfolding real_norm_def
   782     by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
   783 
   784   have "(\<lambda>n. (\<Sum>k<n. a k) * (\<Sum>k<n. b k)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   785     by (intro tendsto_mult summable_LIMSEQ summable_norm_cancel [OF a] summable_norm_cancel [OF b])
   786   hence 1: "(\<lambda>n. setsum ?g (?S1 n)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   787     by (simp only: setsum_product setsum.Sigma [rule_format] finite_lessThan)
   788 
   789   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))"
   790     using a b by (intro tendsto_mult summable_LIMSEQ)
   791   hence "(\<lambda>n. setsum ?f (?S1 n)) \<longlonglongrightarrow> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   792     by (simp only: setsum_product setsum.Sigma [rule_format] finite_lessThan)
   793   hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
   794     by (rule convergentI)
   795   hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
   796     by (rule convergent_Cauchy)
   797   have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
   798   proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
   799     fix r :: real
   800     assume r: "0 < r"
   801     from CauchyD [OF Cauchy r] obtain N
   802     where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
   803     hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
   804       by (simp only: setsum_diff finite_S1 S1_mono)
   805     hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
   806       by (simp only: norm_setsum_f)
   807     show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
   808     proof (intro exI allI impI)
   809       fix n assume "2 * N \<le> n"
   810       hence n: "N \<le> n div 2" by simp
   811       have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
   812         by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
   813                   Diff_mono subset_refl S1_le_S2)
   814       also have "\<dots> < r"
   815         using n div_le_dividend by (rule N)
   816       finally show "setsum ?f (?S1 n - ?S2 n) < r" .
   817     qed
   818   qed
   819   hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
   820     apply (rule Zfun_le [rule_format])
   821     apply (simp only: norm_setsum_f)
   822     apply (rule order_trans [OF norm_setsum setsum_mono])
   823     apply (auto simp add: norm_mult_ineq)
   824     done
   825   hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) \<longlonglongrightarrow> 0"
   826     unfolding tendsto_Zfun_iff diff_0_right
   827     by (simp only: setsum_diff finite_S1 S2_le_S1)
   828 
   829   with 1 have "(\<lambda>n. setsum ?g (?S2 n)) \<longlonglongrightarrow> (\<Sum>k. a k) * (\<Sum>k. b k)"
   830     by (rule Lim_transform2)
   831   thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
   832 qed
   833 
   834 lemma Cauchy_product:
   835   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   836   assumes a: "summable (\<lambda>k. norm (a k))"
   837   assumes b: "summable (\<lambda>k. norm (b k))"
   838   shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i\<le>k. a i * b (k - i))"
   839   using a b
   840   by (rule Cauchy_product_sums [THEN sums_unique])
   841 
   842 lemma summable_Cauchy_product:
   843   assumes "summable (\<lambda>k. norm (a k :: 'a :: {real_normed_algebra,banach}))"
   844           "summable (\<lambda>k. norm (b k))"
   845   shows   "summable (\<lambda>k. \<Sum>i\<le>k. a i * b (k - i))"
   846   using Cauchy_product_sums[OF assms] by (simp add: sums_iff)
   847 
   848 subsection \<open>Series on @{typ real}s\<close>
   849 
   850 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))"
   851   by (rule summable_comparison_test) auto
   852 
   853 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>)"
   854   by (rule summable_comparison_test) auto
   855 
   856 lemma summable_rabs_cancel: "summable (\<lambda>n. \<bar>f n :: real\<bar>) \<Longrightarrow> summable f"
   857   by (rule summable_norm_cancel) simp
   858 
   859 lemma summable_rabs: "summable (\<lambda>n. \<bar>f n :: real\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
   860   by (fold real_norm_def) (rule summable_norm)
   861 
   862 lemma summable_zero_power [simp]: "summable (\<lambda>n. 0 ^ n :: 'a :: {comm_ring_1,topological_space})"
   863 proof -
   864   have "(\<lambda>n. 0 ^ n :: 'a) = (\<lambda>n. if n = 0 then 0^0 else 0)" by (intro ext) (simp add: zero_power)
   865   moreover have "summable \<dots>" by simp
   866   ultimately show ?thesis by simp
   867 qed
   868 
   869 lemma summable_zero_power' [simp]: "summable (\<lambda>n. f n * 0 ^ n :: 'a :: {ring_1,topological_space})"
   870 proof -
   871   have "(\<lambda>n. f n * 0 ^ n :: 'a) = (\<lambda>n. if n = 0 then f 0 * 0^0 else 0)"
   872     by (intro ext) (simp add: zero_power)
   873   moreover have "summable \<dots>" by simp
   874   ultimately show ?thesis by simp
   875 qed
   876 
   877 lemma summable_power_series:
   878   fixes z :: real
   879   assumes le_1: "\<And>i. f i \<le> 1" and nonneg: "\<And>i. 0 \<le> f i" and z: "0 \<le> z" "z < 1"
   880   shows "summable (\<lambda>i. f i * z^i)"
   881 proof (rule summable_comparison_test[OF _ summable_geometric])
   882   show "norm z < 1" using z by (auto simp: less_imp_le)
   883   show "\<And>n. \<exists>N. \<forall>na\<ge>N. norm (f na * z ^ na) \<le> z ^ na"
   884     using z by (auto intro!: exI[of _ 0] mult_left_le_one_le simp: abs_mult nonneg power_abs less_imp_le le_1)
   885 qed
   886 
   887 lemma summable_0_powser:
   888   "summable (\<lambda>n. f n * 0 ^ n :: 'a :: real_normed_div_algebra)"
   889 proof -
   890   have A: "(\<lambda>n. f n * 0 ^ n) = (\<lambda>n. if n = 0 then f n else 0)"
   891     by (intro ext) auto
   892   thus ?thesis by (subst A) simp_all
   893 qed
   894 
   895 lemma summable_powser_split_head:
   896   "summable (\<lambda>n. f (Suc n) * z ^ n :: 'a :: real_normed_div_algebra) = summable (\<lambda>n. f n * z ^ n)"
   897 proof -
   898   have "summable (\<lambda>n. f (Suc n) * z ^ n) \<longleftrightarrow> summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   899   proof
   900     assume "summable (\<lambda>n. f (Suc n) * z ^ n)"
   901     from summable_mult2[OF this, of z] show "summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   902       by (simp add: power_commutes algebra_simps)
   903   next
   904     assume "summable (\<lambda>n. f (Suc n) * z ^ Suc n)"
   905     from summable_mult2[OF this, of "inverse z"] show "summable (\<lambda>n. f (Suc n) * z ^ n)"
   906       by (cases "z \<noteq> 0", subst (asm) power_Suc2) (simp_all add: algebra_simps)
   907   qed
   908   also have "\<dots> \<longleftrightarrow> summable (\<lambda>n. f n * z ^ n)" by (rule summable_Suc_iff)
   909   finally show ?thesis .
   910 qed
   911 
   912 lemma powser_split_head:
   913   assumes "summable (\<lambda>n. f n * z ^ n :: 'a :: {real_normed_div_algebra,banach})"
   914   shows   "suminf (\<lambda>n. f n * z ^ n) = f 0 + suminf (\<lambda>n. f (Suc n) * z ^ n) * z"
   915           "suminf (\<lambda>n. f (Suc n) * z ^ n) * z = suminf (\<lambda>n. f n * z ^ n) - f 0"
   916           "summable (\<lambda>n. f (Suc n) * z ^ n)"
   917 proof -
   918   from assms show "summable (\<lambda>n. f (Suc n) * z ^ n)" by (subst summable_powser_split_head)
   919 
   920   from suminf_mult2[OF this, of z]
   921     have "(\<Sum>n. f (Suc n) * z ^ n) * z = (\<Sum>n. f (Suc n) * z ^ Suc n)"
   922     by (simp add: power_commutes algebra_simps)
   923   also from assms have "\<dots> = suminf (\<lambda>n. f n * z ^ n) - f 0"
   924     by (subst suminf_split_head) simp_all
   925   finally show "suminf (\<lambda>n. f n * z ^ n) = f 0 + suminf (\<lambda>n. f (Suc n) * z ^ n) * z" by simp
   926   thus "suminf (\<lambda>n. f (Suc n) * z ^ n) * z = suminf (\<lambda>n. f n * z ^ n) - f 0" by simp
   927 qed
   928 
   929 lemma summable_partial_sum_bound:
   930   fixes f :: "nat \<Rightarrow> 'a :: banach"
   931   assumes summable: "summable f" and e: "e > (0::real)"
   932   obtains N where "\<And>m n. m \<ge> N \<Longrightarrow> norm (\<Sum>k=m..n. f k) < e"
   933 proof -
   934   from summable have "Cauchy (\<lambda>n. \<Sum>k<n. f k)"
   935     by (simp add: Cauchy_convergent_iff summable_iff_convergent)
   936   from CauchyD[OF this e] obtain N
   937     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
   938   {
   939     fix m n :: nat assume m: "m \<ge> N"
   940     have "norm (\<Sum>k=m..n. f k) < e"
   941     proof (cases "n \<ge> m")
   942       assume n: "n \<ge> m"
   943       with m have "norm ((\<Sum>k<Suc n. f k) - (\<Sum>k<m. f k)) < e" by (intro N) simp_all
   944       also from n have "(\<Sum>k<Suc n. f k) - (\<Sum>k<m. f k) = (\<Sum>k=m..n. f k)"
   945         by (subst setsum_diff [symmetric]) (simp_all add: setsum_last_plus)
   946       finally show ?thesis .
   947     qed (insert e, simp_all)
   948   }
   949   thus ?thesis by (rule that)
   950 qed
   951 
   952 lemma powser_sums_if:
   953   "(\<lambda>n. (if n = m then (1 :: 'a :: {ring_1,topological_space}) else 0) * z^n) sums z^m"
   954 proof -
   955   have "(\<lambda>n. (if n = m then 1 else 0) * z^n) = (\<lambda>n. if n = m then z^n else 0)"
   956     by (intro ext) auto
   957   thus ?thesis by (simp add: sums_single)
   958 qed
   959 
   960 lemma
   961    fixes f :: "nat \<Rightarrow> real"
   962    assumes "summable f"
   963    and "inj g"
   964    and pos: "\<And>x. 0 \<le> f x"
   965    shows summable_reindex: "summable (f o g)"
   966    and suminf_reindex_mono: "suminf (f o g) \<le> suminf f"
   967    and suminf_reindex: "(\<And>x. x \<notin> range g \<Longrightarrow> f x = 0) \<Longrightarrow> suminf (f \<circ> g) = suminf f"
   968 proof -
   969   from \<open>inj g\<close> have [simp]: "\<And>A. inj_on g A" by(rule subset_inj_on) simp
   970 
   971   have smaller: "\<forall>n. (\<Sum>i<n. (f \<circ> g) i) \<le> suminf f"
   972   proof
   973     fix n
   974     have "\<forall> n' \<in> (g ` {..<n}). n' < Suc (Max (g ` {..<n}))"
   975       by(metis Max_ge finite_imageI finite_lessThan not_le not_less_eq)
   976     then obtain m where n: "\<And>n'. n' < n \<Longrightarrow> g n' < m" by blast
   977 
   978     have "(\<Sum>i<n. f (g i)) = setsum f (g ` {..<n})"
   979       by (simp add: setsum.reindex)
   980     also have "\<dots> \<le> (\<Sum>i<m. f i)"
   981       by (rule setsum_mono3) (auto simp add: pos n[rule_format])
   982     also have "\<dots> \<le> suminf f"
   983       using \<open>summable f\<close>
   984       by (rule setsum_le_suminf) (simp add: pos)
   985     finally show "(\<Sum>i<n. (f \<circ>  g) i) \<le> suminf f" by simp
   986   qed
   987 
   988   have "incseq (\<lambda>n. \<Sum>i<n. (f \<circ> g) i)"
   989     by (rule incseq_SucI) (auto simp add: pos)
   990   then obtain  L where L: "(\<lambda> n. \<Sum>i<n. (f \<circ> g) i) \<longlonglongrightarrow> L"
   991     using smaller by(rule incseq_convergent)
   992   hence "(f \<circ> g) sums L" by (simp add: sums_def)
   993   thus "summable (f o g)" by (auto simp add: sums_iff)
   994 
   995   hence "(\<lambda>n. \<Sum>i<n. (f \<circ> g) i) \<longlonglongrightarrow> suminf (f \<circ> g)"
   996     by(rule summable_LIMSEQ)
   997   thus le: "suminf (f \<circ> g) \<le> suminf f"
   998     by(rule LIMSEQ_le_const2)(blast intro: smaller[rule_format])
   999 
  1000   assume f: "\<And>x. x \<notin> range g \<Longrightarrow> f x = 0"
  1001 
  1002   from \<open>summable f\<close> have "suminf f \<le> suminf (f \<circ> g)"
  1003   proof(rule suminf_le_const)
  1004     fix n
  1005     have "\<forall> n' \<in> (g -` {..<n}). n' < Suc (Max (g -` {..<n}))"
  1006       by(auto intro: Max_ge simp add: finite_vimageI less_Suc_eq_le)
  1007     then obtain m where n: "\<And>n'. g n' < n \<Longrightarrow> n' < m" by blast
  1008 
  1009     have "(\<Sum>i<n. f i) = (\<Sum>i\<in>{..<n} \<inter> range g. f i)"
  1010       using f by(auto intro: setsum.mono_neutral_cong_right)
  1011     also have "\<dots> = (\<Sum>i\<in>g -` {..<n}. (f \<circ> g) i)"
  1012       by(rule setsum.reindex_cong[where l=g])(auto)
  1013     also have "\<dots> \<le> (\<Sum>i<m. (f \<circ> g) i)"
  1014       by(rule setsum_mono3)(auto simp add: pos n)
  1015     also have "\<dots> \<le> suminf (f \<circ> g)"
  1016       using \<open>summable (f o g)\<close>
  1017       by(rule setsum_le_suminf)(simp add: pos)
  1018     finally show "setsum f {..<n} \<le> suminf (f \<circ> g)" .
  1019   qed
  1020   with le show "suminf (f \<circ> g) = suminf f" by(rule antisym)
  1021 qed
  1022 
  1023 lemma sums_mono_reindex:
  1024   assumes subseq: "subseq g" and zero: "\<And>n. n \<notin> range g \<Longrightarrow> f n = 0"
  1025   shows   "(\<lambda>n. f (g n)) sums c \<longleftrightarrow> f sums c"
  1026 unfolding sums_def
  1027 proof
  1028   assume lim: "(\<lambda>n. \<Sum>k<n. f k) \<longlonglongrightarrow> c"
  1029   have "(\<lambda>n. \<Sum>k<n. f (g k)) = (\<lambda>n. \<Sum>k<g n. f k)"
  1030   proof
  1031     fix n :: nat
  1032     from subseq have "(\<Sum>k<n. f (g k)) = (\<Sum>k\<in>g`{..<n}. f k)"
  1033       by (subst setsum.reindex) (auto intro: subseq_imp_inj_on)
  1034     also from subseq have "\<dots> = (\<Sum>k<g n. f k)"
  1035       by (intro setsum.mono_neutral_left ballI zero)
  1036          (auto dest: subseq_strict_mono simp: strict_mono_less strict_mono_less_eq)
  1037     finally show "(\<Sum>k<n. f (g k)) = (\<Sum>k<g n. f k)" .
  1038   qed
  1039   also from LIMSEQ_subseq_LIMSEQ[OF lim subseq] have "\<dots> \<longlonglongrightarrow> c" unfolding o_def .
  1040   finally show "(\<lambda>n. \<Sum>k<n. f (g k)) \<longlonglongrightarrow> c" .
  1041 next
  1042   assume lim: "(\<lambda>n. \<Sum>k<n. f (g k)) \<longlonglongrightarrow> c"
  1043   def g_inv \<equiv> "\<lambda>n. LEAST m. g m \<ge> n"
  1044   from filterlim_subseq[OF subseq] have g_inv_ex: "\<exists>m. g m \<ge> n" for n
  1045     by (auto simp: filterlim_at_top eventually_at_top_linorder)
  1046   hence g_inv: "g (g_inv n) \<ge> n" for n unfolding g_inv_def by (rule LeastI_ex)
  1047   have g_inv_least: "m \<ge> g_inv n" if "g m \<ge> n" for m n using that
  1048     unfolding g_inv_def by (rule Least_le)
  1049   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
  1050   have "(\<lambda>n. \<Sum>k<n. f k) = (\<lambda>n. \<Sum>k<g_inv n. f (g k))"
  1051   proof
  1052     fix n :: nat
  1053     {
  1054       fix k assume k: "k \<in> {..<n} - g`{..<g_inv n}"
  1055       have "k \<notin> range g"
  1056       proof (rule notI, elim imageE)
  1057         fix l assume l: "k = g l"
  1058         have "g l < g (g_inv n)" by (rule less_le_trans[OF _ g_inv]) (insert k l, simp_all)
  1059         with subseq have "l < g_inv n" by (simp add: subseq_strict_mono strict_mono_less)
  1060         with k l show False by simp
  1061       qed
  1062       hence "f k = 0" by (rule zero)
  1063     }
  1064     with g_inv_least' g_inv have "(\<Sum>k<n. f k) = (\<Sum>k\<in>g`{..<g_inv n}. f k)"
  1065       by (intro setsum.mono_neutral_right) auto
  1066     also from subseq have "\<dots> = (\<Sum>k<g_inv n. f (g k))" using subseq_imp_inj_on
  1067       by (subst setsum.reindex) simp_all
  1068     finally show "(\<Sum>k<n. f k) = (\<Sum>k<g_inv n. f (g k))" .
  1069   qed
  1070   also {
  1071     fix K n :: nat assume "g K \<le> n"
  1072     also have "n \<le> g (g_inv n)" by (rule g_inv)
  1073     finally have "K \<le> g_inv n" using subseq by (simp add: strict_mono_less_eq subseq_strict_mono)
  1074   }
  1075   hence "filterlim g_inv at_top sequentially"
  1076     by (auto simp: filterlim_at_top eventually_at_top_linorder)
  1077   from lim and this have "(\<lambda>n. \<Sum>k<g_inv n. f (g k)) \<longlonglongrightarrow> c" by (rule filterlim_compose)
  1078   finally show "(\<lambda>n. \<Sum>k<n. f k) \<longlonglongrightarrow> c" .
  1079 qed
  1080 
  1081 lemma summable_mono_reindex:
  1082   assumes subseq: "subseq g" and zero: "\<And>n. n \<notin> range g \<Longrightarrow> f n = 0"
  1083   shows   "summable (\<lambda>n. f (g n)) \<longleftrightarrow> summable f"
  1084   using sums_mono_reindex[of g f, OF assms] by (simp add: summable_def)
  1085 
  1086 lemma suminf_mono_reindex:
  1087   assumes "subseq g" "\<And>n. n \<notin> range g \<Longrightarrow> f n = (0 :: 'a :: {t2_space,comm_monoid_add})"
  1088   shows   "suminf (\<lambda>n. f (g n)) = suminf f"
  1089 proof (cases "summable f")
  1090   case False
  1091   hence "\<not>(\<exists>c. f sums c)" unfolding summable_def by blast
  1092   hence "suminf f = The (\<lambda>_. False)" by (simp add: suminf_def)
  1093   moreover from False have "\<not>summable (\<lambda>n. f (g n))"
  1094     using summable_mono_reindex[of g f, OF assms] by simp
  1095   hence "\<not>(\<exists>c. (\<lambda>n. f (g n)) sums c)" unfolding summable_def by blast
  1096   hence "suminf (\<lambda>n. f (g n)) = The (\<lambda>_. False)" by (simp add: suminf_def)
  1097   ultimately show ?thesis by simp
  1098 qed (insert sums_mono_reindex[of g f, OF assms] summable_mono_reindex[of g f, OF assms],
  1099      simp_all add: sums_iff)
  1100 
  1101 end