spop_reverse_meat.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_reverse
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. spop_reverse::apply_spmat(SpMat<eT>& out, const SpMat<eT>& X, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. const uword X_n_rows = X.n_rows;
  24. const uword X_n_cols = X.n_cols;
  25. const uword X_n_rows_m1 = X_n_rows - 1;
  26. const uword X_n_cols_m1 = X_n_cols - 1;
  27. const uword N = X.n_nonzero;
  28. if(N == uword(0))
  29. {
  30. out.zeros(X_n_rows, X_n_cols);
  31. return;
  32. }
  33. umat locs(2, N);
  34. uword* locs_mem = locs.memptr();
  35. typename SpMat<eT>::const_iterator it = X.begin();
  36. if(dim == 0)
  37. {
  38. for(uword i=0; i < N; ++i)
  39. {
  40. const uword row = it.row();
  41. const uword col = it.col();
  42. (*locs_mem) = X_n_rows_m1 - row; locs_mem++;
  43. (*locs_mem) = col; locs_mem++;
  44. ++it;
  45. }
  46. }
  47. else
  48. if(dim == 1)
  49. {
  50. for(uword i=0; i < N; ++i)
  51. {
  52. const uword row = it.row();
  53. const uword col = it.col();
  54. (*locs_mem) = row; locs_mem++;
  55. (*locs_mem) = X_n_cols_m1 - col; locs_mem++;
  56. ++it;
  57. }
  58. }
  59. const Col<eT> vals(const_cast<eT*>(X.values), N, false);
  60. SpMat<eT> tmp(locs, vals, X_n_rows, X_n_cols, true, false);
  61. out.steal_mem(tmp);
  62. }
  63. template<typename T1>
  64. inline
  65. void
  66. spop_reverse::apply_proxy(SpMat<typename T1::elem_type>& out, const T1& X, const uword dim)
  67. {
  68. arma_extra_debug_sigprint();
  69. typedef typename T1::elem_type eT;
  70. const SpProxy<T1> P(X);
  71. const uword P_n_rows = P.get_n_rows();
  72. const uword P_n_cols = P.get_n_cols();
  73. const uword P_n_rows_m1 = P_n_rows - 1;
  74. const uword P_n_cols_m1 = P_n_cols - 1;
  75. const uword N = P.get_n_nonzero();
  76. if(N == uword(0))
  77. {
  78. out.zeros(P_n_rows, P_n_cols);
  79. return;
  80. }
  81. umat locs(2, N);
  82. Col<eT> vals( N);
  83. uword* locs_mem = locs.memptr();
  84. eT* vals_mem = vals.memptr();
  85. typename SpProxy<T1>::const_iterator_type it = P.begin();
  86. if(dim == 0)
  87. {
  88. for(uword i=0; i < N; ++i)
  89. {
  90. const uword row = it.row();
  91. const uword col = it.col();
  92. (*locs_mem) = P_n_rows_m1 - row; locs_mem++;
  93. (*locs_mem) = col; locs_mem++;
  94. (*vals_mem) = (*it); vals_mem++;
  95. ++it;
  96. }
  97. }
  98. else
  99. if(dim == 1)
  100. {
  101. for(uword i=0; i < N; ++i)
  102. {
  103. const uword row = it.row();
  104. const uword col = it.col();
  105. (*locs_mem) = row; locs_mem++;
  106. (*locs_mem) = P_n_cols_m1 - col; locs_mem++;
  107. (*vals_mem) = (*it); vals_mem++;
  108. ++it;
  109. }
  110. }
  111. SpMat<eT> tmp(locs, vals, P_n_rows, P_n_cols, true, false);
  112. out.steal_mem(tmp);
  113. }
  114. template<typename T1>
  115. inline
  116. void
  117. spop_reverse::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_reverse>& in)
  118. {
  119. arma_extra_debug_sigprint();
  120. const uword dim = in.aux_uword_a;
  121. arma_debug_check( (dim > 1), "reverse(): parameter 'dim' must be 0 or 1" );
  122. if(is_SpMat<T1>::value)
  123. {
  124. const unwrap_spmat<T1> tmp(in.m);
  125. spop_reverse::apply_spmat(out, tmp.M, dim);
  126. }
  127. else
  128. {
  129. spop_reverse::apply_proxy(out, in.m, dim);
  130. }
  131. }
  132. //! @}