Skip to content

Commit

Permalink
sagemathgh-37713: sage.sets: Doctest cosmetics
Browse files Browse the repository at this point in the history
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Cherry-picked from sagemath#35095.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#37713
Reported by: Matthias Köppe
Reviewer(s): Martin Rubey
  • Loading branch information
Release Manager committed May 12, 2024
2 parents 9c99900 + 5ef67f5 commit 41cd5eb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
79 changes: 43 additions & 36 deletions src/sage/sets/recursively_enumerated_set.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ or even::
We can then create the :class:`RecursivelyEnumeratedSet` object with either::
sage: S = RecursivelyEnumeratedSet([''],
....: lambda x: [x+letter for letter in ['a', 'b', 'c']]
....: lambda x: [x + letter for letter in ['a', 'b', 'c']]
....: if len(x) < 2 else [],
....: structure='forest', enumeration='depth',
....: category=FiniteEnumeratedSets())
Expand Down Expand Up @@ -248,8 +248,8 @@ convention is that the generated elements are the ``s := f(n)``, except when
....: st = set(st) # make a copy
....: if st:
....: el = st.pop()
....: for i in range(len(lst)+1):
....: yield (lst[0:i]+[el]+lst[i:], st)
....: for i in range(len(lst) + 1):
....: yield (lst[0:i] + [el] + lst[i:], st)
sage: list(children(([1,2], {3,7,9})))
[([9, 1, 2], {3, 7}), ([1, 9, 2], {3, 7}), ([1, 2, 9], {3, 7})]
sage: def post_process(node):
Expand Down Expand Up @@ -347,8 +347,7 @@ def RecursivelyEnumeratedSet(seeds, successors, structure=None,
A recursive set with a forest structure::
sage: f = lambda a: [2*a,2*a+1]
sage: C = RecursivelyEnumeratedSet([1], f, structure='forest')
sage: C
sage: C = RecursivelyEnumeratedSet([1], f, structure='forest'); C
An enumerated set with a forest structure
sage: it = C.depth_first_search_iterator()
sage: [next(it) for _ in range(7)]
Expand Down Expand Up @@ -676,7 +675,7 @@ cdef class RecursivelyEnumeratedSet_generic(Parent):
EXAMPLES::
sage: R = RecursivelyEnumeratedSet([1], lambda x: [x+1, x-1])
sage: R = RecursivelyEnumeratedSet([1], lambda x: [x + 1, x - 1])
sage: R.seeds()
[1]
"""
Expand Down Expand Up @@ -1125,7 +1124,7 @@ cdef class RecursivelyEnumeratedSet_symmetric(RecursivelyEnumeratedSet_generic):
sage: # needs sage.symbolic
sage: def f(a):
....: sleep(0.05r)
....: return [a-1,a+1]
....: return [a - 1, a + 1]
sage: C = RecursivelyEnumeratedSet([0], f, structure='symmetric')
sage: it = C.graded_component_iterator()
sage: next(it)
Expand Down Expand Up @@ -1185,7 +1184,7 @@ cdef class RecursivelyEnumeratedSet_symmetric(RecursivelyEnumeratedSet_generic):
sage: def f(a):
....: sleep(0.1r)
....: return [a-1,a+1]
....: return [a - 1, a + 1]
sage: C = RecursivelyEnumeratedSet([0], f, structure='symmetric')
sage: from cysignals.alarm import alarm
sage: alarm(0.45); C.graded_component(10)
Expand Down Expand Up @@ -1410,7 +1409,7 @@ cdef class RecursivelyEnumeratedSet_graded(RecursivelyEnumeratedSet_generic):
sage: # needs sage.symbolic
sage: def f(a):
....: sleep(0.1r)
....: return [a+1, a+I]
....: return [a + 1, a + I]
sage: C = RecursivelyEnumeratedSet([0], f, structure='graded')
sage: from cysignals.alarm import alarm
sage: alarm(0.45); C.graded_component(10)
Expand Down Expand Up @@ -1481,7 +1480,7 @@ def _imap_and_filter_none(function, iterable):
sage: p = _imap_and_filter_none(lambda x: x if is_prime(x) else None, range(15))
sage: [next(p), next(p), next(p), next(p), next(p), next(p)]
[2, 3, 5, 7, 11, 13]
sage: p = _imap_and_filter_none(lambda x: x+x, ['a','b','c','d','e'])
sage: p = _imap_and_filter_none(lambda x: x + x, ['a','b','c','d','e'])
sage: [next(p), next(p), next(p), next(p), next(p)]
['aa', 'bb', 'cc', 'dd', 'ee']
"""
Expand Down Expand Up @@ -1509,15 +1508,15 @@ def search_forest_iterator(roots, children, algorithm='depth'):
three, and enumerate its nodes::
sage: from sage.sets.recursively_enumerated_set import search_forest_iterator
sage: list(search_forest_iterator([[]], lambda l: [l+[0], l+[1]]
sage: list(search_forest_iterator([[]], lambda l: [l + [0], l + [1]]
....: if len(l) < 3 else []))
[[], [0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 1], [0, 1, 0],
[0, 1, 1], [1], [1, 0], [1, 0, 0], [1, 0, 1], [1, 1], [1, 1, 0], [1, 1, 1]]
By default, the nodes are iterated through by depth first search.
We can instead use a breadth first search (increasing depth)::
sage: list(search_forest_iterator([[]], lambda l: [l+[0], l+[1]]
sage: list(search_forest_iterator([[]], lambda l: [l + [0], l + [1]]
....: if len(l) < 3 else [],
....: algorithm='breadth'))
[[],
Expand All @@ -1528,7 +1527,8 @@ def search_forest_iterator(roots, children, algorithm='depth'):
This allows for iterating through trees of infinite depth::
sage: it = search_forest_iterator([[]], lambda l: [l+[0], l+[1]], algorithm='breadth')
sage: it = search_forest_iterator([[]], lambda l: [l + [0], l + [1]],
....: algorithm='breadth')
sage: [ next(it) for i in range(16) ]
[[],
[0], [1], [0, 0], [0, 1], [1, 0], [1, 1],
Expand Down Expand Up @@ -1607,7 +1607,7 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: S = RecursivelyEnumeratedSet_forest( [[]],
....: lambda l: [l+[0], l+[1]] if len(l) < 3 else [],
....: lambda l: [l + [0], l + [1]] if len(l) < 3 else [],
....: category=FiniteEnumeratedSets())
sage: S.list()
[[],
Expand Down Expand Up @@ -1691,17 +1691,19 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: class A(UniqueRepresentation, RecursivelyEnumeratedSet_forest):
....: def __init__(self):
....: RecursivelyEnumeratedSet_forest.__init__(self, [()],
....: lambda x : [x+(0,), x+(1,)] if sum(x) < 3 else [],
....: lambda x : sum(x[i]*2^i for i in range(len(x))) if sum(x) != 0 and x[-1] != 0 else None,
....: algorithm = 'breadth',
....: lambda x: [x + (0,), x + (1,)] if sum(x) < 3 else [],
....: lambda x: sum(x[i]*2^i for i in range(len(x)))
....: if sum(x) != 0 and x[-1] != 0 else None,
....: algorithm='breadth',
....: category=InfiniteEnumeratedSets())
sage: MyForest = A(); MyForest
An enumerated set with a forest structure
sage: MyForest.category()
Category of infinite enumerated sets
sage: p = iter(MyForest)
sage: [next(p) for i in range(30)]
[1, 2, 3, 4, 6, 5, 7, 8, 12, 10, 14, 9, 13, 11, 16, 24, 20, 28, 18, 26, 22, 17, 25, 21, 19, 32, 48, 40, 56, 36]
[1, 2, 3, 4, 6, 5, 7, 8, 12, 10, 14, 9, 13, 11, 16, 24,
20, 28, 18, 26, 22, 17, 25, 21, 19, 32, 48, 40, 56, 36]
An alternative approach is to implement ``roots`` and ``children``
as methods of the subclass (in fact they could also be attributes
Expand All @@ -1714,13 +1716,13 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: class A(UniqueRepresentation, RecursivelyEnumeratedSet_forest):
....: def __init__(self):
....: RecursivelyEnumeratedSet_forest.__init__(self, algorithm = 'breadth',
....: RecursivelyEnumeratedSet_forest.__init__(self, algorithm='breadth',
....: category=InfiniteEnumeratedSets())
....: def roots(self):
....: return [()]
....: def children(self, x):
....: if sum(x) < 3:
....: return [x+(0,), x+(1,)]
....: return [x + (0,), x + (1,)]
....: else:
....: return []
....: def post_process(self, x):
Expand All @@ -1734,7 +1736,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
Category of infinite enumerated sets
sage: p = iter(MyForest)
sage: [next(p) for i in range(30)]
[1, 2, 3, 4, 6, 5, 7, 8, 12, 10, 14, 9, 13, 11, 16, 24, 20, 28, 18, 26, 22, 17, 25, 21, 19, 32, 48, 40, 56, 36]
[1, 2, 3, 4, 6, 5, 7, 8, 12, 10, 14, 9, 13, 11, 16, 24,
20, 28, 18, 26, 22, 17, 25, 21, 19, 32, 48, 40, 56, 36]
.. warning::
Expand All @@ -1743,8 +1746,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
anonymous or interactively defined functions::
sage: def children(x):
....: return [x+1]
sage: S = RecursivelyEnumeratedSet_forest( [1], children, category=InfiniteEnumeratedSets())
....: return [x + 1]
sage: S = RecursivelyEnumeratedSet_forest([1], children, category=InfiniteEnumeratedSets())
sage: dumps(S)
Traceback (most recent call last):
...
Expand All @@ -1754,7 +1757,7 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: import __main__
sage: __main__.children = children
sage: S = RecursivelyEnumeratedSet_forest( [1], children, category=InfiniteEnumeratedSets())
sage: S = RecursivelyEnumeratedSet_forest([1], children, category=InfiniteEnumeratedSets())
sage: loads(dumps(S))
An enumerated set with a forest structure
"""
Expand All @@ -1764,7 +1767,7 @@ class RecursivelyEnumeratedSet_forest(Parent):
TESTS::
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: S = RecursivelyEnumeratedSet_forest(NN, lambda x : [], lambda x: x^2 if x.is_prime() else None)
sage: S = RecursivelyEnumeratedSet_forest(NN, lambda x: [], lambda x: x^2 if x.is_prime() else None)
sage: S.category()
Category of enumerated sets
"""
Expand Down Expand Up @@ -1837,7 +1840,7 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: def children(l):
....: return [l+[0], l+[1]]
....: return [l + [0], l + [1]]
sage: C = RecursivelyEnumeratedSet_forest(([],), children)
sage: f = C.__iter__()
sage: next(f)
Expand All @@ -1862,9 +1865,10 @@ class RecursivelyEnumeratedSet_forest(Parent):
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: f = RecursivelyEnumeratedSet_forest([[]],
....: lambda l: [l+[0], l+[1]] if len(l) < 3 else [])
....: lambda l: [l + [0], l + [1]] if len(l) < 3 else [])
sage: list(f.depth_first_search_iterator())
[[], [0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 1], [0, 1, 0], [0, 1, 1], [1], [1, 0], [1, 0, 0], [1, 0, 1], [1, 1], [1, 1, 0], [1, 1, 1]]
[[], [0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 1], [0, 1, 0], [0, 1, 1],
[1], [1, 0], [1, 0, 0], [1, 0, 1], [1, 1], [1, 1, 0], [1, 1, 1]]
"""
return iter(self)

Expand Down Expand Up @@ -1963,7 +1967,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
EXAMPLES::
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: S = RecursivelyEnumeratedSet_forest( [[]], lambda l: [l+[0], l+[1]] if len(l) < 3 else [], category=FiniteEnumeratedSets())
sage: S = RecursivelyEnumeratedSet_forest([[]], lambda l: [l + [0], l + [1]] if len(l) < 3 else [],
....: category=FiniteEnumeratedSets())
sage: [4] in S
False
sage: [1] in S
Expand All @@ -1972,12 +1977,12 @@ class RecursivelyEnumeratedSet_forest(Parent):
False
sage: all(S.__contains__(i) for i in iter(S))
True
sage: S = RecursivelyEnumeratedSet_forest([1], lambda x: [x+1], category=InfiniteEnumeratedSets())
sage: S = RecursivelyEnumeratedSet_forest([1], lambda x: [x + 1], category=InfiniteEnumeratedSets())
sage: 1 in S
True
sage: 732 in S
True
sage: -1 in S # not tested : Will never stop
sage: -1 in S # not tested : Will never stop
The algorithm uses a random enumeration of the nodes of the
forest. This choice was motivated by examples in which both
Expand All @@ -1987,8 +1992,9 @@ class RecursivelyEnumeratedSet_forest(Parent):
root has an infinite number of children::
sage: from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet_forest
sage: S = RecursivelyEnumeratedSet_forest(Family(NN, lambda x : (x, 0)),
....: lambda x : Family(PositiveIntegers(), lambda y : (x[0], y)) if x[1] == 0 else [])
sage: S = RecursivelyEnumeratedSet_forest(
....: Family(NN, lambda x: (x, 0)),
....: lambda x: Family(PositiveIntegers(), lambda y: (x[0], y)) if x[1] == 0 else [])
sage: p = S.depth_first_search_iterator()
sage: [next(p), next(p), next(p), next(p), next(p), next(p), next(p)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
Expand All @@ -2010,7 +2016,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
child. From each root starts an infinite branch of breadth
`1`::
sage: S = RecursivelyEnumeratedSet_forest(Family(NN, lambda x : (x, 0)) , lambda x : [(x[0], x[1]+1)])
sage: S = RecursivelyEnumeratedSet_forest(Family(NN, lambda x: (x, 0)),
....: lambda x: [(x[0], x[1] + 1)])
sage: p = S.depth_first_search_iterator()
sage: [next(p), next(p), next(p), next(p), next(p), next(p), next(p)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
Expand Down Expand Up @@ -2065,12 +2072,12 @@ class RecursivelyEnumeratedSet_forest(Parent):
EXAMPLES::
sage: seeds = [([i],i, i) for i in range(1,10)]
sage: seeds = [([i], i, i) for i in range(1, 10)]
sage: def succ(t):
....: list, sum, last = t
....: return [(list + [i], sum + i, i) for i in range(1, last)]
sage: F = RecursivelyEnumeratedSet(seeds, succ,
....: structure='forest', enumeration='depth')
....: structure='forest', enumeration='depth')
sage: # needs sage.symbolic
sage: y = var('y')
Expand Down
4 changes: 3 additions & 1 deletion src/sage/sets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,9 @@ def __init__(self, X, Y, category=None):
sage: S = Set(QQ)
sage: T = Set(ZZ)
sage: X = S.difference(T); X
Set-theoretic difference of Set of elements of Rational Field and Set of elements of Integer Ring
Set-theoretic difference of
Set of elements of Rational Field and
Set of elements of Integer Ring
sage: X.category()
Category of sets
sage: latex(X)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/sets/totally_ordered_finite_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, parent, data):
r"""
TESTS::
sage: T = TotallyOrderedFiniteSet([3,2,1],facade=False)
sage: T = TotallyOrderedFiniteSet([3,2,1], facade=False)
sage: TestSuite(T.an_element()).run()
"""
Element.__init__(self, parent)
Expand Down

0 comments on commit 41cd5eb

Please sign in to comment.