A computer algebra system written in Rust. Expressions are generic over the algebraic structure they live in (a set, group, ring or field), so the same expression can be evaluated over different sets.
N: natural integers (ordering only)Z: integersR: reals, with exact rationals and symbolic constants (pi,e)C: complex numbers over Gaussian rationals (iis the imaginary unit)Z/n: integers modulonK[x]: univariate polynomials over a setKR^N,C^N: sequences, given by a general term (n^2 + 1) or a recurrence (u(0) := 1,u(n) := 2 u(n - 1) + 1), with termsu(10)and shiftsu(n + 1), also with parameters (f(c, 0) := c,f(c, n) := f(c, n - 1)^2 + c,f(1/2, 3));R^N[k]for the indexkM: matrices with symbolic entries
R supports derivatives, limits, series, integrals (with the Risch algorithm), sums and products (with Gosper's
algorithm, sum(k^2, k, 1, n), sum(1/k^2, k, 1, oo), product(k, k, 1, n)), numerical approximations,
factorization, solving systems of polynomial equations and ordinary differential equations (dsolve(y' = x*y),
dsolve(y'' + y = 0, y(0) = 1, y'(0) = 0): separable, first-order linear, and linear with constant coefficients).
crates/formalith: the computer algebra system librarycrates/formalith-session: interactive sessions (commands, definitions, results), shared by the interfacescrates/formalith-cli: the command line interface (binaryformalith)crates/formalith-web: the browser interface (WebAssembly)crates/formalith-gui: a graphing calculator (egui), natively and in the browser
Nightly Rust is required (rust-toolchain.toml pins it).
use formalith::{field::real::R, parse};
let f = parse!("(x^2 - 1)/(x - 1)", R);
println!("{}", f.simplify());
println!("{}", parse!("integrate(x e^x, x)", R));
println!("{}", parse!("lim(sin(x)/x, x, 0)", R));cargo run # interactive REPL in R
cargo run -- 'diff(sin(x)^2, x)' # evaluate arguments
cargo run -- --set 'Z/7' 'factor(x^2 + 1)'In the REPL, :set <name> changes the set, :typst toggles Typst output, f(x) := x^2 defines a function,
:defs lists definitions and :clear [name] removes them, help builtins lists the builtin functions and
help <name> documents one. Systems are solved with x + y = 3, x - y = 1.
Assumptions on symbols are declared with assume(a > 0, b != 0, n integer), giving sqrt(a^2) = a,
integrate(1/a, a) = ln(a), cos(2 n pi) = 1 or lim(exp(a x), x, oo) = oo. Sessions start with k, m and
n assumed integer: :assumptions lists every assumption, defaults included, :clear n drops one and :clear
restores the defaults.
./dev web builds the WebAssembly package (needs wasm-pack), ./dev web --serve also serves the page on port 8000.
Add --release for an optimized build (otherwise a debug build without wasm-opt), see ./dev --help.
./dev docs --serve writes the documentation page of the builtins into target/docs-site/ and serves it on port
8002.
./dev gui runs the graphical interface (cargo run -p formalith-gui): a list of lines with their results, next
to a plot of curves (y = sin(x), f(x) := x^3 - x, x = y^2), implicit curves and regions (x^2 + y^2 = 1,
y < exp(-x^2)), points ((1, f(1))) and sequences (n^2/10 in the set R^N, or u(0) := 1 and
u(n) := 2 u(n - 1) + 1 in R, as the points (k, u_k), f(c, n) with a slider for c), with sliders for parameters (y = a*sin(b*x)) and the roots, extrema
and intersections of the selected curve. ./dev gui-web builds it for the browser (WebGPU, or WebGL2 without it),
./dev gui-web --serve also serves it on port 8001.
cargo test --workspaceThe files crates/formalith/tests/check_*.rs check results against their definition rather than against
hand-written outputs (derivatives of antiderivatives, substituted solutions, sampled regions and extrema, partial
sums, approx against floating point...). Their random fuzzers are ignored by default:
cargo test --release -p formalith -- --ignored fuzz
FORMALITH_FUZZ_CASES=500 FORMALITH_SEED=42 cargo test -p formalith --test integration check_optimize:: -- --ignoredA failure prints the seed and the failing inputs. FORMALITH_SHOW_SKIPPED=1 lists skipped cases,
FORMALITH_TIMES=1 prints the time of each case and FORMALITH_WORKERS sets the number of threads.
CI runs cargo fmt --all --check and cargo test --workspace on pushes and pull requests. A pre-commit hook formatting the
staged Rust files and running cargo clippy --workspace --all-targets (failing on warnings) is enabled with
git config core.hooksPath .githooks. The fuzzers run every night with a new seed
(.github/workflows/fuzz.yml, also started by hand with a number of cases and a seed).