Skip to content

Commit 4ffa282

Browse files
committed
More internal iterators and cythonization
1 parent a092c7e commit 4ffa282

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

src/sage/matroids/matroid.pxd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ cdef class Matroid(SageObject):
129129
cpdef broken_circuits(self, ordering=*) noexcept
130130
cpdef no_broken_circuits_sets(self, ordering=*) noexcept
131131

132+
# polytopes
133+
cpdef matroid_polytope(self) noexcept
134+
cpdef independence_matroid_polytope(self) noexcept
135+
132136
# isomorphism
133137
cpdef is_isomorphic(self, other, certificate=*) noexcept
134138
cpdef _is_isomorphic(self, other, certificate=*) noexcept
@@ -155,6 +159,7 @@ cdef class Matroid(SageObject):
155159
cpdef modular_cut(self, subsets) noexcept
156160
cpdef linear_subclasses(self, line_length=*, subsets=*) noexcept
157161
cpdef extensions(self, element=*, line_length=*, subsets=*) noexcept
162+
cpdef coextensions(self, element=*, coline_length=*, subsets=*) noexcept
158163

159164
# connectivity
160165
cpdef simplify(self) noexcept
@@ -222,8 +227,12 @@ cdef class Matroid(SageObject):
222227
cpdef automorphism_group(self) noexcept
223228
cpdef bergman_complex(self) noexcept
224229
cpdef augmented_bergman_complex(self) noexcept
230+
cpdef broken_circuit_complex(self, ordering=*) noexcept
225231

226232
# visualization
227233
cpdef plot(self,B=*,lineorders=*,pos_method=*,pos_dict=*,save_pos=*) noexcept
228234
cpdef show(self,B=*,lineorders=*,pos_method=*,pos_dict=*,save_pos=*,lims=*) noexcept
229235
cpdef _fix_positions(self,pos_dict=*,lineorders=*) noexcept
236+
237+
# construction
238+
cpdef direct_sum(self, matroids) noexcept

src/sage/matroids/matroid.pyx

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ additional functionality (e.g. linear extensions).
147147
- :meth:`chow_ring() <sage.matroids.matroid.Matroid.chow_ring>`
148148
- :meth:`matroid_polytope() <sage.matroids.matroid.Matroid.matroid_polytope>`
149149
- :meth:`independence_matroid_polytope() <sage.matroids.matroid.Matroid.independence_matroid_polytope>`
150+
- :meth:`lattice_of_flats() <sage.matroids.matroid.Matroid.lattice_of_flats>`
150151
- :meth:`orlik_solomon_algebra() <sage.matroids.matroid.Matroid.orlik_solomon_algebra>`
151152
- :meth:`bergman_complex() <sage.matroids.matroid.Matroid.bergman_complex>`
152153
- :meth:`augmented_bergman_complex() <sage.matroids.matroid.Matroid.augmented_bergman_complex>`
@@ -1113,9 +1114,9 @@ cdef class Matroid(SageObject):
11131114
if certificate:
11141115
return False, None
11151116
return False
1116-
YY = self.dual().independent_r_sets(cd)
1117-
for X in self.independent_r_sets(rd):
1118-
for Y in YY:
1117+
D = self.dual()
1118+
for X in self.independent_r_sets_iterator(rd):
1119+
for Y in D.independent_r_sets_iterator(cd):
11191120
if X.isdisjoint(Y):
11201121
if N._is_isomorphic(self._minor(contractions=X, deletions=Y)):
11211122
if certificate:
@@ -2405,27 +2406,6 @@ cdef class Matroid(SageObject):
24052406
C.update([self._cocircuit(self.groundset().difference(B).union(set([e]))) for e in B])
24062407
return list(C)
24072408

