Changeset 3168:7209bfa43e10

Ignore:
Timestamp:
02/21/07 11:44:54 (6 years ago)
Branch:
default
Message:

new routine for iterating over rational numbers by Nils Bruin

File:
1 edited

Unmodified
Removed
• sage/rings/rational_field.py

 r2802 def __iter__(self): yield self(0) yield self(1) yield self(-1) from integer_ring import IntegerRing for n in IntegerRing(): m = abs(n) for d in abs(n).coprime_integers(m): yield n/d yield d/n r""" Creates an iterator that generates the rational numbers without repetition. It uses the sequence defined by $a_0=0$ and $a_{n+1}=\frac{1}{2\lfloor a_n\rfloor+1-a_n}$ and generates the sequence $$a_0,a_1,-a_1,a_2,-a_2,\ldots$$ EXAMPLES: This example creates a list consisting of the first 10 terms generated by this function. sage: import itertools sage: [a for a in itertools.islice(Rationals(),10)] [0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3/2] NOTES: A proof of the correctness of this formula is attributed to Sam Vandervelde and Don Zagier [A002487], but a better reference for the origin of this formula would be welcome. REFERENCES: [A002487] Sloane's OLEIS, http://www.research.att.com/~njas/sequences/A002487 AUTHORS: - Nils Bruin (2007-02-20) """ from sage.rings.arith import floor n=self(0) yield n while True: n=1/(2*floor(n)+1-n) yield n yield -n def complex_embedding(self, prec=53):
Note: See TracChangeset for help on using the changeset viewer.