Skip to content

Commit

Permalink
Allow arbitrary (s, t) list for l_homology binding
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchaplin committed Mar 6, 2024
1 parent 87786a6 commit 2eea0f3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gramag"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

[dependencies]
Expand Down
6 changes: 6 additions & 0 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
print(ds.representatives)
print("")

print("Fixed l, custom (s, t) list - computed in parallel!")
ds2 = mg.l_homology(1, representatives=True, node_pairs=[(s, 5) for s in range(6)])
print(ds2.ranks)
print(ds2.representatives)
print("")

print("Errors:")
try:
mg.l_homology(12)
Expand Down
51 changes: 36 additions & 15 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,44 @@ impl MagGraph {
Ok(PyStlHomology(Arc::new(homology)))
}

// TODO: New method - allow arbitrary (s, t) list
fn l_homology(&self, l: usize, representatives: Option<bool>) -> Result<PyDirectSum, MagError> {
fn l_homology(
&self,
l: usize,
representatives: Option<bool>,
node_pairs: Option<Vec<(u32, u32)>>,
) -> Result<PyDirectSum, MagError> {
self.check_l(l)?;
let representatives = representatives.unwrap_or(false);
let stl_homologies: Vec<_> = self
.digraph
.node_identifiers()
.flat_map(|s| self.digraph.node_identifiers().map(move |t| (s, t)))
.par_bridge()
.map(|node_pair| {
(
(node_pair, l),
Arc::new(self.inner_compute_stl_homology(node_pair, l, representatives)),
)
})
.collect();
Ok(PyDirectSum(DirectSum::new(stl_homologies.into_iter())))
let compute_stl_homologies = |node_pairs: Box<
dyn Iterator<Item = (NodeIndex<u32>, NodeIndex<u32>)> + Send,
>| {
node_pairs
.par_bridge()
.map(|node_pair| {
(
(node_pair, l),
Arc::new(self.inner_compute_stl_homology(node_pair, l, representatives)),
)
})
.collect::<Vec<_>>()
.into_iter()
};

let stl_homologies = if let Some(u32_node_pairs) = node_pairs {
compute_stl_homologies(Box::new(
u32_node_pairs
.into_iter()
.map(|(s, t)| (NodeIndex::from(s), NodeIndex::from(t))),
))
} else {
compute_stl_homologies(Box::new(
self.digraph
.node_identifiers()
.flat_map(|s| self.digraph.node_identifiers().map(move |t| (s, t))),
))
};

Ok(PyDirectSum(DirectSum::new(stl_homologies)))
}
}

Expand Down

0 comments on commit 2eea0f3

Please sign in to comment.