C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_02.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 02 *
6
* Author: Arash Partow (1999-2025) *
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
26
template
<
typename
T>
27
void
square_wave
()
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 expression_string =
34
"a *(4 / pi) * "
35
"((1 / 1) * sin( 2 * pi * f * t) + (1 / 3) * sin( 6 * pi * f * t) + "
36
" (1 / 5) * sin(10 * pi * f * t) + (1 / 7) * sin(14 * pi * f * t) + "
37
" (1 / 9) * sin(18 * pi * f * t) + (1 / 11) * sin(22 * pi * f * t) + "
38
" (1 / 13) * sin(26 * pi * f * t) + (1 / 15) * sin(30 * pi * f * t) + "
39
" (1 / 17) * sin(34 * pi * f * t) + (1 / 19) * sin(38 * pi * f * t) + "
40
" (1 / 21) * sin(42 * pi * f * t) + (1 / 23) * sin(46 * pi * f * t) + "
41
" (1 / 25) * sin(50 * pi * f * t) + (1 / 27) * sin(54 * pi * f * t)) "
;
42
43
static
const
T
pi
= T(3.141592653589793238462643383279502);
44
45
const
T f =
pi
/ T(10);
46
const
T a = T(10);
47
T t = T(0);
48
49
symbol_table_t symbol_table;
50
symbol_table.add_variable(
"t"
,t);
51
symbol_table.add_constant(
"f"
,f);
52
symbol_table.add_constant(
"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(expression_string,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
70
int
main
()
71
{
72
square_wave<double>
();
73
return
0;
74
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
pi
const double pi
Definition
exprtk_benchmark.cpp:186
delta
static const double delta
Definition
exprtk_pgo.cpp:32
square_wave
void square_wave()
Definition
exprtk_simple_example_02.cpp:27
main
int main()
Definition
exprtk_simple_example_02.cpp:70
Generated by
1.17.0