The areas / V06. Rings, fields and ordered algebra
Mathlib.Algebra.ContinuedFractions.Basic
54 declarations. Mathlib docs · Source
Basic Definitions/Theorems for Continued Fractions
Summary
We define generalised, simple, and regular continued fractions and functions to evaluate their convergents. We follow the naming conventions from Wikipedia and [wall2018analytic], Chapter 1.
Main definitions
- Generalised continued fractions (gcfs)
- Simple continued fractions (scfs)
- (Regular) continued fractions ((r)cfs)
- Computation of convergents using the recurrence relation in
convs. - Computation of convergents by directly evaluating the fraction described by the gcf in
convs'.
Implementation notes
- The most commonly used kind of continued fractions in the literature are regular continued
fractions. We hence just call them
ContFractin the library. - We use sequences from
Data.Seqto encode potentially infinite sequences.
References
- https://en.wikipedia.org/wiki/Generalized_continued_fraction
- [Wall, H.S., Analytic Theory of Continued Fractions][wall2018analytic]
Tags
numerics, number theory, approximations, fractions
Definitions
Interlude: define some expected coercions and instances.
Interlude: define some expected coercions.
Interlude: define some expected coercions.
Computation of Convergents
We now define how to compute the convergents of a gcf. There are two standard ways to do this:
directly evaluating the (infinite) fraction described by the gcf or using a recurrence relation.
For (r)cfs, these computations are equivalent as shown in
Algebra.ContinuedFractions.ConvergentsEquiv.
We start with the definition of the recurrence relation. Given a gcf g, for all n ≥ 1, we define
A₋₁ = 1, A₀ = h, Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂, andB₋₁ = 0, B₀ = 1, Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂.
Aₙ, Bₙ are called the nth continuants, Aₙ the nth numerator, and Bₙ the
nth denominator of g. The nth convergent of g is given by Aₙ / Bₙ.
theorem GenContFract.mk.injEq E0 #
∀ {α : Type u_1} (h : α) (s : Stream'.Seq (GenContFract.Pair α)) (h_1 : α) (s_1 : Stream'.Seq (GenContFract.Pair α)),
({ h := h, s := s } = { h := h_1, s := s_1 }) = (h = h_1 ∧ s = s_1)
theorem GenContFract.Pair.mk.inj E1 #
∀ {α : Type u_1} {a b a_1 b_1 : α}, { a := a, b := b } = { a := a_1, b := b_1 } → a = a_1 ∧ b = b_1
def GenContFract.ctorIdx E1 #
{α : Type u_1} → GenContFract α → ℕ
theorem GenContFract.Pair.mk.injEq E0 #
∀ {α : Type u_1} (a b a_1 b_1 : α), ({ a := a, b := b } = { a := a_1, b := b_1 }) = (a = a_1 ∧ b = b_1)
def GenContFract.instInhabitedPair.default E1 #
{α : Type u_1} → [Inhabited α] → GenContFract.Pair α
def GenContFract.Pair.ctorIdx E1 #
{α : Type u_1} → GenContFract.Pair α → ℕ
theorem GenContFract.mk.inj E1 #
∀ {α : Type u_1} {h : α} {s : Stream'.Seq (GenContFract.Pair α)} {h_1 : α} {s_1 : Stream'.Seq (GenContFract.Pair α)},
{ h := h, s := s } = { h := h_1, s := s_1 } → h = h_1 ∧ s = s_1
structure GenContFract.Pair E1 #
Type u_1 → Type u_1
We collect a partial numerator aᵢ and partial denominator bᵢ in a pair ⟨aᵢ, bᵢ⟩.
ctor GenContFract.Pair.mk E0 #
{α : Type u_1} → α → α → GenContFract.Pair α
abbrev GenContFract.Pair.a E1 #
{α : Type u_1} → GenContFract.Pair α → α
Partial numerator
abbrev GenContFract.Pair.b E1 #
{α : Type u_1} → GenContFract.Pair α → α
Partial denominator
instance GenContFract.instInhabitedPair E0 #
{a : Type u_2} → [Inhabited a] → Inhabited (GenContFract.Pair a)
instance GenContFract.Pair.instRepr E0 #
{α : Type u_1} → [Repr α] → Repr (GenContFract.Pair α)
Make a GenContFract.Pair printable.
def GenContFract.Pair.map E1 #
{α : Type u_1} → {β : Type u_2} → (α → β) → GenContFract.Pair α → GenContFract.Pair β
Maps a function f on both components of a given pair.
def GenContFract.Pair.coeFn E1 #
{α : Type u_1} → {β : Type u_2} → [Coe α β] → GenContFract.Pair α → GenContFract.Pair β
The coercion between numerator-denominator pairs happens componentwise.
instance GenContFract.Pair.instCoe E0 #
{α : Type u_1} → {β : Type u_2} → [Coe α β] → Coe (GenContFract.Pair α) (GenContFract.Pair β)
Coerce a pair by elementwise coercion.
theorem GenContFract.Pair.coe_toPair E0 #
∀ {α : Type u_1} {β : Type u_2} [inst : Coe α β] {a b : α}, ↑{ a := a, b := b } = { a := Coe.coe a, b := Coe.coe b }
structure GenContFract E1 #
Type u_1 → Type u_1
A generalised continued fraction (gcf) is a potentially infinite expression of the form
$$
h + \dfrac{a_0}
{b_0 + \dfrac{a_1}
{b_1 + \dfrac{a_2}
{b_2 + \dfrac{a_3}
{b_3 + \dots}}}}
$$
where h is called the head term or integer part, the aᵢ are called the
partial numerators and the bᵢ the partial denominators of the gcf.
We store the sequence of partial numerators and denominators in a sequence of GenContFract.Pairs
s.
For convenience, one often writes [h; (a₀, b₀), (a₁, b₁), (a₂, b₂),...].
theorem GenContFract.ext E1 #
∀ {α : Type u_1} {x y : GenContFract α}, x.h = y.h → x.s = y.s → x = y
theorem GenContFract.ext_iff E0 #
∀ {α : Type u_1} {x y : GenContFract α}, x = y ↔ x.h = y.h ∧ x.s = y.s
ctor GenContFract.mk E0 #
{α : Type u_1} → α → Stream'.Seq (GenContFract.Pair α) → GenContFract α
abbrev GenContFract.h E1 #
{α : Type u_1} → GenContFract α → α
Head term
abbrev GenContFract.s E1 #
{α : Type u_1} → GenContFract α → Stream'.Seq (GenContFract.Pair α)
Sequence of partial numerator and denominator pairs.
def GenContFract.ofInteger E1 #
{α : Type u_1} → α → GenContFract α
Constructs a generalized continued fraction without fractional part.
instance GenContFract.instInhabited E0 #
{α : Type u_1} → [Inhabited α] → Inhabited (GenContFract α)
def GenContFract.partNums E1 #
{α : Type u_1} → GenContFract α → Stream'.Seq α
Returns the sequence of partial numerators aᵢ of g.
def GenContFract.partDens E1 #
{α : Type u_1} → GenContFract α → Stream'.Seq α
Returns the sequence of partial denominators bᵢ of g.
def GenContFract.TerminatedAt E1 #
{α : Type u_1} → GenContFract α → ℕ → Prop
A gcf terminated at position n if its sequence terminates at position n.
instance GenContFract.instDecidableTerminatedAt E0 #
{α : Type u_1} → (g : GenContFract α) → (n : ℕ) → Decidable (g.TerminatedAt n)
def GenContFract.Terminates E1 #
{α : Type u_1} → GenContFract α → Prop
A gcf terminates if its sequence terminates.
def GenContFract.coeFn E1 #
{α : Type u_1} → {β : Type u_2} → [Coe α β] → GenContFract α → GenContFract β
The coercion between GenContFract happens on the head term
and all numerator-denominator pairs componentwise.
instance GenContFract.instCoe E0 #
{α : Type u_1} → {β : Type u_2} → [Coe α β] → Coe (GenContFract α) (GenContFract β)
Coerce a gcf by elementwise coercion.
theorem GenContFract.coe_toGenContFract E0 #
∀ {α : Type u_1} {β : Type u_2} [inst : Coe α β] {g : GenContFract α},
↑g = { h := Coe.coe g.h, s := Stream'.Seq.map GenContFract.Pair.coeFn g.s }
def GenContFract.IsSimpContFract E1 #
{α : Type u_1} → GenContFract α → [One α] → Prop
A generalized continued fraction is a simple continued fraction if all partial numerators are equal to one. $$ h + \dfrac{1} {b_0 + \dfrac{1} {b_1 + \dfrac{1} {b_2 + \dfrac{1} {b_3 + \dots}}}} $$
def SimpContFract E1 #
(α : Type u_1) → [One α] → Type (max 0 u_1)
A simple continued fraction (scf) is a generalized continued fraction (gcf) whose partial
numerators are equal to one.
$$
h + \dfrac{1}
{b_0 + \dfrac{1}
{b_1 + \dfrac{1}
{b_2 + \dfrac{1}
{b_3 + \dots}}}}
$$
For convenience, one often writes [h; b₀, b₁, b₂,...].
It is encoded as the subtype of gcfs that satisfy GenContFract.IsSimpContFract.
def SimpContFract.ofInteger E1 #
{α : Type u_1} → [inst : One α] → α → SimpContFract α
Constructs a simple continued fraction without fractional part.
instance SimpContFract.instInhabited E0 #
{α : Type u_1} → [inst : One α] → Inhabited (SimpContFract α)
instance SimpContFract.instCoeGenContFract E0 #
{α : Type u_1} → [inst : One α] → Coe (SimpContFract α) (GenContFract α)
Lift a scf to a gcf using the inclusion map.
def SimpContFract.IsContFract E1 #
{α : Type u_1} → [inst : One α] → [Zero α] → [LT α] → SimpContFract α → Prop
A simple continued fraction is a (regular) continued fraction ((r)cf) if all partial denominators
bᵢ are positive, i.e. 0 < bᵢ.
def ContFract E3 #
(α : Type u_1) → [One α] → [Zero α] → [LT α] → Type (max 0 u_1)
A (regular) continued fraction ((r)cf) is a simple continued fraction (scf) whose partial
denominators are all positive. It is the subtype of scfs that satisfy SimpContFract.IsContFract.
def ContFract.ofInteger E1 #
{α : Type u_1} → [inst : One α] → [inst_1 : Zero α] → [inst_2 : LT α] → α → ContFract α
Constructs a continued fraction without fractional part.
instance ContFract.instInhabited E0 #
{α : Type u_1} → [inst : One α] → [inst_1 : Zero α] → [inst_2 : LT α] → Inhabited (ContFract α)
instance ContFract.instCoeSimpContFract E0 #
{α : Type u_1} → [inst : One α] → [inst_1 : Zero α] → [inst_2 : LT α] → Coe (ContFract α) (SimpContFract α)
Lift a cf to a scf using the inclusion map.
instance ContFract.instCoeGenContFract E0 #
{α : Type u_1} → [inst : One α] → [inst_1 : Zero α] → [inst_2 : LT α] → Coe (ContFract α) (GenContFract α)
Lift a cf to a scf using the inclusion map.
def GenContFract.nextNum E1 #
{K : Type u_2} → [DivisionRing K] → K → K → K → K → K
Returns the next numerator Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂, where predA is Aₙ₋₁,
ppredA is Aₙ₋₂, a is aₙ₋₁, and b is bₙ₋₁.
def GenContFract.nextDen E1 #
{K : Type u_2} → [DivisionRing K] → K → K → K → K → K
Returns the next denominator Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂, where predB is Bₙ₋₁ and
ppredB is Bₙ₋₂, a is aₙ₋₁, and b is bₙ₋₁.
def GenContFract.nextConts E1 #
{K : Type u_2} → [DivisionRing K] → K → K → GenContFract.Pair K → GenContFract.Pair K → GenContFract.Pair K
Returns the next continuants ⟨Aₙ, Bₙ⟩ using nextNum and nextDen, where pred
is ⟨Aₙ₋₁, Bₙ₋₁⟩, ppred is ⟨Aₙ₋₂, Bₙ₋₂⟩, a is aₙ₋₁, and b is bₙ₋₁.
def GenContFract.contsAux E0 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → Stream' (GenContFract.Pair K)
Returns the continuants ⟨Aₙ₋₁, Bₙ₋₁⟩ of g.
def GenContFract.conts E1 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → Stream' (GenContFract.Pair K)
Returns the continuants ⟨Aₙ, Bₙ⟩ of g.
def GenContFract.nums E1 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → Stream' K
Returns the numerators Aₙ of g.
def GenContFract.dens E1 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → Stream' K
Returns the denominators Bₙ of g.
def GenContFract.convs E3 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → Stream' K
Returns the convergents Aₙ / Bₙ of g, where Aₙ, Bₙ are the nth continuants of g.
def GenContFract.convs'Aux E0 #
{K : Type u_2} → [DivisionRing K] → Stream'.Seq (GenContFract.Pair K) → ℕ → K
Returns the approximation of the fraction described by the given sequence up to a given position n.
For example, convs'Aux [(1, 2), (3, 4), (5, 6)] 2 = 1 / (2 + 3 / 4) and
convs'Aux [(1, 2), (3, 4), (5, 6)] 0 = 0.
def GenContFract.convs' E3 #
{K : Type u_2} → [DivisionRing K] → GenContFract K → ℕ → K
Returns the convergents of g by evaluating the fraction described by g up to a given
position n. For example, convs' [9; (1, 2), (3, 4), (5, 6)] 2 = 9 + 1 / (2 + 3 / 4) and
convs' [9; (1, 2), (3, 4), (5, 6)] 0 = 9