comparison armadillo-3.900.4/include/armadillo_bits/op_find_meat.hpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Copyright (C) 2010-2013 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010-2013 Conrad Sanderson
3 // Copyright (C) 2010 Dimitrios Bouzas
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9
10
11 //! \addtogroup op_find
12 //! @{
13
14
15
16 template<typename T1>
17 inline
18 uword
19 op_find::helper
20 (
21 Mat<uword>& indices,
22 const Base<typename T1::elem_type, T1>& X
23 )
24 {
25 arma_extra_debug_sigprint();
26
27 typedef typename T1::elem_type eT;
28
29 const Proxy<T1> A(X.get_ref());
30
31 const uword n_elem = A.get_n_elem();
32
33 indices.set_size(n_elem, 1);
34
35 uword* indices_mem = indices.memptr();
36 uword n_nz = 0;
37
38 if(Proxy<T1>::prefer_at_accessor == false)
39 {
40 typename Proxy<T1>::ea_type PA = A.get_ea();
41
42 for(uword i=0; i<n_elem; ++i)
43 {
44 if(PA[i] != eT(0)) { indices_mem[n_nz] = i; ++n_nz; }
45 }
46 }
47 else
48 {
49 const uword n_rows = A.get_n_rows();
50 const uword n_cols = A.get_n_cols();
51
52 uword i = 0;
53
54 for(uword col=0; col < n_cols; ++col)
55 for(uword row=0; row < n_rows; ++row)
56 {
57 if(A.at(row,col) != eT(0)) { indices_mem[n_nz] = i; ++n_nz; }
58
59 ++i;
60 }
61 }
62
63 return n_nz;
64 }
65
66
67
68 template<typename T1, typename op_type>
69 inline
70 uword
71 op_find::helper
72 (
73 Mat<uword>& indices,
74 const mtOp<uword, T1, op_type>& X,
75 const typename arma_op_rel_only<op_type>::result junk1,
76 const typename arma_not_cx<typename T1::elem_type>::result junk2
77 )
78 {
79 arma_extra_debug_sigprint();
80 arma_ignore(junk1);
81 arma_ignore(junk2);
82
83 typedef typename T1::elem_type eT;
84
85 const eT val = X.aux;
86
87 const Proxy<T1> A(X.m);
88
89 const uword n_elem = A.get_n_elem();
90
91 indices.set_size(n_elem, 1);
92
93 uword* indices_mem = indices.memptr();
94 uword n_nz = 0;
95
96 if(Proxy<T1>::prefer_at_accessor == false)
97 {
98 typename Proxy<T1>::ea_type PA = A.get_ea();
99
100 uword i,j;
101 for(i=0, j=1; j < n_elem; i+=2, j+=2)
102 {
103 const eT tpi = PA[i];
104 const eT tpj = PA[j];
105
106 bool not_zero_i;
107 bool not_zero_j;
108
109 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero_i = (val < tpi); }
110 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero_i = (tpi < val); }
111 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero_i = (val > tpi); }
112 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero_i = (tpi > val); }
113 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero_i = (val <= tpi); }
114 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero_i = (tpi <= val); }
115 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero_i = (val >= tpi); }
116 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero_i = (tpi >= val); }
117 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero_i = (tpi == val); }
118 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero_i = (tpi != val); }
119 else not_zero_i = false;
120
121 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero_j = (val < tpj); }
122 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero_j = (tpj < val); }
123 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero_j = (val > tpj); }
124 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero_j = (tpj > val); }
125 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero_j = (val <= tpj); }
126 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero_j = (tpj <= val); }
127 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero_j = (val >= tpj); }
128 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero_j = (tpj >= val); }
129 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero_j = (tpj == val); }
130 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero_j = (tpj != val); }
131 else not_zero_j = false;
132
133 if(not_zero_i == true) { indices_mem[n_nz] = i; ++n_nz; }
134 if(not_zero_j == true) { indices_mem[n_nz] = j; ++n_nz; }
135 }
136
137 if(i < n_elem)
138 {
139 bool not_zero;
140
141 const eT tmp = PA[i];
142
143 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero = (val < tmp); }
144 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero = (tmp < val); }
145 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero = (val > tmp); }
146 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero = (tmp > val); }
147 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero = (val <= tmp); }
148 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero = (tmp <= val); }
149 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero = (val >= tmp); }
150 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero = (tmp >= val); }
151 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); }
152 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero = (tmp != val); }
153 else not_zero = false;
154
155 if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
156 }
157 }
158 else
159 {
160 const uword n_rows = A.get_n_rows();
161 const uword n_cols = A.get_n_cols();
162
163 uword i = 0;
164
165 for(uword col=0; col < n_cols; ++col)
166 for(uword row=0; row < n_rows; ++row)
167 {
168 const eT tmp = A.at(row,col);
169
170 bool not_zero;
171
172 if(is_same_type<op_type, op_rel_lt_pre >::value == true) { not_zero = (val < tmp); }
173 else if(is_same_type<op_type, op_rel_lt_post >::value == true) { not_zero = (tmp < val); }
174 else if(is_same_type<op_type, op_rel_gt_pre >::value == true) { not_zero = (val > tmp); }
175 else if(is_same_type<op_type, op_rel_gt_post >::value == true) { not_zero = (tmp > val); }
176 else if(is_same_type<op_type, op_rel_lteq_pre >::value == true) { not_zero = (val <= tmp); }
177 else if(is_same_type<op_type, op_rel_lteq_post>::value == true) { not_zero = (tmp <= val); }
178 else if(is_same_type<op_type, op_rel_gteq_pre >::value == true) { not_zero = (val >= tmp); }
179 else if(is_same_type<op_type, op_rel_gteq_post>::value == true) { not_zero = (tmp >= val); }
180 else if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); }
181 else if(is_same_type<op_type, op_rel_noteq >::value == true) { not_zero = (tmp != val); }
182 else not_zero = false;
183
184 if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
185
186 ++i;
187 }
188 }
189
190 return n_nz;
191 }
192
193
194
195 template<typename T1, typename op_type>
196 inline
197 uword
198 op_find::helper
199 (
200 Mat<uword>& indices,
201 const mtOp<uword, T1, op_type>& X,
202 const typename arma_op_rel_only<op_type>::result junk1,
203 const typename arma_cx_only<typename T1::elem_type>::result junk2
204 )
205 {
206 arma_extra_debug_sigprint();
207 arma_ignore(junk1);
208 arma_ignore(junk2);
209
210 typedef typename T1::elem_type eT;
211 typedef typename Proxy<T1>::ea_type ea_type;
212
213 const eT val = X.aux;
214
215 const Proxy<T1> A(X.m);
216
217 ea_type PA = A.get_ea();
218 const uword n_elem = A.get_n_elem();
219
220 indices.set_size(n_elem, 1);
221
222 uword* indices_mem = indices.memptr();
223 uword n_nz = 0;
224
225 for(uword i=0; i<n_elem; ++i)
226 {
227 const eT tmp = PA[i];
228
229 bool not_zero;
230
231 if(is_same_type<op_type, op_rel_eq >::value == true) { not_zero = (tmp == val); }
232 else if(is_same_type<op_type, op_rel_noteq>::value == true) { not_zero = (tmp != val); }
233 else not_zero = false;
234
235 if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
236 }
237
238 return n_nz;
239 }
240
241
242
243 template<typename T1, typename T2, typename glue_type>
244 inline
245 uword
246 op_find::helper
247 (
248 Mat<uword>& indices,
249 const mtGlue<uword, T1, T2, glue_type>& X,
250 const typename arma_glue_rel_only<glue_type>::result junk1,
251 const typename arma_not_cx<typename T1::elem_type>::result junk2,
252 const typename arma_not_cx<typename T2::elem_type>::result junk3
253 )
254 {
255 arma_extra_debug_sigprint();
256 arma_ignore(junk1);
257 arma_ignore(junk2);
258 arma_ignore(junk3);
259
260 typedef typename T1::elem_type eT1;
261 typedef typename T2::elem_type eT2;
262
263 typedef typename Proxy<T1>::ea_type ea_type1;
264 typedef typename Proxy<T2>::ea_type ea_type2;
265
266 const Proxy<T1> A(X.A);
267 const Proxy<T2> B(X.B);
268
269 arma_debug_assert_same_size(A, B, "relational operator");
270
271 ea_type1 PA = A.get_ea();
272 ea_type2 PB = B.get_ea();
273
274 const uword n_elem = B.get_n_elem();
275
276 indices.set_size(n_elem, 1);
277
278 uword* indices_mem = indices.memptr();
279 uword n_nz = 0;
280
281 for(uword i=0; i<n_elem; ++i)
282 {
283 const eT1 tmp1 = PA[i];
284 const eT2 tmp2 = PB[i];
285
286 bool not_zero;
287
288 if(is_same_type<glue_type, glue_rel_lt >::value == true) { not_zero = (tmp1 < tmp2); }
289 else if(is_same_type<glue_type, glue_rel_gt >::value == true) { not_zero = (tmp1 > tmp2); }
290 else if(is_same_type<glue_type, glue_rel_lteq >::value == true) { not_zero = (tmp1 <= tmp2); }
291 else if(is_same_type<glue_type, glue_rel_gteq >::value == true) { not_zero = (tmp1 >= tmp2); }
292 else if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (tmp1 == tmp2); }
293 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (tmp1 != tmp2); }
294 else not_zero = false;
295
296 if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
297 }
298
299 return n_nz;
300 }
301
302
303
304 template<typename T1, typename T2, typename glue_type>
305 inline
306 uword
307 op_find::helper
308 (
309 Mat<uword>& indices,
310 const mtGlue<uword, T1, T2, glue_type>& X,
311 const typename arma_glue_rel_only<glue_type>::result junk1,
312 const typename arma_cx_only<typename T1::elem_type>::result junk2,
313 const typename arma_cx_only<typename T2::elem_type>::result junk3
314 )
315 {
316 arma_extra_debug_sigprint();
317 arma_ignore(junk1);
318 arma_ignore(junk2);
319 arma_ignore(junk3);
320
321 typedef typename Proxy<T1>::ea_type ea_type1;
322 typedef typename Proxy<T2>::ea_type ea_type2;
323
324 const Proxy<T1> A(X.A);
325 const Proxy<T2> B(X.B);
326
327 arma_debug_assert_same_size(A, B, "relational operator");
328
329 ea_type1 PA = A.get_ea();
330 ea_type2 PB = B.get_ea();
331
332 const uword n_elem = B.get_n_elem();
333
334 indices.set_size(n_elem, 1);
335
336 uword* indices_mem = indices.memptr();
337 uword n_nz = 0;
338
339 for(uword i=0; i<n_elem; ++i)
340 {
341 bool not_zero;
342
343 if(is_same_type<glue_type, glue_rel_eq >::value == true) { not_zero = (PA[i] == PB[i]); }
344 else if(is_same_type<glue_type, glue_rel_noteq >::value == true) { not_zero = (PA[i] != PB[i]); }
345 else not_zero = false;
346
347 if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; }
348 }
349
350 return n_nz;
351 }
352
353
354
355 template<typename T1>
356 inline
357 void
358 op_find::apply(Mat<uword>& out, const mtOp<uword, T1, op_find>& X)
359 {
360 arma_extra_debug_sigprint();
361
362 const uword k = X.aux_uword_a;
363 const uword type = X.aux_uword_b;
364
365 Mat<uword> indices;
366 const uword n_nz = op_find::helper(indices, X.m);
367
368 if(n_nz > 0)
369 {
370 if(type == 0) // "first"
371 {
372 out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1);
373 }
374 else // "last"
375 {
376 out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1);
377 }
378 }
379 else
380 {
381 out.set_size(0,1); // empty column vector
382 }
383 }
384
385
386
387 //! @}