Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Added more unit tests, replaced overwritten method quotients() with g…
Browse files Browse the repository at this point in the history
…et preperiod length, so that all doctests pass. Added a few more doctests. Now all methods are accompanied with doctests.
  • Loading branch information
SAGitbot committed May 4, 2016
1 parent fea8eb9 commit 92b8aa0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
34 changes: 23 additions & 11 deletions src/sage/rings/continued_fraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,8 @@ def apply_homography(self, a, b, c, d):
EXAMPLES::
from random import randint
sage: a = Integer(randint(-100,100)); b = Integer(randint(-100,100));
sage: c = Integer(randint(-100,100)); d = Integer(randint(-100,100));
sage: a = Integer(randint(-10,10)); b = Integer(randint(-10,10));
sage: c = Integer(randint(-10,10)); d = Integer(randint(-10,10));
sage: vals = [pi, sqrt(2), 541/227];
sage: for val in vals:
....: x = continued_fraction(val)
Expand All @@ -1150,6 +1149,12 @@ def apply_homography(self, a, b, c, d):
True
True
True
sage: x = continued_fraction(([1,2,3],[4,5]))
sage: val = x.value()
sage: y = continued_fraction((a*val+b)/(c*val+d))
sage: z = x.apply_homography(a,b,c,d)
sage: y == z
True
"""
from rational_field import QQ
from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic
Expand Down Expand Up @@ -1286,14 +1291,6 @@ def quotient(self, n):
return self._x1[n]
return self._x2[(n-len(self._x1)) % len(self._x2)]

def quotients(self):
r"""
Return the tuple _x1 and _x2, the preperiod and period, as
a tuple.
"""

return self._x1, self._x2

def length(self):
r"""
Returns the number of partial quotients of ``self``.
Expand All @@ -1311,6 +1308,21 @@ def length(self):
return Infinity
return Integer(len(self._x1))

def preperiod_length(self):
r"""
Returns the number of partial quotients of the preperiodic part of ``self``.
EXAMPLES::
sage: continued_fraction(2/5).preperiod_length()
3
sage: cf = continued_fraction([(0,1),(2,)]); cf
[0; 1, (2)*]
sage: cf.preperiod_length()
2
"""
return Integer(len(self._x1))

def __cmp__(self, other):
r"""
EXAMPLES::
Expand Down
51 changes: 33 additions & 18 deletions src/sage/rings/continued_fraction_gosper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sage.rings.infinity import Infinity
from sage.rings.integer import Integer
from sage.rings.real_mpfr import RR
from sage.rings.continued_fraction import ContinuedFraction_periodic

class gosper_iterator:

Expand All @@ -34,7 +35,6 @@ def __init__(self, a, b, c, d, x):
TESTS:
::
sage: from random import randint
sage: a = Integer(randint(-10,10)); b = Integer(randint(-10,10));
sage: c = Integer(randint(-10,10)); d = Integer(randint(-10,10));
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
Expand All @@ -60,15 +60,10 @@ def __init__(self, a, b, c, d, x):
self.currently_read = 0

# Rational or quadratic case
if isinstance(self.cf.quotients(), tuple):
self.input_preperiod_length = len(self.cf.quotients()[0])
if self.cf.quotients()[1][0] == +Infinity:
self.input_period_length = 0
else:
self.input_period_length = len(self.cf.quotients()[1])
if isinstance(self.cf, ContinuedFraction_periodic):
self.input_preperiod_length = self.cf.preperiod_length()
# Infinite case
else:
self.input_period_length = 0
self.input_preperiod_length = +Infinity

self.output_preperiod_length = 0
Expand All @@ -77,9 +72,8 @@ def __iter__(self):
"""
Returns the iterable instance of the class. Is called upon `iter(gosper_iterator(a,b,c,d,x))`.
TESTS::
sage: from random import randint
TESTS:
::
sage: a = Integer(randint(-100,100)); b = Integer(randint(-100,100));
sage: c = Integer(randint(-100,100)); d = Integer(randint(-100,100));
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
Expand All @@ -94,9 +88,8 @@ def next(self):
"""
Returns the next term of the transformation.
TESTS::
sage: from random import randint
TESTS:
::
sage: a = Integer(randint(-100,100)); b = Integer(randint(-100,100));
sage: c = Integer(randint(-100,100)); d = Integer(randint(-100,100));
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
Expand Down Expand Up @@ -146,6 +139,17 @@ def next(self):
def emit(self, q):
"""
Changes the state of the iterator, correspondingly to emitting the term `q`.
TESTS:
::
sage: a = Integer(randint(-100,100)); b = Integer(randint(-100,100));
sage: c = Integer(randint(-100,100)); d = Integer(randint(-100,100));
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
sage: gi = gosper_iterator(a,b,c,d,continued_fraction(pi))
sage: for i in range(10):
....: gi.emit(i)
sage: gi.currently_emitted
10
"""
self.currently_emitted += 1
# This is being computed for the case when no states are being saved (still reading preperiod).
Expand All @@ -162,6 +166,17 @@ def emit(self, q):
def ingest(self):
"""
Changes the state of the iterator, correspondingly to ingesting another term from the input continued fraction.
TESTS:
::
sage: a = Integer(randint(-100,100)); b = Integer(randint(-100,100));
sage: c = Integer(randint(-100,100)); d = Integer(randint(-100,100));
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
sage: gi = gosper_iterator(a,b,c,d,continued_fraction(pi))
sage: for i in range(10):
....: gi.ingest()
sage: gi.currently_read
10
"""
try:
p = next(self.x)
Expand All @@ -181,8 +196,8 @@ def bound(n,d):
"""
Helper function for division. Returns infinity if denominator is zero.
TESTS::
TESTS:
::
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
sage: gosper_iterator.bound(1,0)
+Infinity
Expand All @@ -197,8 +212,8 @@ def compare_dicts(d1, d2, ignore_keys):
"""
Helper function, used to compare two dictionaries, ignoring the keys in `ignore_keys`.
TESTS::
TESTS:
::
sage: from sage.rings.continued_fraction_gosper import gosper_iterator
sage: gosper_iterator.compare_dicts({"compared": 1, "ignored": 2},{"compared": 1, "ignored": 3}, ["ignored"])
True
Expand Down

0 comments on commit 92b8aa0

Please sign in to comment.