reduction-1.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Regression for namespace issues surfaced in saveload branch
  3. // (c) Daniel Llorens - 2014-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. #include "mpdebug.hh"
  12. using std::cout, std::endl, std::flush, std::tuple, ra::TestRecorder;
  13. using real = double;
  14. using complex = std::complex<double>;
  15. int main()
  16. {
  17. TestRecorder tr(std::cout);
  18. tr.section("reductions with references I");
  19. {
  20. ra::Big<real, 2> c = {{1, 3, 2}, {7, 1, 3}};
  21. ra::Big<real, 1> m(3, 0);
  22. iter<1>(m) = iter<1>(m) + iter<1>(c); // works
  23. tr.info("sum of columns [ma113]").test_eq(ra::Big<double, 1> {8, 4, 5}, m);
  24. m = 0;
  25. iter<1>(m) = max(iter<1>(m), iter<1>(c)); // works also
  26. tr.info("max of columns [ma113]").test_eq(ra::Big<double, 1> {7, 3, 3}, m);
  27. // using std::max within ra:: caused std::max() to grab T = View when CellBig returned View const &.
  28. // that's why ra::max is declared with DEF_NAME_OP_FWD.
  29. auto c1 = iter<1>(c);
  30. auto m1 = iter<1>(m);
  31. cout << ra::max(*c1, *m1) << endl;
  32. }
  33. tr.section("reductions with references II");
  34. {
  35. ra::Big<complex, 2> c = {{1, 3, 2}, {7, 1, 3}};
  36. ra::Big<complex, 1> m(3, 0);
  37. m = 0;
  38. map([](auto && ... a) -> decltype(auto) { return real_part(RA_FWD(a) ...); }, iter<1>(m))
  39. = map([](auto && ... a) -> decltype(auto) { return real_part(RA_FWD(a) ...); }, iter<1>(c));
  40. tr.info("max of columns [ma113]").test_eq(ra::Big<double, 1> {7, 1, 3}, m);
  41. // still works
  42. m = 0;
  43. iter<1>(m) = real_part(iter<1>(c));
  44. tr.info("max of columns [ma113]").test_eq(ra::Big<double, 1> {7, 1, 3}, m);
  45. // should be the same FIXME doesn't work, and didn't work with the flat() interface either.
  46. // m = 0;
  47. // real_part(iter<1>(m)) = real_part(iter<1>(c));
  48. // tr.info("max of columns [ma113]").test_eq(ra::Big<double, 1> {7, 1, 3}, m);
  49. }
  50. tr.section("reductions with references III");
  51. {
  52. ra::Big<int, 2> c = {{1, 3, 2}, {7, 1, 3}};
  53. ra::Big<int, 1> m(3, 0);
  54. scalar(m) = max(scalar(m), iter<1>(c)); // requires inner forward as in ra.hh DEF_NAME_OP
  55. tr.info("max of columns I").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  56. }
  57. return tr.summary();
  58. }