op_any_meat.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_any
  16. //! @{
  17. template<typename T1>
  18. inline
  19. bool
  20. op_any::any_vec_helper(const Base<typename T1::elem_type, T1>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const Proxy<T1> P(X.get_ref());
  25. const uword n_elem = P.get_n_elem();
  26. if(Proxy<T1>::use_at == false)
  27. {
  28. typename Proxy<T1>::ea_type Pea = P.get_ea();
  29. for(uword i=0; i<n_elem; ++i)
  30. {
  31. if(Pea[i] != eT(0)) { return true; }
  32. }
  33. }
  34. else
  35. {
  36. const uword n_rows = P.get_n_rows();
  37. const uword n_cols = P.get_n_cols();
  38. for(uword col=0; col < n_cols; ++col)
  39. for(uword row=0; row < n_rows; ++row)
  40. {
  41. if(P.at(row,col) != eT(0)) { return true; }
  42. }
  43. }
  44. return false;
  45. }
  46. template<typename eT>
  47. inline
  48. bool
  49. op_any::any_vec_helper(const subview<eT>& X)
  50. {
  51. arma_extra_debug_sigprint();
  52. const uword X_n_rows = X.n_rows;
  53. const uword X_n_cols = X.n_cols;
  54. if(X_n_rows == 1)
  55. {
  56. for(uword col=0; col < X_n_cols; ++col)
  57. {
  58. if(X.at(0,col) != eT(0)) { return true; }
  59. }
  60. }
  61. else
  62. {
  63. for(uword col=0; col < X_n_cols; ++col)
  64. {
  65. const eT* X_colmem = X.colptr(col);
  66. for(uword row=0; row < X_n_rows; ++row)
  67. {
  68. if(X_colmem[row] != eT(0)) { return true; }
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. template<typename T1>
  75. inline
  76. bool
  77. op_any::any_vec_helper(const Op<T1, op_vectorise_col>& X)
  78. {
  79. arma_extra_debug_sigprint();
  80. return op_any::any_vec_helper(X.m);
  81. }
  82. template<typename T1, typename op_type>
  83. inline
  84. bool
  85. op_any::any_vec_helper
  86. (
  87. const mtOp<uword, T1, op_type>& X,
  88. const typename arma_op_rel_only<op_type>::result* junk1,
  89. const typename arma_not_cx<typename T1::elem_type>::result* junk2
  90. )
  91. {
  92. arma_extra_debug_sigprint();
  93. arma_ignore(junk1);
  94. arma_ignore(junk2);
  95. typedef typename T1::elem_type eT;
  96. const eT val = X.aux;
  97. const Proxy<T1> P(X.m);
  98. if(Proxy<T1>::use_at == false)
  99. {
  100. typename Proxy<T1>::ea_type Pea = P.get_ea();
  101. const uword n_elem = P.get_n_elem();
  102. for(uword i=0; i < n_elem; ++i)
  103. {
  104. const eT tmp = Pea[i];
  105. if(is_same_type<op_type, op_rel_lt_pre >::yes) { if(val < tmp) { return true; } }
  106. else if(is_same_type<op_type, op_rel_lt_post >::yes) { if(tmp < val) { return true; } }
  107. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { if(val > tmp) { return true; } }
  108. else if(is_same_type<op_type, op_rel_gt_post >::yes) { if(tmp > val) { return true; } }
  109. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { if(val <= tmp) { return true; } }
  110. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { if(tmp <= val) { return true; } }
  111. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { if(val >= tmp) { return true; } }
  112. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { if(tmp >= val) { return true; } }
  113. else if(is_same_type<op_type, op_rel_eq >::yes) { if(tmp == val) { return true; } }
  114. else if(is_same_type<op_type, op_rel_noteq >::yes) { if(tmp != val) { return true; } }
  115. }
  116. }
  117. else
  118. {
  119. const uword n_rows = P.get_n_rows();
  120. const uword n_cols = P.get_n_cols();
  121. for(uword col=0; col < n_cols; ++col)
  122. for(uword row=0; row < n_rows; ++row)
  123. {
  124. const eT tmp = P.at(row,col);
  125. if(is_same_type<op_type, op_rel_lt_pre >::yes) { if(val < tmp) { return true; } }
  126. else if(is_same_type<op_type, op_rel_lt_post >::yes) { if(tmp < val) { return true; } }
  127. else if(is_same_type<op_type, op_rel_gt_pre >::yes) { if(val > tmp) { return true; } }
  128. else if(is_same_type<op_type, op_rel_gt_post >::yes) { if(tmp > val) { return true; } }
  129. else if(is_same_type<op_type, op_rel_lteq_pre >::yes) { if(val <= tmp) { return true; } }
  130. else if(is_same_type<op_type, op_rel_lteq_post>::yes) { if(tmp <= val) { return true; } }
  131. else if(is_same_type<op_type, op_rel_gteq_pre >::yes) { if(val >= tmp) { return true; } }
  132. else if(is_same_type<op_type, op_rel_gteq_post>::yes) { if(tmp >= val) { return true; } }
  133. else if(is_same_type<op_type, op_rel_eq >::yes) { if(tmp == val) { return true; } }
  134. else if(is_same_type<op_type, op_rel_noteq >::yes) { if(tmp != val) { return true; } }
  135. }
  136. }
  137. return false;
  138. }
  139. template<typename T1, typename T2, typename glue_type>
  140. inline
  141. bool
  142. op_any::any_vec_helper
  143. (
  144. const mtGlue<uword, T1, T2, glue_type>& X,
  145. const typename arma_glue_rel_only<glue_type>::result* junk1,
  146. const typename arma_not_cx<typename T1::elem_type>::result* junk2,
  147. const typename arma_not_cx<typename T2::elem_type>::result* junk3
  148. )
  149. {
  150. arma_extra_debug_sigprint();
  151. arma_ignore(junk1);
  152. arma_ignore(junk2);
  153. arma_ignore(junk3);
  154. typedef typename T1::elem_type eT1;
  155. typedef typename T2::elem_type eT2;
  156. typedef typename Proxy<T1>::ea_type ea_type1;
  157. typedef typename Proxy<T2>::ea_type ea_type2;
  158. const Proxy<T1> A(X.A);
  159. const Proxy<T2> B(X.B);
  160. arma_debug_assert_same_size(A, B, "relational operator");
  161. const bool use_at = (Proxy<T1>::use_at || Proxy<T2>::use_at);
  162. if(use_at == false)
  163. {
  164. ea_type1 PA = A.get_ea();
  165. ea_type2 PB = B.get_ea();
  166. const uword n_elem = A.get_n_elem();
  167. for(uword i=0; i<n_elem; ++i)
  168. {
  169. const eT1 tmp1 = PA[i];
  170. const eT2 tmp2 = PB[i];
  171. if(is_same_type<glue_type, glue_rel_lt >::yes) { if(tmp1 < tmp2) { return true; } }
  172. else if(is_same_type<glue_type, glue_rel_gt >::yes) { if(tmp1 > tmp2) { return true; } }
  173. else if(is_same_type<glue_type, glue_rel_lteq >::yes) { if(tmp1 <= tmp2) { return true; } }
  174. else if(is_same_type<glue_type, glue_rel_gteq >::yes) { if(tmp1 >= tmp2) { return true; } }
  175. else if(is_same_type<glue_type, glue_rel_eq >::yes) { if(tmp1 == tmp2) { return true; } }
  176. else if(is_same_type<glue_type, glue_rel_noteq >::yes) { if(tmp1 != tmp2) { return true; } }
  177. else if(is_same_type<glue_type, glue_rel_and >::yes) { if(tmp1 && tmp2) { return true; } }
  178. else if(is_same_type<glue_type, glue_rel_or >::yes) { if(tmp1 || tmp2) { return true; } }
  179. }
  180. }
  181. else
  182. {
  183. const uword n_rows = A.get_n_rows();
  184. const uword n_cols = A.get_n_cols();
  185. for(uword col=0; col < n_cols; ++col)
  186. for(uword row=0; row < n_rows; ++row)
  187. {
  188. const eT1 tmp1 = A.at(row,col);
  189. const eT2 tmp2 = B.at(row,col);
  190. if(is_same_type<glue_type, glue_rel_lt >::yes) { if(tmp1 < tmp2) { return true; } }
  191. else if(is_same_type<glue_type, glue_rel_gt >::yes) { if(tmp1 > tmp2) { return true; } }
  192. else if(is_same_type<glue_type, glue_rel_lteq >::yes) { if(tmp1 <= tmp2) { return true; } }
  193. else if(is_same_type<glue_type, glue_rel_gteq >::yes) { if(tmp1 >= tmp2) { return true; } }
  194. else if(is_same_type<glue_type, glue_rel_eq >::yes) { if(tmp1 == tmp2) { return true; } }
  195. else if(is_same_type<glue_type, glue_rel_noteq >::yes) { if(tmp1 != tmp2) { return true; } }
  196. else if(is_same_type<glue_type, glue_rel_and >::yes) { if(tmp1 && tmp2) { return true; } }
  197. else if(is_same_type<glue_type, glue_rel_or >::yes) { if(tmp1 || tmp2) { return true; } }
  198. }
  199. }
  200. return false;
  201. }
  202. template<typename T1>
  203. inline
  204. bool
  205. op_any::any_vec(T1& X)
  206. {
  207. arma_extra_debug_sigprint();
  208. return op_any::any_vec_helper(X);
  209. }
  210. template<typename T1>
  211. inline
  212. void
  213. op_any::apply_helper(Mat<uword>& out, const Proxy<T1>& P, const uword dim)
  214. {
  215. arma_extra_debug_sigprint();
  216. const uword n_rows = P.get_n_rows();
  217. const uword n_cols = P.get_n_cols();
  218. typedef typename Proxy<T1>::elem_type eT;
  219. if(dim == 0) // traverse rows (ie. process each column)
  220. {
  221. out.zeros(1, n_cols);
  222. uword* out_mem = out.memptr();
  223. if(is_Mat<typename Proxy<T1>::stored_type>::value)
  224. {
  225. const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
  226. for(uword col=0; col < n_cols; ++col)
  227. {
  228. const eT* colmem = U.M.colptr(col);
  229. for(uword row=0; row < n_rows; ++row)
  230. {
  231. if(colmem[row] != eT(0)) { out_mem[col] = uword(1); break; }
  232. }
  233. }
  234. }
  235. else
  236. {
  237. for(uword col=0; col < n_cols; ++col)
  238. {
  239. for(uword row=0; row < n_rows; ++row)
  240. {
  241. if(P.at(row,col) != eT(0)) { out_mem[col] = uword(1); break; }
  242. }
  243. }
  244. }
  245. }
  246. else
  247. {
  248. out.zeros(n_rows, 1);
  249. uword* out_mem = out.memptr();
  250. if(is_Mat<typename Proxy<T1>::stored_type>::value)
  251. {
  252. const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
  253. for(uword col=0; col < n_cols; ++col)
  254. {
  255. const eT* colmem = U.M.colptr(col);
  256. for(uword row=0; row < n_rows; ++row)
  257. {
  258. if(colmem[row] != eT(0)) { out_mem[row] = uword(1); }
  259. }
  260. }
  261. }
  262. else
  263. {
  264. for(uword col=0; col < n_cols; ++col)
  265. {
  266. for(uword row=0; row < n_rows; ++row)
  267. {
  268. if(P.at(row,col) != eT(0)) { out_mem[row] = uword(1); }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. template<typename T1>
  275. inline
  276. void
  277. op_any::apply(Mat<uword>& out, const mtOp<uword, T1, op_any>& X)
  278. {
  279. arma_extra_debug_sigprint();
  280. const uword dim = X.aux_uword_a;
  281. const Proxy<T1> P(X.m);
  282. if(P.is_alias(out) == false)
  283. {
  284. op_any::apply_helper(out, P, dim);
  285. }
  286. else
  287. {
  288. Mat<uword> out2;
  289. op_any::apply_helper(out2, P, dim);
  290. out.steal_mem(out2);
  291. }
  292. }
  293. //! @}