comparison armadillo-2.4.4/include/armadillo_bits/op_find_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2010 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010 Conrad Sanderson
3 // Copyright (C) 2010 Dimitrios Bouzas
4 //
5 // This file is part of the Armadillo C++ library.
6 // It is provided without any warranty of fitness
7 // for any purpose. You can redistribute this file
8 // and/or modify it under the terms of the GNU
9 // Lesser General Public License (LGPL) as published
10 // by the Free Software Foundation, either version 3
11 // of the License or (at your option) any later version.
12 // (see http://www.opensource.org/licenses for more info)
13
14
15
16 //! \addtogroup op_find
17 //! @{
18
19
20
21 template<typename T1>
22 inline
23 uword
24 op_find::helper
25 (
26 Mat<uword>& indices,
27 const Base<typename T1::elem_type, T1>& X
28 )
29 {
30 arma_extra_debug_sigprint();
31
32 typedef typename T1::elem_type eT;
33 typedef typename Proxy<T1>::ea_type ea_type;
34
35 const Proxy<T1> A(X.get_ref());
36
37 ea_type PA = A.get_ea();
38 const uword n_elem = A.get_n_elem();
39
40 indices.set_size(n_elem, 1);
41
42 uword* indices_mem = indices.memptr();
43 uword n_nz = 0;
44
45 for(uword i=0; i<n_elem; ++i)
46 {
47 if(PA[i] != eT(0))
48 {
49 indices_mem[n_nz] = i;
50 ++n_nz;
51 }
52 }
53
54 return n_nz;
55 }
56
57
58
59 template<typename T1, typename op_type>
60 inline
61 uword
62 op_find::helper
63 (
64 Mat<uword>& indices,
65 const mtOp<uword, T1, op_type>& X,
66 const typename arma_op_rel_only<op_type>::result junk1,
67 const typename arma_not_cx<typename T1::elem_type>::result junk2
68 )
69 {
70 arma_extra_debug_sigprint();
71 arma_ignore(junk1);
72 arma_ignore(junk2);
73
74 typedef typename T1::elem_type eT;
75 typedef typename Proxy<T1>::ea_type ea_type;
76
77 const eT val = X.aux;
78
79 const Proxy<T1> A(X.m);
80
81 ea_type PA = A.get_ea();
82 const uword n_elem = A.get_n_elem();
83
84 indices.set_size(n_elem, 1);
85
86 uword* indices_mem = indices.memptr();
87 uword n_nz = 0;
88
89 for(uword i=0; i<n_elem; ++i)
90 {
91 const eT tmp = PA[i];
92
93 bool not_zero;
94
95 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero = (val < tmp); }
96 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero = (tmp < val); }
97 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero = (val > tmp); }
98 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero = (tmp > val); }
99 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero = (val <= tmp); }
100 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero = (tmp <= val); }
101 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero = (val >= tmp); }
102 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero = (tmp >= val); }
103 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); }
104 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero = (tmp != val); }
105 else not_zero = false;
106
107 if(not_zero == true)
108 {
109 indices_mem[n_nz] = i;
110 ++n_nz;
111 }
112 }
113
114 return n_nz;
115 }
116
117
118
119 template<typename T1, typename op_type>
120 inline
121 uword
122 op_find::helper
123 (
124 Mat<uword>& indices,
125 const mtOp<uword, T1, op_type>& X,
126 const typename arma_op_rel_only<op_type>::result junk1,
127 const typename arma_cx_only<typename T1::elem_type>::result junk2
128 )
129 {
130 arma_extra_debug_sigprint();
131 arma_ignore(junk1);
132 arma_ignore(junk2);
133
134 typedef typename T1::elem_type eT;
135 typedef typename Proxy<T1>::ea_type ea_type;
136
137 const eT val = X.aux;
138
139 const Proxy<T1> A(X.m);
140
141 ea_type PA = A.get_ea();
142 const uword n_elem = A.get_n_elem();
143
144 indices.set_size(n_elem, 1);
145
146 uword* indices_mem = indices.memptr();
147 uword n_nz = 0;
148
149 for(uword i=0; i<n_elem; ++i)
150 {
151 const eT tmp = PA[i];
152
153 bool not_zero;
154
155 if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); }
156 else if(is_same_type<op_type, op_rel_noteq>::value == true) { not_zero = (tmp != val); }
157 else not_zero = false;
158
159 if(not_zero == true)
160 {
161 indices_mem[n_nz] = i;
162 ++n_nz;
163 }
164 }
165
166 return n_nz;
167 }
168
169
170
171 template<typename T1, typename T2, typename glue_type>
172 inline
173 uword
174 op_find::helper
175 (
176 Mat<uword>& indices,
177 const mtGlue<uword, T1, T2, glue_type>& X,
178 const typename arma_glue_rel_only<glue_type>::result junk1,
179 const typename arma_not_cx<typename T1::elem_type>::result junk2,
180 const typename arma_not_cx<typename T2::elem_type>::result junk3
181 )
182 {
183 arma_extra_debug_sigprint();
184 arma_ignore(junk1);
185 arma_ignore(junk2);
186 arma_ignore(junk3);
187
188 typedef typename T1::elem_type eT1;
189 typedef typename T2::elem_type eT2;
190
191 typedef typename Proxy<T1>::ea_type ea_type1;
192 typedef typename Proxy<T2>::ea_type ea_type2;
193
194 const Proxy<T1> A(X.A);
195 const Proxy<T2> B(X.B);
196
197 arma_debug_assert_same_size(A, B, "relational operator");
198
199 ea_type1 PA = A.get_ea();
200 ea_type2 PB = B.get_ea();
201 const uword n_elem = B.get_n_elem();
202
203 indices.set_size(n_elem, 1);
204
205 uword* indices_mem = indices.memptr();
206 uword n_nz = 0;
207
208 for(uword i=0; i<n_elem; ++i)
209 {
210 const eT1 tmp1 = PA[i];
211 const eT2 tmp2 = PB[i];
212
213 bool not_zero;
214
215 if(is_same_type<glue_type, glue_rel_lt >::value == true) { not_zero = (tmp1 < tmp2); }
216 else if(is_same_type<glue_type, glue_rel_gt >::value == true) { not_zero = (tmp1 > tmp2); }
217 else if(is_same_type<glue_type, glue_rel_lteq >::value == true) { not_zero = (tmp1 <= tmp2); }
218 else if(is_same_type<glue_type, glue_rel_gteq >::value == true) { not_zero = (tmp1 >= tmp2); }
219 else if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (tmp1 == tmp2); }
220 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (tmp1 != tmp2); }
221 else not_zero = false;
222
223 if(not_zero == true)
224 {
225 indices_mem[n_nz] = i;
226 ++n_nz;
227 }
228 }
229
230 return n_nz;
231 }
232
233
234
235 template<typename T1, typename T2, typename glue_type>
236 inline
237 uword
238 op_find::helper
239 (
240 Mat<uword>& indices,
241 const mtGlue<uword, T1, T2, glue_type>& X,
242 const typename arma_glue_rel_only<glue_type>::result junk1,
243 const typename arma_cx_only<typename T1::elem_type>::result junk2,
244 const typename arma_cx_only<typename T2::elem_type>::result junk3
245 )
246 {
247 arma_extra_debug_sigprint();
248 arma_ignore(junk1);
249 arma_ignore(junk2);
250 arma_ignore(junk3);
251
252 typedef typename Proxy<T1>::ea_type ea_type1;
253 typedef typename Proxy<T2>::ea_type ea_type2;
254
255 const Proxy<T1> A(X.A);
256 const Proxy<T2> B(X.B);
257
258 arma_debug_assert_same_size(A, B, "relational operator");
259
260 ea_type1 PA = A.get_ea();
261 ea_type2 PB = B.get_ea();
262 const uword n_elem = B.get_n_elem();
263
264 indices.set_size(n_elem, 1);
265
266 uword* indices_mem = indices.memptr();
267 uword n_nz = 0;
268
269 for(uword i=0; i<n_elem; ++i)
270 {
271 bool not_zero;
272
273 if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (PA[i] == PB[i]); }
274 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (PA[i] != PB[i]); }
275 else not_zero = false;
276
277 if(not_zero == true)
278 {
279 indices_mem[n_nz] = i;
280 ++n_nz;
281 }
282 }
283
284 return n_nz;
285 }
286
287
288
289 template<typename T1>
290 inline
291 void
292 op_find::apply(Mat<uword>& out, const mtOp<uword, T1, op_find>& X)
293 {
294 arma_extra_debug_sigprint();
295
296 const uword k = X.aux_uword_a;
297 const uword type = X.aux_uword_b;
298
299 Mat<uword> indices;
300 const uword n_nz = op_find::helper(indices, X.m);
301
302 if(n_nz > 0)
303 {
304 if(type == 0) // "first"
305 {
306 out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1);
307 }
308 else // "last"
309 {
310 out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1);
311 }
312 }
313 else
314 {
315 out.reset();
316 }
317 }
318
319
320
321 //! @}