Mode of #cas
fromContinuedFraction
The rational value of a finite continued fraction , with a theorem in .
#cas [fromContinuedFraction] [a₀, a₁, …, aₖ]fromContinuedFraction takes the terms of a continued fraction and gives back its value, as Mathematica’s FromContinuedFraction does. It undoes continuedFraction. Where I use it most is for convergents. Cut an expansion off after a few terms and you get a fraction that is closer to the number than every fraction with a smaller denominator, which is why and approximate so well. The theorem is the identity in , and Mathlib’s tactic norm_num proves it.
Examples (14)#
Basic Examples (2)
The value of :
#cas [fromContinuedFraction] [4, 2, 6, 7]
rational number
415/93- Domain
ℚ- Input (continued fraction)
[4, 2, 6, 7]
Checked by Lean’s kernel: Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7))
The certificate: 1 checked step
Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7))
Three ones:
#cas [fromContinuedFraction] [1, 1, 1]
rational number
3/2- Domain
ℚ- Input (continued fraction)
[1, 1, 1]
Checked by Lean’s kernel: Value of the continued fraction
3 / 2 = 1 + 1 / (1 + 1 / 1)
The certificate: 1 checked step
Value of the continued fraction
3 / 2 = 1 + 1 / (1 + 1 / 1)
Scope (2)
The first term may be negative:
#cas [fromContinuedFraction] [-3, 1, 2]
rational number
-7/3- Domain
ℚ- Input (continued fraction)
[-3, 1, 2]
Checked by Lean’s kernel: Value of the continued fraction
-(7 / 3) = -3 + 1 / (1 + 1 / 2)
The certificate: 1 checked step
Value of the continued fraction
-(7 / 3) = -3 + 1 / (1 + 1 / 2)
A first term 0 gives a number between 0 and 1:
#cas [fromContinuedFraction] [0, 1, 2]
rational number
2/3- Domain
ℚ- Input (continued fraction)
[0, 1, 2]
Checked by Lean’s kernel: Value of the continued fraction
2 / 3 = 0 + 1 / (1 + 1 / 2)
The certificate: 1 checked step
Value of the continued fraction
2 / 3 = 0 + 1 / (1 + 1 / 2)
Applications (3)
The theorem as a statement in ℚ. Only the three standard axioms are used:
theorem from_continued_fraction_ones : (3 / 2 : ℚ) = 1 + 1 / (1 + 1 / 1) := by cas #print axioms from_continued_fraction_ones
from_continued_fraction_ones depends on the axioms propext, Classical.choice, Quot.sound.
, and its convergents are the classical approximations. is Archimedes’ , and is Zu Chongzhi’s , which is good to six decimals because the next term 292 is large:
#cas [fromContinuedFraction] [3, 7]
rational number
22/7- Domain
ℚ- Input (continued fraction)
[3, 7]
Checked by Lean’s kernel: Value of the continued fraction
22 / 7 = 3 + 1 / 7
The certificate: 1 checked step
Value of the continued fraction
22 / 7 = 3 + 1 / 7
#cas [fromContinuedFraction] [3, 7, 15, 1]
rational number
355/113- Domain
ℚ- Input (continued fraction)
[3, 7, 15, 1]
Checked by Lean’s kernel: Value of the continued fraction
355 / 113 = 3 + 1 / (7 + 1 / (15 + 1 / 1))
The certificate: 1 checked step
Value of the continued fraction
355 / 113 = 3 + 1 / (7 + 1 / (15 + 1 / 1))
The convergents of solve Pell’s equation . Six terms give , and :
#cas [fromContinuedFraction] [1, 2, 2, 2, 2, 2]
rational number
99/70- Domain
ℚ- Input (continued fraction)
[1, 2, 2, 2, 2, 2]
Checked by Lean’s kernel: Value of the continued fraction
99 / 70 = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))))
The certificate: 1 checked step
Value of the continued fraction
99 / 70 = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))))
example : (99 : ℤ) ^ 2 - 2 * 70 ^ 2 = 1 := by norm_num
Lean accepts the proof.
Properties and Relations (2)
continuedFraction goes the other way and gives back the terms:
#cas [continuedFraction] (415/93)
continued fraction
[4; 2, 6, 7]- Domain
ℚ- Input (rational number)
415/93
Checked by Lean’s kernel: Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7)) ∧ (∀ a ∈ [2, 6, 7], 1 ≤ a) ∧ 2 ≤ 7
The certificate: 1 checked step
Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7)) ∧ (∀ a ∈ [2, 6, 7], 1 ≤ a) ∧ 2 ≤ 7
[1, 1, 1] and [1, 2] have the same value, and continuedFraction gives the one whose last term is at least 2:
#cas [fromContinuedFraction] [1, 2]
rational number
3/2- Domain
ℚ- Input (continued fraction)
[1, 2]
Checked by Lean’s kernel: Value of the continued fraction
3 / 2 = 1 + 1 / 2
The certificate: 1 checked step
Value of the continued fraction
3 / 2 = 1 + 1 / 2
#cas [continuedFraction] (3/2)
continued fraction
[1; 2]- Domain
ℚ- Input (rational number)
3/2
Checked by Lean’s kernel: Value of the continued fraction
3 / 2 = 1 + 1 / 2 ∧ (∀ a ∈ [2], 1 ≤ a) ∧ 2 ≤ 2
The certificate: 1 checked step
Value of the continued fraction
3 / 2 = 1 + 1 / 2 ∧ (∀ a ∈ [2], 1 ≤ a) ∧ 2 ≤ 2
Possible Issues (4)
A continued fraction has at least the term :
#cas [fromContinuedFraction] []
Error
fromContinuedFraction: the list is empty
Only the first term may be zero or negative:
#cas [fromContinuedFraction] [1, 0, 2]
Error
fromContinuedFraction: the terms after the first must be positive
The terms are integers:
#cas [fromContinuedFraction] [1, 1/2]
Error
fromContinuedFraction: expected integers
The terms go in a list, in square brackets:
#cas [fromContinuedFraction] (4, 2, 6, 7)
Error
fromContinuedFraction: expected a list `[a₀, a₁, …]`
Neat Examples (1)
Ten ones give the quotient of the fibonacci numbers and , a convergent of the golden ratio :
#cas [fromContinuedFraction] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
rational number
89/55- Domain
ℚ- Input (continued fraction)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Checked by Lean’s kernel: Value of the continued fraction
89 / 55 = 1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / 1))))))))
The certificate: 1 checked step
Value of the continued fraction
89 / 55 = 1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / 1))))))))
