op_median_meat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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_median
  16. //! @{
  17. //! \brief
  18. //! For each row or for each column, find the median value.
  19. //! The result is stored in a dense matrix that has either one column or one row.
  20. //! The dimension, for which the medians are found, is set via the median() function.
  21. template<typename eT, typename T1>
  22. inline
  23. void
  24. op_median::apply(Mat<eT>& out, const Op<T1,op_median>& in, const typename arma_not_cx<eT>::result* junk)
  25. {
  26. arma_extra_debug_sigprint();
  27. arma_ignore(junk);
  28. // typedef typename T1::elem_type eT;
  29. const uword dim = in.aux_uword_a;
  30. arma_debug_check( (dim > 1), "median(): parameter 'dim' must be 0 or 1" );
  31. const Proxy<T1> P(in.m);
  32. typedef typename Proxy<T1>::stored_type P_stored_type;
  33. const bool is_alias = P.is_alias(out);
  34. if(is_Mat<P_stored_type>::value || is_alias)
  35. {
  36. const unwrap_check<P_stored_type> tmp(P.Q, is_alias);
  37. const typename unwrap_check<P_stored_type>::stored_type& X = tmp.M;
  38. const uword X_n_rows = X.n_rows;
  39. const uword X_n_cols = X.n_cols;
  40. if(dim == 0) // in each column
  41. {
  42. arma_extra_debug_print("op_median::apply(): dim = 0");
  43. out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols);
  44. if(X_n_rows > 0)
  45. {
  46. std::vector<eT> tmp_vec(X_n_rows);
  47. for(uword col=0; col < X_n_cols; ++col)
  48. {
  49. arrayops::copy( &(tmp_vec[0]), X.colptr(col), X_n_rows );
  50. out[col] = op_median::direct_median(tmp_vec);
  51. }
  52. }
  53. }
  54. else // in each row
  55. {
  56. arma_extra_debug_print("op_median::apply(): dim = 1");
  57. out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0);
  58. if(X_n_cols > 0)
  59. {
  60. std::vector<eT> tmp_vec(X_n_cols);
  61. for(uword row=0; row < X_n_rows; ++row)
  62. {
  63. for(uword col=0; col < X_n_cols; ++col) { tmp_vec[col] = X.at(row,col); }
  64. out[row] = op_median::direct_median(tmp_vec);
  65. }
  66. }
  67. }
  68. }
  69. else
  70. {
  71. const uword P_n_rows = P.get_n_rows();
  72. const uword P_n_cols = P.get_n_cols();
  73. if(dim == 0) // in each column
  74. {
  75. arma_extra_debug_print("op_median::apply(): dim = 0");
  76. out.set_size((P_n_rows > 0) ? 1 : 0, P_n_cols);
  77. if(P_n_rows > 0)
  78. {
  79. std::vector<eT> tmp_vec(P_n_rows);
  80. for(uword col=0; col < P_n_cols; ++col)
  81. {
  82. for(uword row=0; row < P_n_rows; ++row) { tmp_vec[row] = P.at(row,col); }
  83. out[col] = op_median::direct_median(tmp_vec);
  84. }
  85. }
  86. }
  87. else // in each row
  88. {
  89. arma_extra_debug_print("op_median::apply(): dim = 1");
  90. out.set_size(P_n_rows, (P_n_cols > 0) ? 1 : 0);
  91. if(P_n_cols > 0)
  92. {
  93. std::vector<eT> tmp_vec(P_n_cols);
  94. for(uword row=0; row < P_n_rows; ++row)
  95. {
  96. for(uword col=0; col < P_n_cols; ++col) { tmp_vec[col] = P.at(row,col); }
  97. out[row] = op_median::direct_median(tmp_vec);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. //! Implementation for complex numbers
  104. template<typename eT, typename T1>
  105. inline
  106. void
  107. op_median::apply(Mat<eT>& out, const Op<T1,op_median>& in, const typename arma_cx_only<eT>::result* junk)
  108. {
  109. arma_extra_debug_sigprint();
  110. arma_ignore(junk);
  111. // typedef typename std::complex<T> eT;
  112. typedef typename get_pod_type<eT>::result T;
  113. arma_type_check(( is_same_type<eT, typename T1::elem_type>::no ));
  114. const unwrap_check<T1> tmp(in.m, out);
  115. const Mat<eT>& X = tmp.M;
  116. const uword X_n_rows = X.n_rows;
  117. const uword X_n_cols = X.n_cols;
  118. const uword dim = in.aux_uword_a;
  119. arma_debug_check( (dim > 1), "median(): parameter 'dim' must be 0 or 1" );
  120. if(dim == 0) // in each column
  121. {
  122. arma_extra_debug_print("op_median::apply(): dim = 0");
  123. out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols);
  124. if(X_n_rows > 0)
  125. {
  126. std::vector< arma_cx_median_packet<T> > tmp_vec(X_n_rows);
  127. for(uword col=0; col<X_n_cols; ++col)
  128. {
  129. const eT* colmem = X.colptr(col);
  130. for(uword row=0; row<X_n_rows; ++row)
  131. {
  132. tmp_vec[row].val = std::abs(colmem[row]);
  133. tmp_vec[row].index = row;
  134. }
  135. uword index1;
  136. uword index2;
  137. op_median::direct_cx_median_index(index1, index2, tmp_vec);
  138. out[col] = op_mean::robust_mean(colmem[index1], colmem[index2]);
  139. }
  140. }
  141. }
  142. else
  143. if(dim == 1) // in each row
  144. {
  145. arma_extra_debug_print("op_median::apply(): dim = 1");
  146. out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0);
  147. if(X_n_cols > 0)
  148. {
  149. std::vector< arma_cx_median_packet<T> > tmp_vec(X_n_cols);
  150. for(uword row=0; row<X_n_rows; ++row)
  151. {
  152. for(uword col=0; col<X_n_cols; ++col)
  153. {
  154. tmp_vec[col].val = std::abs(X.at(row,col));
  155. tmp_vec[col].index = col;
  156. }
  157. uword index1;
  158. uword index2;
  159. op_median::direct_cx_median_index(index1, index2, tmp_vec);
  160. out[row] = op_mean::robust_mean( X.at(row,index1), X.at(row,index2) );
  161. }
  162. }
  163. }
  164. }
  165. template<typename T1>
  166. inline
  167. typename T1::elem_type
  168. op_median::median_vec
  169. (
  170. const T1& X,
  171. const typename arma_not_cx<typename T1::elem_type>::result* junk
  172. )
  173. {
  174. arma_extra_debug_sigprint();
  175. arma_ignore(junk);
  176. typedef typename T1::elem_type eT;
  177. typedef typename Proxy<T1>::stored_type P_stored_type;
  178. const Proxy<T1> P(X);
  179. const uword n_elem = P.get_n_elem();
  180. if(n_elem == 0)
  181. {
  182. arma_debug_check(true, "median(): object has no elements");
  183. return Datum<eT>::nan;
  184. }
  185. std::vector<eT> tmp_vec(n_elem);
  186. if(is_Mat<P_stored_type>::value)
  187. {
  188. const unwrap<P_stored_type> tmp(P.Q);
  189. const typename unwrap<P_stored_type>::stored_type& Y = tmp.M;
  190. arrayops::copy( &(tmp_vec[0]), Y.memptr(), n_elem );
  191. }
  192. else
  193. {
  194. if(Proxy<T1>::use_at == false)
  195. {
  196. typedef typename Proxy<T1>::ea_type ea_type;
  197. ea_type A = P.get_ea();
  198. for(uword i=0; i<n_elem; ++i) { tmp_vec[i] = A[i]; }
  199. }
  200. else
  201. {
  202. const uword n_rows = P.get_n_rows();
  203. const uword n_cols = P.get_n_cols();
  204. if(n_cols == 1)
  205. {
  206. for(uword row=0; row < n_rows; ++row) { tmp_vec[row] = P.at(row,0); }
  207. }
  208. else
  209. if(n_rows == 1)
  210. {
  211. for(uword col=0; col < n_cols; ++col) { tmp_vec[col] = P.at(0,col); }
  212. }
  213. else
  214. {
  215. arma_stop_logic_error("op_median::median_vec(): expected a vector" );
  216. }
  217. }
  218. }
  219. return op_median::direct_median(tmp_vec);
  220. }
  221. template<typename T1>
  222. inline
  223. typename T1::elem_type
  224. op_median::median_vec
  225. (
  226. const T1& X,
  227. const typename arma_cx_only<typename T1::elem_type>::result* junk
  228. )
  229. {
  230. arma_extra_debug_sigprint();
  231. arma_ignore(junk);
  232. typedef typename T1::elem_type eT;
  233. typedef typename T1::pod_type T;
  234. const Proxy<T1> P(X);
  235. const uword n_elem = P.get_n_elem();
  236. if(n_elem == 0)
  237. {
  238. arma_debug_check(true, "median(): object has no elements");
  239. return Datum<eT>::nan;
  240. }
  241. std::vector< arma_cx_median_packet<T> > tmp_vec(n_elem);
  242. if(Proxy<T1>::use_at == false)
  243. {
  244. typedef typename Proxy<T1>::ea_type ea_type;
  245. ea_type A = P.get_ea();
  246. for(uword i=0; i<n_elem; ++i)
  247. {
  248. tmp_vec[i].val = std::abs( A[i] );
  249. tmp_vec[i].index = i;
  250. }
  251. uword index1;
  252. uword index2;
  253. op_median::direct_cx_median_index(index1, index2, tmp_vec);
  254. return op_mean::robust_mean( A[index1], A[index2] );
  255. }
  256. else
  257. {
  258. const uword n_rows = P.get_n_rows();
  259. const uword n_cols = P.get_n_cols();
  260. if(n_cols == 1)
  261. {
  262. for(uword row=0; row < n_rows; ++row)
  263. {
  264. tmp_vec[row].val = std::abs( P.at(row,0) );
  265. tmp_vec[row].index = row;
  266. }
  267. uword index1;
  268. uword index2;
  269. op_median::direct_cx_median_index(index1, index2, tmp_vec);
  270. return op_mean::robust_mean( P.at(index1,0), P.at(index2,0) );
  271. }
  272. else
  273. if(n_rows == 1)
  274. {
  275. for(uword col=0; col < n_cols; ++col)
  276. {
  277. tmp_vec[col].val = std::abs( P.at(0,col) );
  278. tmp_vec[col].index = col;
  279. }
  280. uword index1;
  281. uword index2;
  282. op_median::direct_cx_median_index(index1, index2, tmp_vec);
  283. return op_mean::robust_mean( P.at(0,index1), P.at(0,index2) );
  284. }
  285. else
  286. {
  287. arma_stop_logic_error("op_median::median_vec(): expected a vector" );
  288. return eT(0);
  289. }
  290. }
  291. }
  292. //! find the median value of a std::vector (contents is modified)
  293. template<typename eT>
  294. inline
  295. eT
  296. op_median::direct_median(std::vector<eT>& X)
  297. {
  298. arma_extra_debug_sigprint();
  299. const uword n_elem = uword(X.size());
  300. const uword half = n_elem/2;
  301. typename std::vector<eT>::iterator first = X.begin();
  302. typename std::vector<eT>::iterator nth = first + half;
  303. typename std::vector<eT>::iterator pastlast = X.end();
  304. std::nth_element(first, nth, pastlast);
  305. if((n_elem % 2) == 0) // even number of elements
  306. {
  307. typename std::vector<eT>::iterator start = X.begin();
  308. typename std::vector<eT>::iterator pastend = start + half;
  309. const eT val1 = (*nth);
  310. const eT val2 = (*(std::max_element(start, pastend)));
  311. return op_mean::robust_mean(val1, val2);
  312. }
  313. else // odd number of elements
  314. {
  315. return (*nth);
  316. }
  317. }
  318. template<typename T>
  319. inline
  320. void
  321. op_median::direct_cx_median_index
  322. (
  323. uword& out_index1,
  324. uword& out_index2,
  325. std::vector< arma_cx_median_packet<T> >& X
  326. )
  327. {
  328. arma_extra_debug_sigprint();
  329. typedef arma_cx_median_packet<T> eT;
  330. const uword n_elem = uword(X.size());
  331. const uword half = n_elem/2;
  332. typename std::vector<eT>::iterator first = X.begin();
  333. typename std::vector<eT>::iterator nth = first + half;
  334. typename std::vector<eT>::iterator pastlast = X.end();
  335. std::nth_element(first, nth, pastlast);
  336. out_index1 = (*nth).index;
  337. if((n_elem % 2) == 0) // even number of elements
  338. {
  339. typename std::vector<eT>::iterator start = X.begin();
  340. typename std::vector<eT>::iterator pastend = start + half;
  341. out_index2 = (*(std::max_element(start, pastend))).index;
  342. }
  343. else // odd number of elements
  344. {
  345. out_index2 = out_index1;
  346. }
  347. }
  348. //! @}