Mode of #cas
rsolve
The general solution of a linear recurrence with constant coefficients, or the sequence with given initial values, with a theorem.
#cas [rsolve a n] eq#cas [rsolve a n, a 0 = v₀, …] eqrsolve finds a closed form for a linear recurrence, such as Binet’s formula for the Fibonacci numbers. I wanted more than a formula that matches the first few terms, so the theorem is an equivalence about every sequence a : ℕ → ℝ: it satisfies the recurrence, and the initial values when they are given, exactly when it has the printed form.
The closed form comes from the roots of the characteristic polynomial. Rational roots and the roots of quadratic factors are written exactly, and the other roots are certified enclosures. When the right side has terms , we find a particular solution by undetermined coefficients.
Examples (20)#
Basic Examples (2)
The Fibonacci numbers. Binet’s formula is the only sequence with the recurrence and these initial values:
#cas [rsolve a n, a 0 = 0, a 1 = 1] (a (n + 2) = a (n + 1) + a n)
the sequence with these initial values
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
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
Without initial values, the general solution of this recurrence of order 2 has two constants:
#cas [rsolve a n] (a (n + 2) = a (n + 1) + a n)
general solution
a n = c₁*((1 + sqrt(5))/2)^n + c₂*((1 - sqrt(5))/2)^n- Domain
a : ℕ → ℝ- Input (recurrence)
a (n + 2) = a (n + 1) + a n
Checked by Lean’s kernel: Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ * ((1 + √5) / 2) ^ n + c₂ * ((1 - √5) / 2) ^ n
The certificate: 1 checked step
Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ * ((1 + √5) / 2) ^ n + c₂ * ((1 - √5) / 2) ^ n
Scope (8)
A first-order recurrence with a constant term:
#cas [rsolve a n, a 0 = 1] (a (n + 1) = 2 * a n + 1)
the sequence with these initial values
a n = -1 + 2*2^n- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 1) = 2 * a n + 1, a 0 = 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 + 1) = 2 * a n + 1) ∧ a 0 = 1 ↔ ∀ (n : ℕ), a n = -1 + 2 * 2 ^ n
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 1) = 2 * a n + 1) ∧ a 0 = 1 ↔ ∀ (n : ℕ), a n = -1 + 2 * 2 ^ n
Two rational characteristic roots, in the general solution and with initial values:
#cas [rsolve a n] (a (n + 2) = 5 * a (n + 1) - 6 * a n)
general solution
a n = c₁*2^n + c₂*3^n- Domain
a : ℕ → ℝ- Input (recurrence)
a (n + 2) = 5 * a (n + 1) - 6 * a n
Checked by Lean’s kernel: Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = 5 * a (n + 1) - 6 * a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ * 2 ^ n + c₂ * 3 ^ n
The certificate: 1 checked step
Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = 5 * a (n + 1) - 6 * a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ * 2 ^ n + c₂ * 3 ^ n
#cas [rsolve a n, a 0 = 2, a 1 = 3] (a (n + 2) = 3 * a (n + 1) - 2 * a n)
the sequence with these initial values
a n = 1 + 2^n- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 2) = 3 * a (n + 1) - 2 * a n, a 0 = 2, a 1 = 3
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) = 3 * a (n + 1) - 2 * a n) ∧ a 0 = 2 ∧ a 1 = 3 ↔ ∀ (n : ℕ), a n = 1 + 2 ^ n
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = 3 * a (n + 1) - 2 * a n) ∧ a 0 = 2 ∧ a 1 = 3 ↔ ∀ (n : ℕ), a n = 1 + 2 ^ n
A double root gives the terms and :
#cas [rsolve a n] (a (n + 2) = 2 * a (n + 1) - a n)
general solution
a n = c₁ + c₂*n- Domain
a : ℕ → ℝ- Input (recurrence)
a (n + 2) = 2 * a (n + 1) - a n
Checked by Lean’s kernel: Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = 2 * a (n + 1) - a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ + c₂ * ↑n
The certificate: 1 checked step
Closed form of every solution
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = 2 * a (n + 1) - a n) ↔ ∃ c₁ c₂, ∀ (n : ℕ), a n = c₁ + c₂ * ↑n
A polynomial right side, by undetermined coefficients:
#cas [rsolve a n, a 0 = 0, a 1 = 0] (a (n + 2) - 2 * a (n + 1) + a n = 1)
the sequence with these initial values
a n = 1/2*n^2 - 1/2*n- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 2) - 2 * a (n + 1) + a n = 1, a 0 = 0, a 1 = 0
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) - 2 * a (n + 1) + a n = 1) ∧ a 0 = 0 ∧ a 1 = 0 ↔ ∀ (n : ℕ), a n = 1 / 2 * ↑n ^ 2 - 1 / 2 * ↑n
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) - 2 * a (n + 1) + a n = 1) ∧ a 0 = 0 ∧ a 1 = 0 ↔ ∀ (n : ℕ), a n = 1 / 2 * ↑n ^ 2 - 1 / 2 * ↑n
#cas [rsolve a n, a 0 = 1] (a (n + 1) = a n + n)
the sequence with these initial values
a n = -(1/2*n) + 1/2*n^2 + 1- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 1) = a n + n, a 0 = 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 + 1) = a n + ↑n) ∧ a 0 = 1 ↔ ∀ (n : ℕ), a n = -(1 / 2 * ↑n) + 1 / 2 * ↑n ^ 2 + 1
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 1) = a n + ↑n) ∧ a 0 = 1 ↔ ∀ (n : ℕ), a n = -(1 / 2 * ↑n) + 1 / 2 * ↑n ^ 2 + 1
The initial values may sit at any two consecutive indices:
#cas [rsolve a n, a 1 = 1, a 2 = 1] (a (n + 2) = a (n + 1) + a n)
the sequence with these initial values
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 1 = 1, a 2 = 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 1 = 1 ∧ a 2 = 1 ↔ ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5
The certificate: 1 checked step
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 1 = 1 ∧ a 2 = 1 ↔ ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5
The complex characteristic roots , in real form:
#cas [rsolve a n, a 0 = 1, a 1 = 0] (a (n + 2) = - a n)
the sequence with these initial values
a n = re(i^n)- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 2) = - a n, a 0 = 1, a 1 = 0
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) ∧ a 0 = 1 ∧ a 1 = 0 ↔ ∀ (n : ℕ), a n = (Complex.I ^ n).re
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 2) = -a n) ∧ a 0 = 1 ∧ a 1 = 0 ↔ ∀ (n : ℕ), a n = (Complex.I ^ n).re
A cubic with one real root and a pair of complex roots, known by enclosures and named r₁ and z₁:
#cas [rsolve a n] (a (n + 3) = a (n + 1) + a n)
general solution
a n = c₁*r₁^n + c₂*re(z₁^n) + c₃*im(z₁^n)- Domain
a : ℕ → ℝ- Input (recurrence)
a (n + 3) = a (n + 1) + a n
Checked by Lean’s kernel: Closed form of every solution
let r₁ := CAS.AlgRoot.real [-1, -1, 0, 1] 1.32471795 1.32471797; let z₁ := CAS.AlgRoot.complex [-1, -1, 0, 1] (-0.66235898) 0.56227951 2e-8; (r₁ ∈ Set.Ioo 1.32471795 1.32471797 ∧ r₁ ^ 3 - r₁ - 1 = 0) ∧ (z₁.re ∈ Set.Icc (-0.66235900) (-0.66235896) ∧ z₁.im ∈ Set.Icc 0.56227949 0.56227953 ∧ z₁ ^ 3 - z₁ - 1 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 3) = a (n + 1) + a n) ↔ ∃ c₁ c₂ c₃, ∀ (n : ℕ), a n = c₁ * r₁ ^ n + c₂ * (z₁ ^ n).re + c₃ * (z₁ ^ n).im
The certificate: 1 checked step
Closed form of every solution
let r₁ := CAS.AlgRoot.real [-1, -1, 0, 1] 1.32471795 1.32471797; let z₁ := CAS.AlgRoot.complex [-1, -1, 0, 1] (-0.66235898) 0.56227951 2e-8; (r₁ ∈ Set.Ioo 1.32471795 1.32471797 ∧ r₁ ^ 3 - r₁ - 1 = 0) ∧ (z₁.re ∈ Set.Icc (-0.66235900) (-0.66235896) ∧ z₁.im ∈ Set.Icc 0.56227949 0.56227953 ∧ z₁ ^ 3 - z₁ - 1 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 3) = a (n + 1) + a n) ↔ ∃ c₁ c₂ c₃, ∀ (n : ℕ), a n = c₁ * r₁ ^ n + c₂ * (z₁ ^ n).re + c₃ * (z₁ ^ n).im
The quartic is not split, and its four real roots are enclosed:
#cas [rsolve a n] (a (n + 4) = 5 * a (n + 2) - 6 * a n)
general solution
a n = c₁*r₁^n + c₂*r₂^n + c₃*r₃^n + c₄*r₄^n- Domain
a : ℕ → ℝ- Input (recurrence)
a (n + 4) = 5 * a (n + 2) - 6 * a n
Checked by Lean’s kernel: Closed form of every solution
let r₁ := CAS.AlgRoot.real [6, 0, -5, 0, 1] (-1.73205082) (-1.73205080); let r₂ := CAS.AlgRoot.real [6, 0, -5, 0, 1] (-1.41421357) (-1.41421355); let r₃ := CAS.AlgRoot.real [6, 0, -5, 0, 1] 1.41421355 1.41421357; let r₄ := CAS.AlgRoot.real [6, 0, -5, 0, 1] 1.73205080 1.73205082; (r₁ ∈ Set.Ioo (-1.73205082) (-1.73205080) ∧ r₁ ^ 4 - 5 * r₁ ^ 2 + 6 = 0) ∧ (r₂ ∈ Set.Ioo (-1.41421357) (-1.41421355) ∧ r₂ ^ 4 - 5 * r₂ ^ 2 + 6 = 0) ∧ (r₃ ∈ Set.Ioo 1.41421355 1.41421357 ∧ r₃ ^ 4 - 5 * r₃ ^ 2 + 6 = 0) ∧ (r₄ ∈ Set.Ioo 1.73205080 1.73205082 ∧ r₄ ^ 4 - 5 * r₄ ^ 2 + 6 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 4) = 5 * a (n + 2) - 6 * a n) ↔ ∃ c₁ c₂ c₃ c₄, ∀ (n : ℕ), a n = c₁ * r₁ ^ n + c₂ * r₂ ^ n + c₃ * r₃ ^ n + c₄ * r₄ ^ n
The certificate: 1 checked step
Closed form of every solution
let r₁ := CAS.AlgRoot.real [6, 0, -5, 0, 1] (-1.73205082) (-1.73205080); let r₂ := CAS.AlgRoot.real [6, 0, -5, 0, 1] (-1.41421357) (-1.41421355); let r₃ := CAS.AlgRoot.real [6, 0, -5, 0, 1] 1.41421355 1.41421357; let r₄ := CAS.AlgRoot.real [6, 0, -5, 0, 1] 1.73205080 1.73205082; (r₁ ∈ Set.Ioo (-1.73205082) (-1.73205080) ∧ r₁ ^ 4 - 5 * r₁ ^ 2 + 6 = 0) ∧ (r₂ ∈ Set.Ioo (-1.41421357) (-1.41421355) ∧ r₂ ^ 4 - 5 * r₂ ^ 2 + 6 = 0) ∧ (r₃ ∈ Set.Ioo 1.41421355 1.41421357 ∧ r₃ ^ 4 - 5 * r₃ ^ 2 + 6 = 0) ∧ (r₄ ∈ Set.Ioo 1.73205080 1.73205082 ∧ r₄ ^ 4 - 5 * r₄ ^ 2 + 6 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 4) = 5 * a (n + 2) - 6 * a n) ↔ ∃ c₁ c₂ c₃ c₄, ∀ (n : ℕ), a n = c₁ * r₁ ^ n + c₂ * r₂ ^ n + c₃ * r₃ ^ n + c₄ * r₄ ^ n
Applications (1)
The closed form in a proof, by induction from the recurrence. Only the three standard axioms are used:
theorem rsolve_closed_form (a : ℕ → ℝ) (h : ∀ n, a (n + 1) = 2 * a n + 1) (h₀ : a 0 = 1) : ∀ n, a n = -1 + 2 * 2 ^ n := by intro n induction n with | zero => rw [h₀] norm_num | succ n ih => rw [h, ih] ring #print axioms rsolve_closed_form
rsolve_closed_form depends on the axioms propext, Classical.choice, Quot.sound.
Properties and Relations (1)
Possible Issues (7)
The coefficients must be constants. Here the coefficient of a n is :
#cas [rsolve a n] (a (n + 1) = n * a n)
Error
rsolve: recurrences with coefficients that depend on n are not treated (the coefficient of a n is n)
When the coefficient of a n is zero, the recurrence is one of lower order for a (n + 1):
#cas [rsolve a n] (a (n + 2) = a (n + 1))
Error
rsolve: the coefficient of a n is zero; this is a recurrence of lower order for a (n + 1)
The recurrence must be linear in the sequence:
#cas [rsolve a n] (a (n + 1) = a n * a n)
Error
rsolve: the recurrence must be linear in a
A recurrence of order 2 takes two initial values:
#cas [rsolve a n, a 0 = 1] (a (n + 2) = a (n + 1) + a n)
Error
rsolve: give the 2 initial values a k, …, a (k + 1) at consecutive indices
The right side may contain polynomial and exponential terms in only:
#cas [rsolve a n] (a (n + 1) = a n + sin n)
Error
rsolve: the terms in n must be sums of c n^j r^n
The first name in the brackets is the sequence, and the message says how to fix a mismatch:
#cas [rsolve a n] (b (n + 1) = 2 * b n)
Error
rsolve: the equation is about the sequence b, but the mode names a; write #cas [rsolve b n]
The sequence is written at n and at n + k, not at n - k. Shift the recurrence, here to a (n + 2) = a (n + 1) + a n:
#cas [rsolve a n] (a n = a (n - 1) + a (n - 2))
Error
rsolve: expected n or n + k
Neat Examples (1)
The Padovan numbers. Their characteristic polynomial has the plastic number as its real root, here r₁:
#cas [rsolve a n, a 0 = 1, a 1 = 1, a 2 = 1] (a (n + 3) = a (n + 1) + a n)
the sequence with these initial values
a n = (7/23*r₁^2 + 1/23*r₁ + 3/23)*r₁^n + 2*re((7/23*z₁^2 + 1/23*z₁ + 3/23)*z₁^n)- Domain
a : ℕ → ℝ- Input (recurrence with initial values)
a (n + 3) = a (n + 1) + a n, a 0 = 1, a 1 = 1, a 2 = 1
Checked by Lean’s kernel: A sequence has the recurrence and the initial values exactly when it is the closed form
let r₁ := CAS.AlgRoot.real [-1, -1, 0, 1] 1.32471795 1.32471797; let z₁ := CAS.AlgRoot.complex [-1, -1, 0, 1] (-0.66235898) 0.56227951 2e-8; (r₁ ∈ Set.Ioo 1.32471795 1.32471797 ∧ r₁ ^ 3 - r₁ - 1 = 0) ∧ (z₁.re ∈ Set.Icc (-0.66235900) (-0.66235896) ∧ z₁.im ∈ Set.Icc 0.56227949 0.56227953 ∧ z₁ ^ 3 - z₁ - 1 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 3) = a (n + 1) + a n) ∧ a 0 = 1 ∧ a 1 = 1 ∧ a 2 = 1 ↔ ∀ (n : ℕ), a n = (7 / 23 * r₁ ^ 2 + 1 / 23 * r₁ + 3 / 23) * r₁ ^ n + 2 * ((7 / 23 * z₁ ^ 2 + 1 / 23 * z₁ + 3 / 23) * z₁ ^ n).re
The certificate: 1 checked step
A sequence has the recurrence and the initial values exactly when it is the closed form
let r₁ := CAS.AlgRoot.real [-1, -1, 0, 1] 1.32471795 1.32471797; let z₁ := CAS.AlgRoot.complex [-1, -1, 0, 1] (-0.66235898) 0.56227951 2e-8; (r₁ ∈ Set.Ioo 1.32471795 1.32471797 ∧ r₁ ^ 3 - r₁ - 1 = 0) ∧ (z₁.re ∈ Set.Icc (-0.66235900) (-0.66235896) ∧ z₁.im ∈ Set.Icc 0.56227949 0.56227953 ∧ z₁ ^ 3 - z₁ - 1 = 0) ∧ ∀ (a : ℕ → ℝ), (∀ (n : ℕ), a (n + 3) = a (n + 1) + a n) ∧ a 0 = 1 ∧ a 1 = 1 ∧ a 2 = 1 ↔ ∀ (n : ℕ), a n = (7 / 23 * r₁ ^ 2 + 1 / 23 * r₁ + 3 / 23) * r₁ ^ n + 2 * ((7 / 23 * z₁ ^ 2 + 1 / 23 * z₁ + 3 / 23) * z₁ ^ n).re
