C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_05.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 05 *
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
struct
myfunc
:
public
exprtk::ifunction
<T>
28
{
29
using
exprtk::ifunction<T>::operator();
30
31
myfunc
()
32
:
exprtk
::
ifunction
<T>(2)
33
{
exprtk::disable_has_side_effects
(*
this
); }
34
35
T
operator()
(
const
T& v1,
const
T& v2)
36
{
37
return
T(1) + (v1 * v2) / T(3);
38
}
39
};
40
41
template
<
typename
T>
42
T
myotherfunc
(T v0, T v1, T v2)
43
{
44
return
std::abs(v0 - v1) * v2;
45
}
46
47
template
<
typename
T>
48
void
custom_function
()
49
{
50
typedef
exprtk::symbol_table<T>
symbol_table_t;
51
typedef
exprtk::expression<T>
expression_t;
52
typedef
exprtk::parser<T>
parser_t;
53
54
const
std::string expression_string =
55
"myfunc(sin(x / pi), otherfunc(3 * y, x / 2, x * y))"
;
56
57
T x = T(1);
58
T y = T(2);
59
myfunc<T>
mf;
60
61
symbol_table_t symbol_table;
62
symbol_table.add_variable(
"x"
,x);
63
symbol_table.add_variable(
"y"
,y);
64
symbol_table.add_function(
"myfunc"
,mf);
65
symbol_table.add_function(
"otherfunc"
,
myotherfunc
);
66
symbol_table.add_constants();
67
68
expression_t expression;
69
expression.register_symbol_table(symbol_table);
70
71
parser_t parser;
72
parser.compile(expression_string,expression);
73
74
const
T result = expression.value();
75
printf(
"Result: %10.5f\n"
,result);
76
}
77
78
int
main
()
79
{
80
custom_function<double>
();
81
return
0;
82
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::ifunction
Definition
exprtk.hpp:19861
exprtk::ifunction::ifunction
ifunction(const std::size_t &pc)
Definition
exprtk.hpp:19864
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
myotherfunc
T myotherfunc(T v0, T v1, T v2)
Definition
exprtk_simple_example_05.cpp:42
custom_function
void custom_function()
Definition
exprtk_simple_example_05.cpp:48
main
int main()
Definition
exprtk_simple_example_05.cpp:78
exprtk
Definition
exprtk.hpp:60
exprtk::disable_has_side_effects
void disable_has_side_effects(FunctionType &func)
Definition
exprtk.hpp:19839
myfunc
Definition
exprtk_mpfr_test.cpp:3717
myfunc::operator()
T operator()(const T &v1, const T &v2)
Definition
exprtk_simple_example_05.cpp:35
myfunc::myfunc
myfunc()
Definition
exprtk_simple_example_05.cpp:31
Generated by
1.17.0