symcas
Fast, deterministic computer algebra system in Rust.
symcas provides a canonical expression arena with hash-consing, exact arithmetic over arbitrary precision rational numbers, ordered sparse multivariate polynomials, symbolic differentiation, series expansions, directed simplifications, roundtrip plain parsing, and LaTeX rendering.
Key Features
- Canonical Construction & O(1) Equality: Expressions are automatically normalized upon construction through flattening, term sorting, exact arithmetic folding, and like-term combination. Equivalent expressions within the same
Contextshare an identical arena handle (Expr::raw_id). - Exact Numeric Domain: Arbitrary-precision integers and rationals backed by
num-bigint/num-integerwith inlinei64optimization for small integers. - Multivariate Polynomials: Ordered sparse distributed polynomials generic over coefficient rings, supporting addition, multiplication, exact division, polynomial remainder sequences (PRS), multivariate GCD, and square-free factorization.
- Symbolic Calculus: Symbolic differentiation over elementary functions (
sin,cos,tan,exp,log,sqrt,abs), Taylor series expansions, algebraic expansion with size bounds, and cancellation. - Assumptions & Predicates: Three-valued logic (
True,False,Unknown) with deduplicated predicates (Positive,NonZero,Integer, etc.). - Deterministic Roundtrip: Machine-readable
plainformatting that parses back to the exact same expression node (parse(plain(e)) == e), along with publication-readylatexoutput.
Architecture
The system is organized into modular crates:
| Crate | Role |
|---|---|
symcas |
High-level facade crate: prelude, parse, plain, and latex APIs |
cas-domain |
Numeric core: Integer, Rational, Ring, and Field traits |
cas-poly |
Multivariate polynomial algebra, ordering (Lex, DegRevLex), and GCD |
cas-expr |
Expression arena, DAG canonicalization, calculus, and assumptions |
cas-print |
Deterministic plain and latex printers |
cas-parse |
Recursive descent parser for plain mathematical syntax |
Quickstart
Add symcas to your Cargo.toml:
[]
= "0.1.0"
1. Expressions & Canonical Form
use *;
let ctx = new;
let x = ctx.sym;
let y = ctx.sym;
// Arithmetic operations construct canonical forms on the fly
let e = .pow - x.clone.pow - y.clone.pow;
assert_eq!;
// Hash-consing: expressions with identical canonical structure share identical node IDs
let a = x.clone + x.clone;
let b = ctx.int * x.clone;
assert!;
2. Parsing & Roundtrip Guarantee
use *;
let ctx = new;
let e = parse.unwrap;
assert_eq!;
// Parsing plain output yields the exact same canonical node
let back = parse.unwrap;
assert!;
3. Expansion, Simplification & Substitution
use *;
let ctx = new;
let = sym!;
// Expand products and powers
let e = .pow;
let expanded = ctx.expand;
assert_eq!;
// Algebraic substitution
let sub = ctx.subst;
assert_eq!;
// Trigonometric simplification
let trig = ctx.call.pow + ctx.call.pow;
let sim = ctx.simplify;
assert_eq!;
4. Differentiation & Taylor Expansion
use *;
let ctx = new;
let x = ctx.sym;
// Symbolic derivative d/dx (sin(x) * x^2)
let f = ctx.call * x.clone.pow;
let df = ctx.diff;
assert_eq!;
// Taylor series around x = 0 to order 4 for exp(x)
let ex = ctx.call;
let s = ctx.taylor;
assert_eq!;
5. Rational Reduction & Polynomial Operations
use *;
let ctx = new;
let x = ctx.sym;
// Rational cancellation via polynomial GCD
let frac = / ;
let reduced = ctx.cancel;
assert_eq!;
// Factoring polynomials
let poly = x.clone.pow - ctx.int;
let factored = ctx.factor;
assert_eq!;
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.