Skip to content

Commit 647b920

Browse files
author
Matthias Koeppe
committed
sage.matroids: Import fixes, # needs copied from #35095
1 parent 11ad882 commit 647b920

File tree

7 files changed

+90
-53
lines changed

7 files changed

+90
-53
lines changed

src/sage/matroids/advanced.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@
5959
from . import lean_matrix
6060
from .extension import LinearSubclasses, MatroidExtensions
6161
from .union_matroid import MatroidUnion, MatroidSum, PartitionMatroid
62-
from .graphic_matroid import GraphicMatroid
62+
63+
from sage.misc.lazy_import import lazy_import
64+
lazy_import('sage.matroids.graphic_matroid', 'GraphicMatroid')

src/sage/matroids/catalog.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
from sage.misc.lazy_import import lazy_import
4747
from sage.rings.integer_ring import ZZ
4848

49+
lazy_import('sage.rings.finite_rings.finite_field_constructor', 'GF')
50+
lazy_import('sage.schemes.projective.projective_space', 'ProjectiveSpace')
51+
52+
4953
# The order is the same as in Oxley.
5054

5155

@@ -134,7 +138,7 @@ def R6():
134138
True
135139
sage: M.is_connected()
136140
True
137-
sage: M.is_3connected()
141+
sage: M.is_3connected() # needs sage.graphs
138142
False
139143
"""
140144
A = Matrix(GF(3), [
@@ -166,7 +170,7 @@ def Fano():
166170
sage: setprint(sorted(M.nonspanning_circuits()))
167171
[{'a', 'b', 'f'}, {'a', 'c', 'e'}, {'a', 'd', 'g'}, {'b', 'c', 'd'},
168172
{'b', 'e', 'g'}, {'c', 'f', 'g'}, {'d', 'e', 'f'}]
169-
sage: M.delete(M.groundset_list()[randrange(0,
173+
sage: M.delete(M.groundset_list()[randrange(0, # needs sage.graphs
170174
....: 7)]).is_isomorphic(matroids.CompleteGraphic(4))
171175
True
172176
"""
@@ -197,9 +201,9 @@ def NonFano():
197201
sage: setprint(M.nonbases())
198202
[{'a', 'b', 'f'}, {'a', 'c', 'e'}, {'a', 'd', 'g'}, {'b', 'c', 'd'},
199203
{'b', 'e', 'g'}, {'c', 'f', 'g'}]
200-
sage: M.delete('f').is_isomorphic(matroids.CompleteGraphic(4))
204+
sage: M.delete('f').is_isomorphic(matroids.CompleteGraphic(4)) # needs sage.graphs
201205
True
202-
sage: M.delete('g').is_isomorphic(matroids.CompleteGraphic(4))
206+
sage: M.delete('g').is_isomorphic(matroids.CompleteGraphic(4)) # needs sage.graphs
203207
False
204208
"""
205209
A = Matrix(GF(3), [
@@ -226,7 +230,7 @@ def O7():
226230
227231
sage: M = matroids.named_matroids.O7(); M
228232
O7: Ternary matroid of rank 3 on 7 elements, type 0+
229-
sage: M.delete('e').is_isomorphic(matroids.CompleteGraphic(4))
233+
sage: M.delete('e').is_isomorphic(matroids.CompleteGraphic(4)) # needs sage.graphs
230234
True
231235
sage: M.tutte_polynomial()
232236
y^4 + x^3 + x*y^2 + 3*y^3 + 4*x^2 + 5*x*y + 5*y^2 + 4*x + 4*y
@@ -257,7 +261,7 @@ def P7():
257261
P7: Ternary matroid of rank 3 on 7 elements, type 1+
258262
sage: M.f_vector()
259263
[1, 7, 11, 1]
260-
sage: M.has_minor(matroids.CompleteGraphic(4))
264+
sage: M.has_minor(matroids.CompleteGraphic(4)) # needs sage.graphs
261265
False
262266
sage: M.is_valid()
263267
True
@@ -585,7 +589,7 @@ def J():
585589
[{'a', 'b', 'f'}, {'a', 'c', 'g'}, {'a', 'd', 'h'}]
586590
sage: M.is_isomorphic(M.dual())
587591
True
588-
sage: M.has_minor(matroids.CompleteGraphic(4))
592+
sage: M.has_minor(matroids.CompleteGraphic(4)) # needs sage.graphs
589593
False
590594
sage: M.is_valid()
591595
True
@@ -692,6 +696,8 @@ def K33dual():
692696
sage: M.is_valid() # long time, needs sage.graphs
693697
True
694698
"""
699+
from sage.graphs.graph_generators import graphs
700+
695701
E = 'abcdefghi'
696702
G = graphs.CompleteBipartiteGraph(3, 3)
697703
M = Matroid(groundset=E, graph=G, regular=True)
@@ -763,6 +769,8 @@ def CompleteGraphic(n):
763769
sage: M.is_valid()
764770
True
765771
"""
772+
from sage.graphs.graph_generators import graphs
773+
766774
M = Matroid(groundset=list(range((n * (n - 1)) // 2)),
767775
graph=graphs.CompleteGraph(n))
768776
M.rename('M(K' + str(n) + '): ' + repr(M))
@@ -858,7 +866,7 @@ def Whirl(n):
858866
sage: M.is_isomorphic(matroids.Wheel(5))
859867
False
860868
sage: M = matroids.Whirl(3)
861-
sage: M.is_isomorphic(matroids.CompleteGraphic(4))
869+
sage: M.is_isomorphic(matroids.CompleteGraphic(4)) # needs sage.graphs
862870
False
863871
864872
.. TODO::

src/sage/matroids/constructor.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103

104104
from itertools import combinations
105105
from sage.matrix.constructor import Matrix
106-
from sage.graphs.graph import Graph
107106
from sage.structure.element import is_Matrix
108107
from sage.rings.integer_ring import ZZ
109108
from sage.rings.rational_field import QQ
@@ -493,7 +492,7 @@ def Matroid(groundset=None, data=None, **kwds):
493492
....: matrix=[[1, 1, 0], [1, 0, 1], [0, 1, 1]],
494493
....: field=GF(4, 'x'))
495494
Quaternary matroid of rank 3 on 6 elements
496-
sage: Matroid([0, 1, 2, 3, 4, 5],
495+
sage: Matroid([0, 1, 2, 3, 4, 5], # needs sage.graphs
497496
....: matrix=[[1, 1, 0], [1, 0, 1], [0, 1, 1]],
498497
....: field=GF(2), regular=True)
499498
Regular matroid of rank 3 on 6 elements with 16 bases
@@ -628,25 +627,25 @@ def Matroid(groundset=None, data=None, **kwds):
628627
increase speed, this check can be skipped::
629628
630629
sage: M = matroids.named_matroids.Fano()
631-
sage: N = Matroid(M, regular=True)
630+
sage: N = Matroid(M, regular=True) # needs sage.graphs
632631
Traceback (most recent call last):
633632
...
634633
ValueError: input is not a valid regular matroid
635-
sage: N = Matroid(M, regular=True, check=False); N
634+
sage: N = Matroid(M, regular=True, check=False); N # needs sage.graphs
636635
Regular matroid of rank 3 on 7 elements with 32 bases
637636
638-
sage: N.is_valid()
637+
sage: N.is_valid() # needs sage.graphs
639638
False
640639
641640
Sometimes the output is regular, but represents a different matroid
642641
from the one you intended::
643642
644643
sage: M = Matroid(Matrix(GF(3), [[1, 0, 1, 1], [0, 1, 1, 2]]))
645-
sage: N = Matroid(Matrix(GF(3), [[1, 0, 1, 1], [0, 1, 1, 2]]),
644+
sage: N = Matroid(Matrix(GF(3), [[1, 0, 1, 1], [0, 1, 1, 2]]), # needs sage.graphs
646645
....: regular=True)
647-
sage: N.is_valid()
646+
sage: N.is_valid() # needs sage.graphs
648647
True
649-
sage: N.is_isomorphic(M)
648+
sage: N.is_isomorphic(M) # needs sage.graphs
650649
False
651650
652651
TESTS::
@@ -711,7 +710,11 @@ def Matroid(groundset=None, data=None, **kwds):
711710
groundset = None
712711

713712
if key is None:
714-
if isinstance(data, sage.graphs.graph.Graph):
713+
try:
714+
from sage.graphs.graph import Graph
715+
except ImportError:
716+
Graph = ()
717+
if isinstance(data, Graph):
715718
key = 'graph'
716719
elif is_Matrix(data):
717720
key = 'matrix'
@@ -772,6 +775,8 @@ def Matroid(groundset=None, data=None, **kwds):
772775
# Graphs:
773776

774777
elif key == 'graph':
778+
from sage.graphs.graph import Graph
779+
775780
if isinstance(data, sage.graphs.generic_graph.GenericGraph):
776781
G = data
777782
else:

src/sage/matroids/graphic_matroid.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,10 @@
8989
# ****************************************************************************
9090
from .matroid import Matroid
9191

92-
from sage.graphs.graph import Graph
9392
from copy import copy, deepcopy
9493
from .utilities import newlabel, split_vertex, sanitize_contractions_deletions
9594
from itertools import combinations
9695
from sage.rings.integer import Integer
97-
from sage.sets.disjoint_set import DisjointSet
9896

9997

10098
class GraphicMatroid(Matroid):
@@ -186,6 +184,7 @@ def __init__(self, G, groundset=None):
186184
running ._test_not_implemented_methods() . . . pass
187185
running ._test_pickling() . . . pass
188186
"""
187+
from sage.graphs.graph import Graph
189188

