src/HOL/Series.thy
author hoelzl
Tue Mar 18 15:53:48 2014 +0100 (2014-03-18)
changeset 56193 c726ecfb22b6
parent 56178 2a6f58938573
child 56194 9ffbb4004c81
permissions -rw-r--r--
cleanup Series: sorted according to typeclass hierarchy, use {..<_} instead of {0..<_}
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
hoelzl@56193
    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
hoelzl@56193
    16
(* TODO: MOVE *)
hoelzl@56193
    17
lemma Suc_less_iff: "Suc n < m \<longleftrightarrow> (\<exists>m'. m = Suc m' \<and> n < m')"
hoelzl@56193
    18
  by (cases m) auto
paulson@32877
    19
hoelzl@56193
    20
(* TODO: MOVE *)
hoelzl@56193
    21
lemma norm_ratiotest_lemma:
hoelzl@56193
    22
  fixes x y :: "'a::real_normed_vector"
hoelzl@56193
    23
  shows "\<lbrakk>c \<le> 0; norm x \<le> c * norm y\<rbrakk> \<Longrightarrow> x = 0"
hoelzl@56193
    24
apply (subgoal_tac "norm x \<le> 0", simp)
hoelzl@56193
    25
apply (erule order_trans)
hoelzl@56193
    26
apply (simp add: mult_le_0_iff)
paulson@14416
    27
done
nipkow@15539
    28
hoelzl@56193
    29
(* TODO: MOVE *)
hoelzl@56193
    30
lemma rabs_ratiotest_lemma: "[| c \<le> 0; abs x \<le> c * abs y |] ==> x = (0::real)"
hoelzl@56193
    31
by (erule norm_ratiotest_lemma, simp)
hoelzl@56193
    32
hoelzl@56193
    33
(* TODO: MOVE *)
hoelzl@56193
    34
lemma le_Suc_ex: "(k::nat) \<le> l ==> (\<exists>n. l = k + n)"
hoelzl@56193
    35
apply (drule le_imp_less_or_eq)
hoelzl@56193
    36
apply (auto dest: less_imp_Suc_add)
huffman@20692
    37
done
huffman@20692
    38
hoelzl@56193
    39
(* MOVE *)
hoelzl@56193
    40
lemma setsum_even_minus_one [simp]: "(\<Sum>i<2 * n. (-1) ^ Suc i) = (0::'a::ring_1)"
hoelzl@56193
    41
  by (induct "n") auto
hoelzl@56193
    42
hoelzl@56193
    43
(* MOVE *)
hoelzl@56193
    44
lemma setsum_nat_group: "(\<Sum>m<n::nat. setsum f {m * k ..< m*k + k}) = setsum f {..< n * k}"
hoelzl@56193
    45
  apply (subgoal_tac "k = 0 | 0 < k", auto)
hoelzl@56193
    46
  apply (induct "n")
hoelzl@56193
    47
  apply (simp_all add: setsum_add_nat_ivl add_commute atLeast0LessThan[symmetric])
hoelzl@56193
    48
  done
avigad@16819
    49
hoelzl@56193
    50
(* MOVE *)
hoelzl@56193
    51
lemma norm_setsum:
hoelzl@56193
    52
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56193
    53
  shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
hoelzl@56193
    54
  apply (case_tac "finite A")
hoelzl@56193
    55
  apply (erule finite_induct)
hoelzl@56193
    56
  apply simp
hoelzl@56193
    57
  apply simp
hoelzl@56193
    58
  apply (erule order_trans [OF norm_triangle_ineq add_left_mono])
hoelzl@56193
    59
  apply simp
hoelzl@56193
    60
  done
avigad@16819
    61
hoelzl@56193
    62
(* MOVE *)
hoelzl@56193
    63
lemma norm_bound_subset:
hoelzl@56193
    64
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56193
    65
  assumes "finite s" "t \<subseteq> s"
hoelzl@56193
    66
  assumes le: "(\<And>x. x \<in> s \<Longrightarrow> norm(f x) \<le> g x)"
hoelzl@56193
    67
  shows "norm (setsum f t) \<le> setsum g s"
hoelzl@56193
    68
proof -
hoelzl@56193
    69
  have "norm (setsum f t) \<le> (\<Sum>i\<in>t. norm (f i))"
hoelzl@56193
    70
    by (rule norm_setsum)
hoelzl@56193
    71
  also have "\<dots> \<le> (\<Sum>i\<in>t. g i)"
hoelzl@56193
    72
    using assms by (auto intro!: setsum_mono)
hoelzl@56193
    73
  also have "\<dots> \<le> setsum g s"
hoelzl@56193
    74
    using assms order.trans[OF norm_ge_zero le]
hoelzl@56193
    75
    by (auto intro!: setsum_mono3)
hoelzl@56193
    76
  finally show ?thesis .
hoelzl@56193
    77
qed
avigad@16819
    78
hoelzl@56193
    79
(* MOVE *)
hoelzl@56193
    80
lemma (in linorder) lessThan_minus_lessThan [simp]:
hoelzl@56193
    81
  "{..< n} - {..< m} = {m ..< n}"
hoelzl@56193
    82
  by auto
hoelzl@56193
    83
hoelzl@56193
    84
definition
hoelzl@56193
    85
  sums :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a \<Rightarrow> bool"
hoelzl@56193
    86
  (infixr "sums" 80)
hoelzl@56193
    87
where
hoelzl@56193
    88
  "f sums s \<longleftrightarrow> (\<lambda>n. \<Sum>i<n. f i) ----> s"
paulson@14416
    89
hoelzl@56193
    90
definition summable :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> bool" where
hoelzl@56193
    91
   "summable f \<longleftrightarrow> (\<exists>s. f sums s)"
hoelzl@56193
    92
hoelzl@56193
    93
definition
hoelzl@56193
    94
  suminf :: "(nat \<Rightarrow> 'a::{topological_space, comm_monoid_add}) \<Rightarrow> 'a"
hoelzl@56193
    95
  (binder "\<Sum>" 10)
hoelzl@56193
    96
where
hoelzl@56193
    97
  "suminf f = (THE s. f sums s)"
hoelzl@56193
    98
hoelzl@56193
    99
lemma sums_subst[trans]: "f = g \<Longrightarrow> g sums z \<Longrightarrow> f sums z"
hoelzl@56193
   100
  by simp
hoelzl@56193
   101
hoelzl@56193
   102
lemma sums_summable: "f sums l \<Longrightarrow> summable f"
hoelzl@41970
   103
  by (simp add: sums_def summable_def, blast)
paulson@14416
   104
hoelzl@56193
   105
