Skip to content

Core Sparse Matrix Operations

The algebrax.matrix.core module provides fundamental primitives for manipulating sparse dictionary matrices (SparseMatrix[K, V] = dict[K, dict[K, V]]) over numerical fields and general semirings.


Overview of Core Primitives

Function Signature / Operation Mathematical Meaning
add(m1, m2, semiring=...) \(M_1 \oplus M_2\) Element-wise matrix addition over standard arithmetic or any Semiring with identity pruning.
subtract(m1, m2) \(M_1 - M_2\) Element-wise matrix subtraction with zero-pruning.
dot(m1, m2, semiring=...) \(M_1 \otimes M_2\) Matrix multiplication over standard arithmetic or custom semirings (instance, class, or string alias).
commutator(m1, m2) \([M_1, M_2] = M_1 M_2 - M_2 M_1\) Lie commutator bracket of two matrices.
transpose(matrix) \(M^T\) Swaps rows and columns: \(M^T[c, r] = M[r, c]\).
inner(v1, v2, semiring=...) \(\langle v_1, v_2 \rangle\) Vector inner product in \(O(\min(\text{nnz}(v_1), \text{nnz}(v_2)))\) preserving (v1[k], v2[k]) non-commutative operand order.
frobenius_inner(m1, m2, conjugate=...) \(\langle M_1, M_2 \rangle_F\) Frobenius (Hilbert-Schmidt) inner product with optional complex conjugation or semiring dispatch.
power(matrix, n, semiring=...) \(M^n\) Fast binary exponentiation matrix power.
mat_vec(matrix, vector, semiring=...) \(M \cdot v\) Output-sensitive sparse matrix-vector multiplication with native arithmetic fast path.
vec_mat(vector, matrix, semiring=...) \(v^T \cdot M\) Vector-matrix multiplication over a semiring.
vec_add(u, v) \(u + v\) Sparse vector addition with automatic zero-pruning.
vec_scale(u, scalar) \(\alpha u\) Sparse scalar-vector multiplication.
vec_axpy(a, x, y) \(a x + y\) Fused sparse BLAS-1 update (axpy) with zero-tolerance pruning.
vec_norm(u, p=2) \(\|u\|_p\) Sparse vector \(\ell_p\) norm (\(p=1, 2, \infty\), or general \(p > 0\)).
vec_clean(u, tol=1e-14) \(\{k: u_k \mid \lvert u_k \rvert > \tau\}\) Prunes near-zero entries from a sparse vector.
cg_solve(matrix, rhs, ...) \(A \mathbf{x} = \mathbf{b}\) Sparse Conjugate Gradient solver for symmetric positive (semi-)definite systems with optional nullspace deflation.
laplacian_matrix(matrix, normalized=...) \(L = D - W\) Combinatorial, symmetric normalized (\(L_{\mathrm{sym}}\)), or random-walk (\(L_{\mathrm{rw}}\)) Laplacian operator.
trace(matrix, semiring=...) \(\operatorname{Tr}(M) = \bigoplus M_{i,i}\) Diagonal trace reduction over standard arithmetic or any Semiring.
block_diag(*matrices) \(\operatorname{diag}(M_1, M_2, \dots)\) Direct sum block-diagonal matrix construction.
hstack(matrices) \([M_1 \mid M_2]\) Horizontal concatenation along column dimensions.
vstack(matrices) \(\begin{bmatrix} M_1 \\ M_2 \end{bmatrix}\) Vertical concatenation along row dimensions.
block(matrix, rows, cols) \(M[\text{rows}, \text{cols}]\) Slice sub-matrix with index re-basing to 0.
star(matrix, semiring=...) \(M^* = \bigoplus_{k \ge 0} M^k\) Kleene star closure via recursive Conway block decomposition over any StarSemiring.
plus(matrix, semiring=...) \(M^+ = M \cdot M^*\) Strict transitive closure (paths of length \(\ge 1\)) avoiding false self-loops on DAGs.
kronecker_delta(i, j) \(\delta_{i, j}\) Returns 1 if \(i = j\), else 0.

Matrix Inversion (ax.matrix.inv)

For sparse matrix inversion over float, complex, or exact Field[V] structures, use the canonical single-LU \(O(N^3)\) solver inv(matrix, field=..., tol=...) exported directly from algebrax.matrix.


Code Example

import algebrax as ax

# 1. Define sparse matrices
A = {
    'node_A': {'node_A': 1.0, 'node_B': 2.0},
    'node_B': {'node_A': 3.0, 'node_B': 4.0},
}
B = {
    'node_A': {'node_A': 0.5, 'node_B': 1.5},
    'node_B': {'node_A': 2.5, 'node_B': 3.5},
}

# 2. Addition & Transpose
A_plus_B = ax.matrix.add(A, B)
A_T = ax.matrix.transpose(A)

# 3. Matrix Power (A^3)
A_cubed = ax.matrix.power(A, 3)

# 4. Matrix-Vector Multiplication
v = {'node_A': 10.0, 'node_B': 20.0}
Av = ax.matrix.mat_vec(A, v)
print('A * v:', Av)  # -> {'node_A': 50.0, 'node_B': 110.0}

# 5. Graph Laplacian Operator (L = D - W)
graph = {
    'node_A': {'node_B': 2.0},
    'node_B': {'node_A': 2.0},
}
L = ax.matrix.laplacian_matrix(graph)
print('Laplacian Matrix:', L)
# -> {'node_A': {'node_A': 2.0, 'node_B': -2.0}, 'node_B': {'node_A': -2.0, 'node_B': 2.0}}

Ecosystem Bridges (algebrax.converters)

algebrax.converters provides lossless bridges between sparse dictionary matrices and NumPy / SciPy without introducing required runtime dependencies:

  • to_numpy(matrix, shape=..., return_keys=False) and to_scipy(matrix, format='csr', shape=..., return_keys=False):
  • When return_keys=False (default), row and column keys must be non-negative integers (negative integers, booleans, and non-integer keys raise ValueError rather than silently wrapping or dropping entries).
  • Pass return_keys=True to export sparse matrices with arbitrary hashable keys (strings, tuples, sheaf stalk coordinates); returns (array_or_spmatrix, row_keys, col_keys) in canonical sorted order.
  • Empty matrices with to_scipy({}, format=...) honor the requested SciPy sparse format ('csr', 'csc', 'coo', 'dok', 'lil').
  • from_numpy(arr) and from_scipy(sp_matrix):
  • Raise TypeError when given non-numpy.ndarray / non-scipy.sparse inputs, and from_numpy raises ValueError when given an array whose dimensionality is not 2D.
  • prune_sparse(matrix, tol=1e-12), prune_sparse_vector(vector, tol=1e-12), and sparse_matrices_equal(a, b, tol=1e-12):
  • Canonical Layer-1 sparse zero-pruning and NaN-strict matrix equality helpers shared across the library.
from algebrax.converters import to_numpy, to_scipy

# Arbitrary hashable keys -> dense NumPy or SciPy sparse matrix with key maps
labeled = {'node_A': {'node_B': 2.0}, 'node_B': {'node_A': 2.0}}
arr, row_keys, col_keys = to_numpy(labeled, return_keys=True)
sp_csc, _, _ = to_scipy(labeled, format='csc', return_keys=True)