read-me.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/examples - Examples used in top-level README.md
  3. // (c) Daniel Llorens - 2016
  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. // TODO Generate README.md and/or these examples.
  9. #include <iostream>
  10. #include <numeric>
  11. #include "ra/test.hh" // ra/ra.hh without TestRecorder
  12. using std::cout, std::endl, ra::TestRecorder;
  13. using ra::mp::int_list;
  14. int main()
  15. {
  16. TestRecorder tr(std::cout);
  17. tr.section("First example");
  18. {
  19. // run time rank
  20. ra::Big<float> A = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  21. // static rank, run time dimensions
  22. ra::Big<float, 2> B = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  23. // static dimensions
  24. ra::Small<float, 2, 4> C = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  25. // rank-extending op with STL object
  26. B += A + C + std::vector {100., 200.};
  27. // negate right half
  28. B(ra::all, ra::iota(ra::len/2, ra::len/2)) *= -1;
  29. // shape is dynamic, so will be printed
  30. std::cout << "B: " << B << std::endl;
  31. tr.test_eq(ra::Small<float, 2, 4> { {103, 106, -109, -112}, {215, 218, -221, -224} }, B);
  32. }
  33. tr.section("Most things are constexpr");
  34. {
  35. constexpr ra::Small<int, 3> a = { 1, 2, 3 };
  36. static_assert(6==ra::sum(a));
  37. }
  38. tr.section("Dynamic or static array rank. Dynamic or static array shape (all dimensions or none)");
  39. {
  40. ra::Big<char> A({2, 3}, 'a'); // dynamic rank = 2, dynamic shape = {2, 3}
  41. ra::Big<char, 2> B({2, 3}, 'b'); // static rank = 2, dynamic shape = {2, 3}
  42. ra::Small<char, 2, 3> C('c'); // static rank = 2, static shape = {2, 3}
  43. cout << "A: " << A << "\n\n";
  44. cout << "B: " << B << "\n\n";
  45. cout << "C: " << C << "\n\n";
  46. }
  47. tr.section("Memory-owning types and views. You can make array views over any piece of memory");
  48. {
  49. // memory-owning types
  50. ra::Big<char, 2> A({2, 3}, 'a'); // storage is std::vector inside A
  51. ra::Unique<char, 2> B({2, 3}, 'b'); // storage is owned by std::unique_ptr inside B
  52. ra::Small<char, 2, 3> C('c'); // storage is owned by C itself, on the stack
  53. cout << "A: " << A << "\n\n";
  54. cout << "B: " << B << "\n\n";
  55. cout << "C: " << C << "\n\n";
  56. // view types
  57. char cs[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
  58. ra::ViewBig<char, 2> D1({2, 3}, cs); // dynamic sizes and steps, C order
  59. ra::ViewBig<char, 2> D2({{2, 1}, {3, 2}}, cs); // dynamic sizes and steps, Fortran order.
  60. ra::ViewSmall<char, int_list<2, 3>, int_list<3, 1>> D3(cs); // static sizes & steps, C order.
  61. ra::ViewSmall<char, int_list<2, 3>, int_list<1, 2>> D4(cs); // static sizes & steps, Fortran order.
  62. cout << "D1: " << D1 << "\n\n";
  63. cout << "D2: " << D2 << "\n\n";
  64. cout << "D3: " << D3 << "\n\n";
  65. cout << "D4: " << D4 << "\n\n";
  66. }
  67. tr.section("Shape agreement");
  68. // Shape agreement rules and rank extension (broadcasting) for rank-0 operations of any arity
  69. // and operands of any rank, any of which can a reference (so you can write on them). These
  70. // rules are taken from the array language, J.
  71. // (See examples/agreement.cc for more examples.)
  72. {
  73. ra::Big<float, 2> A {{1, 2, 3}, {1, 2, 3}};
  74. ra::Big<float, 1> B {-1, +1};
  75. ra::Big<float, 2> C({2, 3}, 99.);
  76. C = A * B; // C(i, j) = A(i, j) * C(i)
  77. cout << "C: " << C << "\n\n";
  78. ra::Big<float, 1> D({2}, 0.);
  79. D += A * B; // D(i) += A(i, j) * C(i)
  80. cout << "D: " << D << "\n\n";
  81. }
  82. tr.section("Iterators over cells of arbitrary rank");
  83. {
  84. constexpr auto i = ra::iota<0>();
  85. constexpr auto j = ra::iota<1>();
  86. constexpr auto k = ra::iota<2>();
  87. ra::Big<float, 3> A({2, 3, 4}, i+j+k);
  88. ra::Big<float, 2> B({2, 3}, 0);
  89. cout << "A: " << A << "\n\n";
  90. // store the sum of A(i, j, ...) in B(i, j). All these are equivalent.
  91. B = 0; B += A; // default agreement matches prefixes
  92. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A); // default agreement matches prefixes
  93. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<1>()); // give cell rank
  94. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<-2>()); // give frame rank
  95. cout << "B: " << B << "\n\n";
  96. // store the sum of A(i, ...) in B(i, j). The op is re-executed for each j, so don't do it this way.
  97. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<2>()); // give cell rank
  98. cout << "B: " << B << "\n\n";
  99. }
  100. tr.section("A rank conjunction (only for static rank and somewhat fragile)");
  101. {
  102. // This is a translation of J: A = (i.3) -"(0 1) i.4, that is: A(i, j) = b(i)-c(j).
  103. ra::Big<float, 2> A = map(ra::wrank<0, 1>(std::minus<float>()), ra::iota(3), ra::iota(4));
  104. cout << "A: " << A << "\n\n";
  105. }
  106. // See examples/slicing.cc for more examples.
  107. tr.section("A proper selection operator with 'beating' of range or scalar subscripts.");
  108. {
  109. // TODO do implicit reshape in constructors?? so I can accept any 1-array and not only an initializer_list.
  110. ra::Big<char, 3> A({2, 2, 2}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'});
  111. cout << "A: " << A << "\n\n";
  112. // these are all equivalent to e.g. A(:, 0, :) in Octave.
  113. cout << "A1: " << A(ra::all, 0) << "\n\n";
  114. cout << "A2: " << A(ra::all, 0, ra::all) << "\n\n";
  115. cout << "A3: " << A(ra::all, 0, ra::dots<1>) << "\n\n";
  116. // an inverted range.
  117. cout << "A4: " << A(ra::iota(2, 1, -1)) << "\n\n";
  118. // indices can be arrays of any rank.
  119. ra::Big<int, 2> I {{0, 3}, {1, 2}};
  120. ra::Big<char, 1> B {'a', 'b', 'c', 'd'};
  121. cout << "B(I): " << B(I) << "\n\n";
  122. // multiple indexing performs an implicit outer product. this results in a rank
  123. // 4 array X = A(J, 1, J) -> X(i, j, k, l) = A(J(i, j), 1, J(k, l))
  124. ra::Big<int, 2> J {{1, 0}, {0, 1}};
  125. cout << "A(J, 1, J): " << A(J, 1, J) << "\n\n";
  126. // explicit indices do not result in a view (= pointer + steps), but the
  127. // resulting expression can still be written on.
  128. B(I) = ra::Big<char, 2> {{'x', 'y'}, {'z', 'w'}};
  129. cout << "B: " << B << endl;
  130. }
  131. // FIXME bring in some examples from test/stl-compat.cc. Show examples both ways.
  132. tr.section("STL compatibility");
  133. {
  134. ra::Big<char, 1> A = {'x', 'z', 'y'};
  135. std::sort(A.begin(), A.end());
  136. cout << "A: " << A << "\n\n";
  137. tr.test_eq(ra::start({'x', 'y', 'z'}), A);
  138. A = {'x', 'z', 'y'};
  139. std::sort(begin(A), end(A));
  140. cout << "A: " << A << "\n\n";
  141. tr.test_eq(ra::start({'x', 'y', 'z'}), A);
  142. }
  143. {
  144. ra::Big<float, 2> B {{1, 2}, {3, 4}};
  145. B += std::vector<float> {10, 20};
  146. cout << "B: " << B << "\n\n";
  147. tr.test_eq(ra::Big<float, 2> {{11, 12}, {23, 24}}, B);
  148. }
  149. tr.section("Example from the manual [ma100]");
  150. {
  151. ra::Small<int, 3> s {2, 1, 0};
  152. ra::Small<double, 3> z = pick(s, s*s, s+s, sqrt(s));
  153. cout << "z: " << z << endl;
  154. }
  155. tr.section("Example from the manual [ma101]");
  156. {
  157. ra::Big<char, 2> A({2, 5}, "helloworld");
  158. std::cout << ra::noshape << format_array(transpose<1, 0>(A), { .sep0="|" }) << std::endl;
  159. }
  160. {
  161. ra::Big<char const *, 1> A = {"hello", "array", "world"};
  162. std::cout << ra::noshape << format_array(A, { .sep0="|" }) << std::endl;
  163. }
  164. tr.section("Example from the manual [ma102]");
  165. {
  166. // ra::Big<char const *, 1> A({3}, "hello"); // ERROR bc of pointer constructor
  167. ra::Big<char const *, 1> A({3}, ra::scalar("hello"));
  168. std::cout << ra::noshape << format_array(A, { .sep0="|" }) << std::endl;
  169. }
  170. tr.section("Example from the manual [ma103]");
  171. {
  172. ra::Big<int, 2> A {{1, 2}, {3, 4}, {5, 6}};
  173. ra::Big<int, 2> B {{7, 8, 9}, {10, 11, 12}};
  174. ra::Big<int, 2> C({3, 3}, 0.);
  175. for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })), C, A, B);
  176. /* 3 3
  177. 27 30 33
  178. 61 68 75
  179. 95 106 117 */
  180. cout << C << endl;
  181. }
  182. tr.section("Example from the manual [ma104] - dynamic size");
  183. {
  184. ra::Big<int, 3> c({3, 2, 2}, ra::_0 - ra::_1 - 2*ra::_2);
  185. cout << "c: " << c << endl;
  186. cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
  187. }
  188. tr.section("Example from the manual [ma104] - static size");
  189. {
  190. ra::Small<int, 3, 2, 2> c = ra::_0 - ra::_1 - 2*ra::_2;
  191. cout << "c: " << c << endl;
  192. cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
  193. }
  194. tr.section("Example from the manual [ma105]");
  195. {
  196. ra::Big<double, 2> a {{1, 2, 3}, {4, 5, 6}};
  197. ra::Big<double, 1> b {10, 20, 30};
  198. ra::Big<double, 2> c({2, 3}, 0);
  199. iter<1>(c) = iter<1>(a) * iter<1>(b); // multiply each item of a by b
  200. cout << c << endl;
  201. }
  202. tr.section("Example from the manual [ma109]. Contrived to need explicit ply");
  203. {
  204. ra::Big<int, 1> o = {};
  205. ra::Big<int, 1> e = {};
  206. ra::Big<int, 1> n = {1, 2, 7, 9, 12};
  207. ply(where(odd(n), map([&o](auto && x) { o.push_back(x); }, n), map([&e](auto && x) { e.push_back(x); }, n)));
  208. cout << "o: " << ra::noshape << o << ", e: " << ra::noshape << e << endl;
  209. }
  210. tr.section("Example from manual [ma110]");
  211. {
  212. std::cout << exp(ra::Small<double, 3> {4, 5, 6}) << std::endl;
  213. }
  214. tr.section("Example from manual [ma111]");
  215. {
  216. ra::Small<int, 2, 2> a = {{1, 2}, {3, 4}}; // explicit contents
  217. ra::Small<int, 2, 2> b = {1, 2, 3, 4}; // ravel of content
  218. cout << "a: " << a << ", b: " << b << endl;
  219. }
  220. tr.section("Example from manual [ma112]");
  221. {
  222. double bx[6] = {1, 2, 3, 4, 5, 6};
  223. ra::Big<double, 2> b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
  224. cout << "b: " << b << endl;
  225. }
  226. tr.section("Example from manual [ma114]");
  227. {
  228. using sizes = int_list<2, 3>;
  229. using steps = int_list<1, 2>;
  230. ra::SmallArray<int, sizes, steps> a {{1, 2, 3}, {4, 5, 6}}; // stored column-major
  231. cout << "a: " << a << endl;
  232. cout << ra::Small<int, 6>(ra::ptr(a.data())) << endl;
  233. }
  234. tr.section("Example from manual [ma116]");
  235. {
  236. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  237. ra::Big<int, 1> x = {1, 10};
  238. cout << (x(ra::all, ra::insert<2>) * a(ra::insert<1>)) << endl;
  239. cout << (x * a(ra::insert<1>)) << endl; // same thing
  240. }
  241. tr.section("Examples from manual [ma118]");
  242. {
  243. ra::Big<int, 2> A = {{3, 0, 0}, {4, 5, 6}, {0, 5, 6}};
  244. tr.test_eq(sum(A), std::accumulate(ra::begin(A), ra::end(A), 0));
  245. tr.test_eq(sum(cast<int>(A>3)), std::ranges::count(range(A>3), true));
  246. // count rows with 0s in them
  247. tr.test_eq(2, std::ranges::count_if(range(iter<1>(A)), [](auto const & x) { return any(x==0); }));
  248. }
  249. return tr.summary();
  250. }