@@ -2386,6 +2386,19 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2386
2386
[0 0 0]
2387
2387
[1 0 0]
2388
2388
[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')
2389
2402
"""
2390
2403
if self.has_multiple_edges():
2391
2404
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
2396
2409
set(vertices) != set(self.vertex_iterator())):
2397
2410
raise ValueError("``vertices`` must be a permutation of the vertices")
2398
2411
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
+
2399
2421
new_indices = {v: i for i,v in enumerate(vertices)}
2400
2422
2401
2423
D = {}
0 commit comments