Java Geometric Mean geometricMean(double[] nums)

Here you can find the source of geometricMean(double[] nums)

Description

Return the geometric mean of an array of numbers.

License

Open Source License

Parameter

Parameter Description
nums the numbers to average

Return

their geometric mean

Declaration

public static double geometricMean(double[] nums) 

Method Source Code

//package com.java2s;
/*/*  ww w .  ja  va  2 s  .  com*/
 Copyright (C) 2010, 2011 Constantine Lignos

 This file is a part of CATS.

 CATS is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 CATS is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with CATS.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Return the geometric mean of an array of numbers.
     * @param nums the numbers to average
     * @return their geometric mean
     */
    public static double geometricMean(double[] nums) {
        double prod = 1.0;
        for (double num : nums) {
            prod *= num;
        }
        return Math.pow(prod, 1.0 / (double) nums.length);
    }
}

Related

  1. geometricMean(double a, double b)
  2. geometricMean(double... values)
  3. geometricMean(double[] values)
  4. geometricMean(final double[] values)
  5. geometricMean(float[] xs)
  6. geometricMeanFromLog(double[] logValues)