Skip to content

Commit f382241

Browse files
dcoudertdimpase
authored andcommitted
trac #33562: better error message
1 parent c3028e7 commit f382241

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,19 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23862386
[0 0 0]
23872387
[1 0 0]
23882388
[1 0 0]
2389+
2390+
Check error message for non numerical edge weights (:trac:`33562`)::
2391+
2392+
sage: G = Graph([(0, 1)])
2393+
sage: G.weighted_adjacency_matrix()
2394+
Traceback (most recent call last):
2395+
...
2396+
ValueError: the weight function cannot find the weight of (0, 1, None)
2397+
sage: G = Graph([(0, 1, 'a')])
2398+
sage: G.weighted_adjacency_matrix()
2399+
Traceback (most recent call last):
2400+
...
2401+
ValueError: the weight function cannot find the weight of (0, 1, 'a')
23892402
"""
23902403
if self.has_multiple_edges():
23912404
raise NotImplementedError("don't know how to represent weights for a multigraph")
@@ -2396,6 +2409,15 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23962409
set(vertices) != set(self.vertex_iterator())):
23972410
raise ValueError("``vertices`` must be a permutation of the vertices")
23982411

2412+
# Check the edge weights
2413+
if base_ring is None:
2414+
def weight_function(e):
2415+
return e[2]
2416+
else:
2417+
def weight_function(e):
2418+
return base_ring(e[2])
2419+
self._check_weight_function(weight_function)
2420+
23992421
new_indices = {v: i for i,v in enumerate(vertices)}
24002422

24012423
D = {}

0 commit comments

Comments
 (0)