Modules

Chain complexes and homology

Homology turns a space built from cells into a list of abelian groups. Once the cells and their boundaries are written down, everything is integer linear algebra: the Smith normal form of each boundary matrix gives the free part and the torsion.

Chain complexes

A cell complex has nkn_k cells in dimension kk. The chain group Ck=ZnkC_k = \mathbb Z^{n_k} has one generator per kk-cell, and the boundary map ∂k:Ck→Ck−1\partial_k : C_k \to C_{k-1} sends a cell to the signed sum of the cells on its boundary. The boundary of a boundary is empty, so ∂k−1∂k=0\partial_{k-1}\partial_k = 0, and the cycles ker⁡∂k\ker \partial_k contain the boundaries im⁡∂k+1\operatorname{im}\partial_{k+1}. The homology is the quotient

Hk=ker⁡∂k / im⁡∂k+1. H_k = \ker \partial_k \,/\, \operatorname{im} \partial_{k+1}.

chainComplex takes the list [∂1,∂2,… ][\partial_1, \partial_2, \dots]. Each ∂k\partial_k has one column per kk-cell holding its boundary, and one row per (k−1)(k-1)-cell. It checks ∂∂=0\partial\partial = 0 and refuses a list that is not a chain complex.

import { chainComplex } from "@ratmath/structures";

// The torus: one vertex v, edges a, b, one face glued along a b a⁻¹ b⁻¹.
// ∂a = v − v = 0, ∂b = 0, ∂(face) = a + b − a − b = 0.
const T = chainComplex([[[0, 0]], [[0], [0]]]);
String(T);                 // "H0 = Z, H1 = Z^2, H2 = Z"
T.eulerCharacteristic;     // 0 = 1 − 2 + 1

// The projective plane: face glued along a a, so ∂(face) = 2a.
const P = chainComplex([[[0]], [[2]]]);
String(P);                 // "H0 = Z, H1 = Z/2, H2 = 0"

Homology from the Smith normal form

Invertible integer row and column operations bring ∂k\partial_k to the diagonal form diag⁡(d1,…,drk,0,… )\operatorname{diag}(d_1, \dots, d_{r_k}, 0, \dots) with d1∣d2∣⋯d_1 \mid d_2 \mid \cdots (see abelian groups). Then ker⁡∂k\ker\partial_k is free of rank nk−rkn_k - r_k, and inside it im⁡∂k+1\operatorname{im}\partial_{k+1} is spanned by dieid_i e_i for the invariant factors of ∂k+1\partial_{k+1}. So

Hk≅Z nk−rk−rk+1⊕⨁di>1Z/di, H_k \cong \mathbb Z^{\,n_k - r_k - r_{k+1}} \oplus \bigoplus_{d_i > 1} \mathbb Z/d_i,

where the did_i are those of ∂k+1\partial_{k+1}. For the Klein bottle, glued along aba−1ba b a^{-1} b, ∂2=2b\partial_2 = 2b and H1=Z⊕Z/2H_1 = \mathbb Z \oplus \mathbb Z/2.

const K = chainComplex([[[0, 0]], [[0], [2]]]);
String(K.homology(1));          // "Z/2 × Z"
chainComplex([[[0, 0]], [[2, 6], [4, 8]]]).homology(1).toString();   // "Z/2 × Z/4": Smith form diag(2, 4)

Betti numbers, χ and other coefficients

The Betti number bkb_k is the rank of HkH_k, the number of copies of Z\mathbb Z. Over a field the same recipe gives a vector space: over Q\mathbb Q its dimension is bkb_k, and over Z/p\mathbb Z/p the ranks are counted mod pp, so torsion divisible by pp shows up twice (the universal coefficient theorem: dim⁡Hk(Z/p)=bk+tk+tk−1\dim H_k(\mathbb Z/p) = b_k + t_k + t_{k-1}, with tkt_k the number of invariant factors of HkH_k divisible by pp). In every case the alternating sum of Betti numbers is the Euler characteristic χ=∑(−1)knk\chi = \sum (-1)^k n_k, which needs no homology at all.

P.betti();                              // [1, 0, 0]
P.betti({ coefficients: "Z/2" });       // [1, 1, 1]: the Z/2 in H1 is counted in H1 and in H2
String(P.homology(2, { coefficients: "Z/2" }));   // "Z/2"
P.eulerCharacteristic;                  // 1

Glued polygons, simplicial complexes and their homology are in @ratmath/geo, which builds these boundary matrices for you.

Try itEdit the code and run it with @ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs