src/HOL/Library/Arithmetic_Series.thy
author kleing
Fri, 07 Apr 2006 03:20:34 +0200
changeset 19351 c33563c7c14c
permissions -rw-r--r--
renamed ASeries to Arithmetic_Series, removed the ^M
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19351
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     1
(*  Title:      HOL/Library/Arithmetic_Series.thy
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     2
    ID:         $Id$
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     3
    Author:     Benjamin Porter, 2006
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     4
*)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     5
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     6
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     7
header {* Arithmetic Series *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     8
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
     9
theory Arithmetic_Series
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    10
imports Main
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    11
begin
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    12
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    13
section {* Abstract *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    14
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    15
text {* The following document presents a proof of the Arithmetic
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    16
Series Sum formalised in Isabelle/Isar.
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    17
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    18
{\em Theorem:} The series $\sum_{i=1}^{n} a_i$ where $a_{i+1} = a_i +
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    19
d$ for some constant $d$ has the sum $\frac{n}{2} (a_1 + a_n)$
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    20
(i.e. $n$ multiplied by the arithmetic mean of the first and last
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    21
element).
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    22
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    23
{\em Informal Proof:} (from
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    24
"http://mathworld.wolfram.com/ArithmeticSeries.html")
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    25
  The proof is a simple forward proof. Let $S$ equal the sum above and
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    26
  $a$ the first element, then we have
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    27
\begin{tabular}{ll}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    28
  $S$ &$= a + (a+d) + (a+2d) + ... a_n$ \\
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    29
    &$= n*a + d (0 + 1 + 2 + ... n-1)$ \\
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    30
    &$= n*a + d (\frac{1}{2} * (n-1) * n)$   ..using a simple sum identity \\
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    31
    &$= \frac{n}{2} (2a + d(n-1))$ \\
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    32
    & ..but $(a+a_n = a + (a + d(n-1)) = 2a + d(n-1))$ so \\
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    33
  $S$ &$= \frac{n}{2} (a + a_n)$
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    34
\end{tabular}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    35
*}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    36
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    37
section {* Formal Proof *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    38
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    39
text {* We present a proof for the abstract case of a commutative ring,
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    40
we then instantiate for three common types nats, ints and reals. The
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    41
function @{text "of_nat"} maps the natural numbers into any
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    42
commutative ring.
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    43
*}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    44
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    45
lemmas comm_simp [simp] = left_distrib right_distrib add_assoc mult_ac
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    46
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    47
text {* Next we prove the following simple summation law $\sum_{i=1}^n
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    48
i = \frac {n * (n+1)}{2}$. *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    49
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    50
lemma sum_ident:
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    51
  "((1::'a::comm_semiring_1_cancel) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    52
   of_nat n*((of_nat n)+1)"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    53
proof (induct n)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    54
  case 0
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    55
  show ?case by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    56
next
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    57
  case (Suc n)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    58
  then show ?case by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    59
qed
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    60
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    61
text {* The abstract theorem follows. Note that $2$ is displayed as
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    62
$1+1$ to keep the structure as abstract as possible. *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    63
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    64
theorem arith_series_general:
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    65
  "((1::'a::comm_semiring_1_cancel) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    66
  of_nat n * (a + (a + of_nat(n - 1)*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    67
proof cases
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    68
  assume ngt1: "n > 1"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    69
  let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    70
  have
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    71
    "(\<Sum>i\<in>{..<n}. a+?I i*d) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    72
     ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    73
    by (rule setsum_addf)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    74
  also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    75
  also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    76
    by (simp add: setsum_right_distrib setsum_head_upt)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    77
  also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    78
    by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    79
  also from ngt1 have "{1..<n} = {1..n - 1}"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    80
    by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)    
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    81
  also from ngt1 
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    82
  have "(1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..n - 1}. ?I i) = ((1+1)*?n*a + d*?I (n - 1)*?I n)"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    83
    by (simp only: mult_ac sum_ident [of "n - 1"]) (simp add: of_nat_Suc [symmetric])
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    84
  finally show ?thesis by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    85
next
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    86
  assume "\<not>(n > 1)"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    87
  hence "n = 1 \<or> n = 0" by auto
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    88
  thus ?thesis by auto
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    89
qed
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    90
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    91
subsection {* Instantiation *}
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    92
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    93
lemma arith_series_nat:
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    94
  "(2::nat) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    95
proof -
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    96
  have
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    97
    "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    98
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
    99
    by (rule arith_series_general)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   100
  thus ?thesis by (auto simp add: of_nat_id)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   101
qed
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   102
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   103
lemma arith_series_int:
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   104
  "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   105
  of_nat n * (a + (a + of_nat(n - 1)*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   106
proof -
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   107
  have
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   108
    "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   109
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   110
    by (rule arith_series_general)
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   111
  thus ?thesis by simp
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   112
qed
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   113
c33563c7c14c renamed ASeries to Arithmetic_Series, removed the ^M
kleing
parents:
diff changeset
   114
end