/boost_matheval

Evaluate mathematical expressions using Boost.Spirit

Primary LanguageC++Boost Software License 1.0BSL-1.0

Boost Matheval

Build status AppVeyor build status Documentation

This header-only C++11 libary uses Boost.Spirit and Boost.Phoenix to parse and evaluate mathematical expressions.

Motivating example 1

#include <iostream>
#include "matheval.hpp"

int main()
{
    std::cout << matheval::parse<double>("1+1",{}) << '\n';
}

Outputs

2

Motivating example 2

#include <iostream>
#include <map>
#include "matheval.hpp"

int main()
{
    std::map<std::string,double> symtab;
    symtab.emplace(std::make_pair("x",  2));
    symtab.emplace(std::make_pair("y", -1));
    std::cout << matheval::parse<double>("cbrt(x/2 + sqrt(x**2/4 + y**3/24))",symtab) << '\n';
}

Outputs

1.25548

Motivating example 3

We can also evaluate an expression multiple times without paying the cost of parsing again.

#include <iostream>
#include <map>
#include "matheval.hpp"

int main()
{
    matheval::Parser<double> parser;
    parser.parse("x + 1");
    std::cout << parser.evaluate({std::make_pair("x",1)}) << ' '
              << parser.evaluate({std::make_pair("x",2)}) << '\n';
}

Outputs

2 3

Build instructions

Since Boost Matheval is header-only you can simply copy matheval.hpp into your project and start using it. If you want to build the examples or run the tests, you can build them using CMake.

mkdir build
cd build
cmake ..
make         # build the examples
make check   # build and run the tests

Requirements and Limitations

General:

  • C++11 compatible compiler, i.e. GCC >= 4.8, Clang, Visual Studio 2015.
  • Boost.Spirit, Boost.Phoenix, and Boost.MathConstants.
  • No support for ternary functions (e.g. if).
  • No support for complex numbers.

Alternatives

  • GNU libmatheval is a C library built using the parser generator YACC with about the same scope as Boost Matheval. It is a not header-only and does not have a C++ interface at the moment, but it is much faster in terms of compilation time.
  • ExprTk is a true monster with almost 40000 lines in a single header file. It implements a complete state machine including things like logical operations, control structures, and even file IO. Compilation time is even longer than with Boost Matheval.

License

Distributed under the Boost Software License, Version 1.0.