C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_11.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 11 *
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>
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::string wave_program =
34 " var r := 0; "
35 " "
36 " for (var i := 0; i < 1000; i += 1) "
37 " { "
38 " r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
39 " }; "
40 " "
41 " r *= a * (4 / pi); ";
42
43 static const T pi = T(3.141592653589793238462643383279502);
44
45 T f = pi / T(10);
46 T t = T(0);
47 T a = T(10);
48
49 symbol_table_t symbol_table;
50 symbol_table.add_variable("f",f);
51 symbol_table.add_variable("t",t);
52 symbol_table.add_variable("a",a);
53 symbol_table.add_constants();
54
55 expression_t expression;
56 expression.register_symbol_table(symbol_table);
57
58 parser_t parser;
59 parser.compile(wave_program,expression);
60
61 const T delta = (T(4) * pi) / T(1000);
62
63 for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
64 {
65 const T result = expression.value();
66 printf("%19.15f\t%19.15f\n", t, result);
67 }
68}
69
70int main()
71{
72 square_wave2<double>();
73 return 0;
74}
const double pi
static const double delta
void square_wave2()