commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat UnivariateImpl.java AbstractStoreUnivariate.java ListUnivariateImpl.java AbstractUnivariate.java StoreUnivariateImpl.java
Date Tue, 15 Jul 2003 03:45:10 GMT
mdiggory    2003/07/14 20:45:10

  Modified:    math/src/java/org/apache/commons/math/stat
                        UnivariateImpl.java AbstractStoreUnivariate.java
                        ListUnivariateImpl.java AbstractUnivariate.java
                        StoreUnivariateImpl.java
  Log:
  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.
  
  Revision  Changes    Path
  1.19      +15 -19    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java
  
  Index: UnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- UnivariateImpl.java	9 Jul 2003 21:45:23 -0000	1.18
  +++ UnivariateImpl.java	15 Jul 2003 03:45:10 -0000	1.19
  @@ -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;
   
   /**
  @@ -120,7 +123,7 @@
               sumsq.increment(value);
               sumLog.increment(value);
               geoMean.increment(value);
  -            
  +
               moment.increment(value);
               //mean.increment(value);
               //variance.increment(value);
  @@ -158,25 +161,18 @@
           }
       }
   
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
  -     */
  -    protected double[] internalValues() {
  -        return storage == null ? null : storage.getValues();
  -    }
  -
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#start()
  +    /* (non-Javadoc)
  +     * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
        */
  -    protected int start() {
  -        return storage.start();
  -    }
  +    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#size()
  -     */
  -    protected int size() {
  -        return storage.getNumElements();
  +        return Double.NaN;
       }
   
   }
  
  
  
  1.10      +2 -2      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java
  
  Index: AbstractStoreUnivariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AbstractStoreUnivariate.java	9 Jul 2003 21:45:23 -0000	1.9
  +++ AbstractStoreUnivariate.java	15 Jul 2003 03:45:10 -0000	1.10
  @@ -88,7 +88,7 @@
        */
       public double getPercentile(double p) {
           percentile.setPercentile(p);
  -        return percentile.evaluate(this.getValues(), this.start(), this.size());
  +        return apply(percentile);
       }
       
       /**
  
  
  
  1.4       +13 -19    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java
  
  Index: ListUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ListUnivariateImpl.java	9 Jul 2003 21:45:23 -0000	1.3
  +++ ListUnivariateImpl.java	15 Jul 2003 03:45:10 -0000	1.4
  @@ -54,6 +54,8 @@
   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;
   
  @@ -182,27 +184,19 @@
           super.clear();
           list.clear();
       }
  -
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
  -     */
  -    protected double[] internalValues() {
  -        return getValues();
  -    }
  -
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#start()
  +    
  +    /* (non-Javadoc)
  +     * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
        */
  -    protected int start() {
  -        return 0;
  -    }
  +    public double apply(UnivariateStatistic stat) {
  +        double[] v = this.getValues();
   
  -    /**
  -     * @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
        */
  
  
  
  1.2       +14 -73    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractUnivariate.java
  
  Index: AbstractUnivariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractUnivariate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractUnivariate.java	9 Jul 2003 21:45:23 -0000	1.1
  +++ AbstractUnivariate.java	15 Jul 2003 03:45:10 -0000	1.2
  @@ -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;
  @@ -139,23 +140,8 @@
           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 @@
        * @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 @@
        *         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 @@
        *         <= 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 @@
        *         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 @@
        * @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
  
  
  
  1.5       +9 -19     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java
  
  Index: StoreUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- StoreUnivariateImpl.java	9 Jul 2003 21:45:23 -0000	1.4
  +++ StoreUnivariateImpl.java	15 Jul 2003 03:45:10 -0000	1.5
  @@ -53,6 +53,7 @@
    */
   package org.apache.commons.math.stat;
   
  +import org.apache.commons.math.stat.univariate.UnivariateStatistic;
   import org.apache.commons.math.util.ContractableDoubleArray;
   
   /**
  @@ -143,24 +144,13 @@
           }
       }
   
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
  -     */
  -    protected double[] internalValues() {
  -        return eDA.getValues();
  -    }
  -
  -    /**
  -     * @see org.apache.commons.math.stat.AbstractUnivariate#start()
  +    /* (non-Javadoc)
  +     * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
        */
  -    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;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message