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. // compile time rank, 4x2 array
  20. ra::Big<float, 2> A = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  21. // rank-extending op with STL object
  22. A += std::vector {10., 20.};
  23. // negate right half
  24. A(ra::all, ra::iota(ra::len/2, ra::len/2)) *= -1;
  25. // shape is dynamic, so will be printed
  26. std::cout << "A: " << A << std::endl;
  27. }
  28. tr.section("dynamic/static shape");
  29. // Dynamic or static array rank. Dynamic or static array shape (all dimensions or none).
  30. {
  31. ra::Big<char> A({2, 3}, 'a'); // dynamic rank = 2, dynamic shape = {2, 3}
  32. ra::Big<char, 2> B({2, 3}, 'b'); // static rank = 2, dynamic shape = {2, 3}
  33. ra::Small<char, 2, 3> C('c'); // static rank = 2, static shape = {2, 3}
  34. cout << "A: " << A << "\n\n";
  35. cout << "B: " << B << "\n\n";
  36. cout << "C: " << C << "\n\n";
  37. }
  38. tr.section("storage");
  39. // Memory-owning types and views. You can make array views over any piece of memory.
  40. {
  41. // memory-owning types
  42. ra::Big<char, 2> A({2, 3}, 'a'); // storage is std::vector inside A
  43. ra::Unique<char, 2> B({2, 3}, 'b'); // storage is owned by std::unique_ptr inside B
  44. ra::Small<char, 2, 3> C('c'); // storage is owned by C itself, on the stack
  45. cout << "A: " << A << "\n\n";
  46. cout << "B: " << B << "\n\n";
  47. cout << "C: " << C << "\n\n";
  48. // view types
  49. char cs[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
  50. ra::View<char, 2> D1({2, 3}, cs); // dynamic sizes and steps, C order
  51. ra::View<char, 2> D2({{2, 1}, {3, 2}}, cs); // dynamic sizes and steps, Fortran order.
  52. ra::SmallView<char, int_list<2, 3>, int_list<3, 1>> D3(cs); // static sizes & steps, C order.
  53. ra::SmallView<char, int_list<2, 3>, int_list<1, 2>> D4(cs); // static sizes & steps, Fortran order.
  54. cout << "D1: " << D1 << "\n\n";
  55. cout << "D2: " << D2 << "\n\n";
  56. cout << "D3: " << D3 << "\n\n";
  57. cout << "D4: " << D4 << "\n\n";
  58. }
  59. tr.section("shape agreement");
  60. // Shape agreement rules and rank extension (broadcasting) for rank-0 operations of any arity
  61. // and operands of any rank, any of which can a reference (so you can write on them). These
  62. // rules are taken from the array language, J.
  63. // (See examples/agreement.cc for more examples.)
  64. {
  65. ra::Big<float, 2> A {{1, 2, 3}, {1, 2, 3}};
  66. ra::Big<float, 1> B {-1, +1};
  67. ra::Big<float, 2> C({2, 3}, 99.);
  68. C = A * B; // C(i, j) = A(i, j) * C(i)
  69. cout << "C: " << C << "\n\n";
  70. ra::Big<float, 1> D({2}, 0.);
  71. D += A * B; // D(i) += A(i, j) * C(i)
  72. cout << "D: " << D << "\n\n";
  73. }
  74. tr.section("rank iterators");
  75. // Iterators over cells of arbitrary rank.
  76. {
  77. ra::TensorIndex<0> i;
  78. ra::TensorIndex<1> j;
  79. ra::TensorIndex<2> k;
  80. ra::Big<float, 3> A({2, 3, 4}, i+j+k);
  81. ra::Big<float, 2> B({2, 3}, 0);
  82. cout << "A: " << A << "\n\n";
  83. // store the sum of A(i, j, ...) in B(i, j). All these are equivalent.
  84. B = 0; B += A; // default agreement matches prefixes
  85. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A); // default agreement matches prefixes
  86. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<1>()); // give cell rank
  87. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<-2>()); // give frame rank
  88. cout << "B: " << B << "\n\n";
  89. // 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.
  90. for_each([](auto && b, auto && a) { b = ra::sum(a); }, B, A.iter<2>()); // give cell rank
  91. cout << "B: " << B << "\n\n";
  92. }
  93. // A rank conjunction (only for static rank and somewhat fragile).
  94. tr.section("rank conjuction");
  95. {
  96. // This is a translation of J: A = (i.3) -"(0 1) i.4, that is: A(i, j) = b(i)-c(j).
  97. ra::Big<float, 2> A = map(ra::wrank<0, 1>(std::minus<float>()), ra::iota(3), ra::iota(4));
  98. cout << "A: " << A << "\n\n";
  99. }
  100. // A proper selection operator with 'beating' of range or scalar subscripts.
  101. // See examples/slicing.cc for more examples.
  102. tr.section("selector");
  103. {
  104. // TODO do implicit reshape in constructors?? so I can accept any 1-array and not only an initializer_list.
  105. ra::Big<char, 3> A({2, 2, 2}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'});
  106. cout << "A: " << A << "\n\n";
  107. // these are all equivalent to e.g. A(:, 0, :) in Octave.
  108. cout << "A1: " << A(ra::all, 0) << "\n\n";
  109. cout << "A2: " << A(ra::all, 0, ra::all) << "\n\n";
  110. cout << "A3: " << A(ra::all, 0, ra::dots<1>) << "\n\n";
  111. // an inverted range.
  112. cout << "A4: " << A(ra::iota(2, 1, -1)) << "\n\n";
  113. // indices can be arrays of any rank.
  114. ra::Big<int, 2> I {{0, 3}, {1, 2}};
  115. ra::Big<char, 1> B {'a', 'b', 'c', 'd'};
  116. cout << "B(I): " << B(I) << "\n\n";
  117. // multiple indexing performs an implicit outer product. this results in a rank
  118. // 4 array X = A(J, 1, J) -> X(i, j, k, l) = A(J(i, j), 1, J(k, l))
  119. ra::Big<int, 2> J {{1, 0}, {0, 1}};
  120. cout << "A(J, 1, J): " << A(J, 1, J) << "\n\n";
  121. // explicit indices do not result in a View view (= pointer + steps), but the
  122. // resulting expression can still be written on.
  123. B(I) = ra::Big<char, 2> {{'x', 'y'}, {'z', 'w'}};
  124. cout << "B: " << B << endl;
  125. }
  126. // A tensor-index object as in Blitz++ (with some differences).
  127. tr.section("tensorindex");
  128. {
  129. // as shown above.
  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. }