From 09ec114481e97187923cf7f1612aaf310d4d2186 Mon Sep 17 00:00:00 2001 From: Giorgos Mousa Date: Wed, 22 May 2024 11:28:14 +0000 Subject: [PATCH] `SetSystem`: Minor change to accomodate set input This change makes the code cleaner in multiple places. --- .../matroids/circuit_closures_matroid.pyx | 4 +- src/sage/matroids/circuits_matroid.pyx | 18 ++--- src/sage/matroids/flats_matroid.pyx | 8 +-- src/sage/matroids/matroid.pyx | 20 +++--- src/sage/matroids/set_system.pyx | 66 +++++++++---------- 5 files changed, 57 insertions(+), 59 deletions(-) diff --git a/src/sage/matroids/circuit_closures_matroid.pyx b/src/sage/matroids/circuit_closures_matroid.pyx index a732ea192e2..3992b470084 100644 --- a/src/sage/matroids/circuit_closures_matroid.pyx +++ b/src/sage/matroids/circuit_closures_matroid.pyx @@ -402,11 +402,11 @@ cdef class CircuitClosuresMatroid(Matroid): N = CircuitClosuresMatroid(other) if sorted(self._circuit_closures.keys()) != sorted(N._circuit_closures.keys()): return False - SM = SetSystem(list(self.groundset())) + SM = SetSystem(self.groundset()) for r in self._circuit_closures: for C in self._circuit_closures[r]: SM.append(C) - SN = SetSystem(list(N.groundset())) + SN = SetSystem(N.groundset()) for r in N._circuit_closures: for C in N._circuit_closures[r]: SN.append(C) diff --git a/src/sage/matroids/circuits_matroid.pyx b/src/sage/matroids/circuits_matroid.pyx index 6152dae2842..c6e004cfc52 100644 --- a/src/sage/matroids/circuits_matroid.pyx +++ b/src/sage/matroids/circuits_matroid.pyx @@ -289,8 +289,8 @@ cdef class CircuitsMatroid(Matroid): if certificate: return self._is_isomorphic(other), self._isomorphism(other) N = CircuitsMatroid(other) - S = SetSystem(list(self._groundset), self._C) - O = SetSystem(list(N._groundset), N._C) + S = SetSystem(self._groundset, self._C) + O = SetSystem(N._groundset, N._C) return S._isomorphism(O) is not None # representation @@ -470,7 +470,7 @@ cdef class CircuitsMatroid(Matroid): SS = frozenset(S) if SS not in NB: B.add(SS) - return SetSystem(list(self._groundset), B) + return SetSystem(self._groundset, B) def bases_iterator(self): r""" @@ -551,7 +551,7 @@ cdef class CircuitsMatroid(Matroid): SS = frozenset(S) if SS not in D_r: I_r.add(SS) - return SetSystem(list(self._groundset), I_r) + return SetSystem(self._groundset, I_r) cpdef dependent_r_sets(self, long r): r""" @@ -586,7 +586,7 @@ cdef class CircuitsMatroid(Matroid): NB.remove(S) for e in S ^ self._groundset: NB.add(S | set([e])) - return SetSystem(list(self._groundset), NB) + return SetSystem(self._groundset, NB) cpdef circuits(self, k=None): """ @@ -621,7 +621,7 @@ cdef class CircuitsMatroid(Matroid): for i in self._k_C: for c in self._k_C[i]: C.add(c) - return SetSystem(list(self._groundset), C) + return SetSystem(self._groundset, C) def circuits_iterator(self, k=None): """ @@ -675,7 +675,7 @@ cdef class CircuitsMatroid(Matroid): for i in self._k_C: if i <= self._matroid_rank: NSC.update(self._k_C[i]) - return SetSystem(list(self._groundset), NSC) + return SetSystem(self._groundset, NSC) def nonspanning_circuits_iterator(self): """ @@ -755,7 +755,7 @@ cdef class CircuitsMatroid(Matroid): else: B.add(S) - return SetSystem(list(self.groundset()), B) + return SetSystem(self.groundset(), B) cpdef no_broken_circuits_sets(self, ordering=None, reduced=False): r""" @@ -803,7 +803,7 @@ cdef class CircuitsMatroid(Matroid): for f in SimplicialComplex(self.no_broken_circuits_facets(ordering, reduced), maximality_check=False).face_iterator(): NBC.add(frozenset(f)) - return SetSystem(list(self.groundset()), NBC) + return SetSystem(self.groundset(), NBC) cpdef broken_circuit_complex(self, ordering=None, reduced=False): r""" diff --git a/src/sage/matroids/flats_matroid.pyx b/src/sage/matroids/flats_matroid.pyx index 18f117a503b..71a046b42a1 100644 --- a/src/sage/matroids/flats_matroid.pyx +++ b/src/sage/matroids/flats_matroid.pyx @@ -249,8 +249,8 @@ cdef class FlatsMatroid(Matroid): N = FlatsMatroid(other) flats_self = [F for i in self._F for F in self._F[i]] flats_other = [F for i in N._F for F in N._F[i]] - SS = SetSystem(list(self._groundset), flats_self) - OS = SetSystem(list(N._groundset), flats_other) + SS = SetSystem(self._groundset, flats_self) + OS = SetSystem(N._groundset, flats_other) return SS._isomorphism(OS) is not None # representation @@ -432,8 +432,8 @@ cdef class FlatsMatroid(Matroid): frozenset({2, 3})] """ if k in self._F: - return SetSystem(list(self._groundset), self._F[k]) - return SetSystem(list(self._groundset)) + return SetSystem(self._groundset, self._F[k]) + return SetSystem(self._groundset) def flats_iterator(self, k): r""" diff --git a/src/sage/matroids/matroid.pyx b/src/sage/matroids/matroid.pyx index a274df52180..f3a777b9e31 100644 --- a/src/sage/matroids/matroid.pyx +++ b/src/sage/matroids/matroid.pyx @@ -2390,7 +2390,7 @@ cdef class Matroid(SageObject): B_ext.add(B | set([e])) for S in B_ext: C.add(self._circuit(S)) - return SetSystem(list(self.groundset()), C) + return SetSystem(self.groundset(), C) def circuits_iterator(self, k=None): """ @@ -2448,7 +2448,7 @@ cdef class Matroid(SageObject): ['d', 'e', 'f']] """ cdef SetSystem C - C = SetSystem(list(self.groundset())) + C = SetSystem(self.groundset()) for N in self.nonbases_iterator(): if self._rank(N) == self.full_rank() - 1: C.append(self._circuit(N)) @@ -2618,7 +2618,7 @@ cdef class Matroid(SageObject): Test all subsets of the groundset of cardinality ``self.full_rank()`` """ cdef SetSystem res - res = SetSystem(list(self.groundset())) + res = SetSystem(self.groundset()) for X in combinations(self.groundset(), self.full_rank()): if self._rank(X) < len(X): res.append(X) @@ -2679,7 +2679,7 @@ cdef class Matroid(SageObject): X = frozenset(XX) if not self._is_independent(X): D.add(X) - return SetSystem(list(self.groundset()), D) + return SetSystem(self.groundset(), D) def dependent_r_sets_iterator(self, long r): r""" @@ -2731,7 +2731,7 @@ cdef class Matroid(SageObject): :meth:`M.independent_r_sets() ` """ cdef SetSystem res - res = SetSystem(list(self.groundset())) + res = SetSystem(self.groundset()) for X in combinations(self.groundset(), self.full_rank()): if self._rank(frozenset(X)) == len(X): res.append(X) @@ -2871,7 +2871,7 @@ cdef class Matroid(SageObject): X = frozenset(XX) if self._is_independent(X): I.add(X) - return SetSystem(list(self.groundset()), I) + return SetSystem(self.groundset(), I) def independent_r_sets_iterator(self, r): r""" @@ -2995,7 +2995,7 @@ cdef class Matroid(SageObject): ['b', 'c', 'd'], ['b', 'e', 'g'], ['c', 'f', 'g'], ['d', 'e', 'f']] """ - return SetSystem(list(self.groundset()), subsets=[f[0] for f in self._flags(r)]) + return SetSystem(self.groundset(), subsets=[f[0] for f in self._flags(r)]) cpdef coflats(self, r): r""" @@ -3270,7 +3270,7 @@ cdef class Matroid(SageObject): if is_indep: B.append(frozenset(H)) next_level.extend(Ht) - return SetSystem(list(self.groundset()), B) + return SetSystem(self.groundset(), B) def no_broken_circuits_sets_iterator(self, ordering=None): r""" @@ -3579,7 +3579,7 @@ cdef class Matroid(SageObject): return self._is_isomorphic(other), self._isomorphism(other) if self is other: return True - return (self.full_rank() == other.full_rank() and SetSystem(list(self.groundset()), list(self.nonbases()))._isomorphism(SetSystem(list(other.groundset()), list(other.nonbases()))) is not None) + return (self.full_rank() == other.full_rank() and SetSystem(self.groundset(), list(self.nonbases()))._isomorphism(SetSystem(other.groundset(), list(other.nonbases()))) is not None) cpdef isomorphism(self, other): r""" @@ -3645,7 +3645,7 @@ cdef class Matroid(SageObject): if self is other: return {e:e for e in self.groundset()} if self.full_rank() == other.full_rank(): - return SetSystem(list(self.groundset()), list(self.nonbases()))._isomorphism(SetSystem(list(other.groundset()), list(other.nonbases()))) + return SetSystem(self.groundset(), list(self.nonbases()))._isomorphism(SetSystem(other.groundset(), list(other.nonbases()))) else: return None diff --git a/src/sage/matroids/set_system.pyx b/src/sage/matroids/set_system.pyx index 1fc0ed4546a..e702edc6a69 100644 --- a/src/sage/matroids/set_system.pyx +++ b/src/sage/matroids/set_system.pyx @@ -12,10 +12,8 @@ isomorphism testing. AUTHORS: - Rudi Pendavingh, Stefan van Zwam (2013-04-01): initial version - -Methods -======= """ + # **************************************************************************** # Copyright (C) 2013 Rudi Pendavingh # Copyright (C) 2013 Stefan van Zwam @@ -34,7 +32,7 @@ from sage.data_structures.bitset_base cimport * cdef class SetSystem: """ A ``SetSystem`` is an enumerator of a collection of subsets of a given - fixed and finite ground set. It offers the possibility to enumerate its + fixed and finite groundset. It offers the possibility to enumerate its contents. One is most likely to encounter these as output from some Matroid methods:: @@ -83,8 +81,8 @@ cdef class SetSystem: else: self._groundset = groundset self._idx = {} - for i in range(len(groundset)): - self._idx[groundset[i]] = i + for i in range(len(self._groundset)): + self._idx[self._groundset[i]] = i self._groundset_size = len(groundset) self._bitset_size = max(self._groundset_size, 1) @@ -99,11 +97,11 @@ cdef class SetSystem: INPUT: - - ``groundset`` -- a list or tuple of finitely many elements. - - ``subsets`` -- (default: ``None``) an enumerator for a set of - subsets of ``groundset``. - - ``capacity`` -- (default: ``1``) Initial maximal capacity of the set - system. + - ``groundset`` -- list or tuple of finitely many elements + - ``subsets`` -- (default: ``None``) enumerator for a set of subsets of + ``groundset`` + - ``capacity`` -- (default: ``1``) initial maximal capacity of the set + system EXAMPLES:: @@ -165,7 +163,7 @@ cdef class SetSystem: INPUT: - - ``k`` -- an integer. The index of the subset in the system. + - ``k`` -- integer; the index of the subset in the system OUTPUT: @@ -209,13 +207,13 @@ cdef class SetSystem: cdef _relabel(self, mapping): """ - Relabel each element ``e`` of the ground set as ``mapping[e]``, where + Relabel each element `e` of the ground set as ``mapping[e]``, where ``mapping`` is a given injective map. INPUT: - - ``mapping`` -- a python object such that ``mapping[e]`` is the new - label of ``e`` + - ``mapping`` -- a Python object such that ``mapping[e]`` is the new + label of `e` OUTPUT: ``None`` """ @@ -270,7 +268,7 @@ cdef class SetSystem: cdef inline _append(self, bitset_t X): """ - Append subset in internal, bitset format + Append subset in internal, bitset format. """ if self._capacity == self._len: self.resize(self._capacity * 2) @@ -310,7 +308,7 @@ cdef class SetSystem: cpdef _get_groundset(self): """ - Return the ground set of this SetSystem. + Return the groundset of this SetSystem. EXAMPLES:: @@ -326,7 +324,7 @@ cdef class SetSystem: Test if the :class:`SetSystem` is connected. A :class:`SetSystem` is connected if there is no nonempty proper subset - ``X`` of the ground set so the each subset is either contained in ``X`` + ``X`` of the groundset so the each subset is either contained in ``X`` or disjoint from ``X``. EXAMPLES:: @@ -504,30 +502,30 @@ cdef class SetSystem: cpdef _equitable_partition(self, SetSystem P=None, EP=None): r""" - Return an equitable ordered partition of the ground set of the + Return an equitable ordered partition of the groundset of the hypergraph whose edges are the subsets in this SetSystem. - Given any ordered partition `P = (p_1, ..., p_k)` of the ground set of + Given any ordered partition `P = (p_1, ..., p_k)` of the groundset of a hypergraph, any edge `e` of the hypergraph has a characteristic intersection number sequence `i(e)=(|p_1\cap e|, ... , |p_k\cap e|))`. There is an ordered partition `EP` of the edges that groups the edges according to this intersection number sequence. Given this an ordered partition of the edges, we may similarly refine `P` to a new ordered - partition `P'`, by considering the incidence numbers of ground set + partition `P'`, by considering the incidence numbers of groundset elements with each partition element of `EP`. The ordered partition `P` is equitable when `P' = P`. INPUT: - - ``P``, an equitable ordered partition of the ground set, stored as - a SetSystem. - - ``EP``, the corresponding equitable partition of the edges, stored - as a list of lists of indices of subsets of this SetSystem. + - ``P`` -- an equitable ordered partition of the groundset, stored as + a SetSystem + - ``EP`` -- the corresponding equitable partition of the edges, stored + as a list of lists of indices of subsets of this SetSystem OUTPUT: - - ``P``, an equitable ordered partition of the ground set, stored as a + - ``P``, an equitable ordered partition of the groundset, stored as a SetSystem. - ``EP``, the corresponding equitable partition of the edges, stored as a list of lists of indices of subsets of this SetSystem. @@ -596,18 +594,18 @@ cdef class SetSystem: INPUT: - - ``P`` -- (default: ``None``) an ordered partition of the ground set. + - ``P`` -- (default: ``None``) an ordered partition of the groundset - ``EP`` -- (default: ``None``) the corresponding partition of the edges, stored as a list of lists of indices of subsets of this - SetSystem. + SetSystem OUTPUT: - - ``P`` -- an ordered partition of the ground set into singletons, - stored as a SetSystem. + - ``P`` -- an ordered partition of the groundset into singletons, + stored as a SetSystem - ``EP`` -- the corresponding partition of the edges, stored as a list - of lists of indices of subsets of this SetSystem. - - ``h`` -- an integer invariant of the SetSystem. + of lists of indices of subsets of this SetSystem + - ``h`` -- integer invariant of the SetSystem EXAMPLES:: @@ -639,9 +637,9 @@ cdef class SetSystem: - ``other`` -- a SetSystem - ``SP`` (optional) -- a SetSystem storing an ordered partition of the - ground set of ``self`` + groundset of ``self`` - ``OP`` (optional) -- a SetSystem storing an ordered partition of the - ground set of ``other`` + groundset of ``other`` OUTPUT: