C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_08.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 08 *
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
void
composite
()
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
typedef
exprtk::parser_error::type
err_t;
33
typedef
exprtk::function_compositor<T>
compositor_t;
34
typedef
typename
compositor_t::function function_t;
35
36
T x = T(1);
37
T y = T(2);
38
39
compositor_t compositor;
40
41
symbol_table_t& symbol_table = compositor.symbol_table();
42
symbol_table.add_constants();
43
symbol_table.add_variable(
"x"
,x);
44
symbol_table.add_variable(
"y"
,y);
45
46
compositor.add(
47
function_t(
"f"
)
// f(x) = sin(x / pi)
48
.var(
"x"
)
49
.expression(
"sin(x / pi)"
));
50
51
compositor.add(
52
function_t(
"g"
)
// g(x,y) = 3[f(x) + f(y)]
53
.vars(
"x"
,
"y"
)
54
.expression(
"3*[f(x) + f(y)]"
));
55
56
std::string expression_string =
"g(1 + f(x), f(y) / 2)"
;
57
58
expression_t expression;
59
expression.register_symbol_table(symbol_table);
60
61
parser_t parser;
62
63
if
(!parser.compile(expression_string,expression))
64
{
65
printf(
"Error: %s\tExpression: %s\n"
,
66
parser.error().c_str(),
67
expression_string.c_str());
68
69
for
(std::size_t i = 0; i < parser.error_count(); ++i)
70
{
71
const
err_t error = parser.get_error(i);
72
73
printf(
"Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n"
,
74
static_cast<
unsigned
int
>
(i),
75
static_cast<
unsigned
int
>
(error.token.position),
76
exprtk::parser_error::to_str
(error.mode).c_str(),
77
error.diagnostic.c_str(),
78
expression_string.c_str());
79
}
80
81
return
;
82
}
83
84
const
T result = expression.value();
85
86
printf(
"%s = %e\n"
, expression_string.c_str(), result);
87
}
88
89
int
main
()
90
{
91
composite<double>
();
92
return
0;
93
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
composite
void composite()
Definition
exprtk_simple_example_08.cpp:27
main
int main()
Definition
exprtk_simple_example_08.cpp:89
exprtk::parser_error::to_str
std::string to_str(error_mode mode)
Definition
exprtk.hpp:22447
exprtk::parser_error::type
Definition
exprtk.hpp:22404
Generated by
1.17.0