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