running_stat_vec_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  {
23  }
24 
25 
26 
27 template<typename eT>
29  : calc_cov(in_calc_cov)
30  {
32  }
33 
34 
35 
36 template<typename eT>
38  : calc_cov (in_rsv.calc_cov)
39  , counter (in_rsv.counter)
40  , r_mean (in_rsv.r_mean)
41  , r_var (in_rsv.r_var)
42  , r_cov (in_rsv.r_cov)
43  , min_val (in_rsv.min_val)
44  , max_val (in_rsv.max_val)
45  , min_val_norm(in_rsv.min_val_norm)
46  , max_val_norm(in_rsv.max_val_norm)
47  {
49  }
50 
51 
52 
53 template<typename eT>
56  {
58 
59  access::rw(calc_cov) = in_rsv.calc_cov;
60 
61  counter = in_rsv.counter;
62  r_mean = in_rsv.r_mean;
63  r_var = in_rsv.r_var;
64  r_cov = in_rsv.r_cov;
65  min_val = in_rsv.min_val;
66  max_val = in_rsv.max_val;
67  min_val_norm = in_rsv.min_val_norm;
68  max_val_norm = in_rsv.max_val_norm;
69 
70  return *this;
71  }
72 
73 
74 
76 template<typename eT>
77 template<typename T1>
79 inline
80 void
82  {
84 
85  //typedef typename get_pod_type<eT>::result T;
86 
87  const unwrap<T1> tmp(X.get_ref());
88  const Mat<eT>& sample = tmp.M;
89 
90  if( sample.is_empty() )
91  {
92  return;
93  }
94 
95  if( sample.is_finite() == false )
96  {
97  arma_warn(true, "running_stat_vec: sample ignored as it has non-finite elements");
98  return;
99  }
100 
101  running_stat_vec_aux::update_stats(*this, sample);
102  }
103 
104 
105 
107 template<typename eT>
108 template<typename T1>
109 arma_hot
110 inline
111 void
113  {
115 
116  //typedef typename std::complex<typename get_pod_type<eT>::result> eT;
117 
118  const unwrap<T1> tmp(X.get_ref());
119  const Mat<eT>& sample = tmp.M;
120 
121  if( sample.is_empty() )
122  {
123  return;
124  }
125 
126  if( sample.is_finite() == false )
127  {
128  arma_warn(true, "running_stat_vec: sample ignored as it has non-finite elements");
129  return;
130  }
131 
132  running_stat_vec_aux::update_stats(*this, sample);
133  }
134 
135 
136 
138 template<typename eT>
139 inline
140 void
142  {
144 
145  counter.reset();
146 
147  r_mean.reset();
148  r_var.reset();
149  r_cov.reset();
150 
151  min_val.reset();
152  max_val.reset();
153 
156 
157  r_var_dummy.reset();
158  r_cov_dummy.reset();
159 
160  tmp1.reset();
161  tmp2.reset();
162  }
163 
164 
165 
167 template<typename eT>
168 inline
169 const Mat<eT>&
171  {
173 
174  return r_mean;
175  }
176 
177 
178 
180 template<typename eT>
181 inline
184  {
186 
187  const T N = counter.value();
188 
189  if(N > T(1))
190  {
191  if(norm_type == 0)
192  {
193  return r_var;
194  }
195  else
196  {
197  const T N_minus_1 = counter.value_minus_1();
198 
199  r_var_dummy = (N_minus_1/N) * r_var;
200 
201  return r_var_dummy;
202  }
203  }
204  else
205  {
206  r_var_dummy.zeros(r_mean.n_rows, r_mean.n_cols);
207 
208  return r_var_dummy;
209  }
210 
211  }
212 
213 
214 
216 template<typename eT>
217 inline
219 running_stat_vec<eT>::stddev(const uword norm_type) const
220  {
222 
223  const T N = counter.value();
224 
225  if(N > T(1))
226  {
227  if(norm_type == 0)
228  {
229  return sqrt(r_var);
230  }
231  else
232  {
233  const T N_minus_1 = counter.value_minus_1();
234 
235  return sqrt( (N_minus_1/N) * r_var );
236  }
237  }
238  else
239  {
240  return Mat<T>();
241  }
242  }
243 
244 
245 
247 template<typename eT>
248 inline
249 const Mat<eT>&
251  {
253 
254  if(calc_cov == true)
255  {
256  const T N = counter.value();
257 
258  if(N > T(1))
259  {
260  if(norm_type == 0)
261  {
262  return r_cov;
263  }
264  else
265  {
266  const T N_minus_1 = counter.value_minus_1();
267 
268  r_cov_dummy = (N_minus_1/N) * r_cov;
269 
270  return r_cov_dummy;
271  }
272  }
273  else
274  {
275  r_cov_dummy.zeros(r_mean.n_rows, r_mean.n_cols);
276 
277  return r_cov_dummy;
278  }
279  }
280  else
281  {
282  r_cov_dummy.reset();
283 
284  return r_cov_dummy;
285  }
286 
287  }
288 
289 
290 
292 template<typename eT>
293 inline
294 const Mat<eT>&
296  {
298 
299  return min_val;
300  }
301 
302 
303 
305 template<typename eT>
306 inline
307 const Mat<eT>&
309  {
311 
312  return max_val;
313  }
314 
315 
316 
318 template<typename eT>
319 inline
322  {
324 
325  return counter.value();
326  }
327 
328 
329 
330 //
331 
332 
333 
335 template<typename eT>
336 inline
337 void
339  {
341 
342  typedef typename running_stat_vec<eT>::T T;
343 
344  const T N = x.counter.value();
345 
346  if(N > T(0))
347  {
348  arma_debug_assert_same_size(x.r_mean, sample, "running_stat_vec(): dimensionality mismatch");
349 
350  const uword n_elem = sample.n_elem;
351  const eT* sample_mem = sample.memptr();
352  eT* r_mean_mem = x.r_mean.memptr();
353  T* r_var_mem = x.r_var.memptr();
354  eT* min_val_mem = x.min_val.memptr();
355  eT* max_val_mem = x.max_val.memptr();
356 
357  const T N_plus_1 = x.counter.value_plus_1();
358  const T N_minus_1 = x.counter.value_minus_1();
359 
360  if(x.calc_cov == true)
361  {
362  Mat<eT>& tmp1 = x.tmp1;
363  Mat<eT>& tmp2 = x.tmp2;
364 
365  tmp1 = sample - x.r_mean;
366 
367  if(sample.n_cols == 1)
368  {
369  tmp2 = tmp1*trans(tmp1);
370  }
371  else
372  {
373  tmp2 = trans(tmp1)*tmp1;
374  }
375 
376  x.r_cov *= (N_minus_1/N);
377  x.r_cov += tmp2 / N_plus_1;
378  }
379 
380 
381  for(uword i=0; i<n_elem; ++i)
382  {
383  const eT val = sample_mem[i];
384 
385  if(val < min_val_mem[i])
386  {
387  min_val_mem[i] = val;
388  }
389 
390  if(val > max_val_mem[i])
391  {
392  max_val_mem[i] = val;
393  }
394 
395  const eT r_mean_val = r_mean_mem[i];
396  const eT tmp = val - r_mean_val;
397 
398  r_var_mem[i] = N_minus_1/N * r_var_mem[i] + (tmp*tmp)/N_plus_1;
399 
400  r_mean_mem[i] = r_mean_val + (val - r_mean_val)/N_plus_1;
401  }
402  }
403  else
404  {
405  arma_debug_check( (sample.is_vec() == false), "running_stat_vec(): given sample is not a vector");
406 
407  x.r_mean.set_size(sample.n_rows, sample.n_cols);
408 
409  x.r_var.zeros(sample.n_rows, sample.n_cols);
410 
411  if(x.calc_cov == true)
412  {
413  x.r_cov.zeros(sample.n_elem, sample.n_elem);
414  }
415 
416  x.min_val.set_size(sample.n_rows, sample.n_cols);
417  x.max_val.set_size(sample.n_rows, sample.n_cols);
418 
419 
420  const uword n_elem = sample.n_elem;
421  const eT* sample_mem = sample.memptr();
422  eT* r_mean_mem = x.r_mean.memptr();
423  eT* min_val_mem = x.min_val.memptr();
424  eT* max_val_mem = x.max_val.memptr();
425 
426 
427  for(uword i=0; i<n_elem; ++i)
428  {
429  const eT val = sample_mem[i];
430 
431  r_mean_mem[i] = val;
432  min_val_mem[i] = val;
433  max_val_mem[i] = val;
434  }
435  }
436 
437  x.counter++;
438  }
439 
440 
441 
443 template<typename T>
444 inline
445 void
446 running_stat_vec_aux::update_stats(running_stat_vec< std::complex<T> >& x, const Mat<T>& sample)
447  {
449 
450  const Mat< std::complex<T> > tmp = conv_to< Mat< std::complex<T> > >::from(sample);
451 
453  }
454 
455 
456 
458 template<typename T>
459 inline
460 void
461 running_stat_vec_aux::update_stats(running_stat_vec< std::complex<T> >& x, const Mat< std::complex<T> >& sample)
462  {
464 
465  typedef typename std::complex<T> eT;
466 
467  const T N = x.counter.value();
468 
469  if(N > T(0))
470  {
471  arma_debug_assert_same_size(x.r_mean, sample, "running_stat_vec(): dimensionality mismatch");
472 
473  const uword n_elem = sample.n_elem;
474  const eT* sample_mem = sample.memptr();
475  eT* r_mean_mem = x.r_mean.memptr();
476  T* r_var_mem = x.r_var.memptr();
477  eT* min_val_mem = x.min_val.memptr();
478  eT* max_val_mem = x.max_val.memptr();
479  T* min_val_norm_mem = x.min_val_norm.memptr();
480  T* max_val_norm_mem = x.max_val_norm.memptr();
481 
482  const T N_plus_1 = x.counter.value_plus_1();
483  const T N_minus_1 = x.counter.value_minus_1();
484 
485  if(x.calc_cov == true)
486  {
487  Mat<eT>& tmp1 = x.tmp1;
488  Mat<eT>& tmp2 = x.tmp2;
489 
490  tmp1 = sample - x.r_mean;
491 
492  if(sample.n_cols == 1)
493  {
494  tmp2 = arma::conj(tmp1)*strans(tmp1);
495  }
496  else
497  {
498  tmp2 = trans(tmp1)*tmp1; //tmp2 = strans(conj(tmp1))*tmp1;
499  }
500 
501  x.r_cov *= (N_minus_1/N);
502  x.r_cov += tmp2 / N_plus_1;
503  }
504 
505 
506  for(uword i=0; i<n_elem; ++i)
507  {
508  const eT& val = sample_mem[i];
509  const T val_norm = std::norm(val);
510 
511  if(val_norm < min_val_norm_mem[i])
512  {
513  min_val_norm_mem[i] = val_norm;
514  min_val_mem[i] = val;
515  }
516 
517  if(val_norm > max_val_norm_mem[i])
518  {
519  max_val_norm_mem[i] = val_norm;
520  max_val_mem[i] = val;
521  }
522 
523  const eT& r_mean_val = r_mean_mem[i];
524 
525  r_var_mem[i] = N_minus_1/N * r_var_mem[i] + std::norm(val - r_mean_val)/N_plus_1;
526 
527  r_mean_mem[i] = r_mean_val + (val - r_mean_val)/N_plus_1;
528  }
529 
530  }
531  else
532  {
533  arma_debug_check( (sample.is_vec() == false), "running_stat_vec(): given sample is not a vector");
534 
535  x.r_mean.set_size(sample.n_rows, sample.n_cols);
536 
537  x.r_var.zeros(sample.n_rows, sample.n_cols);
538 
539  if(x.calc_cov == true)
540  {
541  x.r_cov.zeros(sample.n_elem, sample.n_elem);
542  }
543 
544  x.min_val.set_size(sample.n_rows, sample.n_cols);
545  x.max_val.set_size(sample.n_rows, sample.n_cols);
546 
547  x.min_val_norm.set_size(sample.n_rows, sample.n_cols);
548  x.max_val_norm.set_size(sample.n_rows, sample.n_cols);
549 
550 
551  const uword n_elem = sample.n_elem;
552  const eT* sample_mem = sample.memptr();
553  eT* r_mean_mem = x.r_mean.memptr();
554  eT* min_val_mem = x.min_val.memptr();
555  eT* max_val_mem = x.max_val.memptr();
556  T* min_val_norm_mem = x.min_val_norm.memptr();
557  T* max_val_norm_mem = x.max_val_norm.memptr();
558 
559  for(uword i=0; i<n_elem; ++i)
560  {
561  const eT& val = sample_mem[i];
562  const T val_norm = std::norm(val);
563 
564  r_mean_mem[i] = val;
565  min_val_mem[i] = val;
566  max_val_mem[i] = val;
567 
568  min_val_norm_mem[i] = val_norm;
569  max_val_norm_mem[i] = val_norm;
570  }
571  }
572 
573  x.counter++;
574  }
575 
576 
577 
arma_inline arma_warn_unused bool is_vec() const
returns true if the object can be interpreted as a column or row vector
Definition: Mat_meat.hpp:3824
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
static void update_stats(running_stat_vec< eT > &x, const Mat< eT > &sample)
update statistics to reflect new sample
eT value() const
eT value_minus_1() const
arma_inline const eOp< T1, eop_sqrt > sqrt(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:403
T count() const
number of samples so far
const Mat< eT > & mean() const
mean or average value
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
#define arma_debug_assert_same_size
Definition: debug.hpp:1086
arma_aligned Mat< eT > min_val
arma_aligned Mat< eT > r_cov
arma_inline const T1 & conj(const Base< typename T1::pod_type, T1 > &A)
Definition: fn_elem.hpp:430
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
const Mat< eT > & min() const
vector with minimum values
arma_warn_unused T1::pod_type norm(const Base< typename T1::elem_type, T1 > &X, const uword k, const typename arma_float_or_cx_only< typename T1::elem_type >::result *junk=0)
Definition: fn_norm.hpp:379
arma_aligned arma_counter< T > counter
arma_inline const Op< T1, op_strans > strans(const Base< typename T1::elem_type, T1 > &X, const typename arma_cx_only< typename T1::elem_type >::result *junk=0)
Definition: fn_strans.hpp:22
u32 uword
Definition: typedef.hpp:85
Mat< T > stddev(const uword norm_type=0) const
standard deviation
arma_inline const Op< T1, op_htrans > trans(const Base< typename T1::elem_type, T1 > &X)
Definition: fn_trans.hpp:21
static arma_inline T1 & rw(const T1 &x)
internal function to allow modification of data declared as read-only
Definition: access.hpp:23
#define arma_debug_check
Definition: debug.hpp:1084
arma_aligned Mat< T > min_val_norm
arma_aligned Mat< eT > r_mean
arma_aligned Mat< T > max_val_norm
#define arma_extra_debug_sigprint_this
Definition: debug.hpp:1117
arma_aligned Mat< T > r_var
const Mat< T > & var(const uword norm_type=0)
variance
arma_aligned Mat< eT > tmp2
void reset()
Definition: Mat_meat.hpp:4553
const Mat< eT > & max() const
vector with maximum values
running_stat_vec(const bool in_calc_cov=false)
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
void arma_cold arma_warn(const bool state, const T1 &x)
print a message to the warn stream
Definition: debug.hpp:298
arma_aligned Mat< eT > max_val
get_pod_type< eT >::result T
eT value_plus_1() const
Dense matrix class.
arma_aligned Mat< eT > tmp1
const Mat & zeros()
Definition: Mat_meat.hpp:4331
const Mat< eT > & cov(const uword norm_type=0)
covariance
const running_stat_vec & operator=(const running_stat_vec &in_rsv)
void reset()
set all statistics to zero
#define arma_hot
arma_aligned Mat< T > r_var_dummy
arma_hot void operator()(const Base< T, T1 > &X)
arma_aligned Mat< eT > r_cov_dummy


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