Skip to content

Commit 48e45e5

Browse files
committed
More suggestions by tscrim and docstring improvements
More examples and other mostly minor improvements Docstring improvements: 1-line references and outputs, 95 column limit in examples
1 parent 3767c65 commit 48e45e5

File tree

4 files changed

+205
-306
lines changed

4 files changed

+205
-306
lines changed

src/sage/matroids/circuits_matroid.pyx

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ cdef class CircuitsMatroid(Matroid):
150150
a subset of ``self.groundset()``
151151
152152
OUTPUT: boolean
153+
154+
EXAMPLES::
155+
156+
sage: M = matroids.Theta(4)
157+
sage: M._is_independent(['y0', 'y1', 'y3', 'x2'])
158+
False
159+
sage: M._is_independent(['y0', 'y2', 'y3', 'x2'])
160+
True
153161
"""
154162
cdef set I = set(F)
155163
cdef int s = len(F)
@@ -170,14 +178,19 @@ cdef class CircuitsMatroid(Matroid):
170178
a subset of ``self.groundset()``
171179
172180
OUTPUT: a frozenset; a maximal independent subset of ``X``
181+
182+
EXAMPLES::
183+
184+
sage: M = matroids.Theta(6)
185+
sage: len(M._max_independent(M.groundset()))
186+
6
173187
"""
174188
cdef set I = set(F)
175189
for i in self._k_C:
176190
for C in self._k_C[i]:
177191
if i <= len(I) and i > 0:
178192
if C <= I:
179-
for e in C:
180-
break
193+
e = next(iter(C))
181194
I.remove(e)
182195

