Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GenericGraph.adjacency_matrix: using sort=True when getting vertices #35245

Merged
merged 5 commits into from
Jun 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,8 @@ def adjacency_matrix(self, sparse=None, vertices=None, *, base_ring=None, **kwds
the vertices defining how they should appear in the
matrix. By default, the ordering given by
:meth:`GenericGraph.vertices` with ``sort=True`` is used.
If the vertices are not comparable, the keyword ``vertices`` must be
used to specify an ordering, or a TypeError exception will be raised.

- ``base_ring`` -- a ring (default: ``ZZ``); the base ring of the matrix
space to use.
Expand Down Expand Up @@ -1918,6 +1920,14 @@ def adjacency_matrix(self, sparse=None, vertices=None, *, base_ring=None, **kwds
Traceback (most recent call last):
...
ValueError: ``vertices`` must be a permutation of the vertices
sage: Graph ([[0, 42, 'John'], [(42, 'John')]]).adjacency_matrix()
Traceback (most recent call last):
...
TypeError: Vertex labels are not comparable. You must specify an ordering using parameter ``vertices``
sage: Graph ([[0, 42, 'John'], [(42, 'John')]]).adjacency_matrix(vertices=['John', 42, 0])
[0 1 0]
[1 0 0]
[0 0 0]
"""
n = self.order()
if sparse is None:
Expand All @@ -1926,7 +1936,12 @@ def adjacency_matrix(self, sparse=None, vertices=None, *, base_ring=None, **kwds
sparse = False

if vertices is None:
vertices = self.vertices(sort=True)
try:
vertices = self.vertices(sort=True)
except TypeError:
raise TypeError("Vertex labels are not comparable. You must "
"specify an ordering using parameter "
"``vertices``")
elif (len(vertices) != n or
set(vertices) != set(self.vertex_iterator())):
raise ValueError("``vertices`` must be a permutation of the vertices")
Expand Down