Skip to main content

Crate mathlex_eval

Crate mathlex_eval 

Source
Expand description

§mathlex-eval

Numerical evaluator for mathematical expression ASTs produced by mathlex.

Compiles a mathlex Expression AST into an efficient internal representation, then evaluates it with variable substitution and N-dimensional broadcasting.

§Two-phase architecture

  1. Compilecompile() validates the AST, substitutes constants, resolves variables, and folds constant subexpressions into an optimized CompiledExpr.
  2. Evaluateeval() creates a lazy EvalHandle that computes output shape from input shapes but defers computation until consumed via scalar(), to_array(), or iter().

§Quick start

use std::collections::HashMap;
use mathlex::{BinaryOp, Expression};
use mathlex_eval::{compile, eval, EvalInput};

// Build AST for: 2*x + 3
let ast = Expression::Binary {
    op: BinaryOp::Add,
    left: Box::new(Expression::Binary {
        op: BinaryOp::Mul,
        left: Box::new(Expression::Integer(2)),
        right: Box::new(Expression::Variable("x".into())),
    }),
    right: Box::new(Expression::Integer(3)),
};

let compiled = compile(&ast, &HashMap::new()).unwrap();

let mut args = HashMap::new();
args.insert("x", EvalInput::Scalar(5.0));
let result = eval(&compiled, args).unwrap().scalar().unwrap();
assert_eq!(result.to_f64(), Some(13.0));

§Feature flags

FlagDefaultDescription
serdeyesSerialize/Deserialize for CompiledExpr and NumericResult
parallelnoRayon-based parallel broadcasting in EvalHandle::to_array()
ffinoSwift FFI bridge via swift-bridge

Re-exports§

pub use compiler::compile;
pub use compiler::ir::CompiledExpr;
pub use error::CompileError;
pub use error::EvalError;
pub use eval::EvalHandle;
pub use eval::EvalInput;
pub use eval::EvalIter;
pub use eval::NumericResult;
pub use eval::eval;

Modules§

compiler
error
eval