8000 ENH: Add format parameter to adjacency_matrix by toroleapinc · Pull Request #8545 · networkx/networkx · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add format parameter to adjacency_matrix#8545

Open
toroleapinc wants to merge 1 commit intonetworkx:mainfrom
toroleapinc:feat/issue-7980-adjacency-matrix-format
Open

ENH: Add format parameter to adjacency_matrix#8545
toroleapinc wants to merge 1 commit intonetworkx:mainfrom
toroleapinc:feat/issue-7980-adjacency-matrix-format

Conversation

@toroleapinc
Copy link

Summary

Add a format parameter to adjacency_matrix() to allow specifying the sparse format of the returned matrix (or 'dense' for a numpy ndarray), consistent with to_scipy_sparse_array().

Partial fix for #7980.

Changes

adjacency_matrix() is a thin wrapper around to_scipy_sparse_array() but previously didn't expose the format parameter. This change simply passes it through.

Example

>>> G = nx.path_graph(4)
>>> nx.adjacency_matrix(G, format='dense')
array([[0, 1, 0, 0],
       [1, 0, 1, 0],
       [0, 1, 0, 1],
       [0, 0, 1, 0]])
>>> nx.adjacency_matrix(G, format='coo')
<COOrdinate sparse array of dtype 'int64'...>

Add a 'format' parameter to adjacency_matrix() to allow specifying
the sparse format of the returned matrix, consistent with
to_scipy_sparse_array(). This includes support for 'dense' format
which returns a numpy ndarray.

The adjacency_matrix function is a thin wrapper around
to_scipy_sparse_array but previously didn't expose the format
parameter. This change simply passes it through.

Closes networkx#7980 (partial: adjacency_matrix only)

Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Copy link
Contributor
@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kwarg should be made keyword-only and a test is needed.

rossbar

This comment was marked as duplicate.

rossbar

This comment was marked as duplicate.

@toroleapinc
Copy link
Author

@rossbar Thanks for the feedback! You're absolutely right about both points:

  1. Keyword-only argument: I'll update the function signature to make the parameter keyword-only
  2. Tests needed: I'll add comprehensive tests covering both strict and non-strict modes

I'll implement these changes and push an update. Thanks for the clear guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants

0