useret.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/examples - User defined expression templates
  3. // Daniel Llorens - 2015
  4. // Adapted from blitz++/examples/useret.cpp
  5. // Blitz++'s ETs have a static applicator, so declaring new operations requires
  6. // a new class. Thanks to C++14, declaring new ET operations is trivial in
  7. // ra:: — just declare a function.
  8. #include "ra/ra.hh"
  9. #include <iostream>
  10. using std::cout, std::endl, std::flush;
  11. double myFunction(double x)
  12. {
  13. return 1.0 / (1 + x);
  14. }
  15. double foobar(double x, double y)
  16. {
  17. return x*y;
  18. }
  19. template <class A>
  20. auto myFunction_ra(A && a)
  21. {
  22. return ra::map(myFunction, std::forward<A>(a));
  23. }
  24. template <class A, class B>
  25. auto foobar_ra(A && a, B && b)
  26. {
  27. return ra::map(foobar, std::forward<A>(a), std::forward<B>(b));
  28. }
  29. int main()
  30. {
  31. ra::Big<double,2> A({4, 4}, 0.), B({4, 4}, 0.), C({4, 4}, 0.);
  32. A = { 0, 1, 2, 3,
  33. 4, 5, 6, 7,
  34. 8, 9, 10, 11,
  35. 12, 13, 14, 15 };
  36. C = 3;
  37. cout << "A = " << A << endl;
  38. cout << "C = " << C << endl;
  39. B = myFunction_ra(A);
  40. cout << "B = myFunction(A) = " << B << endl;
  41. B = foobar_ra(A, C);
  42. cout << "B = foobar(A, C) = " << B << endl;
  43. B = foobar_ra(A, 1.);
  44. cout << "B = foobar(A, 1.) = " << B << endl;
  45. B = foobar_ra(ra::_0, ra::_1);
  46. cout << "B = foobar(tensor::i, tensor::j) = " << B << endl;
  47. }