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