op_min_meat.hpp
Go to the documentation of this file.
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 
16 
17 
18 
19 template<typename eT>
21 inline
22 eT
23 op_min::direct_min(const eT* const X, const uword n_elem)
24  {
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  {
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  {
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
143  {
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
169  {
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 
195 template<typename T1>
197  {
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  {
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  {
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  {
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  {
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  {
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 
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
void set_size(const uword in_elem)
change the matrix to have user specified dimensions (data is not preserved)
Definition: Mat_meat.hpp:4211
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
static eT direct_min(const eT *const X, const uword N)
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
#define arma_extra_debug_print
Definition: debug.hpp:1118
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: Op_bones.hpp:45
arma_inline arma_warn_unused eT * colptr(const uword in_col)
returns a pointer to array of eTs for a specified column; no bounds check
Definition: Mat_meat.hpp:4000
const uword n_elem
u32 uword
Definition: typedef.hpp:85
#define arma_debug_check
Definition: debug.hpp:1084
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the matrix as a vector); no bounds check.
Definition: Mat_meat.hpp:3692
arma_inline const eOp< T1, eop_abs > abs(const Base< typename T1::elem_type, T1 > &X, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
Definition: fn_elem.hpp:317
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
#define arma_pure
Dense matrix class.
const uword n_elem
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_min > &in)
For each row or for each column, find the minimum value. The result is stored in a dense matrix that ...
Class for storing data required to extract and set the diagonals of a matrix.
const Mat< eT > M
Definition: unwrap.hpp:142
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
Definition: Op_bones.hpp:47


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:58