comparison armadillo-2.4.4/include/armadillo_bits/op_max_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) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup op_max
15 //! @{
16
17
18
19 template<typename eT>
20 arma_pure
21 inline
22 eT
23 op_max::direct_max(const eT* const X, const uword n_elem)
24 {
25 arma_extra_debug_sigprint();
26
27 eT max_val = priv::most_neg<eT>();
28
29 uword i,j;
30
31 for(i=0, j=1; j<n_elem; i+=2, j+=2)
32 {
33 const eT X_i = X[i];
34 const eT X_j = X[j];
35
36 if(X_i > max_val)
37 {
38 max_val = X_i;
39 }
40
41 if(X_j > max_val)
42 {
43 max_val = X_j;
44 }
45 }
46
47
48 if(i < n_elem)
49 {
50 const eT X_i = X[i];
51
52 if(X_i > max_val)
53 {
54 max_val = X_i;
55 }
56 }
57
58 return max_val;
59 }
60
61
62
63 template<typename eT>
64 inline
65 eT
66 op_max::direct_max(const eT* const X, const uword n_elem, uword& index_of_max_val)
67 {
68 arma_extra_debug_sigprint();
69
70 eT max_val = priv::most_neg<eT>();
71
72 uword best_index = 0;
73
74 uword i,j;
75
76 for(i=0, j=1; j<n_elem; i+=2, j+=2)
77 {
78 const eT X_i = X[i];
79 const eT X_j = X[j];
80
81 if(X_i > max_val)
82 {
83 max_val = X_i;
84 best_index = i;
85 }
86
87 if(X_j > max_val)
88 {
89 max_val = X_j;
90 best_index = j;
91 }
92 }
93
94
95 if(i < n_elem)
96 {
97 const eT X_i = X[i];
98
99 if(X_i > max_val)
100 {
101 max_val = X_i;
102 best_index = i;
103 }
104 }
105
106 index_of_max_val = best_index;
107
108 return max_val;
109 }
110
111
112
113 template<typename eT>
114 inline
115 eT
116 op_max::direct_max(const Mat<eT>& X, const uword row)
117 {
118 arma_extra_debug_sigprint();
119
120 const uword X_n_cols = X.n_cols;
121
122 eT max_val = priv::most_neg<eT>();
123
124 for(uword col=0; col<X_n_cols; ++col)
125 {
126 const eT tmp_val = X.at(row,col);
127
128 if(tmp_val > max_val)
129 {
130 max_val = tmp_val;
131 }
132 }
133
134 return max_val;
135 }
136
137
138
139 template<typename eT>
140 inline
141 eT
142 op_max::direct_max(const subview<eT>& X)
143 {
144 arma_extra_debug_sigprint();
145
146 const uword X_n_elem = X.n_elem;
147
148 eT max_val = priv::most_neg<eT>();
149
150 for(uword i=0; i<X_n_elem; ++i)
151 {
152 eT tmp_val = X[i];
153
154 if(tmp_val > max_val)
155 {
156 max_val = tmp_val;
157 }
158 }
159
160 return max_val;
161 }
162
163
164
165 template<typename eT>
166 inline
167 eT
168 op_max::direct_max(const diagview<eT>& X)
169 {
170 arma_extra_debug_sigprint();
171
172 const uword X_n_elem = X.n_elem;
173
174 eT max_val = priv::most_neg<eT>();
175
176 for(uword i=0; i<X_n_elem; ++i)
177 {
178 eT tmp_val = X[i];
179
180 if(tmp_val > max_val)
181 {
182 max_val = tmp_val;
183 }
184 }
185
186 return max_val;
187 }
188
189
190
191 //! \brief
192 //! For each row or for each column, find the maximum value.
193 //! The result is stored in a dense matrix that has either one column or one row.
194 //! The dimension, for which the maxima are found, is set via the max() function.
195 template<typename T1>
196 inline
197 void
198 op_max::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_max>& in)
199 {
200 arma_extra_debug_sigprint();
201
202 typedef typename T1::elem_type eT;
203
204 const unwrap_check<T1> tmp(in.m, out);
205 const Mat<eT>& X = tmp.M;
206
207 const uword dim = in.aux_uword_a;
208 arma_debug_check( (dim > 1), "max(): incorrect usage. dim must be 0 or 1");
209
210 const uword X_n_rows = X.n_rows;
211 const uword X_n_cols = X.n_cols;
212
213 if(dim == 0)
214 {
215 arma_extra_debug_print("op_max::apply(), dim = 0");
216
217 arma_debug_check( (X_n_rows == 0), "max(): given object has zero rows" );
218
219 out.set_size(1, X_n_cols);
220
221 eT* out_mem = out.memptr();
222
223 for(uword col=0; col<X_n_cols; ++col)
224 {
225 out_mem[col] = op_max::direct_max( X.colptr(col), X_n_rows );
226 }
227 }
228 else
229 if(dim == 1)
230 {
231 arma_extra_debug_print("op_max::apply(), dim = 1");
232
233 arma_debug_check( (X_n_cols == 0), "max(): given object has zero columns" );
234
235 out.set_size(X_n_rows, 1);
236
237 eT* out_mem = out.memptr();
238
239 for(uword row=0; row<X_n_rows; ++row)
240 {
241 out_mem[row] = op_max::direct_max( X, row );
242 }
243 }
244 }
245
246
247
248 template<typename T>
249 inline
250 std::complex<T>
251 op_max::direct_max(const std::complex<T>* const X, const uword n_elem)
252 {
253 arma_extra_debug_sigprint();
254
255 uword index = 0;
256 T max_val = priv::most_neg<T>();
257
258 for(uword i=0; i<n_elem; ++i)
259 {
260 const T tmp_val = std::abs(X[i]);
261
262 if(tmp_val > max_val)
263 {
264 max_val = tmp_val;
265 index = i;
266 }
267 }
268
269 return X[index];
270 }
271
272
273
274 template<typename T>
275 inline
276 std::complex<T>
277 op_max::direct_max(const std::complex<T>* const X, const uword n_elem, uword& index_of_max_val)
278 {
279 arma_extra_debug_sigprint();
280
281 uword index = 0;
282 T max_val = priv::most_neg<T>();
283
284 for(uword i=0; i<n_elem; ++i)
285 {
286 const T tmp_val = std::abs(X[i]);
287
288 if(tmp_val > max_val)
289 {
290 max_val = tmp_val;
291 index = i;
292 }
293 }
294
295 index_of_max_val = index;
296
297 return X[index];
298 }
299
300
301
302 template<typename T>
303 inline
304 std::complex<T>
305 op_max::direct_max(const Mat< std::complex<T> >& X, const uword row)
306 {
307 arma_extra_debug_sigprint();
308
309 const uword X_n_cols = X.n_cols;
310
311 uword index = 0;
312 T max_val = priv::most_neg<T>();
313
314 for(uword col=0; col<X_n_cols; ++col)
315 {
316 const T tmp_val = std::abs(X.at(row,col));
317
318 if(tmp_val > max_val)
319 {
320 max_val = tmp_val;
321 index = col;
322 }
323 }
324
325 return X.at(row,index);
326 }
327
328
329
330 template<typename T>
331 inline
332 std::complex<T>
333 op_max::direct_max(const subview< std::complex<T> >& X)
334 {
335 arma_extra_debug_sigprint();
336
337 const uword X_n_elem = X.n_elem;
338
339 uword index = 0;
340 T max_val = priv::most_neg<T>();
341
342 for(uword i=0; i<X_n_elem; ++i)
343 {
344 const T tmp_val = std::abs(X[i]);
345
346 if(tmp_val > max_val)
347 {
348 max_val = tmp_val;
349 index = i;
350 }
351 }
352
353 return X[index];
354 }
355
356
357
358 template<typename T>
359 inline
360 std::complex<T>
361 op_max::direct_max(const diagview< std::complex<T> >& X)
362 {
363 arma_extra_debug_sigprint();
364
365 const uword X_n_elem = X.n_elem;
366
367 uword index = 0;
368 T max_val = priv::most_neg<T>();
369
370 for(uword i=0; i<X_n_elem; ++i)
371 {
372 const T tmp_val = std::abs(X[i]);
373
374 if(tmp_val > max_val)
375 {
376 max_val = tmp_val;
377 index = i;
378 }
379 }
380
381 return X[index];
382 }
383
384
385
386 //! @}