Alkahest
A high-performance computer algebra system for Python built for both humans and agents. Symbolic operations run orders of magnitude faster than SymPy and can run on modern accelerated hardware. Every computation produces a derivation log; a meaningful subset can export Lean 4 proofs for independent verification.
Install: the package is published on PyPI; use pip install alkahest (Python 3.9–3.13). See Install below for optional +jit / +full Linux wheels (GitHub Releases or a future extras index) and building from source.
Demo: try the hosted playground (WASM in-browser, or bring your own server/Jupyter URL + token), or run demo-playground/ locally for the full agent and recording stack. See demo-playground/README.md.
Stack: Rust kernel → FLINT/Arb (polynomials, ball arithmetic) → egglog (e-graph simplification) → MLIR/LLVM (native and GPU codegen) → PyO3 → Python
Install
Requirements: Python 3.9–3.13 (PyPI requires-python).
For an isolated environment (recommended when juggling versions or building from source):
&&
Wheels on PyPI are built without the LLVM JIT and without the optional groebner / egraph / parallel Rust features so installs stay small and avoid a runtime dependency on LLVM. Numeric APIs still work via the interpreter fallback; for native LLVM CPU JIT—or the full Gröbner/JIT stack—use a PyTorch-style opt-in wheel (separate artifact / index), not the default PyPI resolver path.
Opt-in Linux wheels: +jit and +full (PyTorch-style)
Why a separate index or direct wheel URL: feature-heavy wheels use a PEP 440 local version (for example 2.0.3+jit or 2.0.3+full). Those builds must not be mixed into the main PyPI project’s simple API for the same reason PyTorch publishes CUDA wheels on download.pytorch.org: otherwise pip install alkahest could resolve a +jit / +full build as “newer” than 2.0.3 and pull LLVM (or a much larger binary) when you wanted the default wheel.
There is no pip install alkahest[jit] / alkahest[full] that swaps the native extension: pip extras only add Python dependencies, not alternate binaries for the same wheel slot.
Until a dedicated PEP 503 simple index is published, tagged releases attach Linux linux_x86_64 wheels on GitHub Releases (CI builds them on ubuntu-22.04, not the manylinux image used for default wheels). Pick the .whl whose tags match your Python (cp311, etc.) and linux_x86_64.
| Local version | Cargo features | When to use |
|---|---|---|
+jit |
jit |
Native LLVM CPU JIT only (smaller than +full). |
+full |
jit groebner parallel egraph |
JIT plus Gröbner-backed solvers, parallel F4, egglog e-graph backend (matches a typical maximal from-source dev build). |
Direct-install examples (adjust tag and filename after checking the release assets):
These wheels vendor LLVM (for JIT) and related .so files under site-packages/alkahest.libs/. If import alkahest fails with a missing libffi-*.so or libLLVM-*.so, prepend that directory to LD_LIBRARY_PATH (or install matching system packages). Release CI uses the same LD_LIBRARY_PATH step when smoke-testing wheels.
If your client chokes on + in the URL, use percent-encoding (2.0.3%2Bfull in the filename segment).
After installing +jit, alkahest.jit_is_available() should be True. After +full, expect that and Gröbner-backed APIs such as alkahest.solve.
macOS and Windows +jit / +full wheels are not produced in CI yet (LLVM / MSYS2 constraints); use building from source there.
Target layout (roadmap): a small extra index URL (PEP 503) hosting only +jit / +full wheels, mirroring PyTorch’s --extra-index-url workflow:
From source
Required to enable optional features (jit, groebner, cuda) or for development. Prerequisites:
- Rust stable ≥ 1.76 and nightly:
| - LLVM 15:
apt install llvm-15 libllvm15 llvm-15-dev/brew install llvm@15 - FLINT ≥ 2.9 (includes GMP and MPFR):
apt install libflint-dev/brew install flint
Optional Cargo features: parallel (sharded pool + parallel F4), egraph (egglog backend), jit (LLVM JIT), groebner (Gröbner solver + Diophantine + homotopy), cuda (NVPTX codegen).
Rust crate
alkahest-cas is also published on crates.io (docs.rs) for use directly from Rust without a Python runtime:
[]
= "2"
# With optional features:
# alkahest-cas = { version = "2", features = ["groebner", "parallel", "egraph"] }
System prerequisites (same libraries as the Python build — must be present before cargo build):
# Debian / Ubuntu
# macOS
The jit feature additionally requires LLVM 15 dev headers (apt install llvm-15-dev / brew install llvm@15). A self-contained runnable example is in examples/rust_quickstart/.
Quick start
=
=
# Differentiation with derivation log
=
# 2*x*cos(x^2)
# list of rewrite steps
# Integration
=
# exp(x)
# Simplification
=
# x
# JIT-compile to native code
=
# 10.0
Explicit polynomial representations
=
=
=
# FLINT-backed univariate polynomial
=
# 3
# [1, -2, 0, 1]
# GCD
=
=
# x - 1
# Factorization over ℤ (FLINT — Zassenhaus / van Hoeij)
=
# unit and list of (UniPoly, exponent)
# Dense univariate mod p (Berlekamp / Cantor–Zassenhaus via FLINT nmod)
= # x^2+1 over GF(2)
# Rational function with automatic GCD normalization
=
# x + 1
# Sparse multivariate polynomial
=
# 3
Symbolic summation (Gosper / recurrences)
Indefinite and definite sums for terms whose shift ratio F(k+1)/F(k) is rational in k—typically polynomials multiplied by gamma of a linear expression in k. General multivariate Zeilberger automation is partial; use verify_wz_pair(F, G, n, k) to check a discrete telescoping certificate after simplification.
=
=
=
= .
=
Difference equations / rsolve
Linear recurrences in one sequence with constant coefficients and a polynomial right-hand side (in the recurrence index n). Write shifts as pool.func("f", [n + integer]), pass the equation as a single expression that simplifies to zero, and optional initials as {n: value} to fix the C0, C1, … symbols.
=
=
=
# f(n) - f(n-1) - 1 == 0 → general solution n + C0
= .
# Fibonacci with f(0)=0, f(1)=1
= .
Non-homogeneous order > 2 and sequences with polynomial coefficients in n are not implemented yet (see RsolveError / E-RSOLVE-*).
Symbolic products (∏)
product_definite(term, k, lo, hi) closes $\prod_{i=\text{lo}}{\text{hi}} \text{term}(i)$ (inclusive) when term simplifies to ℚ(k) whose numerator/denominator factor into ℤ-linear polynomials — the implementation expands each linear factor $\alpha k+\beta$ with $\Gamma$ shifts $\Gamma(\text{hi}+\beta/\alpha+1)/\Gamma(\text{lo}+\beta/\alpha)$ and collects $\alpha{(\text{hi}-\text{lo}+1)\cdot e}$. product_indefinite returns a Γ/power witness Z(k) with simplify-stable ratio Z(k+1)/Z(k)=term. Product(term, (k, lo, hi)).doit() matches SymPy ergonomics (DerivedResult; use .value). Irreducible quadratics in k, extra symbols besides k, and non-integer powers are rejected (ProductError / E-PROD-*).
=
, = ,
=
= ** 2
= . # (k²-1)/k²
Diophantine equations
Two integer unknowns, equation as a single polynomial = 0: linear families a·x + b·y + c = 0, sum of two squares x² + y² = n (finitely many tuples), and unit Pell x² - D·y² = 1 (fundamental solution (x₀, y₀) via the continued-fraction period of √D). Requires the groebner feature in the native build. API: diophantine(equation, [x, y]) → DiophantineSolution with .kind (parametric_linear, finite, pell_fundamental, no_solution) and typed fields.
=
, = ,
=
assert ==
=
assert == and == 3
Quadratics with an x·y cross-term, unequal ellipse coefficients, or generalized Pell right-hand sides ≠ 1 are not implemented yet (DiophantineError / E-DIOPH-*).
Integer number theory
Submodule alkahest.number_theory: isprime, factorint, nextprime, totient, jacobi_symbol, nthroot_mod (prime modulus; k=2 or gcd(k,p−1)=1), discrete_log (linear scan for moderate primes), plus quadratic DirichletChi on odd square-free conductors. Implemented via FLINT fmpz in the Rust kernel; raises NumberTheoryError (E-NT-*) on invalid input.
assert
assert == 1
assert == 4
assert == 144 % 401
Noncommutative algebra
Symbols can opt out of multiplicative commutativity: pool.symbol("A", "real", commutative=False). Then A * B and B * A are distinct expressions, and sorting of Mul factors is disabled. The egglog backend automatically falls back to the rule-based simplifier when such symbols appear.
Pauli matrices (names sx, sy, sz) and a minimal orthogonal Clifford pair (cliff_e1, cliff_e2) have built-in rewrite tables; combine default rules with ak.simplify_pauli or ak.simplify_clifford_orthogonal. See examples/noncommutative.py.
Truncated series / Laurent tail
series(expr, var, point, order) builds a symbolic truncation about (var − point) and appends a BigO(⋯) remainder. Smooth functions use repeated differentiation; simple poles such as 1/x at zero take the rational Laurent path. Series.expr is the pooled sum-plus-order expression; ExprPool.big_o(inner) constructs standalone order bounds.
=
=
=
=
Rigorous interval arithmetic
=
=
=
# guaranteed enclosure of sin(1 ± 1e-10)
String expressions
=
=
# Parse a string into a symbolic expression
=
# (x^2 + (x * 2)) + 1
# Round-trip: parse then pretty-print
=
# \sin\!\left(x\right)^2 + \cos\!\left(x\right)^2
# sin(x)² + cos(x)²
Lattice reduction and approximate integer relations
Exact LLL reduction on integer bases lives under alkahest.lattice; for floating constants (as float or decimal strings) guess_relation searches for small integer coefficient vectors whose dot product has tiny residual relative to the working precision:
=
=
The relation finder is an augmented-lattice + LLL heuristic, not Ferguson–Bailey PSLQ; treat results as exploratory unless verified independently.
Regular chains / triangular decomposition
Lex-order Gröbner bases yield triangular sets used by the polynomial solver. The triangularize(equations, vars) API returns one or more RegularChain objects (polynomials as GbPoly tiles), splitting along factored bottom univariates when applicable. The built-in solve() routine retries backsolving from an extracted chain when the full basis is not directly triangular enough.
=
=
=
= **2 + **2 -
= -
=
assert >= 1
Primary decomposition
Lex-order Gröbner data is used to split ideals via saturations (I : x_i^∞ with (I + (x_i))) and, in the zero-dimensional case, factoring a univariate polynomial in the first Lex variable. primary_decomposition(polys, vars) returns PrimaryComponent objects with .primary() and .associated_prime() Gröbner bases; radical(polys, vars) returns a basis for √I.
=
, , = , ,
=
assert == 2
=
assert
Differential algebra / Rosenfeld–Gröbner
Polynomial DAEs in implicit form g_i(t, y, y') = 0 can be analysed by prolongation (formal time derivatives of each equation, with the same derivative-state extension rule as Pantelides) and ordinary Gröbner bases over the jet variables. Inconsistent systems yield the unit ideal. Use rosenfeld_groebner(dae, order=..., max_prolong_rounds=...); when Pantelides exhausts its index cap, dae_index_reduce(dae) falls back to this pass.
=
=
=
=
=
=
assert is False
Numerical algebraic geometry / homotopy continuation
Square polynomial systems can be solved numerically with a total-degree homotopy in ℂⁿ ((1-t)·γ·G + t·F), Newton polish on real projections, a conservative Smale-style α heuristic, and ArbBall enclosures attached to each coordinate. Use solve(eqs, vars, method="homotopy") for a list of dict solutions (Expr keys → float). For residuals, certification flags, and enclosures, call solve_numerical(...), which returns CertifiedSolution objects (.coordinates, .smale_certified, .to_dict(), .enclosures(), …).
Ideals whose finite root count in ℂⁿ is strictly below the Bézout bound (often called deficient — e.g. the Katsura family) typically need a polyhedral / mixed-volume start system; only the Bézout start (∏ deg F_i paths) is implemented here.
=
, = ,
=
=
=
Composable transformations
=
return
= # symbolic gradient
= # compiled gradient
Directory layout
alkahest/
├── alkahest-core/ # Rust kernel (published as the alkahest-cas crate)
│ ├── src/
│ │ ├── kernel/ # hash-consed expression DAG, ExprPool
│ │ ├── algebra/ # noncommutative Pauli / Clifford rules
│ │ ├── parse.rs # Pratt expression parser (parse / ParseError)
│ │ ├── poly/ # UniPoly, MultiPoly, RationalFunction
│ │ ├── simplify/ # e-graph simplification (egglog)
│ │ ├── diff/ # symbolic differentiation
│ │ ├── integrate/ # symbolic integration
│ │ ├── calculus/ # series / limits
│ │ ├── jit/ # LLVM JIT and interpreter
│ │ ├── ball/ # Arb ball arithmetic
│ │ ├── ode/ # ODE analysis
│ │ ├── dae/ # DAE analysis and index reduction
│ │ ├── diffalg/ # Rosenfeld–Gröbner / differential elimination (groebner)
│ │ ├── solver/ # polynomial solving: Gröbner triangular, regular chains, homotopy
│ │ ├── lean/ # Lean 4 proof certificate export
│ │ └── primitive/ # primitive registration system
│ └── benches/ # criterion benchmarks
├── alkahest-mlir/ # MLIR dialect and lowering passes
├── alkahest-py/ # PyO3 bindings (Rust side)
├── python/alkahest/ # Python package
│ ├── _transform.py # trace, grad, jit decorators
│ ├── _pytree.py # JAX-style pytree flattening
│ ├── _context.py # context manager and defaults
│ └── experimental/ # unstable API surface
├── examples/ # runnable end-to-end examples
│ └── rust_quickstart/ # self-contained Cargo project for alkahest-cas
├── tests/ # Python test suite (pytest + hypothesis)
├── benchmarks/ # Python benchmarks and competitor comparisons
├── fuzz/ # AFL++ fuzz targets
├── docs/ # mdBook and Sphinx documentation
├── website/ # landing page (alkahest-cas.github.io)
│ └── src/ # index.html + styles.css source (deployed via CI)
├── alkahest-skill/ # Skill for AI to use alkahest
├── agent-benchmark/ # benchmark for comparing AI use of alkahest vs other CAS
└── scripts/ # CI helpers (API freeze check, error codes)
Expression representations
| Type | Description |
|---|---|
Expr |
Generic hash-consed symbolic expression |
UniPoly |
Dense univariate polynomial (FLINT-backed) |
MultiPoly |
Sparse multivariate polynomial over ℤ |
RationalFunction |
Quotient of polynomials with GCD normalization |
ArbBall |
Real interval with rigorous error bounds (Arb) |
Representation types are explicit — no silent performance cliffs. Conversion between them is always an opt-in call (UniPoly.from_symbolic(...), etc.).
Result objects
Every top-level operation returns a DerivedResult with:
.value— the result expression.steps— derivation log (list of rewrite rules applied).certificate— Lean 4 proof term, when available
Documentation and further reading
- Documentation site — full API reference and user guide
ROADMAP.md— planned milestonesCONTRIBUTING.md— Rust vs Python layer guideTESTING.md— property-based testing, fuzzing, sanitizers, CI tiersBENCHMARKS.md— criterion and Python benchmark suitesexamples/— runnable end-to-end examplesLICENSE— Apache 2.0 license
Stability
Alkahest follows semantic versioning from 1.0. The stable surface is everything re-exported from alkahest_cas::stable (Rust) and alkahest.__all__ (Python). Experimental APIs live under alkahest_cas::experimental and alkahest.experimental and may change in minor releases.