MathCore lets you go beyond purely numeric expressions by injecting named variables at evaluation time. Whether you’re computing a distance formula, evaluating a polynomial at multiple points, or building a reusable context with physical constants, MathCore’s variable system is straightforward and composable — and works identically whether you’re onDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
std or a bare-metal no_std target.
Evaluating with Variables
evaluate_with_vars()
The most direct way to substitute variables is evaluate_with_vars, which accepts a reference to a HashMap<String, f64> alongside the expression string.
vars with its corresponding f64 value, and then runs the evaluator. The result is either a numeric f64 or a MathError if a variable is undefined, the expression is malformed, or evaluation would overflow.
Example — Pythagorean distance:
Multivariate Expressions
You can include as many variables as your expression requires. All of them are substituted in a single pass before evaluation.The Context Type
For more structured use-cases — where you want a persistent set of named values, or you need to pre-load constants before evaluating a batch of expressions — MathCore provides the Context type defined in mathcore::types.
Creating a Context
Context::with_defaults()
The recommended starting point is with_defaults(), which pre-populates the context with MathCore’s three built-in mathematical constants:
| Constant | Value | Source |
|---|---|---|
pi | 3.141592653589793 | core::f64::consts::PI |
e | 2.718281828459045 | core::f64::consts::E |
tau | 6.283185307179586 | core::f64::consts::TAU |
The
Engine created by MathCore::new() already uses a Context::with_defaults() internally, so pi, e, and tau are always available in expressions without any extra setup.Setting and Retrieving Variables
set_var takes a &str name and an Expr value. Any Expr variant is valid, including symbolic expressions, so you can store unevaluated sub-expressions for later composition.
get_var returns Option<&Expr> — None if the name has never been set.
Building a Context for Multiple Evaluations
ctx.set_var("g", Expr::Number(9.81)); // gravitational acceleration (m/s²)
ctx.set_var("m", Expr::Number(10.0)); // mass in kg
Using Built-in Constants in Expressions
Becausepi, e, and tau live in the default context, you can reference them directly in any expression string passed to calculate, evaluate, or evaluate_with_vars:
no_std HashMap Compatibility
MathCore is #![no_std] at its core. The HashMap<K, V> type exported from mathcore and re-exported through mathcore::prelude is feature-conditional:
