Skip to content

Groups & Permutation Symmetries (algebrax.algebra & algebrax.group)

1. The Practical Problem: Guaranteed Reversibility

A Monoid lets you fold forward, but cannot undo an action. Whenever a system requires reversibility—such as rolling back database transactions, inverting cryptographic permutations, computing relative coordinate deltas, or reversing 3D rigid-body transformations—every element must possess a two-sided inverse: - Multiplicative Group: \(a \cdot a^{-1} = a^{-1} \cdot a = \mathbf{1}\) (HasInv[V]). - Additive Group: \(a + (-a) = (-a) + a = \mathbf{0}\) (HasNeg[V]).


2. The 30-Second Executable Snippet

from algebrax import group
from algebrax.algebra import AddCommGroup, Group, zpow, zsmul


# 1. Multiplicative Group of non-zero floats
class NonZeroFloatGroup(Group[float]):
    @property
    def one(self) -> float:
        return 1.0

    def mul(self, a: float, b: float) -> float:
        return a * b

    def inv(self, a: float) -> float:
        return 1.0 / a

    def div(self, a: float, b: float) -> float:
        return a / b


grp = NonZeroFloatGroup()
assert isinstance(grp, Group)
assert zpow(grp, 2.0, -3) == 0.125  # 2^(-3) = 1/8

# 2. Symmetric Permutation Group S_n over native dicts
p = {0: 1, 1: 2, 2: 0}  # 3-cycle (0 1 2)
p_inv = group.invert(p)
assert group.compose(p, p_inv) == {0: 0, 1: 1, 2: 2}
assert group.signature(p) == 1  # Even permutation

3. Atomic Mixins & Protocol Contracts

Groups extend monoids with HasInv[V] (multiplicative) or HasNeg[V] (additive) plus equational law witnesses (LawAssociative / LawCommutative or LawAddAssociative / LawAddCommutative), and provide derived division (div(a, b) = mul(a, inv(b))) and subtraction (sub(a, b) = add(a, neg(b))):

Named Protocol Mixin Composition Algebraic Signature Key Operations
Group[V] (HasOne[V], HasMul[V], HasInv[V], LawAssociative, Protocol) \(\langle V, \mathbf{1}, \cdot, ^{-1} \rangle\) one, mul, inv, div, is_associative
CommGroup[V] (HasOne[V], HasMul[V], HasInv[V], LawAssociative, LawCommutative, Protocol) \(\langle V, \mathbf{1}, \cdot, ^{-1} \rangle\) Commutative group (\(a \cdot b = b \cdot a\))
AddGroup[V] (HasZero[V], HasAdd[V], HasNeg[V], LawAddAssociative, Protocol) \(\langle V, \mathbf{0}, +, - \rangle\) zero, add, neg, sub, is_add_associative
AddCommGroup[V] (HasZero[V], HasAdd[V], HasNeg[V], LawAddAssociative, LawAddCommutative, Protocol) \(\langle V, \mathbf{0}, +, - \rangle\) Additive abelian group (\(a + b = b + a\))
GroupAction[G, X] (Protocol[G, X]) \(\langle G, X, \operatorname{gact} \rangle\) Left group action \(g \cdot (h \cdot x) = (gh) \cdot x\)

Unboxed Sparse Vectors as an AddCommGroup

In AlgebraX, sparse vectors dict[K, float] form a natural additive abelian group without wrapper classes:

class SparseVectorGroup(AddCommGroup[dict[str, float]]):
    @property
    def zero(self) -> dict[str, float]:
        return {}

    def add(self, u: dict[str, float], v: dict[str, float]) -> dict[str, float]:
        res = {k: u.get(k, 0.0) + v.get(k, 0.0) for k in set(u) | set(v)}
        return {k: val for k, val in res.items() if val != 0.0}

    def neg(self, u: dict[str, float]) -> dict[str, float]:
        return {k: -val for k, val in u.items()}

    def sub(self, u: dict[str, float], v: dict[str, float]) -> dict[str, float]:
        return self.add(u, self.neg(v))