2408-
def cocircuits_iterator(self):
2409-
"""
2410-
Return an iterator over the cocircuits of the matroid.
2411-
2412-
.. SEEALSO::
2413-
2414-
:meth:`M.cocircuit() <sage.matroids.matroid.Matroid.cocircuit>`
2415-
2416-
EXAMPLES::
2417-
2418-
sage: M = matroids.catalog.Fano()
2419-
sage: sorted([sorted(C) for C in M.cocircuits_iterator()])
2420-
[['a', 'b', 'c', 'g'], ['a', 'b', 'd', 'e'], ['a', 'c', 'd', 'f'],
2421-
['a', 'e', 'f', 'g'], ['b', 'c', 'e', 'f'], ['b', 'd', 'f', 'g'],
2422-
['c', 'd', 'e', 'g']]
2423-
"""
2424-
C = set()
2425-
for B in self.bases_iterator():
2426-
C.update([self._cocircuit(self.groundset().difference(B).union(set([e]))) for e in B])
2427-
return list(C)
2428-
24292409
cpdef noncospanning_cocircuits(self) noexcept:
24302410
"""
24312411
Return the noncospanning cocircuits of the matroid.
@@ -3019,7 +2999,7 @@ cdef class Matroid(SageObject):
30192999
`(w_0=1, ..., w_r)` -- are numbers of alternating sign, where `w_i` is
30203000
the value of the coefficient of the `(r-i)`-th degree term of the
30213001
matroid's characteristic polynomial. Moreover, `|w_i|` is the number of
3022-
`i`-faces of the broken circuit complex of the matroid.
3002+
`(i-1)`-dimensional faces of the broken circuit complex of the matroid.
30233003
30243004
OUTPUT: a list of integers
30253005
@@ -3318,7 +3298,7 @@ cdef class Matroid(SageObject):
33183298

33193299
# polytopes
33203300

3321-
def matroid_polytope(self):
3301+
cpdef matroid_polytope(self) noexcept:
33223302
r"""
33233303
Return the matroid polytope of ``self``.
33243304
@@ -3357,10 +3337,15 @@ cdef class Matroid(SageObject):
33573337
n = self.size()
33583338
vector_e = FreeModule(ZZ, n).basis()
33593339
convert = {ind: i for i, ind in enumerate(self.groundset())}
3360-
vertices = [sum(vector_e[convert[i]] for i in B) for B in self.bases_iterator()]
3340+
vertices = []
3341+
for B in self.bases_iterator():
3342+
sum = 0
3343+
for i in B:
3344+
sum += vector_e[convert[i]]
3345+
vertices += [sum]
33613346
return Polyhedron(vertices)
33623347

