Molassembler  1.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Numeric.h
Go to the documentation of this file.
1 
16 #ifndef INCLUDE_TEMPLE_CONSTEXPR_NUMERIC_H
17 #define INCLUDE_TEMPLE_CONSTEXPR_NUMERIC_H
18 
21 
22 #include <numeric>
23 #include <functional>
24 #include <cassert>
25 
26 namespace Scine {
27 namespace Molassembler {
28 namespace Temple {
29 
38 template<class ContainerType>
39 constexpr Traits::getValueType<ContainerType> sum(const ContainerType& container) {
40  using ValueType = Traits::getValueType<ContainerType>;
41 
42  return std::accumulate(
43  std::begin(container),
44  std::end(container),
45  ValueType {0},
46  std::plus<ValueType>()
47  );
48 }
49 
60 template<class ContainerType>
61 constexpr std::enable_if_t<
62  std::is_floating_point<Traits::getValueType<ContainerType>>::value,
63  Traits::getValueType<ContainerType>
64 > average(const ContainerType& container) {
65  if(container.begin() == container.end()) {
66  throw "Average called on an empty container!";
67  }
68 
69  return sum(container) / container.size();
70 }
71 
73 template<class ContainerType>
74 constexpr std::enable_if_t<
75  !std::is_floating_point<Traits::getValueType<ContainerType>>::value,
76  double
77 > average(const ContainerType& container) {
78  if(container.begin() == container.end()) {
79  throw "Average called on an empty container!";
80  }
81 
82  return static_cast<double>(
83  sum(container)
84  ) / container.size();
85 }
86 
94 template<class ContainerType>
95 constexpr std::enable_if_t<
96  std::is_floating_point<Traits::getValueType<ContainerType>>::value,
97  Traits::getValueType<ContainerType>
98 > geometricMean(const ContainerType& container) {
99  if(container.begin() == container.end()) {
100  throw "geometricMean called on an empty container!";
101  }
102 
103  using ValueType = Traits::getValueType<ContainerType>;
104 
105  return Math::pow(
106  reduce(
107  container,
108  ValueType {1.0},
109  std::multiplies<ValueType>()
110  ),
111  1.0 / container.size()
112  );
113 }
114 
119 template<class ContainerType, typename FloatingType>
120 constexpr std::enable_if_t<
121  std::is_floating_point<FloatingType>::value,
122  FloatingType
124  const ContainerType& container,
125  const FloatingType averageValue
126 ) {
127  assert(container.begin() != container.end());
128 
129  FloatingType sum = 0;
130 
131  for(const auto value : container) {
132  auto diff = static_cast<FloatingType>(value) - averageValue;
133  sum += diff * diff;
134  }
135 
136  return Math::sqrt(sum / container.size());
137 }
138 
139 
147 template<class ContainerType>
148 constexpr auto stddev(const ContainerType& container) {
149  assert(container.begin() != container.end());
150 
151  return stddev(container, average(container));
152 }
153 
163 template<class ContainerType>
164 constexpr auto min(const ContainerType& container) {
165  if(container.begin() == container.end()) {
166  throw "Min called on empty container!";
167  }
168 
169  auto smallestIter = container.begin();
170  for(auto it = container.begin(); it != container.end(); ++it) {
171  if(*it < *smallestIter) {
172  smallestIter = it;
173  }
174  }
175 
176  return *smallestIter;
177 }
178 
187 template<class ContainerType>
188 constexpr auto max(const ContainerType& container) {
189  if(container.begin() == container.end()) {
190  throw "Max called on empty container!";
191  }
192 
193  auto largestIter = container.begin();
194 
195  for(auto it = container.begin(); it != container.end(); ++it) {
196  if(*largestIter < *it) {
197  largestIter = it;
198  }
199  }
200 
201  return *largestIter;
202 }
203 
204 } // namespace Temple
205 } // namespace Molassembler
206 } // namespace Scine
207 
208 #endif
constexpr math implementations
constexpr auto max(const ContainerType &container)
Composable max function. Returns the smallest value of any container.
Definition: Numeric.h:188
T accumulate(const Container &container, T init, BinaryFunction &&reductionFunction)
Accumulate shorthand.
Definition: Functional.h:226
Provides a few basic function and container traits.
constexpr T reduce(const ArrayType< T, size > &array, T init, BinaryFunction &&reduction)
Reduce an array-like container with a binary function.
Definition: Containers.h:88
constexpr std::enable_if_t< std::is_floating_point< FloatingType >::value, FloatingType > stddev(const ContainerType &container, const FloatingType averageValue)
Calculate the standard deviation of a container with a known average.
Definition: Numeric.h:123
constexpr std::enable_if_t< std::is_floating_point< Traits::getValueType< ContainerType > >::value, Traits::getValueType< ContainerType >> average(const ContainerType &container)
Definition: Numeric.h:64
constexpr T sum(const ArrayType< T, size > &array)
Sum up all elements of an array-like class.
Definition: Containers.h:111
constexpr std::enable_if_t< std::is_floating_point< Traits::getValueType< ContainerType > >::value, Traits::getValueType< ContainerType >> geometricMean(const ContainerType &container)
Geometric average of all values in a container.
Definition: Numeric.h:98
constexpr auto min(const ContainerType &container)
Composable min_element function. Returns the smallest value of any container.
Definition: Numeric.h:164