src/HOL/Series.thy
author hoelzl
Tue Mar 18 16:29:32 2014 +0100 (2014-03-18)
changeset 56194 9ffbb4004c81
parent 56193 c726ecfb22b6
child 56213 e5720d3c18f0
permissions -rw-r--r--
fix HOL-NSA; move lemmas
     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 header {* Finite Summation and Infinite Series *}
    11 
    12 theory Series
    13 imports Limits
    14 begin
    15 
    16 definition
    17   sums :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a \<Rightarrow> bool"
    18   (infixr "sums" 80)
    19 where
    20   "f sums s \<longleftrightarrow> (\<lambda>n. \<Sum>i<n. f i) ----> s"
    21 
    22 definition summable :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> bool" where
    23    "summable f \<longleftrightarrow> (\<exists>s. f sums s)"
    24 
    25 definition
    26   suminf :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a"
    27   (binder "\<Sum>" 10)
    28 where
    29   "suminf f = (THE s. f sums s)"
    30 
    31 lemma sums_subst[trans]: "f = g \<Longrightarrow> g sums z \<Longrightarrow> f sums z"
    32   by simp
    33 
    34 lemma sums_summable: "f sums l \<Longrightarrow> summable f"
    35   by (simp add: sums_def summable_def, blast)
    36 
    37 lemma summable_iff_convergent: "summable f \<longleftrightarrow> convergent (\<lambda>n. \<Sum>i<n. f i)"
    38   by (simp add: summable_def sums_def convergent_def)
    39 
    40 lemma suminf_eq_lim: "suminf f = lim (\<lambda>n. \<Sum>i<n. f i)"
    41   by (simp add: suminf_def sums_def lim_def)
    42 
    43 lemma sums_finite:
    44   assumes [simp]: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
    45   shows "f sums (\<Sum>n\<in>N. f n)"
    46 proof -
    47   { fix n
    48     have "setsum f {..<n + Suc (Max N)} = setsum f N"
    49     proof cases
    50       assume "N = {}"
    51       with f have "f = (\<lambda>x. 0)" by auto
    52       then show ?thesis by simp
    53     next
    54       assume [simp]: "N \<noteq> {}"
    55       show ?thesis
    56       proof (safe intro!: setsum_mono_zero_right f)
    57         fix i assume "i \<in> N"
    58         then have "i \<le> Max N" by simp
    59         then show "i < n + Suc (Max N)" by simp
    60       qed
    61     qed }
    62   note eq = this
    63   show ?thesis unfolding sums_def
    64     by (rule LIMSEQ_offset[of _ "Suc (Max N)"])
    65        (simp add: eq atLeast0LessThan tendsto_const del: add_Suc_right)
    66 qed
    67 
    68 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)"
    69   using sums_finite[of A "(\<lambda>r. if r \<in> A then f r else 0)"] by simp
    70 
    71 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)"
    72   using sums_If_finite_set[of "{r. P r}"] by simp
    73 
    74 lemma sums_single: "(\<lambda>r. if r = i then f r else 0) sums f i"
    75   using sums_If_finite[of "\<lambda>r. r = i"] by simp
    76 
    77 lemma series_zero: (* REMOVE *)
    78   "(\<And>m. n \<le> m \<Longrightarrow> f m = 0) \<Longrightarrow> f sums (\<Sum>i<n. f i)"
    79   by (rule sums_finite) auto
    80 
    81 lemma sums_zero[simp, intro]: "(\<lambda>n. 0) sums 0"
    82   unfolding sums_def by (simp add: tendsto_const)
    83 
    84 lemma summable_zero[simp, intro]: "summable (\<lambda>n. 0)"
    85   by (rule sums_zero [THEN sums_summable])
    86 
    87 lemma sums_group: "f sums s \<Longrightarrow> 0 < k \<Longrightarrow> (\<lambda>n. setsum f {n * k ..< n * k + k}) sums s"
    88   apply (simp only: sums_def setsum_nat_group tendsto_def eventually_sequentially)
    89   apply safe
    90   apply (erule_tac x=S in allE)
    91   apply safe
    92   apply (rule_tac x="N" in exI, safe)
    93   apply (drule_tac x="n*k" in spec)
    94   apply (erule mp)
    95   apply (erule order_trans)
    96   apply simp
    97   done
    98 
    99 context
   100   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
   101 begin
   102 
   103 lemma summable_sums[intro]: "summable f \<Longrightarrow> f sums (suminf f)"
   104   by (simp add: summable_def sums_def suminf_def)
   105      (metis convergent_LIMSEQ_iff convergent_def lim_def)
   106 
   107 lemma summable_LIMSEQ: "summable f \<Longrightarrow> (\<lambda>n. \<Sum>i<n. f i) ----> suminf f"
   108   by (rule summable_sums [unfolded sums_def])
   109 
   110 lemma sums_unique: "f sums s \<Longrightarrow> s = suminf f"
   111   by (metis limI suminf_eq_lim sums_def)
   112 
   113 lemma sums_iff: "f sums x \<longleftrightarrow> summable f \<and> (suminf f = x)"
   114   by (metis summable_sums sums_summable sums_unique)
   115 
   116 lemma suminf_finite:
   117   assumes N: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
   118   shows "suminf f = (\<Sum>n\<in>N. f n)"
   119   using sums_finite[OF assms, THEN sums_unique] by simp
   120 
   121 end
   122 
   123 lemma suminf_zero[simp]: "suminf (\<lambda>n. 0::'a::{t2_space, comm_monoid_add}) = 0"
   124   by (rule sums_zero [THEN sums_unique, symmetric])
   125 
   126 context
   127   fixes f :: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology}"
   128 begin
   129 
   130 lemma series_pos_le: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 \<le> f m \<Longrightarrow> setsum f {..<n} \<le> suminf f"
   131   apply (rule LIMSEQ_le_const[OF summable_LIMSEQ])
   132   apply assumption
   133   apply (intro exI[of _ n])
   134   apply (auto intro!: setsum_mono2 simp: not_le[symmetric])
   135   done
   136 
   137 lemma suminf_eq_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> suminf f = 0 \<longleftrightarrow> (\<forall>n. f n = 0)"
   138 proof
   139   assume "summable f" "suminf f = 0" and pos: "\<forall>n. 0 \<le> f n"
   140   then have "f sums 0"
   141     by (simp add: sums_iff)
   142   then have f: "(\<lambda>n. \<Sum>i<n. f i) ----> 0"
   143     by (simp add: sums_def atLeast0LessThan)
   144   have "\<And>i. (\<Sum>n\<in>{i}. f n) \<le> 0"
   145   proof (rule LIMSEQ_le_const[OF f])
   146     fix i show "\<exists>N. \<forall>n\<ge>N. (\<Sum>n\<in>{i}. f n) \<le> setsum f {..<n}"
   147       using pos by (intro exI[of _ "Suc i"] allI impI setsum_mono2) auto
   148   qed
   149   with pos show "\<forall>n. f n = 0"
   150     by (auto intro!: antisym)
   151 qed (metis suminf_zero fun_eq_iff)
   152 
   153 lemma suminf_gt_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 < suminf f \<longleftrightarrow> (\<exists>i. 0 < f i)"
   154   using series_pos_le[of 0] suminf_eq_zero_iff by (simp add: less_le)
   155 
   156 lemma suminf_gt_zero: "summable f \<Longrightarrow> \<forall>n. 0 < f n \<Longrightarrow> 0 < suminf f"
   157   using suminf_gt_zero_iff by (simp add: less_imp_le)
   158 
   159 lemma suminf_ge_zero: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 \<le> suminf f"
   160   by (drule_tac n="0" in series_pos_le) simp_all
   161 
   162 lemma suminf_le: "summable f \<Longrightarrow> (\<And>n. setsum f {..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
   163   by (metis LIMSEQ_le_const2 summable_LIMSEQ)
   164 
   165 lemma summable_le: "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
   166   by (rule LIMSEQ_le) (auto intro: setsum_mono summable_LIMSEQ)
   167 
   168 end
   169 
   170 lemma series_pos_less:
   171   fixes f :: "nat \<Rightarrow> 'a::{ordered_ab_semigroup_add_imp_le, ordered_comm_monoid_add, linorder_topology}"
   172   shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 < f m\<rbrakk> \<Longrightarrow> setsum f {..<n} < suminf f"
   173   apply simp
   174   apply (rule_tac y="setsum f {..<Suc n}" in order_less_le_trans)
   175   using add_less_cancel_left [of "setsum f {..<n}" 0 "f n"]
   176   apply simp
   177   apply (erule series_pos_le)
   178   apply (simp add: order_less_imp_le)
   179   done
   180 
   181 lemma sums_Suc_iff:
   182   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   183   shows "(\<lambda>n. f (Suc n)) sums s \<longleftrightarrow> f sums (s + f 0)"
   184 proof -
   185   have "f sums (s + f 0) \<longleftrightarrow> (\<lambda>i. \<Sum>j<Suc i. f j) ----> s + f 0"
   186     by (subst LIMSEQ_Suc_iff) (simp add: sums_def)
   187   also have "\<dots> \<longleftrightarrow> (\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) ----> s + f 0"
   188     by (simp add: ac_simps setsum_reindex image_iff lessThan_Suc_eq_insert_0)
   189   also have "\<dots> \<longleftrightarrow> (\<lambda>n. f (Suc n)) sums s"
   190   proof
   191     assume "(\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) ----> s + f 0"
   192     with tendsto_add[OF this tendsto_const, of "- f 0"]
   193     show "(\<lambda>i. f (Suc i)) sums s"
   194       by (simp add: sums_def)
   195   qed (auto intro: tendsto_add tendsto_const simp: sums_def)
   196   finally show ?thesis ..
   197 qed
   198 
   199 context
   200   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   201 begin
   202 
   203 lemma sums_add: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n + g n) sums (a + b)"
   204   unfolding sums_def by (simp add: setsum_addf tendsto_add)
   205 
   206 lemma summable_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n + g n)"
   207   unfolding summable_def by (auto intro: sums_add)
   208 
   209 lemma suminf_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f + suminf g = (\<Sum>n. f n + g n)"
   210   by (intro sums_unique sums_add summable_sums)
   211 
   212 lemma sums_diff: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n - g n) sums (a - b)"
   213   unfolding sums_def by (simp add: setsum_subtractf tendsto_diff)
   214 
   215 lemma summable_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n - g n)"
   216   unfolding summable_def by (auto intro: sums_diff)
   217 
   218 lemma suminf_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f - suminf g = (\<Sum>n. f n - g n)"
   219   by (intro sums_unique sums_diff summable_sums)
   220 
   221 lemma sums_minus: "f sums a \<Longrightarrow> (\<lambda>n. - f n) sums (- a)"
   222   unfolding sums_def by (simp add: setsum_negf tendsto_minus)
   223 
   224 lemma summable_minus: "summable f \<Longrightarrow> summable (\<lambda>n. - f n)"
   225   unfolding summable_def by (auto intro: sums_minus)
   226 
   227 lemma suminf_minus: "summable f \<Longrightarrow> (\<Sum>n. - f n) = - (\<Sum>n. f n)"
   228   by (intro sums_unique [symmetric] sums_minus summable_sums)
   229 
   230 lemma sums_Suc: "(\<lambda> n. f (Suc n)) sums l \<Longrightarrow> f sums (l + f 0)"
   231   by (simp add: sums_Suc_iff)
   232 
   233 lemma sums_iff_shift: "(\<lambda>i. f (i + n)) sums s \<longleftrightarrow> f sums (s + (\<Sum>i<n. f i))"
   234 proof (induct n arbitrary: s)
   235   case (Suc n)
   236   moreover have "(\<lambda>i. f (Suc i + n)) sums s \<longleftrightarrow> (\<lambda>i. f (i + n)) sums (s + f n)"
   237     by (subst sums_Suc_iff) simp
   238   ultimately show ?case
   239     by (simp add: ac_simps)
   240 qed simp
   241 
   242 lemma summable_iff_shift: "summable (\<lambda>n. f (n + k)) \<longleftrightarrow> summable f"
   243   by (metis diff_add_cancel summable_def sums_iff_shift[abs_def])
   244 
   245 lemma sums_split_initial_segment: "f sums s \<Longrightarrow> (\<lambda>i. f (i + n)) sums (s - (\<Sum>i<n. f i))"
   246   by (simp add: sums_iff_shift)
   247 
   248 lemma summable_ignore_initial_segment: "summable f \<Longrightarrow> summable (\<lambda>n. f(n + k))"
   249   by (simp add: summable_iff_shift)
   250 
   251 lemma suminf_minus_initial_segment: "summable f \<Longrightarrow> (\<Sum>n. f (n + k)) = (\<Sum>n. f n) - (\<Sum>i<k. f i)"
   252   by (rule sums_unique[symmetric]) (auto simp: sums_iff_shift)
   253 
   254 lemma suminf_split_initial_segment: "summable f \<Longrightarrow> suminf f = (\<Sum>n. f(n + k)) + (\<Sum>i<k. f i)"
   255   by (auto simp add: suminf_minus_initial_segment)
   256 
   257 lemma suminf_exist_split: 
   258   fixes r :: real assumes "0 < r" and "summable f"
   259   shows "\<exists>N. \<forall>n\<ge>N. norm (\<Sum>i. f (i + n)) < r"
   260 proof -
   261   from LIMSEQ_D[OF summable_LIMSEQ[OF `summable f`] `0 < r`]
   262   obtain N :: nat where "\<forall> n \<ge> N. norm (setsum f {..<n} - suminf f) < r" by auto
   263   thus ?thesis
   264     by (auto simp: norm_minus_commute suminf_minus_initial_segment[OF `summable f`])
   265 qed
   266 
   267 lemma summable_LIMSEQ_zero: "summable f \<Longrightarrow> f ----> 0"
   268   apply (drule summable_iff_convergent [THEN iffD1])
   269   apply (drule convergent_Cauchy)
   270   apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
   271   apply (drule_tac x="r" in spec, safe)
   272   apply (rule_tac x="M" in exI, safe)
   273   apply (drule_tac x="Suc n" in spec, simp)
   274   apply (drule_tac x="n" in spec, simp)
   275   done
   276 
   277 end
   278 
   279 lemma (in bounded_linear) sums: "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
   280   unfolding sums_def by (drule tendsto, simp only: setsum)
   281 
   282 lemma (in bounded_linear) summable: "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
   283   unfolding summable_def by (auto intro: sums)
   284 
   285 lemma (in bounded_linear) suminf: "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
   286   by (intro sums_unique sums summable_sums)
   287 
   288 lemmas sums_of_real = bounded_linear.sums [OF bounded_linear_of_real]
   289 lemmas summable_of_real = bounded_linear.summable [OF bounded_linear_of_real]
   290 lemmas suminf_of_real = bounded_linear.suminf [OF bounded_linear_of_real]
   291 
   292 context
   293   fixes f :: "nat \<Rightarrow> 'a::real_normed_algebra"
   294 begin
   295 
   296 lemma sums_mult: "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
   297   by (rule bounded_linear.sums [OF bounded_linear_mult_right])
   298 
   299 lemma summable_mult: "summable f \<Longrightarrow> summable (\<lambda>n. c * f n)"
   300   by (rule bounded_linear.summable [OF bounded_linear_mult_right])
   301 
   302 lemma suminf_mult: "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f"
   303   by (rule bounded_linear.suminf [OF bounded_linear_mult_right, symmetric])
   304 
   305 lemma sums_mult2: "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
   306   by (rule bounded_linear.sums [OF bounded_linear_mult_left])
   307 
   308 lemma summable_mult2: "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
   309   by (rule bounded_linear.summable [OF bounded_linear_mult_left])
   310 
   311 lemma suminf_mult2: "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
   312   by (rule bounded_linear.suminf [OF bounded_linear_mult_left])
   313 
   314 end
   315 
   316 context
   317   fixes c :: "'a::real_normed_field"
   318 begin
   319 
   320 lemma sums_divide: "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
   321   by (rule bounded_linear.sums [OF bounded_linear_divide])
   322 
   323 lemma summable_divide: "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
   324   by (rule bounded_linear.summable [OF bounded_linear_divide])
   325 
   326 lemma suminf_divide: "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
   327   by (rule bounded_linear.suminf [OF bounded_linear_divide, symmetric])
   328 
   329 text{*Sum of a geometric progression.*}
   330 
   331 lemma geometric_sums: "norm c < 1 \<Longrightarrow> (\<lambda>n. c^n) sums (1 / (1 - c))"
   332 proof -
   333   assume less_1: "norm c < 1"
   334   hence neq_1: "c \<noteq> 1" by auto
   335   hence neq_0: "c - 1 \<noteq> 0" by simp
   336   from less_1 have lim_0: "(\<lambda>n. c^n) ----> 0"
   337     by (rule LIMSEQ_power_zero)
   338   hence "(\<lambda>n. c ^ n / (c - 1) - 1 / (c - 1)) ----> 0 / (c - 1) - 1 / (c - 1)"
   339     using neq_0 by (intro tendsto_intros)
   340   hence "(\<lambda>n. (c ^ n - 1) / (c - 1)) ----> 1 / (1 - c)"
   341     by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
   342   thus "(\<lambda>n. c ^ n) sums (1 / (1 - c))"
   343     by (simp add: sums_def geometric_sum neq_1)
   344 qed
   345 
   346 lemma summable_geometric: "norm c < 1 \<Longrightarrow> summable (\<lambda>n. c^n)"
   347   by (rule geometric_sums [THEN sums_summable])
   348 
   349 lemma suminf_geometric: "norm c < 1 \<Longrightarrow> suminf (\<lambda>n. c^n) = 1 / (1 - c)"
   350   by (rule sums_unique[symmetric]) (rule geometric_sums)
   351 
   352 end
   353 
   354 lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
   355 proof -
   356   have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
   357     by auto
   358   have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
   359     by simp
   360   thus ?thesis using sums_divide [OF 2, of 2]
   361     by simp
   362 qed
   363 
   364 text{*Cauchy-type criterion for convergence of series (c.f. Harrison)*}
   365 
   366 lemma summable_Cauchy:
   367   fixes f :: "nat \<Rightarrow> 'a::banach"
   368   shows "summable f \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n. norm (setsum f {m..<n}) < e)"
   369   apply (simp only: summable_iff_convergent Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
   370   apply (drule spec, drule (1) mp)
   371   apply (erule exE, rule_tac x="M" in exI, clarify)
   372   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   373   apply (frule (1) order_trans)
   374   apply (drule_tac x="n" in spec, drule (1) mp)
   375   apply (drule_tac x="m" in spec, drule (1) mp)
   376   apply (simp_all add: setsum_diff [symmetric])
   377   apply (drule spec, drule (1) mp)
   378   apply (erule exE, rule_tac x="N" in exI, clarify)
   379   apply (rule_tac x="m" and y="n" in linorder_le_cases)
   380   apply (subst norm_minus_commute)
   381   apply (simp_all add: setsum_diff [symmetric])
   382   done
   383 
   384 context
   385   fixes f :: "nat \<Rightarrow> 'a::banach"
   386 begin  
   387 
   388 text{*Absolute convergence imples normal convergence*}
   389 
   390 lemma summable_norm_cancel: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
   391   apply (simp only: summable_Cauchy, safe)
   392   apply (drule_tac x="e" in spec, safe)
   393   apply (rule_tac x="N" in exI, safe)
   394   apply (drule_tac x="m" in spec, safe)
   395   apply (rule order_le_less_trans [OF norm_setsum])
   396   apply (rule order_le_less_trans [OF abs_ge_self])
   397   apply simp
   398   done
   399 
   400 lemma summable_norm: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
   401   by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel summable_LIMSEQ norm_setsum)
   402 
   403 text {* Comparison tests *}
   404 
   405 lemma summable_comparison_test: "\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n \<Longrightarrow> summable g \<Longrightarrow> summable f"
   406   apply (simp add: summable_Cauchy, safe)
   407   apply (drule_tac x="e" in spec, safe)
   408   apply (rule_tac x = "N + Na" in exI, safe)
   409   apply (rotate_tac 2)
   410   apply (drule_tac x = m in spec)
   411   apply (auto, rotate_tac 2, drule_tac x = n in spec)
   412   apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
   413   apply (rule norm_setsum)
   414   apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
   415   apply (auto intro: setsum_mono simp add: abs_less_iff)
   416   done
   417 
   418 subsection {* The Ratio Test*}
   419 
   420 lemma summable_ratio_test: 
   421   assumes "c < 1" "\<And>n. n \<ge> N \<Longrightarrow> norm (f (Suc n)) \<le> c * norm (f n)"
   422   shows "summable f"
   423 proof cases
   424   assume "0 < c"
   425   show "summable f"
   426   proof (rule summable_comparison_test)
   427     show "\<exists>N'. \<forall>n\<ge>N'. norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   428     proof (intro exI allI impI)
   429       fix n assume "N \<le> n" then show "norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
   430       proof (induct rule: inc_induct)
   431         case (step m)
   432         moreover have "norm (f (Suc m)) / c ^ Suc m * c ^ n \<le> norm (f m) / c ^ m * c ^ n"
   433           using `0 < c` `c < 1` assms(2)[OF `N \<le> m`] by (simp add: field_simps)
   434         ultimately show ?case by simp
   435       qed (insert `0 < c`, simp)
   436     qed
   437     show "summable (\<lambda>n. norm (f N) / c ^ N * c ^ n)"
   438       using `0 < c` `c < 1` by (intro summable_mult summable_geometric) simp
   439   qed
   440 next
   441   assume c: "\<not> 0 < c"
   442   { fix n assume "n \<ge> N"
   443     then have "norm (f (Suc n)) \<le> c * norm (f n)"
   444       by fact
   445     also have "\<dots> \<le> 0"
   446       using c by (simp add: not_less mult_nonpos_nonneg)
   447     finally have "f (Suc n) = 0"
   448       by auto }
   449   then show "summable f"
   450     by (intro sums_summable[OF sums_finite, of "{.. Suc N}"]) (auto simp: not_le Suc_less_eq2)
   451 qed
   452 
   453 end
   454 
   455 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))"
   456   by (rule summable_comparison_test) auto
   457 
   458 lemma summable_rabs_cancel: "summable (\<lambda>n. \<bar>f n :: real\<bar>) \<Longrightarrow> summable f"
   459   by (rule summable_norm_cancel) simp
   460 
   461 text{*Summability of geometric series for real algebras*}
   462 
   463 lemma complete_algebra_summable_geometric:
   464   fixes x :: "'a::{real_normed_algebra_1,banach}"
   465   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   466 proof (rule summable_comparison_test)
   467   show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
   468     by (simp add: norm_power_ineq)
   469   show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
   470     by (simp add: summable_geometric)
   471 qed
   472 
   473 
   474 text{*A summable series of positive terms has limit that is at least as
   475 great as any partial sum.*}
   476 
   477 lemma pos_summable:
   478   fixes f:: "nat \<Rightarrow> real"
   479   assumes pos: "\<And>n. 0 \<le> f n" and le: "\<And>n. setsum f {..<n} \<le> x"
   480   shows "summable f"
   481 proof -
   482   have "convergent (\<lambda>n. setsum f {..<n})"
   483   proof (rule Bseq_mono_convergent)
   484     show "Bseq (\<lambda>n. setsum f {..<n})"
   485       by (intro BseqI'[of _ x]) (auto simp add: setsum_nonneg pos intro: le)
   486   qed (auto intro: setsum_mono2 pos)
   487   thus ?thesis
   488     by (force simp add: summable_def sums_def convergent_def)
   489 qed
   490 
   491 lemma summable_rabs_comparison_test:
   492   fixes f :: "nat \<Rightarrow> real"
   493   shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. \<bar>f n\<bar> \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable (\<lambda>n. \<bar>f n\<bar>)"
   494   by (rule summable_comparison_test) auto
   495 
   496 lemma summable_rabs:
   497   fixes f :: "nat \<Rightarrow> real"
   498   shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
   499 by (fold real_norm_def, rule summable_norm)
   500 
   501 subsection {* Cauchy Product Formula *}
   502 
   503 text {*
   504   Proof based on Analysis WebNotes: Chapter 07, Class 41
   505   @{url "http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm"}
   506 *}
   507 
   508 lemma setsum_triangle_reindex:
   509   fixes n :: nat
   510   shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k<n. \<Sum>i=0..k. f i (k - i))"
   511 proof -
   512   have "(\<Sum>(i, j)\<in>{(i, j). i + j < n}. f i j) =
   513     (\<Sum>(k, i)\<in>(SIGMA k:{..<n}. {0..k}). f i (k - i))"
   514   proof (rule setsum_reindex_cong)
   515     show "inj_on (\<lambda>(k,i). (i, k - i)) (SIGMA k:{..<n}. {0..k})"
   516       by (rule inj_on_inverseI [where g="\<lambda>(i,j). (i+j, i)"], auto)
   517     show "{(i,j). i + j < n} = (\<lambda>(k,i). (i, k - i)) ` (SIGMA k:{..<n}. {0..k})"
   518       by (safe, rule_tac x="(a+b,a)" in image_eqI, auto)
   519     show "\<And>a. (\<lambda>(k, i). f i (k - i)) a = split f ((\<lambda>(k, i). (i, k - i)) a)"
   520       by clarify
   521   qed
   522   thus ?thesis by (simp add: setsum_Sigma)
   523 qed
   524 
   525 lemma Cauchy_product_sums:
   526   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   527   assumes a: "summable (\<lambda>k. norm (a k))"
   528   assumes b: "summable (\<lambda>k. norm (b k))"
   529   shows "(\<lambda>k. \<Sum>i=0..k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
   530 proof -
   531   let ?S1 = "\<lambda>n::nat. {..<n} \<times> {..<n}"
   532   let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
   533   have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
   534   have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
   535   have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
   536   have finite_S1: "\<And>n. finite (?S1 n)" by simp
   537   with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
   538 
   539   let ?g = "\<lambda>(i,j). a i * b j"
   540   let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
   541   have f_nonneg: "\<And>x. 0 \<le> ?f x"
   542     by (auto simp add: mult_nonneg_nonneg)
   543   hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
   544     unfolding real_norm_def
   545     by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
   546 
   547   have "(\<lambda>n. (\<Sum>k<n. a k) * (\<Sum>k<n. b k)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   548     by (intro tendsto_mult summable_LIMSEQ summable_norm_cancel [OF a] summable_norm_cancel [OF b])
   549   hence 1: "(\<lambda>n. setsum ?g (?S1 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   550     by (simp only: setsum_product setsum_Sigma [rule_format] finite_lessThan)
   551 
   552   have "(\<lambda>n. (\<Sum>k<n. norm (a k)) * (\<Sum>k<n. norm (b k))) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   553     using a b by (intro tendsto_mult summable_LIMSEQ)
   554   hence "(\<lambda>n. setsum ?f (?S1 n)) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   555     by (simp only: setsum_product setsum_Sigma [rule_format] finite_lessThan)
   556   hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
   557     by (rule convergentI)
   558   hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
   559     by (rule convergent_Cauchy)
   560   have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
   561   proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
   562     fix r :: real
   563     assume r: "0 < r"
   564     from CauchyD [OF Cauchy r] obtain N
   565     where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
   566     hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
   567       by (simp only: setsum_diff finite_S1 S1_mono)
   568     hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
   569       by (simp only: norm_setsum_f)
   570     show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
   571     proof (intro exI allI impI)
   572       fix n assume "2 * N \<le> n"
   573       hence n: "N \<le> n div 2" by simp
   574       have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
   575         by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
   576                   Diff_mono subset_refl S1_le_S2)
   577       also have "\<dots> < r"
   578         using n div_le_dividend by (rule N)
   579       finally show "setsum ?f (?S1 n - ?S2 n) < r" .
   580     qed
   581   qed
   582   hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
   583     apply (rule Zfun_le [rule_format])
   584     apply (simp only: norm_setsum_f)
   585     apply (rule order_trans [OF norm_setsum setsum_mono])
   586     apply (auto simp add: norm_mult_ineq)
   587     done
   588   hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) ----> 0"
   589     unfolding tendsto_Zfun_iff diff_0_right
   590     by (simp only: setsum_diff finite_S1 S2_le_S1)
   591 
   592   with 1 have "(\<lambda>n. setsum ?g (?S2 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   593     by (rule LIMSEQ_diff_approach_zero2)
   594   thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
   595 qed
   596 
   597 lemma Cauchy_product:
   598   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   599   assumes a: "summable (\<lambda>k. norm (a k))"
   600   assumes b: "summable (\<lambda>k. norm (b k))"
   601   shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i=0..k. a i * b (k - i))"
   602 using a b
   603 by (rule Cauchy_product_sums [THEN sums_unique])
   604 
   605 end