/* ************************************************************** * C++ Mathematical Expression Toolkit Library * * * * Simple Example 5 * * Author: Arash Partow (1999-2013) * * 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 Common Public License. * * http://www.opensource.org/licenses/cpl1.0.php * * * ************************************************************** */ #include #include #include "exprtk.hpp" template struct myfunc : public exprtk::ifunction { myfunc() : exprtk::ifunction(2) {} inline T operator()(const T& v1, const T& v2) { return T(1) + (v1 * v2) / T(3); } }; template void custom_function() { typedef exprtk::expression expression_t; std::string expression_string = "myfunc(sin(x*pi),y/2)"; T x = T(1.0); T y = T(2.0); myfunc mf; exprtk::symbol_table symbol_table; symbol_table.add_variable("x",x); symbol_table.add_variable("y",y); symbol_table.add_function("myfunc",mf); symbol_table.add_constants(); expression_t expression; expression.register_symbol_table(symbol_table); exprtk::parser parser; parser.compile(expression_string,expression); T result = expression.value(); printf("Result: %10.5f\n",result); } int main() { custom_function(); return 0; }