Trait meval::Context
[−]
[src]
pub trait Context { fn get_var(&self, _: &str) -> Option<f64> { ... } fn eval_func(&self, _: &str, _: &[f64]) -> Result<f64, FuncEvalError> { ... } }
Values of variables (and constants) and custom functions for substitution into an evaluated expression.
The built in context is returned by the builtin()
function (or the Builtins
type).
Values of variables/constants can be specified by tuples (name, value)
,
std::collections::HashMap
or std::collections::BTreeMap
.
use meval::Context; let bins = meval::builtin(); // built-ins assert_eq!(bins.get_var("pi"), Some(std::f64::consts::PI)); let myvars = ("x", 2.); assert_eq!(myvars.get_var("x"), Some(2f64)); let mut varmap = std::collections::HashMap::new(); varmap.insert("x", 2.); varmap.insert("y", 3.); assert_eq!(varmap.get_var("x"), Some(2f64)); assert_eq!(varmap.get_var("z"), None);
Custom functions can be defined using one of the CustomFunc
, CustomFunc2
, CustomFunc3
and
CustomFuncN
tuple structs.
use meval::{Context, CustomFunc2}; let cust_func = CustomFunc2("phi", |x, y| x / (y * y)); assert_eq!(cust_func.eval_func("phi", &[2., 3.]), Ok(2. / (3. * 3.)));
A Context
can be built by combining other contexts:
use meval::{Context, CustomFunc2}; let bins = meval::builtin(); // built-ins let myvars = ("x", 2.); let cust_func = CustomFunc2("phi", |x, y| x / (y * y)); // contexts can be combined using tuples let ctx = ((myvars, bins), cust_func); // first context has preference if there's duplicity assert_eq!(meval::eval_str_with_context("x * pi + phi(1., 2.)", ctx).unwrap(), 2. * std::f64::consts::PI + 1. / (2. * 2.));
Provided Methods
fn get_var(&self, _: &str) -> Option<f64>
fn eval_func(&self, _: &str, _: &[f64]) -> Result<f64, FuncEvalError>
Implementors
impl Context for Builtins
impl<'a, T: Context> Context for &'a T
impl<T: Context, S: Context> Context for (T, S)
impl<S: AsRef<str>> Context for (S, f64)
impl<S> Context for HashMap<S, f64> where S: Hash + Eq + Borrow<str>
impl<S> Context for BTreeMap<S, f64> where S: Ord + Borrow<str>
impl<S: AsRef<str>, T: Fn(f64) -> f64> Context for CustomFunc<S, T>
impl<S: AsRef<str>, T: Fn(f64, f64) -> f64> Context for CustomFunc2<S, T>
impl<S: AsRef<str>, T: Fn(f64, f64, f64) -> f64> Context for CustomFunc3<S, T>
impl<S: AsRef<str>, T: Fn(&[f64]) -> f64> Context for CustomFuncN<S, T>
impl<S: AsRef<str>> Context for [(S, f64); 0]
impl<S: AsRef<str>> Context for [(S, f64); 1]
impl<S: AsRef<str>> Context for [(S, f64); 2]
impl<S: AsRef<str>> Context for [(S, f64); 3]
impl<S: AsRef<str>> Context for [(S, f64); 4]
impl<S: AsRef<str>> Context for [(S, f64); 5]
impl<S: AsRef<str>> Context for [(S, f64); 6]
impl<S: AsRef<str>> Context for [(S, f64); 7]
impl<S: AsRef<str>> Context for [(S, f64); 8]