continuedfractions.continuedfraction#
- class continuedfractions.continuedfraction.ContinuedFraction(numerator=0, denominator=None)[source]#
An object-oriented representation of a (finite) simple continued fraction.
An implementation of simple continued fractions as Python objects and instances of the standard library
fractions.Fractionclass, with various properties for the continued fraction, including its coefficients, the order, convergents, and remainders.The term “simple continued fraction” denotes a specific type of continued fraction where the fractional terms only have numerators of \(1\).
Arguments can be any which are valid for creating objects of the
fractions.Fractionsuperclass.For clarification, valid arguments can be one of the following:
a single instance of
numbers.Rational, includingint,fractions.FractionorContinuedFraction, named or unnameda pair of
numbers.Rationalinstances, includingint,fractions.FractionandContinuedFraction, named or unnameda single
floatordecimal.Decimalvalue that is not a special value such asmath.nan,float('inf'), orDecimal('infinity')a single numeric valid string (
str) - validity is determined in the superclass by thefractions._RATIONAL_FORMATtest
Attributes
A generator of the (ordered) sequence of coefficients of the continued fraction.
Generates an enumerated sequence of all convergents of the continued fraction.
A counter for the coefficients.
Generates an enumerated sequence of all even-order convergents of the continued fraction.
The height of this rational number.
The Khinchin mean of the continued fraction, which is defined as the geometric mean of all the tail coefficients.
The (natural) logarithm of the height of this rational number as defined above.
Generates an enumerated sequence of all odd-order convergents of the continued fraction.
The order of the continued fraction, which is the number of its coefficients minus \(1\).
Generates an enumerated sequence of all remainders of the continued fraction in descending order of index.
Methods
Returns the
decimal.Decimalvalue of the continued fraction.convergent(k, /)Returns the \(k\)-th (simple) convergent of the continued fraction.
extend(*new_coeffs)Performs an in-place tail extension of the current sequence of coefficients.
from_coefficients(*coeffs)Returns a
ContinuedFractioninstance from a sequence of (integer) coefficients of a (finite) simple continued fraction.left_mediant(other, /, *[, k])Returns the \(k\)-th left-mediant of the continued fraction with another rational number.
mediant(other, /)Returns the simple mediant of the continued fraction with another continued fraction.
remainder(k, /)Returns the \(k\)-th remainder of the continued fraction.
right_mediant(other, /, *[, k])Returns the \(k\)-th right-mediant of the continued fraction with another rational number.
semiconvergent(k, m, /)Returns the \(m\)-th semiconvergent of two consecutive convergents \(p_{k - 1}\) and \(p_k\) of the continued fraction.
truncate(*tail_coeffs)Performs an in-place truncation of a contiguous trailing segment of the coefficients in the tail.
- property coefficients: Generator[int, None, None]#
A generator of the (ordered) sequence of coefficients of the continued fraction.
Examples
>>> cf = ContinuedFraction('.12345') >>> cf ContinuedFraction(2469, 20000) >>> tuple(cf.coefficients) (0, 8, 9, 1, 21, 1, 1, 5)
- Type:
- property order: int#
The order of the continued fraction, which is the number of its coefficients minus \(1\).
Examples
>>> cf = ContinuedFraction('.12345') >>> cf ContinuedFraction(2469, 20000) >>> tuple(cf.coefficients) (0, 8, 9, 1, 21, 1, 1, 5) >>> cf.order 7
- Type:
- property counter: Counter#
A counter for the coefficients.
Examples
>>> cf = ContinuedFraction(928374923, 8249234) >>> cf.counter Counter({1: 6, 2: 3, 24: 2, 112: 1, 5: 1, 3: 1})
- Type:
- property khinchin_mean: Decimal | None#
The Khinchin mean of the continued fraction, which is defined as the geometric mean of all the tail coefficients.
We define the Khinchin mean \(K_n\) of a (simple) continued fraction \([a_0; a_1, a_2, \ldots, a_n]\) as:
\[K_n := \sqrt[n]{a_1a_2 \cdots a_n} = \left( a_1a_2 \cdots a_n \right)^{\frac{1}{n}}, \hskip{3em} n \geq 1\]This property is intended to make it easier to study the limit of \(K_n\) as \(n \to \infty\).
In the special case of integers or fractions representing integers, whose continued fraction representations consist of only a single element, a null value is returned.
Examples
Note that the default
decimalcontext precision of \(28\) is used in these examples.>>> tuple(ContinuedFraction(649, 200).coefficients) (3, 4, 12, 4) >>> ContinuedFraction(649, 200).khinchin_mean Decimal('5.76899828122963409526846589869819581508636474609375') >>> tuple(ContinuedFraction(415, 93).coefficients) (4, 2, 6, 7) >>> ContinuedFraction(415, 93).khinchin_mean Decimal('4.37951913988788898990378584130667150020599365234375') >>> tuple((ContinuedFraction(649, 200) + ContinuedFraction(415, 93)).coefficients) (7, 1, 2, 2, 2, 1, 1, 11, 1, 2, 12) >>> (ContinuedFraction(649, 200) + ContinuedFraction(415, 93)).khinchin_mean Decimal('2.15015313349074244086978069390170276165008544921875') >>> ContinuedFraction(5000).khinchin_mean
- Type:
- classmethod from_coefficients(*coeffs: int) ContinuedFraction[source]#
Returns a
ContinuedFractioninstance from a sequence of (integer) coefficients of a (finite) simple continued fraction.Invalid coefficients will trigger a
ValueError.- Parameters:
- *coeffsint
An ordered sequence of integer coefficients of a (finite) simple continued fraction.
- Returns:
- ContinuedFraction
A new and fully initialised instance of
ContinuedFractionwith the given sequence of coefficients.
- Raises:
- ValueError
If the given coefficients include non-integers, or where the tail segment contains non-positive integers.
Examples
Constructing a continued fraction for the rational \(\frac{649}{200}\) using the sequence of coefficients \(3, 4, 12, 4\).
>>> c1 = ContinuedFraction.from_coefficients(3, 4, 12, 4) >>> c1 ContinuedFraction(649, 200)
Constructing the continued fraction of the (multiplicative) inverse \(\frac{200}{649}\) using the sequence of coefficients \(0, 3, 4, 12, 4\).
>>> c2 = ContinuedFraction.from_coefficients(0, 3, 4, 12, 4) >>> c2 ContinuedFraction(200, 649)
Validation of coefficients.
>>> ContinuedFraction.from_coefficients('0', 1) Traceback (most recent call last): ... ValueError: Continued fraction coefficients must be integers, and all coefficients from the 1st onwards must be positive. >>> ContinuedFraction.from_coefficients(0, 1, 2.5) Traceback (most recent call last): ... ValueError: Continued fraction coefficients must be integers, and all coefficients from the 1st onwards must be positive. >>> ContinuedFraction.from_coefficients(1, 0) Traceback (most recent call last): ... ValueError: Continued fraction coefficients must be integers, and all coefficients from the 1st onwards must be positive. >>> ContinuedFraction.from_coefficients(1, -1) Traceback (most recent call last): ... ValueError: Continued fraction coefficients must be integers, and all coefficients from the 1st onwards must be positive.
- extend(*new_coeffs: int) None[source]#
Performs an in-place tail extension of the current sequence of coefficients.
Raises a
ValueErrorif the given sequence of values is empty, or includes non-integers or non-positive integers.Note
As this method performs an in-place modification of the existing/ current instance the object ID remains the same.
- Parameters:
- new_coeffsint
An (ordered) sequence of new (positive) integer coefficients by which the tail of the existing sequence of coefficients is to be extended.
- Raises:
- ValueError
If the given sequence is empty or includes non-integers or non- positive integers.
Examples
>>> cf = ContinuedFraction('3.245') >>> cf ContinuedFraction(649, 200) >>> tuple(cf.coefficients) (3, 4, 12, 4) >>> cf.order 3 >>> tuple(cf.convergents) ((0, ContinuedFraction(3, 1)), (1, ContinuedFraction(13, 4)), (2, ContinuedFraction(159, 49)), (3, ContinuedFraction(649, 200))) >>> tuple(cf.remainders) ((3, ContinuedFraction(4, 1)), (2, ContinuedFraction(49, 4)), (1, ContinuedFraction(200, 49)), (0, ContinuedFraction(649, 200))) >>> cf.extend(5, 2) >>> cf ContinuedFraction(7457, 2298) >>> tuple(cf.coefficients) (3, 4, 12, 4, 5, 2) >>> cf.order 5 >>> tuple(cf.convergents) ((0, ContinuedFraction(3, 1)), (1, ContinuedFraction(13, 4)), (2, ContinuedFraction(159, 49)), (3, ContinuedFraction(649, 200)), (4, ContinuedFraction(3404, 1049)), (5, ContinuedFraction(7457, 2298))) >>> tuple(cf.remainders) ((5, ContinuedFraction(2, 1)), (4, ContinuedFraction(11, 2)), (3, ContinuedFraction(46, 11)), (2, ContinuedFraction(563, 46)), (1, ContinuedFraction(2298, 563)), (0, ContinuedFraction(7457, 2298))) >>> cf = ContinuedFraction(649, 200) >>> cf.extend(0, 1) Traceback (most recent call last): ... ValueError: The coefficients to be added to the tail must be positive integers. >>> cf.extend(1, -1) Traceback (most recent call last): ... ValueError: The coefficients to be added to the tail must be positive integers.
- truncate(*tail_coeffs: int) None[source]#
Performs an in-place truncation of a contiguous trailing segment of the coefficients in the tail.
Note
As this method performs an in-place modification of the existing/ current instance the object ID remains the same.
- Parameters:
- tail_coeffsint
An (ordered) sequence of (positive) integers which form a contiguous trailing segment of the current sequence of coefficients and is to be truncated.
- Raises:
- ValueError
If the tail coefficients provided are not positive integers, or do not form a contiguous trailing segment, or their number exceeds the length of the existing tail, i.e. the order of the continued fraction represented by the instance.
Examples
>>> cf = ContinuedFraction('3.245') >>> cf ContinuedFraction(649, 200) >>> tuple(cf.coefficients) (3, 4, 12, 4) >>> cf.order 3 >>> cf.counter Counter({4: 2, 3: 1, 12: 1}) >>> tuple(cf.convergents) ((0, ContinuedFraction(3, 1)), (1, ContinuedFraction(13, 4)), (2, ContinuedFraction(159, 49)), (3, ContinuedFraction(649, 200))) >>> tuple(cf.remainders) ((3, ContinuedFraction(4, 1)), (2, ContinuedFraction(49, 4)), (1, ContinuedFraction(200, 49)), (0, ContinuedFraction(649, 200))) >>> cf.truncate(12, 4) >>> cf ContinuedFraction(13, 4) >>> tuple(cf.coefficients) (3, 4) >>> cf.order 1 >>> tuple(cf.convergents) ((0, ContinuedFraction(3, 1)), (1, ContinuedFraction(13, 4))) >>> tuple(cf.remainders) ((1, ContinuedFraction(4, 1)), (0, ContinuedFraction(13, 4))) >>> cf = ContinuedFraction(649, 200) >>> cf.truncate(4, 12) Traceback (most recent call last): ... ValueError: The coefficients to be truncated from the tail must consist of positive integers and form a contiguous trailing segment of the tail. >>> cf.truncate(3, 4, 12, 4) Traceback (most recent call last): ... ValueError: The coefficients to be truncated from the tail must consist of positive integers and form a contiguous trailing segment of the tail.
- as_decimal() Decimal[source]#
Returns the
decimal.Decimalvalue of the continued fraction.- Returns:
- decimal.Decimal
The
decimal.Decimalrepresentation of the continued fraction.
Examples
Note that the default
decimalcontext precision of \(28\) is used in these examples.>>> import math >>> math.pi 3.141592653589793
Now construct a
ContinuedFractioninstance from it, and check thefloatvalue.>>> cf = ContinuedFraction(math.pi) >>> cf ContinuedFraction(884279719003555, 281474976710656) >>> cf.as_decimal() Decimal('3.141592653589793115997963469')
- convergent(k: int, /) ContinuedFraction[source]#
Returns the \(k\)-th (simple) convergent of the continued fraction.
Given a (simple) continued fraction \([a_0;a_1,a_2,\ldots]\) the \(k\)-th convergent is defined as:
\[C_k = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 \ddots \cfrac{1}{a_{k-1} + \cfrac{1}{a_k}}}}\]The result is a
ContinuedFractioninstance.If the continued fraction is of order \(n\) then it has exactly \(n + 1\) convergents \(C_0,C_1,C_2,\ldots,C_n\) where the \(k\)-th convergent \(C_k = \frac{p_k}{q_k}\) is given by the recurrence relation:
\[\begin{split}\begin{align} p_k &= a_kp_{k - 1} + p_{k - 2} \\ q_k &= a_kq_{k - 1} + q_{k - 2}, \hskip{3em} k \geq 3 \end{align}\end{split}\]where \(p_0 = a_0\), \(q_0 = 1\), \(p_1 = p_1p_0 + 1\), and \(q_1 = p_1\).
- Parameters:
- kint
The index of the convergent, as described above.
- Returns:
- ContinuedFraction
A new
ContinuedFractioninstance representing the \(k\)-th (simple) convergent of the original continued fraction, as described above.
Examples
>>> cf = ContinuedFraction('.12345') >>> cf ContinuedFraction(2469, 20000) >>> cf.convergent(0) ContinuedFraction(0, 1) >>> cf.convergent(2) ContinuedFraction(9, 73) >>> cf.convergent(6) ContinuedFraction(448, 3629) >>> cf.convergent(7) ContinuedFraction(2469, 20000)
- property convergents: Generator[tuple[int, ContinuedFraction], None, None]#
Generates an enumerated sequence of all convergents of the continued fraction.
The convergents are generated as tuples of
intandContinuedFractioninstances, where the integers represent the indices of the convergents.If \(n\) is the order of the continued fraction then \(n + 1\) convergents \(C_0, C_1, \ldots, C_n\) are generated in that order.
- Yields:
- tuple
A tuple of convergent index (
int) and convergents (ContinuedFraction) of the continued fraction.
Examples
>>> cf = ContinuedFraction('3.245') >>> tuple(cf.convergents) ((0, ContinuedFraction(3, 1)), (1, ContinuedFraction(13, 4)), (2, ContinuedFraction(159, 49)), (3, ContinuedFraction(649, 200)))
- property even_order_convergents: Generator[tuple[int, ContinuedFraction], None, None]#
Generates an enumerated sequence of all even-order convergents of the continued fraction.
The convergents are generated as tuples of
intandContinuedFractioninstances, where the integers represent the indices of the convergents.If \(n\) is the order of the continued fraction then only the even- indexed convergents \(C_0, C_2, C_4, \ldots\) are generated.
- Yields:
- tuple
A tuple of convergent index (
int) and convergents (ContinuedFraction) of the continued fraction.
Examples
>>> tuple(ContinuedFraction('3.245').even_order_convergents) ((0, ContinuedFraction(3, 1)), (2, ContinuedFraction(159, 49)))
- property odd_order_convergents: Generator[tuple[int, ContinuedFraction], None, None]#
Generates an enumerated sequence of all odd-order convergents of the continued fraction.
The convergents are generated as tuples of
intandContinuedFractioninstances, where the integers represent the indices of the convergents.If \(n\) is the order of the continued fraction then only the odd- indexed convergents \(C_1, C_3, C_5, \ldots\) are generated.
- Yields:
- tuple
A tuple of convergent index (
int) and convergents (ContinuedFraction) of the continued fraction.
Examples
>>> tuple(ContinuedFraction('3.245').odd_order_convergents) ((1, ContinuedFraction(13, 4)), (3, ContinuedFraction(649, 200)))
- semiconvergent(k: int, m: int, /) ContinuedFraction[source]#
Returns the \(m\)-th semiconvergent of two consecutive convergents \(p_{k - 1}\) and \(p_k\) of the continued fraction.
The integer \(k\) must be positive and determine two consecutive convergents \(p_{k - 1}\) and \(p_k\) of a (finite, simple) continued fraction.
The integer \(m\) can be any positive integer.
- Parameters:
- kint
The integer \(k\) determining two consecutive convergents \(p_{k - 1}\) and \(p_k\) of a (finite, simple) continued fraction \([a_0; a_1, \ldots, a_{k}, a_{k + 1}, \ldots, a_n]\).
- mint
The index of the semiconvergent of the convergents \(p_{k - 1}\) and \(p_k\).
- Returns:
- ContinuedFraction
The \(m\)-th semiconvergent of the convergents \(p_{k - 1}\) and \(p_k\).
- Raises:
- ValueError
If \(k\) or \(m\) are not positive integers, or \(k\) is an integer that does not satisfy \(1 \leq k \leq n\) where \(n\) is the order of the (finite, simple) continued fraction of which \(p_{k - 1}\) and \(p_k\) are convergents.
Examples
>>> cf = ContinuedFraction(-415, 93) >>> tuple(cf.coefficients) (-5, 1, 1, 6, 7) >>> tuple(cf.convergents) ((0, ContinuedFraction(-5, 1)), (1, ContinuedFraction(-4, 1)), (2, ContinuedFraction(-9, 2)), (3, ContinuedFraction(-58, 13)), (4, ContinuedFraction(-415, 93))) >>> cf.semiconvergent(3, 1) ContinuedFraction(-67, 15) >>> cf.semiconvergent(3, 2) ContinuedFraction(-125, 28) >>> cf.semiconvergent(3, 3) ContinuedFraction(-183, 41) >>> cf.semiconvergent(3, 4) ContinuedFraction(-241, 54) >>> cf.semiconvergent(3, 5) ContinuedFraction(-299, 67) >>> cf.semiconvergent(3, 6) ContinuedFraction(-357, 80) >>> cf.semiconvergent(3, 7) ContinuedFraction(-415, 93)
- remainder(k: int, /) ContinuedFraction[source]#
Returns the \(k\)-th remainder of the continued fraction.
Given a (simple) continued fraction \([a_0;a_1,a_2,\ldots]\) the \(k\)-th remainder \(R_k\) is the (simple) continued fraction \([a_k; a_{k + 1}, a_{k + 2}, \ldots]\):
\[R_k = a_k + \cfrac{1}{a_{k + 1} + \cfrac{1}{a_{k + 2} \ddots }}\]where \(R_0\) is just the original continued fraction, i.e. \(R_0 = [a_0; a_1, a_2, \ldots]\).
The result is a
ContinuedFractioninstance.- Parameters:
- kint
The index of the remainder, as described above.
- Returns:
- ContinuedFraction
A new
ContinuedFractioninstance representing the \(k\)-th remainder of the original continued fraction, as described above.
Examples
>>> cf = ContinuedFraction('.12345') >>> cf ContinuedFraction(2469, 20000) >>> cf.remainder(0) ContinuedFraction(2469, 20000) >>> cf.remainder(2) ContinuedFraction(2469, 248) >>> cf.remainder(6) ContinuedFraction(6, 5) >>> cf.remainder(7) ContinuedFraction(5, 1)
- property remainders: Generator[tuple[int, ContinuedFraction], None, None]#
Generates an enumerated sequence of all remainders of the continued fraction in descending order of index.
If \(n\) is the order of the continued fraction then there are \(n + 1\) remainders \(R_0, R_1, \ldots, R_n\), and the method generates these in reverse order \(R_0, R_1, \ldots, R_n\).
The remainders are generated as tuples of
intandContinuedFractioninstances, where the integers represent the indexes of the remainders.- Yields:
- tuple
A tuple of remainder indices (
int) and remainders (ContinuedFraction) of the continued fraction.
Examples
>>> tuple(ContinuedFraction('3.245').remainders) ((3, ContinuedFraction(4, 1)), (2, ContinuedFraction(49, 4)), (1, ContinuedFraction(200, 49)), (0, ContinuedFraction(649, 200))) >>> tuple(ContinuedFraction(-415, 93).remainders) ((4, ContinuedFraction(7, 1)), (3, ContinuedFraction(43, 7)), (2, ContinuedFraction(50, 43)), (1, ContinuedFraction(93, 50)), (0, ContinuedFraction(-415, 93)))
- left_mediant(other: Fraction, /, *, k: int = 1) ContinuedFraction[source]#
Returns the \(k\)-th left-mediant of the continued fraction with another rational number.
For a positive integer \(k\), the \(k\)-th left-mediant of two rational numbers \(r = \frac{a}{b}\) and \(s = \frac{c}{d}\), where \(b, d, b + d \neq 0\), is defined as:
\[\frac{ka + c}{kb + d}, \hskip{3em} k \geq 1\]- Parameters:
- otherfractions.Fraction, ContinuedFraction
The second fraction to use to calculate the \(k\)-th mediant with the first.
- kint, default=1
The order of the mediant, as defined above.
- Returns:
- ContinuedFraction
The \(k\)-th left-mediant of the original fraction and the second fraction, as a
ContinuedFractioninstance.
Examples
>>> c1 = ContinuedFraction('1/2') >>> c2 = ContinuedFraction(3, 5) >>> c1, c2 (ContinuedFraction(1, 2), ContinuedFraction(3, 5)) >>> c1.left_mediant(c2) ContinuedFraction(4, 7) >>> c1.left_mediant(c2, k=2) ContinuedFraction(5, 9) >>> c1.left_mediant(c2, k=3) ContinuedFraction(6, 11) >>> c1.left_mediant(c2, k=100) ContinuedFraction(103, 205) >>> assert c1.left_mediant(c2, k=2) < c1.right_mediant(c2, k=2) >>> assert c1.left_mediant(c2, k=3) < c1.right_mediant(c2, k=3) >>> assert c1.left_mediant(c2, k=100) < c1.right_mediant(c2, k=100)
- right_mediant(other: Fraction, /, *, k: int = 1) ContinuedFraction[source]#
Returns the \(k\)-th right-mediant of the continued fraction with another rational number.
For a positive integer \(k\), the \(k\)-th right-mediant of two rational numbers \(r = \frac{a}{b}\) and \(s = \frac{c}{d}\), where \(b, d, b + d \neq 0\), is defined as:
\[\frac{a + kc}{b + kd}, \hskip{3em} k \geq 1\]- Parameters:
- otherfractions.Fraction, ContinuedFraction
The second fraction to use to calculate the \(k\)-th mediant with the first.
- kint, default=1
The order of the mediant, as defined above.
- Returns:
- ContinuedFraction
The \(k\)-th right-mediant of the original fraction and the second fraction, as a
ContinuedFractioninstance.
Examples
>>> c1 = ContinuedFraction('1/2') >>> c2 = ContinuedFraction(3, 5) >>> c1, c2 (ContinuedFraction(1, 2), ContinuedFraction(3, 5)) >>> c1.right_mediant(c2) ContinuedFraction(4, 7) >>> c1.right_mediant(c2, k=2) ContinuedFraction(7, 12) >>> c1.right_mediant(c2, k=3) ContinuedFraction(10, 17) >>> c1.right_mediant(c2, k=100) ContinuedFraction(301, 502) >>> assert c1.left_mediant(c2, k=2) < c1.right_mediant(c2, k=2) >>> assert c1.left_mediant(c2, k=3) < c1.right_mediant(c2, k=3) >>> assert c1.left_mediant(c2, k=100) < c1.right_mediant(c2, k=100)
- mediant(other: Fraction, /) ContinuedFraction[source]#
Returns the simple mediant of the continued fraction with another continued fraction.
The simple mediant of two rational numbers \(r = \frac{a}{b}\) and \(s = \frac{c}{d}\), where \(b, d, b + d \neq 0\), is defined as:
\[\frac{a + c}{b + d}\]The resulting value \(\frac{a + c}{b + d}\) is the same as the 1st order left- or right-mediant of \(r = \frac{a}{b}\) and \(s = \frac{c}{d}\). So this method would produce the same result as the
left_mediant()orright_mediant()methods where the order \(k\) is set to \(1\).- Parameters:
- otherfractions.Fraction, ContinuedFraction
The other continued fraction.
- Returns:
- ContinuedFraction
The simple mediant of the original fraction and the other continued fraction.
Examples
>>> ContinuedFraction(1, 2).mediant(ContinuedFraction(3, 5)) ContinuedFraction(4, 7) >>> assert ContinuedFraction(1, 2).mediant(ContinuedFraction(3, 5)) == ContinuedFraction(1, 2).left_mediant(ContinuedFraction(3, 5), k=1) >>> assert ContinuedFraction(1, 2).mediant(ContinuedFraction(3, 5)) == ContinuedFraction(1, 2).right_mediant(ContinuedFraction(3, 5), k=1)
- property height: int#
The height of this rational number.
- Returns:
- int
The height of the fraction as a rational number represented by homogeneous coordinates in projective space \(\mathbb{P}^1(\mathbb{Q})\).
Examples
>>> ContinuedFraction(0, 1).height 1 >>> ContinuedFraction(-1, 2).height 2 >>> ContinuedFraction(3, -2).height 3
- Type:
- property log_height: Decimal#
The (natural) logarithm of the height of this rational number as defined above.
- Returns:
- decimal.Decimal
The (natural) logarithm of the height of this fraction as a rational number as defined above.
Examples
>>> ContinuedFraction(0, 1).log_height Decimal('0') >>> ContinuedFraction(-1, 2).log_height Decimal('0.69314718055994528622676398299518041312694549560546875') >>> ContinuedFraction(3, -2).log_height Decimal('1.0986122886681097821082175869378261268138885498046875')
- Type:
- __neg__() ContinuedFraction[source]#
The negation algorithm for a finite simple continued fraction.
The basic algorithm can be described as follows: if \([a_0; a_1,\ldots, a_n]\) is the simple continued fraction of a positive rational number \(\frac{a}{b}\) of finite order \(n\) then \(-\frac{a}{b}\) has the simple continued fraction:
\[\begin{split}\begin{cases} [-a_0;] \hskip{3em} & n = 0 \\ [-(a_0 + 1); 2] \hskip{3em} & n = 1 \text{ and } a_1 = 2 \\ [-(a_0 + 1); a_2 + 1, a_3,\ldots, a_n] \hskip{3em} & n \geq 2 \text{ and } a_1 = 1 \\ [-(a_0 + 1); 1, a_1 - 1, a_2, \ldots,a_n] \hskip{3em} & n \geq 2 \text{ and } a_1 \geq 2 \end{cases}\end{split}\]In applying this algorithm there is an assumption that the last coefficient \(a_n > 1\), as any simple continued fraction of type \([a_0; a_1,\ldots, a_{n} = 1]\) can be reduced to the simple continued fraction \([a_0; a_1,\ldots, a_{n - 1} + 1]\).