Yog.IO.MatrixMarket (YogEx v0.70.0)

Copy Markdown View Source

Matrix Market (.mtx) format serialization support.

Provides functions to serialize and deserialize graphs in the Matrix Market exchange format, a common format for sharing sparse and dense matrices in scientific computing and graph theory.

Format Overview

Matrix Market files start with a header: %%MatrixMarket matrix <format> <field> <symmetry>

  • Format: coordinate (sparse) or array (dense) - Currently coordinate is primary.
  • Field: real, integer, pattern, or complex.
  • Symmetry: general, symmetric, skew-symmetric, or hermitian.

For graph representation:

  • Rows and Columns represent nodes.
  • Matrix indices are 1-based.
  • Symmetric matrices are often used for undirected graphs.

Example

iex> graph = Yog.directed()
...> |> Yog.add_node(1, nil)
...> |> Yog.add_node(2, nil)
...> |> Yog.add_edge!(from: 1, to: 2, with: 5.0)
iex>
iex> mtx_string = Yog.IO.MatrixMarket.serialize(graph)
iex> String.contains?(mtx_string, "%%MatrixMarket matrix coordinate real general")
true
iex> String.contains?(mtx_string, "1 2 5.0")
true

Default Configurations

Default behavior:

  • Node data: Ignored during serialization (Matrix Market only stores matrix values)
  • Edge weights: Converted to float or integer

Summary

Functions

Returns default Matrix Market serialization options.

Creates Matrix Market options with custom configurations.

Parses a Matrix Market string into a graph.

Parses a Matrix Market string with custom parsers.

Reads a graph from a Matrix Market file.

Reads a graph from a Matrix Market file with custom parsers.

Serializes a graph to Matrix Market coordinate format.

Serializes a graph to Matrix Market format with custom options.

Writes a graph to a Matrix Market file.

Writes a graph to a Matrix Market file with custom options.

Functions

default_options()

Returns default Matrix Market serialization options.

  • edge_weight: Function to convert edge data to a number.

options_with(edge_weight)

Creates Matrix Market options with custom configurations.

parse(input, g_type \\ nil)

Parses a Matrix Market string into a graph.

Time Complexity: O(V + E)

Parameters

  • input: Matrix Market format string
  • g_type: :directed or :undirected (overrides symmetry if needed, but usually respects it)

Returns

  • {:ok, {:matrix_market_result, graph, warnings}} on success
  • {:error, reason} on parsing failure

parse_with(input, g_type, node_parser, edge_parser)

Parses a Matrix Market string with custom parsers.

read(path, g_type \\ nil)

Reads a graph from a Matrix Market file.

read_with(path, g_type, node_parser, edge_parser)

Reads a graph from a Matrix Market file with custom parsers.

serialize(graph)

Serializes a graph to Matrix Market coordinate format.

Time Complexity: O(V + E)

Example

iex> graph = Yog.directed()
...> |> Yog.add_node(1, nil)
...> |> Yog.add_node(2, nil)
...> |> Yog.add_edge!(from: 1, to: 2, with: 1.5)
iex> mtx = Yog.IO.MatrixMarket.serialize(graph)
iex> String.contains?(mtx, "1 2 1.5")
true

serialize_with(options, graph)

Serializes a graph to Matrix Market format with custom options.

write(path, graph)

Writes a graph to a Matrix Market file.

write_with(path, options, graph)

Writes a graph to a Matrix Market file with custom options.