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

Commit

Permalink
GenericGraph.incidence_matrix: Accept keyword arguments for matrix co…
Browse files Browse the repository at this point in the history
…nstructor
  • Loading branch information
Matthias Koeppe committed Feb 19, 2022
1 parent e30c7cb commit 6f2185a
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ def adjacency_matrix(self, sparse=None, vertices=None, *, base_ring=None, **kwds
space to use.

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

EXAMPLES::

Expand Down Expand Up @@ -1948,7 +1948,8 @@ def adjacency_matrix(self, sparse=None, vertices=None, *, base_ring=None, **kwds

am = adjacency_matrix # shorter call makes life easier

def incidence_matrix(self, oriented=None, sparse=True, vertices=None, edges=None):
def incidence_matrix(self, oriented=None, sparse=True, vertices=None, edges=None,
*, base_ring=None, **kwds):
r"""
Return the incidence matrix of the (di)graph.

Expand Down Expand Up @@ -1992,6 +1993,12 @@ def incidence_matrix(self, oriented=None, sparse=True, vertices=None, edges=None
``edges``, otherwise, the `i`-th column of the matrix corresponds to
the `i`-th edge in the ordering given by method :meth:`edge_iterator`.

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

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

EXAMPLES::

sage: G = graphs.PetersenGraph()
Expand Down Expand Up @@ -2085,6 +2092,28 @@ def incidence_matrix(self, oriented=None, sparse=True, vertices=None, edges=None
[1 1 0 0]
[0 0 0 1]

A different base ring::

sage: P5.incidence_matrix(base_ring=RDF)
[1.0 0.0 0.0 0.0]
[1.0 1.0 0.0 0.0]
[0.0 1.0 1.0 0.0]
[0.0 0.0 1.0 1.0]
[0.0 0.0 0.0 1.0]

Creating an immutable matrix::

sage: m = P5.incidence_matrix(immutable=True); m
[1 0 0 0]
[1 1 0 0]
[0 1 1 0]
[0 0 1 1]
[0 0 0 1]
sage: m[1,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::

sage: P5 = graphs.PathGraph(5)
Expand Down Expand Up @@ -2131,8 +2160,10 @@ def reorder(u, v):
raise ValueError("``edges`` must be a permutation of the edges")

from sage.matrix.constructor import matrix
from sage.rings.integer_ring import ZZ
m = matrix(ZZ, self.num_verts(), self.num_edges(), sparse=sparse)
if base_ring is None:
base_ring = ZZ
immutable = kwds.pop('immutable', False)
m = matrix(base_ring, self.num_verts(), self.num_edges(), sparse=sparse, **kwds)

if oriented:
for i, e in enumerate(edges):
Expand All @@ -2144,6 +2175,8 @@ def reorder(u, v):
m[verts[e[0]], i] += 1
m[verts[e[1]], i] += 1

if immutable:
m.set_immutable()
return m

def distance_matrix(self, vertices=None, **kwds):
Expand Down

0 comments on commit 6f2185a

Please sign in to comment.