Skip to main content

Algebraic: Multi-Backend Semiring Algebra

A Python package providing semiring algebra implementations with support for NumPy, JAX, and PyTorch backends.

Overview

This package provides abstract semiring interfaces and concrete implementations for:

  • Tropical semirings (MinPlus, MaxPlus) with smooth variants for differentiability
  • Max-Min algebras for robustness semantics
  • Boolean algebras with De Morgan and Heyting algebra variants
  • Counting semirings
  • Custom semirings via the extensible interface

Features

  • AlgebraicArray : Arrays with semiring semantics: override + , * , @ to use custom algebras
  • Multi-Backend : Supports NumPy, JAX, and PyTorch backends with a unified API
  • Differentiable Kernels : Smooth approximations of boolean and tropical operations for neural networks
  • Rich Semiring Library : Tropical, Boolean, Max-Min, Counting, and custom semirings
  • Polynomial Algebras : Sparse and dense multilinear polynomials over semirings

Quick Start

Recommended Import

import algebraic

The top-level algebraic module re-exports all array operations, semiring specifications, and polynomial types.

Basic Semiring Operations

from algebraic.semirings import tropical_semiring, max_min_algebra, boolean_algebra

# Tropical semiring (MaxPlus: max is addition, + is multiplication)
maxplus = tropical_semiring(minplus=False)
a = maxplus.add(2.0, 3.0)  # max(2, 3) = 3
b = maxplus.mul(2.0, 3.0)  # 2 + 3 = 5

# Tropical semiring (MinPlus: min is addition, + is multiplication)
minplus = tropical_semiring(minplus=True)  # or just tropical_semiring()
c = minplus.add(2.0, 3.0)  # min(2, 3) = 2
d = minplus.mul(2.0, 3.0)  # 2 + 3 = 5

# Max-Min algebra (for robustness/STL semantics)
maxmin = max_min_algebra()
e = maxmin.add(-0.5, 0.2)  # max(-0.5, 0.2) = 0.2
f = maxmin.mul(-0.5, 0.2)  # min(-0.5, 0.2) = -0.5

# Boolean algebra
bool_alg = boolean_algebra(mode="logic")
true = bool_alg.one
false = bool_alg.zero
result = bool_alg.add(true, false)  # True OR False = True

AlgebraicArray: Arrays with Semiring Semantics

The AlgebraicArray class wraps backend arrays and overrides arithmetic operations to use semiring semantics.

import algebraic
from algebraic.semirings import tropical_semiring

# Create algebraic arrays with tropical semiring
tropical = tropical_semiring(minplus=True)
a = algebraic.array([1.0, 2.0, 3.0], semiring=tropical, backend="numpy")
b = algebraic.array([4.0, 5.0, 6.0], semiring=tropical, backend="numpy")

# Element-wise operations use semiring semantics
c = a + b  # Tropical addition: [min(1,4), min(2,5), min(3,6)] = [1, 2, 3]
d = a * b  # Tropical multiplication: [1+4, 2+5, 3+6] = [5, 7, 9]

# Reductions use semiring operations
total = algebraic.sum(a)  # min(1, 2, 3) = 1
product = algebraic.prod(a)  # 1 + 2 + 3 = 6

# Matrix multiplication with @ operator
A = algebraic.array([[1.0, 2.0], [3.0, 4.0]], semiring=tropical, backend="numpy")
B = algebraic.array([[5.0, 6.0], [7.0, 8.0]], semiring=tropical, backend="numpy")
C = A @ B  # Tropical matmul: C[i,j] = min_k(A[i,k] + B[k,j])
# Result: [[6, 7], [8, 9]]

Boolean Algebra for Graph and Logic Operations

import algebraic
from algebraic.semirings import boolean_algebra

# Boolean algebra for reachability
bool_alg = boolean_algebra(mode="logic")

# Adjacency matrix: edge from i to j
adj = algebraic.array([[False, True, False], [False, False, True], [True, False, False]], semiring=bool_alg, backend="numpy")

# Matrix multiplication computes 2-step reachability
reach_2 = adj @ adj
# reach_2[i,j] = True if there's a path of length 2 from i to j

# Transitive closure: adj + adj^2 + adj^3 + ...
reach = adj
for _ in range(3):
    reach = reach + (reach @ adj)
# reach[i,j] = True if there's any path from i to j

Smooth Boolean Operations for Learning

import algebraic
from algebraic.semirings import boolean_algebra

# Differentiable boolean operations for neural networks
smooth_bool = boolean_algebra(mode="smooth", temperature=10.0)
soft_bool = boolean_algebra(mode="soft")

# Example: Soft logical operations on continuous values
x = algebraic.array([0.9, 0.8, 0.1], semiring=soft_bool, backend="numpy")
y = algebraic.array([0.7, 0.3, 0.2], semiring=soft_bool, backend="numpy")

# Soft AND: element-wise multiplication
z_and = x * y  # [0.63, 0.24, 0.02]

# Soft OR: probabilistic OR formula
z_or = x + y  # [0.97, 0.86, 0.28]

JAX-Specific Transformations

AlgebraicArray is registered as a JAX PyTree (via algebraic.utils.jax), so standard JAX transforms work out of the box:

import jax
import algebraic
import algebraic.utils.jax  # registers AlgebraicArray as a JAX PyTree
from algebraic.semirings import tropical_semiring

tropical = tropical_semiring(minplus=True)


