exprtk_simple_example_19.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 19 *
  6. * Author: Arash Partow (1999-2020) *
  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. #include <cstdio>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <string>
  21. #include "exprtk.hpp"
  22. template <typename T>
  23. class randu : public exprtk::igeneric_function<T>
  24. {
  25. public:
  26. typedef typename exprtk::igeneric_function<T> igfun_t;
  27. typedef typename igfun_t::parameter_list_t parameter_list_t;
  28. typedef typename igfun_t::generic_type generic_type;
  29. typedef typename generic_type::vector_view vector_t;
  30. using exprtk::igeneric_function<T>::operator();
  31. randu()
  32. : exprtk::igeneric_function<T>("V|VTT")
  33. /*
  34. Overloads:
  35. 0. V - vector
  36. 1. VTT - vector, r0, r1
  37. */
  38. { ::srand(static_cast<unsigned int>(time(NULL))); }
  39. inline T operator()(const std::size_t& ps_index, parameter_list_t parameters)
  40. {
  41. vector_t v(parameters[0]);
  42. std::size_t r0 = 0;
  43. std::size_t r1 = v.size() - 1;
  44. if (
  45. (1 == ps_index) &&
  46. !exprtk::rtl::vecops::helper::
  47. load_vector_range<T>::process(parameters,r0,r1,1,2,0)
  48. )
  49. return T(0);
  50. for (std::size_t i = r0; i <= r1; ++i)
  51. {
  52. v[i] = rnd();
  53. }
  54. return T(1);
  55. }
  56. private:
  57. inline T rnd()
  58. {
  59. // Note: Do not use this in production
  60. // Result is in the interval [0,1)
  61. return T(::rand() / T(RAND_MAX + 1.0));
  62. }
  63. };
  64. template <typename T>
  65. void vector_randu()
  66. {
  67. typedef exprtk::symbol_table<T> symbol_table_t;
  68. typedef exprtk::expression<T> expression_t;
  69. typedef exprtk::parser<T> parser_t;
  70. const std::string vecrandu_program =
  71. " var noise[6] := [0]; "
  72. " "
  73. " if (randu(noise,0,5) == false) "
  74. " { "
  75. " println('Failed to generate noise'); "
  76. " return [false]; "
  77. " } "
  78. " "
  79. " var noisy[6] := signal + (noise - 1/2); "
  80. " "
  81. " for (var i := 0; i < noisy[]; i += 1) "
  82. " { "
  83. " println('noisy[',i,'] = ', noisy[i]); "
  84. " } "
  85. " "
  86. " println('avg: ', avg(noisy)); "
  87. " ";
  88. T signal[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5), T(6.6), T(7.7) };
  89. exprtk::rtl::io::println<T> println;
  90. randu<T> randu;
  91. symbol_table_t symbol_table;
  92. symbol_table.add_vector ("signal" , signal);
  93. symbol_table.add_function("println",println);
  94. symbol_table.add_function("randu" , randu);
  95. expression_t expression;
  96. expression.register_symbol_table(symbol_table);
  97. parser_t parser;
  98. parser.compile(vecrandu_program,expression);
  99. expression.value();
  100. }
  101. int main()
  102. {
  103. vector_randu<double>();
  104. return 0;
  105. }