op_mean_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-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_mean::direct_mean(const eT* const X, const uword n_elem)
24  {
26 
27  typedef typename get_pod_type<eT>::result T;
28 
29  const eT result = arrayops::accumulate(X, n_elem) / T(n_elem);
30 
31  return arma_isfinite(result) ? result : op_mean::direct_mean_robust(X, n_elem);
32  }
33 
34 
35 
36 template<typename eT>
37 inline
38 eT
39 op_mean::direct_mean(const Mat<eT>& X, const uword row)
40  {
42 
43  typedef typename get_pod_type<eT>::result T;
44 
45  const uword X_n_cols = X.n_cols;
46 
47  eT val = eT(0);
48 
49  for(uword col=0; col<X_n_cols; ++col)
50  {
51  val += X.at(row,col);
52  }
53 
54  const eT result = val / T(X_n_cols);
55 
56  return arma_isfinite(result) ? result : direct_mean_robust(X, row);
57  }
58 
59 
60 
61 template<typename eT>
62 inline
63 eT
65  {
67 
68  typedef typename get_pod_type<eT>::result T;
69 
70  const uword X_n_elem = X.n_elem;
71 
72  eT val = eT(0);
73 
74  for(uword i=0; i<X_n_elem; ++i)
75  {
76  val += X[i];
77  }
78 
79  const eT result = val / T(X_n_elem);
80 
81  return arma_isfinite(result) ? result : direct_mean_robust(X);
82  }
83 
84 
85 
86 template<typename eT>
87 inline
88 eT
90  {
92 
93  typedef typename get_pod_type<eT>::result T;
94 
95  const uword X_n_elem = X.n_elem;
96 
97  eT val = eT(0);
98 
99  for(uword i=0; i<X_n_elem; ++i)
100  {
101  val += X[i];
102  }
103 
104  const eT result = val / T(X_n_elem);
105 
106  return arma_isfinite(result) ? result : direct_mean_robust(X);
107  }
108 
109 
110 
115 template<typename T1>
116 inline
117 void
119  {
121 
122  typedef typename T1::elem_type eT;
123  typedef typename get_pod_type<eT>::result T;
124 
125  const unwrap_check<T1> tmp(in.m, out);
126  const Mat<eT>& X = tmp.M;
127 
128  const uword dim = in.aux_uword_a;
129  arma_debug_check( (dim > 1), "mean(): incorrect usage. dim must be 0 or 1");
130 
131  const uword X_n_rows = X.n_rows;
132  const uword X_n_cols = X.n_cols;
133 
134  if(dim == 0)
135  {
136  arma_extra_debug_print("op_mean::apply(), dim = 0");
137 
138  out.set_size( (X_n_rows > 0) ? 1 : 0, X_n_cols );
139 
140  if(X_n_rows > 0)
141  {
142  eT* out_mem = out.memptr();
143 
144  for(uword col=0; col<X_n_cols; ++col)
145  {
146  out_mem[col] = op_mean::direct_mean( X.colptr(col), X_n_rows );
147  }
148  }
149  }
150  else
151  if(dim == 1)
152  {
153  arma_extra_debug_print("op_mean::apply(), dim = 1");
154 
155  out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0);
156 
157  if(X_n_cols > 0)
158  {
159  eT* out_mem = out.memptr();
160 
161  for(uword row=0; row<X_n_rows; ++row)
162  {
163  out_mem[row] = op_mean::direct_mean( X, row );
164  }
165  }
166  }
167  }
168 
169 
170 
171 template<typename eT>
172 arma_pure
173 inline
174 eT
175 op_mean::direct_mean_robust(const eT* const X, const uword n_elem)
176  {
178 
179  // use an adapted form of the mean finding algorithm from the running_stat class
180 
181  typedef typename get_pod_type<eT>::result T;
182 
183  uword i,j;
184 
185  eT r_mean = eT(0);
186 
187  for(i=0, j=1; j<n_elem; i+=2, j+=2)
188  {
189  const eT Xi = X[i];
190  const eT Xj = X[j];
191 
192  r_mean = r_mean + (Xi - r_mean)/T(j); // we need i+1, and j is equivalent to i+1 here
193  r_mean = r_mean + (Xj - r_mean)/T(j+1);
194  }
195 
196 
197  if(i < n_elem)
198  {
199  const eT Xi = X[i];
200 
201  r_mean = r_mean + (Xi - r_mean)/T(i+1);
202  }
203 
204  return r_mean;
205  }
206 
207 
208 
209 template<typename eT>
210 inline
211 eT
213  {
215 
216  typedef typename get_pod_type<eT>::result T;
217 
218  const uword X_n_cols = X.n_cols;
219 
220  eT r_mean = eT(0);
221 
222  for(uword col=0; col<X_n_cols; ++col)
223  {
224  r_mean = r_mean + (X.at(row,col) - r_mean)/T(col+1);
225  }
226 
227  return r_mean;
228  }
229 
230 
231 
232 template<typename eT>
233 inline
234 eT
236  {
238 
239  typedef typename get_pod_type<eT>::result T;
240 
241  const uword X_n_elem = X.n_elem;
242 
243  eT r_mean = eT(0);
244 
245  for(uword i=0; i<X_n_elem; ++i)
246  {
247  r_mean = r_mean + (X[i] - r_mean)/T(i+1);
248  }
249 
250  return r_mean;
251  }
252 
253 
254 
255 template<typename eT>
256 inline
257 eT
259  {
261 
262  typedef typename get_pod_type<eT>::result T;
263 
264  const uword X_n_elem = X.n_elem;
265 
266  eT r_mean = eT(0);
267 
268  for(uword i=0; i<X_n_elem; ++i)
269  {
270  r_mean = r_mean + (X[i] - r_mean)/T(i+1);
271  }
272 
273  return r_mean;
274  }
275 
276 
277 
279 
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_mean(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
static eT direct_mean_robust(const eT *const X, const uword N)
#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
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_mean > &in)
For each row or for each column, find the mean value. The result is stored in a dense matrix that has...
#define arma_pure
Dense matrix class.
const uword n_elem
Class for storing data required to extract and set the diagonals of a matrix.
arma_hot static arma_pure eT accumulate(const eT *src, const uword n_elem)
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
arma_inline bool arma_isfinite(eT val)
Definition: cmath_wrap.hpp:29


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