comparison armadillo-2.4.4/include/armadillo_bits/op_min_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_min
15 //! @{
16
17
18
19 template<typename eT>
20 arma_pure
21 inline
22 eT
23 op_min::direct_min(const eT* const X, const uword n_elem)
24 {
25 arma_extra_debug_sigprint();
26
27 eT min_val = priv::most_pos<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 < min_val)
37 {
38 min_val = X_i;
39 }
40
41 if(X_j < min_val)
42 {
43 min_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 < min_val)
53 {
54 min_val = X_i;
55 }
56 }
57
58 return min_val;
59 }
60
61
62
63 template<typename eT>
64 inline
65 eT
66 op_min::direct_min(const eT* const X, const uword n_elem, uword& index_of_min_val)
67 {
68 arma_extra_debug_sigprint();
69
70 eT min_val = priv::most_pos<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 < min_val)
82 {
83 min_val = X_i;
84 best_index = i;
85 }
86
87 if(X_j < min_val)
88 {
89 min_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 < min_val)
100 {
101 min_val = X_i;
102 best_index = i;
103 }
104 }
105
106 index_of_min_val = best_index;
107
108 return min_val;
109 }
110
111
112
113 template<typename eT>
114 inline
115 eT
116 op_min::direct_min(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 min_val = priv::most_pos<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 < min_val)
129 {
130 min_val = tmp_val;
131 }
132 }
133
134 return min_val;
135 }
136
137
138
139 template<typename eT>
140 inline
141 eT
142 op_min::direct_min(const subview<eT>& X)
143 {
144 arma_extra_debug_sigprint();
145
146 const uword X_n_elem = X.n_elem;
147
148 eT min_val = priv::most_pos<eT>();
149
150 for(uword i=0; i<X_n_elem; ++i)
151 {
152 eT tmp_val = X[i];
153
154 if(tmp_val < min_val)
155 {
156 min_val = tmp_val;
157 }
158 }
159
160 return min_val;
161 }
162
163
164
165 template<typename eT>
166 inline
167 eT
168 op_min::direct_min(const diagview<eT>& X)
169 {
170 arma_extra_debug_sigprint();
171
172 const uword X_n_elem = X.n_elem;
173
174 eT min_val = priv::most_pos<eT>();;
175
176 for(uword i=0; i<X_n_elem; ++i)
177 {
178 eT tmp_val = X[i];
179
180 if(tmp_val < min_val)
181 {
182 min_val = tmp_val;
183 }
184 }
185
186 return min_val;
187 }
188
189
190
191 //! \brief
192 //! For each row or for each column, find the minimum value.
193 //! The result is stored in a dense matrix that has either one column or one row.
194 //! The dimension, for which the minima are found, is set via the min() function.
195 template<typename T1>
196 inline void op_min::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_min>& in)
197 {
198 arma_extra_debug_sigprint();
199
200 typedef typename T1::elem_type eT;
201
202 const unwrap_check<T1> tmp(in.m, out);
203 const Mat<eT>& X = tmp.M;
204
205 const uword dim = in.aux_uword_a;
206 arma_debug_check( (dim > 1), "min(): incorrect usage. dim must be 0 or 1");
207
208 const uword X_n_rows = X.n_rows;
209 const uword X_n_cols = X.n_cols;
210
211 if(dim == 0) // min in each column
212 {
213 arma_extra_debug_print("op_min::apply(), dim = 0");
214
215 arma_debug_check( (X_n_rows == 0), "min(): given object has zero rows" );
216
217 out.set_size(1, X_n_cols);
218
219 eT* out_mem = out.memptr();
220
221 for(uword col=0; col<X_n_cols; ++col)
222 {
223 out_mem[col] = op_min::direct_min( X.colptr(col), X_n_rows );
224 }
225 }
226 else
227 if(dim == 1) // min in each row
228 {
229 arma_extra_debug_print("op_min::apply(), dim = 1");
230
231 arma_debug_check( (X_n_cols == 0), "min(): given object has zero columns" );
232
233 out.set_size(X_n_rows, 1);
234
235 eT* out_mem = out.memptr();
236
237 for(uword row=0; row<X_n_rows; ++row)
238 {
239 out_mem[row] = op_min::direct_min( X, row );
240 }
241 }
242 }
243
244
245
246 template<typename T>
247 inline
248 std::complex<T>
249 op_min::direct_min(const std::complex<T>* const X, const uword n_elem)
250 {
251 arma_extra_debug_sigprint();
252
253 uword index = 0;
254 T min_val = priv::most_pos<T>();
255
256 for(uword i=0; i<n_elem; ++i)
257 {
258 const T tmp_val = std::abs(X[i]);
259
260 if(tmp_val < min_val)
261 {
262 min_val = tmp_val;
263 index = i;
264 }
265 }
266
267 return X[index];
268 }
269
270
271
272 template<typename T>
273 inline
274 std::complex<T>
275 op_min::direct_min(const std::complex<T>* const X, const uword n_elem, uword& index_of_min_val)
276 {
277 arma_extra_debug_sigprint();
278
279 uword index = 0;
280 T min_val = priv::most_pos<T>();
281
282 for(uword i=0; i<n_elem; ++i)
283 {
284 const T tmp_val = std::abs(X[i]);
285
286 if(tmp_val < min_val)
287 {
288 min_val = tmp_val;
289 index = i;
290 }
291 }
292
293 index_of_min_val = index;
294
295 return X[index];
296 }
297
298
299
300 template<typename T>
301 inline
302 std::complex<T>
303 op_min::direct_min(const Mat< std::complex<T> >& X, const uword row)
304 {
305 arma_extra_debug_sigprint();
306
307 const uword X_n_cols = X.n_cols;
308
309 uword index = 0;
310 T min_val = priv::most_pos<T>();
311
312 for(uword col=0; col<X_n_cols; ++col)
313 {
314 const T tmp_val = std::abs(X.at(row,col));
315
316 if(tmp_val < min_val)
317 {
318 min_val = tmp_val;
319 index = col;
320 }
321 }
322
323 return X.at(row,index);
324 }
325
326
327
328 template<typename T>
329 inline
330 std::complex<T>
331 op_min::direct_min(const subview< std::complex<T> >& X)
332 {
333 arma_extra_debug_sigprint();
334
335 const uword X_n_elem = X.n_elem;
336 uword index = 0;
337 T min_val = priv::most_pos<T>();
338
339 for(uword i=0; i<X_n_elem; ++i)
340 {
341 const T tmp_val = std::abs(X[i]);
342
343 if(tmp_val < min_val)
344 {
345 min_val = tmp_val;
346 index = i;
347 }
348 }
349
350 return X[index];
351 }
352
353
354
355 template<typename T>
356 inline
357 std::complex<T>
358 op_min::direct_min(const diagview< std::complex<T> >& X)
359 {
360 arma_extra_debug_sigprint();
361
362 const uword X_n_elem = X.n_elem;
363 uword index = 0;
364 T min_val = priv::most_pos<T>();
365
366 for(uword i=0; i<X_n_elem; ++i)
367 {
368 const T tmp_val = std::abs(X[i]);
369
370 if(tmp_val < min_val)
371 {
372 min_val = tmp_val;
373 index = i;
374 }
375 }
376
377 return X[index];
378 }
379
380
381
382 //! @}