op_find_meat.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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_find
  16. //! @{
  17. template<typename T1>
  18. inline
  19. uword
  20. op_find::helper
  21. (
  22. Mat<uword>& indices,
  23. const Base<typename T1::elem_type, T1>& X
  24. )
  25. {
  26. arma_extra_debug_sigprint();
  27. typedef typename T1::elem_type eT;
  28. const Proxy<T1> A(X.get_ref());
  29. const uword n_elem = A.get_n_elem();
  30. indices.set_size(n_elem, 1);
  31. uword* indices_mem = indices.memptr();
  32. uword n_nz = 0;
  33. if(Proxy<T1>::use_at == false)
  34. {
  35. typename Proxy<T1>::ea_type PA = A.get_ea();
  36. for(uword i=0; i<n_elem; ++i)
  37. {
  38. if(PA[i] != eT(0)) { indices_mem[n_nz] = i; ++n_nz; }
  39. }
  40. }
  41. else
  42. {
  43. const uword n_rows = A.get_n_rows();
  44. const uword n_cols = A.get_n_cols();
  45. uword i = 0;
  46. for(uword col=0; col < n_cols; ++col)
  47. for(uword row=0; row < n_rows; ++row)
  48. {
  49. if(A.at(row,col) != eT(0)) { indices_mem[n_nz] = i; ++n_nz; }
  50. ++i;
  51. }
  52. }
  53. return n_nz;
  54. }
  55. template<typename T1, typename op_type>
  56. inline
  57. uword
  58. op_find::helper
  59. (
  60. Mat<uword>& indices,
  61. const mtOp<uword, T1, op_type>& X,
  62. const typename arma_op_rel_only<op_type>::result* junk1,
  63. const typename arma_not_cx<typename T1::elem_type>::result* junk2
  64. )
  65. {
  66. arma_extra_debug_sigprint();
  67. arma_ignore(junk1);
  68. arma_ignore(junk2);
  69. typedef typename T1::elem_type eT;
  70. const eT val = X.aux;
  71. const Proxy<T1> A(X.m);
  72. const uword n_elem = A.get_n_elem();
  73. indices.set_size(n_elem, 1);
  74. uword* indices_mem = indices.memptr();
  75. uword n_nz = 0;
  76. if(Proxy<T1>::use_at == false)
  77. {
  78. typename Proxy<T1>::ea_type PA = A.get_ea();
  79. uword i,j;
  80. for(i=0, j=1; j < n_elem; i+=2, j+=2)
  81. {
  82. const eT tpi = PA[i];
  83. const eT tpj = PA[j];
  84. bool not_zero_i;
  85. bool not_zero_j;
  86. if(is_same_type<op_type, op_rel_lt_pre >::yes) { not_zero_i = (val < tpi); }
  87. else if(is_same_type<op_type, op_rel_lt_post >::yes) { not_zero_i = (tpi < val); }
  88. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { not_zero_i = (val > tpi); }
  89. else if(is_same_type<op_type, op_rel_gt_post >::yes) { not_zero_i = (tpi > val); }
  90. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { not_zero_i = (val <= tpi); }
  91. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { not_zero_i = (tpi <= val); }
  92. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { not_zero_i = (val >= tpi); }
  93. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { not_zero_i = (tpi >= val); }
  94. else if(is_same_type<op_type, op_rel_eq >::yes) { not_zero_i = (tpi == val); }
  95. else if(is_same_type<op_type, op_rel_noteq >::yes) { not_zero_i = (tpi != val); }
  96. else { not_zero_i = false; }
  97. if(is_same_type<op_type, op_rel_lt_pre >::yes) { not_zero_j = (val < tpj); }
  98. else if(is_same_type<op_type, op_rel_lt_post >::yes) { not_zero_j = (tpj < val); }
  99. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { not_zero_j = (val > tpj); }
  100. else if(is_same_type<op_type, op_rel_gt_post >::yes) { not_zero_j = (tpj > val); }
  101. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { not_zero_j = (val <= tpj); }
  102. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { not_zero_j = (tpj <= val); }
  103. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { not_zero_j = (val >= tpj); }
  104. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { not_zero_j = (tpj >= val); }
  105. else if(is_same_type<op_type, op_rel_eq >::yes) { not_zero_j = (tpj == val); }
  106. else if(is_same_type<op_type, op_rel_noteq >::yes) { not_zero_j = (tpj != val); }
  107. else { not_zero_j = false; }
  108. if(not_zero_i == true) { indices_mem[n_nz] = i; ++n_nz; }
  109. if(not_zero_j == true) { indices_mem[n_nz] = j; ++n_nz; }
  110. }
  111. if(i < n_elem)
  112. {
  113. bool not_zero;
  114. const eT tmp = PA[i];
  115. if(is_same_type<op_type, op_rel_lt_pre >::yes) { not_zero = (val < tmp); }
  116. else if(is_same_type<op_type, op_rel_lt_post >::yes) { not_zero = (tmp < val); }
  117. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { not_zero = (val > tmp); }
  118. else if(is_same_type<op_type, op_rel_gt_post >::yes) { not_zero = (tmp > val); }
  119. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { not_zero = (val <= tmp); }
  120. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { not_zero = (tmp <= val); }
  121. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { not_zero = (val >= tmp); }
  122. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { not_zero = (tmp >= val); }
  123. else if(is_same_type<op_type, op_rel_eq >::yes) { not_zero = (tmp == val); }
  124. else if(is_same_type<op_type, op_rel_noteq >::yes) { not_zero = (tmp != val); }
  125. else { not_zero = false; }
  126. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  127. }
  128. }
  129. else
  130. {
  131. const uword n_rows = A.get_n_rows();
  132. const uword n_cols = A.get_n_cols();
  133. uword i = 0;
  134. for(uword col=0; col < n_cols; ++col)
  135. for(uword row=0; row < n_rows; ++row)
  136. {
  137. const eT tmp = A.at(row,col);
  138. bool not_zero;
  139. if(is_same_type<op_type, op_rel_lt_pre >::yes) { not_zero = (val < tmp); }
  140. else if(is_same_type<op_type, op_rel_lt_post >::yes) { not_zero = (tmp < val); }
  141. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { not_zero = (val > tmp); }
  142. else if(is_same_type<op_type, op_rel_gt_post >::yes) { not_zero = (tmp > val); }
  143. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { not_zero = (val <= tmp); }
  144. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { not_zero = (tmp <= val); }
  145. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { not_zero = (val >= tmp); }
  146. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { not_zero = (tmp >= val); }
  147. else if(is_same_type<op_type, op_rel_eq >::yes) { not_zero = (tmp == val); }
  148. else if(is_same_type<op_type, op_rel_noteq >::yes) { not_zero = (tmp != val); }
  149. else { not_zero = false; }
  150. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  151. ++i;
  152. }
  153. }
  154. return n_nz;
  155. }
  156. template<typename T1, typename op_type>
  157. inline
  158. uword
  159. op_find::helper
  160. (
  161. Mat<uword>& indices,
  162. const mtOp<uword, T1, op_type>& X,
  163. const typename arma_op_rel_only<op_type>::result* junk1,
  164. const typename arma_cx_only<typename T1::elem_type>::result* junk2
  165. )
  166. {
  167. arma_extra_debug_sigprint();
  168. arma_ignore(junk1);
  169. arma_ignore(junk2);
  170. typedef typename T1::elem_type eT;
  171. typedef typename Proxy<T1>::ea_type ea_type;
  172. const eT val = X.aux;
  173. const Proxy<T1> A(X.m);
  174. ea_type PA = A.get_ea();
  175. const uword n_elem = A.get_n_elem();
  176. indices.set_size(n_elem, 1);
  177. uword* indices_mem = indices.memptr();
  178. uword n_nz = 0;
  179. if(Proxy<T1>::use_at == false)
  180. {
  181. for(uword i=0; i<n_elem; ++i)
  182. {
  183. const eT tmp = PA[i];
  184. bool not_zero;
  185. if(is_same_type<op_type, op_rel_eq >::yes) { not_zero = (tmp == val); }
  186. else if(is_same_type<op_type, op_rel_noteq>::yes) { not_zero = (tmp != val); }
  187. else { not_zero = false; }
  188. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  189. }
  190. }
  191. else
  192. {
  193. const uword n_rows = A.get_n_rows();
  194. const uword n_cols = A.get_n_cols();
  195. uword i = 0;
  196. for(uword col=0; col<n_cols; ++col)
  197. for(uword row=0; row<n_rows; ++row)
  198. {
  199. const eT tmp = A.at(row,col);
  200. bool not_zero;
  201. if(is_same_type<op_type, op_rel_eq >::yes) { not_zero = (tmp == val); }
  202. else if(is_same_type<op_type, op_rel_noteq>::yes) { not_zero = (tmp != val); }
  203. else { not_zero = false; }
  204. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  205. i++;
  206. }
  207. }
  208. return n_nz;
  209. }
  210. template<typename T1, typename T2, typename glue_type>
  211. inline
  212. uword
  213. op_find::helper
  214. (
  215. Mat<uword>& indices,
  216. const mtGlue<uword, T1, T2, glue_type>& X,
  217. const typename arma_glue_rel_only<glue_type>::result* junk1,
  218. const typename arma_not_cx<typename T1::elem_type>::result* junk2,
  219. const typename arma_not_cx<typename T2::elem_type>::result* junk3
  220. )
  221. {
  222. arma_extra_debug_sigprint();
  223. arma_ignore(junk1);
  224. arma_ignore(junk2);
  225. arma_ignore(junk3);
  226. typedef typename T1::elem_type eT1;
  227. typedef typename T2::elem_type eT2;
  228. typedef typename Proxy<T1>::ea_type ea_type1;
  229. typedef typename Proxy<T2>::ea_type ea_type2;
  230. const Proxy<T1> A(X.A);
  231. const Proxy<T2> B(X.B);
  232. arma_debug_assert_same_size(A, B, "relational operator");
  233. ea_type1 PA = A.get_ea();
  234. ea_type2 PB = B.get_ea();
  235. const uword n_elem = B.get_n_elem();
  236. indices.set_size(n_elem, 1);
  237. uword* indices_mem = indices.memptr();
  238. uword n_nz = 0;
  239. for(uword i=0; i<n_elem; ++i)
  240. {
  241. const eT1 tmp1 = PA[i];
  242. const eT2 tmp2 = PB[i];
  243. bool not_zero;
  244. if(is_same_type<glue_type, glue_rel_lt >::yes) { not_zero = (tmp1 < tmp2); }
  245. else if(is_same_type<glue_type, glue_rel_gt >::yes) { not_zero = (tmp1 > tmp2); }
  246. else if(is_same_type<glue_type, glue_rel_lteq >::yes) { not_zero = (tmp1 <= tmp2); }
  247. else if(is_same_type<glue_type, glue_rel_gteq >::yes) { not_zero = (tmp1 >= tmp2); }
  248. else if(is_same_type<glue_type, glue_rel_eq >::yes) { not_zero = (tmp1 == tmp2); }
  249. else if(is_same_type<glue_type, glue_rel_noteq >::yes) { not_zero = (tmp1 != tmp2); }
  250. else if(is_same_type<glue_type, glue_rel_and >::yes) { not_zero = (tmp1 && tmp2); }
  251. else if(is_same_type<glue_type, glue_rel_or >::yes) { not_zero = (tmp1 || tmp2); }
  252. else { not_zero = false; }
  253. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  254. }
  255. return n_nz;
  256. }
  257. template<typename T1, typename T2, typename glue_type>
  258. inline
  259. uword
  260. op_find::helper
  261. (
  262. Mat<uword>& indices,
  263. const mtGlue<uword, T1, T2, glue_type>& X,
  264. const typename arma_glue_rel_only<glue_type>::result* junk1,
  265. const typename arma_cx_only<typename T1::elem_type>::result* junk2,
  266. const typename arma_cx_only<typename T2::elem_type>::result* junk3
  267. )
  268. {
  269. arma_extra_debug_sigprint();
  270. arma_ignore(junk1);
  271. arma_ignore(junk2);
  272. arma_ignore(junk3);
  273. typedef typename Proxy<T1>::ea_type ea_type1;
  274. typedef typename Proxy<T2>::ea_type ea_type2;
  275. const Proxy<T1> A(X.A);
  276. const Proxy<T2> B(X.B);
  277. arma_debug_assert_same_size(A, B, "relational operator");
  278. ea_type1 PA = A.get_ea();
  279. ea_type2 PB = B.get_ea();
  280. const uword n_elem = B.get_n_elem();
  281. indices.set_size(n_elem, 1);
  282. uword* indices_mem = indices.memptr();
  283. uword n_nz = 0;
  284. if(Proxy<T1>::use_at == false)
  285. {
  286. for(uword i=0; i<n_elem; ++i)
  287. {
  288. bool not_zero;
  289. if(is_same_type<glue_type, glue_rel_eq >::yes) { not_zero = (PA[i] == PB[i]); }
  290. else if(is_same_type<glue_type, glue_rel_noteq >::yes) { not_zero = (PA[i] != PB[i]); }
  291. else { not_zero = false; }
  292. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  293. }
  294. }
  295. else
  296. {
  297. const uword n_rows = A.get_n_rows();
  298. const uword n_cols = A.get_n_cols();
  299. uword i = 0;
  300. for(uword col=0; col<n_cols; ++col)
  301. for(uword row=0; row<n_rows; ++row)
  302. {
  303. bool not_zero;
  304. if(is_same_type<glue_type, glue_rel_eq >::yes) { not_zero = (A.at(row,col) == B.at(row,col)); }
  305. else if(is_same_type<glue_type, glue_rel_noteq >::yes) { not_zero = (A.at(row,col) != B.at(row,col)); }
  306. else { not_zero = false; }
  307. if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
  308. i++;
  309. }
  310. }
  311. return n_nz;
  312. }
  313. template<typename T1>
  314. inline
  315. void
  316. op_find::apply(Mat<uword>& out, const mtOp<uword, T1, op_find>& X)
  317. {
  318. arma_extra_debug_sigprint();
  319. const uword k = X.aux_uword_a;
  320. const uword type = X.aux_uword_b;
  321. Mat<uword> indices;
  322. const uword n_nz = op_find::helper(indices, X.m);
  323. if(n_nz > 0)
  324. {
  325. if(type == 0) // "first"
  326. {
  327. out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1);
  328. }
  329. else // "last"
  330. {
  331. out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1);
  332. }
  333. }
  334. else
  335. {
  336. out.set_size(0,1); // empty column vector
  337. }
  338. }
  339. //
  340. template<typename T1>
  341. inline
  342. void
  343. op_find_simple::apply(Mat<uword>& out, const mtOp<uword, T1, op_find_simple>& X)
  344. {
  345. arma_extra_debug_sigprint();
  346. Mat<uword> indices;
  347. const uword n_nz = op_find::helper(indices, X.m);
  348. out.steal_mem_col(indices, n_nz);
  349. }
  350. //
  351. template<typename T1>
  352. inline
  353. void
  354. op_find_finite::apply(Mat<uword>& out, const mtOp<uword, T1, op_find_finite>& X)
  355. {
  356. arma_extra_debug_sigprint();
  357. const Proxy<T1> P(X.m);
  358. const uword n_elem = P.get_n_elem();
  359. Mat<uword> indices(n_elem,1);
  360. uword* indices_mem = indices.memptr();
  361. uword count = 0;
  362. if(Proxy<T1>::use_at == false)
  363. {
  364. const typename Proxy<T1>::ea_type Pea = P.get_ea();
  365. for(uword i=0; i<n_elem; ++i)
  366. {
  367. if( arma_isfinite(Pea[i]) ) { indices_mem[count] = i; count++; }
  368. }
  369. }
  370. else
  371. {
  372. const uword n_rows = P.get_n_rows();
  373. const uword n_cols = P.get_n_cols();
  374. uword i = 0;
  375. for(uword col=0; col<n_cols; ++col)
  376. for(uword row=0; row<n_rows; ++row)
  377. {
  378. if( arma_isfinite(P.at(row,col)) ) { indices_mem[count] = i; count++; }
  379. i++;
  380. }
  381. }
  382. out.steal_mem_col(indices, count);
  383. }
  384. template<typename T1>
  385. inline
  386. void
  387. op_find_nonfinite::apply(Mat<uword>& out, const mtOp<uword, T1, op_find_nonfinite>& X)
  388. {
  389. arma_extra_debug_sigprint();
  390. const Proxy<T1> P(X.m);
  391. const uword n_elem = P.get_n_elem();
  392. Mat<uword> indices(n_elem,1);
  393. uword* indices_mem = indices.memptr();
  394. uword count = 0;
  395. if(Proxy<T1>::use_at == false)
  396. {
  397. const typename Proxy<T1>::ea_type Pea = P.get_ea();
  398. for(uword i=0; i<n_elem; ++i)
  399. {
  400. if( arma_isfinite(Pea[i]) == false ) { indices_mem[count] = i; count++; }
  401. }
  402. }
  403. else
  404. {
  405. const uword n_rows = P.get_n_rows();
  406. const uword n_cols = P.get_n_cols();
  407. uword i = 0;
  408. for(uword col=0; col<n_cols; ++col)
  409. for(uword row=0; row<n_rows; ++row)
  410. {
  411. if( arma_isfinite(P.at(row,col)) == false ) { indices_mem[count] = i; count++; }
  412. i++;
  413. }
  414. }
  415. out.steal_mem_col(indices, count);
  416. }
  417. //! @}