svg = SparseVectorGroup()
assert isinstance(svg, AddCommGroup)
v = {'x': 2.0, 'y': -1.0}
assert zsmul(svg, v, -3) == {'x': -6.0, 'y': 3.0}

Permutation & Finite Group Operations (algebrax.group)

Permutations \(\pi \in S_n\) are represented as bijective dictionaries {i: pi_i} (or tuples (pi_0, ..., pi_{n-1}) in symmetric_group(n)), composed uniformly in diagrammatic left-to-right order ("apply first, then second"), matching Category.compose: - group.compose(p1, p2): Evaluates \((p_1 ; p_2)(x) = p_2(p_1(x))\). - group.symmetric_mul(p, q): Evaluates tuple(q[p[i]] for i in range(n)), matching group.compose identically on all pairs in \(S_n\). - group.invert(p): Computes the inverse bijection \(\pi^{-1} = \{v: k\}\). - group.signature(p): Computes the parity sign \(\operatorname{sgn}(\pi) \in \{+1, -1\}\) via cycle decomposition in \(O(N)\) time, powering exact symbolic determinants and exterior wedge products.

For structured finite symmetry analysis, FiniteGroup[G] implements Group[G] and FiniteCommGroup[G] refines it for abelian groups (CommGroup[G]), automatically promoted across canonical constructors, centers, subgroups, quotients, and direct products (cyclic_group, dihedral_group, quaternion_group, symmetric_group, alternating_group, direct_product):

from algebrax.algebra import CommGroup
from algebrax.group import FiniteCommGroup, cyclic_group, quaternion_group, symmetric_group

s3 = symmetric_group(3)
assert s3.order == 6
assert not isinstance(s3, CommGroup)
assert [len(c) for c in s3.conjugacy_classes()] == [1, 3, 2]

# Subgroup generation: A_3 = <(1, 2, 0)> is abelian & normal in S_3, giving quotient S_3 / A_3 ~= C_2
a3 = s3.subgroup([(1, 2, 0)], generate=True)
assert isinstance(a3, FiniteCommGroup) and isinstance(a3, CommGroup)
assert s3.is_normal(a3)
assert isinstance(s3.quotient(a3), FiniteCommGroup)

# Quaternion group Q_8: center Z(Q_8) = {1, -1} and abelian Klein-4 quotient
q8 = quaternion_group()
assert isinstance(q8.center(), FiniteCommGroup)
assert isinstance(q8.quotient(q8.center()), FiniteCommGroup)
assert isinstance(cyclic_group(4), FiniteCommGroup)

4. Falsifiability: Law Verification & Counterexamples

from algebrax.verification import verify_add_comm_group_laws, verify_group_laws

# Positive verification: SparseVectorGroup satisfies all AddCommGroup axioms
samples = [{}, {'x': 1.0}, {'y': -2.0}, {'x': 1.0, 'y': 3.0}]
res = verify_add_comm_group_laws(svg, samples)
assert all(res.values())

# Positive verification: NonZeroFloatGroup satisfies all Group axioms
res_mul = verify_group_laws(grp, [1.0, 2.0, -0.5, 4.0])
assert all(res_mul.values())

# Counterexample: (R, *) is a Monoid, but NOT a Group because 0 has no multiplicative inverse
import pytest


class RealMultWithZero:
    one = 1.0

    def mul(self, a: float, b: float) -> float:
        return a * b

    def inv(self, a: float) -> float:
        if a == 0.0:
            raise ZeroDivisionError('0 has no multiplicative inverse')
        return 1.0 / a


with pytest.raises(ZeroDivisionError):
    RealMultWithZero().inv(0.0)

5. Frontier Horizons & Cross-Domain Bridges

  • Weyl Reflection Groups: Root systems of simple Lie algebras generate finite reflection groups (WeylGroup); see Lie Algebras.
  • Group Rings (\(\mathbb{C}[G]\)): Combining a finite group \(G\) with MonoidAlgebraSemiring yields a group algebra for non-abelian Fourier transforms and representation theory.
  • Simplicial Chain Groups (\(C_k\)): Every chain group in Simplicial Homology is a free AddCommGroup generated by oriented \(k\)-simplices.