Skip to content

Commit

Permalink
Some performance improvements
Browse files Browse the repository at this point in the history
- Don't compute simplex tree unnecessary.
- The `Hashable` test is unnecessary, non-hashable elements will cause the method to fail anyway.
- Optimize boundary computation of simplices.
  • Loading branch information
ffl096 committed Sep 15, 2023
1 parent 3f0cce9 commit c38136b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
14 changes: 5 additions & 9 deletions toponetx/classes/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ def __init__(
construct_tree: bool = True,
**attr,
) -> None:
for i in elements:
if not isinstance(i, Hashable):
raise ValueError(f"All nodes of a simplex must be hashable, got {i}")

if len(elements) != len(set(elements)):
raise ValueError("A simplex cannot contain duplicate nodes.")

Expand Down Expand Up @@ -113,9 +109,7 @@ def construct_simplex_tree(
faceset = set()
for r in range(len(elements), 0, -1):
for face in combinations(elements, r):
faceset.add(
Simplex(elements=sorted(face), construct_tree=False)
) # any face is always ordered
faceset.add(Simplex(elements=face, construct_tree=False))
return frozenset(faceset)

@property
Expand All @@ -124,8 +118,10 @@ def boundary(self) -> frozenset["Simplex[ElementType]"]:
if self.construct_tree:
return frozenset(i for i in self._faces if len(i) == len(self) - 1)
else:
faces = Simplex.construct_simplex_tree(self.elements)
return frozenset(i for i in faces if len(i) == len(self) - 1)
return frozenset(
Simplex(face, construct_tree=False)
for face in combinations(self.elements, len(self) - 1)
)

def sign(self, face: "Simplex[ElementType]") -> int:
"""Calculate the sign of the simplex with respect to a given face.
Expand Down
2 changes: 1 addition & 1 deletion toponetx/classes/simplex_trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def simplex(self) -> Simplex[ElementType] | None:
"""Return a `Simplex` object representing this node."""
if self.label is None:
return None
simplex = Simplex(self.elements)
simplex = Simplex(self.elements, construct_tree=False)
simplex._properties = self.attributes
return simplex

Expand Down

0 comments on commit c38136b

Please sign in to comment.