Discrete Calculus & Spectral Graph Theory (algebrax.analysis)¶
algebrax.analysis provides discrete exterior calculus operators, spectral graph analysis, and network dynamics:
- Graph Laplacian Linear Operator (
laplacian_matrix): Constructs combinatorial (\(L = D - W\)), symmetric normalized (\(L_{\mathrm{sym}} = D^{-1/2} L D^{-1/2}\)), and random-walk normalized (\(L_{\mathrm{rw}} = D^{-1} L\)) operators. - Fiedler Vector & Algebraic Connectivity (
fiedler_vector,algebraic_connectivity): Computes the second smallest Laplacian eigenvalue \(\lambda_2\) and eigenvector \(\mathbf{v}_2\) via Rayleigh Quotient Conjugate Gradient (RQ-CG). - Spectral Graph Bipartitioning (
spectral_bipartition): Dissects networks along structural Cheeger cutlines using sign (\(\theta=0\)) or balanced median (\(\theta = \text{median} (\mathbf{v}_2)\)) thresholds. - Laplacian Smoothing & Heat Diffusion (
laplacian_smoothing): Minimizes discrete Dirichlet energy \(E (\mathbf{u}) = \frac{1}{2} \mathbf{u}^T L \mathbf{u}\) via \(S\) Euler diffusion contractions \(\mathbf{u}^{ (t+1)} = (I - \tau L) \mathbf{u}^{ (t)}\). - Full Eigenspectrum (
laplacian_spectrum): Diagonalizes small-to-medium network Laplacians (\(N \le 150\)) via cyclic Jacobi sweeps. - Forman-Ricci Curvature (
forman_ricci_curvature): Evaluates geometric Ricci curvature on network edges. - Coboundary Gradient (
gradient): Discrete exterior derivative \(d_0 f (i, j) = f (j) - f (i)\). - Discrete Divergence (
divergence): Adjoint boundary codifferential measuring net nodal flux. - Field Laplacian Operator (
laplacian): Direct evaluation of the 0-form field action \(\Delta f = \text{div} (\text{grad } f)\). - Algebraic PageRank (
pagerank): Solves for stationary random walk distributions with teleportation restart. - Eigenvector Centrality (
eigen_centrality): Computes the principal eigenvector \(\lambda_1 \mathbf{x} = A \mathbf{x}\) of the adjacency matrix via power iteration for hub ranking without restart damping.
Spectral Graph Theory & Clustering¶
1. Constructing the Graph Laplacian¶
import algebrax as ax
# Barbell network: two K3 cliques connected by a bridge edge (2 <-> 3)
graph = {
0: {1: 1.0, 2: 1.0},
1: {0: 1.0, 2: 1.0},
2: {0: 1.0, 1: 1.0, 3: 1.0},
3: {2: 1.0, 4: 1.0, 5: 1.0},
4: {3: 1.0, 5: 1.0},
5: {3: 1.0, 4: 1.0},
}
# Combinatorial Laplacian L = D - W
L = ax.matrix.laplacian_matrix(graph)
# Symmetric normalized Laplacian L_sym = D^{-1/2} L D^{-1/2}
L_sym = ax.matrix.laplacian_matrix(graph, normalized='sym')
2. Algebraic Connectivity & Cheeger Bipartitioning¶
The Fiedler eigenvalue \(\lambda_2\) quantifies how easily a network can be partitioned into disconnected components. Cheeger's inequality bounds the optimal conductance \(h (G)\): $\(\frac{\lambda_2}{2} \le h (G) \le \sqrt{2 \lambda_2}\)$
# Compute Fiedler eigenvalue and vector
lambda_2, fiedler = ax.analysis.fiedler_vector(graph)
print(f'Algebraic Connectivity lambda_2: {lambda_2:.4f}')
# Optimal spectral bipartition
cluster_a, cluster_b, metrics = ax.analysis.spectral_bipartition(graph, method='sign')
print('Cluster A:', cluster_a)
print('Cluster B:', cluster_b)
print('Cut Quality Metrics:', metrics)
# Output:
# Cluster A: {0, 1, 2}, Cluster B: {3, 4, 5}
# cut_size: 1.0, conductance: 0.1428, ratio_cut: 0.6667
3. Laplacian Smoothing (Heat Diffusion on Signals)¶
Laplacian smoothing filters spatial high-frequency noise from node signals while preserving global topological features:
# Signal with localized noise
noisy_signal = {0: 10.0, 1: 0.0, 2: 0.0, 3: 10.0, 4: 0.0, 5: 0.0}
# 10 Euler diffusion steps with rate tau=0.1 (pass adjacency graph by default)
smoothed_signal = ax.analysis.laplacian_smoothing(noisy_signal, graph, steps=10, tau=0.1)
print('Smoothed Field:', smoothed_signal)
# Or pass a precomputed Laplacian explicitly with is_laplacian=True
smoothed_from_L = ax.analysis.laplacian_smoothing(noisy_signal, L, steps=10, tau=0.1, is_laplacian=True)
Explicit is_laplacian Flag
laplacian_smoothing(signal, graph, ..., is_laplacian=False) treats graph as an adjacency matrix by default and
computes \(L = D - W\) internally. Pass is_laplacian=True when supplying a precomputed combinatorial or normalized
Laplacian (this avoids fragile sign heuristics on signed adjacency matrices).
4. Principal Eigenvector Centrality¶
Computes the dominant eigenvector \(\lambda_1 \mathbf{x} = \mathbf{A} \mathbf{x}\) of the adjacency matrix via power iteration without damping, ideal for ranking nodes in symmetric graphs and cross-asset correlation matrices:
network = {
'Alice': {'Bob': 1.0, 'Charlie': 1.0},
'Bob': {'Alice': 1.0, 'David': 1.0},
'Charlie': {'Alice': 1.0},
'David': {'Bob': 1.0},
}
centrality = ax.analysis.eigen_centrality(network)
print('Eigenvector Centrality:', centrality)
4b. Semiring-Parameterized Algebraic PageRank (pagerank)¶
pagerank propagates semiring.add, semiring.mul, and semiring.zero consistently across transition steps, dangling mass redistribution, damping, and teleportation blending. It accepts semiring instances, classes, or catalog strings (StandardSemiring, ViterbiSemiring, BottleneckSemiring) and validates real/numeric carriers upfront (raising TypeError for non-numeric semirings like StringSemiring or BooleanSemiring):
from algebrax.analysis import pagerank
from algebrax.semiring import BottleneckSemiring, StandardSemiring, ViterbiSemiring
directed = {'A': {'B': 1.0, 'C': 1.0}, 'B': {'C': 1.0}, 'C': {'A': 1.0}}
pr_std = pagerank(directed, semiring=StandardSemiring(float))
pr_vit = pagerank(directed, semiring=ViterbiSemiring())
pr_bot = pagerank(directed, semiring=BottleneckSemiring())
5. Ramanujan Expander Graphs & Discrete Hecke Operators¶
A connected \(d\)-regular network is an optimal Ramanujan expander graph if every non-trivial adjacency eigenvalue beyond the single trivial \(+d\) eigenvalue (and at most one \(-d\) eigenvalue if bipartite) satisfies the Alon–Boppana / Hasse–Weil bound \(|\lambda_i| \le 2\sqrt{d - 1}\). Disconnected \(d\)-regular graphs carry repeated \(\lambda = +d\) eigenvalues and are strictly rejected (is_ramanujan = False). Moreover, hecke_graph_operator(adjacency, k) generates the commutative algebra of non-backtracking walk operators \(T_k\):
from algebrax.analysis import hecke_graph_operator, is_ramanujan_graph
from algebrax.verification import verify_ramanujan_spectral_gap
# Complete graph K_4 is 3-regular with spectrum {3, -1, -1, -1} (1 <= 2*sqrt(2) ≈ 2.828)
k4 = {i: {j: 1.0 for j in range(4) if j != i} for i in range(4)}
is_ram, max_nontrivial, threshold = is_ramanujan_graph(k4, degree=3)
assert is_ram is True and max_nontrivial <= threshold
assert all(verify_ramanujan_spectral_gap(k4, degree=3).values())
# T_2 = A^2 - 3*I counts non-backtracking walks of length 2 (sparse zeros pruned)
t2 = hecke_graph_operator(k4, 2, degree=3)
assert t2[0][1] == 2.0 and t2[0].get(0, 0.0) == 0.0
Sparse Zero-Omission & Operator Parity
gradient(field, graph) and laplacian(field, graph) respect implicit sparse zeros across all edges incident to
the support of field (matching laplacian_matrix symmetrization and self-loop exclusion), guaranteeing exact
operator identity laplacian(f, G) == mat_vec(laplacian_matrix(G), f) even for single-vertex impulses such as
{0: 1.0}. Similarly, fiedler_vector symmetrizes upper-triangular adjacency graphs before computing degrees and
normalized Laplacians.
Discrete Exterior Calculus & Curvature¶
# Compute Forman-Ricci edge curvature to detect highway choke points
curvature = ax.analysis.forman_ricci_curvature(graph)
print('Edge Curvatures:', curvature)
# Discrete scalar field diffusion flux (divergence of gradient)
states = {0: 10.0, 1: 20.0, 2: 30.0, 3: 40.0, 4: 20.0, 5: 10.0}
flux = ax.analysis.laplacian(states, graph)
print('Laplacian Diffusion Flux:', flux)
# Bridge nested edge flows (SparseMatrix) and simplicial 1-cochains (Cochain)
grad_flow = ax.analysis.gradient(states, graph)
one_form = ax.analysis.Cochain.from_flow(grad_flow)
print('Flow 0 -> 1:', one_form[(0, 1)], 'Flow 1 -> 0:', one_form[(1, 0)])
reconstructed_flow = one_form.to_flow(skew_symmetric=True)
Helmholtz–Hodge Flow Decomposition (\(\omega = d\alpha + \delta\beta + \gamma\))¶
Every edge flow \(\omega \in C^1(K; \mathbb{R})\) on a simplicial complex \(K\) decomposes into three mutually orthogonal components: 1. Exact (\(d_0 \alpha\)): Curl-free flow driven by scalar vertex potentials \(\alpha\). 2. Coexact (\(\delta_2 \beta\)): Divergence-free local rotational eddies circulating around filled 2-simplices (triangles). 3. Harmonic (\(\gamma\)): Global topological circulation wrapping around 1D holes (\(\ker \Delta_1 \cong H^1(K; \mathbb{R})\)).
from algebrax.analysis import helmholtz_hodge_decomposition
from algebrax.topology import SimplicialComplex
# Complex with a filled triangle (0, 1, 2) and a hollow loop (1, 2)-(2, 3)-(1, 3)
sc = SimplicialComplex([(0, 1, 2), (1, 3), (2, 3)])
flow = {(0, 1): 2.5, (1, 2): 3.5, (2, 0): 1.5, (2, 3): 2.0, (3, 1): 2.0}
decomp = helmholtz_hodge_decomposition(sc, flow)
print('Exact (gradient) flow:', decomp.exact)
print('Coexact (triangular curl) flow:', decomp.coexact)
print('Harmonic (topological cycle) flow:', decomp.harmonic)
Related Recipes & Applications¶
- Spectral Graph Clustering & Manifold Smoothing — Community discovery and heat diffusion smoothing on collaboration networks.
- Urban Traffic Resilience — Highway choke-point detection via
forman_ricci_curvature. - Sheaf Cohomology Consensus — Multi-robot convergence via
laplaciandiffusion.