The areas / V06. Rings, fields and ordered algebra
Mathlib.Algebra.ContinuedFractions.Computation.TerminatesIffRat
20 declarations. Mathlib docs · Source
Termination of Continued Fraction Computations (GenContFract.of)
We show that the continued fraction for a value v, as defined in
Mathlib/Algebra/ContinuedFractions/Basic.lean, terminates if and only if v corresponds to a
rational number, that is ↑v = q for some q : ℚ.
Main Theorems
GenContFract.coe_of_rat_eqshows thatGenContFract.of v = GenContFract.of qforv : αgiven that↑v = qandq : ℚ.GenContFract.terminates_iff_ratshows thatGenContFract.of vterminates if and only if↑v = qfor someq : ℚ.
Tags
rational, continued fraction, termination
Terminating Continued Fractions Are Rational
We want to show that the computation of a continued fraction GenContFract.of v
terminates if and only if v ∈ ℚ. In this section, we show the implication from left to right.
We first show that every finite convergent corresponds to a rational number q and then use the
finite correctness proof (of_correctness_of_terminates) of GenContFract.of to show that
v = ↑q.
Technical Translation Lemmas
Before we can show that the continued fraction of a rational number terminates, we have to prove
some technical translation lemmas. More precisely, in this section, we show that, given a rational
number q : ℚ and value v : K with v = ↑q, the continued fraction of q and v coincide.
In particular, we show that
(↑(GenContFract.of q : GenContFract ℚ) : GenContFract K) = GenContFract.of v
in GenContFract.coe_of_rat_eq.
To do this, we proceed bottom-up, showing the correspondence between the basic functions involved in the Computation first and then lift the results step-by-step.
First, we show the correspondence for the very basic functions in
GenContFract.IntFractPair.
Now we lift the coercion results to the continued fraction computation.
Continued Fractions of Rationals Terminate
Finally, we show that the continued fraction of a rational number terminates.
The crucial insight is that, given any q : ℚ with 0 < q < 1, the numerator of Int.fract q is
smaller than the numerator of q. As the continued fraction computation recursively operates on
the fractional part of a value v and 0 ≤ Int.fract v < 1, we infer that the numerator of the
fractional part in the computation decreases by at least one in each step. As 0 ≤ Int.fract v,
this process must stop after finite number of steps, and the computation hence terminates.
theorem GenContFract.exists_gcf_pair_rat_eq_of_nth_contsAux E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] (v : K) (n : ℕ),
∃ conts, (GenContFract.of v).contsAux n = GenContFract.Pair.map Rat.cast conts
theorem GenContFract.exists_gcf_pair_rat_eq_nth_conts E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] (v : K) (n : ℕ),
∃ conts, (GenContFract.of v).conts n = GenContFract.Pair.map Rat.cast conts
theorem GenContFract.exists_rat_eq_nth_num E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] (v : K) (n : ℕ),
∃ q, (GenContFract.of v).nums n = ↑q
theorem GenContFract.exists_rat_eq_nth_den E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] (v : K) (n : ℕ),
∃ q, (GenContFract.of v).dens n = ↑q
theorem GenContFract.exists_rat_eq_nth_conv E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] (v : K) (n : ℕ),
∃ q, (GenContFract.of v).convs n = ↑q
Every finite convergent corresponds to a rational number.
theorem GenContFract.exists_rat_eq_of_terminates E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] {v : K},
(GenContFract.of v).Terminates → ∃ q, v = ↑q
Every terminating continued fraction corresponds to a rational number.
theorem GenContFract.IntFractPair.coe_of_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ},
v = ↑q → GenContFract.IntFractPair.mapFr Rat.cast (GenContFract.IntFractPair.of q) = GenContFract.IntFractPair.of v
theorem GenContFract.IntFractPair.coe_stream_nth_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ},
v = ↑q →
∀ (n : ℕ),
Option.map (GenContFract.IntFractPair.mapFr Rat.cast) (GenContFract.IntFractPair.stream q n) =
GenContFract.IntFractPair.stream v n
theorem GenContFract.IntFractPair.coe_stream'_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ},
v = ↑q →
Stream'.map (Option.map (GenContFract.IntFractPair.mapFr Rat.cast)) (GenContFract.IntFractPair.stream q) =
GenContFract.IntFractPair.stream v
theorem GenContFract.coe_of_h_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ}, v = ↑q → ↑(GenContFract.of q).h = (GenContFract.of v).h
theorem GenContFract.coe_of_s_get?_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ},
v = ↑q →
∀ (n : ℕ), Option.map (GenContFract.Pair.map Rat.cast) ((GenContFract.of q).s.get? n) = (GenContFract.of v).s.get? n
theorem GenContFract.coe_of_s_rat_eq E0 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ}, v = ↑q → Stream'.Seq.map (GenContFract.Pair.map Rat.cast) (GenContFract.of q).s = (GenContFract.of v).s
theorem GenContFract.coe_of_rat_eq E3 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ},
v = ↑q →
{ h := ↑(GenContFract.of q).h, s := Stream'.Seq.map (GenContFract.Pair.map Rat.cast) (GenContFract.of q).s } =
GenContFract.of v
Given (v : K), (q : ℚ), and v = q, we have that of q = of v
theorem GenContFract.of_terminates_iff_of_rat_terminates E1 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] {v : K}
{q : ℚ}, v = ↑q → ((GenContFract.of v).Terminates ↔ (GenContFract.of q).Terminates)
theorem GenContFract.IntFractPair.of_inv_fr_num_lt_num_of_pos E1 #
∀ {q : ℚ}, 0 < q → (GenContFract.IntFractPair.of q⁻¹).fr.num < q.num
Shows that for any q : ℚ with 0 < q < 1, the numerator of the fractional part of
IntFractPair.of q⁻¹ is smaller than the numerator of q.
theorem GenContFract.IntFractPair.stream_succ_nth_fr_num_lt_nth_fr_num_rat E1 #
∀ {q : ℚ} {n : ℕ} {ifp_n ifp_succ_n : GenContFract.IntFractPair ℚ},
GenContFract.IntFractPair.stream q n = some ifp_n →
GenContFract.IntFractPair.stream q (n + 1) = some ifp_succ_n → ifp_succ_n.fr.num < ifp_n.fr.num
Shows that the sequence of numerators of the fractional parts of the stream is strictly antitone.
theorem GenContFract.IntFractPair.stream_nth_fr_num_le_fr_num_sub_n_rat E1 #
∀ {q : ℚ} {n : ℕ} {ifp_n : GenContFract.IntFractPair ℚ},
GenContFract.IntFractPair.stream q n = some ifp_n → ifp_n.fr.num ≤ (GenContFract.IntFractPair.of q).fr.num - ↑n
theorem GenContFract.IntFractPair.exists_nth_stream_eq_none_of_rat E1 #
∀ (q : ℚ), ∃ n, GenContFract.IntFractPair.stream q n = none
theorem GenContFract.terminates_of_rat E1 #
∀ (q : ℚ), (GenContFract.of q).Terminates
The continued fraction of a rational number terminates.
theorem GenContFract.terminates_iff_rat E3 #
∀ {K : Type u_1} [inst : Field K] [inst_1 : LinearOrder K] [inst_2 : FloorRing K] [IsStrictOrderedRing K] (v : K),
(GenContFract.of v).Terminates ↔ ∃ q, v = ↑q
The continued fraction GenContFract.of v terminates if and only if v ∈ ℚ.