C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_instruction_primer.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk CPU Primer *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <string>
22
23
#include "
exprtk.hpp
"
24
25
26
template
<
typename
T>
27
bool
pgo_primer
()
28
{
29
typedef
exprtk::symbol_table<T>
symbol_table_t;
30
typedef
exprtk::expression<T>
expression_t;
31
typedef
exprtk::parser<T>
parser_t;
32
33
static
const
std::string
expression_list
[] =
34
{
35
"(y + x)"
,
36
"2 * (y + x)"
,
37
"(2 * y + 2 * x)"
,
38
"(y + x / y) * (x - y / x)"
,
39
"x / ((x + y) * (x - y)) / y"
,
40
"1 - ((x * y) + (y / x)) - 3"
,
41
"sin(2 * x) + cos(pi / y)"
,
42
"1 - sin(2 * x) + cos(pi / y)"
,
43
"sqrt(1 - sin(2 * x) + cos(pi / y) / 3)"
,
44
"(x^2 / sin(2 * pi / y)) -x / 2"
,
45
"x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y"
,
46
"clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)"
,
47
"iclamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)"
,
48
"max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))"
,
49
"if(avg(x,y) <= x + y, x - y, x * y) + 2 * pi / x"
,
50
"1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^4 - 5.5x^5 + 6.6y^6 - 7.7x^27 + 8.8y^55"
,
51
"(yy + xx)"
,
52
"2 * (yy + xx)"
,
53
"(2 * yy + 2 * xx)"
,
54
"(yy + xx / yy) * (xx - yy / xx)"
,
55
"xx / ((xx + yy) * (xx - yy)) / yy"
,
56
"1 - ((xx * yy) + (yy / xx)) - 3"
,
57
"sin(2 * xx) + cos(pi / yy)"
,
58
"1 - sin(2 * xx) + cos(pi / yy)"
,
59
"sqrt(1 - sin(2 * xx) + cos(pi / yy) / 3)"
,
60
"(xx^2 / sin(2 * pi / yy)) -xx / 2"
,
61
"xx + (cos(yy - sin(2 / xx * pi)) - sin(xx - cos(2 * yy / pi))) - yy"
,
62
"clamp(-1.0, sin(2 * pi * xx) + cos(yy / 2 * pi), +1.0)"
,
63
"max(3.33, min(sqrt(1 - sin(2 * xx) + cos(pi / yy) / 3), 1.11))"
,
64
"if(avg(xx,yy) <= xx + yy, xx - yy, xx * yy) + 2 * pi / xx"
,
65
"1.1xx^1 + 2.2yy^2 - 3.3xx^3 + 4.4yy^4 - 5.5xx^5 + 6.6yy^6 - 7.7xx^27 + 8.8yy^55"
,
66
"(1.1*(2.2*(3.3*(4.4*(5.5*(6.6*(7.7*(8.8*(9.9+x)))))))))"
,
67
"(((((((((x+9.9)*8.8)*7.7)*6.6)*5.5)*4.4)*3.3)*2.2)*1.1)"
,
68
"(x + y) * z"
,
"x + (y * z)"
,
"(x + y) * 7"
,
"x + (y * 7)"
,
69
"(x + 7) * y"
,
"x + (7 * y)"
,
"(7 + x) * y"
,
"7 + (x * y)"
,
70
"(2 + x) * 3"
,
"2 + (x * 3)"
,
"(2 + 3) * x"
,
"2 + (3 * x)"
,
71
"(x + 2) * 3"
,
"x + (2 * 3)"
,
72
"(x + y) * (z / w)"
,
"(x + y) * (z / 7)"
,
"(x + y) * (7 / z)"
,
"(x + 7) * (y / z)"
,
73
"(7 + x) * (y / z)"
,
"(2 + x) * (y / z)"
,
"(x + 2) * (y / 3)"
,
"(2 + x) * (y / 3)"
,
74
"(x + 2) * (3 / y)"
,
"x + (y * (z / w))"
,
"x + (y * (z / 7))"
,
"x + (y * (7 / z))"
,
75
"x + (7 * (y / z))"
,
"7 + (x * (y / z))"
,
"2 + (x * (3 / y))"
,
"x + (2 * (y / 4))"
,
76
"2 + (x * (y / 3))"
,
"x + (2 * (3 / y))"
,
77
"x + ((y * z) / w)"
,
"x + ((y * z) / 7)"
,
"x + ((y * 7) / z)"
,
"x + ((7 * y) / z)"
,
78
"7 + ((y * z) / w)"
,
"2 + ((x * 3) / y)"
,
"x + ((2 * y) / 3)"
,
"2 + ((x * y) / 3)"
,
79
"x + ((2 * 3) / y)"
,
"(((x + y) * z) / w)"
,
80
"(((x + y) * z) / 7)"
,
"(((x + y) * 7) / z)"
,
"(((x + 7) * y) / z)"
,
"(((7 + x) * y) / z)"
,
81
"(((2 + x) * 3) / y)"
,
"(((x + 2) * y) / 3)"
,
"(((2 + x) * y) / 3)"
,
"(((x + 2) * 3) / y)"
,
82
"((x + (y * z)) / w)"
,
"((x + (y * z)) / 7)"
,
"((x + (y * 7)) / y)"
,
"((x + (7 * y)) / z)"
,
83
"((7 + (x * y)) / z)"
,
"((2 + (x * 3)) / y)"
,
"((x + (2 * y)) / 3)"
,
"((2 + (x * y)) / 3)"
,
84
"((x + (2 * 3)) / y)"
,
85
"(xx + yy) * zz"
,
"xx + (yy * zz)"
,
86
"(xx + yy) * 7"
,
"xx + (yy * 7)"
,
87
"(xx + 7) * yy"
,
"xx + (7 * yy)"
,
88
"(7 + xx) * yy"
,
"7 + (xx * yy)"
,
89
"(2 + x) * 3"
,
"2 + (x * 3)"
,
90
"(2 + 3) * x"
,
"2 + (3 * x)"
,
91
"(x + 2) * 3"
,
"x + (2 * 3)"
,
92
"(xx + yy) * (zz / ww)"
,
"(xx + yy) * (zz / 7)"
,
93
"(xx + yy) * (7 / zz)"
,
"(xx + 7) * (yy / zz)"
,
94
"(7 + xx) * (yy / zz)"
,
"(2 + xx) * (yy / zz)"
,
95
"(xx + 2) * (yy / 3)"
,
"(2 + xx) * (yy / 3)"
,
96
"(xx + 2) * (3 / yy)"
,
"xx + (yy * (zz / ww))"
,
97
"xx + (yy * (zz / 7))"
,
"xx + (yy * (7 / zz))"
,
98
"xx + (7 * (yy / zz))"
,
"7 + (xx * (yy / zz))"
,
99
"2 + (xx * (3 / yy))"
,
"xx + (2 * (yy / 4))"
,
100
"2 + (xx * (yy / 3))"
,
"xx + (2 * (3 / yy))"
,
101
"xx + ((yy * zz) / ww)"
,
"xx + ((yy * zz) / 7)"
,
102
"xx + ((yy * 7) / zz)"
,
"xx + ((7 * yy) / zz)"
,
103
"7 + ((yy * zz) / ww)"
,
"2 + ((xx * 3) / yy)"
,
104
"xx + ((2 * yy) / 3)"
,
"2 + ((xx * yy) / 3)"
,
105
"xx + ((2 * 3) / yy)"
,
"(((xx + yy) * zz) / ww)"
,
106
"(((xx + yy) * zz) / 7)"
,
"(((xx + yy) * 7) / zz)"
,
107
"(((xx + 7) * yy) / zz)"
,
"(((7 + xx) * yy) / zz)"
,
108
"(((2 + xx) * 3) / yy)"
,
"(((xx + 2) * yy) / 3)"
,
109
"(((2 + xx) * yy) / 3)"
,
"(((xx + 2) * 3) / yy)"
,
110
"((xx + (yy * zz)) / ww)"
,
"((xx + (yy * zz)) / 7)"
,
111
"((xx + (yy * 7)) / yy)"
,
"((xx + (7 * yy)) / zz)"
,
112
"((7 + (xx * yy)) / zz)"
,
"((2 + (xx * 3)) / yy)"
,
113
"((xx + (2 * yy)) / 3)"
,
"((2 + (xx * yy)) / 3)"
,
114
"((xx + (2 * 3)) / yy)"
115
};
116
117
static
const
std::size_t
expression_list_size
=
sizeof
(
expression_list
) /
sizeof
(std::string);
118
119
T x = T(0);
120
T y = T(0);
121
T z = T(0);
122
T w = T(0);
123
T xx = T(0);
124
T yy = T(0);
125
T zz = T(0);
126
T ww = T(0);
127
128
symbol_table_t symbol_table;
129
symbol_table.add_constants();
130
symbol_table.add_variable(
"x"
, x);
131
symbol_table.add_variable(
"y"
, y);
132
symbol_table.add_variable(
"z"
, z);
133
symbol_table.add_variable(
"w"
, w);
134
symbol_table.add_variable(
"xx"
,xx);
135
symbol_table.add_variable(
"yy"
,yy);
136
symbol_table.add_variable(
"zz"
,zz);
137
symbol_table.add_variable(
"ww"
,ww);
138
139
typedef
typename
std::deque<expression_t> expr_list_t;
140
expr_list_t expr_list;
141
142
const
std::size_t
rounds
= 50;
143
144
{
145
for
(std::size_t r = 0; r <
rounds
; ++r)
146
{
147
expr_list.clear();
148
parser_t parser;
149
150
for
(std::size_t i = 0; i <
expression_list_size
; ++i)
151
{
152
expression_t expression;
153
expression.register_symbol_table(symbol_table);
154
155
if
(!parser.compile(
expression_list
[i],expression))
156
{
157
return
false
;
158
}
159
160
expr_list.push_back(expression);
161
}
162
}
163
}
164
165
struct
execute
166
{
167
static
inline
T process(T& x, T& y, expression_t& expression)
168
{
169
static
const
T lower_bound = T(-20);
170
static
const
T upper_bound = T(+20);
171
static
const
T
delta
= T(0.1);
172
173
T total = T(0);
174
175
for
(x = lower_bound; x <= upper_bound; x +=
delta
)
176
{
177
for
(y = lower_bound; y <= upper_bound; y +=
delta
)
178
{
179
total += expression.value();
180
}
181
}
182
183
return
total;
184
}
185
};
186
187
for
(std::size_t i = 0; i < expr_list.size(); ++i)
188
{
189
execute::process( x, y, expr_list[i]);
190
execute::process(xx, yy, expr_list[i]);
191
}
192
193
{
194
using namespace
exprtk
;
195
196
for
(std::size_t i = 0; i < 10000; ++i)
197
{
198
const
T v = T(123.456 + i);
199
200
if
(
details::is_true
(
details::numeric::nequal
(
details::numeric::fast_exp<T, 1>::result
(v),
details::numeric::pow
(v,T(1)))))
201
return
false
;
202
203
#define else_stmt(N) \
204
else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,N>::result(v),details::numeric::pow(v,T(N))))) \
205
return false; \
206
207
else_stmt
( 2)
else_stmt
( 3)
else_stmt
( 4)
else_stmt
( 5)
208
else_stmt
( 6)
else_stmt
( 7)
else_stmt
( 8)
else_stmt
( 9)
209
else_stmt
(10)
else_stmt
(11)
else_stmt
(12)
else_stmt
(13)
210
else_stmt
(14)
else_stmt
(15)
else_stmt
(16)
else_stmt
(17)
211
else_stmt
(18)
else_stmt
(19)
else_stmt
(20)
else_stmt
(21)
212
else_stmt
(22)
else_stmt
(23)
else_stmt
(24)
else_stmt
(25)
213
else_stmt
(26)
else_stmt
(27)
else_stmt
(28)
else_stmt
(29)
214
else_stmt
(30)
else_stmt
(31)
else_stmt
(32)
else_stmt
(33)
215
else_stmt
(34)
else_stmt
(35)
else_stmt
(36)
else_stmt
(37)
216
else_stmt
(38)
else_stmt
(39)
else_stmt
(40)
else_stmt
(41)
217
else_stmt
(42)
else_stmt
(43)
else_stmt
(44)
else_stmt
(45)
218
else_stmt
(46)
else_stmt
(47)
else_stmt
(48)
else_stmt
(49)
219
else_stmt
(50)
else_stmt
(51)
else_stmt
(52)
else_stmt
(53)
220
else_stmt
(54)
else_stmt
(55)
else_stmt
(56)
else_stmt
(57)
221
else_stmt
(58)
else_stmt
(59)
else_stmt
(60)
else_stmt
(61)
222
}
223
}
224
225
return
true
;
226
}
227
228
int
main
()
229
{
230
pgo_primer<double>
();
231
return
0;
232
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
rounds
static const std::size_t rounds
Definition
exprtk_bsm_benchmark.cpp:26
else_stmt
#define else_stmt(N)
pgo_primer
bool pgo_primer()
Definition
exprtk_instruction_primer.cpp:27
main
int main()
Definition
exprtk_instruction_primer.cpp:228
delta
static const double delta
Definition
exprtk_pgo.cpp:32
expression_list_size
const std::size_t expression_list_size
Definition
exprtk_real_benchmark.cpp:53
expression_list
static const std::string expression_list[]
Definition
exprtk_real_benchmark.cpp:32
exprtk::details::numeric::pow
T pow(const T v0, const T v1)
Definition
exprtk.hpp:1568
exprtk::details::numeric::nequal
T nequal(const T v0, const T v1)
Definition
exprtk.hpp:1554
exprtk::details::is_true
bool is_true(const double v)
Definition
exprtk.hpp:5763
exprtk
Definition
exprtk.hpp:60
exprtk::details::numeric::fast_exp::result
static T result(T v)
Definition
exprtk.hpp:1675
Generated by
1.17.0