op_clamp_meat.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_clamp
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_clamp::apply(Mat<typename T1::elem_type>& out, const mtOp<typename T1::elem_type, T1, op_clamp>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. const Proxy<T1> P(in.m);
  24. if(is_Mat<typename Proxy<T1>::stored_type>::value || P.is_alias(out))
  25. {
  26. const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
  27. op_clamp::apply_direct(out, U.M, in.aux, in.aux_out_eT);
  28. }
  29. else
  30. {
  31. op_clamp::apply_proxy_noalias(out, P, in.aux, in.aux_out_eT);
  32. }
  33. }
  34. template<typename T1>
  35. inline
  36. void
  37. op_clamp::apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val)
  38. {
  39. arma_extra_debug_sigprint();
  40. typedef typename T1::elem_type eT;
  41. const uword n_rows = P.get_n_rows();
  42. const uword n_cols = P.get_n_cols();
  43. out.set_size(n_rows, n_cols);
  44. eT* out_mem = out.memptr();
  45. if(Proxy<T1>::use_at == false)
  46. {
  47. const uword N = P.get_n_elem();
  48. typename Proxy<T1>::ea_type A = P.get_ea();
  49. uword j;
  50. for(j=1; j<N; j+=2)
  51. {
  52. eT val_i = A[j-1];
  53. eT val_j = A[j ];
  54. val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
  55. val_j = (val_j < min_val) ? min_val : ((val_j > max_val) ? max_val : val_j);
  56. (*out_mem) = val_i; out_mem++;
  57. (*out_mem) = val_j; out_mem++;
  58. }
  59. const uword i = j-1;
  60. if(i < N)
  61. {
  62. eT val_i = A[i];
  63. val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
  64. (*out_mem) = val_i;
  65. }
  66. }
  67. else
  68. {
  69. for(uword col=0; col<n_cols; ++col)
  70. for(uword row=0; row<n_rows; ++row)
  71. {
  72. eT val = P.at(row,col);
  73. val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val);
  74. (*out_mem) = val; out_mem++;
  75. }
  76. }
  77. }
  78. template<typename eT>
  79. inline
  80. void
  81. op_clamp::apply_direct(Mat<eT>& out, const Mat<eT>& X, const eT min_val, const eT max_val)
  82. {
  83. arma_extra_debug_sigprint();
  84. if(&out != &X)
  85. {
  86. const Proxy< Mat<eT> > P(X);
  87. op_clamp::apply_proxy_noalias(out, P, min_val, max_val);
  88. }
  89. else
  90. {
  91. arma_extra_debug_print("inplace operation");
  92. const uword N = out.n_elem;
  93. eT* out_mem = out.memptr();
  94. for(uword i=0; i<N; ++i)
  95. {
  96. eT& out_val = out_mem[i];
  97. out_val = (out_val < min_val) ? min_val : ( (out_val > max_val) ? max_val : out_val );
  98. }
  99. }
  100. }
  101. //
  102. template<typename T1>
  103. inline
  104. void
  105. op_clamp::apply(Cube<typename T1::elem_type>& out, const mtOpCube<typename T1::elem_type, T1, op_clamp>& in)
  106. {
  107. arma_extra_debug_sigprint();
  108. const ProxyCube<T1> P(in.m);
  109. if((is_Cube<typename ProxyCube<T1>::stored_type>::value) || P.is_alias(out))
  110. {
  111. const unwrap_cube<typename ProxyCube<T1>::stored_type> U(P.Q);
  112. op_clamp::apply_direct(out, U.M, in.aux, in.aux_out_eT);
  113. }
  114. else
  115. {
  116. op_clamp::apply_proxy_noalias(out, P, in.aux, in.aux_out_eT);
  117. }
  118. }
  119. template<typename T1>
  120. inline
  121. void
  122. op_clamp::apply_proxy_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val)
  123. {
  124. arma_extra_debug_sigprint();
  125. typedef typename T1::elem_type eT;
  126. const uword n_rows = P.get_n_rows();
  127. const uword n_cols = P.get_n_cols();
  128. const uword n_slices = P.get_n_slices();
  129. out.set_size(n_rows, n_cols, n_slices);
  130. eT* out_mem = out.memptr();
  131. if(ProxyCube<T1>::use_at == false)
  132. {
  133. const uword N = P.get_n_elem();
  134. typename ProxyCube<T1>::ea_type A = P.get_ea();
  135. uword j;
  136. for(j=1; j<N; j+=2)
  137. {
  138. eT val_i = A[j-1];
  139. eT val_j = A[j ];
  140. val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
  141. val_j = (val_j < min_val) ? min_val : ((val_j > max_val) ? max_val : val_j);
  142. (*out_mem) = val_i; out_mem++;
  143. (*out_mem) = val_j; out_mem++;
  144. }
  145. const uword i = j-1;
  146. if(i < N)
  147. {
  148. eT val_i = A[i];
  149. val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
  150. (*out_mem) = val_i;
  151. }
  152. }
  153. else
  154. {
  155. for(uword k=0; k < n_slices; ++k)
  156. for(uword j=0; j < n_cols; ++j)
  157. for(uword i=0; i < n_rows; ++i)
  158. {
  159. eT val = P.at(i,j,k);
  160. val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val);
  161. (*out_mem) = val; out_mem++;
  162. }
  163. }
  164. }
  165. template<typename eT>
  166. inline
  167. void
  168. op_clamp::apply_direct(Cube<eT>& out, const Cube<eT>& X, const eT min_val, const eT max_val)
  169. {
  170. arma_extra_debug_sigprint();
  171. if(&out != &X)
  172. {
  173. const ProxyCube< Cube<eT> > P(X);
  174. op_clamp::apply_proxy_noalias(out, P, min_val, max_val);
  175. }
  176. else
  177. {
  178. arma_extra_debug_print("inplace operation");
  179. const uword N = out.n_elem;
  180. eT* out_mem = out.memptr();
  181. for(uword i=0; i<N; ++i)
  182. {
  183. eT& out_val = out_mem[i];
  184. out_val = (out_val < min_val) ? min_val : ( (out_val > max_val) ? max_val : out_val );
  185. }
  186. }
  187. }
  188. //! @}