op_repmat_meat.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_repmat
  16. //! @{
  17. template<typename obj>
  18. inline
  19. void
  20. op_repmat::apply_noalias(Mat<typename obj::elem_type>& out, const obj& X, const uword copies_per_row, const uword copies_per_col)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename obj::elem_type eT;
  24. const uword X_n_rows = obj::is_row ? uword(1) : X.n_rows;
  25. const uword X_n_cols = obj::is_col ? uword(1) : X.n_cols;
  26. out.set_size(X_n_rows * copies_per_row, X_n_cols * copies_per_col);
  27. const uword out_n_rows = out.n_rows;
  28. const uword out_n_cols = out.n_cols;
  29. // if( (out_n_rows > 0) && (out_n_cols > 0) )
  30. // {
  31. // for(uword col = 0; col < out_n_cols; col += X_n_cols)
  32. // for(uword row = 0; row < out_n_rows; row += X_n_rows)
  33. // {
  34. // out.submat(row, col, row+X_n_rows-1, col+X_n_cols-1) = X;
  35. // }
  36. // }
  37. if( (out_n_rows > 0) && (out_n_cols > 0) )
  38. {
  39. if(copies_per_row != 1)
  40. {
  41. for(uword col_copy=0; col_copy < copies_per_col; ++col_copy)
  42. {
  43. const uword out_col_offset = X_n_cols * col_copy;
  44. for(uword col=0; col < X_n_cols; ++col)
  45. {
  46. eT* out_colptr = out.colptr(col + out_col_offset);
  47. const eT* X_colptr = X.colptr(col);
  48. for(uword row_copy=0; row_copy < copies_per_row; ++row_copy)
  49. {
  50. const uword out_row_offset = X_n_rows * row_copy;
  51. arrayops::copy( &out_colptr[out_row_offset], X_colptr, X_n_rows );
  52. }
  53. }
  54. }
  55. }
  56. else
  57. {
  58. for(uword col_copy=0; col_copy < copies_per_col; ++col_copy)
  59. {
  60. const uword out_col_offset = X_n_cols * col_copy;
  61. for(uword col=0; col < X_n_cols; ++col)
  62. {
  63. eT* out_colptr = out.colptr(col + out_col_offset);
  64. const eT* X_colptr = X.colptr(col);
  65. arrayops::copy( out_colptr, X_colptr, X_n_rows );
  66. }
  67. }
  68. }
  69. }
  70. }
  71. template<typename T1>
  72. inline
  73. void
  74. op_repmat::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_repmat>& in)
  75. {
  76. arma_extra_debug_sigprint();
  77. typedef typename T1::elem_type eT;
  78. const uword copies_per_row = in.aux_uword_a;
  79. const uword copies_per_col = in.aux_uword_b;
  80. const quasi_unwrap<T1> U(in.m);
  81. if(U.is_alias(out))
  82. {
  83. Mat<eT> tmp;
  84. op_repmat::apply_noalias(tmp, U.M, copies_per_row, copies_per_col);
  85. out.steal_mem(tmp);
  86. }
  87. else
  88. {
  89. op_repmat::apply_noalias(out, U.M, copies_per_row, copies_per_col);
  90. }
  91. }
  92. //! @}