read-me.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "ra/ra.hh"
  10. #include "ra/test.hh"
  11. #include <iostream>
  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("dynamic/static shape");
  34. // Dynamic or static array rank. Dynamic or static array shape (all dimensions or none).
  35. {
  36. ra::Big<char> A({2, 3}, 'a'); // dynamic rank = 2, dynamic shape = {2, 3}
  37. ra::Big<char, 2> B({2, 3}, 'b'); // static rank = 2, dynamic shape = {2, 3}
  38. ra::Small<char, 2, 3> C('c'); // static rank = 2, static shape = {2, 3}
  39. cout << "A: " << A << "\n\n";
  40. cout << "B: " << B << "\n\n";
  41. cout << "C: " << C << "\n\n";
  42. }
  43. tr.section("storage");
  44. // Memory-owning types and views. You can make array views over any piece of memory.
  45. {
  46. // memory-owning types
  47. ra::Big<char, 2> A({2, 3}, 'a'); // storage is std::vector inside A
  48. ra::Unique<char, 2> B({2, 3}, 'b'); // storage is owned by std::unique_ptr inside B
  49. ra::Small<char, 2, 3> C('c'); // storage is owned by C itself, on the stack
  50. cout << "A: " << A << "\n\n";
  51. cout << "B: " << B << "\n\n";
  52. cout << "C: " << C << "\n\n";
  53. // view types
  54. char cs[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
  55. ra::View<char, 2> D1({2, 3}, cs); // dynamic sizes and steps, C order
  56. ra::View<char, 2> D2({{2, 1}, {3, 2}}, cs); // dynamic sizes and steps, Fortran order.
  57. ra::SmallView<char, int_list<2, 3>, int_list<3, 1>> D3(cs); // static sizes & steps, C order.
  58. ra::SmallView<char, int_list<2, 3>, int_list<1, 2>> D4(cs); // static sizes & steps, Fortran order.
  59. cout << "D1: " << D1 << "\n\n";
  60. cout << "D2: " << D2 << "\n\n";
  61. cout << "D3: " << D3 << "\n\n";
  62. cout << "D4: " << D4 << "\n\n";
  63. }
  64. tr.section("shape agreement");
  65. // Shape agreement rules and rank extension (broadcasting) for rank-0 operations of any arity
  66. // and operands of any rank, any of which can a reference (so you can write on them). These
  67. // rules are taken from the array language, J.
  68. // (See examples/agreement.cc for more examples.)
  69. {
  70. ra::Big<float, 2> A {{1, 2, 3}, {1, 2, 3}};
  71. ra::Big<float, 1> B {-1, +1};
  72. ra::Big<float, 2> C({2, 3}, 99.);
  73. C = A * B; // C(i, j) = A(i, j) * C(i)
  74. cout << "C: " << C << "\n\n";
  75. ra::Big<float, 1> D({2}, 0.);
  76. D += A * B; // D(i) += A(i, j) * C(i)
  77. cout << "D: " << D << "\n\n";
  78. }
  79. tr.section("rank iterators");
  80. // Iterators over cells of arbitrary rank.
  81. {
  82. constexpr auto i = ra::iota<0>();
  83. constexpr auto j = ra::iota<1>();
  84. constexpr auto k = ra::iota<2>();
  85. ra::Big<float, 3> A({2, 3, 4}, i+j+k);
  86. ra::Big<float, 2> B({2, 3}, 0);
  87. cout << "A: " << A << "\n\n";
  88. // store the sum of A(i, j, ...) in B(i, j). All these are equivalent.
  89. B = 0; B += A; // default agreement matches prefixes
  90. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A); // default agreement matches prefixes
  91. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<1>()); // give cell rank
  92. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<-2>()); // give frame rank
  93. cout << "B: " << B << "\n\n";
  94. // 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.
  95. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<2>()); // give cell rank
  96. cout << "B: " << B << "\n\n";
  97. }
  98. // A rank conjunction (only for static rank and somewhat fragile).
  99. tr.section("rank conjuction");
  100. {
  101. // This is a translation of J: A = (i.3) -"(0 1) i.4, that is: A(i, j) = b(i)-c(j).
  102. ra::Big<float, 2> A = map(ra::wrank<0, 1>(std::minus<float>()), ra::iota(3), ra::iota(4));
  103. cout << "A: " << A << "\n\n";
  104. }
  105. // A proper selection operator with 'beating' of range or scalar subscripts.
  106. // See examples/slicing.cc for more examples.
  107. tr.section("selector");
  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 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. tr.section("STL compat");
  132. {
  133. ra::Big<char, 1> A = {'x', 'z', 'y'};
  134. std::sort(A.begin(), A.end());
  135. cout << "A: " << A << "\n\n";
  136. ra::Big<float, 2> B {{1, 2}, {3, 4}};
  137. B += std::vector<float> {10, 20};
  138. cout << "B: " << B << "\n\n";
  139. }
  140. tr.section("example from the manual [ma100]");
  141. {
  142. ra::Small<int, 3> s {2, 1, 0};
  143. ra::Small<double, 3> z = pick(s, s*s, s+s, sqrt(s));
  144. cout << "z: " << z << endl;
  145. }
  146. tr.section("example from the manual [ma101]");
  147. {
  148. ra::Big<char, 2> A({2, 5}, "helloworld");
  149. std::cout << ra::noshape << format_array(transpose<1, 0>(A), "|") << std::endl;
  150. }
  151. {
  152. ra::Big<char const *, 1> A = {"hello", "array", "world"};
  153. std::cout << ra::noshape << format_array(A, "|") << std::endl;
  154. }
  155. tr.section("example from the manual [ma102]");
  156. {
  157. // ra::Big<char const *, 1> A({3}, "hello"); // ERROR bc of pointer constructor
  158. ra::Big<char const *, 1> A({3}, ra::scalar("hello"));
  159. std::cout << ra::noshape << format_array(A, "|") << std::endl;
  160. }
  161. tr.section("example from the manual [ma103]");
  162. {
  163. ra::Big<int, 2> A {{1, 2}, {3, 4}, {5, 6}};
  164. ra::Big<int, 2> B {{7, 8, 9}, {10, 11, 12}};
  165. ra::Big<int, 2> C({3, 3}, 0.);
  166. for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })), C, A, B);
  167. /* 3 3
  168. 27 30 33
  169. 61 68 75
  170. 95 106 117 */
  171. cout << C << endl;
  172. }
  173. tr.section("example from the manual [ma104] - dynamic size");
  174. {
  175. ra::Big<int, 3> c({3, 2, 2}, ra::_0 - ra::_1 - 2*ra::_2);
  176. cout << "c: " << c << endl;
  177. cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
  178. }
  179. tr.section("example from the manual [ma104] - static size");
  180. {
  181. ra::Small<int, 3, 2, 2> c = ra::_0 - ra::_1 - 2*ra::_2;
  182. cout << "c: " << c << endl;
  183. cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
  184. }
  185. tr.section("example from the manual [ma105]");
  186. {
  187. ra::Big<double, 2> a {{1, 2, 3}, {4, 5, 6}};
  188. ra::Big<double, 1> b {10, 20, 30};
  189. ra::Big<double, 2> c({2, 3}, 0);
  190. iter<1>(c) = iter<1>(a) * iter<1>(b); // multiply each item of a by b
  191. cout << c << endl;
  192. }
  193. // example from the manual [ma109]. This is a rare case where I need explicit ply.
  194. {
  195. ra::Big<int, 1> o = {};
  196. ra::Big<int, 1> e = {};
  197. ra::Big<int, 1> n = {1, 2, 7, 9, 12};
  198. ply(where(odd(n), map([&o](auto && x) { o.push_back(x); }, n), map([&e](auto && x) { e.push_back(x); }, n)));
  199. cout << "o: " << ra::noshape << o << ", e: " << ra::noshape << e << endl;
  200. }
  201. tr.section("example from manual [ma110]");
  202. {
  203. std::cout << exp(ra::Small<double, 3> {4, 5, 6}) << std::endl;
  204. }
  205. tr.section("example from manual [ma111]");
  206. {
  207. ra::Small<int, 2, 2> a = {{1, 2}, {3, 4}}; // explicit contents
  208. ra::Small<int, 2, 2> b = {1, 2, 3, 4}; // ravel of content
  209. cout << "a: " << a << ", b: " << b << endl;
  210. }
  211. tr.section("example from manual [ma112]");
  212. {
  213. double bx[6] = {1, 2, 3, 4, 5, 6};
  214. ra::Big<double, 2> b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
  215. cout << "b: " << b << endl;
  216. }
  217. tr.section("example from manual [ma114]");
  218. {
  219. using sizes = int_list<2, 3>;
  220. using steps = int_list<1, 2>;
  221. ra::SmallArray<int, sizes, steps> a {{1, 2, 3}, {4, 5, 6}}; // stored column-major
  222. cout << "a: " << a << endl;
  223. cout << ra::Small<int, 6>(ra::ptr(a.data())) << endl;
  224. }
  225. tr.section("example from manual [ma116]");
  226. {
  227. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  228. ra::Big<int, 1> x = {1, 10};
  229. cout << (x(ra::all, ra::insert<2>) * a(ra::insert<1>)) << endl;
  230. cout << (x * a(ra::insert<1>)) << endl; // same thing
  231. }
  232. return 0;
  233. }