exprtk


C++ Mathematical Expression Library

The C++ Mathematical Expression Toolkit Library (ExprTk) is a simple to use, easy to integrate and extremely efficient mathematical expression parsing and evaluation engine. The parsing engine supports numerous forms of functional and logic processing semantics and is easily extendible.

http://www.partow.net/programming/exprtk/index.html

The ExprTk library has the following capabilities: * Mathematical operators (+, -, *, /, %, ^) * Assignment (:=, +=, -=, *=, /=) * Equalities, Inequalities (=, ==, <>, !=, <, <=, >, >=) * Boolean logic (and, mand, mor, nand, nor, not, or, xor, xnor) * Functions (min, max, avg, sum, abs, ceil, floor, round, roundn, exp, log, log10, logn, root, sqrt, clamp, inrange) * Trigonometry (sin, cos, tan, acos, asin, atan, atan2, cosh, cot, csc, sec, sinh, tanh, d2r, r2d, d2g, g2d, hyp) * Control Structures (if-then-else, ternary conditional, switch case) * Loop Structures (while loop, for loop, repeat until loop, break, continue) * Optimization of expressions (constant folding, operator coupling, strength reduction and special functions for arithmetic sequences) * String operations (concatenation, equalities, inequalities, boolean logic and ranges) * Multiple sequence point and sub expression support * Numeric integration and differentiation * User defined variables, vectors, constants and function support * Support for various numeric types (float, double, long double, MPFR/GMP) * Single header implementation, no building required. No external dependencies. * Completely portable (Compiled and executed upon: x86 x86-64, ARMv7/8, POWER6/7 and AVR32)

Compatible C++ Compilers: * GNU Compiler Collection (3.3+) * IntelĀ® C++ Compiler (8.x+) * Clang/LLVM (1.1+) * PGI C++ (10.x+) * Microsoft Visual Studio C++ Compiler (8.1+) * Comeau C++ Compiler (4.3+) * IBM XL C/C++ (9.x+) * C++ Builder (XE4+)

Example Expressions: * sqrt(1 - (x^2)) * clamp(-1,sin(2 * pi * x) + cos(y / 2 * pi),+1) * sin(2 * x) * if(((x + 2) == 3) and ((y + 5) <= 9),1 + w, 2 / z) * (avg(x,y) <= x + y ? x - y : x * y) + 2 * pi / x * inrange(-2,m,+2) == (({-2 <= m} and [m <= +2]) ? true : false) * ({1 / 1} * [1 / 2] + (1 / 3)) - {1 / 4} ^ [1 / 5] + (1 / 6) -({1 / 7} + [1 / 8] * (1 / 9)) * a * exp(2 * t) + c * z := x + sin(2 * pi / y) * u := 2 * (pi * z) / (w := x + cos(y / pi)) * 2x + 3y + 4z + 5w == 2 * x + 3 * y + 4 * z + 5 * w * 3(x + y) / 2 + 1 == 3 * (x + y) / 2 + 1 * (x + y)3 + 1 / 4 == (x + y) * 3 + 1 / 4 * (x + y)z + 1 / 2 == (x + y) * z + 1 / 2 * (sin(x / pi)cos(2y) + 1) == (sin(x / pi) * cos(2 * y) + 1) * while(x <= 100) { x += 1; } * x <= 'abc123' and (y in 'AString') or ('1x2y3z' != z) * (x like '*123*') or ('a123b' ilike y)

Simple Example ```

include

include

include "exprtk.hpp"

int main() { typedef exprtk::symbol_table symbol_table_t; typedef exprtk::expression expression_t; typedef exprtk::parser parser_t; typedef exprtk::parser_error::type error_t;

std::string expression_str = "z := 2 [sin(x * pi)^3.3 + cos(pi / y)^4.4] % (2.3/3.2x + 3.4/4.3y)";

double x = 1.1; double y = 2.2; double z = 3.3;

symbol_table_t symbol_table; symbol_table.add_constants(); symbol_table.add_variable("x",x); symbol_table.add_variable("y",y); symbol_table.add_variable("z",z);

expression_t expression; expression.register_symbol_table(symbol_table);

parser_t parser;

if (!parser.compile(expression_str,expression)) { printf("Error: %s\tExpression: %s\n", parser.error().c_str(), expression_str.c_str());

  for (std::size_t i = 0; i < parser.error_count(); ++i)
  {
     error_t error = parser.get_error(i);
     printf("Error: %02d Position: %02d Type: [%s] Msg: %s Expr: %s\n",
            static_cast<int>(i),
            static_cast<int>(error.token.position),
            exprtk::parser_error::to_str(error.mode).c_str(),
            error.diagnostic.c_str(),
            expression_str.c_str());
  }

  return 1;

}

double result = expression.value();

printf("Result: %10.5f\n",result);

return 0; } ```

Project Information

The project was created on Jan 26, 2012.

Labels:
CPlusPlus Math expression parser library numerics evaluator fast trigonometry string Integration function float double engine