Skip to content

Commit

Permalink
sagemathgh-38056: SetSystem: Minor change to accommodate set input
Browse files Browse the repository at this point in the history
    
This change makes the code cleaner in multiple places.
The PR also includes some docstring edits in `set_system.pyx`.

### ⌛ Dependencies

- Depends on sagemath#37930 and sagemath#38027
    
URL: sagemath#38056
Reported by: gmou3
Reviewer(s): Matthias Köppe
  • Loading branch information
Release Manager committed May 27, 2024
2 parents 605c6dd + 79143df commit 0ba64f8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/sage/matroids/circuit_closures_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions src/sage/matroids/circuits_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/matroids/flats_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down
20 changes: 10 additions & 10 deletions src/sage/matroids/matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,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):
"""
Expand Down Expand Up @@ -2447,7 +2447,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))
Expand Down Expand Up @@ -2617,7 +2617,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)
Expand Down Expand Up @@ -2678,7 +2678,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"""
Expand Down Expand Up @@ -2730,7 +2730,7 @@ cdef class Matroid(SageObject):
:meth:`M.independent_r_sets() <sage.matroids.matroid.Matroid.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)
Expand Down Expand Up @@ -2870,7 +2870,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"""
Expand Down Expand Up @@ -2994,7 +2994,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"""
Expand Down Expand Up @@ -3269,7 +3269,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"""
Expand Down Expand Up @@ -3578,7 +3578,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"""
Expand Down Expand Up @@ -3644,7 +3644,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

Expand Down
60 changes: 29 additions & 31 deletions src/sage/matroids/set_system.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ isomorphism testing.
AUTHORS:
- Rudi Pendavingh, Stefan van Zwam (2013-04-01): initial version
Methods
=======
"""

# ****************************************************************************
# Copyright (C) 2013 Rudi Pendavingh <rudi.pendavingh@gmail.com>
# Copyright (C) 2013 Stefan van Zwam <stefanvanzwam@gmail.com>
Expand All @@ -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::
Expand Down Expand Up @@ -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)
Expand All @@ -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::
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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::
Expand All @@ -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::
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0ba64f8

Please sign in to comment.