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

Go to the source code of this file.

Functions

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

Function Documentation

◆ complex_numbers()

template<typename T >
void complex_numbers ( )

Definition at line 27 of file exprtk_complex_example.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
33 const std::vector<std::string> expressions =
34 {
35 "(1 + i) / (3 + 2i)",
36 "x + y",
37 "x / y",
38 "(x + i) / (3y + 2i)",
39 };
40
41 symbol_table_t symbol_table;
42
43 T i = T(0.0,1.0);
44 T x = T(1.1,0.0);
45 T y = T(2.2,0.0);
46 T v[5];
47
48 symbol_table.add_variable ("i" , i );
49 symbol_table.add_variable ("x" , x );
50 symbol_table.add_variable ("y" , y );
51 symbol_table.add_vector ("v" , v );
52
53 for (std::size_t i = 0; i < expressions.size(); ++i)
54 {
55 expression_t expression;
56 expression.register_symbol_table(symbol_table);
57
58 parser_t parser;
59 if (!parser.compile(expressions[i], expression))
60 {
61 for (std::size_t error_idx = 0; error_idx < parser.error_count(); ++error_idx)
62 {
63 const auto error = parser.get_error(error_idx);
64 printf("Err: %02d Pos: %02d Type: [%14s] Msg: %s\tExpression: %s\n",
65 static_cast<unsigned int>(error_idx),
66 static_cast<unsigned int>(error.token.position),
67 exprtk::parser_error::to_str(error.mode).c_str(),
68 error.diagnostic.c_str(),
69 expressions[i].c_str());
70 }
71 }
72
73 const auto result = expression.value();
74
75 printf("%s = %12.5f + %12.5fi\n",
76 expressions[i].c_str(),
77 result.c_.real(), result.c_.imag());
78 }
79
80 return;
81}
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 83 of file exprtk_complex_example.cpp.

84{
85 complex_numbers<cmplx::complex_t>();
86 return 0;
87}