useret.C 1.3 KB

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