OpcUaCanOpen
CANopen OPC-UA server
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-2019) *
7
* URL: http://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
* http://www.opensource.org/licenses/MIT *
14
* *
15
**************************************************************
16
*/
17
18
19
#include <cstdio>
20
#include <cstdlib>
21
#include <ctime>
22
#include <string>
23
24
#include "
exprtk.hpp
"
25
26
27
template
<
typename
T>
28
class
randu
:
public
exprtk::igeneric_function
<T>
29
{
30
public
:
31
32
typedef
typename
exprtk::igeneric_function<T>
igfun_t
;
33
typedef
typename
igfun_t::parameter_list_t
parameter_list_t
;
34
typedef
typename
igfun_t::generic_type
generic_type
;
35
typedef
typename
generic_type::vector_view
vector_t
;
36
37
using
exprtk::igeneric_function<T>::operator
();
38
39
randu
()
40
:
exprtk
::
igeneric_function
<T>(
"V|VTT"
)
41
/*
42
Overloads:
43
0. V - vector
44
1. VTT - vector, r0, r1
45
*/
46
{ ::srand(static_cast<unsigned int>(
time
(NULL))); }
47
48
inline
T
operator()
(
const
std::size_t& ps_index,
parameter_list_t
parameters)
49
{
50
vector_t
v(parameters[0]);
51
52
std::size_t r0 = 0;
53
std::size_t r1 = v.
size
() - 1;
54
55
if
(
56
(1 == ps_index) &&
57
!
exprtk::rtl::vecops::helper
::
58
load_vector_range<T>::process
(parameters,r0,r1,1,2,0)
59
)
60
return
T(0);
61
62
for
(std::size_t i = r0; i <= r1; ++i)
63
{
64
v[i] =
rnd
();
65
}
66
67
return
T(1);
68
}
69
70
private
:
71
72
inline
T
rnd
()
73
{
74
// Note: Do not use this in production
75
// Result is in the interval [0,1)
76
return
T(::rand() / T(RAND_MAX + 1.0));
77
}
78
};
79
80
template
<
typename
T>
81
void
vector_randu
()
82
{
83
typedef
exprtk::symbol_table<T>
symbol_table_t
;
84
typedef
exprtk::expression<T>
expression_t
;
85
typedef
exprtk::parser<T>
parser_t
;
86
87
std::string
vecrandu_program =
88
" var noise[6] := [0]; "
89
" "
90
" if (randu(noise,0,5) == false) "
91
" { "
92
" println('Failed to generate noise'); "
93
" return [false]; "
94
" } "
95
" "
96
" var noisy[6] := signal + (noise - 1/2); "
97
" "
98
" for (var i := 0; i < noisy[]; i += 1) "
99
" { "
100
" println('noisy[',i,'] = ', noisy[i]); "
101
" } "
102
" "
103
" println('avg: ', avg(noisy)); "
104
" "
;
105
106
T signal[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5), T(6.6), T(7.7) };
107
108
exprtk::rtl::io::println<T>
println;
109
randu<T>
randu
;
110
111
symbol_table_t
symbol_table;
112
symbol_table.
add_vector
(
"signal"
, signal);
113
symbol_table.add_function(
"println"
,println);
114
symbol_table.add_function(
"randu"
,
randu
);
115
116
expression_t
expression;
117
expression.
register_symbol_table
(symbol_table);
118
119
parser_t
parser
;
120
parser
.compile(vecrandu_program,expression);
121
122
expression.value();
123
}
124
125
int
main
()
126
{
127
vector_randu<double>();
128
return
0;
129
}
exprtk
exprtk_simple_example_19.cpp
Generated on Fri Sep 11 2020 14:30:06 for OpcUaCanOpen by
1.8.14