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