Skip to content

Commit a331d78

Browse files
authored
Merge branch 'main' into remove-39-retworkx
2 parents 2adf9ad + feda690 commit a331d78

File tree

13 files changed

+608
-0
lines changed

13 files changed

+608
-0
lines changed

Cargo.lock

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ rustworkx-core = { path = "rustworkx-core", version = "=0.17.1" }
6666
flate2 = "1.0.35"
6767
pest = "=2.7.15"
6868
pest_derive = "=2.7.15"
69+
nalgebra-sparse = { version = "=0.10", features = ["io"] }
6970

7071
[dependencies.pyo3]
7172
version = "0.24"

docs/source/api/pydigraph_api_functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ the functions from the explicitly typed based on the data type.
6060
rustworkx.digraph_dijkstra_search
6161
rustworkx.digraph_node_link_json
6262
rustworkx.digraph_longest_simple_path
63+
rustworkx.digraph_write_matrix_market

docs/source/api/pygraph_api_functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ typed API based on the data type.
6161
rustworkx.graph_dijkstra_search
6262
rustworkx.graph_node_link_json
6363
rustworkx.graph_longest_simple_path
64+
rustworkx.graph_write_matrix_market

docs/source/api/serialization.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ Serialization
1111
rustworkx.write_graphml
1212
rustworkx.from_node_link_json_file
1313
rustworkx.parse_node_link_json
14+
rustworkx.write_matrix_market
15+
rustworkx.read_matrix_market
16+
rustworkx.read_matrix_market_file
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
features:
2+
- |
3+
Added support for reading and writing matrices in the Matrix Market (MM) format via the new
4+
:func:`~rustworkx.read_matrix_market`, :func:`~rustworkx.read_matrix_market_file`,
5+
and :func:`~rustworkx.write_matrix_market` functions.
6+
These functions enable importing and exporting sparse matrices in COO (Coordinate) format, either from
7+
a file or directly from an in-memory string.

rustworkx/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,30 @@ def write_graphml(graph, path, /, keys=None, compression=None):
23132313
raise TypeError(f"Invalid Input Type {type(graph)} for graph")
23142314

23152315

2316+
@_rustworkx_dispatch
2317+
def write_matrix_market(graph, /, path=None):
2318+
"""Write a graph to Matrix Market format.
2319+
2320+
Matrix Market is a human-readable ASCII file format for storing sparse matrices
2321+
as coordinate format (row, column, value) triplets. The graph is converted to a
2322+
coordinate (COO) sparse matrix representation where edges become non-zero entries.
2323+
2324+
For more information about Matrix Market format, see:
2325+
https://math.nist.gov/MatrixMarket/formats.html
2326+
2327+
:param graph: The graph to write in Matrix Market format. This can be a
2328+
:class:`~rustworkx.PyGraph` or :class:`~rustworkx.PyDiGraph`.
2329+
:param path: Optional file path to write the output. If not specified,
2330+
returns the Matrix Market content as a string.
2331+
:returns: None if a file path was provided, otherwise returns the Matrix Market
2332+
formatted content as a string.
2333+
:rtype: Optional[str]
2334+
:raises ValueError: when the graph cannot be converted to a valid COO matrix.
2335+
:raises IOError: when an error occurs during file I/O or format serialization.
2336+
"""
2337+
raise TypeError(f"Invalid Input Type {type(graph)} for graph")
2338+
2339+
23162340
@_rustworkx_dispatch
23172341
def bfs_layers(graph, sources=None):
23182342
"""Return the BFS layers of a graph as a list of lists.

rustworkx/__init__.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ from .rustworkx import digraph_node_link_json as digraph_node_link_json
171171
from .rustworkx import graph_node_link_json as graph_node_link_json
172172
from .rustworkx import from_node_link_json_file as from_node_link_json_file
173173
from .rustworkx import from_dot as from_dot
174+
from .rustworkx import graph_write_matrix_market as graph_write_matrix_market
175+
from .rustworkx import digraph_write_matrix_market as digraph_write_matrix_market
176+
from .rustworkx import read_matrix_market_file as read_matrix_market_file
177+
from .rustworkx import read_matrix_market as read_matrix_market
174178
from .rustworkx import parse_node_link_json as parse_node_link_json
175179
from .rustworkx import hyperbolic_greedy_routing as hyperbolic_greedy_routing
176180
from .rustworkx import hyperbolic_greedy_success_rate as hyperbolic_greedy_success_rate
@@ -678,6 +682,11 @@ def write_graphml(
678682
keys: list[GraphMLKey] | None = ...,
679683
compression: str | None = ...,
680684
) -> None: ...
685+
def write_matrix_market(
686+
graph: PyGraph | PyDiGraph,
687+
/,
688+
path: str | None = ...,
689+
) -> None: ...
681690
def bfs_layers(
682691
graph: PyGraph | PyDiGraph,
683692
sources: Sequence[int] | None = ...,

rustworkx/rustworkx.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,18 @@ def from_node_link_json_file(
754754
def from_dot(
755755
dot_str: str,
756756
) -> PyDiGraph[_S, _T] | PyGraph[_S, _T]: ...
757+
def read_matrix_market(contents: str) -> PyGraph | PyDiGraph: ...
758+
def read_matrix_market_file(path: str) -> PyGraph | PyDiGraph: ...
759+
def graph_write_matrix_market(
760+
graph: PyGraph,
761+
/,
762+
path: str | None = ...,
763+
) -> str | None: ...
764+
def digraph_write_matrix_market(
765+
graph: PyDiGraph,
766+
/,
767+
path: str | None = ...,
768+
) -> str | None: ...
757769

758770
# Geometry
759771

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod layout;
3131
mod line_graph;
3232
mod link_analysis;
3333
mod matching;
34+
mod matrix_market;
3435
mod planar;
3536
mod random_graph;
3637
mod score;
@@ -61,6 +62,7 @@ use link_analysis::*;
6162
use dot_parser::*;
6263
use geometry::*;
6364
use matching::*;
65+
use matrix_market::*;
6466
use planar::*;
6567
use random_graph::*;
6668
use shortest_path::*;
@@ -688,6 +690,10 @@ fn rustworkx(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
688690
m.add_wrapped(wrap_pyfunction!(pagerank))?;
689691
m.add_wrapped(wrap_pyfunction!(hits))?;
690692
m.add_wrapped(wrap_pyfunction!(from_dot))?;
693+
m.add_wrapped(wrap_pyfunction!(digraph_write_matrix_market))?;
694+
m.add_wrapped(wrap_pyfunction!(graph_write_matrix_market))?;
695+
m.add_wrapped(wrap_pyfunction!(read_matrix_market))?;
696+
m.add_wrapped(wrap_pyfunction!(read_matrix_market_file))?;
691697
m.add_class::<digraph::PyDiGraph>()?;
692698
m.add_class::<graph::PyGraph>()?;
693699
m.add_class::<toposort::TopologicalSorter>()?;

0 commit comments

Comments
 (0)