op_reshape_meat.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup op_reshape
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_reshape::apply_unwrap(Mat<eT>& out, const Mat<eT>& A, const uword in_n_rows, const uword in_n_cols, const uword in_dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. const bool is_alias = (&out == &A);
  24. const uword in_n_elem = in_n_rows * in_n_cols;
  25. if(A.n_elem == in_n_elem)
  26. {
  27. if(in_dim == 0)
  28. {
  29. if(is_alias == false)
  30. {
  31. out.set_size(in_n_rows, in_n_cols);
  32. arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
  33. }
  34. else // &out == &A, i.e. inplace reshape
  35. {
  36. out.set_size(in_n_rows, in_n_cols);
  37. // set_size() doesn't destroy data as long as the number of elements in the matrix remains the same
  38. }
  39. }
  40. else
  41. {
  42. unwrap_check< Mat<eT> > B_tmp(A, is_alias);
  43. const Mat<eT>& B = B_tmp.M;
  44. out.set_size(in_n_rows, in_n_cols);
  45. eT* out_mem = out.memptr();
  46. const uword B_n_rows = B.n_rows;
  47. const uword B_n_cols = B.n_cols;
  48. for(uword row=0; row<B_n_rows; ++row)
  49. {
  50. uword i,j;
  51. for(i=0, j=1; j < B_n_cols; i+=2, j+=2)
  52. {
  53. const eT tmp_i = B.at(row,i);
  54. const eT tmp_j = B.at(row,j);
  55. *out_mem = tmp_i; out_mem++;
  56. *out_mem = tmp_j; out_mem++;
  57. }
  58. if(i < B_n_cols)
  59. {
  60. *out_mem = B.at(row,i); out_mem++;
  61. }
  62. }
  63. }
  64. }
  65. else
  66. {
  67. const unwrap_check< Mat<eT> > B_tmp(A, is_alias);
  68. const Mat<eT>& B = B_tmp.M;
  69. const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
  70. out.set_size(in_n_rows, in_n_cols);
  71. eT* out_mem = out.memptr();
  72. if(in_dim == 0)
  73. {
  74. arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
  75. }
  76. else
  77. {
  78. uword row = 0;
  79. uword col = 0;
  80. const uword B_n_cols = B.n_cols;
  81. for(uword i=0; i<n_elem_to_copy; ++i)
  82. {
  83. out_mem[i] = B.at(row,col);
  84. ++col;
  85. if(col >= B_n_cols)
  86. {
  87. col = 0;
  88. ++row;
  89. }
  90. }
  91. }
  92. for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
  93. {
  94. out_mem[i] = eT(0);
  95. }
  96. }
  97. }
  98. template<typename T1>
  99. inline
  100. void
  101. op_reshape::apply_proxy(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const uword in_n_rows, const uword in_n_cols)
  102. {
  103. arma_extra_debug_sigprint();
  104. typedef typename T1::elem_type eT;
  105. out.set_size(in_n_rows, in_n_cols);
  106. eT* out_mem = out.memptr();
  107. const uword in_n_elem = in_n_rows * in_n_cols;
  108. if(P.get_n_elem() == in_n_elem)
  109. {
  110. if(Proxy<T1>::use_at == false)
  111. {
  112. typename Proxy<T1>::ea_type Pea = P.get_ea();
  113. for(uword i=0; i<in_n_elem; ++i)
  114. {
  115. out_mem[i] = Pea[i];
  116. }
  117. }
  118. else
  119. {
  120. const uword P_n_rows = P.get_n_rows();
  121. const uword P_n_cols = P.get_n_cols();
  122. for(uword col=0; col < P_n_cols; ++col)
  123. {
  124. uword i,j;
  125. for(i=0, j=1; j < P_n_rows; i+=2, j+=2)
  126. {
  127. const eT tmp_i = P.at(i,col);
  128. const eT tmp_j = P.at(j,col);
  129. *out_mem = tmp_i; out_mem++;
  130. *out_mem = tmp_j; out_mem++;
  131. }
  132. if(i < P_n_rows)
  133. {
  134. *out_mem = P.at(i,col); out_mem++;
  135. }
  136. }
  137. }
  138. }
  139. else
  140. {
  141. const uword n_elem_to_copy = (std::min)(P.get_n_elem(), in_n_elem);
  142. if(Proxy<T1>::use_at == false)
  143. {
  144. typename Proxy<T1>::ea_type Pea = P.get_ea();
  145. for(uword i=0; i<n_elem_to_copy; ++i)
  146. {
  147. out_mem[i] = Pea[i];
  148. }
  149. }
  150. else
  151. {
  152. uword i = 0;
  153. const uword P_n_rows = P.get_n_rows();
  154. const uword P_n_cols = P.get_n_cols();
  155. for(uword col=0; col < P_n_cols; ++col)
  156. for(uword row=0; row < P_n_rows; ++row)
  157. {
  158. if(i >= n_elem_to_copy) { goto nested_loop_end; }
  159. out_mem[i] = P.at(row,col);
  160. ++i;
  161. }
  162. nested_loop_end: ;
  163. }
  164. for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
  165. {
  166. out_mem[i] = eT(0);
  167. }
  168. }
  169. }
  170. template<typename T1>
  171. inline
  172. void
  173. op_reshape::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_reshape>& in)
  174. {
  175. arma_extra_debug_sigprint();
  176. typedef typename T1::elem_type eT;
  177. const uword in_n_rows = in.aux_uword_a;
  178. const uword in_n_cols = in.aux_uword_b;
  179. // allow detection of in-place reshape
  180. if(is_Mat<T1>::value || is_Mat<typename Proxy<T1>::stored_type>::value)
  181. {
  182. const unwrap<T1> U(in.m);
  183. op_reshape::apply_unwrap(out, U.M, in_n_rows, in_n_cols, uword(0));
  184. }
  185. else
  186. {
  187. const Proxy<T1> P(in.m);
  188. if(P.is_alias(out))
  189. {
  190. Mat<eT> tmp;
  191. op_reshape::apply_proxy(tmp, P, in_n_rows, in_n_cols);
  192. out.steal_mem(tmp);
  193. }
  194. else
  195. {
  196. op_reshape::apply_proxy(out, P, in_n_rows, in_n_cols);
  197. }
  198. }
  199. }
  200. template<typename T1>
  201. inline
  202. void
  203. op_reshape_ext::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_reshape_ext>& in)
  204. {
  205. arma_extra_debug_sigprint();
  206. const unwrap<T1> tmp(in.m);
  207. const uword in_n_rows = in.aux_uword_a;
  208. const uword in_n_cols = in.aux_uword_b;
  209. const uword in_dim = in.aux_uword_c;
  210. op_reshape::apply_unwrap(out, tmp.M, in_n_rows, in_n_cols, in_dim);
  211. }
  212. template<typename T1>
  213. inline
  214. void
  215. op_reshape_ext::apply(Cube<typename T1::elem_type>& out, const OpCube<T1,op_reshape_ext>& in)
  216. {
  217. arma_extra_debug_sigprint();
  218. typedef typename T1::elem_type eT;
  219. const unwrap_cube<T1> A_tmp(in.m);
  220. const Cube<eT>& A = A_tmp.M;
  221. const uword in_n_rows = in.aux_uword_a;
  222. const uword in_n_cols = in.aux_uword_b;
  223. const uword in_n_slices = in.aux_uword_c;
  224. const uword in_dim = in.aux_uword_d;
  225. const uword in_n_elem = in_n_rows * in_n_cols * in_n_slices;
  226. if(A.n_elem == in_n_elem)
  227. {
  228. if(in_dim == 0)
  229. {
  230. if(&out != &A)
  231. {
  232. out.set_size(in_n_rows, in_n_cols, in_n_slices);
  233. arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
  234. }
  235. else // &out == &A, i.e. inplace resize
  236. {
  237. out.set_size(in_n_rows, in_n_cols, in_n_slices);
  238. // set_size() doesn't destroy data as long as the number of elements in the cube remains the same
  239. }
  240. }
  241. else
  242. {
  243. unwrap_cube_check< Cube<eT> > B_tmp(A, out);
  244. const Cube<eT>& B = B_tmp.M;
  245. out.set_size(in_n_rows, in_n_cols, in_n_slices);
  246. eT* out_mem = out.memptr();
  247. const uword B_n_rows = B.n_rows;
  248. const uword B_n_cols = B.n_cols;
  249. const uword B_n_slices = B.n_slices;
  250. for(uword slice = 0; slice < B_n_slices; ++slice)
  251. for(uword row = 0; row < B_n_rows; ++row )
  252. for(uword col = 0; col < B_n_cols; ++col )
  253. {
  254. *out_mem = B.at(row,col,slice);
  255. out_mem++;
  256. }
  257. }
  258. }
  259. else
  260. {
  261. const unwrap_cube_check< Cube<eT> > B_tmp(A, out);
  262. const Cube<eT>& B = B_tmp.M;
  263. const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
  264. out.set_size(in_n_rows, in_n_cols, in_n_slices);
  265. eT* out_mem = out.memptr();
  266. if(in_dim == 0)
  267. {
  268. arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
  269. }
  270. else
  271. {
  272. uword row = 0;
  273. uword col = 0;
  274. uword slice = 0;
  275. const uword B_n_rows = B.n_rows;
  276. const uword B_n_cols = B.n_cols;
  277. for(uword i=0; i<n_elem_to_copy; ++i)
  278. {
  279. out_mem[i] = B.at(row,col,slice);
  280. ++col;
  281. if(col >= B_n_cols)
  282. {
  283. col = 0;
  284. ++row;
  285. if(row >= B_n_rows)
  286. {
  287. row = 0;
  288. ++slice;
  289. }
  290. }
  291. }
  292. }
  293. for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
  294. {
  295. out_mem[i] = eT(0);
  296. }
  297. }
  298. }
  299. //! @}