MathPHP\Statistics\Average::harmonicMean PHP 메소드

harmonicMean() 공개 정적인 메소드

Appropriate for situations when the average of rates is desired. https://en.wikipedia.org/wiki/Harmonic_mean
public static harmonicMean ( array $numbers ) : number
$numbers array
리턴 number
    public static function harmonicMean(array $numbers)
    {
        if (empty($numbers)) {
            return null;
        }
        // Can't be computed for negative values.
        if (!empty(array_filter($numbers, function ($x) {
            return $x < 0;
        }))) {
            throw new Exception\BadDataException('Harmonic mean cannot be computed for negative values.');
        }
        $n = count($numbers);
        return $n / array_sum(array_map(function ($x) {
            return 1 / $x;
        }, $numbers));
    }