lemma summable_iff_convergent: "summable f \<longleftrightarrow> convergent (\<lambda>n. \<Sum>i<n. f i)"
hoelzl@56193
   106
  by (simp add: summable_def sums_def convergent_def)
paulson@14416
   107
hoelzl@56193
   108
lemma suminf_eq_lim: "suminf f = lim (\<lambda>n. \<Sum>i<n. f i)"
hoelzl@41970
   109
  by (simp add: suminf_def sums_def lim_def)
paulson@32707
   110
hoelzl@47761
   111
lemma sums_finite:
hoelzl@56193
   112
  assumes [simp]: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
hoelzl@47761
   113
  shows "f sums (\<Sum>n\<in>N. f n)"
hoelzl@47761
   114
proof -
hoelzl@47761
   115
  { fix n
hoelzl@47761
   116
    have "setsum f {..<n + Suc (Max N)} = setsum f N"
hoelzl@47761
   117
    proof cases
hoelzl@47761
   118
      assume "N = {}"
hoelzl@47761
   119
      with f have "f = (\<lambda>x. 0)" by auto
hoelzl@47761
   120
      then show ?thesis by simp
hoelzl@47761
   121
    next
hoelzl@47761
   122
      assume [simp]: "N \<noteq> {}"
hoelzl@47761
   123
      show ?thesis
hoelzl@47761
   124
      proof (safe intro!: setsum_mono_zero_right f)
hoelzl@47761
   125
        fix i assume "i \<in> N"
hoelzl@47761
   126
        then have "i \<le> Max N" by simp
hoelzl@47761
   127
        then show "i < n + Suc (Max N)" by simp
hoelzl@47761
   128
      qed
hoelzl@47761
   129
    qed }
hoelzl@47761
   130
  note eq = this
hoelzl@47761
   131
  show ?thesis unfolding sums_def
hoelzl@47761
   132
    by (rule LIMSEQ_offset[of _ "Suc (Max N)"])
hoelzl@47761
   133
       (simp add: eq atLeast0LessThan tendsto_const del: add_Suc_right)
hoelzl@47761
   134
qed
hoelzl@47761
   135
hoelzl@56193
   136
lemma sums_If_finite_set: "finite A \<Longrightarrow> (\<lambda>r. if r \<in> A then f r else 0) sums (\<Sum>r\<in>A. f r)"
hoelzl@47761
   137
  using sums_finite[of A "(\<lambda>r. if r \<in> A then f r else 0)"] by simp
hoelzl@47761
   138
hoelzl@56193
   139
lemma sums_If_finite: "finite {r. P r} \<Longrightarrow> (\<lambda>r. if P r then f r else 0) sums (\<Sum>r | P r. f r)"
hoelzl@56193
   140
  using sums_If_finite_set[of "{r. P r}"] by simp
avigad@16819
   141
hoelzl@56193
   142
lemma sums_single: "(\<lambda>r. if r = i then f r else 0) sums f i"
hoelzl@56193
   143
  using sums_If_finite[of "\<lambda>r. r = i"] by simp
hoelzl@29803
   144
hoelzl@56193
   145
lemma series_zero: (* REMOVE *)
hoelzl@56193
   146
  "(\<And>m. n \<le> m \<Longrightarrow> f m = 0) \<Longrightarrow> f sums (\<Sum>i<n. f i)"
hoelzl@56193
   147
  by (rule sums_finite) auto
paulson@14416
   148
hoelzl@41970
   149
lemma sums_zero[simp, intro]: "(\<lambda>n. 0) sums 0"
huffman@44568
   150
  unfolding sums_def by (simp add: tendsto_const)
nipkow@15539
   151
hoelzl@41970
   152
lemma summable_zero[simp, intro]: "summable (\<lambda>n. 0)"
hoelzl@56193
   153
  by (rule sums_zero [THEN sums_summable])
hoelzl@56193
   154
hoelzl@56193
   155
lemma sums_group: "f sums s \<Longrightarrow> 0 < k \<Longrightarrow> (\<lambda>n. setsum f {n * k ..< n * k + k}) sums s"
hoelzl@56193
   156
  apply (simp only: sums_def setsum_nat_group tendsto_def eventually_sequentially)
hoelzl@56193
   157
  apply safe
hoelzl@56193
   158
  apply (erule_tac x=S in allE)
hoelzl@56193
   159
  apply safe
hoelzl@56193
   160
  apply (rule_tac x="N" in exI, safe)
hoelzl@56193
   161
  apply (drule_tac x="n*k" in spec)
hoelzl@56193
   162
  apply (erule mp)
hoelzl@56193
   163
  apply (erule order_trans)
hoelzl@56193
   164
  apply simp
hoelzl@56193
   165
  done
hoelzl@56193
   166
hoelzl@56193
   167
context
hoelzl@56193
   168
  fixes f :: "nat \<Rightarrow> 'a::{t2_space, comm_monoid_add}"
hoelzl@56193
   169
begin
hoelzl@56193
   170
hoelzl@56193
   171
lemma summable_sums[intro]: "summable f \<Longrightarrow> f sums (suminf f)"
hoelzl@56193
   172
  by (simp add: summable_def sums_def suminf_def)
hoelzl@56193
   173
     (metis convergent_LIMSEQ_iff convergent_def lim_def)
hoelzl@56193
   174
hoelzl@56193
   175
lemma summable_LIMSEQ: "summable f \<Longrightarrow> (\<lambda>n. \<Sum>i<n. f i) ----> suminf f"
hoelzl@56193
   176
  by (rule summable_sums [unfolded sums_def])
hoelzl@56193
   177
hoelzl@56193
   178
lemma sums_unique: "f sums s \<Longrightarrow> s = suminf f"
hoelzl@56193
   179
  by (metis limI suminf_eq_lim sums_def)
hoelzl@56193
   180
hoelzl@56193
   181
lemma sums_iff: "f sums x \<longleftrightarrow> summable f \<and> (suminf f = x)"
hoelzl@56193
   182
  by (metis summable_sums sums_summable sums_unique)
hoelzl@56193
   183
hoelzl@56193
   184
lemma suminf_finite:
hoelzl@56193
   185
  assumes N: "finite N" and f: "\<And>n. n \<notin> N \<Longrightarrow> f n = 0"
hoelzl@56193
   186
  shows "suminf f = (\<Sum>n\<in>N. f n)"
hoelzl@56193
   187
  using sums_finite[OF assms, THEN sums_unique] by simp
hoelzl@56193
   188
hoelzl@56193
   189
end
avigad@16819
   190
hoelzl@41970
   191
lemma suminf_zero[simp]: "suminf (\<lambda>n. 0::'a::{t2_space, comm_monoid_add}) = 0"
hoelzl@56193
   192
  by (rule sums_zero [THEN sums_unique, symmetric])
