commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tobr...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math DoubleArray.java ExpandableDoubleArray.java ListUnivariateImpl.java StoreUnivariateImpl.java Univariate.java UnivariateImpl.java
Date Fri, 16 May 2003 05:23:29 GMT
tobrien     2003/05/15 22:23:29

  Modified:    math/src/java/org/apache/commons/math DoubleArray.java
                        ExpandableDoubleArray.java ListUnivariateImpl.java
                        StoreUnivariateImpl.java Univariate.java
                        UnivariateImpl.java
  Log:
  The Univariate interface now contains getWindow and setWindow in
  addition to a constant which signifies an "infinite" window.  Windowing
  has been added to all three Univariate implementations:
  
  * UnivariateImpl - If the window is not infinite, we keep track of
  0..n elements and discount the contribution of the discarded element when
  our "window" is moved.  If the window is infinite no extra storage is used
  beyond an empty ContractableDoubleArray.
  
  - In the following two implementations, the window size can be changed at anytime.
  
  * ListStoreUnivariateImpl - If the window is not infinite, this
  implementation only takes into account the last n elements of the List.
  
  * StoreUnivariateImpl - Uses an internal ContractableDoubleArray, window size
  can be changed at any time.
  
  Revision  Changes    Path
  1.2       +10 -0     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/DoubleArray.java
  
  Index: DoubleArray.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/DoubleArray.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DoubleArray.java	16 May 2003 04:23:05 -0000	1.1
  +++ DoubleArray.java	16 May 2003 05:23:28 -0000	1.2
  @@ -120,4 +120,14 @@
   	 * @param i number of elements to discard from the front of the array.
   	 */
   	public abstract void discardFrontElements(int i);
  +	
  +	/**
  +	 * Returns the minimum value stored in this array
  +	 */
  +	public abstract double getMin();
  +
  +	/**
  +	 * Returns the maximum value stored in this array
  +	 */
  +	public abstract double getMax();
   }
  
  
  
  1.6       +30 -0     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ExpandableDoubleArray.java
  
  Index: ExpandableDoubleArray.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ExpandableDoubleArray.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ExpandableDoubleArray.java	16 May 2003 04:23:05 -0000	1.5
  +++ ExpandableDoubleArray.java	16 May 2003 05:23:29 -0000	1.6
  @@ -359,4 +359,34 @@
   		return elementArray;
   	}
   
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.DoubleArray#getMax()
  +	 */
  +	public double getMax() {
  +		double max = internalArray[startIndex];
  +		
  +		for( int i = startIndex + 1; i < numElements; i++) {
  +			if( internalArray[i] > max ) {
  +				max = internalArray[i];
  +			}
  +		}
  +
  +		return max;
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.DoubleArray#getMin()
  +	 */
  +	public double getMin() {
  +		double min = internalArray[startIndex];
  +		
  +		for( int i = startIndex + 1; i < numElements; i++) {
  +			if( internalArray[i] < min ) {
  +				min = internalArray[i];
  +			}
  +		}
  +
  +		return min;
  +	}
  +
   }
  
  
  
  1.2       +59 -5     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ListUnivariateImpl.java
  
  Index: ListUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ListUnivariateImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ListUnivariateImpl.java	15 May 2003 06:33:19 -0000	1.1
  +++ ListUnivariateImpl.java	16 May 2003 05:23:29 -0000	1.2
  @@ -61,6 +61,10 @@
    */
   public class ListUnivariateImpl extends AbstractStoreUnivariate {
   
  +	// Holds the value of the windowSize, initial windowSize is the constant
  +	// Univariate.INFINITE_WINDOW
  +	private int windowSize = Univariate.INIFINTE_WINDOW;
  +
   	// Holds a reference to a list - GENERICs are going to make
   	// out lives easier here as we could only accept List<Number>
   	List list;
  @@ -75,10 +79,23 @@
   	 */
   	public double[] getValues() {
   
  -		double[] copiedArray = new double[list.size()];
  +		// If we've got a windowSize, we might not care about the entire list.
  +		List effectiveList = list;
  +		
  +		// If the window size is not INIFINITE_WINDOW AND
  +		// the current list is larger that the window size, we need to
  +		// take into account only the last n elements of the list
  +		// as definied by windowSize
  +		if( windowSize != Univariate.INIFINTE_WINDOW &&
  +			windowSize < list.size() ) {
  +			effectiveList = list.subList( (list.size() - 1) - windowSize, (list.size()-1));
  +		}
  +
  +		// Create an array to hold all values
  +		double[] copiedArray = new double[effectiveList.size()];
   
   		int i = 0;
  -		Iterator it = list.iterator();
  +		Iterator it = effectiveList.iterator();
   		while( it.hasNext() ) {
   			Number n = (Number) it.next();
   			copiedArray[i] = n.doubleValue();
  @@ -92,15 +109,38 @@
   	 * @see org.apache.commons.math.StoreUnivariate#getElement(int)
   	 */
   	public double getElement(int index) {
  -		Number n = (Number) list.get(index);
  -		return n.doubleValue();
  +		
  +		double value = Double.NaN;
  +
  +		if( windowSize != Univariate.INIFINTE_WINDOW &&
  +			 windowSize < list.size() ) {
  +			Number n = (Number) list.get( ( (list.size() - 1) - windowSize ) + index ) ;
  +			value = n.doubleValue();
  +		} else {
  +			Number n = (Number) list.get(index);
  +			value = n.doubleValue();
  +		}
  +		
  +		return value;
   	}
   	
   	/* (non-Javadoc)
   	 * @see org.apache.commons.math.Univariate#getN()
   	 */
   	public double getN() {
  -		return list.size();
  +		double N = 0.0;
  +		
  +		if( windowSize != Univariate.INIFINTE_WINDOW ) {
  +			if( list.size() > windowSize ) {
  +				N = windowSize;
  +			} else {
  +				N = list.size();
  +			}
  +		} else {
  +			N = list.size();
  +		}
  +		
  +		return N;
   	}
   	
   	/* (non-Javadoc)
  @@ -115,6 +155,20 @@
   	 */
   	public void clear() {
   		list.clear();
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#getWindowSize()
  +	 */
  +	public int getWindowSize() {
  +		return windowSize;
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#setWindowSize(int)
  +	 */
  +	public void setWindowSize(int windowSize) {
  +		this.windowSize = windowSize;
   	}
   
   }
  
  
  
  1.4       +45 -6     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariateImpl.java
  
  Index: StoreUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/StoreUnivariateImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StoreUnivariateImpl.java	15 May 2003 15:38:48 -0000	1.3
  +++ StoreUnivariateImpl.java	16 May 2003 05:23:29 -0000	1.4
  @@ -58,10 +58,16 @@
    */
   public class StoreUnivariateImpl extends AbstractStoreUnivariate {
   
  -	ExpandableDoubleArray eDA;
  +	// Use an internal double array
  +	DoubleArray eDA;
  +	
  +	// Store the windowSize
  +	private int windowSize = Univariate.INIFINTE_WINDOW;
   
   	public StoreUnivariateImpl() {
  -		eDA = new ExpandableDoubleArray();
  +		// A contractable double array is used.  memory is reclaimed when
  +		// the storage of the array becomes too empty.
  +		eDA = new ContractableDoubleArray();
   	}
   
   	/* (non-Javadoc)
  @@ -70,7 +76,7 @@
   	public double[] getValues() {
   
   		double[] copiedArray = new double[ eDA.getNumElements() ];
  -		System.arraycopy( eDA.getValues(), 0, copiedArray, 0, eDA.getNumElements());
  +		System.arraycopy( eDA.getElements(), 0, copiedArray, 0, eDA.getNumElements());
   		return copiedArray;
   	}
   
  @@ -91,15 +97,48 @@
   	/* (non-Javadoc)
   	 * @see org.apache.commons.math.Univariate#addValue(double)
   	 */
  -	public void addValue(double v) {
  -		eDA.addElement( v );
  +	public synchronized void addValue(double v) {
  +		if( windowSize != Univariate.INIFINTE_WINDOW ) {
  +			if( getN() == windowSize ) {
  +				eDA.addElementRolling( v );
  +			} else if( getN() < windowSize ) {
  +				eDA.addElement(v);
  +			} else {
  +				throw new RuntimeException( "A window Univariate had more element than " +
  +					"the windowSize.  This is an inconsistent state.");
  +			}
  +		} else {
  +			eDA.addElement(v);
  +		}
   	}
   
   	/* (non-Javadoc)
   	 * @see org.apache.commons.math.Univariate#clear()
   	 */
  -	public void clear() {
  +	public synchronized void clear() {
   		eDA.clear();
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#getWindowSize()
  +	 */
  +	public int getWindowSize() {
  +		return windowSize;
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#setWindowSize(int)
  +	 */
  +	public synchronized void setWindowSize(int windowSize) {
  +		this.windowSize = windowSize;
  +		
  +		// We need to check to see if we need to discard elements
  +		// from the front of the array.  If the windowSize is less than 
  +		// the current number of elements.
  +		if( windowSize < eDA.getNumElements() ) {
  +			eDA.discardFrontElements( eDA.getNumElements() - windowSize);
  +		}
  +		
   	}
   
   }
  
  
  
  1.3       +23 -1     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java
  
  Index: Univariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Univariate.java	15 May 2003 05:39:00 -0000	1.2
  +++ Univariate.java	16 May 2003 05:23:29 -0000	1.3
  @@ -117,4 +117,26 @@
   
   	/** Resets all sums to 0, resets min and max */
   	public abstract void clear();
  +	
  +	/**
  +	 * This constant signals that a Univariate implementation
  +	 * takes into account the contributions of an infinite number of
  +	 * elements.  In other words, if getWindow returns this
  +	 * constant, there is, in effect, no "window".
  +	 */
  +	public static final int INIFINTE_WINDOW = -1;
  +
  +	/**
  +	 * Univariate has the ability to return only measures for the
  +	 * last N elements added to the set of values.  This function returns
  +	 */
  +	public abstract int getWindowSize();
  +	
  +	/**
  +	 * Sets the window.  windowSize controls the number of value
  +	 * which contribute to the values returned by Univariate.  
  +	 * For example, a window value of 10 means that getMean()
  +	 * will return the mean of the last 10 values added.
  +	 */
  +	public abstract void setWindowSize(int windowSize);
   }
  
  
  
  1.3       +58 -6     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java
  
  Index: UnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UnivariateImpl.java	15 May 2003 06:33:19 -0000	1.2
  +++ UnivariateImpl.java	16 May 2003 05:23:29 -0000	1.3
  @@ -69,6 +69,15 @@
   */
   public class UnivariateImpl implements Univariate, Serializable {
   
  +	/** hold the window size **/
  +	private int windowSize = Univariate.INIFINTE_WINDOW;
  +	
  +	/** Just in case, the windowSize is not inifinite, we need to
  +	 *   keep an array to remember values 0 to N
  +	 */
  +	private DoubleArray doubleArray =
  +		new ContractableDoubleArray(); 
  +
       /** running sum of values that have been added */
       private double sum = 0.0;
   
  @@ -140,11 +149,40 @@
        * @param v the value to be added 
        */
       private void insertValue(double v) {
  -        n += 1.0;
  -        if (v < min) min = v;
  -        if (v > max) max = v;
  -        sum += v;
  -        sumsq += v*v;
  +    	
  +    	if( windowSize != Univariate.INIFINTE_WINDOW ) {
  +    		if( windowSize == n ) {
  +				double discarded = doubleArray.addElementRolling( v );        	
  +			
  +				// Remove the influence of the discarded
  +				sum -= discarded;
  +				sumsq -= discarded * discarded;
  +			
  +				// Include the influence of the new
  +				// TODO: The next two lines seems rather expensive, but
  +				// I don't see many alternatives.			 
  +				min = doubleArray.getMin();
  +				max = doubleArray.getMax();
  +				sum += v;
  +				sumsq += v*v;
  +    		} else {
  +				doubleArray.addElement( v );        	
  +	        	n += 1.0;
  +    	    	if (v < min) min = v;
  +       			if (v > max) max = v;
  +        		sum += v;
  +        		sumsq += v*v;
  +    		}
  +    	} else {
  +			// If the windowSize is inifinite please don't take the time to
  +			// worry about storing any values.  We don't need to discard the
  +			// influence of any single item.
  +			n += 1.0;
  +			if (v < min) min = v;
  +			if (v > max) max = v;
  +			sum += v;
  +			sumsq += v*v;
  +    	}
       }
   
       /** Getter for property max.
  @@ -214,5 +252,19 @@
           this.min = Double.MAX_VALUE;
           this.max = Double.MIN_VALUE;
       }
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#getWindowSize()
  +	 */
  +	public int getWindowSize() {
  +		return windowSize;
  +	}
  +
  +	/* (non-Javadoc)
  +	 * @see org.apache.commons.math.Univariate#setWindowSize(int)
  +	 */
  +	public void setWindowSize(int windowSize) {
  +		this.windowSize = windowSize;
  +	}
   
   }
  
  
  

---------------------------------------------------------------------
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