Skip to main content

Crate tpt_math_symbolic

Crate tpt_math_symbolic 

Source
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. via Rc/interior mutability smuggled past the ownership model, or a malicious parser) would recurse until the stack overflows. Construction through the public Expr API produces acyclic trees, so normal use is safe; only hand-built or externally-parsed input needs scrutiny.

  • f64 round-trip breaks exactness for transcendental functions. Coefficient values are rendered through fmt::Display and parsed back via f64 when evaluating functions such as sin/cos/exp (see apply_func). For the default f64 coefficient this is the identity path and is fine, but it means exact coefficients (the exact feature’s BigRational) are first coerced to f64, 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 the f64 round-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§

CompileError
Why an expression could not be compiled.
Expr
A symbolic expression over coefficient type C.
ParseError
Why an expression string failed to parse.

Traits§

Coefficient
A numeric value that can sit at a leaf of an Expr and survive simplification, evaluation and differentiation.
Symbolic
Extension trait providing fluent constructor and calculus helpers.

Functions§

compile
Compile expr to a closure whose arguments are the variables vars, 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.
ExprRational
Expr<Rational> — exact, arbitrary-precision symbolic expression.