avigad@16819
   193
hoelzl@56193
   194
context
hoelzl@56193
   195
  fixes f :: "nat \<Rightarrow> 'a::{ordered_comm_monoid_add, linorder_topology}"
hoelzl@56193
   196
begin
paulson@14416
   197
hoelzl@56193
   198
lemma series_pos_le: "summable f \<Longrightarrow> \<forall>m\<ge>n. 0 \<le> f m \<Longrightarrow> setsum f {..<n} \<le> suminf f"
hoelzl@56193
   199
  apply (rule LIMSEQ_le_const[OF summable_LIMSEQ])
hoelzl@50999
   200
  apply assumption
hoelzl@50999
   201
  apply (intro exI[of _ n])
hoelzl@56193
   202
  apply (auto intro!: setsum_mono2 simp: not_le[symmetric])
hoelzl@50999
   203
  done
paulson@14416
   204
hoelzl@56193
   205
lemma suminf_eq_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> suminf f = 0 \<longleftrightarrow> (\<forall>n. f n = 0)"
hoelzl@50999
   206
proof
hoelzl@50999
   207
  assume "summable f" "suminf f = 0" and pos: "\<forall>n. 0 \<le> f n"
hoelzl@50999
   208
  then have "f sums 0"
hoelzl@50999
   209
    by (simp add: sums_iff)
hoelzl@50999
   210
  then have f: "(\<lambda>n. \<Sum>i<n. f i) ----> 0"
hoelzl@50999
   211
    by (simp add: sums_def atLeast0LessThan)
hoelzl@50999
   212
  have "\<And>i. (\<Sum>n\<in>{i}. f n) \<le> 0"
hoelzl@50999
   213
  proof (rule LIMSEQ_le_const[OF f])
hoelzl@50999
   214
    fix i show "\<exists>N. \<forall>n\<ge>N. (\<Sum>n\<in>{i}. f n) \<le> setsum f {..<n}"
hoelzl@50999
   215
      using pos by (intro exI[of _ "Suc i"] allI impI setsum_mono2) auto
hoelzl@50999
   216
  qed
hoelzl@50999
   217
  with pos show "\<forall>n. f n = 0"
hoelzl@50999
   218
    by (auto intro!: antisym)
hoelzl@56193
   219
qed (metis suminf_zero fun_eq_iff)
hoelzl@56193
   220
hoelzl@56193
   221
lemma suminf_gt_zero_iff: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 < suminf f \<longleftrightarrow> (\<exists>i. 0 < f i)"
hoelzl@56193
   222
  using series_pos_le[of 0] suminf_eq_zero_iff by (simp add: less_le)
hoelzl@56193
   223
hoelzl@56193
   224
lemma suminf_gt_zero: "summable f \<Longrightarrow> \<forall>n. 0 < f n \<Longrightarrow> 0 < suminf f"
hoelzl@56193
   225
  using suminf_gt_zero_iff by (simp add: less_imp_le)
hoelzl@56193
   226
hoelzl@56193
   227
lemma suminf_ge_zero: "summable f \<Longrightarrow> \<forall>n. 0 \<le> f n \<Longrightarrow> 0 \<le> suminf f"
hoelzl@56193
   228
  by (drule_tac n="0" in series_pos_le) simp_all
hoelzl@56193
   229
hoelzl@56193
   230
lemma suminf_le: "summable f \<Longrightarrow> (\<And>n. setsum f {..<n} \<le> x) \<Longrightarrow> suminf f \<le> x"
hoelzl@56193
   231
  by (metis LIMSEQ_le_const2 summable_LIMSEQ)
hoelzl@56193
   232
hoelzl@56193
   233
lemma summable_le: "\<lbrakk>\<forall>n. f n \<le> g n; summable f; summable g\<rbrakk> \<Longrightarrow> suminf f \<le> suminf g"
hoelzl@56193
   234
  by (rule LIMSEQ_le) (auto intro: setsum_mono summable_LIMSEQ)
hoelzl@56193
   235
hoelzl@56193
   236
end
hoelzl@56193
   237
hoelzl@56193
   238
lemma series_pos_less:
hoelzl@56193
   239
  fixes f :: "nat \<Rightarrow> 'a::{ordered_ab_semigroup_add_imp_le, ordered_comm_monoid_add, linorder_topology}"
hoelzl@56193
   240
  shows "\<lbrakk>summable f; \<forall>m\<ge>n. 0 < f m\<rbrakk> \<Longrightarrow> setsum f {..<n} < suminf f"
hoelzl@56193
   241
  apply simp
hoelzl@56193
   242
  apply (rule_tac y="setsum f {..<Suc n}" in order_less_le_trans)
hoelzl@56193
   243
  using add_less_cancel_left [of "setsum f {..<n}" 0 "f n"]
hoelzl@56193
   244
  apply simp
hoelzl@56193
   245
  apply (erule series_pos_le)
hoelzl@56193
   246
  apply (simp add: order_less_imp_le)
hoelzl@56193
   247
  done
hoelzl@56193
   248
hoelzl@56193
   249
lemma sums_Suc_iff:
hoelzl@56193
   250
  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
hoelzl@56193
   251
  shows "(\<lambda>n. f (Suc n)) sums s \<longleftrightarrow> f sums (s + f 0)"
hoelzl@56193
   252
proof -
hoelzl@56193
   253
  have "f sums (s + f 0) \<longleftrightarrow> (\<lambda>i. \<Sum>j<Suc i. f j) ----> s + f 0"
hoelzl@56193
   254
    by (subst LIMSEQ_Suc_iff) (simp add: sums_def)
hoelzl@56193
   255
  also have "\<dots> \<longleftrightarrow> (\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) ----> s + f 0"
hoelzl@56193
   256
    by (simp add: ac_simps setsum_reindex image_iff lessThan_Suc_eq_insert_0)
hoelzl@56193
   257
  also have "\<dots> \<longleftrightarrow> (\<lambda>n. f (Suc n)) sums s"
hoelzl@56193
   258
  proof
hoelzl@56193
   259
    assume "(\<lambda>i. (\<Sum>j<i. f (Suc j)) + f 0) ----> s + f 0"
hoelzl@56193
   260
    with tendsto_add[OF this tendsto_const, of "- f 0"]
hoelzl@56193
   261
    show "(\<lambda>i. f (Suc i)) sums s"
hoelzl@56193
   262
      by (simp add: sums_def)
hoelzl@56193
   263
  qed (auto intro: tendsto_add tendsto_const simp: sums_def)
hoelzl@56193
   264
  finally show ?thesis ..
hoelzl@50999
   265
qed
hoelzl@50999
   266
hoelzl@56193
   267
context
hoelzl@56193
   268
  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
