glue_hist_meat.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 glue_hist
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. glue_hist::apply_noalias(Mat<uword>& out, const Mat<eT>& X, const Mat<eT>& C, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. arma_debug_check( ((C.is_vec() == false) && (C.is_empty() == false)), "hist(): parameter 'centers' must be a vector" );
  24. const uword X_n_rows = X.n_rows;
  25. const uword X_n_cols = X.n_cols;
  26. const uword C_n_elem = C.n_elem;
  27. if( C_n_elem == 0 ) { out.reset(); return; }
  28. arma_debug_check
  29. (
  30. ((Col<eT>(const_cast<eT*>(C.memptr()), C_n_elem, false, false)).is_sorted("strictascend") == false),
  31. "hist(): given 'centers' vector does not contain monotonically increasing values"
  32. );
  33. const eT* C_mem = C.memptr();
  34. const eT center_0 = C_mem[0];
  35. if(dim == 0)
  36. {
  37. out.zeros(C_n_elem, X_n_cols);
  38. for(uword col=0; col < X_n_cols; ++col)
  39. {
  40. const eT* X_coldata = X.colptr(col);
  41. uword* out_coldata = out.colptr(col);
  42. for(uword row=0; row < X_n_rows; ++row)
  43. {
  44. const eT val = X_coldata[row];
  45. if(arma_isfinite(val))
  46. {
  47. eT opt_dist = (center_0 >= val) ? (center_0 - val) : (val - center_0);
  48. uword opt_index = 0;
  49. for(uword j=1; j < C_n_elem; ++j)
  50. {
  51. const eT center = C_mem[j];
  52. const eT dist = (center >= val) ? (center - val) : (val - center);
  53. if(dist < opt_dist)
  54. {
  55. opt_dist = dist;
  56. opt_index = j;
  57. }
  58. else
  59. {
  60. break;
  61. }
  62. }
  63. out_coldata[opt_index]++;
  64. }
  65. else
  66. {
  67. // -inf
  68. if(val < eT(0)) { out_coldata[0]++; }
  69. // +inf
  70. if(val > eT(0)) { out_coldata[C_n_elem-1]++; }
  71. // ignore NaN
  72. }
  73. }
  74. }
  75. }
  76. else
  77. if(dim == 1)
  78. {
  79. out.zeros(X_n_rows, C_n_elem);
  80. if(X_n_rows == 1)
  81. {
  82. const uword X_n_elem = X.n_elem;
  83. const eT* X_mem = X.memptr();
  84. uword* out_mem = out.memptr();
  85. for(uword i=0; i < X_n_elem; ++i)
  86. {
  87. const eT val = X_mem[i];
  88. if(is_finite(val))
  89. {
  90. eT opt_dist = (val >= center_0) ? (val - center_0) : (center_0 - val);
  91. uword opt_index = 0;
  92. for(uword j=1; j < C_n_elem; ++j)
  93. {
  94. const eT center = C_mem[j];
  95. const eT dist = (val >= center) ? (val - center) : (center - val);
  96. if(dist < opt_dist)
  97. {
  98. opt_dist = dist;
  99. opt_index = j;
  100. }
  101. else
  102. {
  103. break;
  104. }
  105. }
  106. out_mem[opt_index]++;
  107. }
  108. else
  109. {
  110. // -inf
  111. if(val < eT(0)) { out_mem[0]++; }
  112. // +inf
  113. if(val > eT(0)) { out_mem[C_n_elem-1]++; }
  114. // ignore NaN
  115. }
  116. }
  117. }
  118. else
  119. {
  120. for(uword row=0; row < X_n_rows; ++row)
  121. {
  122. for(uword col=0; col < X_n_cols; ++col)
  123. {
  124. const eT val = X.at(row,col);
  125. if(arma_isfinite(val))
  126. {
  127. eT opt_dist = (center_0 >= val) ? (center_0 - val) : (val - center_0);
  128. uword opt_index = 0;
  129. for(uword j=1; j < C_n_elem; ++j)
  130. {
  131. const eT center = C_mem[j];
  132. const eT dist = (center >= val) ? (center - val) : (val - center);
  133. if(dist < opt_dist)
  134. {
  135. opt_dist = dist;
  136. opt_index = j;
  137. }
  138. else
  139. {
  140. break;
  141. }
  142. }
  143. out.at(row,opt_index)++;
  144. }
  145. else
  146. {
  147. // -inf
  148. if(val < eT(0)) { out.at(row,0)++; }
  149. // +inf
  150. if(val > eT(0)) { out.at(row,C_n_elem-1)++; }
  151. // ignore NaN
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. template<typename T1, typename T2>
  159. inline
  160. void
  161. glue_hist::apply(Mat<uword>& out, const mtGlue<uword,T1,T2,glue_hist>& expr)
  162. {
  163. arma_extra_debug_sigprint();
  164. const uword dim = expr.aux_uword;
  165. arma_debug_check( (dim > 1), "hist(): parameter 'dim' must be 0 or 1" );
  166. const quasi_unwrap<T1> UA(expr.A);
  167. const quasi_unwrap<T2> UB(expr.B);
  168. if(UA.is_alias(out) || UB.is_alias(out))
  169. {
  170. Mat<uword> tmp;
  171. glue_hist::apply_noalias(tmp, UA.M, UB.M, dim);
  172. out.steal_mem(tmp);
  173. }
  174. else
  175. {
  176. glue_hist::apply_noalias(out, UA.M, UB.M, dim);
  177. }
  178. }
  179. template<typename T1, typename T2>
  180. inline
  181. void
  182. glue_hist_default::apply(Mat<uword>& out, const mtGlue<uword,T1,T2,glue_hist_default>& expr)
  183. {
  184. arma_extra_debug_sigprint();
  185. const quasi_unwrap<T1> UA(expr.A);
  186. const quasi_unwrap<T2> UB(expr.B);
  187. const uword dim = (T1::is_xvec) ? uword(UA.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  188. if(UA.is_alias(out) || UB.is_alias(out))
  189. {
  190. Mat<uword> tmp;
  191. glue_hist::apply_noalias(tmp, UA.M, UB.M, dim);
  192. out.steal_mem(tmp);
  193. }
  194. else
  195. {
  196. glue_hist::apply_noalias(out, UA.M, UB.M, dim);
  197. }
  198. }
  199. //! @}