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

Commit

Permalink
use new product_cantor_pairing and delete old product_diagonal
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Sep 30, 2015
1 parent e33703b commit ba99790
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 120 deletions.
4 changes: 2 additions & 2 deletions src/sage/rings/asymptotic/asymptotic_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,12 +1112,12 @@ def some_elements(self):
O(z^(-1/2)),
-8*z^(3/2) + O(z^(1/2)))
"""
from sage.rings.asymptotic.term_monoid import product_diagonal
from sage.misc.mrange import product_cantor_pairing
from sage.rings.asymptotic.term_monoid import TermMonoid
E = TermMonoid('exact', self.growth_group, self.coefficient_ring)
O = TermMonoid('O', self.growth_group, self.coefficient_ring)
return iter(-self(e)**3 + self(o)
for e, o in product_diagonal(
for e, o in product_cantor_pairing(
E.some_elements(), O.some_elements()))


Expand Down
120 changes: 2 additions & 118 deletions src/sage/rings/asymptotic/term_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,123 +205,6 @@

import sage


def product_diagonal(A, B):
r"""
Return an iterator over the product of `A` and `B` which iterates
along the diagonal.
INPUT:
- ``A`` and ``B`` -- iterables (over a finite number of elements)
OUTPUT:
An iterator over `(a,b)` for `a \in A` and `b \in B`.
EXAMPLES::
sage: from sage.rings.asymptotic.term_monoid import product_diagonal
sage: tuple(product_diagonal(srange(2), srange(2)))
((0, 0), (0, 1), (1, 0), (1, 1))
sage: tuple(product_diagonal(srange(4), srange(2)))
((0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1))
sage: tuple(product_diagonal(srange(2), srange(3)))
((0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (1, 2))
sage: tuple(''.join(p) for p in product_diagonal('abc', 'xyz'))
('ax', 'ay', 'bx', 'az', 'by', 'cx', 'bz', 'cy', 'cz')
TESTS:
Check that all pairs are returned::
sage: all(len(tuple(product_diagonal(srange(m), srange(n)))) == m*n
....: for m in srange(5) for n in srange(5))
True
Check that everthing is loaded in the correct order::
sage: def it(s, n):
....: for i in srange(n):
....: print '%s loads item number %s' % (s, i)
....: yield i
sage: for p in product_diagonal(it('A', 2), it('B', 2)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
(1, 1)
sage: for p in product_diagonal(it('A', 3), it('B', 2)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
(1, 1)
A loads item number 2
(2, 0)
(2, 1)
sage: for p in product_diagonal(it('A', 2), it('B', 4)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
B loads item number 2
(0, 2)
(1, 1)
B loads item number 3
(0, 3)
(1, 2)
(1, 3)
"""
# when writing this code I thought the solution would be shorter...

class iter_as_list(list):
def __init__(self, iterable):
self.it = iter(iterable)
self.newdata = True
def __getitem__(self, i):
self.newdata = False
try:
while len(self) <= i:
self.append(next(self.it))
self.newdata = True
except StopIteration:
raise
return list.__getitem__(self, i)

from itertools import count
A = iter_as_list(A)
B = iter_as_list(B)
for s in count():
for i in range(s+1):
stopped = False
try:
a = A[i]
except StopIteration:
stopped = True
try:
b = B[s-i]
except StopIteration:
stopped = True
if stopped:
continue
yield a, b
if not A.newdata and not B.newdata and s >= len(A) + len(B):
return


def absorption(left, right):
r"""
Let one of the two passed terms absorb the other.
Expand Down Expand Up @@ -2073,7 +1956,8 @@ def some_elements(self):
z^2, -2*z^(1/2), 2*z^(-1/2), -z^2, z^(-2))
"""
return iter(self(g, c) for g, c in product_diagonal(
from sage.misc.mrange import product_cantor_pairing
return iter(self(g, c) for g, c in product_cantor_pairing(
self.growth_group.some_elements(),
iter(c for c in self.base_ring().some_elements() if c != 0)))

Expand Down

0 comments on commit ba99790

Please sign in to comment.