Application of "apply(Functor x)" strategy (thank you Al Chou) for evaluating UnivariateStatistics against the internal storage collection without exposing the collection or its bounds.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140987 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-07-15 03:45:10 +00:00
parent 1d62c398db
commit 2f4e703627
5 changed files with 53 additions and 132 deletions

View File

@ -59,7 +59,7 @@ import org.apache.commons.math.stat.univariate.rank.Percentile;
/**
* Provides univariate measures for an array of doubles.
* @version $Revision: 1.9 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.10 $ $Date: 2003/07/15 03:45:10 $
*/
public abstract class AbstractStoreUnivariate
extends AbstractUnivariate
@ -88,7 +88,7 @@ public abstract class AbstractStoreUnivariate
*/
public double getPercentile(double p) {
percentile.setPercentile(p);
return percentile.evaluate(this.getValues(), this.start(), this.size());
return apply(percentile);
}
/**

View File

@ -53,6 +53,7 @@
*/
package org.apache.commons.math.stat;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.stat.univariate.moment.FourthMoment;
import org.apache.commons.math.stat.univariate.moment.GeometricMean;
import org.apache.commons.math.stat.univariate.moment.Kurtosis;
@ -67,7 +68,7 @@ import org.apache.commons.math.stat.univariate.summary.SumOfSquares;
/**
* Provides univariate measures for an array of doubles.
* @version $Revision: 1.1 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.2 $ $Date: 2003/07/15 03:45:10 $
*/
public abstract class AbstractUnivariate implements Univariate {
@ -139,23 +140,8 @@ public abstract class AbstractUnivariate implements Univariate {
setWindowSize(window);
}
/**
* Returns the internalValues array.
* @return the array
*/
protected abstract double[] internalValues();
/**
* Returns the start index of the array
* @return start index
*/
protected abstract int start();
/**
* Returns the size of the array appropriate for doing calculations.
* @return Usually this is just numElements.
*/
protected abstract int size();
public abstract double apply(UnivariateStatistic stat);
/**
* If windowSize is set to Infinite,
@ -178,36 +164,21 @@ public abstract class AbstractUnivariate implements Univariate {
* @see org.apache.commons.math.stat.Univariate#getSum()
*/
public double getSum() {
double[] v = internalValues();
if (v != null) {
return sum.evaluate(v, this.start(), this.size());
}
return sum.getResult();
return apply(sum);
}
/**
* @see org.apache.commons.math.stat.Univariate#getSumsq()
*/
public double getSumsq() {
double[] v = internalValues();
if (v != null) {
return sumsq.evaluate(v, this.start(), this.size());
}
return sumsq.getResult();
return apply(sumsq);
}
/**
* @see org.apache.commons.math.stat.Univariate#getMean()
*/
public double getMean() {
double[] v = internalValues();
if (v != null) {
return mean.evaluate(v, this.start(), this.size());
}
return mean.getResult();
return apply(mean);
}
/**
@ -239,12 +210,7 @@ public abstract class AbstractUnivariate implements Univariate {
* a <= 1 value set.
*/
public double getVariance() {
double[] v = internalValues();
if (v != null) {
return variance.evaluate(v, this.start(), this.size());
}
return variance.getResult();
return apply(variance);
}
/**
@ -256,12 +222,7 @@ public abstract class AbstractUnivariate implements Univariate {
* <= 2 value set.
*/
public double getSkewness() {
double[] v = internalValues();
if (v != null) {
return skewness.evaluate(v, this.start(), this.size());
}
return skewness.getResult();
return apply(skewness);
}
/**
@ -274,12 +235,7 @@ public abstract class AbstractUnivariate implements Univariate {
* value set.
*/
public double getKurtosis() {
double[] v = internalValues();
if (v != null) {
return kurtosis.evaluate(v, this.start(), this.size());
}
return kurtosis.getResult();
return apply(kurtosis);
}
/**
@ -301,38 +257,23 @@ public abstract class AbstractUnivariate implements Univariate {
* @see org.apache.commons.math.stat.Univariate#getMax()
*/
public double getMax() {
double[] v = internalValues();
if (v != null) {
return max.evaluate(v, this.start(), this.size());
}
return max.getResult();
return apply(max);
}
/**
* @see org.apache.commons.math.stat.Univariate#getMin()
*/
public double getMin() {
double[] v = internalValues();
if (v != null) {
return min.evaluate(v, this.start(), this.size());
}
return min.getResult();
return apply(min);
}
/**
* @see org.apache.commons.math.stat.Univariate#getGeometricMean()
*/
public double getGeometricMean() {
double[] v = internalValues();
if (v != null) {
return geoMean.evaluate(v, this.start(), this.size());
}
return geoMean.getResult();
return apply(geoMean);
}
/**
* Generates a text report displaying
* univariate statistics from values that

View File

@ -54,11 +54,13 @@
package org.apache.commons.math.stat;
import java.util.List;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.DefaultTransformer;
import org.apache.commons.math.util.NumberTransformer;
/**
* @version $Revision: 1.3 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.4 $ $Date: 2003/07/15 03:45:10 $
*/
public class ListUnivariateImpl
extends AbstractStoreUnivariate
@ -182,27 +184,19 @@ public class ListUnivariateImpl
super.clear();
list.clear();
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
/* (non-Javadoc)
* @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
protected double[] internalValues() {
return getValues();
}
public double apply(UnivariateStatistic stat) {
double[] v = this.getValues();
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#start()
*/
protected int start() {
return 0;
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#size()
*/
protected int size() {
return getN();
if (v != null) {
return stat.evaluate(v, 0, v.length);
}
return Double.NaN;
}
/**
* @return
*/

View File

@ -53,10 +53,11 @@
*/
package org.apache.commons.math.stat;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.ContractableDoubleArray;
/**
* @version $Revision: 1.4 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:45:10 $
*/
public class StoreUnivariateImpl extends AbstractStoreUnivariate {
@ -143,24 +144,13 @@ public class StoreUnivariateImpl extends AbstractStoreUnivariate {
}
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
/* (non-Javadoc)
* @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
protected double[] internalValues() {
return eDA.getValues();
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#start()
*/
protected int start() {
return eDA.start();
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#size()
*/
protected int size() {
return eDA.getNumElements();
public double apply(UnivariateStatistic stat) {
if (eDA != null) {
return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements());
}
return Double.NaN;
}
}

View File

@ -54,6 +54,9 @@
package org.apache.commons.math.stat;
import java.io.Serializable;
import org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.FixedDoubleArray;
/**
@ -64,7 +67,7 @@ import org.apache.commons.math.util.FixedDoubleArray;
* Integers, floats and longs can be added, but they will be converted
* to doubles by addValue().
*
* @version $Revision: 1.18 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.19 $ $Date: 2003/07/15 03:45:10 $
*/
public class UnivariateImpl
extends AbstractUnivariate
@ -120,7 +123,7 @@ public class UnivariateImpl
sumsq.increment(value);
sumLog.increment(value);
geoMean.increment(value);
moment.increment(value);
//mean.increment(value);
//variance.increment(value);
@ -158,25 +161,18 @@ public class UnivariateImpl
}
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
/* (non-Javadoc)
* @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
protected double[] internalValues() {
return storage == null ? null : storage.getValues();
}
public double apply(UnivariateStatistic stat) {
if (storage != null) {
return stat.evaluate(storage.getValues(), storage.start(), storage.getNumElements());
} else if (stat instanceof StorelessUnivariateStatistic) {
return ((StorelessUnivariateStatistic) stat).getResult();
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#start()
*/
protected int start() {
return storage.start();
}
/**
* @see org.apache.commons.math.stat.AbstractUnivariate#size()
*/
protected int size() {
return storage.getNumElements();
return Double.NaN;
}
}