@@ -2316,7 +2316,8 @@ def distance_matrix(self, vertices=None, *, base_ring=None, **kwds):
2316
2316
ret.set_immutable()
2317
2317
return ret
2318
2318
2319
- def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=None, **kwds):
2319
+ def weighted_adjacency_matrix(self, sparse=True, vertices=None,
2320
+ default_weight=None, *, base_ring=None, **kwds):
2320
2321
"""
2321
2322
Return the weighted adjacency matrix of the graph.
2322
2323
@@ -2333,6 +2334,10 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2333
2334
each vertex is represented by its position in the list returned by
2334
2335
method :meth:`vertices`
2335
2336
2337
+ - ``default_weight`` -- (default: ``None``); specifies the weight to
2338
+ replace any ``None`` edge label. When not specified an error is raised
2339
+ if the label of an edge is ``None``.
2340
+
2336
2341
- ``base_ring`` -- a ring (default: determined from the weights); the base
2337
2342
ring of the matrix space to use.
2338
2343
@@ -2393,12 +2398,15 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2393
2398
sage: G.weighted_adjacency_matrix()
2394
2399
Traceback (most recent call last):
2395
2400
...
2396
- ValueError: the weight function cannot find the weight of (0, 1, None)
2401
+ ValueError: cannot find the weight of (0, 1, None). Consider setting parameter 'default_weight'
2402
+ sage: G.weighted_adjacency_matrix(default_weight=3)
2403
+ [0 3]
2404
+ [3 0]
2397
2405
sage: G = Graph([(0, 1, 'a')])
2398
2406
sage: G.weighted_adjacency_matrix()
2399
2407
Traceback (most recent call last):
2400
2408
...
2401
- ValueError: the weight function cannot find the weight of (0, 1, 'a')
2409
+ TypeError: Cannot convert NoneType to sage.structure.parent.Parent
2402
2410
"""
2403
2411
if self.has_multiple_edges():
2404
2412
raise NotImplementedError("don't know how to represent weights for a multigraph")
@@ -2409,29 +2417,34 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
2409
2417
set(vertices) != set(self.vertex_iterator())):
2410
2418
raise ValueError("``vertices`` must be a permutation of the vertices")
2411
2419
2412
- # Check the edge weights
2413
- if base_ring is None:
2414
- def weight_function(e):
2415
- return e[2]
2420
+ # Method for checking edge weights and setting default weight
2421
+ if default_weight is None:
2422
+ def func(u, v, label):
2423
+ if label is None:
2424
+ raise ValueError(f"cannot find the weight of ({u}, {v}, None). "
2425
+ "Consider setting parameter 'default_weight'")
2426
+ return label
2416
2427
else:
2417
- def weight_function(e):
2418
- return base_ring(e[2])
2419
- self._check_weight_function(weight_function)
2428
+ def func(u, v, label):
2429
+ if label is None:
2430
+ return default_weight
2431
+ return label
2420
2432
2421
2433
new_indices = {v: i for i,v in enumerate(vertices)}
2422
2434
2423
2435
D = {}
2424
2436
if self._directed:
2425
- for u, v, l in self.edge_iterator():
2437
+ for u, v, label in self.edge_iterator():
2426
2438
i = new_indices[u]
2427
2439
j = new_indices[v]
2428
- D[i,j] = l
2440
+ D[i,j] = func(u, v, label)
2429
2441
else:
2430
- for u, v, l in self.edge_iterator():
2442
+ for u, v, label in self.edge_iterator():
2431
2443
i = new_indices[u]
2432
2444
j = new_indices[v]
2433
- D[i,j] = l
2434
- D[j,i] = l
2445
+ label = func(u, v, label)
2446
+ D[i,j] = label
2447
+ D[j,i] = label
2435
2448
from sage.matrix.constructor import matrix
2436
2449
if base_ring is None:
2437
2450
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)
0 commit comments