From 3e5470499656dd737dad317a9ca87fa452a1b79f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 11 Nov 2023 09:38:41 +0100 Subject: [PATCH 1/2] use less _element_constructor --- .../multiset_partition_into_sets_ordered.py | 2 +- src/sage/combinat/posets/hasse_cython.pyx | 35 +++++++++++-------- .../finite_rings/hom_prime_finite_field.pyx | 4 +-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/sage/combinat/multiset_partition_into_sets_ordered.py b/src/sage/combinat/multiset_partition_into_sets_ordered.py index a8855beb412..c544d9ae2ef 100755 --- a/src/sage/combinat/multiset_partition_into_sets_ordered.py +++ b/src/sage/combinat/multiset_partition_into_sets_ordered.py @@ -1699,7 +1699,7 @@ def _satisfies_constraints(self, x): .. NOTE:: This test will cause an infinite recursion with - ``self._element_constructor()`` if the ``__contains__`` + ``self._element_constructor_()`` if the ``__contains__`` method in ``OrderedMultisetPartitionsIntoSets_X`` is removed. TESTS:: diff --git a/src/sage/combinat/posets/hasse_cython.pyx b/src/sage/combinat/posets/hasse_cython.pyx index 8f7a869fc17..1aac8c7cb37 100644 --- a/src/sage/combinat/posets/hasse_cython.pyx +++ b/src/sage/combinat/posets/hasse_cython.pyx @@ -30,8 +30,8 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): - ``positions`` -- a list of sets of integers describing the poset, as given by the lazy attribute ``_leq_storage`` of Hasse diagrams. - - ``element_constructor`` -- used to determine the type of chains, - for example ``list`` or ``tuple`` + - ``constructor`` -- used to determine the type of chains, + for example :class:`list` or :class:`tuple` - ``exclude`` -- list of integers that should not belong to the chains @@ -50,7 +50,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): sage: list(D) [[], [0], [0, 1], [1]] """ - def __init__(self, list positions, element_constructor, + def __init__(self, list positions, constructor, list exclude, conversion=None): """ The enumerated set of increasing chains. @@ -71,7 +71,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): self._greater_than = positions self._vertices = list(range(n)) - self._element_constructor = element_constructor + self._constructor = constructor self._conversion = conversion if conversion is not None: self._from_poset = {elt: i for i, elt in enumerate(conversion)} @@ -92,8 +92,8 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): sage: from sage.combinat.posets.hasse_cython import IncreasingChains sage: D = IncreasingChains([{0,1},{1}], list, []) - sage: [x in D for x in D] - [True, True, True, True] + sage: all(x in D for x in D) + True sage: [2] in D False sage: [1,1] in D @@ -102,6 +102,13 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): sage: P = Poset({'a':['b'],'b':[]}) sage: ['a'] in P.chains() True + + TESTS:: + + sage: from sage.combinat.posets.hasse_cython import IncreasingChains + sage: D = IncreasingChains([{0,1},{1}], list, []) + sage: all(tuple(x) in D for x in D) + True """ cdef int k cdef Py_ssize_t i, x, y @@ -109,9 +116,8 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): return True if self._conversion is not None: tup = [self._from_poset[elt] for elt in tup] - for i in tup: - if not(0 <= i < self._n): - return False + if any(not(0 <= i < self._n) for i in tup): + return False y = tup[0] for k in range(1, len(tup)): x = y @@ -127,7 +133,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): If ``conversion`` was provided, it first converts elements of the chain to elements of this list. - Then the given ``element_constructor`` is applied to the chain. + Then the given ``constructor`` is applied to the chain. EXAMPLES:: @@ -142,8 +148,8 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): """ cdef Py_ssize_t i if self._conversion is not None: - return self._element_constructor(self._conversion[i] for i in chain) - return self._element_constructor(chain) + return self._constructor(self._conversion[i] for i in chain) + return self._constructor(chain) def children(self, chain): """ @@ -163,6 +169,5 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): cdef Py_ssize_t x, y if not chain: return [(x,) for x in self._vertices] - else: - x = chain[-1] - return [chain + (y,) for y in self._greater_than[x] if x != y] + x = chain[-1] + return [chain + (y,) for y in self._greater_than[x] if x != y] diff --git a/src/sage/rings/finite_rings/hom_prime_finite_field.pyx b/src/sage/rings/finite_rings/hom_prime_finite_field.pyx index 6bc38546612..9453493ee6d 100644 --- a/src/sage/rings/finite_rings/hom_prime_finite_field.pyx +++ b/src/sage/rings/finite_rings/hom_prime_finite_field.pyx @@ -37,7 +37,7 @@ from sage.rings.finite_rings.finite_field_base import FiniteField cdef class SectionFiniteFieldHomomorphism_prime(SectionFiniteFieldHomomorphism_generic): cpdef Element _call_(self, x) noexcept: try: - return self._codomain._element_constructor(x) + return self._codomain._element_constructor_(x) except TypeError: raise ValueError("%s is not in the image of %s" % (x, self._inverse)) @@ -89,7 +89,7 @@ cdef class FiniteFieldHomomorphism_prime(FiniteFieldHomomorphism_generic): sage: a.parent() Finite Field in t of size 3^5 """ - return self._codomain._element_constructor(x) + return self._codomain._element_constructor_(x) cdef class FrobeniusEndomorphism_prime(FrobeniusEndomorphism_finite_field): From f4a7f2692e7edab5a95dd90c1da2d5c18fd81d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Mon, 13 Nov 2023 09:10:52 +0100 Subject: [PATCH 2/2] change back the argument to element_constructor --- src/sage/combinat/posets/hasse_cython.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sage/combinat/posets/hasse_cython.pyx b/src/sage/combinat/posets/hasse_cython.pyx index 1aac8c7cb37..2048febd192 100644 --- a/src/sage/combinat/posets/hasse_cython.pyx +++ b/src/sage/combinat/posets/hasse_cython.pyx @@ -30,7 +30,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): - ``positions`` -- a list of sets of integers describing the poset, as given by the lazy attribute ``_leq_storage`` of Hasse diagrams. - - ``constructor`` -- used to determine the type of chains, + - ``element_constructor`` -- used to determine the type of chains, for example :class:`list` or :class:`tuple` - ``exclude`` -- list of integers that should not belong to the chains @@ -50,7 +50,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): sage: list(D) [[], [0], [0, 1], [1]] """ - def __init__(self, list positions, constructor, + def __init__(self, list positions, element_constructor, list exclude, conversion=None): """ The enumerated set of increasing chains. @@ -71,7 +71,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): self._greater_than = positions self._vertices = list(range(n)) - self._constructor = constructor + self._constructor = element_constructor self._conversion = conversion if conversion is not None: self._from_poset = {elt: i for i, elt in enumerate(conversion)} @@ -133,7 +133,7 @@ class IncreasingChains(RecursivelyEnumeratedSet_forest): If ``conversion`` was provided, it first converts elements of the chain to elements of this list. - Then the given ``constructor`` is applied to the chain. + Then the given ``element_constructor`` is applied to the chain. EXAMPLES::