Guide

Sums and recurrences

Finite sums and products in closed form, sums of series, and linear recurrences, each with a theorem.

What I usually want from a sum is a formula, not a number. With a symbolic upper bound n you get a closed form, and SymLean proves it for every n by induction. With a fixed upper bound the sum is evaluated exactly. A finite product in closed form also comes with a theorem for every upper bound. The sum of a series comes with a HasSum theorem, which also says that the series converges, and a linear recurrence with constant coefficients gets its general solution, or the only sequence with the given initial values.

Sums and products

  • sumA finite sum in closed form or by its exact value, or the sum of a series, with a theorem.
  • productA finite product in closed form, telescoped or in factorials, with a theorem for every upper bound.

Recurrences

  • rsolveThe general solution of a linear recurrence with constant coefficients, or the sequence with given initial values, with a theorem.

The Basel problem, with Mathlib’s HasSum. The theorem says that the series converges and that its sum is π2/6. The sum starts at k=1, so the theorem writes the shift out as k+1:

In[1]:=
#cas [sum k 1 ∞] (1/k^2)
Out[1]=

sum of the series (ζ value)

π26
pi^2/6
Domain
k ∈ ℕ, k ≥ 1
Input (summand)
1/k^2

Checked by Lean’s kernel: Sum of the series by hasSum_zeta_nat

HasSum (fun k ↦ 1 / ↑(k + 1) ^ 2) (Real.pi ^ 2 / 6)
The certificate: 1 checked step
  1. Sum of the series by hasSum_zeta_nat

    HasSum (fun k ↦ 1 / ↑(k + 1) ^ 2) (Real.pi ^ 2 / 6)

    Checked by Lean’s kernel

Binet’s formula for the Fibonacci numbers. The theorem is an equivalence about every sequence a : ℕ → ℝ: it has the recurrence and these initial values exactly when it is the closed form, so no other sequence has them:

In[2]:=
#cas [rsolve a n, a 0 = 0, a 1 = 1] (a (n + 2) = a (n + 1) + a n)
Out[2]=

the sequence with these initial values

a(n)=(1+52)n−(1−52)n5
a n = (((1 + sqrt(5))/2)^n - ((1 - sqrt(5))/2)^n)/sqrt(5)
Domain
a : ℕ → ℝ
Input (recurrence with initial values)
a (n + 2) = a (n + 1) + a n, a 0 = 0, a 1 = 1

Checked by Lean’s kernel: A sequence has the recurrence and the initial values exactly when it is the closed form

∀ (a : ℕ → ℝ),
  (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ∧ a 0 = 0 ∧ a 1 = 1 ↔
    ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5
The certificate: 1 checked step
  1. A sequence has the recurrence and the initial values exactly when it is the closed form

    ∀ (a : ℕ → ℝ),
      (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ∧ a 0 = 0 ∧ a 1 = 1 ↔
        ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5

    Checked by Lean’s kernel

The closed forms of sums cover terms ckjrk, which include polynomials, geometric terms and their products, and hypergeometric terms through Gosper’s algorithm. The harmonic numbers have no closed form in hypergeometric terms, so ∑1/k is refused, and so is a summand with a parameter. A fixed upper bound sums at most 200 terms. A series takes terms ckjrk with |r|<1, or c/k2m through the values of the zeta function, so odd zeta values such as ζ(3) are refused. A product takes a constant times powers of linear factors k+s with integer shifts. Write those factors out. In a proof, cas proves the closed forms of finite sums and products. It doesn’t prove the statements about series or those of rsolve, but the closed form of a recurrence follows from the recurrence by induction.