All Projects → rekka → meval-rs

rekka / meval-rs

Licence: Unlicense, MIT licenses found Licenses found Unlicense UNLICENSE MIT LICENSE-MIT
Math expression parser and evaluation library for Rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to meval-rs

Avalanche
Avalanche: a End-to-End Library for Continual Learning.
Stars: ✭ 151 (+27.97%)
Mutual labels:  evaluation
Expressionevaluator
A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
Stars: ✭ 194 (+64.41%)
Mutual labels:  evaluation
mathpad
Interactive scratchpad calculator for VS Code
Stars: ✭ 20 (-83.05%)
Mutual labels:  math-expressions
Rouge 2.0
ROUGE automatic summarization evaluation toolkit. Support for ROUGE-[N, L, S, SU], stemming and stopwords in different languages, unicode text evaluation, CSV output.
Stars: ✭ 167 (+41.53%)
Mutual labels:  evaluation
Asr Evaluation
Python module for evaluating ASR hypotheses (e.g. word error rate, word recognition rate).
Stars: ✭ 190 (+61.02%)
Mutual labels:  evaluation
Errant
ERRor ANnotation Toolkit: Automatically extract and classify grammatical errors in parallel original and corrected sentences.
Stars: ✭ 208 (+76.27%)
Mutual labels:  evaluation
Palmetto
Palmetto is a quality measuring tool for topics
Stars: ✭ 144 (+22.03%)
Mutual labels:  evaluation
asciidoctor-katex
Asciidoctor extension for converting latexmath using KaTeX at build time
Stars: ✭ 16 (-86.44%)
Mutual labels:  math-expressions
Sarosperceptionkitti
ROS package for the Perception (Sensor Processing, Detection, Tracking and Evaluation) of the KITTI Vision Benchmark Suite
Stars: ✭ 193 (+63.56%)
Mutual labels:  evaluation
Automatic speech recognition
End-to-end Automatic Speech Recognition for Madarian and English in Tensorflow
Stars: ✭ 2,751 (+2231.36%)
Mutual labels:  evaluation
Compile Hero
🔰Visual Studio Code Extension For Compiling Language
Stars: ✭ 169 (+43.22%)
Mutual labels:  evaluation
Germanwordembeddings
Toolkit to obtain and preprocess german corpora, train models using word2vec (gensim) and evaluate them with generated testsets
Stars: ✭ 189 (+60.17%)
Mutual labels:  evaluation
Klipse
Klipse is a JavaScript plugin for embedding interactive code snippets in tech blogs.
Stars: ✭ 2,841 (+2307.63%)
Mutual labels:  evaluation
Api Diff
A command line tool for diffing json rest APIs
Stars: ✭ 164 (+38.98%)
Mutual labels:  evaluation
react-native-math-view
Math view for react native! No WebView!
Stars: ✭ 49 (-58.47%)
Mutual labels:  math-expressions
Rival
RiVal recommender system evaluation toolkit
Stars: ✭ 145 (+22.88%)
Mutual labels:  evaluation
Polara
Recommender system and evaluation framework for top-n recommendations tasks that respects polarity of feedbacks. Fast, flexible and easy to use. Written in python, boosted by scientific python stack.
Stars: ✭ 205 (+73.73%)
Mutual labels:  evaluation
MathImprove
Modify and Improve math expressions.
Stars: ✭ 13 (-88.98%)
Mutual labels:  math-expressions
android-expr-eval
Android application to solve math expressions
Stars: ✭ 17 (-85.59%)
Mutual labels:  math-expressions
Simpleeval
Simple Safe Sandboxed Extensible Expression Evaluator for Python
Stars: ✭ 246 (+108.47%)
Mutual labels:  evaluation

Build Status meval at docs.rs

meval

This Rust crate provides a simple math expression parsing and evaluation. Its main goal is to be convenient to use, while allowing for some flexibility. Currently works only with f64 types. A typical use case is the configuration of numerical computations in Rust, think initial data and boundary conditions, via config files or command line arguments.

Documentation

Installation

