All Projects → likebike → Fasteval

likebike / Fasteval

Licence: mit
Fast and safe evaluation of algebraic expressions

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Fasteval

Mather
zzllrr mather(an offline tool for Math learning, education and research)小乐数学,离线可用的数学学习(自学或教学)、研究辅助工具。计划覆盖数学全部学科的解题、作图、演示、探索工具箱。目前是演示Demo版(抛转引玉),但已经支持数学公式编辑显示,部分作图功能,部分学科,如线性代数、离散数学的部分解题功能。最终目标是推动专业数学家、编程专家、教育工作者、科普工作者共同打造出更加专业级的Mather数学工具
Stars: ✭ 270 (+52.54%)
Mutual labels:  math, algebra
Gap
Main development repository for GAP - Groups, Algorithms, Programming, a System for Computational Discrete Algebra
Stars: ✭ 447 (+152.54%)
Mutual labels:  math, algebra
Grassmann.jl
⟨Leibniz-Grassmann-Clifford⟩ differential geometric algebra / multivector simplicial complex
Stars: ✭ 289 (+63.28%)
Mutual labels:  math, algebra
math eval
✖️➕➖➗ `math_eval` safely evaluates mathematical expressions
Stars: ✭ 33 (-81.36%)
Mutual labels:  math, eval
Reduce.jl
Symbolic parser generator for Julia language expressions using REDUCE algebra term rewriter
Stars: ✭ 172 (-2.82%)
Mutual labels:  math, algebra
Euler
The open-source computational framework for the Swift language
Stars: ✭ 37 (-79.1%)
Mutual labels:  algebra, math
Newton Api
➗ A really micro micro-service for advanced math.
Stars: ✭ 358 (+102.26%)
Mutual labels:  math, algebra
Atosym
algebraic expressions parsing and evaluation through a property system based algorithm
Stars: ✭ 15 (-91.53%)
Mutual labels:  algebra, math
Algebra
means completeness and balancing, from the Arabic word الجبر
Stars: ✭ 92 (-48.02%)
Mutual labels:  math, algebra
Unplugged
Open book about math and computer science.
Stars: ✭ 1,189 (+571.75%)
Mutual labels:  math, algebra
decimal-eval
A tiny, safe, fast JavaScript library for decimal arithmetic expressions.
Stars: ✭ 18 (-89.83%)
Mutual labels:  math, eval
Alga
Abstract algebra for Rust.
Stars: ✭ 154 (-12.99%)
Mutual labels:  math, algebra
racket-cas
Simple computer algebra system
Stars: ✭ 58 (-67.23%)
Mutual labels:  algebra, math
Angourimath
Open-source symbolic algebra library for C# and F#. One of the most powerful in .NET
Stars: ✭ 266 (+50.28%)
Mutual labels:  math, algebra
augmath
Interactive Computer Algebra System. Augmenting how we *do* mathematics using computers
Stars: ✭ 41 (-76.84%)
Mutual labels:  algebra, math
Nerdamer
a symbolic math expression evaluator for javascript
Stars: ✭ 322 (+81.92%)
Mutual labels:  math, algebra
Expressionevaluator
A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
Stars: ✭ 194 (+9.6%)
Mutual labels:  math, eval
Mathematics for Machine Learning
Learn mathematics behind machine learning and explore different mathematics in machine learning.
Stars: ✭ 28 (-84.18%)
Mutual labels:  algebra, math
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-88.7%)
Mutual labels:  math, algebra
Abstract Algebra Cheatsheet
📗 A visualization of key structures in abstract algebra.
Stars: ✭ 137 (-22.6%)
Mutual labels:  math, algebra

fasteval

Fast and safe evaluation of algebraic expressions

fasteval is a library for parsing, compiling, and evaluating algebraic expressions. It can be used directly as a calculator language (much like python), and it is an excellent foundation for building higher-level-languages.

Documentation:

Usage

Add this to your Cargo.toml:

[dependencies]
fasteval = "0.2.4"

You can use codegen-units=1 for better run-time performance. In some cases it will greatly improves LLVM's compile-time optimizations.

If you are using a 'nightly' Rust compiler, you can build with --features nightly to enable optimizations that aren't yet available in 'stable' Rust.

You can build with --no-default-features to disable alphabetical keywords like and, or, NaN, inf. (These words might be important to your applications.)

You can build with --features unsafe-vars to enable Unsafe Variables.

