C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_complex_example.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Example using a simple Complex type *
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>
23#include "exprtk.hpp"
24
25
26template <typename T>
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}
82
83int main()
84{
85 complex_numbers<cmplx::complex_t>();
86 return 0;
87}
void complex_numbers()
std::string to_str(error_mode mode)
Definition exprtk.hpp:22098