3363-
def independence_matroid_polytope(self):
3348+
cpdef independence_matroid_polytope(self) noexcept:
33643349
r"""
33653350
Return the independence matroid polytope of ``self``.
33663351
@@ -3400,7 +3385,12 @@ cdef class Matroid(SageObject):
34003385
ambient = FreeModule(ZZ, n)
34013386
vector_e = ambient.basis()
34023387
convert = {ind: i for i, ind in enumerate(self.groundset())}
3403-
vertices = [ambient.sum(vector_e[convert[i]] for i in IS) for IS in self.independent_sets_iterator()]
3388+
vertices = []
3389+
for IS in self.independent_sets_iterator():
3390+
lst = []
3391+
for i in IS:
3392+
lst += [vector_e[convert[i]]]
3393+
vertices += [ambient.sum(lst)]
34043394
return Polyhedron(vertices)
34053395

34063396
# isomorphism and equality
@@ -4256,8 +4246,7 @@ cdef class Matroid(SageObject):
42564246
sage: matroids.catalog.NonFano().has_minor(M)
42574247
True
42584248
sage: matroids.catalog.NonFano().has_minor(M, certificate=True)
4259-
(True, (frozenset(), frozenset({'g'}),
4260-
{0: 'b', 1: 'c', 2: 'a', 3: 'd', 4: 'e', 5: 'f'}))
4249+
(True, (frozenset(), frozenset({...}), {...}))
42614250
sage: M = matroids.catalog.Fano()
42624251
sage: M.has_minor(M, True)
42634252
(True,
@@ -4735,7 +4724,7 @@ cdef class Matroid(SageObject):
47354724
raise ValueError("cannot extend by element already in groundset")
47364725
return extension.MatroidExtensions(self, element, line_length=line_length, subsets=subsets) # return enumerator
47374726

4738-
def coextensions(self, element=None, coline_length=None, subsets=None):
4727+
cpdef coextensions(self, element=None, coline_length=None, subsets=None) noexcept:
47394728
r"""
47404729
Return an iterable set of single-element coextensions of the matroid.
47414730
@@ -6632,12 +6621,12 @@ cdef class Matroid(SageObject):
66326621
sage: M.is_chordal(4, 5)
66336622
False
66346623
sage: M.is_chordal(4, 5, certificate=True)
6635-
(False, frozenset({'a', 'b', 'e', 'f', 'g'}))
6624+
(False, frozenset({...}))
66366625
"""
66376626
cdef frozenset C
66386627
if k2 is None:
66396628
k2 = len(self.groundset()) + 1 # This is always larger than the rank
6640-
for C in self.circuits():
6629+
for C in self.circuits_iterator():
66416630
if len(C) < k1 or len(C) > k2:
66426631
continue
66436632
if not self._is_circuit_chordal(C):
@@ -7754,9 +7743,9 @@ cdef class Matroid(SageObject):
77547743
77557744
\chi_M(\lambda) = \sum_{S \subseteq E} (-1)^{|S|}\lambda^{r(E)-r(S)},
77567745
7757-
where `E` is the groundset and`r` is the matroid's rank function. The
7746+
where `E` is the groundset and `r` is the matroid's rank function. The
77587747
characteristic polynomial is also equal to
7759-
\sum_{i = 0}^r w_i\lambda^{r-i}, where `\{w_i\}_{i=0}^r` are the
7748+
`\sum_{i = 0}^r w_i\lambda^{r-i}`, where `\{w_i\}_{i=0}^r` are the
77607749
Whitney numbers of the first kind.
77617750
77627751
INPUT:
@@ -8106,7 +8095,7 @@ cdef class Matroid(SageObject):
81068095
self._cached_info = {'plot_positions': pos_dict, 'lineorders': lineorders}
81078096
return
81088097

8109-
def broken_circuit_complex(self, ordering=None):
8098+
cpdef broken_circuit_complex(self, ordering=None) noexcept:
81108099
r"""
81118100
Return the broken circuit complex of ``self``.
81128101
@@ -8171,7 +8160,7 @@ cdef class Matroid(SageObject):
81718160
[Oxl2011]_, p. 189.
81728161
"""
81738162
from sage.topology.simplicial_complex import SimplicialComplex
8174-
return SimplicialComplex(self.bases()).automorphism_group()
8163+
return SimplicialComplex(self.bases(), maximality_check=False).automorphism_group()
81758164

81768165
cpdef bergman_complex(self) noexcept:
81778166
r"""
@@ -8265,7 +8254,7 @@ cdef class Matroid(SageObject):
82658254
"""
82668255
# Construct independent set complex from bases
82678256
from sage.topology.simplicial_complex import SimplicialComplex
8268-
IM = SimplicialComplex(self.bases())
8257+
IM = SimplicialComplex(self.bases(), maximality_check=False)
82698258
82708259
LM = self.lattice_of_flats()
82718260
@@ -8332,7 +8321,7 @@ cdef class Matroid(SageObject):
83328321
matroids.insert(0, self)
83338322
return union_matroid.MatroidUnion(iter(matroids))
83348323
8335-
def direct_sum(self, matroids):
8324+
cpdef direct_sum(self, matroids) noexcept:
83368325
r"""
83378326
Return the matroid direct sum with another matroid or list of
83388327
matroids.

0 commit comments

Comments
 (0)