-
Notifications
You must be signed in to change notification settings - Fork 267
/
exprtk_simple_example_05.cpp
82 lines (66 loc) · 2.24 KB
/
exprtk_simple_example_05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 05 *
* Author: Arash Partow (1999-2024) *
* URL: https://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. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
struct myfunc : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
myfunc()
: exprtk::ifunction<T>(2)
{ exprtk::disable_has_side_effects(*this); }
T operator()(const T& v1, const T& v2)
{
return T(1) + (v1 * v2) / T(3);
}
};
template <typename T>
T myotherfunc(T v0, T v1, T v2)
{
return std::abs(v0 - v1) * v2;
}
template <typename T>
void custom_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> 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<T> 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<double>();
return 0;
}