public final class Sum
extends java.lang.Object
implements java.io.Serializable
If there are no values in the dataset, then 0 is returned.
If any of the values are
NaN
, then NaN
is returned.
Note that this implementation is not synchronized. If
multiple threads access an instance of this class concurrently, and at least
one of the threads invokes the increment()
or
clear()
method, it must be synchronized externally.
Constructor and Description |
---|
Sum()
Create a Sum instance
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the internal state of the Statistic
|
static boolean |
equals(double x,
double y,
int maxUlps)
Returns true if both arguments are equal or within the range of allowed
error (inclusive).
|
boolean |
equals(java.lang.Object object)
Returns true iff
object is an
AbstractStorelessUnivariateStatistic returning the same
values as this for getResult() and getN() |
double |
evaluate()
Returns the result of evaluating the statistic over the stored data.
|
double |
evaluate(double[] values)
This default implementation calls
clear() , then invokes
increment(double) in a loop over the the input array, and then uses
getResult() to compute the return value. |
double |
evaluate(double[] values,
double[] weights)
The weighted sum of the entries in the the input array.
|
double |
evaluate(double[] values,
double[] weights,
int begin,
int length)
The weighted sum of the entries in the specified portion of
the input array, or 0 if the designated subarray
is empty.
|
double |
evaluate(double[] values,
int begin,
int length)
The sum of the entries in the specified portion of
the input array, or 0 if the designated subarray
is empty.
|
double[] |
getData()
Get a copy of the stored data array.
|
protected double[] |
getDataRef()
Get a reference to the stored data array.
|
long |
getN() |
double |
getResult()
Returns the current value of the Statistic.
|
int |
hashCode()
Returns hash code based on getResult() and getN()
|
void |
increment(double d)
Updates the internal state of the statistic to reflect the addition of the new value.
|
void |
incrementAll(double[] values)
This default implementation just calls
increment(double) in a loop over
the input array. |
void |
incrementAll(double[] values,
int begin,
int length)
This default implementation just calls
increment(double) in a loop over
the specified portion of the input array. |
void |
setData(double[] values)
Set the data array.
|
void |
setData(double[] values,
int begin,
int length)
Set the data array.
|
protected boolean |
test(double[] values,
double[] weights,
int begin,
int length)
This method is used by
evaluate(double[], double[], int, int) methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero. |
protected boolean |
test(double[] values,
double[] weights,
int begin,
int length,
boolean allowEmpty)
This method is used by
evaluate(double[], double[], int, int) methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero. |
protected boolean |
test(double[] values,
int begin,
int length)
This method is used by
evaluate(double[], int, int) methods
to verify that the input parameters designate a subarray of positive length. |
protected boolean |
test(double[] values,
int begin,
int length,
boolean allowEmpty)
This method is used by
evaluate(double[], int, int) methods
to verify that the input parameters designate a subarray of positive length. |
static boolean |
verifyValues(double[] values,
double[] weights,
int begin,
int length,
boolean allowEmpty)
This method is used
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
|
static boolean |
verifyValues(double[] values,
int begin,
int length,
boolean allowEmpty) |
public void increment(double d)
d
- the new value.public double getResult()
Double.NaN
if it
has been cleared or just instantiated.public long getN()
public void clear()
public double evaluate(double[] values, int begin, int length)
Throws MathIllegalArgumentException
if the array is null.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includejava.lang.NullPointerException
- if the array is nulljava.lang.IllegalStateException
- if the array index
parameters are not validpublic double evaluate(double[] values, double[] weights, int begin, int length)
Throws MathIllegalArgumentException
if any of the following are true:
Uses the formula,
weighted sum = Σ(values[i] * weights[i])
values
- the input arrayweights
- the weights arraybegin
- index of the first array element to includelength
- the number of elements to includejava.lang.IllegalStateException
- if the parameters are not validpublic double evaluate(double[] values, double[] weights)
Throws MathIllegalArgumentException
if any of the following are true:
Uses the formula,
weighted sum = Σ(values[i] * weights[i])
values
- the input arrayweights
- the weights arraypublic double evaluate(double[] values)
clear()
, then invokes
increment(double)
in a loop over the the input array, and then uses
getResult()
to compute the return value.
Note that this implementation changes the internal state of the
statistic. Its side effects are the same as invoking clear()
and
then incrementAll(double[])
.
Implementations may override this method with a more efficient and possibly more accurate implementation that works directly with the input array.
If the array is null, a MathIllegalArgumentException is thrown.
values
- input arrayjava.lang.IllegalStateException
- if values is nullpublic void incrementAll(double[] values)
increment(double)
in a loop over
the input array.
Throws IllegalArgumentException if the input values array is null.
values
- values to addjava.lang.NullPointerException
- if values is nullpublic void incrementAll(double[] values, int begin, int length)
increment(double)
in a loop over
the specified portion of the input array.
Throws IllegalArgumentException if the input values array is null.
values
- array holding values to addbegin
- index of the first array element to addlength
- number of array elements to addjava.lang.NullPointerException
- if values is nullpublic boolean equals(java.lang.Object object)
object
is an
AbstractStorelessUnivariateStatistic
returning the same
values as this for getResult()
and getN()
equals
in class java.lang.Object
object
- object to test equality against.public int hashCode()
hashCode
in class java.lang.Object
public static boolean equals(double x, double y, int maxUlps)
Two float numbers are considered equal if there are (maxUlps - 1)
(or fewer) floating point numbers between them, i.e. two adjacent
floating point numbers are considered equal.
Adapted from Bruce Dawson
x
- first valuey
- second valuemaxUlps
- is the number of floating point
values between
and
.public void setData(double[] values)
The stored value is a copy of the parameter array, not the array itself.
values
- data array to store (may be null to remove stored data)evaluate()
public double[] getData()
protected double[] getDataRef()
public void setData(double[] values, int begin, int length)
values
- data array to storebegin
- the index of the first element to includelength
- the number of elements to include
are not validevaluate()
public double evaluate()
The stored array is the one which was set by previous calls to setData(double[])
.
java.lang.IllegalStateException
- if the stored data array is nullprotected boolean test(double[] values, int begin, int length)
evaluate(double[], int, int)
methods
to verify that the input parameters designate a subarray of positive length.
true
iff the parameters designate a subarray of
positive lengthMathIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeprotected boolean test(double[] values, int begin, int length, boolean allowEmpty)
evaluate(double[], int, int)
methods
to verify that the input parameters designate a subarray of positive length.
true
iff the parameters designate a subarray of
non-negative lengthIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0 unless allowEmpty
is true
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeallowEmpty
- if true
then zero length arrays are allowedjava.lang.NullPointerException
- or IllegalStateException
if the indices are invalid or the array is nullprotected boolean test(double[] values, double[] weights, int begin, int length)
evaluate(double[], double[], int, int)
methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
true
iff the parameters designate a subarray of
positive length and the weights array contains legitimate values.IllegalArgumentException
if any of the following are true:
false
length
is 0.
values
- the input arrayweights
- the weights arraybegin
- index of the first array element to includelength
- the number of elements to includejava.lang.NullPointerException
- or java.lang.IllegalStateException if
the indices are invalid or the array is nullpublic static boolean verifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty)
true
iff the parameters designate a subarray of
non-negative length and the weights array contains legitimate values.MathIllegalArgumentException
if any of the following are true:
false
length
is 0 unless allowEmpty
is true
.
values
- the input array.weights
- the weights array.begin
- index of the first array element to include.length
- the number of elements to include.allowEmpty
- if true
than allow zero length arrays to pass.true
if the parameters are valid.java.lang.NullPointerException
- if either of the arrays are nulljava.lang.IllegalStateException
- if the array indices are not valid,
the weights array contains NaN, infinite or negative elements, or there
are no positive weights.protected boolean test(double[] values, double[] weights, int begin, int length, boolean allowEmpty)
evaluate(double[], double[], int, int)
methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
true
iff the parameters designate a subarray of
non-negative length and the weights array contains legitimate values.MathIllegalArgumentException
if any of the following are true:
false
length
is 0 unless allowEmpty
is true
.
values
- the input array.weights
- the weights array.begin
- index of the first array element to include.length
- the number of elements to include.allowEmpty
- if true
than allow zero length arrays to pass.true
if the parameters are valid.java.lang.NullPointerException
- if either of the arrays are nulljava.lang.IllegalStateException
- if the array indices are not valid,
the weights array contains NaN, infinite or negative elements, or there
are no positive weights.public static boolean verifyValues(double[] values, int begin, int length, boolean allowEmpty)