src/HOL/Series.thy
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25)
changeset 47108 2a1953f0d20d
parent 46904 f30e941b4512
child 47761 dfe747e72fa8
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
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:
wenzelm@46904
    90
  fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
wenzelm@46904
    91
  assumes "summable f"
wenzelm@46904
    92
  shows "f sums (suminf f)"
hoelzl@41970
    93
proof -
wenzelm@46904
    94
  from assms obtain s where s: "(\<lambda>n. setsum f {0..<n}) ----> s"
wenzelm@46904
    95
    unfolding summable_def sums_def [abs_def] ..
wenzelm@46904
    96
  then show ?thesis unfolding sums_def [abs_def] 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)
huffman@44710
   128
  apply (rule tendsto_diff [OF _ tendsto_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 .
huffman@44710
   172
  from tendsto_add[OF this tendsto_const, 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
huffman@44568
   189
    apply (rule tendsto_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@44568
   194
  unfolding sums_def by (simp add: tendsto_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@44568
   204
  unfolding sums_def by (drule tendsto, 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@44568
   266
  unfolding sums_def by (simp add: setsum_addf tendsto_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@44568
   281
  unfolding sums_def by (simp add: setsum_subtractf tendsto_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@44568
   296
  unfolding sums_def by (simp add: setsum_negf tendsto_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"
huffman@44727
   310
  shows "\<lbrakk>f sums s; 0 < k\<rbrakk> \<Longrightarrow> (\<lambda>n. setsum f {n*k..<n*k+k}) sums s"
huffman@20692
   311
apply (simp only: sums_def sumr_group)
huffman@31336
   312
apply (unfold LIMSEQ_iff, safe)
huffman@20692
   313
apply (drule_tac x="r" in spec, safe)
huffman@20692
   314
apply (rule_tac x="no" in exI, safe)
huffman@20692
   315
apply (drule_tac x="n*k" in spec)
huffman@20692
   316
apply (erule mp)
huffman@20692
   317
apply (erule order_trans)
huffman@20692
   318
apply simp
paulson@14416
   319
done
paulson@14416
   320
paulson@15085
   321
text{*A summable series of positive terms has limit that is at least as
paulson@15085
   322
great as any partial sum.*}
paulson@14416
   323
paulson@33271
   324
lemma pos_summable:
paulson@33271
   325
  fixes f:: "nat \<Rightarrow> real"
paulson@33271
   326
  assumes pos: "!!n. 0 \<le> f n" and le: "!!n. setsum f {0..<n} \<le> x"
paulson@33271
   327
  shows "summable f"
paulson@33271
   328
proof -
hoelzl@41970
   329
  have "convergent (\<lambda>n. setsum f {0..<n})"
paulson@33271
   330
    proof (rule Bseq_mono_convergent)
paulson@33271
   331
      show "Bseq (\<lambda>n. setsum f {0..<n})"
wenzelm@33536
   332
        by (rule f_inc_g_dec_Beq_f [of "(\<lambda>n. setsum f {0..<n})" "\<lambda>n. x"])
hoelzl@41970
   333
           (auto simp add: le pos)
hoelzl@41970
   334
    next
paulson@33271
   335
      show "\<forall>m n. m \<le> n \<longrightarrow> setsum f {0..<m} \<le> setsum f {0..<n}"
hoelzl@41970
   336
        by (auto intro: setsum_mono2 pos)
paulson@33271
   337
    qed
paulson@33271
   338
  then obtain L where "(%n. setsum f {0..<n}) ----> L"
paulson@33271
   339
    by (blast dest: convergentD)
paulson@33271
   340
  thus ?thesis
hoelzl@41970
   341
    by (force simp add: summable_def sums_def)
paulson@33271
   342
qed
paulson@33271
   343
huffman@20692
   344
lemma series_pos_le:
huffman@20692
   345
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   346
  shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 \<le> f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} \<le> suminf f"
paulson@14416
   347
apply (drule summable_sums)
paulson@14416
   348
apply (simp add: sums_def)
huffman@44568
   349
apply (cut_tac k = "setsum f {0..<n}" in tendsto_const)
nipkow@15539
   350
apply (erule LIMSEQ_le, blast)
huffman@20692
   351
apply (rule_tac x="n" in exI, clarify)
nipkow@15539
   352
apply (rule setsum_mono2)
nipkow@15539
   353
apply auto
paulson@14416
   354
done
paulson@14416
   355
paulson@14416
   356
lemma series_pos_less:
huffman@20692
   357
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   358
  shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 < f m\<rbrakk> \<Longrightarrow> setsum f {0..<n} < suminf f"
huffman@20692
   359
apply (rule_tac y="setsum f {0..<Suc n}" in order_less_le_trans)
huffman@20692
   360
apply simp
huffman@20692
   361
apply (erule series_pos_le)
huffman@20692
   362
apply (simp add: order_less_imp_le)
huffman@20692
   363
done
huffman@20692
   364
huffman@20692
   365
lemma suminf_gt_zero:
huffman@20692
   366
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   367
  shows "\<lbrakk>summable f; \<forall>n. 0 < f n\<rbrakk> \<Longrightarrow> 0 < suminf f"
huffman@20692
   368
by (drule_tac n="0" in series_pos_less, simp_all)
huffman@20692
   369
huffman@20692
   370
lemma suminf_ge_zero:
huffman@20692
   371
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   372
  shows "\<lbrakk>summable f; \<forall>n. 0 \<le> f n\<rbrakk> \<Longrightarrow> 0 \<le> suminf f"
huffman@20692
   373
by (drule_tac n="0" in series_pos_le, simp_all)
huffman@20692
   374
huffman@20692
   375
lemma sumr_pos_lt_pair:
huffman@20692
   376
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   377
  shows "\<lbrakk>summable f;
huffman@20692
   378
        \<forall>d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc(Suc 0) * d) + 1))\<rbrakk>
huffman@20692
   379
      \<Longrightarrow> setsum f {0..<k} < suminf f"
huffman@30082
   380
unfolding One_nat_def
huffman@20692
   381
apply (subst suminf_split_initial_segment [where k="k"])
huffman@20692
   382
apply assumption
huffman@20692
   383
apply simp
huffman@20692
   384
apply (drule_tac k="k" in summable_ignore_initial_segment)
huffman@44727
   385
apply (drule_tac k="Suc (Suc 0)" in sums_group [OF summable_sums], simp)
huffman@20692
   386
apply simp
huffman@20692
   387
apply (frule sums_unique)
huffman@20692
   388
apply (drule sums_summable)
huffman@20692
   389
apply simp
huffman@20692
   390
apply (erule suminf_gt_zero)
huffman@20692
   391
apply (simp add: add_ac)
paulson@14416
   392
done
paulson@14416
   393
paulson@15085
   394
text{*Sum of a geometric progression.*}
paulson@14416
   395
ballarin@17149
   396
lemmas sumr_geometric = geometric_sum [where 'a = real]
paulson@14416
   397
huffman@20692
   398
lemma geometric_sums:
haftmann@31017
   399
  fixes x :: "'a::{real_normed_field}"
huffman@20692
   400
  shows "norm x < 1 \<Longrightarrow> (\<lambda>n. x ^ n) sums (1 / (1 - x))"
huffman@20692
   401
proof -
huffman@20692
   402
  assume less_1: "norm x < 1"
huffman@20692
   403
  hence neq_1: "x \<noteq> 1" by auto
huffman@20692
   404
  hence neq_0: "x - 1 \<noteq> 0" by simp
huffman@20692
   405
  from less_1 have lim_0: "(\<lambda>n. x ^ n) ----> 0"
huffman@20692
   406
    by (rule LIMSEQ_power_zero)
huffman@22719
   407
  hence "(\<lambda>n. x ^ n / (x - 1) - 1 / (x - 1)) ----> 0 / (x - 1) - 1 / (x - 1)"
huffman@44568
   408
    using neq_0 by (intro tendsto_intros)
huffman@20692
   409
  hence "(\<lambda>n. (x ^ n - 1) / (x - 1)) ----> 1 / (1 - x)"
huffman@20692
   410
    by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
huffman@20692
   411
  thus "(\<lambda>n. x ^ n) sums (1 / (1 - x))"
huffman@20692
   412
    by (simp add: sums_def geometric_sum neq_1)
huffman@20692
   413
qed
huffman@20692
   414
huffman@20692
   415
lemma summable_geometric:
haftmann@31017
   416
  fixes x :: "'a::{real_normed_field}"
huffman@20692
   417
  shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
huffman@20692
   418
by (rule geometric_sums [THEN sums_summable])
paulson@14416
   419
huffman@47108
   420
lemma half: "0 < 1 / (2::'a::linordered_field)"
huffman@47108
   421
  by simp
paulson@33271
   422
paulson@33271
   423
lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
paulson@33271
   424
proof -
paulson@33271
   425
  have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
paulson@33271
   426
    by auto
paulson@33271
   427
  have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
paulson@33271
   428
    by simp
huffman@44282
   429
  thus ?thesis using sums_divide [OF 2, of 2]
paulson@33271
   430
    by simp
paulson@33271
   431
qed
paulson@33271
   432
paulson@15085
   433
text{*Cauchy-type criterion for convergence of series (c.f. Harrison)*}
paulson@15085
   434
nipkow@15539
   435
lemma summable_convergent_sumr_iff:
nipkow@15539
   436
 "summable f = convergent (%n. setsum f {0..<n})"
paulson@14416
   437
by (simp add: summable_def sums_def convergent_def)
paulson@14416
   438
hoelzl@41970
   439
lemma summable_LIMSEQ_zero:
huffman@44726
   440
  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
hoelzl@41970
   441
  shows "summable f \<Longrightarrow> f ----> 0"
huffman@20689
   442
apply (drule summable_convergent_sumr_iff [THEN iffD1])
huffman@20692
   443
apply (drule convergent_Cauchy)
huffman@31336
   444
apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
huffman@20689
   445
apply (drule_tac x="r" in spec, safe)
huffman@20689
   446
apply (rule_tac x="M" in exI, safe)
huffman@20689
   447
apply (drule_tac x="Suc n" in spec, simp)
huffman@20689
   448
apply (drule_tac x="n" in spec, simp)
huffman@20689
   449
done
huffman@20689
   450
paulson@32707
   451
lemma suminf_le:
paulson@32707
   452
  fixes x :: real
paulson@32707
   453
  shows "summable f \<Longrightarrow> (!!n. setsum f {0..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
hoelzl@41970
   454
  by (simp add: summable_convergent_sumr_iff suminf_eq_lim lim_le)
paulson@32707
   455
paulson@14416
   456
lemma summable_Cauchy:
hoelzl@41970
   457
     "summable (f::nat \<Rightarrow> 'a::banach) =
huffman@20848
   458
      (\<forall>e > 0. \<exists>N. \<forall>m \<ge> N. \<forall>n. norm (setsum f {m..<n}) < e)"
huffman@31336
   459
apply (simp only: summable_convergent_sumr_iff Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
huffman@20410
   460
apply (drule spec, drule (1) mp)
huffman@20410
   461
apply (erule exE, rule_tac x="M" in exI, clarify)
huffman@20410
   462
apply (rule_tac x="m" and y="n" in linorder_le_cases)
huffman@20410
   463
apply (frule (1) order_trans)
huffman@20410
   464
apply (drule_tac x="n" in spec, drule (1) mp)
huffman@20410
   465
apply (drule_tac x="m" in spec, drule (1) mp)
huffman@20410
   466
apply (simp add: setsum_diff [symmetric])
huffman@20410
   467
apply simp
huffman@20410
   468
apply (drule spec, drule (1) mp)
huffman@20410
   469
apply (erule exE, rule_tac x="N" in exI, clarify)
huffman@20410
   470
apply (rule_tac x="m" and y="n" in linorder_le_cases)
huffman@20552
   471
apply (subst norm_minus_commute)
huffman@20410
   472
apply (simp add: setsum_diff [symmetric])
huffman@20410
   473
apply (simp add: setsum_diff [symmetric])
paulson@14416
   474
done
paulson@14416
   475
paulson@15085
   476
text{*Comparison test*}
paulson@15085
   477
huffman@20692
   478
lemma norm_setsum:
huffman@20692
   479
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@20692
   480
  shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
huffman@20692
   481
apply (case_tac "finite A")
huffman@20692
   482
apply (erule finite_induct)
huffman@20692
   483
apply simp
huffman@20692
   484
apply simp
huffman@20692
   485
apply (erule order_trans [OF norm_triangle_ineq add_left_mono])
huffman@20692
   486
apply simp
huffman@20692
   487
done
huffman@20692
   488
paulson@14416
   489
lemma summable_comparison_test:
huffman@20848
   490
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   491
  shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f"
huffman@20692
   492
apply (simp add: summable_Cauchy, safe)
huffman@20692
   493
apply (drule_tac x="e" in spec, safe)
huffman@20692
   494
apply (rule_tac x = "N + Na" in exI, safe)
paulson@14416
   495
apply (rotate_tac 2)
paulson@14416
   496
apply (drule_tac x = m in spec)
paulson@14416
   497
apply (auto, rotate_tac 2, drule_tac x = n in spec)
huffman@20848
   498
apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
huffman@20848
   499
apply (rule norm_setsum)
nipkow@15539
   500
apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
huffman@22998
   501
apply (auto intro: setsum_mono simp add: abs_less_iff)
paulson@14416
   502
done
paulson@14416
   503
huffman@20848
   504
lemma summable_norm_comparison_test:
huffman@20848
   505
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   506
  shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk>
huffman@20848
   507
         \<Longrightarrow> summable (\<lambda>n. norm (f n))"
huffman@20848
   508
apply (rule summable_comparison_test)
huffman@20848
   509
apply (auto)
huffman@20848
   510
done
huffman@20848
   511
paulson@14416
   512
lemma summable_rabs_comparison_test:
huffman@20692
   513
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   514
  shows "\<lbrakk>\<exists>N. \<forall>n\<ge>N. \<bar>f n\<bar> \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable (\<lambda>n. \<bar>f n\<bar>)"
paulson@14416
   515
apply (rule summable_comparison_test)
nipkow@15543
   516
apply (auto)
paulson@14416
   517
done
paulson@14416
   518
huffman@23084
   519
text{*Summability of geometric series for real algebras*}
huffman@23084
   520
huffman@23084
   521
lemma complete_algebra_summable_geometric:
haftmann@31017
   522
  fixes x :: "'a::{real_normed_algebra_1,banach}"
huffman@23084
   523
  shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
huffman@23084
   524
proof (rule summable_comparison_test)
huffman@23084
   525
  show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
huffman@23084
   526
    by (simp add: norm_power_ineq)
huffman@23084
   527
  show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
huffman@23084
   528
    by (simp add: summable_geometric)
huffman@23084
   529
qed
huffman@23084
   530
paulson@15085
   531
text{*Limit comparison property for series (c.f. jrh)*}
paulson@15085
   532
paulson@14416
   533
lemma summable_le:
huffman@20692
   534
  fixes f g :: "nat \<Rightarrow> real"
huffman@20692
   535
  shows "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
paulson@14416
   536
apply (drule summable_sums)+
huffman@20692
   537
apply (simp only: sums_def, erule (1) LIMSEQ_le)
paulson@14416
   538
apply (rule exI)
nipkow@15539
   539
apply (auto intro!: setsum_mono)
paulson@14416
   540
done
paulson@14416
   541
paulson@14416
   542
lemma summable_le2:
huffman@20692
   543
  fixes f g :: "nat \<Rightarrow> real"
huffman@20692
   544
  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
   545
apply (subgoal_tac "summable f")
huffman@20848
   546
apply (auto intro!: summable_le)
huffman@22998
   547
apply (simp add: abs_le_iff)
huffman@20848
   548
apply (rule_tac g="g" in summable_comparison_test, simp_all)
paulson@14416
   549
done
paulson@14416
   550
kleing@19106
   551
(* specialisation for the common 0 case *)
kleing@19106
   552
lemma suminf_0_le:
kleing@19106
   553
  fixes f::"nat\<Rightarrow>real"
kleing@19106
   554
  assumes gt0: "\<forall>n. 0 \<le> f n" and sm: "summable f"
kleing@19106
   555
  shows "0 \<le> suminf f"
kleing@19106
   556
proof -
kleing@19106
   557
  let ?g = "(\<lambda>n. (0::real))"
kleing@19106
   558
  from gt0 have "\<forall>n. ?g n \<le> f n" by simp
kleing@19106
   559
  moreover have "summable ?g" by (rule summable_zero)
kleing@19106
   560
  moreover from sm have "summable f" .
kleing@19106
   561
  ultimately have "suminf ?g \<le> suminf f" by (rule summable_le)
huffman@44289
   562
  then show "0 \<le> suminf f" by simp
hoelzl@41970
   563
qed
kleing@19106
   564
kleing@19106
   565
paulson@15085
   566
text{*Absolute convergence imples normal convergence*}
huffman@20848
   567
lemma summable_norm_cancel:
huffman@20848
   568
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   569
  shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
huffman@20692
   570
apply (simp only: summable_Cauchy, safe)
huffman@20692
   571
apply (drule_tac x="e" in spec, safe)
huffman@20692
   572
apply (rule_tac x="N" in exI, safe)
huffman@20692
   573
apply (drule_tac x="m" in spec, safe)
huffman@20848
   574
apply (rule order_le_less_trans [OF norm_setsum])
huffman@20848
   575
apply (rule order_le_less_trans [OF abs_ge_self])
huffman@20692
   576
apply simp
paulson@14416
   577
done
paulson@14416
   578
huffman@20848
   579
lemma summable_rabs_cancel:
huffman@20848
   580
  fixes f :: "nat \<Rightarrow> real"
huffman@20848
   581
  shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> summable f"
huffman@20848
   582
by (rule summable_norm_cancel, simp)
huffman@20848
   583
paulson@15085
   584
text{*Absolute convergence of series*}
huffman@20848
   585
lemma summable_norm:
huffman@20848
   586
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   587
  shows "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
huffman@44568
   588
  by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel
huffman@20848
   589
                summable_sumr_LIMSEQ_suminf norm_setsum)
huffman@20848
   590
paulson@14416
   591
lemma summable_rabs:
huffman@20692
   592
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   593
  shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
huffman@20848
   594
by (fold real_norm_def, rule summable_norm)
paulson@14416
   595
paulson@14416
   596
subsection{* The Ratio Test*}
paulson@14416
   597
huffman@20848
   598
lemma norm_ratiotest_lemma:
huffman@22852
   599
  fixes x y :: "'a::real_normed_vector"
huffman@20848
   600
  shows "\<lbrakk>c \<le> 0; norm x \<le> c * norm y\<rbrakk> \<Longrightarrow> x = 0"
huffman@20848
   601
apply (subgoal_tac "norm x \<le> 0", simp)
huffman@20848
   602
apply (erule order_trans)
huffman@20848
   603
apply (simp add: mult_le_0_iff)
huffman@20848
   604
done
huffman@20848
   605
paulson@14416
   606
lemma rabs_ratiotest_lemma: "[| c \<le> 0; abs x \<le> c * abs y |] ==> x = (0::real)"
huffman@20848
   607
by (erule norm_ratiotest_lemma, simp)
paulson@14416
   608
paulson@14416
   609
lemma le_Suc_ex: "(k::nat) \<le> l ==> (\<exists>n. l = k + n)"
paulson@14416
   610
apply (drule le_imp_less_or_eq)
paulson@14416
   611
apply (auto dest: less_imp_Suc_add)
paulson@14416
   612
done
paulson@14416
   613
paulson@14416
   614
lemma le_Suc_ex_iff: "((k::nat) \<le> l) = (\<exists>n. l = k + n)"
paulson@14416
   615
by (auto simp add: le_Suc_ex)
paulson@14416
   616
paulson@14416
   617
(*All this trouble just to get 0<c *)
paulson@14416
   618
lemma ratio_test_lemma2:
huffman@20848
   619
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   620
  shows "\<lbrakk>\<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> 0 < c \<or> summable f"
paulson@14416
   621
apply (simp (no_asm) add: linorder_not_le [symmetric])
paulson@14416
   622
apply (simp add: summable_Cauchy)
nipkow@15543
   623
apply (safe, subgoal_tac "\<forall>n. N < n --> f (n) = 0")
nipkow@15543
   624
 prefer 2
nipkow@15543
   625
 apply clarify
huffman@30082
   626
 apply(erule_tac x = "n - Suc 0" in allE)
nipkow@15543
   627
 apply (simp add:diff_Suc split:nat.splits)
huffman@20848
   628
 apply (blast intro: norm_ratiotest_lemma)
paulson@14416
   629
apply (rule_tac x = "Suc N" in exI, clarify)
huffman@44710
   630
apply(simp cong del: setsum_cong cong: setsum_ivl_cong)
paulson@14416
   631
done
paulson@14416
   632
paulson@14416
   633
lemma ratio_test:
huffman@20848
   634
  fixes f :: "nat \<Rightarrow> 'a::banach"
huffman@20848
   635
  shows "\<lbrakk>c < 1; \<forall>n\<ge>N. norm (f (Suc n)) \<le> c * norm (f n)\<rbrakk> \<Longrightarrow> summable f"
paulson@14416
   636
apply (frule ratio_test_lemma2, auto)
hoelzl@41970
   637
apply (rule_tac g = "%n. (norm (f N) / (c ^ N))*c ^ n"
paulson@15234
   638
       in summable_comparison_test)
paulson@14416
   639
apply (rule_tac x = N in exI, safe)
paulson@14416
   640
apply (drule le_Suc_ex_iff [THEN iffD1])
huffman@22959
   641
apply (auto simp add: power_add field_power_not_zero)
nipkow@15539
   642
apply (induct_tac "na", auto)
huffman@20848
   643
apply (rule_tac y = "c * norm (f (N + n))" in order_trans)
paulson@14416
   644
apply (auto intro: mult_right_mono simp add: summable_def)
huffman@20848
   645
apply (rule_tac x = "norm (f N) * (1/ (1 - c)) / (c ^ N)" in exI)
hoelzl@41970
   646
apply (rule sums_divide)
haftmann@27108
   647
apply (rule sums_mult)
paulson@15234
   648
apply (auto intro!: geometric_sums)
paulson@14416
   649
done
paulson@14416
   650
huffman@23111
   651
subsection {* Cauchy Product Formula *}
huffman@23111
   652
huffman@23111
   653
(* Proof based on Analysis WebNotes: Chapter 07, Class 41
huffman@23111
   654
http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm *)
huffman@23111
   655
huffman@23111
   656
lemma setsum_triangle_reindex:
huffman@23111
   657
  fixes n :: nat
huffman@23111
   658
  shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k=0..<n. \<Sum>i=0..k. f i (k - i))"
huffman@23111
   659
proof -
huffman@23111
   660
  have "(\<Sum>(i, j)\<in>{(i, j). i + j < n}. f i j) =
huffman@23111
   661
    (\<Sum>(k, i)\<in>(SIGMA k:{0..<n}. {0..k}). f i (k - i))"
huffman@23111
   662
  proof (rule setsum_reindex_cong)
huffman@23111
   663
    show "inj_on (\<lambda>(k,i). (i, k - i)) (SIGMA k:{0..<n}. {0..k})"
huffman@23111
   664
      by (rule inj_on_inverseI [where g="\<lambda>(i,j). (i+j, i)"], auto)
huffman@23111
   665
    show "{(i,j). i + j < n} = (\<lambda>(k,i). (i, k - i)) ` (SIGMA k:{0..<n}. {0..k})"
huffman@23111
   666
      by (safe, rule_tac x="(a+b,a)" in image_eqI, auto)
huffman@23111
   667
    show "\<And>a. (\<lambda>(k, i). f i (k - i)) a = split f ((\<lambda>(k, i). (i, k - i)) a)"
huffman@23111
   668
      by clarify
huffman@23111
   669
  qed
huffman@23111
   670
  thus ?thesis by (simp add: setsum_Sigma)
huffman@23111
   671
qed
huffman@23111
   672
huffman@23111
   673
lemma Cauchy_product_sums:
huffman@23111
   674
  fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111
   675
  assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111
   676
  assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111
   677
  shows "(\<lambda>k. \<Sum>i=0..k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
huffman@23111
   678
proof -
huffman@23111
   679
  let ?S1 = "\<lambda>n::nat. {0..<n} \<times> {0..<n}"
huffman@23111
   680
  let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
huffman@23111
   681
  have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
huffman@23111
   682
  have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
huffman@23111
   683
  have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
huffman@23111
   684
  have finite_S1: "\<And>n. finite (?S1 n)" by simp
huffman@23111
   685
  with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
huffman@23111
   686
huffman@23111
   687
  let ?g = "\<lambda>(i,j). a i * b j"
huffman@23111
   688
  let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
huffman@23111
   689
  have f_nonneg: "\<And>x. 0 \<le> ?f x"
huffman@23111
   690
    by (auto simp add: mult_nonneg_nonneg)
huffman@23111
   691
  hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
huffman@23111
   692
    unfolding real_norm_def
huffman@23111
   693
    by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
huffman@23111
   694
huffman@23111
   695
  have "(\<lambda>n. (\<Sum>k=0..<n. a k) * (\<Sum>k=0..<n. b k))
huffman@23111
   696
           ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@44568
   697
    by (intro tendsto_mult summable_sumr_LIMSEQ_suminf
huffman@23111
   698
        summable_norm_cancel [OF a] summable_norm_cancel [OF b])
huffman@23111
   699
  hence 1: "(\<lambda>n. setsum ?g (?S1 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111
   700
    by (simp only: setsum_product setsum_Sigma [rule_format]
huffman@23111
   701
                   finite_atLeastLessThan)
huffman@23111
   702
huffman@23111
   703
  have "(\<lambda>n. (\<Sum>k=0..<n. norm (a k)) * (\<Sum>k=0..<n. norm (b k)))
huffman@23111
   704
       ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
huffman@44568
   705
    using a b by (intro tendsto_mult summable_sumr_LIMSEQ_suminf)
huffman@23111
   706
  hence "(\<lambda>n. setsum ?f (?S1 n)) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
huffman@23111
   707
    by (simp only: setsum_product setsum_Sigma [rule_format]
huffman@23111
   708
                   finite_atLeastLessThan)
huffman@23111
   709
  hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111
   710
    by (rule convergentI)
huffman@23111
   711
  hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111
   712
    by (rule convergent_Cauchy)
huffman@36657
   713
  have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
huffman@36657
   714
  proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
huffman@23111
   715
    fix r :: real
huffman@23111
   716
    assume r: "0 < r"
huffman@23111
   717
    from CauchyD [OF Cauchy r] obtain N
huffman@23111
   718
    where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
huffman@23111
   719
    hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
huffman@23111
   720
      by (simp only: setsum_diff finite_S1 S1_mono)
huffman@23111
   721
    hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
huffman@23111
   722
      by (simp only: norm_setsum_f)
huffman@23111
   723
    show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
huffman@23111
   724
    proof (intro exI allI impI)
huffman@23111
   725
      fix n assume "2 * N \<le> n"
huffman@23111
   726
      hence n: "N \<le> n div 2" by simp
huffman@23111
   727
      have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
huffman@23111
   728
        by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
huffman@23111
   729
                  Diff_mono subset_refl S1_le_S2)
huffman@23111
   730
      also have "\<dots> < r"
huffman@23111
   731
        using n div_le_dividend by (rule N)
huffman@23111
   732
      finally show "setsum ?f (?S1 n - ?S2 n) < r" .
huffman@23111
   733
    qed
huffman@23111
   734
  qed
huffman@36657
   735
  hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
huffman@36657
   736
    apply (rule Zfun_le [rule_format])
huffman@23111
   737
    apply (simp only: norm_setsum_f)
huffman@23111
   738
    apply (rule order_trans [OF norm_setsum setsum_mono])
huffman@23111
   739
    apply (auto simp add: norm_mult_ineq)
huffman@23111
   740
    done
huffman@23111
   741
  hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) ----> 0"
huffman@36660
   742
    unfolding tendsto_Zfun_iff diff_0_right
huffman@36657
   743
    by (simp only: setsum_diff finite_S1 S2_le_S1)
huffman@23111
   744
huffman@23111
   745
  with 1 have "(\<lambda>n. setsum ?g (?S2 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111
   746
    by (rule LIMSEQ_diff_approach_zero2)
huffman@23111
   747
  thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
huffman@23111
   748
qed
huffman@23111
   749
huffman@23111
   750
lemma Cauchy_product:
huffman@23111
   751
  fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111
   752
  assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111
   753
  assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111
   754
  shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i=0..k. a i * b (k - i))"
huffman@23441
   755
using a b
huffman@23111
   756
by (rule Cauchy_product_sums [THEN sums_unique])
huffman@23111
   757
paulson@14416
   758
end