1-- Arithmetic series2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Real.ArithmeticSeries8import Natural9import FiniteSum1011defgraph arithmeticTerms (A D : Real) (n ∈ ℕ) : ℝ := A + (realNat(n):R) * D12proof13have typedN := nat.omega.backward at n with member14have count := realNat.real at n with typedN15have term : Real((A:R) + (realNat(n):R) * (D:R))16typecheck17unfoldRealat term18exact term1920theorem arithmeticTerms.sequence (A D : Real)21shows RealSequence(arithmeticTerms(A, D))22proof23unfold RealSequence24exact arithmeticTerms.function at A, D with*2526-- The first n terms, including the empty prefix when n = 0.27theorem arithmeticSeries.doubled (A D : Real, n : Nat)28shows (2:R) * (∑ i < n, arithmeticTerms(A, D)(i):R) =29 (realNat(n):R) * ((2:R) * A + ((realNat(n):R) - (1:R)) * D)30proof31induction n32| zero =>33rw [realPartialSum.zero, realNat.zero]34ring35| succ(k) using ih =>36have sequence := arithmeticTerms.sequence at A, D with*37have value := arithmeticTerms.value at A, D, k with*38rw [realPartialSum.succ, value, realNat.succ]39linear_combination ih4041-- Inclusive indexing matches a + (a+d) + ... + (a+n*d).42theorem arithmeticSeries.inclusive (A D : Real, n : Nat)43shows (2:R) * (∑ i < n +1, arithmeticTerms(A, D)(i):R) =44 ((realNat(n):R) + (1:R)) * ((2:R) * A + (realNat(n):R) * D)45proof46have bound : n +1= succ(n) := add.right_one at n47rw bound48have sequence := arithmeticTerms.sequence at A, D with*49have doubled := arithmeticSeries.doubled at A, D, succ(n) with*50rw realNat.succ at doubled51linear_combination doubled5253theorem arithmeticSeries.first_last (A D : Real, n : Nat)54shows (2:R) * (∑ i < n +1, arithmeticTerms(A, D)(i):R) =55 ((realNat(n):R) + (1:R)) * (A + (arithmeticTerms(A, D)[n]:R))56proof57have bound : n +1= succ(n) := add.right_one at n58rw bound59have sequence := arithmeticTerms.sequence at A, D with*60have value := arithmeticTerms.value at A, D, n with*61have inclusive := arithmeticSeries.inclusive at A, D, n with*62rw bound at inclusive63rw value64linear_combination inclusive6566theorem arithmeticSeries.gauss (n : Nat)67shows (2:R) * (∑ i < n +1, arithmeticTerms(realZero, realOne)(i):R) =68 (realNat(n):R) * ((realNat(n):R) + (1:R))69proof70have bound : n +1= succ(n) := add.right_one at n71rw bound72have sequence := arithmeticTerms.sequence at realZero, realOne with*73have inclusive := arithmeticSeries.inclusive at realZero, realOne, n with*74rw bound at inclusive75linear_combination inclusive7677theorem arithmeticSeries.odd_sum (n : Nat)78shows ∑ k < n, arithmeticTerms(realOne, (2:R))(k) =79 (realNat(n):R) * (realNat(n):R)80proof81have sequence := arithmeticTerms.sequence at realOne, (2:R) with*82haveprefix:= realPartialSum.real with sequence83have doubled := arithmeticSeries.doubled at realOne, (2:R), n with*84apply realAddValue.double_cancel withprefix85linear_combination doubled8687theorem arithmeticSeries.quotient (A D : Real, n : Nat)88shows ∑ i < n +1, arithmeticTerms(A, D)(i) =89 (((realNat(n):R) + (1:R)) * ((2:R) * A + (realNat(n):R) * D)) /90 (2:R)91proof92have bound : n +1= succ(n) := add.right_one at n93rw bound94have sequence := arithmeticTerms.sequence at A, D with*95haveprefix:= realPartialSum.real at arithmeticTerms(A, D), succ(n) with sequence96have numerator : Real(((realNat(n):R) + (1:R)) * ((2:R) * A + (realNat(n):R) * D))97typecheck98have inclusive := arithmeticSeries.inclusive at A, D, n with*99rw bound at inclusive100apply realDiv.of_double_eq at ∑ i < succ(n), arithmeticTerms(A, D)(i),101 ((realNat(n):R) + (1:R)) * ((2:R) * A + (realNat(n):R) * D) withprefix, numerator102linear_combination inclusive103