diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java index 051eaf6c3..bc96a8781 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java @@ -17,45 +17,41 @@ package org.apache.commons.math.stat.univariate.summary; import java.io.Serializable; -import org - .apache - .commons - .math - .stat - .univariate - .AbstractStorelessUnivariateStatistic; +import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; /** - * Returns the sum of the natural logs for this collection of values. + * Returns the sum of the natural logs for this collection of values. + *

+ * Uses {@link java.lang.Math#log(double)} to compute the logs. Therefore, + *

* - * @version $Revision: 1.17 $ $Date: 2004/04/27 16:42:32 $ + * @version $Revision: 1.18 $ $Date: 2004/06/18 06:32:07 $ */ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ static final long serialVersionUID = -370076995648386763L; - /** */ + /**Number of values that have been added */ private int n = 0; /** * The currently running value */ - private double value = Double.NaN; - - /** */ - private boolean init = true; + private double value = 0d; /** * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) */ public void increment(final double d) { - if (init) { - value = Math.log(d); - init = false; - } else { - value += Math.log(d); - } + value += Math.log(d); n++; } @@ -63,7 +59,11 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements S * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult() */ public double getResult() { - return value; + if (n > 0) { + return value; + } else { + return Double.NaN; + } } /** @@ -77,23 +77,22 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements S * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear() */ public void clear() { - value = Double.NaN; - init = true; + value = 0d; n = 0; } /** - * Returns the sum of the natural logs for this collection of values + * Returns the sum of the natural logs for this collection of values. + *

+ * See {@link SumOfLogs}. + * * @param values Is a double[] containing the values * @param begin processing at this point in the array * @param length the number of elements to include * @return the sumLog value or Double.NaN if the array is empty * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) */ - public double evaluate( - final double[] values, - final int begin, - final int length) { + public double evaluate(final double[] values, final int begin, final int length) { double sumLog = Double.NaN; if (test(values, begin, length)) { sumLog = 0.0;