Updated source code to reflect Jakarta source code guidelines specifically

tab characters.  Changes were driven by Checkstyle report on
http://jakarta.apache.org/commons/sandbox/math


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140843 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2003-05-20 18:15:29 +00:00
parent a008ed1316
commit 437e275a50
10 changed files with 1276 additions and 1206 deletions

View File

@ -60,193 +60,210 @@ package org.apache.commons.math;
*/
public abstract class AbstractStoreUnivariate implements StoreUnivariate {
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getMode()
*/
public double getMode() {
// Mode depends on a refactor Freq class
throw new UnsupportedOperationException("getMode() is not yet implemented");
}
/**
* Returns the most frequently occuring value
* @see org.apache.commons.math.StoreUnivariate#getMode()
*/
public double getMode() {
// Mode depends on a refactor Freq class
String msg = "getMode() is not yet implemented";
throw new UnsupportedOperationException(msg);
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getSkewness()
*/
public double getSkewness() {
// Initialize the skewness
double skewness = Double.NaN;
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
/**
* Returns the skewness of this collection of values
* @see org.apache.commons.math.StoreUnivariate#getSkewness()
*/
public double getSkewness() {
// Initialize the skewness
double skewness = Double.NaN;
// Sum the cubes of the distance from the mean divided by the standard deviation
double accum = 0.0;
for( int i = 0; i < getN(); i++ ) {
accum += Math.pow( (getElement(i) - mean) / stdDev, 3.0);
}
// Get N
double n = getN();
// Calculate skewness
skewness = ( n / ( (n-1) * (n-2) ) ) * accum;
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
return skewness;
}
// Sum the cubes of the distance from the mean divided by the
// standard deviation
double accum = 0.0;
for (int i = 0; i < getN(); i++) {
accum += Math.pow((getElement(i) - mean) / stdDev, 3.0);
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getKurtosis()
*/
public double getKurtosis() {
// Initialize the kurtosis
double kurtosis = Double.NaN;
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
// Get N
double n = getN();
// Sum the ^4 of the distance from the mean divided by the standard deviation
double accum = 0.0;
for( int i = 0; i < getN(); i++ ) {
accum += Math.pow( (getElement(i) - mean) / stdDev, 4.0);
}
// Get N
double n = getN();
double coefficientOne = ( n * (n+1)) / ( (n-1) * (n-2) * (n-3) );
double termTwo = ( ( 3 * Math.pow( n - 1, 2.0)) / ( (n-2) * (n-3) ) );
// Calculate kurtosis
kurtosis = ( coefficientOne * accum ) - termTwo;
// Calculate skewness
skewness = (n / ((n - 1) * (n - 2))) * accum;
return kurtosis;
}
return skewness;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getKurtosisClass()
*/
public int getKurtosisClass() {
/**
* Returns the kurtosis for this collection of values
* @see org.apache.commons.math.StoreUnivariate#getKurtosis()
*/
public double getKurtosis() {
// Initialize the kurtosis
double kurtosis = Double.NaN;
int kClass = StoreUnivariate.MESOKURTIC;
double kurtosis = getKurtosis();
if( kurtosis > 0 ) {
kClass = StoreUnivariate.LEPTOKURTIC;
} else if( kurtosis < 0 ) {
kClass = StoreUnivariate.PLATYKURTIC;
}
return( kClass );
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
}
// Sum the ^4 of the distance from the mean divided by the
// standard deviation
double accum = 0.0;
for (int i = 0; i < getN(); i++) {
accum += Math.pow((getElement(i) - mean) / stdDev, 4.0);
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMean()
*/
public double getMean() {
double arithMean = getSum() / getN();
return arithMean;
}
// Get N
double n = getN();
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getVariance()
*/
public double getVariance() {
// Initialize variance
double variance = Double.NaN;
double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
double termTwo = ((3 * Math.pow(n - 1, 2.0))
/ ((n - 2) * (n - 3)));
// Calculate kurtosis
kurtosis = (coefficientOne * accum) - termTwo;
if( getN() == 1 ) {
// If this is a single value
variance = 0;
} else if( getN() > 1 ) {
// Get the mean
double mean = getMean();
return kurtosis;
}
// Calculate the sum of the squares of the distance between each value and the mean
double accum = 0.0;
for( int i = 0; i < getN(); i++ ){
accum += Math.pow( (getElement(i) - mean), 2.0 );
}
// Divide the accumulator by N - Hmmm... unbiased or biased?
variance = accum / (getN() - 1);
}
return variance;
}
/**
* Returns the type or class of kurtosis that this collection of
* values exhibits
* @see org.apache.commons.math.StoreUnivariate#getKurtosisClass()
*/
public int getKurtosisClass() {
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getStandardDeviation()
*/
public double getStandardDeviation() {
double stdDev = Double.NaN;
if( getN() != 0 ) {
stdDev = Math.sqrt( getVariance() );
}
return( stdDev );
}
int kClass = StoreUnivariate.MESOKURTIC;
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMax()
*/
public double getMax() {
// Initialize maximum to NaN
double max = Double.NaN;
for( int i = 0; i < getN(); i++) {
if( i == 0 ) {
max = getElement(i);
} else {
if( getElement(i) > max ) {
max = getElement(i);
}
}
}
double kurtosis = getKurtosis();
if (kurtosis > 0) {
kClass = StoreUnivariate.LEPTOKURTIC;
} else if (kurtosis < 0) {
kClass = StoreUnivariate.PLATYKURTIC;
}
return max;
}
return (kClass);
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMin()
*/
public double getMin() {
// Initialize minimum to NaN
double min = Double.NaN;
for( int i = 0; i < getN(); i++) {
if( i == 0 ) {
min = getElement(i);
} else {
if( getElement(i) < min ) {
min = getElement(i);
}
}
}
}
return min;
}
/**
* Returns the mean for this collection of values
* @see org.apache.commons.math.Univariate#getMean()
*/
public double getMean() {
double arithMean = getSum() / getN();
return arithMean;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getSum()
*/
public double getSum() {
double accum = 0.0;
for( int i = 0; i < getN(); i++) {
accum += getElement(i);
}
return accum;
}
/**
* Returns the variance for this collection of values
* @see org.apache.commons.math.Univariate#getVariance()
*/
public double getVariance() {
// Initialize variance
double variance = Double.NaN;
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getSumsq()
*/
public double getSumsq() {
double accum = 0.0;
for( int i = 0; i < getN(); i++) {
accum += Math.pow(getElement(i), 2.0);
}
return accum;
}
if (getN() == 1) {
// If this is a single value
variance = 0;
} else if (getN() > 1) {
// Get the mean
double mean = getMean();
// Calculate the sum of the squares of the distance between each
// value and the mean
double accum = 0.0;
for (int i = 0; i < getN(); i++){
accum += Math.pow((getElement(i) - mean), 2.0);
}
// Divide the accumulator by N - Hmmm... unbiased or biased?
variance = accum / (getN() - 1);
}
return variance;
}
/**
* Returns the standard deviation for this collection of values
* @see org.apache.commons.math.Univariate#getStandardDeviation()
*/
public double getStandardDeviation() {
double stdDev = Double.NaN;
if (getN() != 0) {
stdDev = Math.sqrt(getVariance());
}
return (stdDev);
}
/**
* Returns the maximum value contained herein.
* @see org.apache.commons.math.Univariate#getMax()
*/
public double getMax() {
// Initialize maximum to NaN
double max = Double.NaN;
for (int i = 0; i < getN(); i++) {
if (i == 0) {
max = getElement(i);
} else {
if (getElement(i) > max) {
max = getElement(i);
}
}
}
return max;
}
/**
* Returns the minimum value contained herein
* @see org.apache.commons.math.Univariate#getMin()
*/
public double getMin() {
// Initialize minimum to NaN
double min = Double.NaN;
for (int i = 0; i < getN(); i++) {
if (i == 0) {
min = getElement(i);
} else {
if (getElement(i) < min) {
min = getElement(i);
}
}
}
return min;
}
/**
* Returns the sum of all values contained herein
* @see org.apache.commons.math.Univariate#getSum()
*/
public double getSum() {
double accum = 0.0;
for (int i = 0; i < getN(); i++) {
accum += getElement(i);
}
return accum;
}
/**
* Returns the sun of the squares of all values contained herein
* @see org.apache.commons.math.Univariate#getSumsq()
*/
public double getSumsq() {
double accum = 0.0;
for (int i = 0; i < getN(); i++) {
accum += Math.pow(getElement(i), 2.0);
}
return accum;
}
}

View File

@ -60,194 +60,212 @@ import java.io.Serializable;
*
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
*/
public class ContractableDoubleArray extends ExpandableDoubleArray implements Serializable {
public class ContractableDoubleArray
extends ExpandableDoubleArray
implements Serializable {
// The contraction criteria is related to the expansion factor. Since this array is allowed to contract
//
protected float contractionCriteria = 2.5f;
// The contraction criteria defines the conditions under which this
// object will "resize" the internal array to the number of elements
// contained in the element array + 1
protected float contractionCriteria = 2.5f;
/**
* Create an expandable double array with the
* default initial capactiy of 16, an expansion factor of 2.00, and a contractionCriteria of 2.5
*/
public ContractableDoubleArray() {
super();
}
/**
* Create an expandable double array with the default initial capacity of
* 16, an expansion factor of 2.00, and a contractionCriteria of 2.5
*/
public ContractableDoubleArray() {
super();
}
/**
* Create an expandable double array with the
* specified initial capacity, the defult expansion factor of 2.00, and a contractionCriteria of 2.5
*
* @param initialCapacity The initial size of the internal storage array
*/
public ContractableDoubleArray(int initialCapacity) {
super( initialCapacity );
}
/**
* Create an expandable double array with the specified initial capacity,
* the defult expansion factor of 2.00, and a contractionCriteria of 2.5
*
* @param initialCapacity The initial size of the internal storage array
*/
public ContractableDoubleArray(int initialCapacity) {
super(initialCapacity);
}
/**
* Create an expandable double array with the
* specificed initial capacity and expand factor, with a contractionCriteria of 2.5
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this parameter
*/
public ContractableDoubleArray(int initialCapacity, float expansionFactor) {
this.expansionFactor = expansionFactor;
setInitialCapacity( initialCapacity );
internalArray = new double[initialCapacity];
checkContractExpand(getContractionCriteria(), expansionFactor);
}
/**
* Create an expandable double array with the specificed initial capacity
* and expand factor, with a contractionCriteria of 2.5
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this
* parameter
*/
public ContractableDoubleArray(int initialCapacity,
float expansionFactor) {
this.expansionFactor = expansionFactor;
setInitialCapacity(initialCapacity);
internalArray = new double[initialCapacity];
checkContractExpand(getContractionCriteria(), expansionFactor);
}
/**
* Create an expandable double array with the
* specificed initial capacity, expand factor, and contractionCriteria
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this parameter
*/
public ContractableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) {
this.contractionCriteria = contractionCriteria;
this.expansionFactor = expansionFactor;
setInitialCapacity( initialCapacity );
internalArray = new double[initialCapacity];
checkContractExpand(contractionCriteria, expansionFactor);
}
/**
* Create an expandable double array with the
* specificed initial capacity, expand factor, and contractionCriteria
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this
* parameter
*/
public ContractableDoubleArray(int initialCapacity,
float expansionFactor,
float contractionCriteria) {
this.contractionCriteria = contractionCriteria;
this.expansionFactor = expansionFactor;
setInitialCapacity(initialCapacity);
internalArray = new double[initialCapacity];
checkContractExpand(contractionCriteria, expansionFactor);
}
/**
* Contracts the storage array to the (size of the element set) + 1 - to avoid a zero length array.
* This function also resets the startIndex to zero
*/
public synchronized void contract() {
double[] tempArray = new double[numElements + 1];
/**
* Contracts the storage array to the (size of the element set) + 1 - to
* avoid a zero length array. This function also resets the startIndex to
* zero.
*/
public synchronized void contract() {
double[] tempArray = new double[numElements + 1];
// Copy and swap - copy only the element array from the src array.
System.arraycopy(internalArray,startIndex,tempArray,0,numElements);
internalArray = tempArray;
// Reset the start index to zero
startIndex = 0;
}
// Copy and swap - copy only the element array from the src array.
System.arraycopy(internalArray,startIndex,tempArray,0,numElements);
internalArray = tempArray;
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized void addElement(double value) {
super.addElement( value );
if( shouldContract() ) {
contract();
}
}
// Reset the start index to zero
startIndex = 0;
}
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized double addElementRolling(double value) {
double discarded = super.addElementRolling(value);
// Check the contraction criteria
if( shouldContract() ) {
contract();
}
return discarded;
}
/**
* Should contract returns true if the ratio of (internal storage length) to (number of elements)
* is larger than the contractionCriteria value. In other words, using the default value
* of 2.5, if the internal storage array provides more than 2.5x the space needed to store
* numElements, then this function returns true
*
* @return true if array satisfies the contraction criteria
*/
private synchronized boolean shouldContract() {
boolean shouldContract = false;
if( ( internalArray.length / numElements ) > contractionCriteria ) {
shouldContract = true;
}
return shouldContract;
}
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized void addElement(double value) {
super.addElement(value);
if (shouldContract()) {
contract();
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#setElement(int, double)
*/
public synchronized void setElement(int index, double value) {
super.setElement(index, value);
if( shouldContract() ) {
contract();
}
}
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized double addElementRolling(double value) {
double discarded = super.addElementRolling(value);
// Check the contraction criteria
if (shouldContract()) {
contract();
}
return discarded;
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#setExpansionFactor(float)
*/
public void setExpansionFactor(float expansionFactor) {
checkContractExpand(getContractionCriteria(), expansionFactor);
super.setExpansionFactor(expansionFactor);
}
/**
* Should contract returns true if the ratio of (internal storage length)
* to (number of elements) is larger than the contractionCriteria value.
* In other words, using the default value of 2.5, if the internal storage
* array provides more than 2.5x the space needed to store numElements,
* then this function returns true
*
* @return true if array satisfies the contraction criteria
*/
private synchronized boolean shouldContract() {
boolean shouldContract = false;
if ((internalArray.length / numElements) > contractionCriteria) {
shouldContract = true;
}
return shouldContract;
}
/**
* The contraction criteria defines when the internal array will contract to store only the
* number of elements in the element array. This contractionCriteria gaurantees that
* the internal storage array will never exceed this factor more than the space needed
* to store numElements.
*
* @return the contraction criteria used to reclaim memory when array is empty
*/
public float getContractionCriteria() {
return contractionCriteria;
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#setElement(int, double)
*/
public synchronized void setElement(int index, double value) {
super.setElement(index, value);
if (shouldContract()) {
contract();
}
}
/**
* Sets the contraction criteria for this ExpandContractDoubleArray.
*
* @param new contraction criteria
*/
public void setContractionCriteria(float contractionCriteria) {
checkContractExpand( contractionCriteria, getExpansionFactor() );
this.contractionCriteria = contractionCriteria;
}
/**
* Checks the expansion factor and the contraction criteria and throws an IllegalArgumentException
* if the contractionCriteria is less than the expansionCriteria
*
* @param expansionFactor
* @param contractionCriteria
*/
protected void checkContractExpand( float contractionCritera, float expansionFactor ) {
if( contractionCritera < expansionFactor ) {
throw new IllegalArgumentException( "Contraction criteria can never be smaller than " + "the expansion factor. This would lead to a never ending loop of expansion and " + "contraction as a newly expanded internal storage array would immediately " + "satisfy the criteria for contraction");
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#setExpansionFactor(float)
*/
public void setExpansionFactor(float expansionFactor) {
checkContractExpand(getContractionCriteria(), expansionFactor);
super.setExpansionFactor(expansionFactor);
}
if( contractionCriteria <= 1.0 ) {
throw new IllegalArgumentException( "The contraction criteria must be a number larger than" +
" one. If the contractionCriteria is less than or equal to one an endless loop of contraction " +
"and expansion would ensue as an internalArray.length == numElements would satisfy " +
"the contraction criteria");
}
if (expansionFactor < 1.0) {
throw new IllegalArgumentException(
"The expansion factor must be a number greater than" + "1.0");
}
/**
* The contraction criteria defines when the internal array will contract
* to store only the number of elements in the element array. This
* contractionCriteria gaurantees that the internal storage array will
* never exceed this factor more than the space needed to store
* numElements.
*
* @return the contraction criteria used to reclaim memory when array is
* empty
*/
public float getContractionCriteria() {
return contractionCriteria;
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#discardFrontElements(int)
*/
public synchronized void discardFrontElements(int i) {
super.discardFrontElements(i);
if( shouldContract() ) {
contract();
}
/**
* Sets the contraction criteria for this ExpandContractDoubleArray.
*
* @param new contraction criteria
*/
public void setContractionCriteria(float contractionCriteria) {
checkContractExpand(contractionCriteria, getExpansionFactor());
}
this.contractionCriteria = contractionCriteria;
}
/**
* Checks the expansion factor and the contraction criteria and throws an
* IllegalArgumentException if the contractionCriteria is less than the
* expansionCriteria
*
* @param expansionFactor
* @param contractionCriteria
*/
protected void checkContractExpand(float contractionCritera,
float expansionFactor) {
if (contractionCritera < expansionFactor) {
String msg = "Contraction criteria can never be smaller than " +
"the expansion factor. This would lead to a never ending " +
"loop of expansion and contraction as a newly expanded " +
"internal storage array would immediately satisfy the " +
"criteria for contraction";
throw new IllegalArgumentException(msg);
}
if (contractionCriteria <= 1.0) {
String msg = "The contraction criteria must be a number larger " +
"than one. If the contractionCriteria is less than or " +
"equal to one an endless loop of contraction and expansion " +
"would ensue as an internalArray.length == numElements " +
"would satisfy the contraction criteria";
throw new IllegalArgumentException(msg);
}
if (expansionFactor < 1.0) {
String msg = "The expansion factor must be a number greater " +
"than 1.0";
throw new IllegalArgumentException(msg);
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.ExpandableDoubleArray#discardFrontElements(int)
*/
public synchronized void discardFrontElements(int i) {
super.discardFrontElements(i);
if (shouldContract()) {
contract();
}
}
}

View File

@ -63,71 +63,86 @@ import java.util.NoSuchElementException;
*/
public interface DoubleArray {
/**
* Returns the number of elements currently in the array. Please note
* that this is different from the length of the internal storage array.
* @return number of elements
*/
public abstract int getNumElements();
/**
* Returns the number of elements currently in the array. Please note
* that this is different from the length of the internal storage array.
* @return number of elements
*/
int getNumElements();
/**
* Returns the element at the specified index
*
* @param index index to fetch a value from
* @return value stored at the specified index
*/
public abstract double getElement(int index) throws NoSuchElementException;
//TODO: Throwing a NoSuchElementException might not be the right
//thing to do, it may be more helpful to just throw ArrayOutOfBounds...
/**
* Sets the element at the specified index. This method will expand the internal storage array to
* accomodate the insertion of a value at an index beyond the current capacity.
* @param index index to store a value in
* @param value value to store at the specified index
*/
public abstract void setElement(int index, double value);
/**
* Returns the element at the specified index
*
* @param index index to fetch a value from
* @return value stored at the specified index
* @throws NoSuchElementException exception thrown if the array index
* exceeds the known boundaries of this array.
*
*/
double getElement(int index) throws NoSuchElementException;
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public abstract void addElement(double value);
/**
* Sets the element at the specified index. This method will expand the
* internal storage array to accomodate the insertion of a value at an
* index beyond the current capacity.
* @param index index to store a value in
* @param value value to store at the specified index
*/
void setElement(int index, double value);
/**
* Adds an element and moves the window of elements up one. This
* has the effect of a FIFO. when you "roll" the array an element is removed
* from the array. The return value of this function is the discarded double.
*
* @return the value which has been discarded or "pushed" out of the array
* by this rolling insert.
*/
public abstract double addElementRolling(double value);
/**
* Returns a double[] of elements
*/
public abstract double[] getElements();
/**
* Clear the double array
*/
public abstract void clear();
/**
* Adds an element to the end of this expandable array
*
* @param value to be added to end of array
*/
void addElement(double value);
/**
* Discards values from the front of the list. This function removes n elements from
* the front of the array.
*
* @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();
/**
* Adds an element and moves the window of elements up one. This
* has the effect of a FIFO. when you "roll" the array an element is
* removed from the array. The return value of this function is the
* discarded double.
*
* @param value the value to be added to the array
* @return the value which has been discarded or "pushed" out of the array
* by this rolling insert.
*/
double addElementRolling(double value);
/**
* Returns the maximum value stored in this array
*/
public abstract double getMax();
}
/**
* Returns a double[] of elements
*
* @return all elements added to the array
*/
double[] getElements();
/**
* Clear the double array
*/
void clear();
/**
* Discards values from the front of the list. This function removes n
* elements from the front of the array.
*
*@param i number of elements to discard from the front of the array.
*/
void discardFrontElements(int i);
/**
* Returns the minimum value stored in this array
*
* @return minimum value contained in this array
*/
double getMin();
/**
* Returns the maximum value stored in this array
*
* @return maximum value contained in this array
*/
double getMax();
}

View File

@ -63,330 +63,342 @@ import java.util.NoSuchElementException;
*/
public class ExpandableDoubleArray implements Serializable, DoubleArray {
// This is the internal storage array.
protected double[] internalArray;
// This is the internal storage array.
protected double[] internalArray;
// Number of elements in the array
protected int numElements = 0;
// Number of elements in the array
protected int numElements = 0;
// Keeps track of a starting index
protected int startIndex = 0;
// The initial capacity of the array.
// Initial capacity is not exposed as a property as it is only meaningful
// when passed to a constructor.
protected int initialCapacity = 16;
// The expand factor of the array. When the array need to be expanded,
// the new array size will be internalArray.length * expandFactor
protected float expansionFactor = 2.0f;
/**
* Create an expandable double array with the
* default initial capactiy of 16 and an expansion factor of 2.00
*/
public ExpandableDoubleArray() {
internalArray = new double[initialCapacity];
}
/**
* Create an expandable double array with the
* specified initial capacity and the defult expansion factor of 2.00
*
* @param initialCapacity The initial size of the internal storage array
*/
public ExpandableDoubleArray(int initialCapacity) {
setInitialCapacity(initialCapacity);
internalArray = new double[this.initialCapacity];
}
/**
* Create an expandable double array with the
* specificed initial capacity and expand factor.
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this
* parameter
*/
public ExpandableDoubleArray(int initialCapacity, float expansionFactor) {
setInitialCapacity(initialCapacity);
setExpansionFactor(expansionFactor);
this.initialCapacity = initialCapacity;
internalArray = new double[initialCapacity];
}
/**
* The expansion factor controls the size of a new aray when an array
* needs to be expanded. When a value is inserted into a full array, the
* new array size is calculated as the current array size times this
* expansion factor. The default expansion factor is 2.0
*
* @return the expansion factor of this expandable double array
*/
public float getExpansionFactor() {
return expansionFactor;
}
/**
* Sets the expansion factor for this expandable double array.
* The expansion factor will affect the next expansion of this array.
*
* @param expansionFactor the expansion factor of this array
*/
public void setExpansionFactor(float expansionFactor) {
// The expansion factor *must* be larger than 1.0, otherwise we'll
// have an inconsistency upon expansion we'll start shrinking which
// will lead to ArrayOutOfBound exceptions.
if (expansionFactor > 1.0) {
this.expansionFactor = expansionFactor;
} else {
String msg = "The expansion factor must be a number greater " +
"than 1.0";
throw new IllegalArgumentException(msg);
}
}
/**
* Sets the initial capacity
*
* @param initialCapacity
*/
public void setInitialCapacity(int initialCapacity) {
if (initialCapacity > 0) {
this.initialCapacity = initialCapacity;
} else {
String msg = "The initial capacity supplied: " + initialCapacity +
"must be a positive integer";
throw new IllegalArgumentException(msg);
}
}
/**
* Returns the internal storage array
*
* @return the internal storage array used by this object
*/
protected double[] getValues() {
return (internalArray);
}
/**
* Returns the number of elements currently in the array. Please note
* that this is different from the length of the internal storage array.
* @return number of elements
*/
public int getNumElements() {
return (numElements);
}
/**
* This function allows you to control the number of elements contained
* in this array, and can be used to "throw" out the last n values in an
* array. This feature is mainly targetted at the subclasses of this
* array class. Note that this function will also expand the internal
* array as needed.
*
* @param a new number of elements
*/
public synchronized void setNumElements(int i) {
// If index is negative thrown an error
if (i < 0) {
String msg = "Number of elements must be zero or a positive " +
"integer";
throw new IllegalArgumentException(msg);
}
// Test the new num elements, check to see if the array needs to be
// expanded to accomodate this new number of elements
if ((startIndex + i) > internalArray.length) {
expandTo(startIndex + i);
}
// Set the new number of elements to new value
numElements = i;
}
/**
* Returns the element at the specified index
*
* @param index index to fetch a value from
* @return value stored at the specified index
*/
public double getElement(int index) throws NoSuchElementException {
double value = Double.NaN;
if (index >= numElements) {
String msg = "The index specified: " + index +
" is larger than the current number of elements";
throw new NoSuchElementException(msg);
} else if (index >= 0) {
value = internalArray[startIndex + index];
} else {
String msg = "Elements cannot be retrieved from a negative " +
"array index";
throw new IllegalArgumentException(msg);
}
return value;
}
/**
* Sets the element at the specified index. This method will expand the
* internal storage array to accomodate the insertion of a value at an
* index beyond the current capacity.
*
* @param index index to store a value in
* @param value value to store at the specified index
*/
public synchronized void setElement(int index, double value) {
if (index < 0) {
String msg = "Cannot set an element at a negative index";
throw new IllegalArgumentException(msg);
}
if ((startIndex + index) >= internalArray.length) {
expandTo(startIndex + (index + 1));
numElements = index + 1;
}
internalArray[startIndex + index] = value;
}
/**
* Expands the internal storage array to the specified size.
*
* @param size Size of the new internal storage array
*/
private synchronized void expandTo(int size) {
double[] tempArray = new double[size];
// Copy and swap
System.arraycopy(internalArray,0,tempArray,0,internalArray.length);
internalArray = tempArray;
}
/**
* Expands the internal storage array using the expansion factor
*/
protected synchronized void expand() {
// notice the use of Math.ceil(), this gaurantees that we will always
// have an array of at least currentSize + 1. Assume that the
// current initial capacity is 1 and the expansion factor
// is 1.000000000000000001. The newly calculated size will be
// rounded up to 2 after the multiplication is performed.
int newSize = (int) Math.ceil(internalArray.length * expansionFactor);
double[] tempArray = new double[newSize];
// Copy and swap
System.arraycopy(internalArray, 0, tempArray, 0, internalArray.length);
internalArray = tempArray;
}
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized void addElement(double value) {
numElements++;
if ((startIndex + numElements) > internalArray.length) {
expand();
}
internalArray[startIndex + (numElements - 1)] = value;
}
/**
* Adds an element and moves the window of elements up one. This
* has the effect of a FIFO. when you "roll" the array an element is
* removed from the array. The return value of this function is the
* discarded double.
*
* @return the value which has been discarded or "pushed" out of the array
* by this rolling insert.
*/
public synchronized double addElementRolling(double value) {
double discarded = internalArray[startIndex];
if ((startIndex + (numElements+1)) > internalArray.length) {
expand();
}
// Increment the start index
startIndex += 1;
// Add the new value
internalArray[startIndex + (numElements - 1)] = value;
return discarded;
}
/**
* Notice the package scope on this method. This method is simply here
* for the JUnit test, it allows us check if the expansion is working
* properly after a number of expansions. This is not meant to be a part
* of the public interface of this class.
*
* @return the length of the internal storage array.
*/
int getInternalLength() {
return (internalArray.length);
}
// Keeps track of a starting index
protected int startIndex = 0;
/**
* Clear the array, reset the size to the initialCapacity and the number
* of elements to zero.
*/
public synchronized void clear() {
numElements = 0;
internalArray = new double[initialCapacity];
}
// The initial capacity of the array.
// Initial capacity is not exposed as a property as it is only meaningful
// when passed to a constructor.
protected int initialCapacity = 16;
/**
* Discards values from the front of the list. This function removes n
* elements from the front of the array.
*
* @param i number of elements to discard from the front of the array.
*/
public synchronized void discardFrontElements(int i) {
// The expand factor of the array. When the array need to be expanded, the new array size
// will be internalArray.length * expandFactor
protected float expansionFactor = 2.0f;
if (i > numElements) {
String msg = "Cannot discard more elements than are" +
"contained in this array.";
throw new IllegalArgumentException(msg);
} else if (i < 0) {
String msg = "Cannot discard a negative number of elements.";
throw new IllegalArgumentException(msg);
} else {
// "Subtract" this number of discarded from numElements
numElements -= i;
startIndex += i;
}
}
/**
* Create an expandable double array with the
* default initial capactiy of 16 and an expansion factor of 2.00
*/
public ExpandableDoubleArray() {
internalArray = new double[initialCapacity];
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElements()
*/
public double[] getElements() {
double[] elementArray = new double[numElements];
System.arraycopy(internalArray, startIndex,
elementArray, 0, numElements);
return elementArray;
}
/**
* Create an expandable double array with the
* specified initial capacity and the defult expansion factor of 2.00
*
* @param initialCapacity The initial size of the internal storage array
*/
public ExpandableDoubleArray(int initialCapacity) {
setInitialCapacity(initialCapacity);
internalArray = new double[this.initialCapacity];
}
/**
* Create an expandable double array with the
* specificed initial capacity and expand factor.
*
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this parameter
*/
public ExpandableDoubleArray(int initialCapacity, float expansionFactor) {
setInitialCapacity( initialCapacity );
setExpansionFactor(expansionFactor);
this.initialCapacity = initialCapacity;
internalArray = new double[initialCapacity];
}
/**
* The expansion factor controls the size of a new aray when an array needs to be expanded.
* When a value is inserted into a full array, the new array size is calculated as the
* current array size times this expansion factor. The default expansion factor is 2.0
*
* @return the expansion factor of this expandable double array
*/
public float getExpansionFactor() {
return expansionFactor;
}
/**
* Sets the expansion factor for this expandable double array. The expansion factor will
* affect the next expansion of this array.
*
* @param expansionFactor the expansion factor of this array
*/
public void setExpansionFactor(float expansionFactor) {
// The expansion factor *must* be larger than 1.0, otherwise we'll have an inconsistency
// upon expansion we'll start shrinking which will lead to ArrayOutOfBound exceptions.
if (expansionFactor > 1.0) {
this.expansionFactor = expansionFactor;
} else {
throw new IllegalArgumentException(
"The expansion factor must be a number greater than" + "1.0");
}
}
/**
* Sets the initial capacity
*
* @param initialCapacity
*/
public void setInitialCapacity(int initialCapacity) {
if (initialCapacity > 0) {
this.initialCapacity = initialCapacity;
} else {
throw new IllegalArgumentException(
"The initial capacity supplied: "
+ initialCapacity
+ "must be a positive integer");
}
}
/**
* Returns the internal storage array
*
* @return the internal storage array used by this object
*/
protected double[] getValues() {
return (internalArray);
}
/**
* Returns the number of elements currently in the array. Please note
* that this is different from the length of the internal storage array.
* @return number of elements
*/
public int getNumElements() {
return (numElements);
}
/**
* This function allows you to control the number of elements contained in this
* array, and can be used to "throw" out the last n values in an array. This
* feature is mainly targetted at the subclasses of this array class. Note
* that this function will also expand the internal array as needed.
*
* @param a new number of elements
*/
public synchronized void setNumElements(int i) {
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getMax()
*/
public double getMax() {
double max = internalArray[startIndex];
// If index is negative thrown an error
if( i < 0 ) {
throw new IllegalArgumentException( "Number of elements must be zero or a positive integer");
}
// Test the new num elements, check to see if the array needs to be expanded to
// accomodate this new number of elements
if( (startIndex + i) > internalArray.length ) {
expandTo( startIndex + i );
}
// Set the new number of elements to new value
numElements = i;
}
for (int i = startIndex + 1; i < (numElements + startIndex); i++) {
if (internalArray[i] > max) {
max = internalArray[i];
}
}
return max;
}
/**
* Returns the element at the specified index
*
* @param index index to fetch a value from
* @return value stored at the specified index
*/
public double getElement(int index) throws NoSuchElementException {
double value = Double.NaN;
if (index >= numElements) {
throw new NoSuchElementException(
"The index specified: "
+ index
+ " is larger than the "
+ "current number of elements");
} else if (index >= 0) {
value = internalArray[startIndex + index];
} else {
throw new IllegalArgumentException(
"Elements cannot be retrieved from a negative array index");
}
return value;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getMin()
*/
public double getMin() {
double min = internalArray[startIndex];
/**
* Sets the element at the specified index. This method will expand the internal storage array to
* accomodate the insertion of a value at an index beyond the current capacity.
* @param index index to store a value in
* @param value value to store at the specified index
*/
public synchronized void setElement(int index, double value) {
if( index < 0 ) {
throw new IllegalArgumentException( "Cannot set an element at a negative index");
}
if ( (startIndex + index) >= internalArray.length) {
expandTo( startIndex + (index + 1));
numElements = index + 1;
}
internalArray[startIndex + index] = value;
}
/**
* Expands the internal storage array to the specified size.
*
* @param size Size of the new internal storage array
*/
private synchronized void expandTo(int size) {
double[] tempArray = new double[size];
// Copy and swap
System.arraycopy(internalArray,0,tempArray,0,internalArray.length);
internalArray = tempArray;
}
/**
* Expands the internal storage array using the expansion factor
*/
protected synchronized void expand() {
// notice the use of Math.ceil(), this gaurantees that we will always have an array of at least
// currentSize + 1. Assume that the current initial capacity is 1 and the expansion factor
// is 1.000000000000000001. The newly calculated size will be rounded up to 2 after
// the multiplication is performed.
int newSize = (int) Math.ceil(internalArray.length * expansionFactor);
double[] tempArray =
new double[newSize];
// Copy and swap
System.arraycopy(internalArray, 0, tempArray, 0, internalArray.length);
internalArray = tempArray;
}
/**
* Adds an element to the end of this expandable array
*
* @return value to be added to end of array
*/
public synchronized void addElement(double value) {
numElements++;
if ( (startIndex + numElements) > internalArray.length) {
expand();
}
internalArray[startIndex + (numElements - 1)] = value;
}
/**
* Adds an element and moves the window of elements up one. This
* has the effect of a FIFO. when you "roll" the array an element is removed
* from the array. The return value of this function is the discarded double.
*
* @return the value which has been discarded or "pushed" out of the array
* by this rolling insert.
*/
public synchronized double addElementRolling(double value) {
double discarded = internalArray[startIndex];
if ( (startIndex + (numElements+1) ) > internalArray.length) {
expand();
}
// Increment the start index
startIndex += 1;
// Add the new value
internalArray[startIndex + (numElements - 1)] = value;
return discarded;
}
/**
* Notice the package scope on this method. This method is simply here for the JUnit
* test, it allows us check if the expansion is working properly after a number of expansions. This
* is not meant to be a part of the public interface of this class.
*
* @return the length of the internal storage array.
*/
int getInternalLength() {
return (internalArray.length);
}
/**
* Clear the array, reset the size to the initialCapacity and the number of elements to zero
*/
public synchronized void clear() {
numElements = 0;
internalArray = new double[initialCapacity];
}
/**
* Discards values from the front of the list. This function removes n elements from
* the front of the array.
*
* @param i number of elements to discard from the front of the array.
*/
public synchronized void discardFrontElements(int i) {
if( i > numElements ) {
throw new IllegalArgumentException( "Cannot discard more elements than are" + "contained in this array.");
} else if( i < 0 ) {
throw new IllegalArgumentException( "Cannot discard a negative number" + " of elements.");
} else {
// "Subtract" this number of discarded from numElements
numElements -= i;
startIndex += i;
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElements()
*/
public double[] getElements() {
double[] elementArray = new double[numElements];
System.arraycopy(internalArray, startIndex, elementArray, 0, numElements);
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 + startIndex); 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 + startIndex); i++) {
if( internalArray[i] < min ) {
min = internalArray[i];
}
}
return min;
}
for (int i = startIndex + 1; i < (numElements + startIndex); i++) {
if (internalArray[i] < min) {
min = internalArray[i];
}
}
return min;
}
}

View File

@ -65,135 +65,140 @@ import java.util.NoSuchElementException;
*/
public class FixedDoubleArray implements DoubleArray {
double[] internalArray;
int size = 0;
int nextAdd = 0;
int maxElements = 0;
double[] internalArray;
public FixedDoubleArray(int maxElements) {
this.maxElements = maxElements;
internalArray = new double[maxElements];
}
int size = 0;
int nextAdd = 0;
int maxElements = 0;
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getNumElements()
*/
public int getNumElements() {
return size;
}
public FixedDoubleArray(int maxElements) {
this.maxElements = maxElements;
internalArray = new double[maxElements];
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElement(int)
*/
public double getElement(int index) throws NoSuchElementException {
if( index > (size-1) ) {
throw new ArrayIndexOutOfBoundsException("Attempted to retrieve an element outside of" + "the element array");
} else {
return internalArray[index];
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getNumElements()
*/
public int getNumElements() {
return size;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElement(int)
*/
public double getElement(int index) throws NoSuchElementException {
if (index > (size-1)) {
String msg = "Attempted to retrieve an element outside of " +
"the element array";
throw new ArrayIndexOutOfBoundsException(msg);
} else {
return internalArray[index];
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#setElement(int, double)
*/
public void setElement(int index, double value) {
if (index > (size-1)) {
String msg = "Attempted to set an element outside of" +
"the element array";
throw new ArrayIndexOutOfBoundsException(msg);
} else {
internalArray[index] = value;
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#addElement(double)
*/
public void addElement(double value) {
if (size < internalArray.length) {
size++;
internalArray[nextAdd] = value;
nextAdd++;
nextAdd = nextAdd % (maxElements);
} else {
String msg = "Attempted to add a value to an array of fixed " +
"size, please use addElementRolling to avoid this exception";
throw new ArrayIndexOutOfBoundsException(msg);
}
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#setElement(int, double)
*/
public void setElement(int index, double value) {
if( index > (size-1) ) {
throw new ArrayIndexOutOfBoundsException("Attempted to set an element outside of" +
"the element array");
} else {
internalArray[index] = value;
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#addElementRolling(double)
*/
public double addElementRolling(double value) {
if (size < internalArray.length) {
size++;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#addElement(double)
*/
public void addElement(double value) {
if( size < internalArray.length ) {
size++;
internalArray[nextAdd] = value;
nextAdd++;
nextAdd = nextAdd % (maxElements);
double discarded = internalArray[nextAdd];
} else {
throw new ArrayIndexOutOfBoundsException("Attempted to add a value to an array of fixed size, please " + "use addElementRolling to avoid this exception");
}
}
internalArray[nextAdd] = value;
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#addElementRolling(double)
*/
public double addElementRolling(double value) {
if( size < internalArray.length ) {
size++;
}
double discarded = internalArray[nextAdd];
nextAdd++;
nextAdd = nextAdd % maxElements;
internalArray[nextAdd] = value;
// but we return the value which was "replaced"
return (discarded);
}
nextAdd++;
nextAdd = nextAdd % maxElements;
// but we return the value which was "replaced"
return( discarded );
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElements()
*/
public double[] getElements() {
double[] copy = new double[internalArray.length];
System.arraycopy(internalArray, 0, copy, 0, internalArray.length);
return copy;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getElements()
*/
public double[] getElements() {
double[] copy = new double[internalArray.length];
System.arraycopy(internalArray, 0, copy, 0, internalArray.length);
return copy;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#clear()
*/
public void clear() {
size = 0;
nextAdd = 0;
internalArray = new double[maxElements];
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#clear()
*/
public void clear() {
size = 0;
nextAdd = 0;
internalArray = new double[maxElements];
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#discardFrontElements(int)
*/
public void discardFrontElements(int i) {
// TODO: AH! implemented there is not concept of "front"
// in an array that discards values when rolling..... anyone?
String msg = "Discarding front element not supported in " +
"FixedDoubleArray";
throw new RuntimeException(msg);
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#discardFrontElements(int)
*/
public void discardFrontElements(int i) {
// TODO: AH! implemented there is not concept of "front"
// in an array that discards values when rolling..... anyone?
throw new RuntimeException("Discarding front element not supported in FixedDoubleArray");
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getMin()
*/
public double getMin() {
double min = internalArray[0];
for( int i = 1; i < size; i++) {
if( internalArray[i] < min ) {
min = internalArray[i];
}
}
return min;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getMax()
*/
public double getMax() {
double max = internalArray[0];
for( int i = 1; i < size; 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[0];
for (int i = 1; i < size; i++) {
if (internalArray[i] < min) {
min = internalArray[i];
}
}
return min;
}
/* (non-Javadoc)
* @see org.apache.commons.math.DoubleArray#getMax()
*/
public double getMax() {
double max = internalArray[0];
for (int i = 1; i < size; i++) {
if (internalArray[i] > max) {
max = internalArray[i];
}
}
return max;
}
}

View File

@ -61,114 +61,111 @@ import java.util.List;
*/
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 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;
// Holds a reference to a list - GENERICs are going to make
// out lives easier here as we could only accept List<Number>
List list;
public ListUnivariateImpl( List list ) {
this.list = list;
}
public ListUnivariateImpl(List list) {
this.list = list;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getValues()
*/
public double[] getValues() {
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getValues()
*/
public double[] getValues() {
// If we've got a windowSize, we might not care about the entire list.
List effectiveList = list;
// 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));
}
// 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()];
// Create an array to hold all values
double[] copiedArray = new double[effectiveList.size()];
int i = 0;
Iterator it = effectiveList.iterator();
while( it.hasNext() ) {
Number n = (Number) it.next();
copiedArray[i] = n.doubleValue();
i++;
}
int i = 0;
Iterator it = effectiveList.iterator();
while(it.hasNext()) {
Number n = (Number) it.next();
copiedArray[i] = n.doubleValue();
i++;
}
return copiedArray;
}
return copiedArray;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getElement(int)
*/
public double getElement(int index) {
double value = Double.NaN;
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getElement(int)
*/
public double getElement(int index) {
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() {
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)
* @see org.apache.commons.math.Univariate#addValue(double)
*/
public void addValue(double v) {
list.add( new Double(v));
}
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#clear()
*/
public void clear() {
list.clear();
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getN()
*/
public double getN() {
double N = 0.0;
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getWindowSize()
*/
public int getWindowSize() {
return windowSize;
}
if (windowSize != Univariate.INIFINTE_WINDOW) {
if (list.size() > windowSize) {
N = windowSize;
} else {
N = list.size();
}
} else {
N = list.size();
}
return N;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#setWindowSize(int)
*/
public void setWindowSize(int windowSize) {
this.windowSize = windowSize;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#addValue(double)
*/
public void addValue(double v) {
list.add(new Double(v));
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#clear()
*/
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;
}
}

View File

@ -63,64 +63,68 @@ package org.apache.commons.math;
*/
public interface StoreUnivariate extends Univariate {
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/
public static int LEPTOKURTIC = 1;
/**
* A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution
*/
public static int MESOKURTIC = 0;
/**
* A PLATYKURTIC set has a negative kurtosis (a flat "peak")
*/
public static int PLATYKURTIC = -1;
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/
public static int LEPTOKURTIC = 1;
/**
* Returns the mode of the values that have been added. The mode is
* the element which occurs with the most frequency
* @return the mode
*/
public abstract double getMode();
/**
* A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution
*/
public static int MESOKURTIC = 0;
/**
* Returns the skewness of a given distribution. Skewness is a measure of the
* assymetry of a given distribution.
*
* @return The skewness of this distribution
*/
public abstract double getSkewness();
/**
* A PLATYKURTIC set has a negative kurtosis (a flat "peak")
*/
public static int PLATYKURTIC = -1;
/**
* Kurtosis is a measure of the "peakedness" of a distribution
*
* @return the mode
*/
public abstract double getKurtosis();
/**
* Returns the Kurtosis "classification" a distribution can be leptokurtic (high peak), platykurtic (flat peak),
* or mesokurtic (zero kurtosis).
*
* @return A static constant defined in this interface, StoredDeviation.LEPTOKURITC,
* StoredDeviation.PLATYKURTIC, or StoredDeviation.MESOKURTIC
*/
public abstract int getKurtosisClass();
/**
* Returns the current set of values in an array of double primitives. The order of addition is preserved
*
* @return returns the current set of numbers in the order in which they were added to this set
*/
public abstract double[] getValues();
/**
* Returns the element at the specified index
*
* @return return the element at the specified index
*/
public abstract double getElement(int index);
/**
* Returns the mode of the values that have been added. The mode is
* the element which occurs with the most frequency
* @return the mode
*/
public abstract double getMode();
/**
* Returns the skewness of a given distribution. Skewness is a
* measure of the assymetry of a given distribution.
*
* @return The skewness of this distribution
*/
public abstract double getSkewness();
/**
* Kurtosis is a measure of the "peakedness" of a distribution
*
* @return the mode
*/
public abstract double getKurtosis();
/**
* Returns the Kurtosis "classification" a distribution can be
* leptokurtic (high peak), platykurtic (flat peak),
* or mesokurtic (zero kurtosis).
*
* @return A static constant defined in this interface,
* StoredDeviation.LEPTOKURITC, StoredDeviation.PLATYKURTIC, or
* StoredDeviation.MESOKURTIC
*/
public abstract int getKurtosisClass();
/**
* Returns the current set of values in an array of double primitives.
* The order of addition is preserved
*
* @return returns the current set of numbers in the order in which they
* were added to this set
*/
public abstract double[] getValues();
/**
* Returns the element at the specified index
*
* @return return the element at the specified index
*/
public abstract double getElement(int index);
}

View File

@ -58,87 +58,87 @@ package org.apache.commons.math;
*/
public class StoreUnivariateImpl extends AbstractStoreUnivariate {
// Use an internal double array
DoubleArray eDA;
// Store the windowSize
private int windowSize = Univariate.INIFINTE_WINDOW;
// Use an internal double array
DoubleArray eDA;
public StoreUnivariateImpl() {
// A contractable double array is used. memory is reclaimed when
// the storage of the array becomes too empty.
eDA = new ContractableDoubleArray();
}
// Store the windowSize
private int windowSize = Univariate.INIFINTE_WINDOW;
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getValues()
*/
public double[] getValues() {
public StoreUnivariateImpl() {
// A contractable double array is used. memory is reclaimed when
// the storage of the array becomes too empty.
eDA = new ContractableDoubleArray();
}
double[] copiedArray = new double[ eDA.getNumElements() ];
System.arraycopy( eDA.getElements(), 0, copiedArray, 0, eDA.getNumElements());
return copiedArray;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getValues()
*/
public double[] getValues() {
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getElement(int)
*/
public double getElement(int index) {
return eDA.getElement(index);
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getN()
*/
public double getN() {
return eDA.getNumElements();
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#addValue(double)
*/
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);
}
}
double[] copiedArray = new double[ eDA.getNumElements() ];
System.arraycopy( eDA.getElements(), 0,
copiedArray, 0, eDA.getNumElements());
return copiedArray;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#clear()
*/
public synchronized void clear() {
eDA.clear();
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getElement(int)
*/
public double getElement(int index) {
return eDA.getElement(index);
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getWindowSize()
*/
public int getWindowSize() {
return windowSize;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getN()
*/
public double getN() {
return eDA.getNumElements();
}
/* (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);
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#addValue(double)
*/
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 {
String msg = "A window Univariate had more element than " +
"the windowSize. This is an inconsistent state.";
throw new RuntimeException( msg );
}
} else {
eDA.addElement(v);
}
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#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);
}
}
}

View File

@ -61,82 +61,82 @@
*
* @author Phil Steitz
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
* @version $Revision: 1.3 $ $Date: 2003/05/16 05:23:29 $
* @version $Revision: 1.4 $ $Date: 2003/05/20 18:15:29 $
*
*/
public interface Univariate {
/**
* Adds the value to the set of numbers
* @param v the value to be added
*/
public abstract void addValue(double v);
/**
* Returns the mean of the values that have been added
* @return mean value
*/
public abstract double getMean();
/**
* Returns the variance of the values that have been added
* @return variance value
*/
public abstract double getVariance();
/**
* Adds the value to the set of numbers
* @param v the value to be added
*/
public abstract void addValue(double v);
/**
* Returns the standard deviation of the values that have been added
* @return standard deviation value
*/
public abstract double getStandardDeviation();
/**
* Returns the mean of the values that have been added
* @return mean value
*/
public abstract double getMean();
/** Getter for property max.
* @return Value of property max.
*/
public abstract double getMax();
/**
* Returns the variance of the values that have been added
* @return variance value
*/
public abstract double getVariance();
/** Getter for property min.
* @return Value of property min.
*/
public abstract double getMin();
/**
* Returns the standard deviation of the values that have been added
* @return standard deviation value
*/
public abstract double getStandardDeviation();
/** Getter for property n.
* @return Value of property n.
*/
public abstract double getN();
/** Getter for property max.
* @return Value of property max.
*/
public abstract double getMax();
/** Getter for property sum.
* @return Value of property sum.
*/
public abstract double getSum();
/** Getter for property min.
* @return Value of property min.
*/
public abstract double getMin();
/** Getter for property sumsq.
* @return Value of property sumsq.
*/
public abstract double getSumsq();
/** Getter for property n.
* @return Value of property n.
*/
public abstract double getN();
/** 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;
/** Getter for property sum.
* @return Value of property sum.
*/
public abstract double getSum();
/**
* 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);
}
/** Getter for property sumsq.
* @return Value of property sumsq.
*/
public abstract double getSumsq();
/** 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);
}

View File

@ -64,19 +64,19 @@ import java.io.Serializable;
* to doubles by addValue().
*
* @author Phil Steitz
* @version $Revision: 1.4 $ $Date: 2003/05/17 23:24:21 $
* @version $Revision: 1.5 $ $Date: 2003/05/20 18:15:29 $
*
*/
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;
/** 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;
/** running sum of values that have been added */
private double sum = 0.0;
@ -99,9 +99,9 @@ public class UnivariateImpl implements Univariate, Serializable {
/** Create a new univariate with a fixed window **/
public UnivariateImpl(int window) {
windowSize = window;
doubleArray = new FixedDoubleArray( window );
}
windowSize = window;
doubleArray = new FixedDoubleArray( window );
}
/**
* Adds the value, updating running sums.
@ -124,25 +124,26 @@ public class UnivariateImpl implements Univariate, Serializable {
/**
* Returns the variance of the values that have been added.
* @return The variance of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a single value set.
* an empty set of values and 0.0 is returned for a single value set.
*/
public double getVariance() {
double variance = Double.NaN;
if( n == 1 ) {
variance = 0.0;
} else if( n > 1 ) {
double xbar = getMean();
variance = (sumsq - xbar*xbar*n)/(n-1);
}
return variance;
double variance = Double.NaN;
if( n == 1 ) {
variance = 0.0;
} else if( n > 1 ) {
double xbar = getMean();
variance = (sumsq - xbar*xbar*n)/(n-1);
}
return variance;
}
/**
* Returns the standard deviation of the values that have been added
* @return The standard deviation of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a single value set.
* @return The standard deviation of a set of values. Double.NaN is
* returned for an empty set of values and 0.0 is returned for
* a single value set.
*/
public double getStandardDeviation() {
return (new Double(Math.sqrt
@ -154,45 +155,45 @@ public class UnivariateImpl implements Univariate, Serializable {
* @param v the value to be added
*/
private void insertValue(double v) {
if( windowSize != Univariate.INIFINTE_WINDOW ) {
if( windowSize == n ) {
double discarded = doubleArray.addElementRolling( v );
// Remove the influence of discarded value ONLY
// if the discard value has any meaning. In other words
// don't discount until we "roll".
if( windowSize > doubleArray.getNumElements() ) {
// 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;
}
if( windowSize != Univariate.INIFINTE_WINDOW ) {
if( windowSize == n ) {
double discarded = doubleArray.addElementRolling( v );
// Remove the influence of discarded value ONLY
// if the discard value has any meaning. In other words
// don't discount until we "roll".
if( windowSize > doubleArray.getNumElements() ) {
// 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.
@ -263,18 +264,19 @@ public class UnivariateImpl implements Univariate, Serializable {
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) {
throw new RuntimeException( "A fixed window size must be set via the UnivariateImpl constructor");
}
/* (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) {
String msg = "A fixed window size must be set via the " +
"UnivariateImpl constructor";
throw new RuntimeException( msg );
}
}