exprtk_simple_example_06.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 6 *
  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 <string>
  19. #include "exprtk.hpp"
  20. template <typename T>
  21. void vector_function()
  22. {
  23. typedef exprtk::symbol_table<T> symbol_table_t;
  24. typedef exprtk::expression<T> expression_t;
  25. typedef exprtk::parser<T> parser_t;
  26. const std::string expression_string =
  27. " for (var i := 0; i < min(x[],y[],z[]); i += 1) "
  28. " { "
  29. " z[i] := 3sin(x[i]) + 2log(y[i]); "
  30. " } ";
  31. T x[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5) };
  32. T y[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5) };
  33. T z[] = { T(0.0), T(0.0), T(0.0), T(0.0), T(0.0) };
  34. symbol_table_t symbol_table;
  35. symbol_table.add_vector("x",x);
  36. symbol_table.add_vector("y",y);
  37. symbol_table.add_vector("z",z);
  38. expression_t expression;
  39. expression.register_symbol_table(symbol_table);
  40. parser_t parser;
  41. parser.compile(expression_string,expression);
  42. expression.value();
  43. }
  44. int main()
  45. {
  46. vector_function<double>();
  47. return 0;
  48. }