spop_repmat_meat.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 spop_repmat
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. spop_repmat::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_repmat>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const unwrap_spmat<T1> U(X.m);
  25. if(U.is_alias(out))
  26. {
  27. SpMat<eT> tmp;
  28. spop_repmat::apply_noalias(tmp, X.aux_uword_a, X.aux_uword_b, U.M);
  29. out.steal_mem(tmp);
  30. }
  31. else
  32. {
  33. spop_repmat::apply_noalias(out, X.aux_uword_a, X.aux_uword_b, U.M);
  34. }
  35. }
  36. template<typename eT>
  37. inline
  38. void
  39. spop_repmat::apply_noalias(SpMat<eT>& out, const uword A_n_rows, const uword A_n_cols, const SpMat<eT>& B)
  40. {
  41. arma_extra_debug_sigprint();
  42. const uword B_n_rows = B.n_rows;
  43. const uword B_n_cols = B.n_cols;
  44. const uword out_n_nonzero = A_n_rows * A_n_cols * B.n_nonzero;
  45. out.reserve(A_n_rows * B_n_rows, A_n_cols * B_n_cols, out_n_nonzero);
  46. if(out_n_nonzero == 0) { return; }
  47. access::rw(out.col_ptrs[0]) = 0;
  48. uword count = 0;
  49. for(uword A_col=0; A_col < A_n_cols; ++A_col)
  50. for(uword B_col=0; B_col < B_n_cols; ++B_col)
  51. {
  52. for(uword A_row=0; A_row < A_n_rows; ++A_row)
  53. {
  54. const uword out_row = A_row * B_n_rows;
  55. for(uword B_i = B.col_ptrs[B_col]; B_i < B.col_ptrs[B_col+1]; ++B_i)
  56. {
  57. access::rw(out.values[count]) = B.values[B_i];
  58. access::rw(out.row_indices[count]) = out_row + B.row_indices[B_i];
  59. count++;
  60. }
  61. }
  62. access::rw(out.col_ptrs[A_col * B_n_cols + B_col + 1]) = count;
  63. }
  64. }
  65. // template<typename T1>
  66. // inline
  67. // void
  68. // spop_repmat::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_repmat>& in)
  69. // {
  70. // arma_extra_debug_sigprint();
  71. //
  72. // typedef typename T1::elem_type eT;
  73. //
  74. // const unwrap_spmat<T1> U(in.m);
  75. // const SpMat<eT>& X = U.M;
  76. //
  77. // const uword X_n_rows = X.n_rows;
  78. // const uword X_n_cols = X.n_cols;
  79. //
  80. // const uword copies_per_row = in.aux_uword_a;
  81. // const uword copies_per_col = in.aux_uword_b;
  82. //
  83. // // out.set_size(X_n_rows * copies_per_row, X_n_cols * copies_per_col);
  84. // //
  85. // // const uword out_n_rows = out.n_rows;
  86. // // const uword out_n_cols = out.n_cols;
  87. // //
  88. // // if( (out_n_rows > 0) && (out_n_cols > 0) )
  89. // // {
  90. // // for(uword col = 0; col < out_n_cols; col += X_n_cols)
  91. // // for(uword row = 0; row < out_n_rows; row += X_n_rows)
  92. // // {
  93. // // out.submat(row, col, row+X_n_rows-1, col+X_n_cols-1) = X;
  94. // // }
  95. // // }
  96. //
  97. // const uword out_n_rows = X_n_rows * copies_per_row;
  98. // const uword out_n_cols = X_n_cols * copies_per_col;
  99. // const uword out_nnz = X.n_nonzero * copies_per_row * copies_per_col;
  100. //
  101. // if( (out_n_rows > 0) && (out_n_cols > 0) && (out_nnz > 0) )
  102. // {
  103. // umat locs(2, out_nnz);
  104. // Col<eT> vals( out_nnz);
  105. //
  106. // uword* locs_mem = locs.memptr();
  107. // eT* vals_mem = vals.memptr();
  108. //
  109. // typename SpMat<eT>::const_iterator X_begin = X.begin();
  110. // typename SpMat<eT>::const_iterator X_end = X.end();
  111. // typename SpMat<eT>::const_iterator X_it;
  112. //
  113. // for(uword col_offset = 0; col_offset < out_n_cols; col_offset += X_n_cols)
  114. // for(uword row_offset = 0; row_offset < out_n_rows; row_offset += X_n_rows)
  115. // {
  116. // for(X_it = X_begin; X_it != X_end; ++X_it)
  117. // {
  118. // const uword out_row = row_offset + X_it.row();
  119. // const uword out_col = col_offset + X_it.col();
  120. //
  121. // (*locs_mem) = out_row; ++locs_mem;
  122. // (*locs_mem) = out_col; ++locs_mem;
  123. //
  124. // (*vals_mem) = (*X_it); ++vals_mem;
  125. // }
  126. // }
  127. //
  128. // out = SpMat<eT>(locs, vals, out_n_rows, out_n_cols);
  129. // }
  130. // else
  131. // {
  132. // out.zeros(out_n_rows, out_n_cols);
  133. // }
  134. // }
  135. //! @}