Boost Matheval
Evaluate mathematical expressions using Boost.Spirit
Short Documentation

Boost Matheval is a libary built on top of Boost.Spirit, Boost.Fusion, and Boost.Phoenix to parse and evaluate mathematical expressions.

The interface consists of the class matheval::Parser and the convenience function matheval::parse function which both skip all whitespace in the input sequence, i.e. x+y is equivalent to x +\ty\n. The required type for the symbol table is std::map<std::string, double>.

If you want to parse a user provided or otherwise non-constant expression, you probably want to use the convenience function

std::string expression = "...";
std::map<std::string, double> symbol_table = { ... };
matheval::parse(expression, symbol_table);

If, however, you can reuse the expression and only want to evaluate for different values in the symbol table, you should use the class interface, because there you only pay the cost of parsing when actually calling matheval::Parser::parse.

std::string expression = "...";
std::map<std::string, double> symbol_table = { ... };
parser.parse(expression);
double result = parser.evaluate(symbol_table);

Because the templates of Boost.Spirit take quite some time to instantiate the implementation is hidden behind an opaque pointer, so that you only have to compile all these templates once.

Supported arithmetic operations

Commonly known arithmetic operations are supported:

  • Addition x + y
  • Subtraction x - y
  • Multiplication x*y
  • Division x/y
  • Modulus x%y (fmod for floating point types)
  • Power x**y

Supported unary functions

Most functions of the standard math header <cmath> are supported. On top of that, Boost Matheval defines the mathematical sign function sgn.

  • abs
  • acos
  • acosh (X3 only)
  • asin
  • asinh (X3 only)
  • atan
  • atanh (X3 only)
  • cbrt (X3 only)
  • ceil
  • cos
  • cosh
  • deg
  • erf (X3 only)
  • erfc (X3 only)
  • exp
  • exp2 (X3 only)
  • floor
  • isinf
  • isnan
  • log
  • log2 (X3 only)
  • log10
  • rad
  • round (X3 only)
  • sgn
  • sin
  • sinh
  • sqrt
  • tan
  • tanh
  • tgamma (X3 only)

Supported binary functions

Binary functions are rather uncommon in mathematics. Nevertheless there exist some:

  • atan2
  • max (X3 only)
  • min (X3 only)
  • pow

Known constants

Currently only very few constants are defined.

  • e
  • epsilon
  • phi
  • pi

Variables

Variables are composed of alphanumerical characters (a-z, A-Z, 0-9) and the underscore character _, but always start with a letter. In other words, variables cannot start with a digit or an underscore.

Warning
Constants always have higher priority than variables. That is to say, that
matheval::parse("e",{std::make_pair("e", 1)});
will evaluate to 2.71828 (Euler's number) regardsless.
matheval::Parser::parse
void parse(std::string const &expr)
Parse the mathematical expression into an abstract syntax tree.
matheval::Parser
Parse a mathematical expression.
Definition: matheval.hpp:25
matheval::parse
double parse(std::string const &expr, std::map< std::string, double > const &st=std::map< std::string, double >())
Convenience function.
Definition: matheval.hpp:58
matheval::Parser::evaluate
double evaluate(std::map< std::string, double > const &st=std::map< std::string, double >())
Evaluate the abstract syntax tree for a given symbol table.