hoelzl@56193
   269
begin
hoelzl@56193
   270
hoelzl@56193
   271
lemma sums_add: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n + g n) sums (a + b)"
hoelzl@56193
   272
  unfolding sums_def by (simp add: setsum_addf tendsto_add)
hoelzl@56193
   273
hoelzl@56193
   274
lemma summable_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n + g n)"
hoelzl@56193
   275
  unfolding summable_def by (auto intro: sums_add)
hoelzl@56193
   276
hoelzl@56193
   277
lemma suminf_add: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f + suminf g = (\<Sum>n. f n + g n)"
hoelzl@56193
   278
  by (intro sums_unique sums_add summable_sums)
hoelzl@56193
   279
hoelzl@56193
   280
lemma sums_diff: "f sums a \<Longrightarrow> g sums b \<Longrightarrow> (\<lambda>n. f n - g n) sums (a - b)"
hoelzl@56193
   281
  unfolding sums_def by (simp add: setsum_subtractf tendsto_diff)
hoelzl@56193
   282
hoelzl@56193
   283
lemma summable_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> summable (\<lambda>n. f n - g n)"
hoelzl@56193
   284
  unfolding summable_def by (auto intro: sums_diff)
hoelzl@56193
   285
hoelzl@56193
   286
lemma suminf_diff: "summable f \<Longrightarrow> summable g \<Longrightarrow> suminf f - suminf g = (\<Sum>n. f n - g n)"
hoelzl@56193
   287
  by (intro sums_unique sums_diff summable_sums)
hoelzl@56193
   288
hoelzl@56193
   289
lemma sums_minus: "f sums a \<Longrightarrow> (\<lambda>n. - f n) sums (- a)"
hoelzl@56193
   290
  unfolding sums_def by (simp add: setsum_negf tendsto_minus)
hoelzl@56193
   291
hoelzl@56193
   292
lemma summable_minus: "summable f \<Longrightarrow> summable (\<lambda>n. - f n)"
hoelzl@56193
   293
  unfolding summable_def by (auto intro: sums_minus)
huffman@20692
   294
hoelzl@56193
   295
lemma suminf_minus: "summable f \<Longrightarrow> (\<Sum>n. - f n) = - (\<Sum>n. f n)"
hoelzl@56193
   296
  by (intro sums_unique [symmetric] sums_minus summable_sums)
hoelzl@56193
   297
hoelzl@56193
   298
lemma sums_Suc: "(\<lambda> n. f (Suc n)) sums l \<Longrightarrow> f sums (l + f 0)"
hoelzl@56193
   299
  by (simp add: sums_Suc_iff)
hoelzl@56193
   300
hoelzl@56193
   301
lemma sums_iff_shift: "(\<lambda>i. f (i + n)) sums s \<longleftrightarrow> f sums (s + (\<Sum>i<n. f i))"
hoelzl@56193
   302
proof (induct n arbitrary: s)
hoelzl@56193
   303
  case (Suc n)
hoelzl@56193
   304
  moreover have "(\<lambda>i. f (Suc i + n)) sums s \<longleftrightarrow> (\<lambda>i. f (i + n)) sums (s + f n)"
hoelzl@56193
   305
    by (subst sums_Suc_iff) simp
hoelzl@56193
   306
  ultimately show ?case
hoelzl@56193
   307
    by (simp add: ac_simps)
hoelzl@56193
   308
qed simp
huffman@20692
   309
hoelzl@56193
   310
lemma summable_iff_shift: "summable (\<lambda>n. f (n + k)) \<longleftrightarrow> summable f"
hoelzl@56193
   311
  by (metis diff_add_cancel summable_def sums_iff_shift[abs_def])
hoelzl@56193
   312
hoelzl@56193
   313
lemma sums_split_initial_segment: "f sums s \<Longrightarrow> (\<lambda>i. f (i + n)) sums (s - (\<Sum>i<n. f i))"
hoelzl@56193
   314
  by (simp add: sums_iff_shift)
hoelzl@56193
   315
hoelzl@56193
   316
lemma summable_ignore_initial_segment: "summable f \<Longrightarrow> summable (\<lambda>n. f(n + k))"
hoelzl@56193
   317
  by (simp add: summable_iff_shift)
hoelzl@56193
   318
hoelzl@56193
   319
lemma suminf_minus_initial_segment: "summable f \<Longrightarrow> (\<Sum>n. f (n + k)) = (\<Sum>n. f n) - (\<Sum>i<k. f i)"
hoelzl@56193
   320
  by (rule sums_unique[symmetric]) (auto simp: sums_iff_shift)
hoelzl@56193
   321
hoelzl@56193
   322
lemma suminf_split_initial_segment: "summable f \<Longrightarrow> suminf f = (\<Sum>n. f(n + k)) + (\<Sum>i<k. f i)"
hoelzl@56193
   323
  by (auto simp add: suminf_minus_initial_segment)
huffman@20692
   324
hoelzl@56193
   325
lemma suminf_exist_split: 
hoelzl@56193
   326
  fixes r :: real assumes "0 < r" and "summable f"
hoelzl@56193
   327
  shows "\<exists>N. \<forall>n\<ge>N. norm (\<Sum>i. f (i + n)) < r"
hoelzl@56193
   328
proof -
hoelzl@56193
   329
  from LIMSEQ_D[OF summable_LIMSEQ[OF `summable f`] `0 < r`]
hoelzl@56193
   330
  obtain N :: nat where "\<forall> n \<ge> N. norm (setsum f {..<n} - suminf f) < r" by auto
hoelzl@56193
   331
  thus ?thesis
hoelzl@56193
   332
    by (auto simp: norm_minus_commute suminf_minus_initial_segment[OF `summable f`])
hoelzl@56193
   333
qed
hoelzl@56193
   334
hoelzl@56193
   335
lemma summable_LIMSEQ_zero: "summable f \<Longrightarrow> f ----> 0"
hoelzl@56193
   336
  apply (drule summable_iff_convergent [THEN iffD1])
hoelzl@56193
   337
  apply (drule convergent_Cauchy)
hoelzl@56193
   338
  apply (simp only: Cauchy_iff LIMSEQ_iff, safe)
hoelzl@56193
   339
  apply (drule_tac x="r" in spec, safe)
hoelzl@56193
   340
  apply (rule_tac x="M" in exI, safe)
hoelzl@56193
   341
  apply (drule_tac x="Suc n" in spec, simp)
hoelzl@56193
   342
  apply (drule_tac x="n" in spec, simp)
hoelzl@56193
   343
  done
hoelzl@56193
   344
hoelzl@56193
   345
end
hoelzl@56193
   346
hoelzl@56193
   347
