Skip to main content

hypergraph_rs/linalg/
mod.rs

1//! Matrices derived from the bipartite hypergraph substrate.
2//!
3//! Row order follows [`Hypergraph::node_ids`] (node insertion order); column
4//! order follows [`Hypergraph::edge_ids`] (edge insertion order). This is the
5//! III.7 determinism contract and matches XGI's dict-ordering
6//! (`xgi/linalg/hypergraph_matrix.py`).
7
8mod hypergraph_matrix;
9mod laplacian;
10
11pub use hypergraph_matrix::{
12    adjacency_matrix, clique_motif_matrix, degree_matrix, incidence_matrix,
13    incidence_matrix_sparse, intersection_profile,
14};
15pub use laplacian::{laplacian, multiorder_laplacian, normalized_hypergraph_laplacian};
16
17use crate::Hypergraph;
18use thiserror::Error;
19
20/// Errors raised by linear-algebra operators that have preconditions.
21#[derive(Debug, Clone, Error, PartialEq)]
22pub enum LinalgError {
23    /// The normalized Laplacian is undefined when isolated nodes exist
24    /// (division by a zero degree). XGI parity: `laplacian_matrix.py:222-225`.
25    #[error("every node must be a member of an edge to avoid divide-by-zero")]
26    IsolatedNodes,
27}
28
29/// Edge IDs kept after an optional order filter, in edge insertion order.
30///
31/// `order = Some(d)` keeps edges whose membership count equals `d + 1`
32/// (edge order = size − 1). `order = None` keeps all edges.
33pub(crate) fn filtered_edge_ids<N, E, M>(
34    h: &Hypergraph<N, E, M>,
35    order: Option<usize>,
36) -> Vec<String> {
37    match order {
38        None => h.edge_ids(),
39        Some(d) => h
40            .edge_ids()
41            .into_iter()
42            .filter(|e| h.members(e).map(|m| m.len()) == Some(d + 1))
43            .collect(),
44    }
45}