C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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
28template <typename T>
30{
31public:
32
37
38 using exprtk::igeneric_function<T>::operator();
39
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
72private:
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
82template <typename T>
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
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;
120
121 parser_t parser;
122 parser.compile(vecrandu_program,expression);
123
125}
126
127int main()
128{
129 vector_randu<double>();
130 return 0;
131}
bool register_symbol_table(symbol_table< T > &st)
Definition exprtk.hpp:21726
igeneric_function(const std::string &param_seq="", const return_type rtr_type=e_rtrn_scalar)
Definition exprtk.hpp:19667
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
bool add_function(const std::string &function_name, function_t &function)
Definition exprtk.hpp:20811
bool add_vector(const std::string &vector_name, T(&v)[N])
Definition exprtk.hpp:20974
igfun_t::parameter_list_t parameter_list_t
igfun_t::generic_type generic_type
exprtk::igeneric_function< T > igfun_t
T operator()(const std::size_t &ps_index, parameter_list_t parameters)
generic_type::vector_view vector_t
void vector_randu()
std::size_t size() const
Definition exprtk.hpp:4772