lemma (in bounded_linear) sums: "(\<lambda>n. X n) sums a \<Longrightarrow> (\<lambda>n. f (X n)) sums (f a)"
hoelzl@56193
   348
  unfolding sums_def by (drule tendsto, simp only: setsum)
hoelzl@56193
   349
hoelzl@56193
   350
lemma (in bounded_linear) summable: "summable (\<lambda>n. X n) \<Longrightarrow> summable (\<lambda>n. f (X n))"
hoelzl@56193
   351
  unfolding summable_def by (auto intro: sums)
hoelzl@56193
   352
hoelzl@56193
   353
lemma (in bounded_linear) suminf: "summable (\<lambda>n. X n) \<Longrightarrow> f (\<Sum>n. X n) = (\<Sum>n. f (X n))"
hoelzl@56193
   354
  by (intro sums_unique sums summable_sums)
hoelzl@56193
   355
hoelzl@56193
   356
lemmas sums_of_real = bounded_linear.sums [OF bounded_linear_of_real]
hoelzl@56193
   357
lemmas summable_of_real = bounded_linear.summable [OF bounded_linear_of_real]
hoelzl@56193
   358
lemmas suminf_of_real = bounded_linear.suminf [OF bounded_linear_of_real]
hoelzl@56193
   359
hoelzl@56193
   360
context
hoelzl@56193
   361
  fixes f :: "nat \<Rightarrow> 'a::real_normed_algebra"
hoelzl@56193
   362
begin
hoelzl@56193
   363
hoelzl@56193
   364
lemma sums_mult: "f sums a \<Longrightarrow> (\<lambda>n. c * f n) sums (c * a)"
hoelzl@56193
   365
  by (rule bounded_linear.sums [OF bounded_linear_mult_right])
hoelzl@56193
   366
hoelzl@56193
   367
lemma summable_mult: "summable f \<Longrightarrow> summable (\<lambda>n. c * f n)"
hoelzl@56193
   368
  by (rule bounded_linear.summable [OF bounded_linear_mult_right])
hoelzl@56193
   369
hoelzl@56193
   370
lemma suminf_mult: "summable f \<Longrightarrow> suminf (\<lambda>n. c * f n) = c * suminf f"
hoelzl@56193
   371
  by (rule bounded_linear.suminf [OF bounded_linear_mult_right, symmetric])
hoelzl@56193
   372
hoelzl@56193
   373
lemma sums_mult2: "f sums a \<Longrightarrow> (\<lambda>n. f n * c) sums (a * c)"
hoelzl@56193
   374
  by (rule bounded_linear.sums [OF bounded_linear_mult_left])
hoelzl@56193
   375
hoelzl@56193
   376
lemma summable_mult2: "summable f \<Longrightarrow> summable (\<lambda>n. f n * c)"
hoelzl@56193
   377
  by (rule bounded_linear.summable [OF bounded_linear_mult_left])
hoelzl@56193
   378
hoelzl@56193
   379
lemma suminf_mult2: "summable f \<Longrightarrow> suminf f * c = (\<Sum>n. f n * c)"
hoelzl@56193
   380
  by (rule bounded_linear.suminf [OF bounded_linear_mult_left])
hoelzl@56193
   381
hoelzl@56193
   382
end
hoelzl@56193
   383
hoelzl@56193
   384
context
hoelzl@56193
   385
  fixes c :: "'a::real_normed_field"
hoelzl@56193
   386
begin
hoelzl@56193
   387
hoelzl@56193
   388
lemma sums_divide: "f sums a \<Longrightarrow> (\<lambda>n. f n / c) sums (a / c)"
hoelzl@56193
   389
  by (rule bounded_linear.sums [OF bounded_linear_divide])
hoelzl@56193
   390
hoelzl@56193
   391
lemma summable_divide: "summable f \<Longrightarrow> summable (\<lambda>n. f n / c)"
hoelzl@56193
   392
  by (rule bounded_linear.summable [OF bounded_linear_divide])
hoelzl@56193
   393
hoelzl@56193
   394
lemma suminf_divide: "summable f \<Longrightarrow> suminf (\<lambda>n. f n / c) = suminf f / c"
hoelzl@56193
   395
  by (rule bounded_linear.suminf [OF bounded_linear_divide, symmetric])
paulson@14416
   396
paulson@15085
   397
text{*Sum of a geometric progression.*}
paulson@14416
   398
hoelzl@56193
   399
lemma geometric_sums: "norm c < 1 \<Longrightarrow> (\<lambda>n. c^n) sums (1 / (1 - c))"
huffman@20692
   400
proof -
hoelzl@56193
   401
  assume less_1: "norm c < 1"
hoelzl@56193
   402
  hence neq_1: "c \<noteq> 1" by auto
hoelzl@56193
   403
  hence neq_0: "c - 1 \<noteq> 0" by simp
hoelzl@56193
   404
  from less_1 have lim_0: "(\<lambda>n. c^n) ----> 0"
huffman@20692
   405
    by (rule LIMSEQ_power_zero)
hoelzl@56193
   406
  hence "(\<lambda>n. c ^ n / (c - 1) - 1 / (c - 1)) ----> 0 / (c - 1) - 1 / (c - 1)"
huffman@44568
   407
    using neq_0 by (intro tendsto_intros)
hoelzl@56193
   408
  hence "(\<lambda>n. (c ^ n - 1) / (c - 1)) ----> 1 / (1 - c)"
huffman@20692
   409
    by (simp add: nonzero_minus_divide_right [OF neq_0] diff_divide_distrib)
hoelzl@56193
   410
  thus "(\<lambda>n. c ^ n) sums (1 / (1 - c))"
huffman@20692
   411
    by (simp add: sums_def geometric_sum neq_1)
huffman@20692
   412
qed
huffman@20692
   413
hoelzl@56193
   414
lemma summable_geometric: "norm c < 1 \<Longrightarrow> summable (\<lambda>n. c^n)"
hoelzl@56193
   415
  by (rule geometric_sums [THEN sums_summable])
paulson@14416
   416
hoelzl@56193
   417
lemma suminf_geometric: "norm c < 1 \<Longrightarrow> suminf (\<lambda>n. c^n) = 1 / (1 - c)"
hoelzl@56193
   418
  by (rule sums_unique[symmetric]) (rule geometric_sums)
hoelzl@56193
   419
hoelzl@56193
   420
end
paulson@33271
   421
paulson@33271
   422
lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
paulson@33271
   423
proof -
paulson@33271
   424
  have 2: "(\<lambda>n. (1/2::real)^n) sums 2" using geometric_sums [of "1/2::real"]
paulson@33271
   425
    by auto
paulson@33271
   426
  have "(\<lambda>n. (1/2::real)^Suc n) = (\<lambda>n. (1 / 2) ^ n / 2)"
