16 #ifndef INCLUDE_TEMPLE_CONSTEXPR_NUMERIC_H
17 #define INCLUDE_TEMPLE_CONSTEXPR_NUMERIC_H
27 namespace Molassembler {
38 template<
class ContainerType>
39 constexpr Traits::getValueType<ContainerType>
sum(
const ContainerType& container) {
40 using ValueType = Traits::getValueType<ContainerType>;
43 std::begin(container),
46 std::plus<ValueType>()
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!";
69 return sum(container) / container.size();
73 template<
class ContainerType>
74 constexpr std::enable_if_t<
75 !std::is_floating_point<Traits::getValueType<ContainerType>>::value,
77 >
average(
const ContainerType& container) {
78 if(container.begin() == container.end()) {
79 throw "Average called on an empty container!";
82 return static_cast<double>(
94 template<
class ContainerType>
95 constexpr std::enable_if_t<
96 std::is_floating_point<Traits::getValueType<ContainerType>>::value,
97 Traits::getValueType<ContainerType>
99 if(container.begin() == container.end()) {
100 throw "geometricMean called on an empty container!";
103 using ValueType = Traits::getValueType<ContainerType>;
109 std::multiplies<ValueType>()
111 1.0 / container.size()
119 template<
class ContainerType,
typename FloatingType>
120 constexpr std::enable_if_t<
121 std::is_floating_point<FloatingType>::value,
124 const ContainerType& container,
125 const FloatingType averageValue
127 assert(container.begin() != container.end());
129 FloatingType
sum = 0;
131 for(
const auto value : container) {
132 auto diff =
static_cast<FloatingType
>(value) - averageValue;
136 return Math::sqrt(sum / container.size());
147 template<
class ContainerType>
148 constexpr
auto stddev(
const ContainerType& container) {
149 assert(container.begin() != container.end());
163 template<
class ContainerType>
164 constexpr
auto min(
const ContainerType& container) {
165 if(container.begin() == container.end()) {
166 throw "Min called on empty container!";
169 auto smallestIter = container.begin();
170 for(
auto it = container.begin(); it != container.end(); ++it) {
171 if(*it < *smallestIter) {
176 return *smallestIter;
187 template<
class ContainerType>
188 constexpr
auto max(
const ContainerType& container) {
189 if(container.begin() == container.end()) {
190 throw "Max called on empty container!";
193 auto largestIter = container.begin();
195 for(
auto it = container.begin(); it != container.end(); ++it) {
196 if(*largestIter < *it) {
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