183196
return frozenset(I)
@@ -193,6 +206,16 @@ cdef class CircuitsMatroid(Matroid):
193206
194207
OUTPUT: a frozenset; a circuit contained in ``X``, if it exists.
195208
Otherwise an error is raised.
209+
210+
EXAMPLES::
211+
212+
sage: M = matroids.Theta(4)
213+
sage: sorted(M._circuit(['y0', 'y1', 'y3', 'x2']))
214+
['x2', 'y0', 'y1', 'y3']
215+
sage: M._circuit(['y0', 'y2', 'y3', 'x2'])
216+
Traceback (most recent call last):
217+
...
218+
ValueError: no circuit in independent set
196219
"""
197220
cdef set I = set(F)
198221
for C in self._C:
@@ -212,6 +235,17 @@ cdef class CircuitsMatroid(Matroid):
212235
OUTPUT: boolean, and, if certificate = True, a dictionary giving the
213236
isomorphism or None
214237
238+
EXAMPLES::
239+
240+
sage: M = matroids.Spike(3)
241+
sage: from sage.matroids.basis_matroid import BasisMatroid
242+
sage: N = BasisMatroid(M)
243+
sage: M.is_isomorphic(N)
244+
True
245+
sage: N = matroids.catalog.Vamos()
246+
sage: M.is_isomorphic(N)
247+
False
248+
215249
.. NOTE::
216250
217251
Internal version that does no input checking.
@@ -226,9 +260,16 @@ cdef class CircuitsMatroid(Matroid):
226260
def _repr_(self):
227261
"""
228262
Return a string representation of the matroid.
263+
264+
EXAMPLES::
265+
266+
sage: matroids.Theta(10)
267+
Theta_10: Matroid of rank 10 on 20 elements with 490 circuits
268+
sage: matroids.catalog.NonDesargues()
269+
NonDesargues: Matroid of rank 3 on 10 elements with 9 nonspanning circuits
229270
"""
230271
if self._nsc_defined:
231-
return Matroid._repr_(self) + " with " + str(len(self.nonspanning_circuits())) + " non-spanning circuits"
272+
return Matroid._repr_(self) + " with " + str(len(self.nonspanning_circuits())) + " nonspanning circuits"
232273
else:
233274
return Matroid._repr_(self) + " with " + str(len(self._C)) + " circuits"
234275

@@ -387,13 +428,16 @@ cdef class CircuitsMatroid(Matroid):
387428
sage: len(M.bases())
388429
6
389430
"""
390-
cdef SetSystem B
431+
cdef SetSystem B, NSC
432+
cdef bint flag
391433
B = SetSystem(list(self.groundset()))
434+
NSC = self.nonspanning_circuits()
392435
from itertools import combinations
393436
for S in combinations(self._groundset, self._matroid_rank):
394437
flag = True
395-
for C in self.nonspanning_circuits():
396-
if C <= set(S):
438+
S = frozenset(S)
439+
for C in NSC:
440+
if C <= S:
397441
flag = False
398442
break
399443
if flag:
@@ -411,16 +455,20 @@ cdef class CircuitsMatroid(Matroid):
411455
sage: it = M.bases_iterator()
412456
sage: it.__next__()
413457
frozenset({0, 1})
458+
sage: sorted(M.bases_iterator(), key=str)
459+
[frozenset({0, 1}),
460+
frozenset({0, 2}),
461+
frozenset({0, 3}),
462+
frozenset({1, 2}),
463+
frozenset({1, 3}),
464+
frozenset({2, 3})]
414465
"""
415466
from itertools import combinations
467+
cdef SetSystem NSC = self.nonspanning_circuits()
416468
for B in combinations(self._groundset, self._matroid_rank):
417-
flag = True
418-
for C in self.nonspanning_circuits():
419-
if C <= set(B):
420-
flag = False
421-
break
422-
if flag:
423-
yield frozenset(B)
469+
B = frozenset(B)
470+
if not any(C <= B for C in NSC):
471+
yield B
424472

425473
cpdef circuits(self, k=None) noexcept:
426474
"""
@@ -438,12 +486,20 @@ cdef class CircuitsMatroid(Matroid):
438486
sage: M = CircuitsMatroid(matroids.Uniform(2, 4))
439487
sage: M.circuits()
440488
Iterator over a system of subsets
489+
sage: list(M.circuits(0))
490+
[]
491+
sage: sorted(M.circuits(3), key=str)
492+
[frozenset({0, 1, 2}),
493+
frozenset({0, 1, 3}),
494+
frozenset({0, 2, 3}),
495+
frozenset({1, 2, 3})]
441496
"""
442497
cdef SetSystem C
443498
C = SetSystem(list(self.groundset()))
444-
if k:
445-
for c in self._k_C[k]:
446-
C.append(c)
499+
if k is not None:
500+
if k in self._k_C:
501+
for c in self._k_C[k]:
502+
C.append(c)
447503
else:
448504
for i in self._k_C:
449505
for c in self._k_C[i]:
@@ -464,18 +520,26 @@ cdef class CircuitsMatroid(Matroid):
464520
sage: M = CircuitsMatroid(matroids.Uniform(2, 4))
465521
sage: sum(1 for C in M.circuits_iterator())
466522
4
467-
"""
468-
if k:
469-
for C in self._k_C[k]:
470-
yield C
523+
sage: list(M.circuits_iterator(0))
524+
[]
525+
sage: sorted(M.circuits_iterator(3), key=str)
526+
[frozenset({0, 1, 2}),
527+
frozenset({0, 1, 3}),
528+
frozenset({0, 2, 3}),
529+
frozenset({1, 2, 3})]
530+
"""
531+
if k is not None:
532+
if k in self._k_C:
533+
for C in self._k_C[k]:
534+
yield C
471535
else:
472536
for i in self._k_C:
473537
for C in self._k_C[i]:
474538
yield C
475539

476540
cpdef nonspanning_circuits(self) noexcept:
477541
"""
478-
Return the list of nonspanning circuits of the matroid.
542+
Return the nonspanning circuits of the matroid.
479543
480544
OUTPUT: a SetSystem
481545
@@ -486,13 +550,11 @@ cdef class CircuitsMatroid(Matroid):
486550
sage: M.nonspanning_circuits()
487551
Iterator over a system of subsets
488552
"""
489-
cdef SetSystem NSC
490-
NSC = SetSystem(list(self.groundset()))
553+
cdef list NSC = []
491554
for i in self._k_C:
492555
if i <= self.rank():
493-
for C in self._k_C[i]:
494-
NSC.append(C)
495-
return NSC
556+
NSC.extend(self._k_C[i])
557+
return SetSystem(list(self.groundset()), NSC)
496558

497559
def nonspanning_circuits_iterator(self):
498560
"""
@@ -591,9 +653,7 @@ cdef class CircuitsMatroid(Matroid):
591653
sage: matroids.Theta(10).girth()
592654
3
593655
594-
REFERENCES:
595-
596-
[Oxl2011]_, p. 327.
656+
REFERENCES: [Oxl2011]_, p. 327.
597657
"""
598658
return min(self._k_C, default=float('inf'))
599659

@@ -655,16 +715,17 @@ cdef class CircuitsMatroid(Matroid):
655715
for j in self._k_C:
656716
if i <= j:
657717
for C1 in self._k_C[i]:
658-
if len(C1) == 0:
718+
if not C1:
659719
return False
660720
for C2 in self._k_C[j]:
661721
if C1 < C2:
662722
return False
663723
if C1 == C2:
664724
break
725+
UC12 = set(C1) | set(C2)
665726
for e in C1 & C2:
666727
flag = False
667-
S = (set(C1) | set(C2)) - {e}
728+
S = UC12 - {e}
668729
for k in self._k_C:
669730
if k <= len(S) and not flag:
670731
for C3 in self._k_C[k]:

src/sage/matroids/database_collections.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ def AllMatroids(n, r=None, type="all"):
102102
AttributeError: The type "nice" is not available. There needs to be an "is_nice()"
103103
attribute for the type to be supported.
104104
105-
REFERENCES:
106-
107-
The underlying database was retrieved from Yoshitake Matsumoto's Database
108-
of Matroids; see [Mat2012]_.
105+
REFERENCES: The underlying database was retrieved from Yoshitake
106+
Matsumoto's Database of Matroids; see [Mat2012]_.
109107
110108
TESTS::
111109
@@ -248,10 +246,8 @@ def OxleyMatroids():
248246
:mod:`Matroid catalog <sage.matroids.matroids_catalog>`, under
249247
``Oxley's matroid collection``.
250248
251-
REFERENCES:
252-
253-
These matroids are the nonparametrized matroids that appear in the
254-
Appendix ``Some Interesting Matroids`` in [Oxl2011]_ (p. 639-64).
249+
REFERENCES: These matroids are the nonparametrized matroids that appear in
250+
the Appendix ``Some Interesting Matroids`` in [Oxl2011]_ (p. 639-64).
255251
"""
256252
from sage.matroids.database_matroids import (
257253
U24, U25, U35, K4, Whirl3, Q6, P6, U36, R6,

0 commit comments

Comments
 (0)