op_diff_meat.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_diff
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_diff::apply_noalias(Mat<eT>& out, const Mat<eT>& X, const uword k, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. uword n_rows = X.n_rows;
  24. uword n_cols = X.n_cols;
  25. if(dim == 0)
  26. {
  27. if(n_rows <= k) { out.set_size(0,n_cols); return; }
  28. --n_rows;
  29. out.set_size(n_rows,n_cols);
  30. for(uword col=0; col < n_cols; ++col)
  31. {
  32. eT* out_colmem = out.colptr(col);
  33. const eT* X_colmem = X.colptr(col);
  34. for(uword row=0; row < n_rows; ++row)
  35. {
  36. const eT val0 = X_colmem[row ];
  37. const eT val1 = X_colmem[row+1];
  38. out_colmem[row] = val1 - val0;
  39. }
  40. }
  41. if(k >= 2)
  42. {
  43. for(uword iter=2; iter <= k; ++iter)
  44. {
  45. --n_rows;
  46. for(uword col=0; col < n_cols; ++col)
  47. {
  48. eT* colmem = out.colptr(col);
  49. for(uword row=0; row < n_rows; ++row)
  50. {
  51. const eT val0 = colmem[row ];
  52. const eT val1 = colmem[row+1];
  53. colmem[row] = val1 - val0;
  54. }
  55. }
  56. }
  57. out = out( span(0,n_rows-1), span::all );
  58. }
  59. }
  60. else
  61. if(dim == 1)
  62. {
  63. if(n_cols <= k) { out.set_size(n_rows,0); return; }
  64. --n_cols;
  65. out.set_size(n_rows,n_cols);
  66. if(n_rows == 1)
  67. {
  68. const eT* X_mem = X.memptr();
  69. eT* out_mem = out.memptr();
  70. for(uword col=0; col < n_cols; ++col)
  71. {
  72. const eT val0 = X_mem[col ];
  73. const eT val1 = X_mem[col+1];
  74. out_mem[col] = val1 - val0;
  75. }
  76. }
  77. else
  78. {
  79. for(uword col=0; col < n_cols; ++col)
  80. {
  81. eT* out_col_mem = out.colptr(col);
  82. const eT* X_col0_mem = X.colptr(col );
  83. const eT* X_col1_mem = X.colptr(col+1);
  84. for(uword row=0; row < n_rows; ++row)
  85. {
  86. out_col_mem[row] = X_col1_mem[row] - X_col0_mem[row];
  87. }
  88. }
  89. }
  90. if(k >= 2)
  91. {
  92. for(uword iter=2; iter <= k; ++iter)
  93. {
  94. --n_cols;
  95. if(n_rows == 1)
  96. {
  97. eT* out_mem = out.memptr();
  98. for(uword col=0; col < n_cols; ++col)
  99. {
  100. const eT val0 = out_mem[col ];
  101. const eT val1 = out_mem[col+1];
  102. out_mem[col] = val1 - val0;
  103. }
  104. }
  105. else
  106. {
  107. for(uword col=0; col < n_cols; ++col)
  108. {
  109. eT* col0_mem = out.colptr(col );
  110. const eT* col1_mem = out.colptr(col+1);
  111. for(uword row=0; row < n_rows; ++row)
  112. {
  113. col0_mem[row] = col1_mem[row] - col0_mem[row];
  114. }
  115. }
  116. }
  117. }
  118. out = out( span::all, span(0,n_cols-1) );
  119. }
  120. }
  121. }
  122. template<typename T1>
  123. inline
  124. void
  125. op_diff::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_diff>& in)
  126. {
  127. arma_extra_debug_sigprint();
  128. typedef typename T1::elem_type eT;
  129. const uword k = in.aux_uword_a;
  130. const uword dim = in.aux_uword_b;
  131. arma_debug_check( (dim > 1), "diff(): parameter 'dim' must be 0 or 1" );
  132. if(k == 0) { out = in.m; return; }
  133. const quasi_unwrap<T1> U(in.m);
  134. if(U.is_alias(out))
  135. {
  136. Mat<eT> tmp;
  137. op_diff::apply_noalias(tmp, U.M, k, dim);
  138. out.steal_mem(tmp);
  139. }
  140. else
  141. {
  142. op_diff::apply_noalias(out, U.M, k, dim);
  143. }
  144. }
  145. template<typename T1>
  146. inline
  147. void
  148. op_diff_vec::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_diff_vec>& in)
  149. {
  150. arma_extra_debug_sigprint();
  151. typedef typename T1::elem_type eT;
  152. const uword k = in.aux_uword_a;
  153. if(k == 0) { out = in.m; return; }
  154. const quasi_unwrap<T1> U(in.m);
  155. const uword dim = (T1::is_xvec) ? uword(U.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  156. if(U.is_alias(out))
  157. {
  158. Mat<eT> tmp;
  159. op_diff::apply_noalias(tmp, U.M, k, dim);
  160. out.steal_mem(tmp);
  161. }
  162. else
  163. {
  164. op_diff::apply_noalias(out, U.M, k, dim);
  165. }
  166. }
  167. //! @}