Boost Matheval
Evaluate mathematical expressions using Boost.Spirit
|
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
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.
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.
Commonly known arithmetic operations are supported:
x + y
x - y
x*y
x/y
x%y
(fmod
for floating point types)x**y
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)Binary functions are rather uncommon in mathematics. Nevertheless there exist some:
atan2
max
(X3 only)min
(X3 only)pow
Currently only very few constants are defined.
e
epsilon
phi
pi
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.
2.71828
(Euler's number) regardsless.