/* ************************************************************** * C++ Mathematical Expression Toolkit Library * * * * Simple Example 5 * * Author: Arash Partow (1999-2021) * * URL: http://www.partow.net/programming/exprtk/index.html * * * * Copyright notice: * * Free use of the Mathematical Expression Toolkit Library is * * permitted under the guidelines and in accordance with the * * most current version of the MIT License. * * http://www.opensource.org/licenses/MIT * * * ************************************************************** */ #include #include #include "exprtk.hpp" template struct myfunc : public exprtk::ifunction { using exprtk::ifunction::operator(); myfunc() : exprtk::ifunction(2) { exprtk::disable_has_side_effects(*this); } inline T operator()(const T& v1, const T& v2) { return T(1) + (v1 * v2) / T(3); } }; template inline T myotherfunc(T v0, T v1, T v2) { return std::abs(v0 - v1) * v2; } template void custom_function() { typedef exprtk::symbol_table symbol_table_t; typedef exprtk::expression expression_t; typedef exprtk::parser parser_t; const std::string expression_string = "myfunc(sin(x / pi), otherfunc(3 * y, x / 2, x * y))"; T x = T(1); T y = T(2); myfunc mf; symbol_table_t symbol_table; symbol_table.add_variable("x",x); symbol_table.add_variable("y",y); symbol_table.add_function("myfunc",mf); symbol_table.add_function("otherfunc",myotherfunc); symbol_table.add_constants(); expression_t expression; expression.register_symbol_table(symbol_table); parser_t parser; parser.compile(expression_string,expression); const T result = expression.value(); printf("Result: %10.5f\n",result); } int main() { custom_function(); return 0; }