paulson@33271
   427
    by simp
huffman@44282
   428
  thus ?thesis using sums_divide [OF 2, of 2]
paulson@33271
   429
    by simp
paulson@33271
   430
qed
paulson@33271
   431
paulson@15085
   432
text{*Cauchy-type criterion for convergence of series (c.f. Harrison)*}
paulson@15085
   433
hoelzl@56193
   434
lemma summable_Cauchy:
hoelzl@56193
   435
  fixes f :: "nat \<Rightarrow> 'a::banach"
hoelzl@56193
   436
  shows "summable f \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n. norm (setsum f {m..<n}) < e)"
hoelzl@56193
   437
  apply (simp only: summable_iff_convergent Cauchy_convergent_iff [symmetric] Cauchy_iff, safe)
hoelzl@56193
   438
  apply (drule spec, drule (1) mp)
hoelzl@56193
   439
  apply (erule exE, rule_tac x="M" in exI, clarify)
hoelzl@56193
   440
  apply (rule_tac x="m" and y="n" in linorder_le_cases)
hoelzl@56193
   441
  apply (frule (1) order_trans)
hoelzl@56193
   442
  apply (drule_tac x="n" in spec, drule (1) mp)
hoelzl@56193
   443
  apply (drule_tac x="m" in spec, drule (1) mp)
hoelzl@56193
   444
  apply (simp_all add: setsum_diff [symmetric])
hoelzl@56193
   445
  apply (drule spec, drule (1) mp)
hoelzl@56193
   446
  apply (erule exE, rule_tac x="N" in exI, clarify)
hoelzl@56193
   447
  apply (rule_tac x="m" and y="n" in linorder_le_cases)
hoelzl@56193
   448
  apply (subst norm_minus_commute)
hoelzl@56193
   449
  apply (simp_all add: setsum_diff [symmetric])
hoelzl@56193
   450
  done
paulson@14416
   451
hoelzl@56193
   452
context
hoelzl@56193
   453
  fixes f :: "nat \<Rightarrow> 'a::banach"
hoelzl@56193
   454
begin  
hoelzl@56193
   455
hoelzl@56193
   456
text{*Absolute convergence imples normal convergence*}
huffman@20689
   457
hoelzl@56193
   458
lemma summable_norm_cancel:
hoelzl@56193
   459
  "summable (\<lambda>n. norm (f n)) \<Longrightarrow> summable f"
hoelzl@56193
   460
  apply (simp only: summable_Cauchy, safe)
hoelzl@56193
   461
  apply (drule_tac x="e" in spec, safe)
hoelzl@56193
   462
  apply (rule_tac x="N" in exI, safe)
hoelzl@56193
   463
  apply (drule_tac x="m" in spec, safe)
hoelzl@56193
   464
  apply (rule order_le_less_trans [OF norm_setsum])
hoelzl@56193
   465
  apply (rule order_le_less_trans [OF abs_ge_self])
hoelzl@56193
   466
  apply simp
hoelzl@50999
   467
  done
paulson@32707
   468
hoelzl@56193
   469
lemma summable_norm: "summable (\<lambda>n. norm (f n)) \<Longrightarrow> norm (suminf f) \<le> (\<Sum>n. norm (f n))"
hoelzl@56193
   470
  by (auto intro: LIMSEQ_le tendsto_norm summable_norm_cancel summable_LIMSEQ norm_setsum)
hoelzl@56193
   471
hoelzl@56193
   472
text {* Comparison tests *}
paulson@14416
   473
hoelzl@56193
   474
lemma summable_comparison_test: "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable f"
hoelzl@56193
   475
  apply (simp add: summable_Cauchy, safe)
hoelzl@56193
   476
  apply (drule_tac x="e" in spec, safe)
hoelzl@56193
   477
  apply (rule_tac x = "N + Na" in exI, safe)
hoelzl@56193
   478
  apply (rotate_tac 2)
hoelzl@56193
   479
  apply (drule_tac x = m in spec)
hoelzl@56193
   480
  apply (auto, rotate_tac 2, drule_tac x = n in spec)
hoelzl@56193
   481
  apply (rule_tac y = "\<Sum>k=m..<n. norm (f k)" in order_le_less_trans)
hoelzl@56193
   482
  apply (rule norm_setsum)
hoelzl@56193
   483
  apply (rule_tac y = "setsum g {m..<n}" in order_le_less_trans)
hoelzl@56193
   484
  apply (auto intro: setsum_mono simp add: abs_less_iff)
hoelzl@56193
   485
  done
hoelzl@56193
   486
hoelzl@56193
   487
subsection {* The Ratio Test*}
paulson@15085
   488
hoelzl@56193
   489
lemma summable_ratio_test: 
hoelzl@56193
   490
  assumes "c < 1" "\<And>n. n \<ge> N \<Longrightarrow> norm (f (Suc n)) \<le> c * norm (f n)"
hoelzl@56193
   491
  shows "summable f"
hoelzl@56193
   492
proof cases
hoelzl@56193
   493
  assume "0 < c"
hoelzl@56193
   494
  show "summable f"
hoelzl@56193
   495
  proof (rule summable_comparison_test)
hoelzl@56193
   496
    show "\<exists>N'. \<forall>n\<ge>N'. norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
hoelzl@56193
   497
    proof (intro exI allI impI)
hoelzl@56193
   498
      fix n assume "N \<le> n" then show "norm (f n) \<le> (norm (f N) / (c ^ N)) * c ^ n"
hoelzl@56193
   499
      proof (induct rule: inc_induct)
hoelzl@56193
   500
        case (step m)
hoelzl@56193
   501
        moreover have "norm (f (Suc m)) / c ^ Suc m * c ^ n \<le> norm (f m) / c ^ m * c ^ n"
hoelzl@56193
   502
          using `0 < c` `c < 1` assms(2)[OF `N \<le> m`] by (simp add: field_simps)
hoelzl@56193
   503
        ultimately show ?case by simp
hoelzl@56193
   504
      qed (insert `0 < c`, simp)
hoelzl@56193
   505
    qed
hoelzl@56193
   506
    show "summable (\<lambda>n. norm (f N) / c ^ N * c ^ n)"
hoelzl@56193
   507
      using `0 < c` `c < 1` by (intro summable_mult summable_geometric) simp
hoelzl@56193
   508
  qed
hoelzl@56193
   509
next
hoelzl@56193
   510
  assume c: "\<not> 0 < c"