@jax.jit
def shortest_paths(dist_matrix):
    """Compute all-pairs shortest paths using tropical matrix multiplication."""
    n = dist_matrix.shape[0]
    result = dist_matrix
    for _ in range(n - 1):
        result = result @ dist_matrix
    return result

For batching, use algebraic.vmap which delegates to jax.vmap or torch.vmap depending on the backend:

from algebraic import vmap

batched_fn = vmap(my_fn, backend="jax")

Advanced Features

Functional Index Updates

AlgebraicArray supports functional index updates with semiring operations:

import algebraic
from algebraic.semirings import tropical_semiring

tropical = tropical_semiring(minplus=True)
arr = algebraic.array([1.0, 2.0, 3.0, 4.0], semiring=tropical, backend="numpy")

# Functional updates (returns new array)
new_arr = arr.at[1].set(0.5)  # Set index 1 to 0.5

# Add using semiring addition (min for tropical)
updated = arr.at[1].add(1.5)  # arr[1] = min(2.0, 1.5) = 1.5

# Multiply using semiring multiplication (+ for tropical)
scaled = arr.at[2].multiply(2.0)  # arr[2] = 3.0 + 2.0 = 5.0

Multilinear Polynomials

Work with sparse and dense polynomial representations over semirings:

from algebraic.polynomials import PolyDict, MonomialBasis
from algebraic.semirings import boolean_algebra

bool_alg = boolean_algebra(mode="logic")

# Sparse representation (efficient for few terms)
x0 = PolyDict.variable(0, num_vars=3, algebra=bool_alg, backend="numpy")
x1 = PolyDict.variable(1, num_vars=3, algebra=bool_alg, backend="numpy")
p = x0 * x1 + x1  # Polynomial: (x0 AND x1) OR x1

# Evaluate at a point
result = p.evaluate({0: True, 1: False, 2: True})

# Dense monomial basis (efficient for many terms)
mb0 = MonomialBasis.variable(0, num_vars=2, bool_alg, backend="numpy")
mb1 = MonomialBasis.variable(1, num_vars=2, bool_alg, backend="numpy")
q = mb0 * mb1  # Represented as dense tensor

Core Concepts

Semirings

A semiring :math: (S, \oplus, \otimes, \mathbf{0}, \mathbf{1}) consists of:

  • Addition (:math: \oplus ): Combines alternative paths/outcomes
  • Multiplication (:math: \otimes ): Combines sequential compositions
  • Additive identity (:math: \mathbf{0} ): Identity for :math: \oplus
  • Multiplicative identity (:math: \mathbf{1} ): Identity for :math: \otimes

Lattices

Bounded distributive lattices specialize semirings where:

  • Join (:math: \lor ) = Addition (:math: \oplus )
  • Meet (:math: \land ) = Multiplication (:math: \otimes )
  • Top = Multiplicative identity (:math: \mathbf{1} )
  • Bottom = Additive identity (:math: \mathbf{0} )

Available Semirings

Name Addition Multiplication Use Case
Boolean Logical OR Logical AND Logic, SAT
Tropical (MaxPlus) max + Optimization, path problems
Tropical (MinPlus) min + Shortest paths, distances
Max-Min max min Robustness degrees, STL
Counting + $\times$ Counting paths

Use Cases

Graph Algorithms

  • Shortest paths : Use tropical semirings for Floyd-Warshall algorithm
  • Reachability : Boolean algebra for transitive closure
  • Path counting : Counting semiring for enumeration

Formal Verification

  • Temporal logic : Signal Temporal Logic (STL) with max-min algebra
  • Automata theory : Weighted automata with tropical semirings
  • Model checking : Boolean polynomials for state space exploration

Machine Learning

  • Differentiable logic : Soft/smooth boolean operations for neural networks
  • Attention mechanisms : Tropical attention for robust aggregation
  • Graph neural networks : Semiring-based message passing

Optimization

  • Dynamic programming : Tropical semirings for Bellman equations
  • Constraint satisfaction : Boolean algebra for SAT solving
  • Resource allocation : Max-min algebra for bottleneck optimization

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

algebraic_arrays-1.3.8.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

algebraic_arrays-1.3.8-py3-none-any.whl (78.3 kB view details)

Uploaded Python 3

File details

Details for the file algebraic_arrays-1.3.8.tar.gz.

File metadata

  • Download URL: algebraic_arrays-1.3.8.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for algebraic_arrays-1.3.8.tar.gz
Algorithm Hash digest
SHA256 5dddb7fc935ea5c7f1b6dbc0be471ce237b9cbb3b816dcfd75ce8c577521c0f4
MD5 80066d6dc0c20dcd9d2cfcb3cb4ec652
BLAKE2b-256 21c27b12e8a3d1ca157a6984710def72bea92021f224559920d76ab22232a9d9

See more details on using hashes here.

File details

Details for the file algebraic_arrays-1.3.8-py3-none-any.whl.

File metadata

  • Download URL: algebraic_arrays-1.3.8-py3-none-any.whl
  • Upload date:
  • Size: 78.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for algebraic_arrays-1.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 47de4b0df20c7b428f26356292405233129832293167f3cc3f1b4b941bc12064
MD5 a612b09825ad6de03376143773f6851c
BLAKE2b-256 fbc931dadfa978726304fb1efaf2a6cc58846c732d5fbe59a6f7fa611d653c45

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.3.8 This release

2 files

1.3.7

2 files

1.3.6

2 files

1.3.5

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.8

2 files

1.2.7

2 files

1.2.6

2 files

1.2.5

2 files

1.2.4

2 files

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page