op_all_meat.hpp 11 KB

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