spop_mean_meat.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_mean
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. spop_mean::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_mean>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const uword dim = in.aux_uword_a;
  25. arma_debug_check( (dim > 1), "mean(): parameter 'dim' must be 0 or 1" );
  26. const SpProxy<T1> p(in.m);
  27. if(p.is_alias(out) == false)
  28. {
  29. spop_mean::apply_noalias_fast(out, p, dim);
  30. }
  31. else
  32. {
  33. SpMat<eT> tmp;
  34. spop_mean::apply_noalias_fast(tmp, p, dim);
  35. out.steal_mem(tmp);
  36. }
  37. }
  38. template<typename T1>
  39. inline
  40. void
  41. spop_mean::apply_noalias_fast
  42. (
  43. SpMat<typename T1::elem_type>& out,
  44. const SpProxy<T1>& p,
  45. const uword dim
  46. )
  47. {
  48. arma_extra_debug_sigprint();
  49. typedef typename T1::elem_type eT;
  50. typedef typename T1::pod_type T;
  51. const uword p_n_rows = p.get_n_rows();
  52. const uword p_n_cols = p.get_n_cols();
  53. if( (p_n_rows == 0) || (p_n_cols == 0) || (p.get_n_nonzero() == 0) )
  54. {
  55. if(dim == 0) { out.zeros((p_n_rows > 0) ? 1 : 0, p_n_cols); }
  56. if(dim == 1) { out.zeros(p_n_rows, (p_n_cols > 0) ? 1 : 0); }
  57. return;
  58. }
  59. if(dim == 0) // find the mean in each column
  60. {
  61. Row<eT> acc(p_n_cols, fill::zeros);
  62. eT* acc_mem = acc.memptr();
  63. if(SpProxy<T1>::use_iterator)
  64. {
  65. typename SpProxy<T1>::const_iterator_type it = p.begin();
  66. const uword N = p.get_n_nonzero();
  67. for(uword i=0; i < N; ++i) { acc_mem[it.col()] += (*it); ++it; }
  68. acc /= T(p_n_rows);
  69. }
  70. else
  71. {
  72. for(uword col = 0; col < p_n_cols; ++col)
  73. {
  74. acc_mem[col] = arrayops::accumulate
  75. (
  76. &p.get_values()[p.get_col_ptrs()[col]],
  77. p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col]
  78. ) / T(p_n_rows);
  79. }
  80. }
  81. out = acc;
  82. }
  83. else
  84. if(dim == 1) // find the mean in each row
  85. {
  86. Col<eT> acc(p_n_rows, fill::zeros);
  87. eT* acc_mem = acc.memptr();
  88. typename SpProxy<T1>::const_iterator_type it = p.begin();
  89. const uword N = p.get_n_nonzero();
  90. for(uword i=0; i < N; ++i) { acc_mem[it.row()] += (*it); ++it; }
  91. acc /= T(p_n_cols);
  92. out = acc;
  93. }
  94. if(out.is_finite() == false)
  95. {
  96. spop_mean::apply_noalias_slow(out, p, dim);
  97. }
  98. }
  99. template<typename T1>
  100. inline
  101. void
  102. spop_mean::apply_noalias_slow
  103. (
  104. SpMat<typename T1::elem_type>& out,
  105. const SpProxy<T1>& p,
  106. const uword dim
  107. )
  108. {
  109. arma_extra_debug_sigprint();
  110. typedef typename T1::elem_type eT;
  111. const uword p_n_rows = p.get_n_rows();
  112. const uword p_n_cols = p.get_n_cols();
  113. if(dim == 0) // find the mean in each column
  114. {
  115. arma_extra_debug_print("spop_mean::apply_noalias(): dim = 0");
  116. out.set_size((p_n_rows > 0) ? 1 : 0, p_n_cols);
  117. if( (p_n_rows == 0) || (p.get_n_nonzero() == 0) ) { return; }
  118. for(uword col = 0; col < p_n_cols; ++col)
  119. {
  120. // Do we have to use an iterator or can we use memory directly?
  121. if(SpProxy<T1>::use_iterator)
  122. {
  123. typename SpProxy<T1>::const_iterator_type it = p.begin_col(col);
  124. typename SpProxy<T1>::const_iterator_type end = p.begin_col(col + 1);
  125. const uword n_zero = p_n_rows - (end.pos() - it.pos());
  126. out.at(0,col) = spop_mean::iterator_mean(it, end, n_zero, eT(0));
  127. }
  128. else
  129. {
  130. out.at(0,col) = spop_mean::direct_mean
  131. (
  132. &p.get_values()[p.get_col_ptrs()[col]],
  133. p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col],
  134. p_n_rows
  135. );
  136. }
  137. }
  138. }
  139. else
  140. if(dim == 1) // find the mean in each row
  141. {
  142. arma_extra_debug_print("spop_mean::apply_noalias(): dim = 1");
  143. out.set_size(p_n_rows, (p_n_cols > 0) ? 1 : 0);
  144. if( (p_n_cols == 0) || (p.get_n_nonzero() == 0) ) { return; }
  145. for(uword row = 0; row < p_n_rows; ++row)
  146. {
  147. // We must use an iterator regardless of how it is stored.
  148. typename SpProxy<T1>::const_row_iterator_type it = p.begin_row(row);
  149. typename SpProxy<T1>::const_row_iterator_type end = p.end_row(row);
  150. const uword n_zero = p_n_cols - (end.pos() - it.pos());
  151. out.at(row,0) = spop_mean::iterator_mean(it, end, n_zero, eT(0));
  152. }
  153. }
  154. }
  155. template<typename eT>
  156. inline
  157. eT
  158. spop_mean::direct_mean
  159. (
  160. const eT* const X,
  161. const uword length,
  162. const uword N
  163. )
  164. {
  165. arma_extra_debug_sigprint();
  166. typedef typename get_pod_type<eT>::result T;
  167. const eT result = ((length > 0) && (N > 0)) ? eT(arrayops::accumulate(X, length) / T(N)) : eT(0);
  168. return arma_isfinite(result) ? result : spop_mean::direct_mean_robust(X, length, N);
  169. }
  170. template<typename eT>
  171. inline
  172. eT
  173. spop_mean::direct_mean_robust
  174. (
  175. const eT* const X,
  176. const uword length,
  177. const uword N
  178. )
  179. {
  180. arma_extra_debug_sigprint();
  181. typedef typename get_pod_type<eT>::result T;
  182. uword i, j;
  183. eT r_mean = eT(0);
  184. const uword diff = (N - length); // number of zeros
  185. for(i = 0, j = 1; j < length; i += 2, j += 2)
  186. {
  187. const eT Xi = X[i];
  188. const eT Xj = X[j];
  189. r_mean += (Xi - r_mean) / T(diff + j);
  190. r_mean += (Xj - r_mean) / T(diff + j + 1);
  191. }
  192. if(i < length)
  193. {
  194. const eT Xi = X[i];
  195. r_mean += (Xi - r_mean) / T(diff + i + 1);
  196. }
  197. return r_mean;
  198. }
  199. template<typename T1>
  200. inline
  201. typename T1::elem_type
  202. spop_mean::mean_all(const SpBase<typename T1::elem_type, T1>& X)
  203. {
  204. arma_extra_debug_sigprint();
  205. SpProxy<T1> p(X.get_ref());
  206. if(SpProxy<T1>::use_iterator)
  207. {
  208. typename SpProxy<T1>::const_iterator_type it = p.begin();
  209. typename SpProxy<T1>::const_iterator_type end = p.end();
  210. return spop_mean::iterator_mean(it, end, p.get_n_elem() - p.get_n_nonzero(), typename T1::elem_type(0));
  211. }
  212. else // use_iterator == false; that is, we can directly access the values array
  213. {
  214. return spop_mean::direct_mean(p.get_values(), p.get_n_nonzero(), p.get_n_elem());
  215. }
  216. }
  217. template<typename T1, typename spop_type>
  218. inline
  219. typename T1::elem_type
  220. spop_mean::mean_all(const SpOp<T1, spop_type>& expr)
  221. {
  222. arma_extra_debug_sigprint();
  223. typedef typename T1::elem_type eT;
  224. const bool is_vectorise = \
  225. (is_same_type<spop_type, spop_vectorise_row>::yes)
  226. || (is_same_type<spop_type, spop_vectorise_col>::yes)
  227. || (is_same_type<spop_type, spop_vectorise_all>::yes);
  228. if(is_vectorise)
  229. {
  230. return spop_mean::mean_all(expr.m);
  231. }
  232. const SpMat<eT> tmp = expr;
  233. return spop_mean::mean_all(tmp);
  234. }
  235. template<typename T1, typename eT>
  236. inline
  237. eT
  238. spop_mean::iterator_mean(T1& it, const T1& end, const uword n_zero, const eT junk)
  239. {
  240. arma_extra_debug_sigprint();
  241. arma_ignore(junk);
  242. typedef typename get_pod_type<eT>::result T;
  243. eT acc = eT(0);
  244. T1 backup_it(it); // in case we have to use robust iterator_mean
  245. const uword it_begin_pos = it.pos();
  246. while (it != end)
  247. {
  248. acc += (*it);
  249. ++it;
  250. }
  251. const uword count = n_zero + (it.pos() - it_begin_pos);
  252. const eT result = (count > 0) ? eT(acc / T(count)) : eT(0);
  253. return arma_isfinite(result) ? result : spop_mean::iterator_mean_robust(backup_it, end, n_zero, eT(0));
  254. }
  255. template<typename T1, typename eT>
  256. inline
  257. eT
  258. spop_mean::iterator_mean_robust(T1& it, const T1& end, const uword n_zero, const eT junk)
  259. {
  260. arma_extra_debug_sigprint();
  261. arma_ignore(junk);
  262. typedef typename get_pod_type<eT>::result T;
  263. eT r_mean = eT(0);
  264. const uword it_begin_pos = it.pos();
  265. while (it != end)
  266. {
  267. r_mean += ((*it - r_mean) / T(n_zero + (it.pos() - it_begin_pos) + 1));
  268. ++it;
  269. }
  270. return r_mean;
  271. }
  272. //! @}