Features

  • No dependencies.
  • Safe execution of untrusted expressions.
  • Works with stable Rust.
  • Supports interpretation (i.e. parse & eval) as well as compiled execution (i.e. parse, compile, eval).
  • Supports Variables and Custom Functions.
  • fasteval is a good base for building higher-level languages.
  • Supports many built-in functions and constants.
  • Supports all the standard algebraic unary and binary operators (+ - * / ^ %), as well as comparisons (< <= == != >= >) and logical operators (&& ||) with short-circuit support.
  • Easy integration into many different types of applications, including scoped evaluation.
  • Very fast performance.

Easy Example

Here is one simple example. See the API Reference for many more!

The ez_eval() function performs the entire allocation-parse-eval process for you. It is slightly inefficient because it always allocates a fresh Slab, but it is very simple to use:

fn main() -> Result<(), fasteval::Error> {
    // This example doesn't use any variables, so just use an EmptyNamespace:
    let mut ns = fasteval::EmptyNamespace;

    let val = fasteval::ez_eval(
        "1+2*3/4^5%6 + log(100K) + log(e(),100) + [3*(3-3)/3] + (2<3) && 1.23",    &mut ns)?;
    //    |            |      |    |   |          |               |   |
    //    |            |      |    |   |          |               |   boolean logic with short-circuit support
    //    |            |      |    |   |          |               comparisons
    //    |            |      |    |   |          square-brackets act like parenthesis
    //    |            |      |    |   built-in constants: e(), pi()
    //    |            |      |    'log' can take an optional first 'base' argument, defaults to 10
    //    |            |      numeric literal with suffix: p, n, µ, m, K, M, G, T
    //    |            many built-in functions: print, int, ceil, floor, abs, sign, log, round, min, max, sin, asin, ...
    //    standard binary operators

    assert_eq!(val, 1.23);

    Ok(())
}

REPL Demo

github.com/likebike/fasteval$ rlwrap cargo run --release --example repl
    Finished release [optimized] target(s) in 0.01s
     Running `target/release/examples/repl`
>>> print("Hello fasteval", 1, 2, 3)
Hello fasteval 1 2 3
3
>>> _ + 1
4
>>> _ + 1
5
>>> _ * 2
10
>>> _ ^ 0.5
3.1622776601683795
>>> let a = 1
1
>>> let b = a + 1
2
>>> let c = a + b * 3
7
>>> a + b + c
10
>>> push
Entered scope[1]
>>> let b = b + 10
12
>>> a + b + c
20
>>> pop
Exited scope[1]
>>> a + b + c
10
>>> 1+2*3/4^5%6 + log(100K) + log(e(),100) + [3*(3-3)/3] + (2<3) && 1.23
1.23
>>> 1+2*3/4^5%6 + print("log(100K) =",log(100K)) + log(e(),100) + [3*(3-3)/3] + (2<3) && 1.23
log(100K) = 5
1.23

Safety

fasteval is designed to evaluate untrusted expressions safely. By default, an expression can only perform math operations; there is no way for it to access other types of operations (like network or filesystem or external commands). Additionally, we guard against malicious expressions:

  • Expressions that are too large (greater than 4KB).
  • Expressions that are too-deeply nested (greater than 32 levels).
  • Expressions with too many values (greater than 64).
  • Expressions with too many sub-expressions (greater than 64).

All limits can be customized at parse time. If any limits are exceeded, parse() will return an Error.

Note that it is possible for you (the developer) to define custom functions which might perform dangerous operations. It is your responsibility to make sure that all custom functionality is safe.

Performance Benchmarks

Here is a short summary of the performance benchmarks. For a more complete report and anlysis, see the official documentation.

Charts

Note that the following charts use logarithmic scales. Therefore, tiny visual differences actually represent very significant performance differences.

Performance of evaluation of a compiled expression:
Compiled Eval Performance

Performance of one-time interpretation (parse and eval):
Interpretation Performance

Performance of compiled Unsafe Variables, compared to the tinyexpr C library (the only other library in our test set that supports this mode):
Unsafe Compiled Eval Performance

Performance of interpreted Unsafe Variables, compared to the tinyexpr C library (the only other library in our test set that supports this mode):
Unsafe Interpretation Performance

Summary

The impressive thing about these results is that fasteval consistently achieves the fastest times across every benchmark and in every mode of operation (interpreted, compiled, and unsafe). It's easy to create a design to claim the #1 spot in any one of these metrics by sacrificing performance in another, but it is difficult to create a design that can be #1 across-the-board.

Because of the broad and robust performance advantages, fasteval is very likely to be an excellent choice for your dynamic evaluation needs.

License

fasteval is distributed under the terms the MIT license.

See LICENSE for details.

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].