Skip to content

Homology & Chain Complexes (algebrax.topology.homology)

algebrax.topology.homology (re-exported at the top level of algebrax.topology) formalizes sequence spaces \(C_k(K; R)\) and sparse boundary operators \(\partial_k: C_k \to C_{k-1}\) over abstract simplicial complexes and general chain complexes.


Theoretical Foundations

  1. Boundary Operator (\(\partial_k\)): Maps each oriented \(k\)-simplex \([v_0, \dots, v_k]\) to the alternating sum of its \((k-1)\)-faces: $\(\partial_k([v_0, \dots, v_k]) = \sum_{i=0}^k (-1)^i [v_0, \dots, \hat{v}_i, \dots, v_k]\)$
  2. Homological Nilpotency: The boundary of a boundary is identically zero: $\(\partial_{k-1} \circ \partial_k = \mathbf{0}\)$
  3. Betti Numbers (\(\beta_k\)): The rank of the \(k\)-th homology group \(H_k(K) = \ker(\partial_k) / \operatorname{im}(\partial_{k+1})\) counts \(k\)-dimensional topological holes:
  4. \(\beta_0\): Connected components
  5. \(\beta_1\): 1D circular loops / tunnels
  6. \(\beta_2\): 2D enclosed voids / cavities $\(\beta_k = \dim(\ker \partial_k) - \operatorname{rank}(\partial_{k+1})\)$

Simplicial Complexes (SimplicialComplex)

SimplicialComplex automatically enforces downward face closure (\(\sigma \in K, \tau \subseteq \sigma \implies \tau \in K\)), canonicalizes vertex ordering, rejects degenerate repeated-vertex simplices (such as (0, 0)) with ValueError, lazily builds exact integer-signed (+1/-1) sparse boundary matrices \(\partial_k\) on first access, and computes Betti numbers over \(\mathbb{R}\), \(\mathbb{Q}\), or any finite field \(\mathbb{F}_p\):

from fractions import Fraction
import algebrax as ax
from algebrax.algebra import ModularField, StandardField

# 1. 1D Hollow Circle Topology (S^1)
ring_edges = [(0, 1), (1, 2), (2, 3), (0, 3)]
sc_ring = ax.topology.SimplicialComplex(ring_edges)

# Verify homological nilpotency: d_0 o d_1 == 0
assert sc_ring.verify_nilpotency(k=1)

# Compute Betti numbers
betti_ring = sc_ring.betti_numbers(max_k=1)
print(f'1D Ring Betti Numbers: beta_0={betti_ring[0]}, beta_1={betti_ring[1]}')
# Output: 1D Ring Betti Numbers: beta_0=1, beta_1=1

# 2. 3D Solid Tetrahedron Topology (contractible)
sc_tet = ax.topology.SimplicialComplex([(0, 1, 2, 3)])
betti_tet = sc_tet.betti_numbers(max_k=2)
print(f'Solid Tetrahedron Betti Numbers: {betti_tet}')
# Output: {0: 1, 1: 0, 2: 0}

# 3. Detecting Z/2Z Torsion on the Real Projective Plane RP^2
rp2_triangles = [
    (0, 1, 2), (0, 2, 3), (0, 3, 4), (0, 4, 5), (0, 5, 1),
    (1, 2, 4), (2, 3, 5), (3, 4, 1), (4, 5, 2), (5, 1, 3),
]
sc_rp2 = ax.topology.SimplicialComplex(rp2_triangles)
assert sc_rp2.betti_numbers(max_k=2, field=ModularField(2)) == {0: 1, 1: 1, 2: 1}
assert sc_rp2.betti_numbers(max_k=2, field=StandardField(Fraction)) == {0: 1, 1: 0, 2: 0}

General Sparse Chain Complexes (SparseChainComplex)

When working with non-simplicial cell complexes (such as cubical complexes, CW complexes, or algebraic chain complexes), construct SparseChainComplex directly from a dictionary of boundary matrices {k: D_k}:

from algebrax.topology import SparseChainComplex

# Boundary matrices for a 1D cycle with 3 vertices and 3 edges
d1 = {
    (0,): {(0, 1): -1.0, (0, 2): -1.0},
    (1,): {(0, 1): 1.0, (1, 2): -1.0},
    (2,): {(0, 2): 1.0, (1, 2): 1.0},
}
cc = SparseChainComplex({1: d1})
assert cc.verify_nilpotency(k=1)
print('Betti numbers:', cc.betti_numbers(max_k=1))
# Output: {0: 1, 1: 1}