Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
GenericGraph.weighted_adjacency_matrix: Accept keyword arguments for …
Browse files Browse the repository at this point in the history
…matrix constructor
  • Loading branch information
Matthias Koeppe committed Feb 19, 2022
1 parent 03343f7 commit 688d054
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,7 @@ def distance_matrix(self, vertices=None, **kwds):

return ret

def weighted_adjacency_matrix(self, sparse=True, vertices=None):
def weighted_adjacency_matrix(self, sparse=True, vertices=None, base_ring=None, **kwds):
"""
Return the weighted adjacency matrix of the graph.

Expand All @@ -2257,6 +2257,12 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None):
each vertex is represented by its position in the list returned by
method :meth:`vertices`

- ``base_ring`` -- a ring (default: determined from the weights); the base
ring of the matrix space to use.

- ``**kwds`` -- other keywords to pass to
:func:`~sage.matrix.constructor.matrix`

EXAMPLES::

sage: G = Graph(sparse=True, weighted=True)
Expand All @@ -2275,6 +2281,26 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None):
[0 2 0 1]
[4 3 1 0]

Using a different matrix implementation::

sage: M = G.weighted_adjacency_matrix(sparse=False, base_ring=ZZ, implementation='numpy'); M
[0 1 3 4]
[1 0 2 0]
[3 2 0 0]
[4 0 0 0]

As an immutable matrix::

sage: M = G.weighted_adjacency_matrix(immutable=True); M
[0 1 3 4]
[1 0 2 0]
[3 2 0 0]
[4 0 0 0]
sage: M[2, 2] = 1
Traceback (most recent call last):
...
ValueError: matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).

TESTS:

The following doctest verifies that :trac:`4888` is fixed::
Expand Down Expand Up @@ -2309,7 +2335,10 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None):
D[i,j] = l
D[j,i] = l
from sage.matrix.constructor import matrix
M = matrix(self.num_verts(), D, sparse=sparse)
if base_ring is None:
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)
else:
M = matrix(base_ring, self.num_verts(), D, sparse=sparse, **kwds)
return M

def kirchhoff_matrix(self, weighted=None, indegree=True, normalized=False, signless=False, **kwds):
Expand Down

0 comments on commit 688d054

Please sign in to comment.