hep.aida.tfloat.bin.StaticFloatBin1D Maven / Gradle / Ivy
Show all versions of parallelcolt Show documentation
package hep.aida.tfloat.bin;
import cern.colt.list.tfloat.FloatArrayList;
import cern.jet.stat.tfloat.FloatDescriptive;
/**
* 1-dimensional non-rebinnable bin consuming float elements;
* Efficiently computes basic statistics of data sequences. First see the package summary and javadoc tree view to get the broad picture.
*
* The data streamed into a SimpleBin1D is not preserved! As a
* consequence infinitely many elements can be added to this bin. As a further
* consequence this bin cannot compute more than basic statistics. It is also
* not rebinnable. If these drawbacks matter, consider to use a
* {@link DynamicFloatBin1D}, which overcomes them at the expense of increased
* memory requirements.
*
* This class is fully thread safe (all public methods are synchronized). Thus,
* you can have one or more threads adding to the bin as well as one or more
* threads reading and viewing the statistics of the bin while it is
* filled. For high performance, add data in large chunks (buffers) via
* method addAllOf rather than piecewise via method add.
*
* Implementation: Incremental maintainance. Performance linear in the
* number of elements added.
*
* @author [email protected]
* @version 0.9, 03-Jul-99
*/
public class StaticFloatBin1D extends AbstractFloatBin1D {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The number of elements consumed by incremental parameter maintainance.
*/
protected int size = 0;
// cached parameters
protected float min = 0.0f; // Min( x[i] )
protected float max = 0.0f; // Max( x[i] )
protected float sum = 0.0f; // Sum( x[i] )
protected float sum_xx = 0.0f; // Sum( x[i]*x[i] )
/**
* Function arguments used by method addAllOf(...) For memory tuning only.
* Avoids allocating a new array of arguments each time addAllOf(...) is
* called.
*
* Each bin does not need its own set of argument vars since they are
* declared as "static". addAllOf(...) of this class uses only 4 entries.
* Subclasses computing additional incremental statistics may need more
* arguments. So, to be on the safe side we allocate space for 20 args. Be
* sure you access this arguments only in synchronized blocks like
* synchronized (arguments) { do it }
*
* By the way, the whole fuss would be unnecessary if Java would know INOUT
* parameters (call by reference).
*/
static transient protected float[] arguments = new float[20];
/**
* Constructs and returns an empty bin.
*/
public StaticFloatBin1D() {
clear();
}
/**
* Adds the specified element to the receiver.
*
* @param element
* element to be appended.
*/
public synchronized void add(float element) {
// prototyping implementation; inefficient; TODO
this.addAllOf(new FloatArrayList(new float[] { element }));
/*
* sumSquares += element * element; if (this.done == 0) { // initial
* setup this.min = element; this.max = element; } else { if (element <
* this.min) this.min = element; if (element > this.max) this.max =
* element;
*
* float oldMean = this.mean; this.mean += (element -
* this.mean)/(done+1); this.sumsq +=
* (element-this.mean)*(element-oldMean); // cool, huh? } this.done++;
*/
}
/**
* Adds the part of the specified list between indexes from
* (inclusive) and to (inclusive) to the receiver.
*
* @param list
* the list of which elements shall be added.
* @param from
* the index of the first element to be added (inclusive).
* @param to
* the index of the last element to be added (inclusive).
* @throws IndexOutOfBoundsException
* if
* list.size()>0 && (from<0 || from>to || to>=list.size())
* .
*/
public synchronized void addAllOfFromTo(FloatArrayList list, int from, int to) {
// if (this.arguments == null) setUpCache();
synchronized (arguments) {
// prepare arguments
arguments[0] = this.min;
arguments[1] = this.max;
arguments[2] = this.sum;
arguments[3] = this.sum_xx;
FloatDescriptive.incrementalUpdate(list, from, to, arguments);
// store the new parameters back
this.min = arguments[0];
this.max = arguments[1];
this.sum = arguments[2];
this.sum_xx = arguments[3];
this.size += to - from + 1;
}
}
/**
* Removes all elements from the receiver. The receiver will be empty after
* this call returns.
*/
public synchronized void clear() {
clearAllMeasures();
this.size = 0;
}
/**
* Resets the values of all measures.
*/
protected void clearAllMeasures() {
this.min = Float.POSITIVE_INFINITY;
this.max = Float.NEGATIVE_INFINITY;
this.sum = 0.0f;
this.sum_xx = 0.0f;
}
/**
* Returns false. Returns whether a client can obtain all elements
* added to the receiver. In other words, tells whether the receiver
* internally preserves all added elements. If the receiver is rebinnable,
* the elements can be obtained via elements() methods.
*
*/
public synchronized boolean isRebinnable() {
return false;
}
/**
* Returns the maximum.
*/
public synchronized float max() {
return this.max;
}
/**
* Returns the minimum.
*/
public synchronized float min() {
return this.min;
}
/**
* Returns the number of elements contained in the receiver.
*
* @return the number of elements contained in the receiver.
*/
public synchronized int size() {
return this.size;
}
/**
* Returns the sum of all elements, which is Sum( x[i] ).
*/
public synchronized float sum() {
return this.sum;
}
/**
* Returns the sum of squares, which is Sum( x[i] * x[i] ).
*/
public synchronized float sumOfSquares() {
return this.sum_xx;
}
}