| 31 | namespace averages |
| 32 | { |
| 33 | double geometric_mean(std::span<const double> data) |
| 34 | { |
| 35 | // The geometric mean of n elements |
| 36 | // is defined as the n-th root of the product of all n elements |
| 37 | double product{ 1 }; |
| 38 | for (auto value : data) |
| 39 | product *= value; |
| 40 | |
| 41 | return data.empty() |
| 42 | ? std::numeric_limits<double>::quiet_NaN() |
| 43 | : std::pow(product, 1.0 / data.size()); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |