op_max_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_max::direct_max(const eT* const X, const uword n_elem)
24  {
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  {
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  {
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
143  {
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
169  {
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 
195 template<typename T1>
196 inline
197 void
199  {
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  {
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  {
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  {
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  {
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  {
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 
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
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_max > &in)
For each row or for each column, find the maximum value. The result is stored in a dense matrix that ...
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
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
static eT direct_max(const eT *const X, const uword N)
#define arma_pure
Dense matrix class.
const uword n_elem
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