C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_simple_example_08.cpp File Reference
#include <cstdio>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_simple_example_08.cpp:

Go to the source code of this file.

Functions

template<typename T >
void composite ()
 
int main ()
 

Function Documentation

◆ composite()

template<typename T >
void composite ( )

Definition at line 27 of file exprtk_simple_example_08.cpp.

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}
std::string to_str(error_mode mode)
Definition exprtk.hpp:22098

References exprtk::parser_error::to_str().

Here is the call graph for this function:

◆ main()

int main ( )

Definition at line 89 of file exprtk_simple_example_08.cpp.

90{
91 composite<double>();
92 return 0;
93}