C++ Mathematical Expression Toolkit (ExprTk)
release
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_19.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 19 *
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 <cstdlib>
22
#include <ctime>
23
#include <string>
24
25
#include "
exprtk.hpp
"
26
27
28
template
<
typename
T>
29
class
randu
:
public
exprtk::igeneric_function
<T>
30
{
31
public
:
32
33
typedef
typename
exprtk::igeneric_function<T>
igfun_t
;
34
typedef
typename
igfun_t::parameter_list_t
parameter_list_t
;
35
typedef
typename
igfun_t::generic_type
generic_type
;
36
typedef
typename
generic_type::vector_view
vector_t
;
37
38
using
exprtk::igeneric_function
<T>::operator();
39
40
randu
()
41
:
exprtk
::
igeneric_function
<T>(
"V|VTT"
)
42
/*
43
Overloads:
44
0. V - vector
45
1. VTT - vector, r0, r1
46
*/
47
{ ::srand(
static_cast<
unsigned
int
>
(time(NULL))); }
48
49
inline
T
operator()
(
const
std::size_t& ps_index,
parameter_list_t
parameters)
50
{
51
vector_t
v(parameters[0]);
52
53
std::size_t r0 = 0;
54
std::size_t r1 = v.
size
() - 1;
55
56
using namespace
exprtk::rtl::vecops::helper
;
57
58
if
(
59
(1 == ps_index) &&
60
!
load_vector_range<T>::process
(parameters, r0, r1, 1, 2, 0)
61
)
62
return
T(0);
63
64
for
(std::size_t i = r0; i <= r1; ++i)
65
{
66
v[i] =
rnd
();
67
}
68
69
return
T(1);
70
}
71
72
private
:
73
74
inline
T
rnd
()
75
{
76
// Note: Do not use this in production
77
// Result is in the interval [0,1)
78
return
T(::rand() / T(RAND_MAX + 1.0));
79
}
80
};
81
82
template
<
typename
T>
83
void
vector_randu
()
84
{
85
typedef
exprtk::symbol_table<T>
symbol_table_t;
86
typedef
exprtk::expression<T>
expression_t;
87
typedef
exprtk::parser<T>
parser_t;
88
89
const
std::string vecrandu_program =
90
" var noise[6] := [0]; "
91
" "
92
" if (randu(noise,0,5) == false) "
93
" { "
94
" println('Failed to generate noise'); "
95
" return [false]; "
96
" }; "
97
" "
98
" var noisy[noise[]] := signal + (noise - 1/2); "
99
" "
100
" for (var i := 0; i < noisy[]; i += 1) "
101
" { "
102
" println('noisy[',i,'] = ', noisy[i]); "
103
" }; "
104
" "
105
" println('avg: ', avg(noisy)); "
106
" "
;
107
108
T signal[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5), T(6.6), T(7.7) };
109
110
exprtk::rtl::io::println<T>
println;
111
randu<T>
randu
;
112
113
symbol_table_t
symbol_table
;
114
symbol_table
.
add_vector
(
"signal"
, signal );
115
symbol_table
.
add_function
(
"println"
, println);
116
symbol_table
.
add_function
(
"randu"
,
randu
);
117
118
expression_t
expression
;
119
expression
.
register_symbol_table
(
symbol_table
);
120
121
parser_t
parser
;
122
parser
.
compile
(vecrandu_program,
expression
);
123
124
expression
.
value
();
125
}
126
127
int
main
()
128
{
129
vector_randu<double>();
130
return
0;
131
}
exprtk::expression
Definition
exprtk.hpp:21487
exprtk::expression::register_symbol_table
bool register_symbol_table(symbol_table< T > &st)
Definition
exprtk.hpp:21726
exprtk::expression::value
T value() const
Definition
exprtk.hpp:21703
exprtk::igeneric_function
Definition
exprtk.hpp:19653
exprtk::igeneric_function::igeneric_function
igeneric_function(const std::string ¶m_seq="", const return_type rtr_type=e_rtrn_scalar)
Definition
exprtk.hpp:19667
exprtk::parser
Definition
exprtk.hpp:22176
exprtk::parser::compile
bool compile(const std::string &expression_string, expression< T > &expr)
Definition
exprtk.hpp:24443
exprtk::symbol_table
Definition
exprtk.hpp:19745
exprtk::symbol_table::add_function
bool add_function(const std::string &function_name, function_t &function)
Definition
exprtk.hpp:20811
exprtk::symbol_table::add_vector
bool add_vector(const std::string &vector_name, T(&v)[N])
Definition
exprtk.hpp:20974
exprtk::type_store::parameter_list
Definition
exprtk.hpp:4702
randu
Definition
exprtk_simple_example_19.cpp:30
randu::parameter_list_t
igfun_t::parameter_list_t parameter_list_t
Definition
exprtk_simple_example_19.cpp:34
randu::randu
randu()
Definition
exprtk_simple_example_19.cpp:40
randu::generic_type
igfun_t::generic_type generic_type
Definition
exprtk_simple_example_19.cpp:35
randu::rnd
T rnd()
Definition
exprtk_simple_example_19.cpp:74
randu::igfun_t
exprtk::igeneric_function< T > igfun_t
Definition
exprtk_simple_example_19.cpp:33
randu::operator()
T operator()(const std::size_t &ps_index, parameter_list_t parameters)
Definition
exprtk_simple_example_19.cpp:49
randu::vector_t
generic_type::vector_view vector_t
Definition
exprtk_simple_example_19.cpp:36
exprtk.hpp
vector_randu
void vector_randu()
Definition
exprtk_simple_example_19.cpp:83
main
int main()
Definition
exprtk_simple_example_19.cpp:127
exprtk::rtl::vecops::helper
Definition
exprtk.hpp:44208
exprtk
Definition
exprtk.hpp:60
exprtk::rtl::io::println
Definition
exprtk.hpp:43650
exprtk::rtl::vecops::helper::load_vector_range
Definition
exprtk.hpp:44224
exprtk::type_store::type_view
Definition
exprtk.hpp:4758
exprtk::type_store::type_view::size
std::size_t size() const
Definition
exprtk.hpp:4772
exprtk::type_store
Definition
exprtk.hpp:4677
Generated by
1.9.8