Improved efficiency and javadoc.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-06-18 06:32:07 +00:00
parent 4a92f7c65e
commit 528614edae
1 changed files with 27 additions and 28 deletions

View File

@ -17,45 +17,41 @@ package org.apache.commons.math.stat.univariate.summary;
import java.io.Serializable; import java.io.Serializable;
import org import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
.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.
* <p>
* Uses {@link java.lang.Math#log(double)} to compute the logs. Therefore,
* <ul>
* <li>If any of values are < 0, the result is <code>NaN.</code></li>
* <li>If all values are non-negative and less than <code>Double.POSITIVE_INFINITY</code>,
* but at least one value is 0, the result is <code>Double.NEGATIVE_INFINITY.</code></li>
* <li>If both <code>Double.POSITIVE_INFINITY</code> and
* <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
* <code>NaN.</code></li>
* </ul>
* *
* @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 { public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable {
/** Serializable version identifier */ /** Serializable version identifier */
static final long serialVersionUID = -370076995648386763L; static final long serialVersionUID = -370076995648386763L;
/** */ /**Number of values that have been added */
private int n = 0; private int n = 0;
/** /**
* The currently running value * The currently running value
*/ */
private double value = Double.NaN; private double value = 0d;
/** */
private boolean init = true;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(final double d) { public void increment(final double d) {
if (init) { value += Math.log(d);
value = Math.log(d);
init = false;
} else {
value += Math.log(d);
}
n++; n++;
} }
@ -63,7 +59,11 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements S
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double 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() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
public void clear() { public void clear() {
value = Double.NaN; value = 0d;
init = true;
n = 0; 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.
* <p>
* See {@link SumOfLogs}.
*
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length the number of elements to include * @param length the number of elements to include
* @return the sumLog value or Double.NaN if the array is empty * @return the sumLog value or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate( public double evaluate(final double[] values, final int begin, final int length) {
final double[] values,
final int begin,
final int length) {
double sumLog = Double.NaN; double sumLog = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
sumLog = 0.0; sumLog = 0.0;