diff --git a/src/sage/sets/recursively_enumerated_set.pyx b/src/sage/sets/recursively_enumerated_set.pyx index 56bd8d16850..183d52b8c4a 100644 --- a/src/sage/sets/recursively_enumerated_set.pyx +++ b/src/sage/sets/recursively_enumerated_set.pyx @@ -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()) @@ -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): @@ -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)] @@ -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] """ @@ -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) @@ -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) @@ -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) @@ -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'] """ @@ -1509,7 +1508,7 @@ 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]] @@ -1517,7 +1516,7 @@ def search_forest_iterator(roots, children, algorithm='depth'): 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')) [[], @@ -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], @@ -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() [[], @@ -1691,9 +1691,10 @@ 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 @@ -1701,7 +1702,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] An alternative approach is to implement ``roots`` and ``children`` as methods of the subclass (in fact they could also be attributes @@ -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): @@ -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:: @@ -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): ... @@ -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 """ @@ -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 """ @@ -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) @@ -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) @@ -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 @@ -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 @@ -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)] @@ -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)] @@ -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') diff --git a/src/sage/sets/set.py b/src/sage/sets/set.py index fe351298d0a..bdabfee5098 100644 --- a/src/sage/sets/set.py +++ b/src/sage/sets/set.py @@ -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) diff --git a/src/sage/sets/totally_ordered_finite_set.py b/src/sage/sets/totally_ordered_finite_set.py index 7efba359dc0..9191992c285 100644 --- a/src/sage/sets/totally_ordered_finite_set.py +++ b/src/sage/sets/totally_ordered_finite_set.py @@ -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)