
paulson@10751: (*  Title       : Series.thy
paulson@10751:     Author      : Jacques D. Fleuriot
paulson@10751:     Copyright   : 1998  University of Cambridge
paulson@14416: 
paulson@14416: Converted to Isar and polished by lcp
nipkow@15539: Converted to setsum and polished yet more by TNN
avigad@16819: Additional contributions by Jeremy Avigad
paulson@10751: *) 
paulson@10751: 
paulson@14416: header{*Finite Summation and Infinite Series*}
paulson@10751: 
nipkow@15131: theory Series
haftmann@29197: imports SEQ
nipkow@15131: begin
nipkow@15561: 
wenzelm@19765: definition
huffman@20692:    sums  :: "(nat \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@21404:      (infixr "sums" 80) where
wenzelm@19765:    "f sums s = (%n. setsum f {0..<n}) ----> s"
paulson@10751: 
wenzelm@21404: definition
wenzelm@21404:    summable :: "(nat \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> bool" where
wenzelm@19765:    "summable f = (\<exists>s. f sums s)"
paulson@14416: 
wenzelm@21404: definition
wenzelm@21404:    suminf   :: "(nat \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> 'a" where
huffman@20688:    "suminf f = (THE s. f sums s)"
paulson@14416: 
nipkow@15546: syntax
huffman@20692:   "_suminf" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a" ("\<Sum>_. _" [0, 10] 10)
nipkow@15546: translations
wenzelm@20770:   "\<Sum>i. b" == "CONST suminf (%i. b)"
nipkow@15546: 
paulson@14416: 
nipkow@15539: lemma sumr_diff_mult_const:
nipkow@15539:  "setsum f {0..<n} - (real n*r) = setsum (%i. f i - r) {0..<n::nat}"
nipkow@15536: by (simp add: diff_minus setsum_addf real_of_nat_def)
nipkow@15536: 
nipkow@15542: lemma real_setsum_nat_ivl_bounded:
nipkow@15542:      "(!!p. p < n \<Longrightarrow> f(p) \<le> K)
nipkow@15542:       \<Longrightarrow> setsum f {0..<n::nat} \<le> real n * K"
nipkow@15542: using setsum_bounded[where A = "{0..<n}"]
nipkow@15542: by (auto simp:real_of_nat_def)
paulson@14416: 
nipkow@15539: (* Generalize from real to some algebraic structure? *)
nipkow@15539: lemma sumr_minus_one_realpow_zero [simp]:
nipkow@15543:   "(\<Sum>i=0..<2*n. (-1) ^ Suc i) = (0::real)"
paulson@15251: by (induct "n", auto)
paulson@14416: 
nipkow@15539: (* FIXME this is an awful lemma! *)
nipkow@15539: lemma sumr_one_lb_realpow_zero [simp]:
nipkow@15539:   "(\<Sum>n=Suc 0..<n. f(n) * (0::real) ^ n) = 0"
huffman@20692: by (rule setsum_0', simp)
paulson@14416: 
nipkow@15543: lemma sumr_group:
nipkow@15539:      "(\<Sum>m=0..<n::nat. setsum f {m * k ..< m*k + k}) = setsum f {0 ..< n * k}"
nipkow@15543: apply (subgoal_tac "k = 0 | 0 < k", auto)
paulson@15251: apply (induct "n")
nipkow@15539: apply (simp_all add: setsum_add_nat_ivl add_commute)
paulson@14416: done
nipkow@15539: 
huffman@20692: lemma sumr_offset3:
huffman@20692:   "setsum f {0::nat..<n+k} = (\<Sum>m=0..<n. f (m+k)) + setsum f {0..<k}"
huffman@20692: apply (subst setsum_shift_bounds_nat_ivl [symmetric])
huffman@20692: apply (simp add: setsum_add_nat_ivl add_commute)
huffman@20692: done
huffman@20692: 
avigad@16819: lemma sumr_offset:
huffman@20692:   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
huffman@20692:   shows "(\<Sum>m=0..<n. f(m+k)) = setsum f {0..<n+k} - setsum f {0..<k}"
huffman@20692: by (simp add: sumr_offset3)
avigad@16819: 
avigad@16819: lemma sumr_offset2:
avigad@16819:  "\<forall>f. (\<Sum>m=0..<n::nat. f(m+k)::real) = setsum f {0..<n+k} - setsum f {0..<k}"
huffman@20692: by (simp add: sumr_offset)
avigad@16819: 
avigad@16819: lemma sumr_offset4:
huffman@20692:   "\<forall>n f. setsum f {0::nat..<n+k} = (\<Sum>m=0..<n. f (m+k)::real) + setsum f {0..<k}"
huffman@20692: by (clarify, rule sumr_offset3)
avigad@16819: 
avigad@16819: (*
avigad@16819: lemma sumr_from_1_from_0: "0 < n ==>
avigad@16819:       (\<Sum>n=Suc 0 ..< Suc n. if even(n) then 0 else
avigad@16819:              ((- 1) ^ ((n - (Suc 0)) div 2))/(real (fact n))) * a ^ n =
avigad@16819:       (\<Sum>n=0..<Suc n. if even(n) then 0 else
avigad@16819:              ((- 1) ^ ((n - (Suc 0)) div 2))/(real (fact n))) * a ^ n"
avigad@16819: by (rule_tac n1 = 1 in sumr_split_add [THEN subst], auto)
avigad@16819: *)
paulson@14416: 
paulson@14416: subsection{* Infinite Sums, by the Properties of Limits*}
paulson@14416: 
paulson@14416: (*----------------------
paulson@14416:    suminf is the sum   
paulson@14416:  ---------------------*)
paulson@14416: lemma sums_summable: "f sums l ==> summable f"
paulson@14416: by (simp add: sums_def summable_def, blast)
paulson@14416: 
paulson@14416: lemma summable_sums: "summable f ==> f sums (suminf f)"
huffman@20688: apply (simp add: summable_def suminf_def sums_def)
huffman@20688: apply (blast intro: theI LIMSEQ_unique)
paulson@14416: done
paulson@14416: 
paulson@14416: lemma summable_sumr_LIMSEQ_suminf: 
nipkow@15539:      "summable f ==> (%n. setsum f {0..<n}) ----> (suminf f)"
huffman@20688: by (rule summable_sums [unfolded sums_def])
paulson@14416: 
paulson@14416: (*-------------------
paulson@14416:     sum is unique                    
paulson@14416:  ------------------*)
paulson@14416: lemma sums_unique: "f sums s ==> (s = suminf f)"
paulson@14416: apply (frule sums_summable [THEN summable_sums])
paulson@14416: apply (auto intro!: LIMSEQ_unique simp add: sums_def)
paulson@14416: done
paulson@14416: 
avigad@16819: lemma sums_split_initial_segment: "f sums s ==> 
avigad@16819:   (%n. f(n + k)) sums (s - (SUM i = 0..< k. f i))"
avigad@16819:   apply (unfold sums_def);
avigad@16819:   apply (simp add: sumr_offset); 
avigad@16819:   apply (rule LIMSEQ_diff_const)
avigad@16819:   apply (rule LIMSEQ_ignore_initial_segment)
avigad@16819:   apply assumption
avigad@16819: done
avigad@16819: 
avigad@16819: lemma summable_ignore_initial_segment: "summable f ==> 
avigad@16819:     summable (%n. f(n + k))"
avigad@16819:   apply (unfold summable_def)
avigad@16819:   apply (auto intro: sums_split_initial_segment)
avigad@16819: done
avigad@16819: 
avigad@16819: lemma suminf_minus_initial_segment: "summable f ==>
avigad@16819:     suminf f = s ==> suminf (%n. f(n + k)) = s - (SUM i = 0..< k. f i)"
avigad@16819:   apply (frule summable_ignore_initial_segment)
avigad@16819:   apply (rule sums_unique [THEN sym])
avigad@16819:   apply (frule summable_sums)
avigad@16819:   apply (rule sums_split_initial_segment)
avigad@16819:   apply auto
avigad@16819: done
avigad@16819: 
avigad@16819: lemma suminf_split_initial_segment: "summable f ==> 
avigad@16819:     suminf f = (SUM i = 0..< k. f i) + suminf (%n. f(n + k))"
avigad@16819: by (auto simp add: suminf_minus_initial_segment)
avigad@16819: 
hoelzl@29803: lemma suminf_exist_split: fixes r :: real assumes "0 < r" and "summable a"
hoelzl@29803:   shows "\<exists> N. \<forall> n \<ge> N. \<bar> \<Sum> i. a (i + n) \<bar> < r"
hoelzl@29803: proof -
hoelzl@29803:   from LIMSEQ_D[OF summable_sumr_LIMSEQ_suminf[OF `summable a`] `0 < r`]
hoelzl@29803:   obtain N :: nat where "\<forall> n \<ge> N. norm (setsum a {0..<n} - suminf a) < r" by auto
hoelzl@29803:   thus ?thesis unfolding suminf_minus_initial_segment[OF `summable a` refl] abs_minus_commute real_norm_def
hoelzl@29803:     by auto
hoelzl@29803: qed
hoelzl@29803: 
hoelzl@29803: lemma sums_Suc: assumes sumSuc: "(\<lambda> n. f (Suc n)) sums l" shows "f sums (l + f 0)"
hoelzl@29803: proof -
hoelzl@29803:   from sumSuc[unfolded sums_def]
hoelzl@29803:   have "(\<lambda>i. \<Sum>n = Suc 0..<Suc i. f n) ----> l" unfolding setsum_reindex[OF inj_Suc] image_Suc_atLeastLessThan[symmetric] comp_def .
hoelzl@29803:   from LIMSEQ_add_const[OF this, where b="f 0"] 
hoelzl@29803:   have "(\<lambda>i. \<Sum>n = 0..<Suc i. f n) ----> l + f 0" unfolding add_commute setsum_head_upt_Suc[OF zero_less_Suc] .
hoelzl@29803:   thus ?thesis unfolding sums_def by (rule LIMSEQ_imp_Suc)
hoelzl@29803: qed
hoelzl@29803: 
paulson@14416: lemma series_zero: 
nipkow@15539:      "(\<forall>m. n \<le> m --> f(m) = 0) ==> f sums (setsum f {0..<n})"
nipkow@15537: apply (simp add: sums_def LIMSEQ_def diff_minus[symmetric], safe)
paulson@14416: apply (rule_tac x = n in exI)
nipkow@15542: apply (clarsimp simp add:setsum_diff[symmetric] cong:setsum_ivl_cong)
paulson@14416: done
paulson@14416: 
huffman@23121: lemma sums_zero: "(\<lambda>n. 0) sums 0"
huffman@23121: unfolding sums_def by (simp add: LIMSEQ_const)
nipkow@15539: 
huffman@23121: lemma summable_zero: "summable (\<lambda>n. 0)"
huffman@23121: by (rule sums_zero [THEN sums_summable])
avigad@16819: 
huffman@23121: lemma suminf_zero: "suminf (\<lambda>n. 0) = 0"
huffman@23121: by (rule sums_zero [THEN sums_unique, symmetric])
avigad@16819:   
huffman@23119: lemma (in bounded_linear) sums:
huffman@23119:   "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
huffman@23119: unfolding sums_def by (drule LIMSEQ, simp only: setsum)
huffman@23119: 
huffman@23119: lemma (in bounded_linear) summable:
huffman@23119:   "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
huffman@23119: unfolding summable_def by (auto intro: sums)
huffman@23119: 
huffman@23119: lemma (in bounded_linear) suminf:
huffman@23119:   "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
huffman@23121: by (intro sums_unique sums summable_sums)
huffman@23119: 
huffman@20692: lemma sums_mult:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@20692:   shows "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
huffman@23127: by (rule mult_right.sums)
paulson@14416: 
huffman@20692: lemma summable_mult:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@23121:   shows "summable f \<Longrightarrow> summable (%n. c * f n)"
huffman@23127: by (rule mult_right.summable)
avigad@16819: 
huffman@20692: lemma suminf_mult:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@20692:   shows "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f";
huffman@23127: by (rule mult_right.suminf [symmetric])
avigad@16819: 
huffman@20692: lemma sums_mult2:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@20692:   shows "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
huffman@23127: by (rule mult_left.sums)
avigad@16819: 
huffman@20692: lemma summable_mult2:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@20692:   shows "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
huffman@23127: by (rule mult_left.summable)
avigad@16819: 
huffman@20692: lemma suminf_mult2:
huffman@20692:   fixes c :: "'a::real_normed_algebra"
huffman@20692:   shows "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
huffman@23127: by (rule mult_left.suminf)
avigad@16819: 
huffman@20692: lemma sums_divide:
huffman@20692:   fixes c :: "'a::real_normed_field"
huffman@20692:   shows "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
huffman@23127: by (rule divide.sums)
paulson@14416: 
huffman@20692: lemma summable_divide:
huffman@20692:   fixes c :: "'a::real_normed_field"
huffman@20692:   shows "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
huffman@23127: by (rule divide.summable)
avigad@16819: 
huffman@20692: lemma suminf_divide:
huffman@20692:   fixes c :: "'a::real_normed_field"
huffman@20692:   shows "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
huffman@23127: by (rule divide.suminf [symmetric])
avigad@16819: 
huffman@23121: lemma sums_add: "\<lbrakk>X sums a; Y sums b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n + Y n) sums (a + b)"
huffman@23121: unfolding sums_def by (simp add: setsum_addf LIMSEQ_add)
avigad@16819: 
huffman@23121: lemma summable_add: "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> summable (\<lambda>n. X n + Y n)"
huffman@23121: unfolding summable_def by (auto intro: sums_add)
avigad@16819: 
avigad@16819: lemma suminf_add:
huffman@23121:   "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> suminf X + suminf Y = (\<Sum>n. X n + Y n)"
huffman@23121: by (intro sums_unique sums_add summable_sums)
paulson@14416: 
huffman@23121: lemma sums_diff: "\<lbrakk>X sums a; Y sums b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n - Y n) sums (a - b)"
huffman@23121: unfolding sums_def by (simp add: setsum_subtractf LIMSEQ_diff)
huffman@23121: 
huffman@23121: lemma summable_diff: "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> summable (\<lambda>n. X n - Y n)"
huffman@23121: unfolding summable_def by (auto intro: sums_diff)
paulson@14416: 
paulson@14416: lemma suminf_diff:
huffman@23121:   "\<lbrakk>summable X; summable Y\<rbrakk> \<Longrightarrow> suminf X - suminf Y = (\<Sum>n. X n - Y n)"
huffman@23121: by (intro sums_unique sums_diff summable_sums)
paulson@14416: 
huffman@23121: lemma sums_minus: "X sums a ==> (\<lambda>n. - X n) sums (- a)"
huffman@23121: unfolding sums_def by (simp add: setsum_negf LIMSEQ_minus)
avigad@16819: 
huffman@23121: lemma summable_minus: "summable X \<Longrightarrow> summable (\<lambda>n. - X n)"
huffman@23121: unfolding summable_def by (auto intro: sums_minus)
avigad@16819: 
huffman@23121: lemma suminf_minus: "summable X \<Longrightarrow> (\<Sum>n. - X n) = - (\<Sum>n. X n)"
huffman@23121: by (intro sums_unique [symmetric] sums_minus summable_sums)
paulson@14416: 
paulson@14416: lemma sums_group:
nipkow@15539:      "[|summable f; 0 < k |] ==> (%n. setsum f {n*k..<n*k+k}) sums (suminf f)"
paulson@14416: apply (drule summable_sums)
huffman@20692: apply (simp only: sums_def sumr_group)
huffman@20692: apply (unfold LIMSEQ_def, safe)
huffman@20692: apply (drule_tac x="r" in spec, safe)
huffman@20692: apply (rule_tac x="no" in exI, safe)
huffman@20692: apply (drule_tac x="n*k" in spec)
huffman@20692: apply (erule mp)
huffman@20692: apply (erule order_trans)
huffman@20692: apply simp
paulson@14416: done
paulson@14416: 
paulson@15085: text{*A summable series of positive terms has limit that is at least as
paulson@15085: great as any partial sum.*}
paulson@14416: 
huffman@20692: lemma series_pos_le:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 \<le> f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} \<le> suminf f"
paulson@14416: apply (drule summable_sums)
paulson@14416: apply (simp add: sums_def)
nipkow@15539: apply (cut_tac k = "setsum f {0..<n}" in LIMSEQ_const)
nipkow@15539: apply (erule LIMSEQ_le, blast)
huffman@20692: apply (rule_tac x="n" in exI, clarify)
nipkow@15539: apply (rule setsum_mono2)
nipkow@15539: apply auto
paulson@14416: done
paulson@14416: 
paulson@14416: lemma series_pos_less:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 < f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} < suminf f"
huffman@20692: apply (rule_tac y="setsum f {0..<Suc n}" in order_less_le_trans)
huffman@20692: apply simp
huffman@20692: apply (erule series_pos_le)
huffman@20692: apply (simp add: order_less_imp_le)
huffman@20692: done
huffman@20692: 
huffman@20692: lemma suminf_gt_zero:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>summable f; \<forall>n. 0 < f n\<rbrakk> \<Longrightarrow> 0 < suminf f"
huffman@20692: by (drule_tac n="0" in series_pos_less, simp_all)
huffman@20692: 
huffman@20692: lemma suminf_ge_zero:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>summable f; \<forall>n. 0 \<le> f n\<rbrakk> \<Longrightarrow> 0 \<le> suminf f"
huffman@20692: by (drule_tac n="0" in series_pos_le, simp_all)
huffman@20692: 
huffman@20692: lemma sumr_pos_lt_pair:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>summable f;
huffman@20692:         \<forall>d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc(Suc 0) * d) + 1))\<rbrakk>
huffman@20692:       \<Longrightarrow> setsum f {0..<k} < suminf f"
huffman@30082: unfolding One_nat_def
huffman@20692: apply (subst suminf_split_initial_segment [where k="k"])
huffman@20692: apply assumption
huffman@20692: apply simp
huffman@20692: apply (drule_tac k="k" in summable_ignore_initial_segment)
huffman@20692: apply (drule_tac k="Suc (Suc 0)" in sums_group, simp)
huffman@20692: apply simp
huffman@20692: apply (frule sums_unique)
huffman@20692: apply (drule sums_summable)
huffman@20692: apply simp
huffman@20692: apply (erule suminf_gt_zero)
huffman@20692: apply (simp add: add_ac)
paulson@14416: done
paulson@14416: 
paulson@15085: text{*Sum of a geometric progression.*}
paulson@14416: 
ballarin@17149: lemmas sumr_geometric = geometric_sum [where 'a = real]
paulson@14416: 
huffman@20692: lemma geometric_sums:
huffman@22719:   fixes x :: "'a::{real_normed_field,recpower}"
huffman@20692:   shows "norm x < 1 \<Longrightarrow> (\<lambda>n. x ^ n) sums (1 / (1 - x))"
huffman@20692: proof -
huffman@20692:   assume less_1: "norm x < 1"
huffman@20692:   hence neq_1: "x \<noteq> 1" by auto
huffman@20692:   hence neq_0: "x - 1 \<noteq> 0" by simp
huffman@20692:   from less_1 have lim_0: "(\<lambda>n. x ^ n) ----> 0"
huffman@20692:     by (rule LIMSEQ_power_zero)
huffman@22719:   hence "(\<lambda>n. x ^ n / (x - 1) - 1 / (x - 1)) ----> 0 / (x - 1) - 1 / (x - 1)"
huffman@20692:     using neq_0 by (intro LIMSEQ_divide LIMSEQ_diff LIMSEQ_const)
huffman@20692:   hence "(\<lambda>n. (x ^ n - 1) / (x - 1)) ----> 1 / (1 - x)"
huffman@20692:     by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
huffman@20692:   thus "(\<lambda>n. x ^ n) sums (1 / (1 - x))"
huffman@20692:     by (simp add: sums_def geometric_sum neq_1)
huffman@20692: qed
huffman@20692: 
huffman@20692: lemma summable_geometric:
huffman@22719:   fixes x :: "'a::{real_normed_field,recpower}"
huffman@20692:   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
huffman@20692: by (rule geometric_sums [THEN sums_summable])
paulson@14416: 
paulson@15085: text{*Cauchy-type criterion for convergence of series (c.f. Harrison)*}
paulson@15085: 
nipkow@15539: lemma summable_convergent_sumr_iff:
nipkow@15539:  "summable f = convergent (%n. setsum f {0..<n})"
paulson@14416: by (simp add: summable_def sums_def convergent_def)
paulson@14416: 
huffman@20689: lemma summable_LIMSEQ_zero: "summable f \<Longrightarrow> f ----> 0"
huffman@20689: apply (drule summable_convergent_sumr_iff [THEN iffD1])
huffman@20692: apply (drule convergent_Cauchy)
huffman@20689: apply (simp only: Cauchy_def LIMSEQ_def, safe)
huffman@20689: apply (drule_tac x="r" in spec, safe)
huffman@20689: apply (rule_tac x="M" in exI, safe)
huffman@20689: apply (drule_tac x="Suc n" in spec, simp)
huffman@20689: apply (drule_tac x="n" in spec, simp)
huffman@20689: done
huffman@20689: 
paulson@14416: lemma summable_Cauchy:
huffman@20848:      "summable (f::nat \<Rightarrow> 'a::banach) =  
huffman@20848:       (\<forall>e > 0. \<exists>N. \<forall>m \<ge> N. \<forall>n. norm (setsum f {m..<n}) < e)"
huffman@20848: apply (simp only: summable_convergent_sumr_iff Cauchy_convergent_iff [symmetric] Cauchy_def, safe)
huffman@20410: apply (drule spec, drule (1) mp)
huffman@20410: apply (erule exE, rule_tac x="M" in exI, clarify)
huffman@20410: apply (rule_tac x="m" and y="n" in linorder_le_cases)
huffman@20410: apply (frule (1) order_trans)
huffman@20410: apply (drule_tac x="n" in spec, drule (1) mp)
huffman@20410: apply (drule_tac x="m" in spec, drule (1) mp)
huffman@20410: apply (simp add: setsum_diff [symmetric])
huffman@20410: apply simp
huffman@20410: apply (drule spec, drule (1) mp)
huffman@20410: apply (erule exE, rule_tac x="N" in exI, clarify)
huffman@20410: apply (rule_tac x="m" and y="n" in linorder_le_cases)
huffman@20552: apply (subst norm_minus_commute)
huffman@20410: apply (simp add: setsum_diff [symmetric])
huffman@20410: apply (simp add: setsum_diff [symmetric])
paulson@14416: done
paulson@14416: 
paulson@15085: text{*Comparison test*}
paulson@15085: 
huffman@20692: lemma norm_setsum:
huffman@20692:   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@20692:   shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
huffman@20692: apply (case_tac "finite A")
huffman@20692: apply (erule finite_induct)
huffman@20692: apply simp
huffman@20692: apply simp
huffman@20692: apply (erule order_trans [OF norm_triangle_ineq add_left_mono])
huffman@20692: apply simp
huffman@20692: done
huffman@20692: 
paulson@14416: lemma summable_comparison_test:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f"
huffman@20692: apply (simp add: summable_Cauchy, safe)
huffman@20692: apply (drule_tac x="e" in spec, safe)
huffman@20692: apply (rule_tac x = "N + Na" in exI, safe)
paulson@14416: apply (rotate_tac 2)
paulson@14416: apply (drule_tac x = m in spec)
paulson@14416: apply (auto, rotate_tac 2, drule_tac x = n in spec)
huffman@20848: apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
huffman@20848: apply (rule norm_setsum)
nipkow@15539: apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
huffman@22998: apply (auto intro: setsum_mono simp add: abs_less_iff)
paulson@14416: done
paulson@14416: 
huffman@20848: lemma summable_norm_comparison_test:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk>
huffman@20848:          \<Longrightarrow> summable (\<lambda>n. norm (f n))"
huffman@20848: apply (rule summable_comparison_test)
huffman@20848: apply (auto)
huffman@20848: done
huffman@20848: 
paulson@14416: lemma summable_rabs_comparison_test:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   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>)"
paulson@14416: apply (rule summable_comparison_test)
nipkow@15543: apply (auto)
paulson@14416: done
paulson@14416: 
huffman@23084: text{*Summability of geometric series for real algebras*}
huffman@23084: 
huffman@23084: lemma complete_algebra_summable_geometric:
huffman@23084:   fixes x :: "'a::{real_normed_algebra_1,banach,recpower}"
huffman@23084:   shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
huffman@23084: proof (rule summable_comparison_test)
huffman@23084:   show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
huffman@23084:     by (simp add: norm_power_ineq)
huffman@23084:   show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
huffman@23084:     by (simp add: summable_geometric)
huffman@23084: qed
huffman@23084: 
paulson@15085: text{*Limit comparison property for series (c.f. jrh)*}
paulson@15085: 
paulson@14416: lemma summable_le:
huffman@20692:   fixes f g :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
paulson@14416: apply (drule summable_sums)+
huffman@20692: apply (simp only: sums_def, erule (1) LIMSEQ_le)
paulson@14416: apply (rule exI)
nipkow@15539: apply (auto intro!: setsum_mono)
paulson@14416: done
paulson@14416: 
paulson@14416: lemma summable_le2:
huffman@20692:   fixes f g :: "nat \<Rightarrow> real"
huffman@20692:   shows "\<lbrakk>\<forall>n. \<bar>f n\<bar> \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f \<and> suminf f \<le> suminf g"
huffman@20848: apply (subgoal_tac "summable f")
huffman@20848: apply (auto intro!: summable_le)
huffman@22998: apply (simp add: abs_le_iff)
huffman@20848: apply (rule_tac g="g" in summable_comparison_test, simp_all)
paulson@14416: done
paulson@14416: 
kleing@19106: (* specialisation for the common 0 case *)
kleing@19106: lemma suminf_0_le:
kleing@19106:   fixes f::"nat\<Rightarrow>real"
kleing@19106:   assumes gt0: "\<forall>n. 0 \<le> f n" and sm: "summable f"
kleing@19106:   shows "0 \<le> suminf f"
kleing@19106: proof -
kleing@19106:   let ?g = "(\<lambda>n. (0::real))"
kleing@19106:   from gt0 have "\<forall>n. ?g n \<le> f n" by simp
kleing@19106:   moreover have "summable ?g" by (rule summable_zero)
kleing@19106:   moreover from sm have "summable f" .
kleing@19106:   ultimately have "suminf ?g \<le> suminf f" by (rule summable_le)
kleing@19106:   then show "0 \<le> suminf f" by (simp add: suminf_zero)
kleing@19106: qed 
kleing@19106: 
kleing@19106: 
paulson@15085: text{*Absolute convergence imples normal convergence*}
huffman@20848: lemma summable_norm_cancel:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
huffman@20692: apply (simp only: summable_Cauchy, safe)
huffman@20692: apply (drule_tac x="e" in spec, safe)
huffman@20692: apply (rule_tac x="N" in exI, safe)
huffman@20692: apply (drule_tac x="m" in spec, safe)
huffman@20848: apply (rule order_le_less_trans [OF norm_setsum])
huffman@20848: apply (rule order_le_less_trans [OF abs_ge_self])
huffman@20692: apply simp
paulson@14416: done
paulson@14416: 
huffman@20848: lemma summable_rabs_cancel:
huffman@20848:   fixes f :: "nat \<Rightarrow> real"
huffman@20848:   shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> summable f"
huffman@20848: by (rule summable_norm_cancel, simp)
huffman@20848: 
paulson@15085: text{*Absolute convergence of series*}
huffman@20848: lemma summable_norm:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
huffman@20848: by (auto intro: LIMSEQ_le LIMSEQ_norm summable_norm_cancel
huffman@20848:                 summable_sumr_LIMSEQ_suminf norm_setsum)
huffman@20848: 
paulson@14416: lemma summable_rabs:
huffman@20692:   fixes f :: "nat \<Rightarrow> real"
huffman@20692:   shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
huffman@20848: by (fold real_norm_def, rule summable_norm)
paulson@14416: 
paulson@14416: subsection{* The Ratio Test*}
paulson@14416: 
huffman@20848: lemma norm_ratiotest_lemma:
huffman@22852:   fixes x y :: "'a::real_normed_vector"
huffman@20848:   shows "\<lbrakk>c \<le> 0; norm x \<le> c * norm y\<rbrakk> \<Longrightarrow> x = 0"
huffman@20848: apply (subgoal_tac "norm x \<le> 0", simp)
huffman@20848: apply (erule order_trans)
huffman@20848: apply (simp add: mult_le_0_iff)
huffman@20848: done
huffman@20848: 
paulson@14416: lemma rabs_ratiotest_lemma: "[| c \<le> 0; abs x \<le> c * abs y |] ==> x = (0::real)"
huffman@20848: by (erule norm_ratiotest_lemma, simp)
paulson@14416: 
paulson@14416: lemma le_Suc_ex: "(k::nat) \<le> l ==> (\<exists>n. l = k + n)"
paulson@14416: apply (drule le_imp_less_or_eq)
paulson@14416: apply (auto dest: less_imp_Suc_add)
paulson@14416: done
paulson@14416: 
paulson@14416: lemma le_Suc_ex_iff: "((k::nat) \<le> l) = (\<exists>n. l = k + n)"
paulson@14416: by (auto simp add: le_Suc_ex)
paulson@14416: 
paulson@14416: (*All this trouble just to get 0<c *)
paulson@14416: lemma ratio_test_lemma2:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "\<lbrakk>\<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> 0 < c \<or> summable f"
paulson@14416: apply (simp (no_asm) add: linorder_not_le [symmetric])
paulson@14416: apply (simp add: summable_Cauchy)
nipkow@15543: apply (safe, subgoal_tac "\<forall>n. N < n --> f (n) = 0")
nipkow@15543:  prefer 2
nipkow@15543:  apply clarify
huffman@30082:  apply(erule_tac x = "n - Suc 0" in allE)
nipkow@15543:  apply (simp add:diff_Suc split:nat.splits)
huffman@20848:  apply (blast intro: norm_ratiotest_lemma)
paulson@14416: apply (rule_tac x = "Suc N" in exI, clarify)
nipkow@15543: apply(simp cong:setsum_ivl_cong)
paulson@14416: done
paulson@14416: 
paulson@14416: lemma ratio_test:
huffman@20848:   fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848:   shows "\<lbrakk>c < 1; \<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> summable f"
paulson@14416: apply (frule ratio_test_lemma2, auto)
huffman@20848: apply (rule_tac g = "%n. (norm (f N) / (c ^ N))*c ^ n" 
paulson@15234:        in summable_comparison_test)
paulson@14416: apply (rule_tac x = N in exI, safe)
paulson@14416: apply (drule le_Suc_ex_iff [THEN iffD1])
huffman@22959: apply (auto simp add: power_add field_power_not_zero)
nipkow@15539: apply (induct_tac "na", auto)
huffman@20848: apply (rule_tac y = "c * norm (f (N + n))" in order_trans)
paulson@14416: apply (auto intro: mult_right_mono simp add: summable_def)
paulson@14416: apply (simp add: mult_ac)
huffman@20848: apply (rule_tac x = "norm (f N) * (1/ (1 - c)) / (c ^ N)" in exI)
paulson@15234: apply (rule sums_divide) 
haftmann@27108: apply (rule sums_mult)
paulson@15234: apply (auto intro!: geometric_sums)
paulson@14416: done
paulson@14416: 
huffman@23111: subsection {* Cauchy Product Formula *}
huffman@23111: 
huffman@23111: (* Proof based on Analysis WebNotes: Chapter 07, Class 41
huffman@23111: http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm *)
huffman@23111: 
huffman@23111: lemma setsum_triangle_reindex:
huffman@23111:   fixes n :: nat
huffman@23111:   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))"
huffman@23111: proof -
huffman@23111:   have "(\<Sum>(i, j)\<in>{(i, j). i + j < n}. f i j) =
huffman@23111:     (\<Sum>(k, i)\<in>(SIGMA k:{0..<n}. {0..k}). f i (k - i))"
huffman@23111:   proof (rule setsum_reindex_cong)
huffman@23111:     show "inj_on (\<lambda>(k,i). (i, k - i)) (SIGMA k:{0..<n}. {0..k})"
huffman@23111:       by (rule inj_on_inverseI [where g="\<lambda>(i,j). (i+j, i)"], auto)
huffman@23111:     show "{(i,j). i + j < n} = (\<lambda>(k,i). (i, k - i)) ` (SIGMA k:{0..<n}. {0..k})"
huffman@23111:       by (safe, rule_tac x="(a+b,a)" in image_eqI, auto)
huffman@23111:     show "\<And>a. (\<lambda>(k, i). f i (k - i)) a = split f ((\<lambda>(k, i). (i, k - i)) a)"
huffman@23111:       by clarify
huffman@23111:   qed
huffman@23111:   thus ?thesis by (simp add: setsum_Sigma)
huffman@23111: qed
huffman@23111: 
huffman@23111: lemma Cauchy_product_sums:
huffman@23111:   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111:   assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111:   assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111:   shows "(\<lambda>k. \<Sum>i=0..k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
huffman@23111: proof -
huffman@23111:   let ?S1 = "\<lambda>n::nat. {0..<n} \<times> {0..<n}"
huffman@23111:   let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
huffman@23111:   have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
huffman@23111:   have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
huffman@23111:   have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
huffman@23111:   have finite_S1: "\<And>n. finite (?S1 n)" by simp
huffman@23111:   with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
huffman@23111: 
huffman@23111:   let ?g = "\<lambda>(i,j). a i * b j"
huffman@23111:   let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
huffman@23111:   have f_nonneg: "\<And>x. 0 \<le> ?f x"
huffman@23111:     by (auto simp add: mult_nonneg_nonneg)
huffman@23111:   hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
huffman@23111:     unfolding real_norm_def
huffman@23111:     by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
huffman@23111: 
huffman@23111:   have "(\<lambda>n. (\<Sum>k=0..<n. a k) * (\<Sum>k=0..<n. b k))
huffman@23111:            ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111:     by (intro LIMSEQ_mult summable_sumr_LIMSEQ_suminf
huffman@23111:         summable_norm_cancel [OF a] summable_norm_cancel [OF b])
huffman@23111:   hence 1: "(\<lambda>n. setsum ?g (?S1 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111:     by (simp only: setsum_product setsum_Sigma [rule_format]
huffman@23111:                    finite_atLeastLessThan)
huffman@23111: 
huffman@23111:   have "(\<lambda>n. (\<Sum>k=0..<n. norm (a k)) * (\<Sum>k=0..<n. norm (b k)))
huffman@23111:        ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
huffman@23111:     using a b by (intro LIMSEQ_mult summable_sumr_LIMSEQ_suminf)
huffman@23111:   hence "(\<lambda>n. setsum ?f (?S1 n)) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
huffman@23111:     by (simp only: setsum_product setsum_Sigma [rule_format]
huffman@23111:                    finite_atLeastLessThan)
huffman@23111:   hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111:     by (rule convergentI)
huffman@23111:   hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111:     by (rule convergent_Cauchy)
huffman@23111:   have "Zseq (\<lambda>n. setsum ?f (?S1 n - ?S2 n))"
huffman@23111:   proof (rule ZseqI, simp only: norm_setsum_f)
huffman@23111:     fix r :: real
huffman@23111:     assume r: "0 < r"
huffman@23111:     from CauchyD [OF Cauchy r] obtain N
huffman@23111:     where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
huffman@23111:     hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
huffman@23111:       by (simp only: setsum_diff finite_S1 S1_mono)
huffman@23111:     hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
huffman@23111:       by (simp only: norm_setsum_f)
huffman@23111:     show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
huffman@23111:     proof (intro exI allI impI)
huffman@23111:       fix n assume "2 * N \<le> n"
huffman@23111:       hence n: "N \<le> n div 2" by simp
huffman@23111:       have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
huffman@23111:         by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
huffman@23111:                   Diff_mono subset_refl S1_le_S2)
huffman@23111:       also have "\<dots> < r"
huffman@23111:         using n div_le_dividend by (rule N)
huffman@23111:       finally show "setsum ?f (?S1 n - ?S2 n) < r" .
huffman@23111:     qed
huffman@23111:   qed
huffman@23111:   hence "Zseq (\<lambda>n. setsum ?g (?S1 n - ?S2 n))"
huffman@23111:     apply (rule Zseq_le [rule_format])
huffman@23111:     apply (simp only: norm_setsum_f)
huffman@23111:     apply (rule order_trans [OF norm_setsum setsum_mono])
huffman@23111:     apply (auto simp add: norm_mult_ineq)
huffman@23111:     done
huffman@23111:   hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) ----> 0"
huffman@23111:     by (simp only: LIMSEQ_Zseq_iff setsum_diff finite_S1 S2_le_S1 diff_0_right)
huffman@23111: 
huffman@23111:   with 1 have "(\<lambda>n. setsum ?g (?S2 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111:     by (rule LIMSEQ_diff_approach_zero2)
huffman@23111:   thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
huffman@23111: qed
huffman@23111: 
huffman@23111: lemma Cauchy_product:
huffman@23111:   fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111:   assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111:   assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111:   shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i=0..k. a i * b (k - i))"
huffman@23441: using a b
huffman@23111: by (rule Cauchy_product_sums [THEN sums_unique])
huffman@23111: 
paulson@14416: end




