eDSP  0.0.1
A cross-platform DSP library written in C++.
harmonic_mean.hpp
Go to the documentation of this file.
1 /*
2  * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3  * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation, either version 3 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along width
16  * this program. If not, see <http://www.gnu.org/licenses/>
17  *
18  * File: harmonic_mean.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 2018-06-13
21  */
22 #ifndef EDSP_STATISTICAL_HARMONIC_MEAN_H
23 #define EDSP_STATISTICAL_HARMONIC_MEAN_H
24 
25 #include <edsp/math/numeric.hpp>
26 #include <edsp/meta/iterator.hpp>
27 #include <numeric>
28 
29 namespace edsp { namespace statistics {
30 
44  template <typename ForwardIt>
45  constexpr meta::value_type_t<ForwardIt> harmonic_mean(ForwardIt first, ForwardIt last) {
46  using input_t = meta::value_type_t<ForwardIt>;
47  const auto predicate = [](const input_t prev, const input_t current) { return prev + math::inv(current); };
48  const auto acc = std::accumulate(first, last, static_cast<input_t>(0), predicate);
49  return static_cast<input_t>(std::distance(first, last)) / acc;
50  }
51 }} // namespace edsp::statistics
52 
53 #endif // EDSP_STATISTICAL_HARMONIC_MEAN_H
constexpr T inv(T x)
Computes the inverse value of the input number.
Definition: numeric.hpp:208
constexpr meta::value_type_t< ForwardIt > harmonic_mean(ForwardIt first, ForwardIt last)
Computes the harmonic mean of the range [first, last)
Definition: harmonic_mean.hpp:45
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29