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