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 `0`th-indexed arithmetic sequence.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
|
|
|
structure Arithmetic where
|
2023-04-08 21:09:11 +00:00
|
|
|
a₀ : Real
|
|
|
|
Δ : Real
|
2023-02-14 16:54:31 +00:00
|
|
|
|
|
|
|
namespace Arithmetic
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
2023-02-14 16:54:31 +00:00
|
|
|
Returns the value of the `n`th term of an arithmetic sequence.
|
2023-04-08 21:09:11 +00:00
|
|
|
|
|
|
|
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 : Arithmetic) (n : Nat) : Real :=
|
|
|
|
seq.a₀ + seq.Δ * n
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
2023-02-14 16:54:31 +00:00
|
|
|
Returns the value of the `n`th term of an arithmetic sequence.
|
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 : Arithmetic → Nat → Real
|
2023-02-14 16:54:31 +00:00
|
|
|
| seq, 0 => seq.a₀
|
|
|
|
| seq, (n + 1) => seq.Δ + seq.termRecursive n
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
The recursive and closed term definitions of an arithmetic sequence agree with
|
|
|
|
one another.
|
2023-02-14 16:54:31 +00:00
|
|
|
-/
|
|
|
|
theorem term_recursive_closed (seq : Arithmetic) (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 termRecursive termClosed; norm_num
|
2023-04-09 14:11:27 +00:00
|
|
|
| succ n ih =>
|
|
|
|
calc
|
2023-02-14 16:54:31 +00:00
|
|
|
termRecursive seq (Nat.succ n)
|
2023-04-02 02:59:12 +00:00
|
|
|
= seq.Δ + seq.termRecursive n := rfl
|
|
|
|
_ = seq.Δ + seq.termClosed n := by rw [ih]
|
|
|
|
_ = seq.Δ + (seq.a₀ + seq.Δ * n) := rfl
|
2023-04-08 21:09:11 +00:00
|
|
|
_ = seq.a₀ + seq.Δ * (↑n + 1) := by ring
|
|
|
|
_ = seq.a₀ + seq.Δ * ↑(n + 1) := by simp
|
2023-04-08 16:46:27 +00:00
|
|
|
_ = termClosed seq (n + 1) := rfl
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-09 14:11:27 +00:00
|
|
|
/--
|
|
|
|
A term is equal to the next in the sequence minus the common difference.
|
|
|
|
-/
|
|
|
|
theorem term_closed_sub_succ_delta {seq : Arithmetic}
|
|
|
|
: seq.termClosed n = seq.termClosed (n + 1) - seq.Δ :=
|
|
|
|
calc
|
|
|
|
seq.termClosed n
|
|
|
|
_ = seq.a₀ + seq.Δ * n := rfl
|
|
|
|
_ = seq.a₀ + seq.Δ * n + seq.Δ - seq.Δ := by rw [add_sub_cancel]
|
|
|
|
_ = seq.a₀ + seq.Δ * (↑n + 1) - seq.Δ := by ring_nf
|
|
|
|
_ = seq.a₀ + seq.Δ * ↑(n + 1) - seq.Δ := by simp only [Nat.cast_add, Nat.cast_one]
|
|
|
|
_ = seq.termClosed (n + 1) - seq.Δ := rfl
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
The summation of the first `n + 1` terms of an arithmetic sequence.
|
|
|
|
|
|
|
|
This function calculates the sum directly.
|
|
|
|
-/
|
|
|
|
noncomputable def sum_closed (seq : Arithmetic) (n : Nat) : Real :=
|
2023-04-09 14:11:27 +00:00
|
|
|
(n + 1) * (seq.a₀ + seq.termClosed n) / 2
|
2023-04-08 21:09:11 +00:00
|
|
|
|
|
|
|
/--
|
|
|
|
The summation of the first `n + 1` terms of an arithmetic 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 : Arithmetic → Nat → Real
|
2023-04-09 14:11:27 +00:00
|
|
|
| seq, 0 => seq.a₀
|
2023-04-08 21:09:11 +00:00
|
|
|
| seq, (n + 1) => seq.termClosed (n + 1) + seq.sum_recursive n
|
|
|
|
|
2023-04-09 14:11:27 +00:00
|
|
|
/--
|
|
|
|
Simplify a summation of terms found in the proof of `sum_recursive_closed`.
|
|
|
|
-/
|
|
|
|
private lemma sub_delta_summand_eq_two_mul_a₀ {seq : Arithmetic}
|
|
|
|
: seq.a₀ + seq.termClosed (n + 1) - (n + 1) * seq.Δ = 2 * seq.a₀ :=
|
|
|
|
calc
|
|
|
|
seq.a₀ + seq.termClosed (n + 1) - (n + 1) * seq.Δ
|
|
|
|
_ = seq.a₀ + (seq.a₀ + seq.Δ * ↑(n + 1)) - (n + 1) * seq.Δ := rfl
|
|
|
|
_ = seq.a₀ + seq.a₀ + seq.Δ * ↑(n + 1) - (n + 1) * seq.Δ := by rw [←add_assoc]
|
|
|
|
_ = seq.a₀ + seq.a₀ + seq.Δ * (n + 1) - (n + 1) * seq.Δ := by simp only [Nat.cast_add, Nat.cast_one]
|
|
|
|
_ = 2 * seq.a₀ := by ring_nf
|
|
|
|
|
2023-04-08 21:09:11 +00:00
|
|
|
/--
|
|
|
|
The recursive and closed definitions of the sum of an arithmetic sequence agree
|
|
|
|
with one another.
|
|
|
|
-/
|
|
|
|
theorem sum_recursive_closed (seq : Arithmetic) (n : Nat)
|
2023-04-09 14:11:27 +00:00
|
|
|
: seq.sum_recursive n = seq.sum_closed n := by
|
|
|
|
induction n with
|
|
|
|
| zero =>
|
|
|
|
unfold sum_recursive sum_closed termClosed
|
|
|
|
norm_num
|
|
|
|
| succ n ih =>
|
|
|
|
calc
|
|
|
|
seq.sum_recursive (n + 1)
|
|
|
|
_ = seq.termClosed (n + 1) + seq.sum_recursive n := rfl
|
|
|
|
_ = seq.termClosed (n + 1) + seq.sum_closed n := by rw [ih]
|
|
|
|
_ = seq.termClosed (n + 1) + ((n + 1) * (seq.a₀ + seq.termClosed n)) / 2 := rfl
|
|
|
|
_ = (2 * seq.termClosed (n + 1) + n * seq.a₀ + n * seq.termClosed n + seq.a₀ + seq.termClosed n) / 2 := by ring_nf
|
|
|
|
_ = (2 * seq.termClosed (n + 1) + n * seq.a₀ + n * (seq.termClosed (n + 1) - seq.Δ) + seq.a₀ + (seq.termClosed (n + 1) - seq.Δ)) / 2 := by rw [@term_closed_sub_succ_delta n]
|
|
|
|
_ = (2 * seq.termClosed (n + 1) + n * seq.a₀ + n * seq.termClosed (n + 1) + (seq.a₀ + seq.termClosed (n + 1) - (n + 1) * seq.Δ)) / 2 := by ring_nf
|
|
|
|
_ = (2 * seq.termClosed (n + 1) + n * seq.a₀ + n * seq.termClosed (n + 1) + 2 * seq.a₀) / 2 := by rw [sub_delta_summand_eq_two_mul_a₀]
|
|
|
|
_ = ((n + 1) + 1) * (seq.a₀ + seq.termClosed (n + 1)) / 2 := by ring_nf
|
|
|
|
_ = (↑(n + 1) + 1) * (seq.a₀ + seq.termClosed (n + 1)) / 2 := by simp only [Nat.cast_add, Nat.cast_one]
|
|
|
|
_ = seq.sum_closed (n + 1) := rfl
|
2023-02-14 16:54:31 +00:00
|
|
|
|
2023-04-08 16:32:20 +00:00
|
|
|
end Arithmetic
|