From 0def426df9b72aec18fbec1f0ec28f434ec59450 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Tue, 29 Jun 2004 15:39:15 +0000 Subject: [PATCH] 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 --- .../math/stat/univariate/summary/Sum.java | 4 +-- .../stat/univariate/summary/SumOfSquares.java | 27 ++++++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java b/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java index 2af1293f1..e91eb5def 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java @@ -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 * NaN, then NaN 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 { @@ -33,7 +33,7 @@ public class Sum extends AbstractStorelessUnivariateStatistic implements Seriali static final long serialVersionUID = -8231831954703408316L; /** */ - private int n = 0; + private long n = 0; /** * The currently running sum. diff --git a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java index cb53e64dd..eaa108df6 100644 --- a/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java +++ b/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java @@ -17,18 +17,15 @@ 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 squares of the available values. - * - * @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 + * NaN, then NaN is returned. + * + * @version $Revision: 1.19 $ $Date: 2004/06/29 15:39:15 $ */ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable { @@ -36,7 +33,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement static final long serialVersionUID = 1460986908574398008L; /** */ - private int n = 0; + private long n = 0; /** * The currently running sumSq @@ -47,7 +44,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) */ public void increment(final double d) { - if (Double.isNaN(value)) { + if (n == 0) { value = d * d; } else { value += d * d; @@ -79,21 +76,19 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implement /** * Returns the sum of the squares of the available values. + * * @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 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) */ - 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 sumSq = Double.NaN; if (test(values, begin, length)) { sumSq = 0.0; for (int i = begin; i < begin + length; i++) { - sumSq += Math.pow(values[i], 2.0); + sumSq += values[i] * values[i]; } } return sumSq;