C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-2024) *
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
26template <typename T>
27struct myfunc : public exprtk::ifunction<T>
28{
29 using exprtk::ifunction<T>::operator();
30
34
35 T operator()(const T& v1, const T& v2)
36 {
37 return T(1) + (v1 * v2) / T(3);
38 }
39};
40
41template <typename T>
42T myotherfunc(T v0, T v1, T v2)
43{
44 return std::abs(v0 - v1) * v2;
45}
46
47template <typename T>
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
78int main()
79{
80 custom_function<double>();
81 return 0;
82}
ifunction(const std::size_t &pc)
Definition exprtk.hpp:19545
T myotherfunc(T v0, T v1, T v2)
void custom_function()
void disable_has_side_effects(FunctionType &func)
Definition exprtk.hpp:19520
T operator()(const T &v1, const T &v2)