spop_var_meat.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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_var
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. spop_var::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_var>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. //typedef typename T1::elem_type in_eT;
  24. typedef typename T1::pod_type out_eT;
  25. const uword norm_type = in.aux_uword_a;
  26. const uword dim = in.aux_uword_b;
  27. arma_debug_check( (norm_type > 1), "var(): parameter 'norm_type' must be 0 or 1" );
  28. arma_debug_check( (dim > 1), "var(): parameter 'dim' must be 0 or 1" );
  29. const SpProxy<T1> p(in.m);
  30. if(p.is_alias(out) == false)
  31. {
  32. spop_var::apply_noalias(out, p, norm_type, dim);
  33. }
  34. else
  35. {
  36. SpMat<out_eT> tmp;
  37. spop_var::apply_noalias(tmp, p, norm_type, dim);
  38. out.steal_mem(tmp);
  39. }
  40. }
  41. template<typename T1>
  42. inline
  43. void
  44. spop_var::apply_noalias
  45. (
  46. SpMat<typename T1::pod_type>& out,
  47. const SpProxy<T1>& p,
  48. const uword norm_type,
  49. const uword dim
  50. )
  51. {
  52. arma_extra_debug_sigprint();
  53. typedef typename T1::elem_type in_eT;
  54. //typedef typename T1::pod_type out_eT;
  55. const uword p_n_rows = p.get_n_rows();
  56. const uword p_n_cols = p.get_n_cols();
  57. // TODO: this is slow; rewrite based on the approach used by sparse mean()
  58. if(dim == 0) // find variance in each column
  59. {
  60. arma_extra_debug_print("spop_var::apply_noalias(): dim = 0");
  61. out.set_size((p_n_rows > 0) ? 1 : 0, p_n_cols);
  62. if( (p_n_rows == 0) || (p.get_n_nonzero() == 0) ) { return; }
  63. for(uword col = 0; col < p_n_cols; ++col)
  64. {
  65. if(SpProxy<T1>::use_iterator)
  66. {
  67. // We must use an iterator; we can't access memory directly.
  68. typename SpProxy<T1>::const_iterator_type it = p.begin_col(col);
  69. typename SpProxy<T1>::const_iterator_type end = p.begin_col(col + 1);
  70. const uword n_zero = p_n_rows - (end.pos() - it.pos());
  71. // in_eT is used just to get the specialization right (complex / noncomplex)
  72. out.at(0, col) = spop_var::iterator_var(it, end, n_zero, norm_type, in_eT(0));
  73. }
  74. else
  75. {
  76. // We can use direct memory access to calculate the variance.
  77. out.at(0, col) = spop_var::direct_var
  78. (
  79. &p.get_values()[p.get_col_ptrs()[col]],
  80. p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col],
  81. p_n_rows,
  82. norm_type
  83. );
  84. }
  85. }
  86. }
  87. else
  88. if(dim == 1) // find variance in each row
  89. {
  90. arma_extra_debug_print("spop_var::apply_noalias(): dim = 1");
  91. out.set_size(p_n_rows, (p_n_cols > 0) ? 1 : 0);
  92. if( (p_n_cols == 0) || (p.get_n_nonzero() == 0) ) { return; }
  93. for(uword row = 0; row < p_n_rows; ++row)
  94. {
  95. // We have to use an iterator here regardless of whether or not we can
  96. // directly access memory.
  97. typename SpProxy<T1>::const_row_iterator_type it = p.begin_row(row);
  98. typename SpProxy<T1>::const_row_iterator_type end = p.end_row(row);
  99. const uword n_zero = p_n_cols - (end.pos() - it.pos());
  100. out.at(row, 0) = spop_var::iterator_var(it, end, n_zero, norm_type, in_eT(0));
  101. }
  102. }
  103. }
  104. template<typename T1>
  105. inline
  106. typename T1::pod_type
  107. spop_var::var_vec
  108. (
  109. const T1& X,
  110. const uword norm_type
  111. )
  112. {
  113. arma_extra_debug_sigprint();
  114. arma_debug_check( (norm_type > 1), "var(): parameter 'norm_type' must be 0 or 1" );
  115. // conditionally unwrap it into a temporary and then directly operate.
  116. const unwrap_spmat<T1> tmp(X);
  117. return direct_var(tmp.M.values, tmp.M.n_nonzero, tmp.M.n_elem, norm_type);
  118. }
  119. template<typename eT>
  120. inline
  121. eT
  122. spop_var::direct_var
  123. (
  124. const eT* const X,
  125. const uword length,
  126. const uword N,
  127. const uword norm_type
  128. )
  129. {
  130. arma_extra_debug_sigprint();
  131. if(length >= 2 && N >= 2)
  132. {
  133. const eT acc1 = spop_mean::direct_mean(X, length, N);
  134. eT acc2 = eT(0);
  135. eT acc3 = eT(0);
  136. uword i, j;
  137. for(i = 0, j = 1; j < length; i += 2, j += 2)
  138. {
  139. const eT Xi = X[i];
  140. const eT Xj = X[j];
  141. const eT tmpi = acc1 - Xi;
  142. const eT tmpj = acc1 - Xj;
  143. acc2 += tmpi * tmpi + tmpj * tmpj;
  144. acc3 += tmpi + tmpj;
  145. }
  146. if(i < length)
  147. {
  148. const eT Xi = X[i];
  149. const eT tmpi = acc1 - Xi;
  150. acc2 += tmpi * tmpi;
  151. acc3 += tmpi;
  152. }
  153. // Now add in all zero elements.
  154. acc2 += (N - length) * (acc1 * acc1);
  155. acc3 += (N - length) * acc1;
  156. const eT norm_val = (norm_type == 0) ? eT(N - 1) : eT(N);
  157. const eT var_val = (acc2 - (acc3 * acc3) / eT(N)) / norm_val;
  158. return var_val;
  159. }
  160. else if(length == 1 && N > 1) // if N == 1, then variance is zero.
  161. {
  162. const eT mean = X[0] / eT(N);
  163. const eT val = mean - X[0];
  164. const eT acc2 = (val * val) + (N - length) * (mean * mean);
  165. const eT acc3 = val + (N - length) * mean;
  166. const eT norm_val = (norm_type == 0) ? eT(N - 1) : eT(N);
  167. const eT var_val = (acc2 - (acc3 * acc3) / eT(N)) / norm_val;
  168. return var_val;
  169. }
  170. else
  171. {
  172. return eT(0);
  173. }
  174. }
  175. template<typename T>
  176. inline
  177. T
  178. spop_var::direct_var
  179. (
  180. const std::complex<T>* const X,
  181. const uword length,
  182. const uword N,
  183. const uword norm_type
  184. )
  185. {
  186. arma_extra_debug_sigprint();
  187. typedef typename std::complex<T> eT;
  188. if(length >= 2 && N >= 2)
  189. {
  190. const eT acc1 = spop_mean::direct_mean(X, length, N);
  191. T acc2 = T(0);
  192. eT acc3 = eT(0);
  193. for (uword i = 0; i < length; ++i)
  194. {
  195. const eT tmp = acc1 - X[i];
  196. acc2 += std::norm(tmp);
  197. acc3 += tmp;
  198. }
  199. // Add zero elements to sums
  200. acc2 += std::norm(acc1) * T(N - length);
  201. acc3 += acc1 * T(N - length);
  202. const T norm_val = (norm_type == 0) ? T(N - 1) : T(N);
  203. const T var_val = (acc2 - std::norm(acc3) / T(N)) / norm_val;
  204. return var_val;
  205. }
  206. else if(length == 1 && N > 1) // if N == 1, then variance is zero.
  207. {
  208. const eT mean = X[0] / T(N);
  209. const eT val = mean - X[0];
  210. const T acc2 = std::norm(val) + (N - length) * std::norm(mean);
  211. const eT acc3 = val + T(N - length) * mean;
  212. const T norm_val = (norm_type == 0) ? T(N - 1) : T(N);
  213. const T var_val = (acc2 - std::norm(acc3) / T(N)) / norm_val;
  214. return var_val;
  215. }
  216. else
  217. {
  218. return T(0); // All elements are zero
  219. }
  220. }
  221. template<typename T1, typename eT>
  222. inline
  223. eT
  224. spop_var::iterator_var
  225. (
  226. T1& it,
  227. const T1& end,
  228. const uword n_zero,
  229. const uword norm_type,
  230. const eT junk1,
  231. const typename arma_not_cx<eT>::result* junk2
  232. )
  233. {
  234. arma_extra_debug_sigprint();
  235. arma_ignore(junk1);
  236. arma_ignore(junk2);
  237. T1 new_it(it); // for mean
  238. // T1 backup_it(it); // in case we have to call robust iterator_var
  239. eT mean = spop_mean::iterator_mean(new_it, end, n_zero, eT(0));
  240. eT acc2 = eT(0);
  241. eT acc3 = eT(0);
  242. const uword it_begin_pos = it.pos();
  243. while (it != end)
  244. {
  245. const eT tmp = mean - (*it);
  246. acc2 += (tmp * tmp);
  247. acc3 += (tmp);
  248. ++it;
  249. }
  250. const uword n_nonzero = (it.pos() - it_begin_pos);
  251. if (n_nonzero == 0)
  252. {
  253. return eT(0);
  254. }
  255. if (n_nonzero + n_zero == 1)
  256. {
  257. return eT(0); // only one element
  258. }
  259. // Add in entries for zeros.
  260. acc2 += eT(n_zero) * (mean * mean);
  261. acc3 += eT(n_zero) * mean;
  262. const eT norm_val = (norm_type == 0) ? eT(n_zero + n_nonzero - 1) : eT(n_zero + n_nonzero);
  263. const eT var_val = (acc2 - (acc3 * acc3) / eT(n_nonzero + n_zero)) / norm_val;
  264. return var_val;
  265. }
  266. template<typename T1, typename eT>
  267. inline
  268. typename get_pod_type<eT>::result
  269. spop_var::iterator_var
  270. (
  271. T1& it,
  272. const T1& end,
  273. const uword n_zero,
  274. const uword norm_type,
  275. const eT junk1,
  276. const typename arma_cx_only<eT>::result* junk2
  277. )
  278. {
  279. arma_extra_debug_sigprint();
  280. arma_ignore(junk1);
  281. arma_ignore(junk2);
  282. typedef typename get_pod_type<eT>::result T;
  283. T1 new_it(it); // for mean
  284. // T1 backup_it(it); // in case we have to call robust iterator_var
  285. eT mean = spop_mean::iterator_mean(new_it, end, n_zero, eT(0));
  286. T acc2 = T(0);
  287. eT acc3 = eT(0);
  288. const uword it_begin_pos = it.pos();
  289. while (it != end)
  290. {
  291. eT tmp = mean - (*it);
  292. acc2 += std::norm(tmp);
  293. acc3 += (tmp);
  294. ++it;
  295. }
  296. const uword n_nonzero = (it.pos() - it_begin_pos);
  297. if (n_nonzero == 0)
  298. {
  299. return T(0);
  300. }
  301. if (n_nonzero + n_zero == 1)
  302. {
  303. return T(0); // only one element
  304. }
  305. // Add in entries for zero elements.
  306. acc2 += T(n_zero) * std::norm(mean);
  307. acc3 += T(n_zero) * mean;
  308. const T norm_val = (norm_type == 0) ? T(n_zero + n_nonzero - 1) : T(n_zero + n_nonzero);
  309. const T var_val = (acc2 - std::norm(acc3) / T(n_nonzero + n_zero)) / norm_val;
  310. return var_val;
  311. }
  312. //! @}