spglue_join_meat.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 spglue_join
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. spglue_join_cols::apply(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_join_cols>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const unwrap_spmat<T1> UA(X.A);
  25. const unwrap_spmat<T2> UB(X.B);
  26. if(UA.is_alias(out) || UB.is_alias(out))
  27. {
  28. SpMat<eT> tmp;
  29. spglue_join_cols::apply_noalias(tmp, UA.M, UB.M);
  30. out.steal_mem(tmp);
  31. }
  32. else
  33. {
  34. spglue_join_cols::apply_noalias(out, UA.M, UB.M);
  35. }
  36. }
  37. template<typename eT>
  38. inline
  39. void
  40. spglue_join_cols::apply_noalias(SpMat<eT>& out, const SpMat<eT>& A, const SpMat<eT>& B)
  41. {
  42. arma_extra_debug_sigprint();
  43. const uword A_n_rows = A.n_rows;
  44. const uword A_n_cols = A.n_cols;
  45. const uword B_n_rows = B.n_rows;
  46. const uword B_n_cols = B.n_cols;
  47. arma_debug_check
  48. (
  49. ( (A_n_cols != B_n_cols) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
  50. "join_cols() / join_vert(): number of columns must be the same"
  51. );
  52. out.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
  53. if( out.n_elem > 0 )
  54. {
  55. if(A.is_empty() == false)
  56. {
  57. out.submat(0, 0, A_n_rows-1, out.n_cols-1) = A;
  58. }
  59. if(B.is_empty() == false)
  60. {
  61. out.submat(A_n_rows, 0, out.n_rows-1, out.n_cols-1) = B;
  62. }
  63. }
  64. }
  65. template<typename eT, typename T1, typename T2, typename T3>
  66. inline
  67. void
  68. spglue_join_cols::apply(SpMat<eT>& out, const SpBase<eT,T1>& A_expr, const SpBase<eT,T2>& B_expr, const SpBase<eT,T3>& C_expr)
  69. {
  70. arma_extra_debug_sigprint();
  71. const unwrap_spmat<T1> UA(A_expr.get_ref());
  72. const unwrap_spmat<T2> UB(B_expr.get_ref());
  73. const unwrap_spmat<T3> UC(C_expr.get_ref());
  74. const SpMat<eT>& A = UA.M;
  75. const SpMat<eT>& B = UB.M;
  76. const SpMat<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. spglue_join_cols::apply(SpMat<eT>& out, const SpBase<eT,T1>& A_expr, const SpBase<eT,T2>& B_expr, const SpBase<eT,T3>& C_expr, const SpBase<eT,T4>& D_expr)
  96. {
  97. arma_extra_debug_sigprint();
  98. const unwrap_spmat<T1> UA(A_expr.get_ref());
  99. const unwrap_spmat<T2> UB(B_expr.get_ref());
  100. const unwrap_spmat<T3> UC(C_expr.get_ref());
  101. const unwrap_spmat<T4> UD(D_expr.get_ref());
  102. const SpMat<eT>& A = UA.M;
  103. const SpMat<eT>& B = UB.M;
  104. const SpMat<eT>& C = UC.M;
  105. const SpMat<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. spglue_join_rows::apply(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_join_rows>& X)
  128. {
  129. arma_extra_debug_sigprint();
  130. typedef typename T1::elem_type eT;
  131. const unwrap_spmat<T1> UA(X.A);
  132. const unwrap_spmat<T2> UB(X.B);
  133. if(UA.is_alias(out) || UB.is_alias(out))
  134. {
  135. SpMat<eT> tmp;
  136. spglue_join_rows::apply_noalias(tmp, UA.M, UB.M);
  137. out.steal_mem(tmp);
  138. }
  139. else
  140. {
  141. spglue_join_rows::apply_noalias(out, UA.M, UB.M);
  142. }
  143. }
  144. template<typename eT>
  145. inline
  146. void
  147. spglue_join_rows::apply_noalias(SpMat<eT>& out, const SpMat<eT>& A, const SpMat<eT>& B)
  148. {
  149. arma_extra_debug_sigprint();
  150. const uword A_n_rows = A.n_rows;
  151. const uword A_n_cols = A.n_cols;
  152. const uword A_n_nz = A.n_nonzero;
  153. const uword B_n_rows = B.n_rows;
  154. const uword B_n_cols = B.n_cols;
  155. const uword B_n_nz = B.n_nonzero;
  156. arma_debug_check
  157. (
  158. ( (A_n_rows != B.n_rows) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
  159. "join_rows() / join_horiz(): number of rows must be the same"
  160. );
  161. const uword C_n_rows = (std::max)(A_n_rows, B_n_rows);
  162. const uword C_n_cols = A_n_cols + B_n_cols;
  163. const uword C_n_nz = A_n_nz + B_n_nz;
  164. if( ((C_n_rows * C_n_cols) == 0) || (C_n_nz == 0) )
  165. {
  166. out.zeros(C_n_rows, C_n_cols);
  167. return;
  168. }
  169. out.reserve(C_n_rows, C_n_cols, C_n_nz);
  170. arrayops::copy( access::rwp(out.values), A.values, A_n_nz );
  171. arrayops::copy( access::rwp(out.values) + A_n_nz, B.values, B_n_nz+1 );
  172. arrayops::copy( access::rwp(out.row_indices), A.row_indices, A_n_nz );
  173. arrayops::copy( access::rwp(out.row_indices) + A_n_nz, B.row_indices, B_n_nz+1 );
  174. arrayops::copy( access::rwp(out.col_ptrs), A.col_ptrs, A_n_cols );
  175. arrayops::copy( access::rwp(out.col_ptrs) + A_n_cols, B.col_ptrs, B_n_cols+2 );
  176. arrayops::inplace_plus( access::rwp(out.col_ptrs) + A_n_cols, A_n_nz, B_n_cols+1 );
  177. // // OLD METHOD
  178. //
  179. // umat locs(2, C_n_nz);
  180. // Col<eT> vals( C_n_nz);
  181. //
  182. // uword* locs_mem = locs.memptr();
  183. // eT* vals_mem = vals.memptr();
  184. //
  185. // typename SpMat<eT>::const_iterator A_it = A.begin();
  186. //
  187. // for(uword i=0; i < A_n_nz; ++i)
  188. // {
  189. // const uword row = A_it.row();
  190. // const uword col = A_it.col();
  191. //
  192. // (*locs_mem) = row; locs_mem++;
  193. // (*locs_mem) = col; locs_mem++;
  194. //
  195. // (*vals_mem) = (*A_it); vals_mem++;
  196. //
  197. // ++A_it;
  198. // }
  199. //
  200. // typename SpMat<eT>::const_iterator B_it = B.begin();
  201. //
  202. // for(uword i=0; i < B_n_nz; ++i)
  203. // {
  204. // const uword row = B_it.row();
  205. // const uword col = A_n_cols + B_it.col();
  206. //
  207. // (*locs_mem) = row; locs_mem++;
  208. // (*locs_mem) = col; locs_mem++;
  209. //
  210. // (*vals_mem) = (*B_it); vals_mem++;
  211. //
  212. // ++B_it;
  213. // }
  214. //
  215. // // TODO: the first element of B within C will always have a larger index than the last element of A in C;
  216. // // TODO: so, is sorting really necessary here?
  217. // SpMat<eT> tmp(locs, vals, C_n_rows, C_n_cols, true, false);
  218. //
  219. // out.steal_mem(tmp);
  220. }
  221. template<typename eT, typename T1, typename T2, typename T3>
  222. inline
  223. void
  224. spglue_join_rows::apply(SpMat<eT>& out, const SpBase<eT,T1>& A_expr, const SpBase<eT,T2>& B_expr, const SpBase<eT,T3>& C_expr)
  225. {
  226. arma_extra_debug_sigprint();
  227. const unwrap_spmat<T1> UA(A_expr.get_ref());
  228. const unwrap_spmat<T2> UB(B_expr.get_ref());
  229. const unwrap_spmat<T3> UC(C_expr.get_ref());
  230. const SpMat<eT>& A = UA.M;
  231. const SpMat<eT>& B = UB.M;
  232. const SpMat<eT>& C = UC.M;
  233. SpMat<eT> tmp;
  234. spglue_join_rows::apply_noalias(tmp, A, B);
  235. spglue_join_rows::apply_noalias(out, tmp, C);
  236. }
  237. template<typename eT, typename T1, typename T2, typename T3, typename T4>
  238. inline
  239. void
  240. spglue_join_rows::apply(SpMat<eT>& out, const SpBase<eT,T1>& A_expr, const SpBase<eT,T2>& B_expr, const SpBase<eT,T3>& C_expr, const SpBase<eT,T4>& D_expr)
  241. {
  242. arma_extra_debug_sigprint();
  243. const unwrap_spmat<T1> UA(A_expr.get_ref());
  244. const unwrap_spmat<T2> UB(B_expr.get_ref());
  245. const unwrap_spmat<T3> UC(C_expr.get_ref());
  246. const unwrap_spmat<T4> UD(D_expr.get_ref());
  247. const SpMat<eT>& A = UA.M;
  248. const SpMat<eT>& B = UB.M;
  249. const SpMat<eT>& C = UC.M;
  250. const SpMat<eT>& D = UD.M;
  251. SpMat<eT> AB;
  252. SpMat<eT> ABC;
  253. spglue_join_rows::apply_noalias(AB, A, B);
  254. spglue_join_rows::apply_noalias(ABC, AB, C);
  255. spglue_join_rows::apply_noalias(out, ABC, D);
  256. }
  257. //! @}