From bf24f1fc52ff42ebd88321c994e2613ab97f3a3e Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Wed, 19 Oct 2016 18:20:26 +1100 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk_simple_example_19.cpp | 129 +++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 exprtk_simple_example_19.cpp diff --git a/exprtk_simple_example_19.cpp b/exprtk_simple_example_19.cpp new file mode 100644 index 0000000..186cd5a --- /dev/null +++ b/exprtk_simple_example_19.cpp @@ -0,0 +1,129 @@ +/* + ************************************************************** + * C++ Mathematical Expression Toolkit Library * + * * + * Simple Example 19 * + * Author: Arash Partow (1999-2016) * + * URL: http://www.partow.net/programming/exprtk/index.html * + * * + * Copyright notice: * + * Free use of the Mathematical Expression Toolkit Library is * + * permitted under the guidelines and in accordance with the * + * most current version of the Common Public License. * + * http://www.opensource.org/licenses/cpl1.0.php * + * * + ************************************************************** +*/ + + +#include +#include +#include +#include + +#include "exprtk.hpp" + + +template +class randu : public exprtk::igeneric_function +{ +public: + + typedef typename exprtk::igeneric_function igfun_t; + typedef typename igfun_t::parameter_list_t parameter_list_t; + typedef typename igfun_t::generic_type generic_type; + typedef typename generic_type::vector_view vector_t; + + using exprtk::igeneric_function::operator(); + + randu() + : exprtk::igeneric_function("V|VTT") + /* + Overloads: + 0. V - vector + 1. VTT - vector, r0, r1 + */ + { ::srand(static_cast(time(NULL))); } + + inline T operator()(const std::size_t& ps_index, parameter_list_t parameters) + { + vector_t v(parameters[0]); + + std::size_t r0 = 0; + std::size_t r1 = v.size() - 1; + + if ( + (1 == ps_index) && + !exprtk::rtl::vecops::helper:: + load_vector_range::process(parameters,r0,r1,1,2,0) + ) + return T(0); + + for (std::size_t i = r0; i <= r1; ++i) + { + v[i] = rnd(); + } + + return T(1); + } + +private: + + inline T rnd() + { + // Note: Do not use this in production + // Result is in the interval [0,1) + return T(::rand() / T(RAND_MAX + 1.0)); + } +}; + +template +void vector_randu() +{ + typedef exprtk::symbol_table symbol_table_t; + typedef exprtk::expression expression_t; + typedef exprtk::parser parser_t; + + std::string vecrandu_program = + " var noise[6] := [0]; " + " " + " if (randu(noise,0,5) == false) " + " { " + " println('Failed to generate noise'); " + " return [false]; " + " } " + " " + " var noisy[6] := signal + (noise - 1/2); " + " " + " for (var i := 0; i < noisy[]; i += 1) " + " { " + " println('noisy[',i,'] = ', noisy[i]); " + " } " + " " + " println('avg: ', avg(noisy)); " + " "; + + T signal[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5), T(6.6), T(7.7) }; + + exprtk::rtl::io::println println; + randu randu; + + symbol_table_t symbol_table; + symbol_table.add_vector ("signal" , signal); + symbol_table.add_function("println",println); + symbol_table.add_function("randu" , randu); + + expression_t expression; + expression.register_symbol_table(symbol_table); + + parser_t parser; + parser.compile(vecrandu_program,expression); + + expression.value(); +} + +int main() +{ + vector_randu(); + return 0; +}