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