Simply add the corresponding entry to your Cargo.toml dependency list:

[dependencies]
meval = "0.2"

Requires Rust 1.26.

Simple examples

fn main() {
    let r = meval::eval_str("1 + 2").unwrap();

    println!("1 + 2 = {}", r);
}

Need to define a Rust function from an expression? No problem, use Expr for this and more:

fn main() {
    let expr: meval::Expr = "sin(pi * x)".parse().unwrap();
    let func = expr.bind("x").unwrap();

    let vs: Vec<_> = (0..100+1).map(|i| func(i as f64 / 100.)).collect();

    println!("sin(pi * x), 0 <= x <= 1: {:?}", vs);
}

Custom constants and functions? Define a Context!

use meval::{Expr, Context};

let y = 1.;
let expr: Expr = "phi(-2 * zeta + x)".parse().unwrap();

// create a context with function definitions and variables
let mut ctx = Context::new(); // built-ins
ctx.func("phi", |x| x + y)
   .var("zeta", -1.);
// bind function with a custom context
let func = expr.bind_with_context(ctx, "x").unwrap();
assert_eq!(func(2.), -2. * -1. + 2. + 1.);

For functions of 2, 3, and N variables use Context::func2, Context::func3 and Context::funcn, respectively. See Context for more options.

If you need a custom function depending on mutable parameters, you will need to use a Cell:

use std::cell::Cell;
use meval::{Expr, Context};
let y = Cell::new(0.);
let expr: Expr = "phi(x)".parse().unwrap();

let mut ctx = Context::empty(); // no built-ins
ctx.func("phi", |x| x + y.get());

let func = expr.bind_with_context(ctx, "x").unwrap();
assert_eq!(func(2.), 2.);
y.set(3.);
assert_eq!(func(2.), 5.);

Supported expressions

meval supports basic mathematical operations on floating point numbers:

  • binary operators: +, -, *, /, % (remainder), ^ (power)
  • unary operators: +, -, ! (factorial)

It supports custom variables and functions like x, weight, C_0, f(1), etc. A variable or function name must start with [a-zA-Z_] and can contain only [a-zA-Z0-9_]. Custom functions with a variable number of arguments are also supported.

Build-ins (given by the context Context::new() and when no context provided) currently supported:

  • functions implemented using functions of the same name in Rust std library:

    • sqrt, abs
    • exp, ln, log10
    • sin, cos, tan, asin, acos, atan, atan2
    • sinh, cosh, tanh, asinh, acosh, atanh
    • floor, ceil, round
    • signum
  • other functions:

    • max(x, ...), min(x, ...): maximum and minimumum of 1 or more numbers
  • constants:

    • pi
    • e

Deserialization

Expr supports deserialization using the serde library to make flexible configuration easy to set up, if the feature serde is enabled (disabled by default).

#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate meval;
use meval::{Expr, Context};

#[derive(Deserialize)]
struct Ode {
    #[serde(deserialize_with = "meval::de::as_f64")]
    x0: f64,
    #[serde(deserialize_with = "meval::de::as_f64")]
    t0: f64,
    f: Expr,
}

fn main() {
    let config = r#"
        x0 = "cos(1.)"
        t0 = 2
        f = "sin(x)"
    "#;
    let ode: Ode = toml::from_str(config).unwrap();

    assert_eq!(ode.x0, 1f64.cos());
    assert_eq!(ode.t0, 2f64);
    assert_eq!(ode.f.bind("x").unwrap()(2.), 2f64.sin());
}

Related projects

This is a toy project of mine for learning Rust, and to be hopefully useful when writing command line scripts. There is no plan to make this anything more than math expression -> number "converter". For more advanced uses, see:

  • evalexpr -- A more complete expression evaluator that supports value types, boolean operators, strings, assignment operators, ...
  • dyon -- A rusty dynamically typed scripting language
  • gluon -- A static, type inferred programming language for application embedding
  • rodolf0/tox -- another shunting yard expression parser

License

This project is dual-licensed under the Unlicense and MIT licenses.

You may use this code under the terms of either license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].