Skip to content

Commit 7dee543

Browse files
dcoudertdimpase
authored andcommitted
trac #33562: add paramter default_weight
1 parent f382241 commit 7dee543

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,8 @@ def distance_matrix(self, vertices=None, *, base_ring=None, **kwds):
23162316
ret.set_immutable()
23172317
return ret
23182318

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):
23202321
"""
23212322
Return the weighted adjacency matrix of the graph.
23222323

@@ -2333,6 +2334,10 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23332334
each vertex is represented by its position in the list returned by
23342335
method :meth:`vertices`
23352336

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+
23362341
- ``base_ring`` -- a ring (default: determined from the weights); the base
23372342
ring of the matrix space to use.
23382343

@@ -2393,12 +2398,15 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23932398
sage: G.weighted_adjacency_matrix()
23942399
Traceback (most recent call last):
23952400
...
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]
23972405
sage: G = Graph([(0, 1, 'a')])
23982406
sage: G.weighted_adjacency_matrix()
23992407
Traceback (most recent call last):
24002408
...
2401-
ValueError: the weight function cannot find the weight of (0, 1, 'a')
2409+
TypeError: Cannot convert NoneType to sage.structure.parent.Parent
24022410
"""
24032411
if self.has_multiple_edges():
24042412
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
24092417
set(vertices) != set(self.vertex_iterator())):
24102418
raise ValueError("``vertices`` must be a permutation of the vertices")
24112419

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
24162427
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
24202432

24212433
new_indices = {v: i for i,v in enumerate(vertices)}
24222434

24232435
D = {}
24242436
if self._directed:
2425-
for u, v, l in self.edge_iterator():
2437+
for u, v, label in self.edge_iterator():
24262438
i = new_indices[u]
24272439
j = new_indices[v]
2428-
D[i,j] = l
2440+
D[i,j] = func(u, v, label)
24292441
else:
2430-
for u, v, l in self.edge_iterator():
2442+
for u, v, label in self.edge_iterator():
24312443
i = new_indices[u]
24322444
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
24352448
from sage.matrix.constructor import matrix
24362449
if base_ring is None:
24372450
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)

0 commit comments

Comments
 (0)