Symbolic math library for expression trees, automatic differentiation, simplification, and code generation.
arael-sym provides a lightweight computer algebra system built around a
reference-counted expression tree ([E]). Expressions are constructed from
symbols and constants, combined with standard arithmetic operators (which
auto-simplify), and then differentiated, evaluated, pretty-printed, or
compiled to Rust source code.
This crate is the symbolic engine behind the
arael optimization framework, where it powers
compile-time constraint differentiation and code generation. It can also
be used independently for any symbolic math task.
Scope and limitations
arael-sym is focused on what's needed for nonlinear optimization:
scalar expressions, differentiation, and code generation. Compared to
a full CAS like Python's SymPy, it does not support:
- Symbolic integration
- Equation solving (solve for x)
- Symbolic matrix algebra (symbolic determinant, inverse, eigenvalues)
- Polynomial factoring, GCD, partial fractions
- Limits, series expansion, Taylor series
- Assumptions / domain reasoning (positive, real, integer)
- Pattern matching / rewrite rules
- Pretty-printing of intermediate simplification steps
Examples
The [sym!] macro auto-inserts .clone() on reused variables, so you
can write natural math without ownership boilerplate.
Basics
use *;
let result = sym! ;
assert_eq!;
Differentiation
use *;
let result = sym! ;
assert_eq!;
Evaluation
use *;
let val = sym! ;
assert_eq!;
Code generation
use *;
let = sym! ;
assert_eq!;
assert_eq!;
Common Subexpression Elimination (CSE)
use *;
sym! ;
// Output:
// let __x0 = cos(x) * sin(x);
// __x0 + 1
// 2 * __x0
Vectors and Matrices
use *;
let dot = sym! ;
assert_eq!;
Jacobian
use *;
let = sym! ;
assert_eq!; // d(x*y)/dx
assert_eq!; // d(x*y)/dy
assert_eq!; // d(sin(x)+y)/dx
assert_eq!; // d(sin(x)+y)/dy
Parsing
use *;
let f = parse.unwrap;
assert_eq!;
let vars = from;
assert!;