use hypergraph_rs::linalg::{
adjacency_matrix, clique_motif_matrix, degree_matrix, incidence_matrix,
incidence_matrix_sparse, intersection_profile,
};
use hypergraph_rs::Hypergraph;
use ndarray::{array, Array1};
fn sample() -> Hypergraph {
let mut h: Hypergraph = Hypergraph::new();
for n in ["1", "2", "3", "4", "5", "6", "7"] {
h.add_node(n, serde_json::Value::Null);
}
h.add_edge(
vec!["1".into(), "2".into(), "3".into(), "7".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(vec!["4".into()], Some("e1".into()), serde_json::Value::Null)
.unwrap();
h.add_edge(
vec!["5".into(), "6".into(), "7".into()],
Some("e2".into()),
serde_json::Value::Null,
)
.unwrap();
h
}
#[test]
fn test_incidence_matrix_values_and_order() {
let h = sample();
let (i, rows, cols) = incidence_matrix(&h, None);
assert_eq!(rows, vec!["1", "2", "3", "4", "5", "6", "7"]);
assert_eq!(cols, vec!["e0", "e1", "e2"]);
let expected = array![
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
];
assert_eq!(i, expected);
}
#[test]
fn test_incidence_matrix_order_filter() {
let h = sample();
let (i, _rows, cols) = incidence_matrix(&h, Some(2));
assert_eq!(cols, vec!["e2"]);
assert_eq!(i.shape(), &[7, 1]);
assert_eq!(i.column(0).sum(), 3.0);
}
#[test]
fn test_incidence_matrix_empty() {
let h: Hypergraph = Hypergraph::new();
let (i, rows, cols) = incidence_matrix(&h, None);
assert_eq!(i.shape(), &[0, 0]);
assert!(rows.is_empty());
assert!(cols.is_empty());
}
#[test]
fn test_incidence_matrix_sparse_matches_dense() {
let h = sample();
let (dense, _, _) = incidence_matrix(&h, None);
let (sparse, _, _) = incidence_matrix_sparse(&h, None);
assert_eq!(sparse.to_dense(), dense);
}
#[test]
fn test_adjacency_binary() {
let h = sample();
let (a, rows) = adjacency_matrix(&h, None, 1, false);
assert_eq!(rows, vec!["1", "2", "3", "4", "5", "6", "7"]);
assert_eq!(a[[6, 0]], 1.0); assert_eq!(a[[6, 4]], 1.0); assert_eq!(a[[0, 0]], 0.0); assert_eq!(a[[3, 0]], 0.0); }
#[test]
fn test_adjacency_weighted_counts_shared_edges() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["a".into(), "b".into(), "c".into()],
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
let (a, rows) = adjacency_matrix(&h, None, 1, true);
let ia = rows.iter().position(|x| x == "a").unwrap();
let ib = rows.iter().position(|x| x == "b").unwrap();
assert_eq!(a[[ia, ib]], 2.0);
}
#[test]
fn test_adjacency_s_threshold() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["a".into(), "b".into(), "c".into()],
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
let (a, rows) = adjacency_matrix(&h, None, 2, false);
let ia = rows.iter().position(|x| x == "a").unwrap();
let ib = rows.iter().position(|x| x == "b").unwrap();
let ic = rows.iter().position(|x| x == "c").unwrap();
assert_eq!(a[[ia, ib]], 1.0);
assert_eq!(a[[ia, ic]], 0.0);
}
#[test]
fn test_adjacency_no_edges_returns_zeros_n_by_n() {
let mut h: Hypergraph = Hypergraph::new();
h.add_node("a", serde_json::Value::Null);
h.add_node("b", serde_json::Value::Null);
let (a, rows) = adjacency_matrix(&h, None, 1, false);
assert_eq!(a.shape(), &[2, 2]);
assert_eq!(rows, vec!["a", "b"]);
assert_eq!(a.sum(), 0.0);
}
#[test]
fn test_clique_motif_equals_weighted_adjacency() {
let h = sample();
let (w, _) = clique_motif_matrix(&h);
let (a, _) = adjacency_matrix(&h, None, 1, true);
assert_eq!(w, a);
}
#[test]
fn test_intersection_profile_diagonal_is_edge_size() {
let h = sample();
let (p, edge_rows) = intersection_profile(&h, None);
assert_eq!(edge_rows, vec!["e0", "e1", "e2"]);
assert_eq!(p[[0, 0]], 4.0); assert_eq!(p[[1, 1]], 1.0); assert_eq!(p[[2, 2]], 3.0); assert_eq!(p[[0, 2]], 1.0);
assert_eq!(p[[0, 1]], 0.0);
}
#[test]
fn test_degree_matrix_counts_memberships() {
let h = sample();
let (k, node_rows) = degree_matrix(&h, None);
assert_eq!(node_rows, vec!["1", "2", "3", "4", "5", "6", "7"]);
let expected: Array1<f64> = Array1::from(vec![1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0]);
assert_eq!(k, expected); }
#[test]
fn test_degree_matrix_empty_is_zeros_over_nodes() {
let mut h: Hypergraph = Hypergraph::new();
h.add_node("a", serde_json::Value::Null);
h.add_node("b", serde_json::Value::Null);
let (k, rows) = degree_matrix(&h, None);
assert_eq!(k, Array1::from(vec![0.0, 0.0]));
assert_eq!(rows, vec!["a", "b"]);
}
use hypergraph_rs::linalg::LinalgError;
use hypergraph_rs::linalg::{laplacian, multiorder_laplacian, normalized_hypergraph_laplacian};
fn approx(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-9
}
#[test]
fn test_laplacian_order1_is_graph_laplacian() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["b".into(), "c".into()],
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["a".into(), "c".into()],
Some("e2".into()),
serde_json::Value::Null,
)
.unwrap();
let (l, rows) = laplacian(&h, 1, false);
assert_eq!(rows, vec!["a", "b", "c"]);
assert!(approx(l[[0, 0]], 2.0));
assert!(approx(l[[0, 1]], -1.0));
for i in 0..3 {
assert!(approx(l.row(i).sum(), 0.0));
}
}
#[test]
fn test_laplacian_empty() {
let h: Hypergraph = Hypergraph::new();
let (l, rows) = laplacian(&h, 1, false);
assert_eq!(l.shape(), &[0, 0]);
assert!(rows.is_empty());
}
#[test]
fn test_multiorder_laplacian_shape() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["a".into(), "b".into(), "c".into()],
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
let (l, rows) = multiorder_laplacian(&h, &[1, 2], &[1.0, 1.0], false);
assert_eq!(rows.len(), 3);
assert_eq!(l.shape(), &[3, 3]);
for i in 0..3 {
assert!(approx(l.row(i).sum(), 0.0));
}
}
#[test]
fn test_normalized_laplacian_isolated_node_errors() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_node("isolated", serde_json::Value::Null);
let result = normalized_hypergraph_laplacian(&h, false);
assert_eq!(result, Err(LinalgError::IsolatedNodes));
}
#[test]
fn test_normalized_laplacian_ok() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
vec!["b".into(), "c".into()],
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
let (l, rows) = normalized_hypergraph_laplacian(&h, false).unwrap();
assert_eq!(rows, vec!["a", "b", "c"]);
assert_eq!(l.shape(), &[3, 3]);
}
#[test]
fn test_normalized_laplacian_empty_edge_no_nan_or_inf() {
let mut h: Hypergraph = Hypergraph::new();
h.add_edge(
vec!["a".into(), "b".into()],
Some("e0".into()),
serde_json::Value::Null,
)
.unwrap();
h.add_edge(
Vec::<String>::new(),
Some("e1".into()),
serde_json::Value::Null,
)
.unwrap();
let (l, rows) = normalized_hypergraph_laplacian(&h, false).unwrap();
assert_eq!(rows, vec!["a", "b"]);
for x in l.iter() {
assert!(x.is_finite(), "expected finite entry, got {x}");
}
}