src/HOL/Series.thy
author huffman
Mon Sep 05 16:07:40 2011 -0700 (2011-09-05)
changeset 44726 8478eab380e9
parent 44710 9caf6883f1f4
child 44727 d45acd50a894
permissions -rw-r--r--
generalize some 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 SEQ Deriv
    14 begin
    15 
    16 definition
    17    sums  :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a \<Rightarrow> bool"
    18      (infixr "sums" 80) where
    19    "f sums s = (%n. setsum f {0..<n}) ----> s"
    20 
    21 definition
    22    summable :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> bool" where
    23    "summable f = (\<exists>s. f sums s)"
    24 
    25 definition
    26    suminf   :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a" where
    27    "suminf f = (THE s. f sums s)"
    28 
    29 notation suminf (binder "\<Sum>" 10)
    30 
    31 
    32 lemma [trans]: "f=g ==> g sums z ==> f sums z"
    33   by simp
    34 
    35 lemma sumr_diff_mult_const:
    36  "setsum f {0..<n} - (real n*r) = setsum (%i. f i - r) {0..<n::nat}"
    37 by (simp add: diff_minus setsum_addf real_of_nat_def)
    38 
    39 lemma real_setsum_nat_ivl_bounded:
    40      "(!!p. p < n \<Longrightarrow> f(p) \<le> K)
    41       \<Longrightarrow> setsum f {0..<n::nat} \<le> real n * K"
    42 using setsum_bounded[where A = "{0..<n}"]
    43 by (auto simp:real_of_nat_def)
    44 
    45 (* Generalize from real to some algebraic structure? *)
    46 lemma sumr_minus_one_realpow_zero [simp]:
    47   "(\<Sum>i=0..<2*n. (-1) ^ Suc i) = (0::real)"
    48 by (induct "n", auto)
    49 
    50 (* FIXME this is an awful lemma! *)
    51 lemma sumr_one_lb_realpow_zero [simp]:
    52   "(\<Sum>n=Suc 0..<n. f(n) * (0::real) ^ n) = 0"
    53 by (rule setsum_0', simp)
    54 
    55 lemma sumr_group:
    56      "(\<Sum>m=0..<n::nat. setsum f {m * k ..< m*k + k}) = setsum f {0 ..< n * k}"
    57 apply (subgoal_tac "k = 0 | 0 < k", auto)
    58 apply (induct "n")
    59 apply (simp_all add: setsum_add_nat_ivl add_commute)
    60 done
    61 
    62 lemma sumr_offset3:
    63   "setsum f {0::nat..<n+k} = (\<Sum>m=0..<n. f (m+k)) + setsum f {0..<k}"
    64 apply (subst setsum_shift_bounds_nat_ivl [symmetric])
    65 apply (simp add: setsum_add_nat_ivl add_commute)
    66 done
    67 
    68 lemma sumr_offset:
    69   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
    70   shows "(\<Sum>m=0..<n. f(m+k)) = setsum f {0..<n+k} - setsum f {0..<k}"
    71 by (simp add: sumr_offset3)
    72 
    73 lemma sumr_offset2:
    74  "\<forall>f. (\<Sum>m=0..<n::nat. f(m+k)::real) = setsum f {0..<n+k} - setsum f {0..<k}"
    75 by (simp add: sumr_offset)
    76 
    77 lemma sumr_offset4:
    78   "\<forall>n f. setsum f {0::nat..<n+k} = (\<Sum>m=0..<n. f (m+k)::real) + setsum f {0..<k}"
    79 by (clarify, rule sumr_offset3)
    80 
    81 subsection{* Infinite Sums, by the Properties of Limits*}
    82 
    83 (*----------------------
    84    suminf is the sum
    85  ---------------------*)
    86 lemma sums_summable: "f sums l ==> summable f"
    87   by (simp add: sums_def summable_def, blast)
    88 
    89 lemma summable_sums:
    90   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}" assumes "summable f" shows "f sums (suminf f)"
    91 proof -
    92   from assms guess s unfolding summable_def sums_def_raw .. note s = this
    93   then show ?thesis unfolding sums_def_raw suminf_def
    94     by (rule theI, auto intro!: tendsto_unique[OF trivial_limit_sequentially])
    95 qed
    96 
    97 lemma summable_sumr_LIMSEQ_suminf:
    98   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
    99   shows "summable f \<Longrightarrow> (\<lambda>n. setsum f {0..<n}) ----> suminf f"
   100 by (rule summable_sums [unfolded sums_def])
   101 
   102 lemma suminf_eq_lim: "suminf f = lim (%n. setsum f {0..<n})"
   103   by (simp add: suminf_def sums_def lim_def)
   104 
   105 (*-------------------
   106     sum is unique
   107  ------------------*)
   108 lemma sums_unique:
   109   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
   110   shows "f sums s \<Longrightarrow> (s = suminf f)"
   111 apply (frule sums_summable[THEN summable_sums])
   112 apply (auto intro!: tendsto_unique[OF trivial_limit_sequentially] simp add: sums_def)
   113 done
   114 
   115 lemma sums_iff:
   116   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
   117   shows "f sums x \<longleftrightarrow> summable f \<and> (suminf f = x)"
   118   by (metis summable_sums sums_summable sums_unique)
   119 
   120 lemma sums_split_initial_segment:
   121   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   122   shows "f sums s ==> (\<lambda>n. f(n + k)) sums (s - (SUM i = 0..< k. f i))"
   123   apply (unfold sums_def)
   124   apply (simp add: sumr_offset)
   125   apply (rule tendsto_diff [OF _ tendsto_const])
   126   apply (rule LIMSEQ_ignore_initial_segment)
   127   apply assumption
   128 done
   129 
   130 lemma summable_ignore_initial_segment:
   131   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   132   shows "summable f ==> summable (%n. f(n + k))"
   133   apply (unfold summable_def)
   134   apply (auto intro: sums_split_initial_segment)
   135 done
   136 
   137 lemma suminf_minus_initial_segment:
   138   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   139   shows "summable f ==>
   140     suminf f = s ==> suminf (%n. f(n + k)) = s - (SUM i = 0..< k. f i)"
   141   apply (frule summable_ignore_initial_segment)
   142   apply (rule sums_unique [THEN sym])
   143   apply (frule summable_sums)
   144   apply (rule sums_split_initial_segment)
   145   apply auto
   146 done
   147 
   148 lemma suminf_split_initial_segment:
   149   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   150   shows "summable f ==>
   151     suminf f = (SUM i = 0..< k. f i) + (\<Sum>n. f(n + k))"
   152 by (auto simp add: suminf_minus_initial_segment)
   153 
   154 lemma suminf_exist_split: fixes r :: real assumes "0 < r" and "summable a"
   155   shows "\<exists> N. \<forall> n \<ge> N. \<bar> \<Sum> i. a (i + n) \<bar> < r"
   156 proof -
   157   from LIMSEQ_D[OF summable_sumr_LIMSEQ_suminf[OF `summable a`] `0 < r`]
   158   obtain N :: nat where "\<forall> n \<ge> N. norm (setsum a {0..<n} - suminf a) < r" by auto
   159   thus ?thesis unfolding suminf_minus_initial_segment[OF `summable a` refl] abs_minus_commute real_norm_def
   160     by auto
   161 qed
   162 
   163 lemma sums_Suc:
   164   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   165   assumes sumSuc: "(\<lambda> n. f (Suc n)) sums l" shows "f sums (l + f 0)"
   166 proof -
   167   from sumSuc[unfolded sums_def]
   168   have "(\<lambda>i. \<Sum>n = Suc 0..<Suc i. f n) ----> l" unfolding setsum_reindex[OF inj_Suc] image_Suc_atLeastLessThan[symmetric] comp_def .
   169   from tendsto_add[OF this tendsto_const, where b="f 0"]
   170   have "(\<lambda>i. \<Sum>n = 0..<Suc i. f n) ----> l + f 0" unfolding add_commute setsum_head_upt_Suc[OF zero_less_Suc] .
   171   thus ?thesis unfolding sums_def by (rule LIMSEQ_imp_Suc)
   172 qed
   173 
   174 lemma series_zero:
   175   fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
   176   assumes "\<forall>m. n \<le> m \<longrightarrow> f m = 0"
   177   shows "f sums (setsum f {0..<n})"
   178 proof -
   179   { fix k :: nat have "setsum f {0..<k + n} = setsum f {0..<n}"
   180       using assms by (induct k) auto }
   181   note setsum_const = this
   182   show ?thesis
   183     unfolding sums_def
   184     apply (rule LIMSEQ_offset[of _ n])
   185     unfolding setsum_const
   186     apply (rule tendsto_const)
   187     done
   188 qed
   189 
   190 lemma sums_zero[simp, intro]: "(\<lambda>n. 0) sums 0"
   191   unfolding sums_def by (simp add: tendsto_const)
   192 
   193 lemma summable_zero[simp, intro]: "summable (\<lambda>n. 0)"
   194 by (rule sums_zero [THEN sums_summable])
   195 
   196 lemma suminf_zero[simp]: "suminf (\<lambda>n. 0::'a::{t2_space, comm_monoid_add}) = 0"
   197 by (rule sums_zero [THEN sums_unique, symmetric])
   198 
   199 lemma (in bounded_linear) sums:
   200   "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
   201   unfolding sums_def by (drule tendsto, simp only: setsum)
   202 
   203 lemma (in bounded_linear) summable:
   204   "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
   205 unfolding summable_def by (auto intro: sums)
   206 
   207 lemma (in bounded_linear) suminf:
   208   "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
   209 by (intro sums_unique sums summable_sums)
   210 
   211 lemmas sums_of_real = bounded_linear.sums [OF bounded_linear_of_real]
   212 lemmas summable_of_real = bounded_linear.summable [OF bounded_linear_of_real]
   213 lemmas suminf_of_real = bounded_linear.suminf [OF bounded_linear_of_real]
   214 
   215 lemma sums_mult:
   216   fixes c :: "'a::real_normed_algebra"
   217   shows "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
   218   by (rule bounded_linear.sums [OF bounded_linear_mult_right])
   219 
   220 lemma summable_mult:
   221   fixes c :: "'a::real_normed_algebra"
   222   shows "summable f \<Longrightarrow> summable (%n. c * f n)"
   223   by (rule bounded_linear.summable [OF bounded_linear_mult_right])
   224 
   225 lemma suminf_mult:
   226   fixes c :: "'a::real_normed_algebra"
   227   shows "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f"
   228   by (rule bounded_linear.suminf [OF bounded_linear_mult_right, symmetric])
   229 
   230 lemma sums_mult2:
   231   fixes c :: "'a::real_normed_algebra"
   232   shows "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
   233   by (rule bounded_linear.sums [OF bounded_linear_mult_left])
   234 
   235 lemma summable_mult2:
   236   fixes c :: "'a::real_normed_algebra"
   237   shows "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
   238   by (rule bounded_linear.summable [OF bounded_linear_mult_left])
   239 
   240 lemma suminf_mult2:
   241   fixes c :: "'a::real_normed_algebra"
   242   shows "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
   243   by (rule bounded_linear.suminf [OF bounded_linear_mult_left])
   244 
   245 lemma sums_divide:
   246   fixes c :: "'a::real_normed_field"
   247   shows "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
   248   by (rule bounded_linear.sums [OF bounded_linear_divide])
   249 
   250 lemma summable_divide:
   251   fixes c :: "'a::real_normed_field"
   252   shows "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
   253   by (rule bounded_linear.summable [OF bounded_linear_divide])
   254 
   255 lemma suminf_divide:
   256   fixes c :: "'a::real_normed_field"
   257   shows "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
   258   by (rule bounded_linear.suminf [OF bounded_linear_divide, symmetric])
   259 
   260 lemma sums_add:
   261   fixes a b :: "'a::real_normed_field"
   262   shows "\<lbrakk>X sums a; Y sums b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n + Y n) sums (a + b)"
   263   unfolding sums_def by (simp add: setsum_addf tendsto_add)
   264 
   265 lemma summable_add:
   266   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
   267   shows "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> summable (\<lambda>n. X n + Y n)"
   268 unfolding summable_def by (auto intro: sums_add)
   269 
   270 lemma suminf_add:
   271   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
   272   shows "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> suminf X + suminf Y = (\<Sum>n. X n + Y n)"
   273 by (intro sums_unique sums_add summable_sums)
   274 
   275 lemma sums_diff:
   276   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
   277   shows "\<lbrakk>X sums a; Y sums b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n - Y n) sums (a - b)"
   278   unfolding sums_def by (simp add: setsum_subtractf tendsto_diff)
   279 
   280 lemma summable_diff:
   281   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
   282   shows "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> summable (\<lambda>n. X n - Y n)"
   283 unfolding summable_def by (auto intro: sums_diff)
   284 
   285 lemma suminf_diff:
   286   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
   287   shows "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> suminf X - suminf Y = (\<Sum>n. X n - Y n)"
   288 by (intro sums_unique sums_diff summable_sums)
   289 
   290 lemma sums_minus:
   291   fixes X :: "nat \<Rightarrow> 'a::real_normed_field"
   292   shows "X sums a ==> (\<lambda>n. - X n) sums (- a)"
   293   unfolding sums_def by (simp add: setsum_negf tendsto_minus)
   294 
   295 lemma summable_minus:
   296   fixes X :: "nat \<Rightarrow> 'a::real_normed_field"
   297   shows "summable X \<Longrightarrow> summable (\<lambda>n. - X n)"
   298 unfolding summable_def by (auto intro: sums_minus)
   299 
   300 lemma suminf_minus:
   301   fixes X :: "nat \<Rightarrow> 'a::real_normed_field"
   302   shows "summable X \<Longrightarrow> (\<Sum>n. - X n) = - (\<Sum>n. X n)"
   303 by (intro sums_unique [symmetric] sums_minus summable_sums)
   304 
   305 lemma sums_group:
   306   fixes f :: "nat \<Rightarrow> 'a::real_normed_field"
   307   shows "[|summable f; 0 < k |] ==> (%n. setsum f {n*k..<n*k+k}) sums (suminf f)"
   308 apply (drule summable_sums)
   309 apply (simp only: sums_def sumr_group)
   310 apply (unfold LIMSEQ_iff, safe)
   311 apply (drule_tac x="r" in spec, safe)
   312 apply (rule_tac x="no" in exI, safe)
   313 apply (drule_tac x="n*k" in spec)
   314 apply (erule mp)
   315 apply (erule order_trans)
   316 apply simp
   317 done
   318 
   319 text{*A summable series of positive terms has limit that is at least as
   320 great as any partial sum.*}
   321 
   322 lemma pos_summable:
   323   fixes f:: "nat \<Rightarrow> real"
   324   assumes pos: "!!n. 0 \<le> f n" and le: "!!n. setsum f {0..<n} \<le> x"
   325   shows "summable f"
   326 proof -
   327   have "convergent (\<lambda>n. setsum f {0..<n})"
   328     proof (rule Bseq_mono_convergent)
   329       show "Bseq (\<lambda>n. setsum f {0..<n})"
   330         by (rule f_inc_g_dec_Beq_f [of "(\<lambda>n. setsum f {0..<n})" "\<lambda>n. x"])
   331            (auto simp add: le pos)
   332     next
   333       show "\<forall>m n. m \<le> n \<longrightarrow> setsum f {0..<m} \<le> setsum f {0..<n}"
   334         by (auto intro: setsum_mono2 pos)
   335     qed
   336   then obtain L where "(%n. setsum f {0..<n}) ----> L"
   337     by (blast dest: convergentD)
   338   thus ?thesis
   339     by (force simp add: summable_def sums_def)
   340 qed
   341 
   342 lemma series_pos_le:
   343   fixes f :: "nat \<Rightarrow> real"
   344   shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 \<le> f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} \<le> suminf f"
   345 apply (drule summable_sums)
   346 apply (simp add: sums_def)
   347 apply (cut_tac k = "setsum f {0..<n}" in tendsto_const)
   348 apply (erule LIMSEQ_le, blast)
   349 apply (rule_tac x="n" in exI, clarify)
   350 apply (rule setsum_mono2)
   351 apply auto
   352 done
   353 
   354 lemma series_pos_less:
   355   fixes f :: "nat \<Rightarrow> real"
   356   shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 < f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} < suminf f"
   357 apply (rule_tac y="setsum f {0..<Suc n}" in order_less_le_trans)
   358 apply simp
   359 apply (erule series_pos_le)
   360 apply (simp add: order_less_imp_le)
   361 done
   362 
   363 lemma suminf_gt_zero:
   364   fixes f :: "nat \<Rightarrow> real"
   365   shows "\<lbrakk>summable f; \<forall>n. 0 < f n\<rbrakk> \<Longrightarrow> 0 < suminf f"
   366 by (drule_tac n="0" in series_pos_less, simp_all)
   367 
   368 lemma suminf_ge_zero:
   369   fixes f :: "nat \<Rightarrow> real"
   370   shows "\<lbrakk>summable f; \<forall>n. 0 \<le> f n\<rbrakk> \<Longrightarrow> 0 \<le> suminf f"
   371 by (drule_tac n="0" in series_pos_le, simp_all)
   372 
   373 lemma sumr_pos_lt_pair:
   374   fixes f :: "nat \<Rightarrow> real"
   375   shows "\<lbrakk>summable f;
   376         \<forall>d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc(Suc 0) * d) + 1))\<rbrakk>
   377       \<Longrightarrow> setsum f {0..<k} < suminf f"
   378 unfolding One_nat_def
   379 apply (subst suminf_split_initial_segment [where k="k"])
   380 apply assumption
   381 apply simp
   382 apply (drule_tac k="k" in summable_ignore_initial_segment)
   383 apply (drule_tac k="Suc (Suc 0)" in sums_group, simp)
   384 apply simp
   385 apply (frule sums_unique)
   386 apply (drule sums_summable)
   387 apply simp
   388 apply (erule suminf_gt_zero)
   389 apply (simp add: add_ac)
   390 done
   391 
   392 text{*Sum of a geometric progression.*}
   393 
   394 lemmas sumr_geometric = geometric_sum [where 'a = real]
   395 
   396 lemma geometric_sums:
   397   fixes x :: "'a::{real_normed_field}"
   398   shows "norm x < 1 \<Longrightarrow> (\<lambda>n. x ^ n) sums (1 / (1 - x))"
   399 proof -
   400   assume less_1: "norm x < 1"
   401   hence neq_1: "x \<noteq> 1" by auto
   402   hence neq_0: "x - 1 \<noteq> 0" by simp
   403   from less_1 have lim_0: "(\<lambda>n. x ^ n) ----> 0"
   404     by (rule LIMSEQ_power_zero)
   405   hence "(\<lambda>n. x ^ n / (x - 1) - 1 / (x - 1)) ----> 0 / (x - 1) - 1 / (x - 1)"
   406     using neq_0 by (intro tendsto_intros)
   407   hence "(\<lambda>n. (x ^ n - 1) / (x - 1)) ----> 1 / (1 - x)"
   408     by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
   409   thus "(\<lambda>n. x ^ n) sums (1 / (1 - x))"
   410     by (simp add: sums_def geometric_sum neq_1)
   411 qed
   412 
   413 lemma summable_geometric:
   414   fixes x :: "'a::{real_normed_field}"
   415   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   416 by (rule geometric_sums [THEN sums_summable])
   417 
   418 lemma half: "0 < 1 / (2::'a::{number_ring,linordered_field_inverse_zero})"
   419   by arith
   420 
   421 lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
   422 proof -
   423   have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
   424     by auto
   425   have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
   426     by simp
   427   thus ?thesis using sums_divide [OF 2, of 2]
   428     by simp
   429 qed
   430 
   431 text{*Cauchy-type criterion for convergence of series (c.f. Harrison)*}
   432 
   433 lemma summable_convergent_sumr_iff:
   434  "summable f = convergent (%n. setsum f {0..<n})"
   435 by (simp add: summable_def sums_def convergent_def)
   436 
   437 lemma summable_LIMSEQ_zero:
   438   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   439   shows "summable f \<Longrightarrow> f ----> 0"
   440 apply (drule summable_convergent_sumr_iff [THEN iffD1])
   441 apply (drule convergent_Cauchy)
   442 apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
   443 apply (drule_tac x="r" in spec, safe)
   444 apply (rule_tac x="M" in exI, safe)
   445 apply (drule_tac x="Suc n" in spec, simp)
   446 apply (drule_tac x="n" in spec, simp)
   447 done
   448 
   449 lemma suminf_le:
   450   fixes x :: real
   451   shows "summable f \<Longrightarrow> (!!n. setsum f {0..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
   452   by (simp add: summable_convergent_sumr_iff suminf_eq_lim lim_le)
   453 
   454 lemma summable_Cauchy:
   455      "summable (f::nat \<Rightarrow> 'a::banach) =
   456       (\<forall>e > 0. \<exists>N. \<forall>m \<ge> N. \<forall>n. norm (setsum f {m..<n}) < e)"
   457 apply (simp only: summable_convergent_sumr_iff Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
   458 apply (drule spec, drule (1) mp)
   459 apply (erule exE, rule_tac x="M" in exI, clarify)
   460 apply (rule_tac x="m" and y="n" in linorder_le_cases)
   461 apply (frule (1) order_trans)
   462 apply (drule_tac x="n" in spec, drule (1) mp)
   463 apply (drule_tac x="m" in spec, drule (1) mp)
   464 apply (simp add: setsum_diff [symmetric])
   465 apply simp
   466 apply (drule spec, drule (1) mp)
   467 apply (erule exE, rule_tac x="N" in exI, clarify)
   468 apply (rule_tac x="m" and y="n" in linorder_le_cases)
   469 apply (subst norm_minus_commute)
   470 apply (simp add: setsum_diff [symmetric])
   471 apply (simp add: setsum_diff [symmetric])
   472 done
   473 
   474 text{*Comparison test*}
   475 
   476 lemma norm_setsum:
   477   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   478   shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
   479 apply (case_tac "finite A")
   480 apply (erule finite_induct)
   481 apply simp
   482 apply simp
   483 apply (erule order_trans [OF norm_triangle_ineq add_left_mono])
   484 apply simp
   485 done
   486 
   487 lemma summable_comparison_test:
   488   fixes f :: "nat \<Rightarrow> 'a::banach"
   489   shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f"
   490 apply (simp add: summable_Cauchy, safe)
   491 apply (drule_tac x="e" in spec, safe)
   492 apply (rule_tac x = "N + Na" in exI, safe)
   493 apply (rotate_tac 2)
   494 apply (drule_tac x = m in spec)
   495 apply (auto, rotate_tac 2, drule_tac x = n in spec)
   496 apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
   497 apply (rule norm_setsum)
   498 apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
   499 apply (auto intro: setsum_mono simp add: abs_less_iff)
   500 done
   501 
   502 lemma summable_norm_comparison_test:
   503   fixes f :: "nat \<Rightarrow> 'a::banach"
   504   shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk>
   505          \<Longrightarrow> summable (\<lambda>n. norm (f n))"
   506 apply (rule summable_comparison_test)
   507 apply (auto)
   508 done
   509 
   510 lemma summable_rabs_comparison_test:
   511   fixes f :: "nat \<Rightarrow> real"
   512   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>)"
   513 apply (rule summable_comparison_test)
   514 apply (auto)
   515 done
   516 
   517 text{*Summability of geometric series for real algebras*}
   518 
   519 lemma complete_algebra_summable_geometric:
   520   fixes x :: "'a::{real_normed_algebra_1,banach}"
   521   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   522 proof (rule summable_comparison_test)
   523   show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
   524     by (simp add: norm_power_ineq)
   525   show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
   526     by (simp add: summable_geometric)
   527 qed
   528 
   529 text{*Limit comparison property for series (c.f. jrh)*}
   530 
   531 lemma summable_le:
   532   fixes f g :: "nat \<Rightarrow> real"
   533   shows "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
   534 apply (drule summable_sums)+
   535 apply (simp only: sums_def, erule (1) LIMSEQ_le)
   536 apply (rule exI)
   537 apply (auto intro!: setsum_mono)
   538 done
   539 
   540 lemma summable_le2:
   541   fixes f g :: "nat \<Rightarrow> real"
   542   shows "\<lbrakk>\<forall>n. \<bar>f n\<bar> \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f \<and> suminf f \<le> suminf g"
   543 apply (subgoal_tac "summable f")
   544 apply (auto intro!: summable_le)
   545 apply (simp add: abs_le_iff)
   546 apply (rule_tac g="g" in summable_comparison_test, simp_all)
   547 done
   548 
   549 (* specialisation for the common 0 case *)
   550 lemma suminf_0_le:
   551   fixes f::"nat\<Rightarrow>real"
   552   assumes gt0: "\<forall>n. 0 \<le> f n" and sm: "summable f"
   553   shows "0 \<le> suminf f"
   554 proof -
   555   let ?g = "(\<lambda>n. (0::real))"
   556   from gt0 have "\<forall>n. ?g n \<le> f n" by simp
   557   moreover have "summable ?g" by (rule summable_zero)
   558   moreover from sm have "summable f" .
   559   ultimately have "suminf ?g \<le> suminf f" by (rule summable_le)
   560   then show "0 \<le> suminf f" by simp
   561 qed
   562 
   563 
   564 text{*Absolute convergence imples normal convergence*}
   565 lemma summable_norm_cancel:
   566   fixes f :: "nat \<Rightarrow> 'a::banach"
   567   shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
   568 apply (simp only: summable_Cauchy, safe)
   569 apply (drule_tac x="e" in spec, safe)
   570 apply (rule_tac x="N" in exI, safe)
   571 apply (drule_tac x="m" in spec, safe)
   572 apply (rule order_le_less_trans [OF norm_setsum])
   573 apply (rule order_le_less_trans [OF abs_ge_self])
   574 apply simp
   575 done
   576 
   577 lemma summable_rabs_cancel:
   578   fixes f :: "nat \<Rightarrow> real"
   579   shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> summable f"
   580 by (rule summable_norm_cancel, simp)
   581 
   582 text{*Absolute convergence of series*}
   583 lemma summable_norm:
   584   fixes f :: "nat \<Rightarrow> 'a::banach"
   585   shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
   586   by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel
   587                 summable_sumr_LIMSEQ_suminf norm_setsum)
   588 
   589 lemma summable_rabs:
   590   fixes f :: "nat \<Rightarrow> real"
   591   shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
   592 by (fold real_norm_def, rule summable_norm)
   593 
   594 subsection{* The Ratio Test*}
   595 
   596 lemma norm_ratiotest_lemma:
   597   fixes x y :: "'a::real_normed_vector"
   598   shows "\<lbrakk>c \<le> 0; norm x \<le> c * norm y\<rbrakk> \<Longrightarrow> x = 0"
   599 apply (subgoal_tac "norm x \<le> 0", simp)
   600 apply (erule order_trans)
   601 apply (simp add: mult_le_0_iff)
   602 done
   603 
   604 lemma rabs_ratiotest_lemma: "[| c \<le> 0; abs x \<le> c * abs y |] ==> x = (0::real)"
   605 by (erule norm_ratiotest_lemma, simp)
   606 
   607 lemma le_Suc_ex: "(k::nat) \<le> l ==> (\<exists>n. l = k + n)"
   608 apply (drule le_imp_less_or_eq)
   609 apply (auto dest: less_imp_Suc_add)
   610 done
   611 
   612 lemma le_Suc_ex_iff: "((k::nat) \<le> l) = (\<exists>n. l = k + n)"
   613 by (auto simp add: le_Suc_ex)
   614 
   615 (*All this trouble just to get 0<c *)
   616 lemma ratio_test_lemma2:
   617   fixes f :: "nat \<Rightarrow> 'a::banach"
   618   shows "\<lbrakk>\<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> 0 < c \<or> summable f"
   619 apply (simp (no_asm) add: linorder_not_le [symmetric])
   620 apply (simp add: summable_Cauchy)
   621 apply (safe, subgoal_tac "\<forall>n. N < n --> f (n) = 0")
   622  prefer 2
   623  apply clarify
   624  apply(erule_tac x = "n - Suc 0" in allE)
   625  apply (simp add:diff_Suc split:nat.splits)
   626  apply (blast intro: norm_ratiotest_lemma)
   627 apply (rule_tac x = "Suc N" in exI, clarify)
   628 apply(simp cong del: setsum_cong cong: setsum_ivl_cong)
   629 done
   630 
   631 lemma ratio_test:
   632   fixes f :: "nat \<Rightarrow> 'a::banach"
   633   shows "\<lbrakk>c < 1; \<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> summable f"
   634 apply (frule ratio_test_lemma2, auto)
   635 apply (rule_tac g = "%n. (norm (f N) / (c ^ N))*c ^ n"
   636        in summable_comparison_test)
   637 apply (rule_tac x = N in exI, safe)
   638 apply (drule le_Suc_ex_iff [THEN iffD1])
   639 apply (auto simp add: power_add field_power_not_zero)
   640 apply (induct_tac "na", auto)
   641 apply (rule_tac y = "c * norm (f (N + n))" in order_trans)
   642 apply (auto intro: mult_right_mono simp add: summable_def)
   643 apply (rule_tac x = "norm (f N) * (1/ (1 - c)) / (c ^ N)" in exI)
   644 apply (rule sums_divide)
   645 apply (rule sums_mult)
   646 apply (auto intro!: geometric_sums)
   647 done
   648 
   649 subsection {* Cauchy Product Formula *}
   650 
   651 (* Proof based on Analysis WebNotes: Chapter 07, Class 41
   652 http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm *)
   653 
   654 lemma setsum_triangle_reindex:
   655   fixes n :: nat
   656   shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k=0..<n. \<Sum>i=0..k. f i (k - i))"
   657 proof -
   658   have "(\<Sum>(i, j)\<in>{(i, j). i + j < n}. f i j) =
   659     (\<Sum>(k, i)\<in>(SIGMA k:{0..<n}. {0..k}). f i (k - i))"
   660   proof (rule setsum_reindex_cong)
   661     show "inj_on (\<lambda>(k,i). (i, k - i)) (SIGMA k:{0..<n}. {0..k})"
   662       by (rule inj_on_inverseI [where g="\<lambda>(i,j). (i+j, i)"], auto)
   663     show "{(i,j). i + j < n} = (\<lambda>(k,i). (i, k - i)) ` (SIGMA k:{0..<n}. {0..k})"
   664       by (safe, rule_tac x="(a+b,a)" in image_eqI, auto)
   665     show "\<And>a. (\<lambda>(k, i). f i (k - i)) a = split f ((\<lambda>(k, i). (i, k - i)) a)"
   666       by clarify
   667   qed
   668   thus ?thesis by (simp add: setsum_Sigma)
   669 qed
   670 
   671 lemma Cauchy_product_sums:
   672   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   673   assumes a: "summable (\<lambda>k. norm (a k))"
   674   assumes b: "summable (\<lambda>k. norm (b k))"
   675   shows "(\<lambda>k. \<Sum>i=0..k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
   676 proof -
   677   let ?S1 = "\<lambda>n::nat. {0..<n} \<times> {0..<n}"
   678   let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
   679   have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
   680   have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
   681   have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
   682   have finite_S1: "\<And>n. finite (?S1 n)" by simp
   683   with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
   684 
   685   let ?g = "\<lambda>(i,j). a i * b j"
   686   let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
   687   have f_nonneg: "\<And>x. 0 \<le> ?f x"
   688     by (auto simp add: mult_nonneg_nonneg)
   689   hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
   690     unfolding real_norm_def
   691     by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
   692 
   693   have "(\<lambda>n. (\<Sum>k=0..<n. a k) * (\<Sum>k=0..<n. b k))
   694            ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   695     by (intro tendsto_mult summable_sumr_LIMSEQ_suminf
   696         summable_norm_cancel [OF a] summable_norm_cancel [OF b])
   697   hence 1: "(\<lambda>n. setsum ?g (?S1 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   698     by (simp only: setsum_product setsum_Sigma [rule_format]
   699                    finite_atLeastLessThan)
   700 
   701   have "(\<lambda>n. (\<Sum>k=0..<n. norm (a k)) * (\<Sum>k=0..<n. norm (b k)))
   702        ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   703     using a b by (intro tendsto_mult summable_sumr_LIMSEQ_suminf)
   704   hence "(\<lambda>n. setsum ?f (?S1 n)) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
   705     by (simp only: setsum_product setsum_Sigma [rule_format]
   706                    finite_atLeastLessThan)
   707   hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
   708     by (rule convergentI)
   709   hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
   710     by (rule convergent_Cauchy)
   711   have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
   712   proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
   713     fix r :: real
   714     assume r: "0 < r"
   715     from CauchyD [OF Cauchy r] obtain N
   716     where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
   717     hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
   718       by (simp only: setsum_diff finite_S1 S1_mono)
   719     hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
   720       by (simp only: norm_setsum_f)
   721     show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
   722     proof (intro exI allI impI)
   723       fix n assume "2 * N \<le> n"
   724       hence n: "N \<le> n div 2" by simp
   725       have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
   726         by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
   727                   Diff_mono subset_refl S1_le_S2)
   728       also have "\<dots> < r"
   729         using n div_le_dividend by (rule N)
   730       finally show "setsum ?f (?S1 n - ?S2 n) < r" .
   731     qed
   732   qed
   733   hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
   734     apply (rule Zfun_le [rule_format])
   735     apply (simp only: norm_setsum_f)
   736     apply (rule order_trans [OF norm_setsum setsum_mono])
   737     apply (auto simp add: norm_mult_ineq)
   738     done
   739   hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) ----> 0"
   740     unfolding tendsto_Zfun_iff diff_0_right
   741     by (simp only: setsum_diff finite_S1 S2_le_S1)
   742 
   743   with 1 have "(\<lambda>n. setsum ?g (?S2 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
   744     by (rule LIMSEQ_diff_approach_zero2)
   745   thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
   746 qed
   747 
   748 lemma Cauchy_product:
   749   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
   750   assumes a: "summable (\<lambda>k. norm (a k))"
   751   assumes b: "summable (\<lambda>k. norm (b k))"
   752   shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i=0..k. a i * b (k - i))"
   753 using a b
   754 by (rule Cauchy_product_sums [THEN sums_unique])
   755 
   756 end