Java Geometric Mean geometricMean(final double[] values)

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

Description

Returns the geometric mean of the entries in the input array.

License

Open Source License

Parameter

Parameter Description
values a parameter

Declaration

public static final double geometricMean(final double[] values) 

Method Source Code

//package com.java2s;
/***************************************************************************
 *  Copyright (C) 2012 by H-Store Project                                  *
 *  Brown University                                                       *
 *  Massachusetts Institute of Technology                                  *
 *  Yale University                                                        *
 *                                                                         *
 *  http://hstore.cs.brown.edu/                                            *
 *                                                                         *
 *  Permission is hereby granted, free of charge, to any person obtaining  *
 *  a copy of this software and associated documentation files (the        *
 *  "Software"), to deal in the Software without restriction, including    *
 *  without limitation the rights to use, copy, modify, merge, publish,    *
 *  distribute, sublicense, and/or sell copies of the Software, and to     *
 *  permit persons to whom the Software is furnished to do so, subject to  *
 *  the following conditions:                                              *
 *                                                                         *
 *  The above copyright notice and this permission notice shall be         *
 *  included in all copies or substantial portions of the Software.        *
 *                                                                         *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        *
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     *
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      *
 *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  *
 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  *
 *  OTHER DEALINGS IN THE SOFTWARE.                                        *
 ***************************************************************************/

public class Main {
    /**//w ww  .  ja v a 2 s  .c  om
     * Returns the geometric mean of the entries in the input array.
     * 
     * @param values
     * @return
     */
    public static final double geometricMean(final double[] values) {
        return (geometricMean(values, null));
    }

    /**
     * Returns the geometric mean of the entries in the input array. If the
     * zero_value is not null, all zeroes will be replaced with that value
     * 
     * @param values
     * @param zero_value
     * @return
     */
    public static final double geometricMean(final double[] values, final Double zero_value) {
        double sumLog = 0.0d;
        for (double v : values) {
            if (v == 0 && zero_value != null)
                v = zero_value;
            sumLog += Math.log(v);
        }
        return Math.exp(sumLog / (double) values.length);
    }
}

Related

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