Expand description
Permissive-license symbolic math (CAS).
tpt-math-symbolic provides a small, dependency-light computer-algebra
core built around an expression tree Expr<C> parameterised over a
numeric Coefficient. It supports construction, algebraic
simplification, symbolic differentiation, substitution and evaluation.
§Design decision (spec.txt open call)
The expression tree is generic over Coefficient rather than hard-wired
to a single number type. The default convenience alias is
Expr64 (Expr<f64>). Enable the exact feature to also get
ExprRational, which is backed by arbitrary-precision
tpt_math_exact::BigRational wrapped in a Coefficient implementation.
This keeps the crate genuinely coefficient-agnostic without forcing every
consumer onto exact arithmetic.
§Correctness caveats
Two properties of this CAS are intentional but worth flagging before you depend on it:
-
Unbounded recursion in
simplify. Simplification is implemented as straight structural recursion over the expression tree (simplify). It terminates for any finite, acyclic expression, but it is not guarded against cyclic/self-referential input: building an expression that contains itself (e.g. viaRc/interior mutability smuggled past the ownership model, or a malicious parser) would recurse until the stack overflows. Construction through the publicExprAPI produces acyclic trees, so normal use is safe; only hand-built or externally-parsed input needs scrutiny. -
f64round-trip breaks exactness for transcendental functions.Coefficientvalues are rendered throughfmt::Displayand parsed back viaf64when evaluating functions such assin/cos/exp(seeapply_func). For the defaultf64coefficient this is the identity path and is fine, but it means exact coefficients (theexactfeature’sBigRational) are first coerced tof64, losing precision and making transcendental results approximate even when the inputs were exact. Algebraic simplification (add/mul/pow of rationals) stays exact; only transcendentals go through thef64round-trip.
use tpt_math_symbolic::{Expr64, Symbolic};
// Build (3*x + 1)^2 and differentiate with respect to x.
let x = Expr64::var("x");
let expr = (Expr64::from(3.0) * x.clone() + Expr64::from(1.0)).pow(2);
let d = expr.derivative("x").simplify();
assert_eq!(d.eval_str("x", 2.0).unwrap(), 42.0);Structs§
- Compiled
- An expression compiled to a closure over an argument slice.
- Rational
- Arbitrary-precision rational wrapper implementing
Coefficient.
Enums§
- Compile
Error - Why an expression could not be compiled.
- Expr
- A symbolic expression over coefficient type
C. - Parse
Error - Why an expression string failed to parse.
Traits§
- Coefficient
- A numeric value that can sit at a leaf of an
Exprand survive simplification, evaluation and differentiation. - Symbolic
- Extension trait providing fluent constructor and calculus helpers.
Functions§
- compile
- Compile
exprto a closure whose arguments are the variablesvars, in that order. - parse_
expr - Parse an infix expression such as
"x^2 + sin(x)".
Type Aliases§
- Expr64
Expr<f64>— the default, floating-point symbolic expression.- Expr
Rational Expr<Rational>— exact, arbitrary-precision symbolic expression.