2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
|
|
|
* Simple Example 5 *
|
2023-01-01 02:18:35 +00:00
|
|
|
* Author: Arash Partow (1999-2023) *
|
|
|
|
* URL: https://www.partow.net/programming/exprtk/index.html *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
* Copyright notice: *
|
|
|
|
* Free use of the Mathematical Expression Toolkit Library is *
|
|
|
|
* permitted under the guidelines and in accordance with the *
|
2017-02-21 21:44:24 +00:00
|
|
|
* most current version of the MIT License. *
|
2023-01-01 02:18:35 +00:00
|
|
|
* https://www.opensource.org/licenses/MIT *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
**************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2020-01-01 00:00:00 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
2014-07-15 13:09:08 +00:00
|
|
|
template <typename T>
|
2013-04-14 07:01:14 +00:00
|
|
|
struct myfunc : public exprtk::ifunction<T>
|
|
|
|
{
|
2016-09-15 23:30:36 +00:00
|
|
|
using exprtk::ifunction<T>::operator();
|
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
myfunc()
|
|
|
|
: exprtk::ifunction<T>(2)
|
2016-10-05 02:33:35 +00:00
|
|
|
{ exprtk::disable_has_side_effects(*this); }
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
inline T operator()(const T& v1, const T& v2)
|
|
|
|
{
|
|
|
|
return T(1) + (v1 * v2) / T(3);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-26 06:01:09 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T myotherfunc(T v0, T v1, T v2)
|
|
|
|
{
|
|
|
|
return std::abs(v0 - v1) * v2;
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:46:38 +00:00
|
|
|
template <typename T>
|
2013-04-14 07:01:14 +00:00
|
|
|
void custom_function()
|
|
|
|
{
|
2015-03-28 12:21:55 +00:00
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
2015-03-28 12:21:55 +00:00
|
|
|
|
2020-01-01 00:00:00 +00:00
|
|
|
const std::string expression_string =
|
2016-10-26 06:01:09 +00:00
|
|
|
"myfunc(sin(x / pi), otherfunc(3 * y, x / 2, x * y))";
|
2015-03-28 12:21:55 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
T x = T(1);
|
|
|
|
T y = T(2);
|
2013-04-14 07:01:14 +00:00
|
|
|
myfunc<T> mf;
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
symbol_table_t symbol_table;
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table.add_variable("x",x);
|
|
|
|
symbol_table.add_variable("y",y);
|
|
|
|
symbol_table.add_function("myfunc",mf);
|
2016-10-26 06:01:09 +00:00
|
|
|
symbol_table.add_function("otherfunc",myotherfunc);
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table.add_constants();
|
|
|
|
|
|
|
|
expression_t expression;
|
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
parser_t parser;
|
2013-04-14 07:01:14 +00:00
|
|
|
parser.compile(expression_string,expression);
|
|
|
|
|
2020-01-01 00:00:00 +00:00
|
|
|
const T result = expression.value();
|
2013-04-14 07:01:14 +00:00
|
|
|
printf("Result: %10.5f\n",result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
custom_function<double>();
|
|
|
|
return 0;
|
|
|
|
}
|