$search
00001 // Copyright (C) 2010 NICTA (www.nicta.com.au) 00002 // Copyright (C) 2010 Conrad Sanderson 00003 // Copyright (C) 2010 Dimitrios Bouzas 00004 // 00005 // This file is part of the Armadillo C++ library. 00006 // It is provided without any warranty of fitness 00007 // for any purpose. You can redistribute this file 00008 // and/or modify it under the terms of the GNU 00009 // Lesser General Public License (LGPL) as published 00010 // by the Free Software Foundation, either version 3 00011 // of the License or (at your option) any later version. 00012 // (see http://www.opensource.org/licenses for more info) 00013 00014 00015 00018 00019 00020 00021 template<typename T1> 00022 inline 00023 uword 00024 op_find::helper 00025 ( 00026 Mat<uword>& indices, 00027 const Base<typename T1::elem_type, T1>& X 00028 ) 00029 { 00030 arma_extra_debug_sigprint(); 00031 00032 typedef typename T1::elem_type eT; 00033 typedef typename Proxy<T1>::ea_type ea_type; 00034 00035 const Proxy<T1> A(X.get_ref()); 00036 00037 ea_type PA = A.get_ea(); 00038 const uword n_elem = A.get_n_elem(); 00039 00040 indices.set_size(n_elem, 1); 00041 00042 uword* indices_mem = indices.memptr(); 00043 uword n_nz = 0; 00044 00045 for(uword i=0; i<n_elem; ++i) 00046 { 00047 if(PA[i] != eT(0)) 00048 { 00049 indices_mem[n_nz] = i; 00050 ++n_nz; 00051 } 00052 } 00053 00054 return n_nz; 00055 } 00056 00057 00058 00059 template<typename T1, typename op_type> 00060 inline 00061 uword 00062 op_find::helper 00063 ( 00064 Mat<uword>& indices, 00065 const mtOp<uword, T1, op_type>& X, 00066 const typename arma_op_rel_only<op_type>::result junk1, 00067 const typename arma_not_cx<typename T1::elem_type>::result junk2 00068 ) 00069 { 00070 arma_extra_debug_sigprint(); 00071 arma_ignore(junk1); 00072 arma_ignore(junk2); 00073 00074 typedef typename T1::elem_type eT; 00075 typedef typename Proxy<T1>::ea_type ea_type; 00076 00077 const eT val = X.aux; 00078 00079 const Proxy<T1> A(X.m); 00080 00081 ea_type PA = A.get_ea(); 00082 const uword n_elem = A.get_n_elem(); 00083 00084 indices.set_size(n_elem, 1); 00085 00086 uword* indices_mem = indices.memptr(); 00087 uword n_nz = 0; 00088 00089 for(uword i=0; i<n_elem; ++i) 00090 { 00091 const eT tmp = PA[i]; 00092 00093 bool not_zero; 00094 00095 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero = (val < tmp); } 00096 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero = (tmp < val); } 00097 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero = (val > tmp); } 00098 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero = (tmp > val); } 00099 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero = (val <= tmp); } 00100 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero = (tmp <= val); } 00101 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero = (val >= tmp); } 00102 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero = (tmp >= val); } 00103 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); } 00104 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero = (tmp != val); } 00105 else not_zero = false; 00106 00107 if(not_zero == true) 00108 { 00109 indices_mem[n_nz] = i; 00110 ++n_nz; 00111 } 00112 } 00113 00114 return n_nz; 00115 } 00116 00117 00118 00119 template<typename T1, typename op_type> 00120 inline 00121 uword 00122 op_find::helper 00123 ( 00124 Mat<uword>& indices, 00125 const mtOp<uword, T1, op_type>& X, 00126 const typename arma_op_rel_only<op_type>::result junk1, 00127 const typename arma_cx_only<typename T1::elem_type>::result junk2 00128 ) 00129 { 00130 arma_extra_debug_sigprint(); 00131 arma_ignore(junk1); 00132 arma_ignore(junk2); 00133 00134 typedef typename T1::elem_type eT; 00135 typedef typename Proxy<T1>::ea_type ea_type; 00136 00137 const eT val = X.aux; 00138 00139 const Proxy<T1> A(X.m); 00140 00141 ea_type PA = A.get_ea(); 00142 const uword n_elem = A.get_n_elem(); 00143 00144 indices.set_size(n_elem, 1); 00145 00146 uword* indices_mem = indices.memptr(); 00147 uword n_nz = 0; 00148 00149 for(uword i=0; i<n_elem; ++i) 00150 { 00151 const eT tmp = PA[i]; 00152 00153 bool not_zero; 00154 00155 if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); } 00156 else if(is_same_type<op_type, op_rel_noteq>::value == true) { not_zero = (tmp != val); } 00157 else not_zero = false; 00158 00159 if(not_zero == true) 00160 { 00161 indices_mem[n_nz] = i; 00162 ++n_nz; 00163 } 00164 } 00165 00166 return n_nz; 00167 } 00168 00169 00170 00171 template<typename T1, typename T2, typename glue_type> 00172 inline 00173 uword 00174 op_find::helper 00175 ( 00176 Mat<uword>& indices, 00177 const mtGlue<uword, T1, T2, glue_type>& X, 00178 const typename arma_glue_rel_only<glue_type>::result junk1, 00179 const typename arma_not_cx<typename T1::elem_type>::result junk2, 00180 const typename arma_not_cx<typename T2::elem_type>::result junk3 00181 ) 00182 { 00183 arma_extra_debug_sigprint(); 00184 arma_ignore(junk1); 00185 arma_ignore(junk2); 00186 arma_ignore(junk3); 00187 00188 typedef typename T1::elem_type eT1; 00189 typedef typename T2::elem_type eT2; 00190 00191 typedef typename Proxy<T1>::ea_type ea_type1; 00192 typedef typename Proxy<T2>::ea_type ea_type2; 00193 00194 const Proxy<T1> A(X.A); 00195 const Proxy<T2> B(X.B); 00196 00197 arma_debug_assert_same_size(A, B, "relational operator"); 00198 00199 ea_type1 PA = A.get_ea(); 00200 ea_type2 PB = B.get_ea(); 00201 const uword n_elem = B.get_n_elem(); 00202 00203 indices.set_size(n_elem, 1); 00204 00205 uword* indices_mem = indices.memptr(); 00206 uword n_nz = 0; 00207 00208 for(uword i=0; i<n_elem; ++i) 00209 { 00210 const eT1 tmp1 = PA[i]; 00211 const eT2 tmp2 = PB[i]; 00212 00213 bool not_zero; 00214 00215 if(is_same_type<glue_type, glue_rel_lt >::value == true) { not_zero = (tmp1 < tmp2); } 00216 else if(is_same_type<glue_type, glue_rel_gt >::value == true) { not_zero = (tmp1 > tmp2); } 00217 else if(is_same_type<glue_type, glue_rel_lteq >::value == true) { not_zero = (tmp1 <= tmp2); } 00218 else if(is_same_type<glue_type, glue_rel_gteq >::value == true) { not_zero = (tmp1 >= tmp2); } 00219 else if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (tmp1 == tmp2); } 00220 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (tmp1 != tmp2); } 00221 else not_zero = false; 00222 00223 if(not_zero == true) 00224 { 00225 indices_mem[n_nz] = i; 00226 ++n_nz; 00227 } 00228 } 00229 00230 return n_nz; 00231 } 00232 00233 00234 00235 template<typename T1, typename T2, typename glue_type> 00236 inline 00237 uword 00238 op_find::helper 00239 ( 00240 Mat<uword>& indices, 00241 const mtGlue<uword, T1, T2, glue_type>& X, 00242 const typename arma_glue_rel_only<glue_type>::result junk1, 00243 const typename arma_cx_only<typename T1::elem_type>::result junk2, 00244 const typename arma_cx_only<typename T2::elem_type>::result junk3 00245 ) 00246 { 00247 arma_extra_debug_sigprint(); 00248 arma_ignore(junk1); 00249 arma_ignore(junk2); 00250 arma_ignore(junk3); 00251 00252 typedef typename Proxy<T1>::ea_type ea_type1; 00253 typedef typename Proxy<T2>::ea_type ea_type2; 00254 00255 const Proxy<T1> A(X.A); 00256 const Proxy<T2> B(X.B); 00257 00258 arma_debug_assert_same_size(A, B, "relational operator"); 00259 00260 ea_type1 PA = A.get_ea(); 00261 ea_type2 PB = B.get_ea(); 00262 const uword n_elem = B.get_n_elem(); 00263 00264 indices.set_size(n_elem, 1); 00265 00266 uword* indices_mem = indices.memptr(); 00267 uword n_nz = 0; 00268 00269 for(uword i=0; i<n_elem; ++i) 00270 { 00271 bool not_zero; 00272 00273 if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (PA[i] == PB[i]); } 00274 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (PA[i] != PB[i]); } 00275 else not_zero = false; 00276 00277 if(not_zero == true) 00278 { 00279 indices_mem[n_nz] = i; 00280 ++n_nz; 00281 } 00282 } 00283 00284 return n_nz; 00285 } 00286 00287 00288 00289 template<typename T1> 00290 inline 00291 void 00292 op_find::apply(Mat<uword>& out, const mtOp<uword, T1, op_find>& X) 00293 { 00294 arma_extra_debug_sigprint(); 00295 00296 const uword k = X.aux_uword_a; 00297 const uword type = X.aux_uword_b; 00298 00299 Mat<uword> indices; 00300 const uword n_nz = op_find::helper(indices, X.m); 00301 00302 if(n_nz > 0) 00303 { 00304 if(type == 0) // "first" 00305 { 00306 out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1); 00307 } 00308 else // "last" 00309 { 00310 out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1); 00311 } 00312 } 00313 else 00314 { 00315 out.reset(); 00316 } 00317 } 00318 00319 00320