view-ops.hh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Operations specific to Views.
  3. // (c) Daniel Llorens - 2013-2014, 2017
  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. #pragma once
  9. #include "big.hh"
  10. #include <complex>
  11. namespace ra {
  12. template <class T, rank_t RANK> inline
  13. View<T, RANK> reverse(View<T, RANK> const & view, int k)
  14. {
  15. View<T, RANK> r = view;
  16. auto & dim = r.dimv[k];
  17. if (dim.len!=0) {
  18. r.p += dim.step*(dim.len-1);
  19. dim.step *= -1;
  20. }
  21. return r;
  22. }
  23. // dynamic transposed axes list.
  24. template <class T, rank_t RANK, class S> inline
  25. View<T, RANK_ANY> transpose_(S && s, View<T, RANK> const & view)
  26. {
  27. RA_CHECK(view.rank()==ra::size(s));
  28. auto rp = std::max_element(s.begin(), s.end());
  29. rank_t dstrank = (rp==s.end() ? 0 : *rp+1);
  30. View<T, RANK_ANY> r { decltype(r.dimv)(dstrank, Dim { DIM_BAD, 0 }), view.data() };
  31. for (int k=0; int sk: s) {
  32. Dim & dest = r.dimv[sk];
  33. dest.step += view.dimv[k].step;
  34. dest.len = dest.len>=0 ? std::min(dest.len, view.dimv[k].len) : view.dimv[k].len;
  35. ++k;
  36. }
  37. return r;
  38. }
  39. template <class T, rank_t RANK, class S> inline
  40. View<T, RANK_ANY> transpose(S && s, View<T, RANK> const & view)
  41. {
  42. return transpose_(std::forward<S>(s), view);
  43. }
  44. // Note that we need the compile time values and not the sizes to deduce the rank of the output, so it would be useless to provide a builtin array shim as we do with reshape().
  45. template <class T, rank_t RANK> inline
  46. View<T, RANK_ANY> transpose(std::initializer_list<ra::rank_t> s, View<T, RANK> const & view)
  47. {
  48. return transpose_(s, view);
  49. }
  50. // static transposed axes list.
  51. template <int ... Iarg, class T, rank_t RANK> inline
  52. auto transpose(View<T, RANK> const & view)
  53. {
  54. static_assert(RANK==RANK_ANY || RANK==sizeof...(Iarg), "Bad output rank.");
  55. RA_CHECK(view.rank()==sizeof...(Iarg), "Bad output rank: ", view.rank(), "should be ", (sizeof...(Iarg)), ".");
  56. using dummy_s = mp::makelist<sizeof...(Iarg), mp::int_c<0>>;
  57. using ti = axes_list_indices<mp::int_list<Iarg ...>, dummy_s, dummy_s>;
  58. constexpr rank_t DSTRANK = mp::len<typename ti::dst>;
  59. View<T, DSTRANK> r { decltype(r.dimv)(Dim { DIM_BAD, 0 }), view.data() };
  60. std::array<int, sizeof...(Iarg)> s {{ Iarg ... }};
  61. for (int k=0; int sk: s) {
  62. Dim & dest = r.dimv[sk];
  63. dest.step += view.dimv[k].step;
  64. dest.len = dest.len>=0 ? std::min(dest.len, view.dimv[k].len) : view.dimv[k].len;
  65. ++k;
  66. }
  67. return r;
  68. }
  69. template <class T, rank_t RANK> inline
  70. auto diag(View<T, RANK> const & view)
  71. {
  72. return transpose<0, 0>(view);
  73. }
  74. template <class T, rank_t RANK> inline
  75. bool is_ravel_free(View<T, RANK> const & a)
  76. {
  77. int r = a.rank()-1;
  78. for (; r>=0 && a.len(r)==1; --r) {}
  79. if (r<0) { return true; }
  80. ra::dim_t s = a.step(r)*a.len(r);
  81. while (--r>=0) {
  82. if (1!=a.len(r)) {
  83. if (a.step(r)!=s) {
  84. return false;
  85. }
  86. s *= a.len(r);
  87. }
  88. }
  89. return true;
  90. }
  91. template <class T, rank_t RANK> inline
  92. View<T, 1> ravel_free(View<T, RANK> const & a)
  93. {
  94. RA_CHECK(is_ravel_free(a));
  95. int r = a.rank()-1;
  96. for (; r>=0 && a.len(r)==1; --r) {}
  97. ra::dim_t s = r<0 ? 1 : a.step(r);
  98. return ra::View<T, 1>({{size(a), s}}, a.p);
  99. }
  100. template <class T, rank_t RANK, class S> inline
  101. auto reshape_(View<T, RANK> const & a, S && sb_)
  102. {
  103. auto sb = concrete(std::forward<S>(sb_));
  104. // FIXME when we need to copy, accept/return Shared
  105. dim_t la = ra::size(a);
  106. dim_t lb = 1;
  107. for (int i=0; i<ra::size(sb); ++i) {
  108. if (sb[i]==-1) {
  109. dim_t quot = lb;
  110. for (int j=i+1; j<ra::size(sb); ++j) {
  111. quot *= sb[j];
  112. RA_CHECK(quot>0, "Cannot deduce dimensions.");
  113. }
  114. auto pv = la/quot;
  115. RA_CHECK((la%quot==0 && pv>=0), "Bad placeholder.");
  116. sb[i] = pv;
  117. lb = la;
  118. break;
  119. } else {
  120. lb *= sb[i];
  121. }
  122. }
  123. auto sa = shape(a);
  124. // FIXME should be able to reshape Scalar etc.
  125. View<T, ra::size_s(sb)> b(map([](auto i) { return Dim { DIM_BAD, 0 }; }, ra::iota(ra::size(sb))), a.data());
  126. rank_t i = 0;
  127. for (; i<a.rank() && i<b.rank(); ++i) {
  128. if (sa[a.rank()-i-1]!=sb[b.rank()-i-1]) {
  129. assert(is_ravel_free(a) && "reshape w/copy not implemented");
  130. if (la>=lb) {
  131. // FIXME View(SS const & s, T * p). Cf [ra37].
  132. b.filldim(sb);
  133. for (int j=0; j!=b.rank(); ++j) {
  134. b.dimv[j].step *= a.step(a.rank()-1);
  135. }
  136. return b;
  137. } else {
  138. assert(0 && "reshape case not implemented");
  139. }
  140. } else {
  141. // select
  142. b.dimv[b.rank()-i-1] = a.dimv[a.rank()-i-1];
  143. }
  144. }
  145. if (i==a.rank()) {
  146. // tile & return
  147. for (rank_t j=i; j<b.rank(); ++j) {
  148. b.dimv[b.rank()-j-1] = { sb[b.rank()-j-1], 0 };
  149. }
  150. }
  151. return b;
  152. }
  153. template <class T, rank_t RANK, class S> inline
  154. auto reshape(View<T, RANK> const & a, S && sb_)
  155. {
  156. return reshape_(a, std::forward<S>(sb_));
  157. }
  158. // We need dimtype bc {1, ...} deduces to int and that fails to match ra::dim_t.
  159. // We could use initializer_list to handle the general case, but that would produce a var rank result because its size cannot be deduced at compile time :-/. Unfortunately an initializer_list specialization would override this one, so we cannot provide it as a fallback.
  160. template <class T, rank_t RANK, class dimtype, int N> inline
  161. auto reshape(View<T, RANK> const & a, dimtype const (&sb_)[N])
  162. {
  163. return reshape_(a, sb_);
  164. }
  165. // lo = lower bounds, hi = upper bounds.
  166. // The stencil indices are in [0 lo+1+hi] = [-lo +hi].
  167. template <class LO, class HI, class T, rank_t N> inline
  168. View<T, rank_sum(N, N)>
  169. stencil(View<T, N> const & a, LO && lo, HI && hi)
  170. {
  171. View<T, rank_sum(N, N)> s;
  172. s.p = a.data();
  173. ra::resize(s.dimv, 2*a.rank());
  174. RA_CHECK(every(lo>=0));
  175. RA_CHECK(every(hi>=0));
  176. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  177. {
  178. RA_CHECK(dima.len>=lo+hi, "Stencil is too large for array.");
  179. dims = {dima.len-lo-hi, dima.step};
  180. },
  181. ptr(s.dimv.data()), a.dimv, lo, hi);
  182. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  183. { dims = {lo+hi+1, dima.step}; },
  184. ptr(s.dimv.data()+a.rank()), a.dimv, lo, hi);
  185. return s;
  186. }
  187. // Make last sizes of View<> be compile-time constants.
  188. template <class super_t, rank_t SUPERR, class T, rank_t RANK> inline
  189. auto explode_(View<T, RANK> const & a)
  190. {
  191. // TODO Reduce to single check, either the first or the second.
  192. static_assert(RANK>=SUPERR || RANK==RANK_ANY, "rank of a is too low");
  193. RA_CHECK(a.rank()>=SUPERR, "Rank of a ", a.rank(), " should be at least ", SUPERR, ".");
  194. View<super_t, rank_sum(RANK, -SUPERR)> b;
  195. ra::resize(b.dimv, a.rank()-SUPERR);
  196. dim_t r = 1;
  197. for (int i=0; i<SUPERR; ++i) {
  198. r *= a.len(i+b.rank());
  199. }
  200. RA_CHECK(r*sizeof(T)==sizeof(super_t), "Length of axes ", r*sizeof(T), " doesn't match type ", sizeof(super_t), ".");
  201. for (int i=0; i<b.rank(); ++i) {
  202. RA_CHECK(a.step(i) % r==0, "Step of axes ", a.step(i), " doesn't match type ", r, " on axis ", i, ".");
  203. b.dimv[i] = { .len = a.len(i), .step = a.step(i) / r };
  204. }
  205. RA_CHECK((b.rank()==0 || a.step(b.rank()-1)==r), "Super type is not compact in array.");
  206. b.p = reinterpret_cast<super_t *>(a.data());
  207. return b;
  208. }
  209. template <class super_t, class T, rank_t RANK> inline
  210. auto explode(View<T, RANK> const & a)
  211. {
  212. return explode_<super_t, (std::is_same_v<super_t, std::complex<T>> ? 1 : rank_s<super_t>())>(a);
  213. }
  214. // FIXME Consider these in as namespace level generics in atom.hh
  215. template <class T> inline int gstep(int i) { if constexpr (is_scalar<T>) return 1; else return T::step(i); }
  216. template <class T> inline int glen(int i) { if constexpr (is_scalar<T>) return 1; else return T::len(i); }
  217. // TODO This routine is not totally safe; the ranks below SUBR must be compact, which is not checked.
  218. template <class sub_t, class super_t, rank_t RANK> inline
  219. auto collapse(View<super_t, RANK> const & a)
  220. {
  221. using super_v = value_t<super_t>;
  222. using sub_v = value_t<sub_t>;
  223. constexpr int subtype = sizeof(super_v)/sizeof(sub_t);
  224. constexpr int SUBR = rank_s<super_t>() - rank_s<sub_t>();
  225. View<sub_t, rank_sum(RANK, SUBR+int(subtype>1))> b;
  226. resize(b.dimv, a.rank()+SUBR+int(subtype>1));
  227. constexpr dim_t r = sizeof(super_t)/sizeof(sub_t);
  228. static_assert(sizeof(super_t)==r*sizeof(sub_t), "cannot make axis of super_t from sub_t");
  229. for (int i=0; i<a.rank(); ++i) {
  230. b.dimv[i] = { .len = a.len(i), .step = a.step(i) * r };
  231. }
  232. constexpr int t = sizeof(super_v)/sizeof(sub_v);
  233. constexpr int s = sizeof(sub_t)/sizeof(sub_v);
  234. static_assert(t*sizeof(sub_v)>=1, "bad subtype");
  235. for (int i=0; i<SUBR; ++i) {
  236. RA_CHECK(((gstep<super_t>(i)/s)*s==gstep<super_t>(i)), "Bad steps."); // TODO is actually static
  237. b.dimv[a.rank()+i] = { .len = glen<super_t>(i), .step = gstep<super_t>(i) / s * t };
  238. }
  239. if (subtype>1) {
  240. b.dimv[a.rank()+SUBR] = { .len = t, .step = 1 };
  241. }
  242. b.p = reinterpret_cast<sub_t *>(a.data());
  243. return b;
  244. }
  245. // For functions that require compact arrays (TODO they really shouldn't).
  246. template <class A> inline
  247. bool const crm(A const & a)
  248. {
  249. return ra::size(a)==0 || is_c_order(a);
  250. }
  251. } // namespace ra