@@ -2324,7 +2324,8 @@ def distance_matrix(self, vertices=None, *, base_ring=None, **kwds):
2324
2324
ret.set_immutable()
2325
2325
return ret
2326
2326
2327
- def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=None, **kwds):
2327
+ def weighted_adjacency_matrix(self, sparse=True, vertices=None,
2328
+ default_weight=None, *, base_ring=None, **kwds):
2328
2329
"""
2329
2330
Return the weighted adjacency matrix of the graph.
2330
2331
@@ -2341,6 +2342,10 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2341
2342
each vertex is represented by its position in the list returned by
2342
2343
method :meth:`vertices`
2343
2344
2345
+ - ``default_weight`` -- (default: ``None``); specifies the weight to
2346
+ replace any ``None`` edge label. When not specified an error is raised
2347
+ if the label of an edge is ``None``.
2348
+
2344
2349
- ``base_ring`` -- a ring (default: determined from the weights); the base
2345
2350
ring of the matrix space to use.
2346
2351
@@ -2394,6 +2399,22 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2394
2399
[0 0 0]
2395
2400
[1 0 0]
2396
2401
[1 0 0]
2402
+
2403
+ Check error message for non numerical edge weights (:trac:`33562`)::
2404
+
2405
+ sage: G = Graph([(0, 1)])
2406
+ sage: G.weighted_adjacency_matrix()
2407
+ Traceback (most recent call last):
2408
+ ...
2409
+ ValueError: cannot find the weight of (0, 1, None). Consider setting parameter 'default_weight'
2410
+ sage: G.weighted_adjacency_matrix(default_weight=3)
2411
+ [0 3]
2412
+ [3 0]
2413
+ sage: G = Graph([(0, 1, 'a')])
2414
+ sage: G.weighted_adjacency_matrix()
2415
+ Traceback (most recent call last):
2416
+ ...
2417
+ TypeError: Cannot convert NoneType to sage.structure.parent.Parent
2397
2418
"""
2398
2419
if self.has_multiple_edges():
2399
2420
raise NotImplementedError("don't know how to represent weights for a multigraph")
@@ -2404,20 +2425,35 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2404
2425
set(vertices) != set(self.vertex_iterator())):
2405
2426
raise ValueError("``vertices`` must be a permutation of the vertices")
2406
2427
2407
- new_indices = {v: i for i, v in enumerate(vertices)}
2428
+ # Method for checking edge weights and setting default weight
2429
+ if default_weight is None:
2430
+ def func(u, v, label):
2431
+ if label is None:
2432
+ raise ValueError(f"cannot find the weight of ({u}, {v}, None). "
2433
+ "Consider setting parameter 'default_weight'")
2434
+ return label
2435
+ else:
2436
+ def func(u, v, label):
2437
+ if label is None:
2438
+ return default_weight
2439
+ return label
2440
+
2441
+ new_indices = {v: i for i,v in enumerate(vertices)}
2408
2442
2409
2443
D = {}
2410
2444
if self._directed:
2411
- for u, v, l in self.edge_iterator():
2445
+ for u, v, label in self.edge_iterator():
2412
2446
i = new_indices[u]
2413
2447
j = new_indices[v]
2414
- D[i, j] = l
2448
+ D[i, j] = func(u, v, label)
2415
2449
else:
2416
- for u, v, l in self.edge_iterator():
2450
+ for u, v, label in self.edge_iterator():
2417
2451
i = new_indices[u]
2418
2452
j = new_indices[v]
2419
- D[i, j] = l
2420
- D[j, i] = l
2453
+ label = func(u, v, label)
2454
+ D[i, j] = label
2455
+ D[j, i] = label
2456
+
2421
2457
from sage.matrix.constructor import matrix
2422
2458
if base_ring is None:
2423
2459
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)
0 commit comments