Skip to content

Commit

Permalink
Implement iteration over more polynomial rings
Browse files Browse the repository at this point in the history
  • Loading branch information
user202729 committed Feb 4, 2025
1 parent 4563c6b commit bc5c6c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
34 changes: 25 additions & 9 deletions src/sage/rings/polynomial/polynomial_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def __init__(self, base_ring, name=None, sparse=False, implementation=None,
(Dedekind domains and euclidean domains
and noetherian rings and infinite enumerated sets
and metric spaces)
and Category of infinite sets
and Category of infinite enumerated sets
sage: category(GF(7)['x'])
Join of Category of euclidean domains
Expand Down Expand Up @@ -1051,22 +1051,38 @@ def __iter__(self):
sage: [*R]
[0]
sage: R.<x> = QQ[]
sage: list(islice(iter(R), 10)) # when this is implemented add Enumerated() to category(R)
Traceback (most recent call last):
...
NotImplementedError: iteration over infinite base ring not yet implemented
sage: l = list(islice(iter(R), 50)); l
[0, 1, -1, x, 1/2, x + 1, x^2, -1/2, -x, x^2 + 1, x^3, 2, x - 1, ...]
sage: len(set(l))
50
"""
# adapted from sage.modules.free_module.FreeModule_generic.__iter__
R = self.base_ring()
if R.cardinality() == Infinity:
raise NotImplementedError("iteration over infinite base ring not yet implemented")
# adapted from sage.modules.free_module.FreeModule_generic.__iter__
iters = []
zero = R.zero()
v = []
n = 0
yield self.zero()
if R.is_zero():
return

zero = R.zero()
if R.cardinality() == Infinity:
from sage.categories.sets_cat import cartesian_product
from sage.sets.disjoint_union_enumerated_sets import DisjointUnionEnumeratedSets
from sage.sets.family import Family
from sage.rings.semirings.non_negative_integer_semiring import NN
from sage.sets.set import Set
R_nonzero = Set(R) - Set([zero])
def polynomials_with_degree(d):
"""
Return the family of polynomials with degree exactly ``d``.
"""
nonlocal self, R, R_nonzero
return Family(cartesian_product([R] * d + [R_nonzero]),
lambda t: self([*t]), lazy=True)
yield from DisjointUnionEnumeratedSets(Family(NN, polynomials_with_degree))
assert False, "this should not be reached"

while True:
if n == len(iters):
iters.append(iter(R))
Expand Down
5 changes: 3 additions & 2 deletions src/sage/rings/polynomial/polynomial_ring_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,9 @@ def polynomial_default_category(base_ring_category, n_variables):
if n_variables:
# here we assume the base ring to be nonzero
category = category.Infinite()
if base_ring_category.is_subcategory(_FiniteSets) and n_variables == 1:
# base_ring_category.is_subcategory(_EnumeratedSets) suffices but this is not yet implemented
if base_ring_category.is_subcategory(_EnumeratedSets) and n_variables == 1:
# n_variables == 1 is not necessary but iteration over multivariate polynomial ring
# is not yet implemented
category = category.Enumerated()
else:
if base_ring_category.is_subcategory(_Fields):
Expand Down

0 comments on commit bc5c6c1

Please sign in to comment.