2023-04-08 21:09:11 +00:00
|
|
|
import Mathlib.Data.Real.Basic
|
2023-02-14 16:54:31 +00:00
|
|
|
import Mathlib.Tactic.NormNum
|
|
|
|
import Mathlib.Tactic.Ring
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
A `0th`-indexed geometric sequence.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
|
|
|
structure Geometric where
|
2023-04-08 21:09:11 +00:00
|
|
|
a₀ : Real
|
|
|
|
r : Real
|
2023-02-14 16:54:31 +00:00
|
|
|
|
|
|
|
namespace Geometric
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
Returns the value of the `n`th term of a geometric sequence.
|
|
|
|
|
|
|
|
This function calculates the value of this term directly. Keep in mind the
|
|
|
|
sequence is `0`th-indexed.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
2023-04-08 21:09:11 +00:00
|
|
|
def termClosed (seq : Geometric) (n : Nat) : Real :=
|
|
|
|
seq.a₀ * seq.r ^ n
|
|
|
|
|
|
|
|
/--
|
|
|
|
Returns the value of the `n`th term of a geometric sequence.
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
This function calculates the value of this term recursively. Keep in mind the
|
|
|
|
sequence is `0`th-indexed.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
2023-04-08 21:09:11 +00:00
|
|
|
def termRecursive : Geometric → Nat → Real
|
2023-02-14 16:54:31 +00:00
|
|
|
| seq, 0 => seq.a₀
|
|
|
|
| seq, (n + 1) => seq.r * (seq.termRecursive n)
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
The recursive and closed term definitions of a geometric sequence agree with
|
|
|
|
one another.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
|
|
|
theorem term_recursive_closed (seq : Geometric) (n : Nat)
|
2023-04-08 21:09:11 +00:00
|
|
|
: seq.termRecursive n = seq.termClosed n := by
|
2023-04-08 16:46:27 +00:00
|
|
|
induction n with
|
|
|
|
| zero => unfold termClosed termRecursive; norm_num
|
|
|
|
| succ n ih => calc
|
2023-02-14 16:54:31 +00:00
|
|
|
seq.termRecursive (n + 1)
|
2023-04-02 02:59:12 +00:00
|
|
|
= 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
|
2023-04-08 16:46:27 +00:00
|
|
|
_ = seq.termClosed (n + 1) := rfl
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
The summation of the first `n + 1` terms of a geometric sequence.
|
|
|
|
|
|
|
|
This function calculates the sum directly.
|
|
|
|
-/
|
|
|
|
noncomputable def sum_closed_ratio_neq_one (seq : Geometric) (n : Nat)
|
|
|
|
: seq.r ≠ 1 → Real :=
|
|
|
|
fun _ => (seq.a₀ * (1 - seq.r ^ (n + 1))) / (1 - seq.r)
|
|
|
|
|
|
|
|
/--
|
|
|
|
The summation of the first `n + 1` terms of a geometric sequence.
|
|
|
|
|
|
|
|
This function calculates the sum recursively.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
2023-04-08 21:09:11 +00:00
|
|
|
def sum_recursive : Geometric → Nat → Real
|
2023-04-09 14:11:27 +00:00
|
|
|
| seq, 0 => seq.a₀
|
|
|
|
| seq, (n + 1) => seq.termClosed (n + 1) + seq.sum_recursive n
|
2023-04-08 21:09:11 +00:00
|
|
|
|
|
|
|
/--
|
2023-04-09 14:11:27 +00:00
|
|
|
The recursive and closed definitions of the sum of a geometric sequence agree
|
2023-04-08 21:09:11 +00:00
|
|
|
with one another.
|
|
|
|
-/
|
|
|
|
theorem sum_recursive_closed (seq : Geometric) (n : Nat) (p : seq.r ≠ 1)
|
2023-04-09 14:11:27 +00:00
|
|
|
: sum_recursive seq n = sum_closed_ratio_neq_one seq n p := by
|
|
|
|
have h : 1 - seq.r ≠ 0 := by
|
|
|
|
intro h
|
|
|
|
rw [sub_eq_iff_eq_add, zero_add] at h
|
|
|
|
exact False.elim (p (Eq.symm h))
|
|
|
|
induction n with
|
|
|
|
| zero =>
|
|
|
|
unfold sum_recursive sum_closed_ratio_neq_one
|
|
|
|
simp
|
|
|
|
rw [mul_div_assoc, div_self h, mul_one]
|
|
|
|
| succ n ih =>
|
|
|
|
calc
|
|
|
|
sum_recursive seq (n + 1)
|
|
|
|
_ = seq.termClosed (n + 1) + seq.sum_recursive n := rfl
|
|
|
|
_ = seq.termClosed (n + 1) + sum_closed_ratio_neq_one seq n p := by rw [ih]
|
|
|
|
_ = seq.a₀ * seq.r ^ (n + 1) + (seq.a₀ * (1 - seq.r ^ (n + 1))) / (1 - seq.r) := rfl
|
|
|
|
_ = seq.a₀ * seq.r ^ (n + 1) * (1 - seq.r) / (1 - seq.r) + (seq.a₀ * (1 - seq.r ^ (n + 1))) / (1 - seq.r) := by rw [mul_div_cancel _ h]
|
|
|
|
_ = (seq.a₀ * (1 - seq.r ^ (n + 1 + 1))) / (1 - seq.r) := by ring_nf
|
|
|
|
_ = sum_closed_ratio_neq_one seq (n + 1) p := rfl
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-08 16:32:20 +00:00
|
|
|
end Geometric
|