This project is read-only.

Project Description

The C++ Mathematical Expression Toolkit Library (ExprTk) is a simple to use, easy to integrate and extremely efficient run-time mathematical expression parser and evaluation engine. ExprTk supports numerous forms of functional, logical and vector processing semantics and is very easily extendible.

Download

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


Capabilities

The ExprTk library has the following capabilities:
  • Mathematical operators (+, -, *, /, %, ^)
  • 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)
  • Equalities, Inequalities (=, ==, <>, !=, <, <=, >, >=)
  • Assignment (:=, +=, -=, *=, /=)
  • Boolean logic (and, nand, nor, not, or, xor, xnor, mand, mor)
  • 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, strength reduction, operator coupling, special functions and dead code elimination)
  • String operations (equalities, inequalities, boolean logic, concatenation and ranges)
  • Expression local variables, vectors and strings
  • User defined variables, vectors, strings, constants and function support
  • Multivariate function composition
  • Multiple sequence point and sub expression support
  • Numeric integration and differentiation
  • Vector Processing: BLAS-L1 (axpy, axpby, axpb), all/any-true/false, count, rotate-left/right, shift-left/right, sort, nth_element, iota, sum, kahan-sum, dot-product, copy
  • File-IO package (routines include: open, close, read, write, getline, support for binary and text modes)
  • 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+)
  • 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)
  • inrange(-2,m,+2) == if(({-2 <= m} and [m <= +2]),1,0)
  • ({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)
  • (avg(x,y) <= x + y ? x - y : x * y) + 2.345 * pi / x
  • while(x <= 100) { x -= 1; }
  • x <= 'abc123' and (y in 'AString') or ('1x2y3z' != z)
  • (x like '*123*') or ('a123b' ilike y)

Simple Example Usage

This example will compile and evaluate a simple expression containing one variable called: x

#include <cstdio>
#include <string>

#include "exprtk.hpp"

int main()
{
   typedef exprtk::symbol_table<double> symbol_table_t;
   typedef exprtk::expression<double>     expression_t;
   typedef exprtk::parser<double>             parser_t;

   std::string expression_str = "abs((2 * x)  - 1)";

   double x = 1.1;

   // Register x with the symbol_table
   symbol_table_t symbol_table;
   symbol_table.add_variable("x",x);

   // Instantiate expression and register symbol_table
   expression_t expression;
   expression.register_symbol_table(symbol_table);

   // Instantiate parser and compile the expression
   parser_t parser;
   parser.compile(expression_str,expression);

   double result = 0.0;

   // Evaluate and print result for when x = 1.1
   result = expression.value();
   printf("Result1: %10.5f\n",result);

   // Update/modify the value of x
   x = 2.2;

   // Evaluate and print result for when x = 2.2
   result = expression.value();
   printf("Result2: %10.5f\n",result);

   return 0;
}

More Extensive Example

The following example demonstrates how an expression with three variables specialized upon the double type is compiled and evaluated using ExprTk. Furthermore, there is code for displaying any errors that occur during the expression compilation process and finally the result of the evaluation is printed to stdout.

#include <cstdio>
#include <string>

#include "exprtk.hpp"

int main()
{
   typedef exprtk::symbol_table<double> symbol_table_t;
   typedef exprtk::expression<double> expression_t;
   typedef exprtk::parser<double> 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)
      {
         const 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;
}


Extensive Documentation

http://partow.net/programming/exprtk/code/readme.txt

Last edited May 27 at 1:33 AM by ArashPartow, version 25