Add initial arithmetic/geometric sequence/series definitions/theorems.

finite-set-exercises
Joshua Potter 2023-02-14 09:54:31 -07:00
parent ed0dc18a0a
commit 827229a927
11 changed files with 147 additions and 1 deletions

View File

@ -1 +1,2 @@
def hello := "world" import Bookshelf.Sequence.Arithmetic
import Bookshelf.Sequence.Geometric

View File

@ -0,0 +1,66 @@
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Ring
/--
A 0th-indexed arithmetic sequence.
-/
structure Arithmetic where
a₀ : Int
Δ : Int
namespace Arithmetic
/--
Returns the value of the `n`th term of an arithmetic sequence.
-/
def termClosed (seq : Arithmetic) (n : Nat) : Int := seq.a₀ + seq.Δ * n
/--
Returns the value of the `n`th term of an arithmetic sequence.
-/
def termRecursive : Arithmetic → Nat → Int
| seq, 0 => seq.a₀
| seq, (n + 1) => seq.Δ + seq.termRecursive n
/--
The recursive definition and closed definitions of an arithmetic sequence are
equivalent.
-/
theorem term_recursive_closed (seq : Arithmetic) (n : Nat)
: seq.termRecursive n = seq.termClosed n :=
Nat.recOn
n
(by unfold termRecursive termClosed; norm_num)
(fun n ih => calc
termRecursive seq (Nat.succ n)
= seq.Δ + seq.termRecursive n := rfl
_ = seq.Δ + seq.termClosed n := by rw [ih]
_ = seq.Δ + (seq.a₀ + seq.Δ * n) := rfl
_ = seq.a₀ + seq.Δ * (n + 1) := by ring
_ = termClosed seq (n + 1) := rfl)
/--
Summation of the first `n` terms of an arithmetic sequence.
-/
def sum : Arithmetic → Nat → Int
| _, 0 => 0
| seq, (n + 1) => seq.termClosed n + seq.sum n
/--
The closed formula of the summation of the first `n` terms of an arithmetic
series.
--/
theorem sum_closed_formula (seq : Arithmetic) (n : Nat)
: seq.sum n = (n / 2) * (seq.a₀ + seq.termClosed (n - 1)) :=
Nat.recOn
n
(by unfold sum termClosed; norm_num)
(fun n ih => calc
sum seq n.succ
= seq.termClosed n + seq.sum n := rfl
_ = seq.termClosed n + (n / 2 * (seq.a₀ + seq.termClosed (n - 1))) := by rw [ih]
_ = seq.a₀ + seq.Δ * n + (n / 2 * (seq.a₀ + (seq.a₀ + seq.Δ * ↑(n - 1)))) := rfl
-- TODO: To continue, need to find how to deal with division.
_ = ↑(n + 1) / 2 * (seq.a₀ + seq.termClosed n) := by sorry)
end Arithmetic

View File

@ -0,0 +1,49 @@
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Ring
/--
A 0th-indexed geometric sequence.
-/
structure Geometric where
a₀ : Int
r : Int
namespace Geometric
/--
The value of the `n`th term of an geometric sequence.
-/
def termClosed (seq : Geometric) (n : Nat) : Int := seq.a₀ * seq.r ^ n
/--
The value of the `n`th term of an geometric sequence.
-/
def termRecursive : Geometric → Nat → Int
| seq, 0 => seq.a₀
| seq, (n + 1) => seq.r * (seq.termRecursive n)
/--
The recursive definition and closed definitions of a geometric sequence are
equivalent.
-/
theorem term_recursive_closed (seq : Geometric) (n : Nat)
: seq.termRecursive n = seq.termClosed n :=
Nat.recOn
n
(by unfold termClosed termRecursive; norm_num)
(fun n ih => calc
seq.termRecursive (n + 1)
= seq.r * (seq.termRecursive n) := rfl
_ = seq.r * (seq.termClosed n) := by rw [ih]
_ = seq.r * (seq.a₀ * seq.r ^ n) := rfl
_ = seq.a₀ * seq.r ^ (n + 1) := by ring
_ = seq.termClosed (n + 1) := rfl)
/--
Summation of the first `n` terms of a geometric sequence.
-/
def sum : Geometric → Nat → Int
| _, 0 => 0
| seq, (n + 1) => seq.termClosed n + seq.sum n
end Geometric

27
lake-manifest.json Normal file
View File

@ -0,0 +1,27 @@
{"version": 4,
"packagesDir": "lake-packages",
"packages":
[{"git":
{"url": "https://github.com/leanprover-community/mathlib4.git",
"subDir?": null,
"rev": "065e9956724008d1059577700fd676eaf25f35d5",
"name": "mathlib",
"inputRev?": null}},
{"git":
{"url": "https://github.com/gebner/quote4",
"subDir?": null,
"rev": "7ac99aa3fac487bec1d5860e751b99c7418298cf",
"name": "Qq",
"inputRev?": "master"}},
{"git":
{"url": "https://github.com/JLimperg/aesop",
"subDir?": null,
"rev": "ba61f7fec6174d8c7d2796457da5a8d0b0da44c6",
"name": "aesop",
"inputRev?": "master"}},
{"git":
{"url": "https://github.com/leanprover/std4",
"subDir?": null,
"rev": "de7e2a79905a3f87cad1ad5bf57045206f9738c7",
"name": "std",
"inputRev?": "main"}}]}

View File

@ -1,6 +1,9 @@
import Lake import Lake
open Lake DSL open Lake DSL
require mathlib from git
"https://github.com/leanprover-community/mathlib4.git"
package «bookshelf» { package «bookshelf» {
-- add package configuration options here -- add package configuration options here
} }