Agroeye  1.0
ContainerMath.h
Go to the documentation of this file.
1 #ifndef CONTAINERMATH_H
2 #define CONTAINERMATH_H
3 
4 #include <algorithm>
5 #include <numeric>
6 #include <iterator>
7 #include <type_traits>
8 #include <cmath>
9 #include <iostream>
10 
11 namespace agroeye {
12 namespace algorithm {
13 namespace math {
14 
23 template < class Iterator >
24 double mean(Iterator first, Iterator last) {
25  size_t distance = std::distance(first, last);
26  double sum = std::accumulate(first, last, *first-*first);
27  return sum/distance;
28 }
29 
40 template < class Iterator >
41 double mean(Iterator first, Iterator last, typename Iterator::value_type ignoreValue) {
42  // Number of elements in container
43  size_t distance = std::distance(first, last);
44 
45  // Sum of all values
46  double sum = std::accumulate(first, last, *first-*first);
47 
48  // How many times ignored values occured
49  size_t ignoreValuesOccur = std::count(first, last, ignoreValue);
50 
51  // Substract from total sum
52  sum -= ignoreValuesOccur*ignoreValue;
53 
54  // New number of elements taken into account
55  size_t newCount = distance - ignoreValuesOccur;
56 
57  return sum/newCount;
58 }
59 
61 template < typename T >
62 struct quadraticdifference_accumulator{
63  const T mean;
64  T ignoredValue;
65  const bool allVals;
66  quadraticdifference_accumulator(T m) :
67  mean(m), allVals(true) {};
68  quadraticdifference_accumulator(T m, T ignore) :
69  mean(m), ignoredValue(ignore), allVals(false) {};
70 
71  T operator()(T initial, T following) {
72  T val = initial;
73  if (allVals || following!=ignoredValue)
74  val = pow(mean-following, 2) + initial;
75  return val;
76  }
77 };
79 
88 template < class Iterator >
89 double stddev(Iterator first, Iterator last) {
90  quadraticdifference_accumulator<double> qDiff (mean(first, last));
91  double initVal = 0;
92  const auto elementsCount = std::distance(first, last);
93  const double sum = std::accumulate(first, last, initVal, qDiff);
94 
95  return pow(sum/elementsCount, 0.5);
96 }
97 
108 template < class Iterator >
109 double stddev(Iterator first, Iterator last, typename Iterator::value_type ignoreValue) {
110  quadraticdifference_accumulator<double> qDiff (mean(first, last, ignoreValue), ignoreValue);
111  double initVal = 0;
112  const auto elementsCount = std::distance(first, last) - std::count(first, last, ignoreValue);
113  const double sum = std::accumulate(first, last, initVal, qDiff);
114 
115  return pow(sum/elementsCount, 0.5);
116 }
117 
118 
119 } // math namespace
120 } // algorithm namespace
121 } // agroeye namespace
122 
123 #endif
Definition: ContainerMath.h:11
double mean(Iterator first, Iterator last)
Metafunction computing the mean value. This function calculates the mean value of elements in given c...
Definition: ContainerMath.h:24
double stddev(Iterator first, Iterator last)
Metafunction computing the standard deviation value. This function calculates the standard deviation ...
Definition: ContainerMath.h:89