hoelzl@56193
   511
  { fix n assume "n \<ge> N"
hoelzl@56193
   512
    then have "norm (f (Suc n)) \<le> c * norm (f n)"
hoelzl@56193
   513
      by fact
hoelzl@56193
   514
    also have "\<dots> \<le> 0"
hoelzl@56193
   515
      using c by (simp add: not_less mult_nonpos_nonneg)
hoelzl@56193
   516
    finally have "f (Suc n) = 0"
hoelzl@56193
   517
      by auto }
hoelzl@56193
   518
  then show "summable f"
hoelzl@56193
   519
    by (intro sums_summable[OF sums_finite, of "{.. Suc N}"]) (auto simp: not_le Suc_less_iff)
lp15@56178
   520
qed
lp15@56178
   521
hoelzl@56193
   522
end
paulson@14416
   523
huffman@20848
   524
lemma summable_norm_comparison_test:
hoelzl@56193
   525
  "\<lbrakk>\<exists>N. \<forall>n\<ge>N. norm (f n) \<le> g n; summable g\<rbrakk> \<Longrightarrow> summable (\<lambda>n. norm (f n))"
hoelzl@56193
   526
  by (rule summable_comparison_test) auto
huffman@20848
   527
hoelzl@56193
   528
lemma summable_rabs_cancel:
huffman@20692
   529
  fixes f :: "nat \<Rightarrow> real"
hoelzl@56193
   530
  shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> summable f"
hoelzl@56193
   531
  by (rule summable_norm_cancel) simp
paulson@14416
   532
huffman@23084
   533
text{*Summability of geometric series for real algebras*}
huffman@23084
   534
huffman@23084
   535
lemma complete_algebra_summable_geometric:
haftmann@31017
   536
  fixes x :: "'a::{real_normed_algebra_1,banach}"
huffman@23084
   537
  shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
huffman@23084
   538
proof (rule summable_comparison_test)
huffman@23084
   539
  show "\<exists>N. \<forall>n\<ge>N. norm (x ^ n) \<le> norm x ^ n"
huffman@23084
   540
    by (simp add: norm_power_ineq)
huffman@23084
   541
  show "norm x < 1 \<Longrightarrow> summable (\<lambda>n. norm x ^ n)"
huffman@23084
   542
    by (simp add: summable_geometric)
huffman@23084
   543
qed
huffman@23084
   544
paulson@15085
   545
hoelzl@56193
   546
text{*A summable series of positive terms has limit that is at least as
hoelzl@56193
   547
great as any partial sum.*}
paulson@14416
   548
hoelzl@56193
   549
lemma pos_summable:
hoelzl@56193
   550
  fixes f:: "nat \<Rightarrow> real"
hoelzl@56193
   551
  assumes pos: "\<And>n. 0 \<le> f n" and le: "\<And>n. setsum f {..<n} \<le> x"
hoelzl@56193
   552
  shows "summable f"
hoelzl@56193
   553
proof -
hoelzl@56193
   554
  have "convergent (\<lambda>n. setsum f {..<n})"
hoelzl@56193
   555
  proof (rule Bseq_mono_convergent)
hoelzl@56193
   556
    show "Bseq (\<lambda>n. setsum f {..<n})"
hoelzl@56193
   557
      by (intro BseqI'[of _ x]) (auto simp add: setsum_nonneg pos intro: le)
hoelzl@56193
   558
  qed (auto intro: setsum_mono2 pos)
hoelzl@56193
   559
  thus ?thesis
hoelzl@56193
   560
    by (force simp add: summable_def sums_def convergent_def)
hoelzl@56193
   561
qed
kleing@19106
   562
hoelzl@56193
   563
lemma summable_rabs_comparison_test:
huffman@20848
   564
  fixes f :: "nat \<Rightarrow> real"
hoelzl@56193
   565
  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>)"
hoelzl@56193
   566
  by (rule summable_comparison_test) auto
huffman@20848
   567
paulson@14416
   568
lemma summable_rabs:
huffman@20692
   569
  fixes f :: "nat \<Rightarrow> real"
huffman@20692
   570
  shows "summable (\<lambda>n. \<bar>f n\<bar>) \<Longrightarrow> \<bar>suminf f\<bar> \<le> (\<Sum>n. \<bar>f n\<bar>)"
huffman@20848
   571
by (fold real_norm_def, rule summable_norm)
paulson@14416
   572
huffman@23111
   573
subsection {* Cauchy Product Formula *}
huffman@23111
   574
wenzelm@54703
   575
text {*
wenzelm@54703
   576
  Proof based on Analysis WebNotes: Chapter 07, Class 41
wenzelm@54703
   577
  @{url "http://www.math.unl.edu/~webnotes/classes/class41/prp77.htm"}
wenzelm@54703
   578
*}
huffman@23111
   579
huffman@23111
   580
lemma setsum_triangle_reindex:
huffman@23111
   581
  fixes n :: nat
hoelzl@56193
   582
  shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k<n. \<Sum>i=0..k. f i (k - i))"
huffman@23111
   583
proof -
huffman@23111
   584
  have "(\<Sum>(i, j)\<in>{(i, j). i + j < n}. f i j) =
hoelzl@56193
   585
    (\<Sum>(k, i)\<in>(SIGMA k:{..<n}. {0..k}). f i (k - i))"
huffman@23111
   586
  proof (rule setsum_reindex_cong)
hoelzl@56193
   587
    show "inj_on (\<lambda>(k,i). (i, k - i)) (SIGMA k:{..<n}. {0..k})"
huffman@23111
   588
      by (rule inj_on_inverseI [where g="\<lambda>(i,j). (i+j, i)"], auto)
hoelzl@56193
   589
    show "{(i,j). i + j < n} = (\<lambda>(k,i). (i, k - i)) ` (SIGMA k:{..<n}. {0..k})"
huffman@23111
   590
      by (safe, rule_tac x="(a+b,a)" in image_eqI, auto)
huffman@23111
   591
    show "\<And>a. (\<lambda>(k, i). f i (k - i)) a = split f ((\<lambda>(k, i). (i, k - i)) a)"
huffman@23111
   592
      by clarify
huffman@23111
   593
  qed
huffman@23111
   594
  thus ?thesis by (simp add: setsum_Sigma)
huffman@23111
   595
qed
huffman@23111
   596
huffman@23111
   597
lemma Cauchy_product_sums:
huffman@23111
   598
  fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111
   599
  assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111
   600
  assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111
   601
  shows "(\<lambda>k. \<Sum>i=0..k. a i * b (k - i)) sums ((\<Sum>k. a k) * (\<Sum>k. b k))"
huffman@23111
   602
proof -
hoelzl@56193
   603
  let ?S1 = "\<lambda>n::nat. {..<n} \<times> {..<n}"
huffman@23111
   604
  let ?S2 = "\<lambda>n::nat. {(i,j). i + j < n}"
