Fixed, documented NaN handling; changed internal counter to long; replaced pow with *.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141352 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ee7756953e
commit
0def426df9
|
@ -25,7 +25,7 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
|
||||||
* If there are no values in the dataset, or any of the values are
|
* If there are no values in the dataset, or any of the values are
|
||||||
* <code>NaN</code>, then <code>NaN</code> is returned.
|
* <code>NaN</code>, then <code>NaN</code> is returned.
|
||||||
*
|
*
|
||||||
* @version $Revision: 1.21 $ $Date: 2004/06/29 14:50:21 $
|
* @version $Revision: 1.22 $ $Date: 2004/06/29 15:39:15 $
|
||||||
*/
|
*/
|
||||||
public class Sum extends AbstractStorelessUnivariateStatistic implements Serializable {
|
public class Sum extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class Sum extends AbstractStorelessUnivariateStatistic implements Seriali
|
||||||
static final long serialVersionUID = -8231831954703408316L;
|
static final long serialVersionUID = -8231831954703408316L;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
private int n = 0;
|
private long n = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The currently running sum.
|
* The currently running sum.
|
||||||
|
|
|
@ -17,18 +17,15 @@ 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 squares of the available values.
|
* Returns the sum of the squares of the available values.
|
||||||
*
|
* <p>
|
||||||
* @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:16 $
|
* If there are no values in the dataset, or any of the values are
|
||||||
|
* <code>NaN</code>, then <code>NaN</code> is returned.
|
||||||
|
*
|
||||||
|
* @version $Revision: 1.19 $ $Date: 2004/06/29 15:39:15 $
|
||||||
*/
|
*/
|
||||||
public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable {
|
public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable {
|
||||||
|
|
||||||
|
@ -36,7 +33,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement
|
||||||
static final long serialVersionUID = 1460986908574398008L;
|
static final long serialVersionUID = 1460986908574398008L;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
private int n = 0;
|
private long n = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The currently running sumSq
|
* The currently running sumSq
|
||||||
|
@ -47,7 +44,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement
|
||||||
* @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 (Double.isNaN(value)) {
|
if (n == 0) {
|
||||||
value = d * d;
|
value = d * d;
|
||||||
} else {
|
} else {
|
||||||
value += d * d;
|
value += d * d;
|
||||||
|
@ -79,21 +76,19 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sum of the squares of the available values.
|
* Returns the sum of the squares of the available values.
|
||||||
|
*
|
||||||
* @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 sum of the squared values or Double.NaN if the array is empty
|
* @return the sum of the squared values 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 sumSq = Double.NaN;
|
double sumSq = Double.NaN;
|
||||||
if (test(values, begin, length)) {
|
if (test(values, begin, length)) {
|
||||||
sumSq = 0.0;
|
sumSq = 0.0;
|
||||||
for (int i = begin; i < begin + length; i++) {
|
for (int i = begin; i < begin + length; i++) {
|
||||||
sumSq += Math.pow(values[i], 2.0);
|
sumSq += values[i] * values[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sumSq;
|
return sumSq;
|
||||||
|
|
Loading…
Reference in New Issue