190189
if groundset is None:
191190
# Try to construct a ground set based on the edge labels.
@@ -284,6 +283,8 @@ def _rank(self, X):
284283
1
285284
286285
"""
286+
from sage.sets.disjoint_set import DisjointSet
287+
287288
edges = self.groundset_to_edges(X)
288289
vertices = set([u for (u, v, l) in edges]).union(
289290
[v for (u, v, l) in edges])
@@ -713,6 +714,8 @@ def _corank(self, X):
713714
sage: M._corank([1,2,3])
714715
3
715716
"""
717+
from sage.sets.disjoint_set import DisjointSet
718+
716719
all_vertices = self._G.vertices(sort=False)
717720
not_our_edges = self.groundset_to_edges(self._groundset.difference(X))
718721
DS_vertices = DisjointSet(all_vertices)
@@ -826,6 +829,8 @@ def _max_independent(self, X):
826829
sage: sorted(N._max_independent(frozenset(['a'])))
827830
[]
828831
"""
832+
from sage.sets.disjoint_set import DisjointSet
833+
829834
edges = self.groundset_to_edges(X)
830835
vertices = set([u for (u, v, l) in edges])
831836
vertices.update([v for (u, v, l) in edges])
@@ -861,6 +866,8 @@ def _max_coindependent(self, X):
861866
sage: sorted(N.max_coindependent([0,1,2,5]))
862867
[1, 2, 5]
863868
"""
869+
from sage.sets.disjoint_set import DisjointSet
870+
864871
edges = self.groundset_to_edges(X)
865872
all_vertices = self._G.vertices(sort=False)
866873
not_our_edges = self.groundset_to_edges(self._groundset.difference(X))
@@ -924,6 +931,8 @@ def _circuit(self, X):
924931
[4, 5]
925932
926933
"""
934+
from sage.sets.disjoint_set import DisjointSet
935+
927936
edges = self.groundset_to_edges(X)
928937
vertices = set([u for (u, v, l) in edges]).union(
929938
set([v for (u, v, l) in edges]))

src/sage/matroids/linear_matroid.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,8 +2844,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
28442844
EXAMPLES::
28452845
28462846
sage: M = matroids.Wheel(3)
2847-
sage: OS = M.orlik_terao_algebra()
2848-
sage: OS
2847+
sage: OS = M.orlik_terao_algebra(); OS
28492848
Orlik-Terao algebra of Wheel(3):
28502849
Regular matroid of rank 3 on 6 elements with 16 bases
28512850
over Integer Ring

0 commit comments

Comments
 (0)