yog_io/matrix

Adjacency Matrix format support.

This module provides functions to convert between graphs and adjacency matrices. Adjacency matrices are dense representations where matrix[i][j] represents the edge from node i to node j.

Format Overview

The file representation is a space-separated text file where each row corresponds to a row in the matrix.

Example

0.0 1.0 0.5
1.0 0.0 0.0
0.0 0.0 0.0

Usage

import yog/model.{Undirected}
import yog_io/matrix as matrix_io

pub fn main() {
  let assert Ok(graph) = matrix_io.read("graph.mat", Undirected)
}

Types

Errors that can occur during Matrix operations

pub type MatrixError {
  NotSquare(rows: Int, cols: Int)
  ReadError(path: String, error: String)
  WriteError(path: String, error: String)
}

Constructors

  • NotSquare(rows: Int, cols: Int)

    Matrix is not square (n x n)

  • ReadError(path: String, error: String)

    File not found or couldn’t be read

  • WriteError(path: String, error: String)

    File couldn’t be written

Values

pub fn from_matrix(
  graph_type: model.GraphType,
  matrix: List(List(Float)),
) -> Result(model.Graph(Nil, Float), MatrixError)

Creates a graph from an adjacency matrix.

An adjacency matrix is a square matrix where matrix[i][j] represents the weight of the edge from node i to node j. A value of 0.0 indicates no edge.

pub fn from_string(
  content: String,
) -> Result(List(List(Float)), MatrixError)

Parses an adjacency matrix from a string.

pub fn read(
  path: String,
  graph_type: model.GraphType,
) -> Result(model.Graph(Nil, Float), MatrixError)

Reads an adjacency matrix from a file.

pub fn serialize(matrix: List(List(Float))) -> String

Converts a matrix to a space-separated string.

pub fn to_matrix(
  graph: model.Graph(n, Float),
) -> #(List(Int), List(List(Float)))

Exports a graph to an adjacency matrix representation.

Returns a tuple #(nodes, matrix) where:

  • nodes is a list of node IDs in the order they appear in the matrix
  • matrix is the adjacency matrix (list of lists of weights)
pub fn write(
  path: String,
  graph: model.Graph(n, Float),
) -> Result(Nil, MatrixError)

Writes an adjacency matrix to a file.

Search Document