glue_join_meat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 glue_join
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_join_cols::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& A, const Proxy<T2>& B)
  21. {
  22. arma_extra_debug_sigprint();
  23. const uword A_n_rows = A.get_n_rows();
  24. const uword A_n_cols = A.get_n_cols();
  25. const uword B_n_rows = B.get_n_rows();
  26. const uword B_n_cols = B.get_n_cols();
  27. arma_debug_check
  28. (
  29. ( (A_n_cols != B_n_cols) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
  30. "join_cols() / join_vert(): number of columns must be the same"
  31. );
  32. out.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
  33. if( out.n_elem > 0 )
  34. {
  35. if(A.get_n_elem() > 0)
  36. {
  37. out.submat(0, 0, A_n_rows-1, out.n_cols-1) = A.Q;
  38. }
  39. if(B.get_n_elem() > 0)
  40. {
  41. out.submat(A_n_rows, 0, out.n_rows-1, out.n_cols-1) = B.Q;
  42. }
  43. }
  44. }
  45. template<typename T1, typename T2>
  46. inline
  47. void
  48. glue_join_cols::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join_cols>& X)
  49. {
  50. arma_extra_debug_sigprint();
  51. typedef typename T1::elem_type eT;
  52. const Proxy<T1> A(X.A);
  53. const Proxy<T2> B(X.B);
  54. if( (A.is_alias(out) == false) && (B.is_alias(out) == false) )
  55. {
  56. glue_join_cols::apply_noalias(out, A, B);
  57. }
  58. else
  59. {
  60. Mat<eT> tmp;
  61. glue_join_cols::apply_noalias(tmp, A, B);
  62. out.steal_mem(tmp);
  63. }
  64. }
  65. template<typename eT, typename T1, typename T2, typename T3>
  66. inline
  67. void
  68. glue_join_cols::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr)
  69. {
  70. arma_extra_debug_sigprint();
  71. const quasi_unwrap<T1> UA(A_expr.get_ref());
  72. const quasi_unwrap<T2> UB(B_expr.get_ref());
  73. const quasi_unwrap<T3> UC(C_expr.get_ref());
  74. const Mat<eT>& A = UA.M;
  75. const Mat<eT>& B = UB.M;
  76. const Mat<eT>& C = UC.M;
  77. const uword out_n_rows = A.n_rows + B.n_rows + C.n_rows;
  78. const uword out_n_cols = (std::max)((std::max)(A.n_cols, B.n_cols), C.n_cols);
  79. arma_debug_check( ((A.n_cols != out_n_cols) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  80. arma_debug_check( ((B.n_cols != out_n_cols) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  81. arma_debug_check( ((C.n_cols != out_n_cols) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  82. out.set_size(out_n_rows, out_n_cols);
  83. if(out.n_elem == 0) { return; }
  84. uword row_start = 0;
  85. uword row_end_p1 = 0;
  86. if(A.n_elem > 0) { row_end_p1 += A.n_rows; out.rows(row_start, row_end_p1 - 1) = A; }
  87. row_start = row_end_p1;
  88. if(B.n_elem > 0) { row_end_p1 += B.n_rows; out.rows(row_start, row_end_p1 - 1) = B; }
  89. row_start = row_end_p1;
  90. if(C.n_elem > 0) { row_end_p1 += C.n_rows; out.rows(row_start, row_end_p1 - 1) = C; }
  91. }
  92. template<typename eT, typename T1, typename T2, typename T3, typename T4>
  93. inline
  94. void
  95. glue_join_cols::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr, const Base<eT,T4>& D_expr)
  96. {
  97. arma_extra_debug_sigprint();
  98. const quasi_unwrap<T1> UA(A_expr.get_ref());
  99. const quasi_unwrap<T2> UB(B_expr.get_ref());
  100. const quasi_unwrap<T3> UC(C_expr.get_ref());
  101. const quasi_unwrap<T4> UD(D_expr.get_ref());
  102. const Mat<eT>& A = UA.M;
  103. const Mat<eT>& B = UB.M;
  104. const Mat<eT>& C = UC.M;
  105. const Mat<eT>& D = UD.M;
  106. const uword out_n_rows = A.n_rows + B.n_rows + C.n_rows + D.n_rows;
  107. const uword out_n_cols = (std::max)(((std::max)((std::max)(A.n_cols, B.n_cols), C.n_cols)), D.n_cols);
  108. arma_debug_check( ((A.n_cols != out_n_cols) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  109. arma_debug_check( ((B.n_cols != out_n_cols) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  110. arma_debug_check( ((C.n_cols != out_n_cols) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  111. arma_debug_check( ((D.n_cols != out_n_cols) && ((D.n_rows > 0) || (D.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
  112. out.set_size(out_n_rows, out_n_cols);
  113. if(out.n_elem == 0) { return; }
  114. uword row_start = 0;
  115. uword row_end_p1 = 0;
  116. if(A.n_elem > 0) { row_end_p1 += A.n_rows; out.rows(row_start, row_end_p1 - 1) = A; }
  117. row_start = row_end_p1;
  118. if(B.n_elem > 0) { row_end_p1 += B.n_rows; out.rows(row_start, row_end_p1 - 1) = B; }
  119. row_start = row_end_p1;
  120. if(C.n_elem > 0) { row_end_p1 += C.n_rows; out.rows(row_start, row_end_p1 - 1) = C; }
  121. row_start = row_end_p1;
  122. if(D.n_elem > 0) { row_end_p1 += D.n_rows; out.rows(row_start, row_end_p1 - 1) = D; }
  123. }
  124. template<typename T1, typename T2>
  125. inline
  126. void
  127. glue_join_rows::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& A, const Proxy<T2>& B)
  128. {
  129. arma_extra_debug_sigprint();
  130. const uword A_n_rows = A.get_n_rows();
  131. const uword A_n_cols = A.get_n_cols();
  132. const uword B_n_rows = B.get_n_rows();
  133. const uword B_n_cols = B.get_n_cols();
  134. arma_debug_check
  135. (
  136. ( (A_n_rows != B_n_rows) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
  137. "join_rows() / join_horiz(): number of rows must be the same"
  138. );
  139. out.set_size( (std::max)(A_n_rows, B_n_rows), A_n_cols + B_n_cols );
  140. if( out.n_elem > 0 )
  141. {
  142. if(A.get_n_elem() > 0)
  143. {
  144. out.submat(0, 0, out.n_rows-1, A_n_cols-1) = A.Q;
  145. }
  146. if(B.get_n_elem() > 0)
  147. {
  148. out.submat(0, A_n_cols, out.n_rows-1, out.n_cols-1) = B.Q;
  149. }
  150. }
  151. }
  152. template<typename T1, typename T2>
  153. inline
  154. void
  155. glue_join_rows::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join_rows>& X)
  156. {
  157. arma_extra_debug_sigprint();
  158. typedef typename T1::elem_type eT;
  159. const Proxy<T1> A(X.A);
  160. const Proxy<T2> B(X.B);
  161. if( (A.is_alias(out) == false) && (B.is_alias(out) == false) )
  162. {
  163. glue_join_rows::apply_noalias(out, A, B);
  164. }
  165. else
  166. {
  167. Mat<eT> tmp;
  168. glue_join_rows::apply_noalias(tmp, A, B);
  169. out.steal_mem(tmp);
  170. }
  171. }
  172. template<typename eT, typename T1, typename T2, typename T3>
  173. inline
  174. void
  175. glue_join_rows::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr)
  176. {
  177. arma_extra_debug_sigprint();
  178. const quasi_unwrap<T1> UA(A_expr.get_ref());
  179. const quasi_unwrap<T2> UB(B_expr.get_ref());
  180. const quasi_unwrap<T3> UC(C_expr.get_ref());
  181. const Mat<eT>& A = UA.M;
  182. const Mat<eT>& B = UB.M;
  183. const Mat<eT>& C = UC.M;
  184. const uword out_n_rows = (std::max)((std::max)(A.n_rows, B.n_rows), C.n_rows);
  185. const uword out_n_cols = A.n_cols + B.n_cols + C.n_cols;
  186. arma_debug_check( ((A.n_rows != out_n_rows) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  187. arma_debug_check( ((B.n_rows != out_n_rows) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  188. arma_debug_check( ((C.n_rows != out_n_rows) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  189. out.set_size(out_n_rows, out_n_cols);
  190. if(out.n_elem == 0) { return; }
  191. uword col_start = 0;
  192. uword col_end_p1 = 0;
  193. if(A.n_elem > 0) { col_end_p1 += A.n_cols; out.cols(col_start, col_end_p1 - 1) = A; }
  194. col_start = col_end_p1;
  195. if(B.n_elem > 0) { col_end_p1 += B.n_cols; out.cols(col_start, col_end_p1 - 1) = B; }
  196. col_start = col_end_p1;
  197. if(C.n_elem > 0) { col_end_p1 += C.n_cols; out.cols(col_start, col_end_p1 - 1) = C; }
  198. }
  199. template<typename eT, typename T1, typename T2, typename T3, typename T4>
  200. inline
  201. void
  202. glue_join_rows::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr, const Base<eT,T4>& D_expr)
  203. {
  204. arma_extra_debug_sigprint();
  205. const quasi_unwrap<T1> UA(A_expr.get_ref());
  206. const quasi_unwrap<T2> UB(B_expr.get_ref());
  207. const quasi_unwrap<T3> UC(C_expr.get_ref());
  208. const quasi_unwrap<T4> UD(D_expr.get_ref());
  209. const Mat<eT>& A = UA.M;
  210. const Mat<eT>& B = UB.M;
  211. const Mat<eT>& C = UC.M;
  212. const Mat<eT>& D = UD.M;
  213. const uword out_n_rows = (std::max)(((std::max)((std::max)(A.n_rows, B.n_rows), C.n_rows)), D.n_rows);
  214. const uword out_n_cols = A.n_cols + B.n_cols + C.n_cols + D.n_cols;
  215. arma_debug_check( ((A.n_rows != out_n_rows) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  216. arma_debug_check( ((B.n_rows != out_n_rows) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  217. arma_debug_check( ((C.n_rows != out_n_rows) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  218. arma_debug_check( ((D.n_rows != out_n_rows) && ((D.n_rows > 0) || (D.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
  219. out.set_size(out_n_rows, out_n_cols);
  220. if(out.n_elem == 0) { return; }
  221. uword col_start = 0;
  222. uword col_end_p1 = 0;
  223. if(A.n_elem > 0) { col_end_p1 += A.n_cols; out.cols(col_start, col_end_p1 - 1) = A; }
  224. col_start = col_end_p1;
  225. if(B.n_elem > 0) { col_end_p1 += B.n_cols; out.cols(col_start, col_end_p1 - 1) = B; }
  226. col_start = col_end_p1;
  227. if(C.n_elem > 0) { col_end_p1 += C.n_cols; out.cols(col_start, col_end_p1 - 1) = C; }
  228. col_start = col_end_p1;
  229. if(D.n_elem > 0) { col_end_p1 += D.n_cols; out.cols(col_start, col_end_p1 - 1) = D; }
  230. }
  231. template<typename T1, typename T2>
  232. inline
  233. void
  234. glue_join_slices::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1,T2,glue_join_slices>& X)
  235. {
  236. arma_extra_debug_sigprint();
  237. typedef typename T1::elem_type eT;
  238. const unwrap_cube<T1> A_tmp(X.A);
  239. const unwrap_cube<T2> B_tmp(X.B);
  240. const Cube<eT>& A = A_tmp.M;
  241. const Cube<eT>& B = B_tmp.M;
  242. if(A.n_elem == 0) { out = B; return; }
  243. if(B.n_elem == 0) { out = A; return; }
  244. arma_debug_check( ( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) ), "join_slices(): size of slices must be the same" );
  245. if( (&out != &A) && (&out != &B) )
  246. {
  247. out.set_size(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
  248. out.slices(0, A.n_slices-1 ) = A;
  249. out.slices(A.n_slices, out.n_slices-1) = B;
  250. }
  251. else // we have aliasing
  252. {
  253. Cube<eT> C(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
  254. C.slices(0, A.n_slices-1) = A;
  255. C.slices(A.n_slices, C.n_slices-1) = B;
  256. out.steal_mem(C);
  257. }
  258. }
  259. //! @}