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