huffman@23111
   605
  have S1_mono: "\<And>m n. m \<le> n \<Longrightarrow> ?S1 m \<subseteq> ?S1 n" by auto
huffman@23111
   606
  have S2_le_S1: "\<And>n. ?S2 n \<subseteq> ?S1 n" by auto
huffman@23111
   607
  have S1_le_S2: "\<And>n. ?S1 (n div 2) \<subseteq> ?S2 n" by auto
huffman@23111
   608
  have finite_S1: "\<And>n. finite (?S1 n)" by simp
huffman@23111
   609
  with S2_le_S1 have finite_S2: "\<And>n. finite (?S2 n)" by (rule finite_subset)
huffman@23111
   610
huffman@23111
   611
  let ?g = "\<lambda>(i,j). a i * b j"
huffman@23111
   612
  let ?f = "\<lambda>(i,j). norm (a i) * norm (b j)"
huffman@23111
   613
  have f_nonneg: "\<And>x. 0 \<le> ?f x"
huffman@23111
   614
    by (auto simp add: mult_nonneg_nonneg)
huffman@23111
   615
  hence norm_setsum_f: "\<And>A. norm (setsum ?f A) = setsum ?f A"
huffman@23111
   616
    unfolding real_norm_def
huffman@23111
   617
    by (simp only: abs_of_nonneg setsum_nonneg [rule_format])
huffman@23111
   618
hoelzl@56193
   619
  have "(\<lambda>n. (\<Sum>k<n. a k) * (\<Sum>k<n. b k)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
hoelzl@56193
   620
    by (intro tendsto_mult summable_LIMSEQ summable_norm_cancel [OF a] summable_norm_cancel [OF b])
huffman@23111
   621
  hence 1: "(\<lambda>n. setsum ?g (?S1 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
hoelzl@56193
   622
    by (simp only: setsum_product setsum_Sigma [rule_format] finite_lessThan)
huffman@23111
   623
hoelzl@56193
   624
  have "(\<lambda>n. (\<Sum>k<n. norm (a k)) * (\<Sum>k<n. norm (b k))) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
hoelzl@56193
   625
    using a b by (intro tendsto_mult summable_LIMSEQ)
huffman@23111
   626
  hence "(\<lambda>n. setsum ?f (?S1 n)) ----> (\<Sum>k. norm (a k)) * (\<Sum>k. norm (b k))"
hoelzl@56193
   627
    by (simp only: setsum_product setsum_Sigma [rule_format] finite_lessThan)
huffman@23111
   628
  hence "convergent (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111
   629
    by (rule convergentI)
huffman@23111
   630
  hence Cauchy: "Cauchy (\<lambda>n. setsum ?f (?S1 n))"
huffman@23111
   631
    by (rule convergent_Cauchy)
huffman@36657
   632
  have "Zfun (\<lambda>n. setsum ?f (?S1 n - ?S2 n)) sequentially"
huffman@36657
   633
  proof (rule ZfunI, simp only: eventually_sequentially norm_setsum_f)
huffman@23111
   634
    fix r :: real
huffman@23111
   635
    assume r: "0 < r"
huffman@23111
   636
    from CauchyD [OF Cauchy r] obtain N
huffman@23111
   637
    where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (setsum ?f (?S1 m) - setsum ?f (?S1 n)) < r" ..
huffman@23111
   638
    hence "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> norm (setsum ?f (?S1 m - ?S1 n)) < r"
huffman@23111
   639
      by (simp only: setsum_diff finite_S1 S1_mono)
huffman@23111
   640
    hence N: "\<And>m n. \<lbrakk>N \<le> n; n \<le> m\<rbrakk> \<Longrightarrow> setsum ?f (?S1 m - ?S1 n) < r"
huffman@23111
   641
      by (simp only: norm_setsum_f)
huffman@23111
   642
    show "\<exists>N. \<forall>n\<ge>N. setsum ?f (?S1 n - ?S2 n) < r"
huffman@23111
   643
    proof (intro exI allI impI)
huffman@23111
   644
      fix n assume "2 * N \<le> n"
huffman@23111
   645
      hence n: "N \<le> n div 2" by simp
huffman@23111
   646
      have "setsum ?f (?S1 n - ?S2 n) \<le> setsum ?f (?S1 n - ?S1 (n div 2))"
huffman@23111
   647
        by (intro setsum_mono2 finite_Diff finite_S1 f_nonneg
huffman@23111
   648
                  Diff_mono subset_refl S1_le_S2)
huffman@23111
   649
      also have "\<dots> < r"
huffman@23111
   650
        using n div_le_dividend by (rule N)
huffman@23111
   651
      finally show "setsum ?f (?S1 n - ?S2 n) < r" .
huffman@23111
   652
    qed
huffman@23111
   653
  qed
huffman@36657
   654
  hence "Zfun (\<lambda>n. setsum ?g (?S1 n - ?S2 n)) sequentially"
huffman@36657
   655
    apply (rule Zfun_le [rule_format])
huffman@23111
   656
    apply (simp only: norm_setsum_f)
huffman@23111
   657
    apply (rule order_trans [OF norm_setsum setsum_mono])
huffman@23111
   658
    apply (auto simp add: norm_mult_ineq)
huffman@23111
   659
    done
huffman@23111
   660
  hence 2: "(\<lambda>n. setsum ?g (?S1 n) - setsum ?g (?S2 n)) ----> 0"
huffman@36660
   661
    unfolding tendsto_Zfun_iff diff_0_right
huffman@36657
   662
    by (simp only: setsum_diff finite_S1 S2_le_S1)
huffman@23111
   663
huffman@23111
   664
  with 1 have "(\<lambda>n. setsum ?g (?S2 n)) ----> (\<Sum>k. a k) * (\<Sum>k. b k)"
huffman@23111
   665
    by (rule LIMSEQ_diff_approach_zero2)
huffman@23111
   666
  thus ?thesis by (simp only: sums_def setsum_triangle_reindex)
huffman@23111
   667
qed
huffman@23111
   668
huffman@23111
   669
lemma Cauchy_product:
huffman@23111
   670
  fixes a b :: "nat \<Rightarrow> 'a::{real_normed_algebra,banach}"
huffman@23111
   671
  assumes a: "summable (\<lambda>k. norm (a k))"
huffman@23111
   672
  assumes b: "summable (\<lambda>k. norm (b k))"
huffman@23111
   673
  shows "(\<Sum>k. a k) * (\<Sum>k. b k) = (\<Sum>k. \<Sum>i=0..k. a i * b (k - i))"
huffman@23441
   674
using a b
huffman@23111
   675
by (rule Cauchy_product_sums [THEN sums_unique])
huffman@23111
   676
paulson@14416
   677
end