public final class Data extends Object
double-valued data.
Unless otherwise noted, the methods of this class:
NullPointerException when supplied with null
data.add(double, double...)).
IllegalArgumentException if they operate on all elements
of a dataset to yield a singular numeric or boolean value, but the supplied
dataset is empty, with some documented exceptions.
IllegalArguementException if they operate on multiple
datasets and the datasets are not the same size.Doubles.isFinite(double)). For
example, any method that operates on data containing Double.NaN or
infinite values will likely return NaNs or infinite values as a
result. See the Java Math for details on the behavior of individual
functions referenced herein.Buyer beware.
Many methods in this class are overloaded with Collection<Double>
arguments. Those overloaded methods that return a single result or
information about a supplied data set typically require a
Collection<Double> as an argument, whereas methods that transform
data in place require a List<Double> subtype.
For other useful Double utilities, see the Google Guava
Doubles class.
| Modifier and Type | Method and Description |
|---|---|
static double[] |
abs(double... data)
Set the elements of
data to their absolute value in place. |
static List<Double> |
abs(List<Double> data)
Set the elements of
data to their absolute value in place. |
static double[][][] |
add(double[][][] data1,
double[][][] data2)
Add the values of
data2 to data1 in place. |
static double[][] |
add(double[][] data1,
double[][] data2)
Add the values of
data2 to data1 in place. |
static double[] |
add(double[] data1,
double[] data2)
Add the values of
data2 to data1 in place. |
static double[] |
add(double term,
double... data)
Add a
term to the elements of data in place. |
static double[][] |
add(double term,
double[][] data)
Add a
term to the elements of data in place. |
static double[][][] |
add(double term,
double[][][] data)
Add a
term to the elements of data in place. |
static List<Double> |
add(double term,
List<Double> data)
Add a
term to the elements of data in place. |
static List<Double> |
add(List<Double> data1,
List<Double> data2)
Add the values of
data2 to data1 in place. |
static <T> Map<T,Double> |
add(Map<T,Double> map1,
Map<T,Double> map2)
Adds the entries of
map2 to map1 in place. |
static double[] |
buildCleanSequence(double min,
double max,
double step,
boolean ascending,
int scale)
Creates a sequence of evenly spaced values starting at
min and
ending at max. |
static double[] |
buildSequence(double min,
double max,
double step,
boolean ascending)
Creates a sequence of evenly spaced values starting at
min and
ending at max. |
static double |
checkDelta(double min,
double max,
double Δ)
Ensure validity of sequence discretization parameters.
|
static Collection<Double> |
checkFinite(Collection<Double> data)
Ensure the elements of
data are finite. |
static double[] |
checkFinite(double... data)
Ensure the elements of
data are finite. |
static double |
checkFinite(String label,
double value)
Ensure
value is finite. |
static Collection<Double> |
checkInRange(Range<Double> range,
Collection<Double> data)
Ensure the elements of
data fall within the specified Range
. |
static double[] |
checkInRange(Range<Double> range,
double... data)
Ensure the elements of
data fall within the specified Range
. |
static double |
checkInRange(Range<Double> range,
String label,
double value)
Ensure
value falls within the specified Range. |
static Collection<Double> |
checkSize(int min,
Collection<Double> data)
Ensure
data.size() ≥ min. |
static double[] |
checkSize(int min,
double[] data)
Ensure
data.length ≥ min. |
static void |
checkSizes(Collection<Double> data1,
Collection<Double> data2)
Ensure the supplied datasets are the same size.
|
static void |
checkSizes(double[][][] data1,
double[][][] data2)
Ensure the 1st dimensions of the supplied datasets are the same
size.
|
static void |
checkSizes(double[][] data1,
double[][] data2)
Ensure the 1st dimensions of the supplied datasets are the same
size.
|
static void |
checkSizes(double[] data1,
double[] data2)
Ensure the supplied datasets are the same size.
|
static double |
checkWeight(double weight)
Ensure
0.0 < weight ≤ 1.0. |
static Collection<Double> |
checkWeights(Collection<Double> weights)
Ensure each
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001. |
static Collection<Double> |
checkWeights(Collection<Double> weights,
boolean allowZero)
Ensure each
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001, optionally allowing zero-valued
weights. |
static double[] |
checkWeights(double[] weights)
Ensure each
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001. |
static double[] |
checkWeights(double[] weights,
Boolean allowZero)
Ensure each
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001, optionally allowing zero-valued
weights. |
static double[] |
collapse(double[][] data)
Sum the arrays in the 2nd dimension of
data into a new 1-D array. |
static double[][] |
collapse(double[][][] data)
Sum the arrays in the 3rd dimension of
data into the 2nd dimension
of a new 2-D array. |
static XySequence |
combine(Iterable<XySequence> sequences)
Deprecated.
|
static double[][] |
copyOf(double[][] data)
Create a deep copy of a two-dimensional data array.
|
static double[][][] |
copyOf(double[][][] data)
Create a deep copy of a three-dimensional data array.
|
static double[] |
cumulate(double... data)
Cumulate the values of
data in place. |
static double[] |
diff(double... data)
Build an array of the differences between the adjacent elements of
data. |
static double[] |
divide(double[] data1,
double[] data2)
Divide the elements of
data1 by the elements of data2 in
place. |
static List<Double> |
divide(List<Double> data1,
List<Double> data2)
Divide the elements of
data1 by the elements of data2 in
place. |
static double[] |
exp(double... data)
Raise Euler's number
e to each of the elements of data in
place. |
static List<Double> |
exp(List<Double> data)
Raise Euler's number
e to each of the elements of data in
place. |
static double[] |
flip(double... data)
Flip the sign of the elements of
data in place. |
static List<Double> |
flip(List<Double> data)
Flip the sign of the elements of
data in place. |
static double[] |
ln(double[] data)
Take the natural logarithm of the elements of
data in place. |
static List<Double> |
ln(List<Double> data)
Take the natural logarithm of the elements of
data in place. |
static double[] |
log(double... data)
Take the base-10 logarithm of the elements of
data in place. |
static List<Double> |
log(List<Double> data)
Take the base-10 logarithm of the elements of
data in place. |
static double[] |
multiply(double[] data1,
double[] data2)
Multiply the elements of
data1 by the elements of data2 in
place. |
static double[] |
multiply(double scale,
double... data)
Multiply (
scale) the elements of data in place. |
static double[][] |
multiply(double scale,
double[][] data)
Multiply (
scale) the elements of data in place. |
static double[][][] |
multiply(double scale,
double[][][] data)
Multiply (
scale) the elements of data in place. |
static List<Double> |
multiply(double scale,
List<Double> data)
Multiply (
scale) the elements of data in place. |
static List<Double> |
multiply(List<Double> data1,
List<Double> data2)
Multiply the elements of
data1 by the elements of data2 in
place. |
static double[] |
normalize(double... data)
Normalize the elements of
data in place such that they sum to 1. |
static List<Double> |
normalize(List<Double> data)
Normalize the elements of
data in place such that they sum to 1. |
static double |
percentDiff(double test,
double target)
Compute the difference between
test and target, relative to
target, as a percent. |
static double[] |
positivize(double... data)
Ensures positivity of values by adding
Math.abs(min(data)) in place
if min < 0. |
static double[] |
pow10(double... data)
Raise the elements of
data to the power of 10 in place. |
static List<Double> |
pow10(List<Double> data)
Raise the elements of
data to the power of 10 in place. |
static double[] |
round(int scale,
double... data)
Round the elements of
data in place to double values of a specified
scale (precision). |
static List<Double> |
round(int scale,
List<Double> data)
Round the elements of
data in place to double values of a specified
scale (precision). |
static double[] |
subtract(double[] data1,
double[] data2)
Subtract the values of
data2 from data1 in place. |
static List<Double> |
subtract(List<Double> data1,
List<Double> data2)
Subtract the values of
data2 from data1 in place. |
static double |
sum(Collection<Double> data)
Sum the elements of
data. |
static double |
sum(double... data)
Sum the elements of
data. |
static String |
toString(double[][] data)
Format a two-dimensional data array for printing.
|
static String |
toString(double[][][] data)
Format a three-dimensional data array for printing
|
static double[] |
transform(DoubleUnaryOperator function,
double... data)
Transform
data by a DoubleUnaryOperator in place. |
static List<Double> |
transform(DoubleUnaryOperator function,
List<Double> data)
Transform
data by a DoubleUnaryOperator in place. |
static double[] |
transform(Function<Double,Double> function,
double... data)
Transform
data by a function in place. |
static double[] |
transform(int lower,
int upper,
DoubleUnaryOperator function,
double... data)
Transform
data in a given range, [minIndex,
maxIndex), by a DoubleUnaryOperator in place. |
static double[] |
transform(Range<Integer> range,
DoubleUnaryOperator function,
double... data)
Transform
data in a given Range by a
DoubleUnaryOperator in place. |
static double |
weightedSum(double[] data,
double[] weights)
Compute the weighted sum of the supplied data.
|
static double |
weightedSumLn(double[] data,
double[] weights)
Compute a weighted sum of the supplied data in linear space, given data
that is in natural log space.
|
public static List<Double> add(double term, List<Double> data)
term to the elements of data in place.data - to operate onterm - to adddatapublic static double[] add(double term,
double... data)
term to the elements of data in place.data - to operate onterm - to adddatapublic static double[][] add(double term,
double[][] data)
term to the elements of data in place.data - to operate onterm - to adddatapublic static double[][][] add(double term,
double[][][] data)
term to the elements of data in place.data - to operate onterm - to adddatapublic static List<Double> add(List<Double> data1, List<Double> data2)
data2 to data1 in place.data1 - data2 - data1public static double[] add(double[] data1,
double[] data2)
data2 to data1 in place.data1 - data2 - data1public static double[][] add(double[][] data1,
double[][] data2)
data2 to data1 in place.data1 - data2 - data1public static double[][][] add(double[][][] data1,
double[][][] data2)
data2 to data1 in place.data1 - data2 - data1public static <T> Map<T,Double> add(Map<T,Double> map1, Map<T,Double> map2)
map2 to map1 in place. If a key from
map2 exists in map1, then the value for that key is added
to the corresponding value in map1. If no such key exists in map 1,
then the key and value from map2 are transferred as is. Note that this
method is not synchronized.map1 - map2 - map1public static List<Double> subtract(List<Double> data1, List<Double> data2)
data2 from data1 in place. To
subtract a term from every value of a dataset, use
add(double, List) with a negative addend.data1 - data2 - data1public static double[] subtract(double[] data1,
double[] data2)
data2 from data1 in place. To
subtract a term from every value of a dataset, use
add(double, double...) with a negative addend.data1 - data2 - data1public static List<Double> multiply(double scale, List<Double> data)
scale) the elements of data in place.data - to operate onscale - factordatapublic static double[] multiply(double scale,
double... data)
scale) the elements of data in place.data - to operate onscale - factordatapublic static double[][] multiply(double scale,
double[][] data)
scale) the elements of data in place.data - to operate onscale - factordatapublic static double[][][] multiply(double scale,
double[][][] data)
scale) the elements of data in place.data - to operate onscale - factordatapublic static List<Double> multiply(List<Double> data1, List<Double> data2)
data1 by the elements of data2 in
place.data1 - data2 - data1public static double[] multiply(double[] data1,
double[] data2)
data1 by the elements of data2 in
place.data1 - data2 - data1public static double[] divide(double[] data1,
double[] data2)
data1 by the elements of data2 in
place. To divide every value of a dataset by some term, use
multiply(double, double...) with 1/divisor.data1 - data2 - data1public static List<Double> divide(List<Double> data1, List<Double> data2)
data1 by the elements of data2 in
place. To divide every value of a dataset by some term, use
multiply(double, List) with 1/divisor.data1 - data2 - data1public static List<Double> abs(List<Double> data)
data to their absolute value in place.data - to operate ondataMath.abs(double)public static double[] abs(double... data)
data to their absolute value in place.data - to operate ondataMath.abs(double)public static List<Double> exp(List<Double> data)
e to each of the elements of data in
place.data - to operate ondataMath.exp(double)public static double[] exp(double... data)
e to each of the elements of data in
place.data - to operate ondataMath.exp(double)public static List<Double> ln(List<Double> data)
data in place.data - to operate ondataMath.log(double)public static double[] ln(double[] data)
data in place.data - to operate ondataMath.log(double)public static List<Double> pow10(List<Double> data)
data to the power of 10 in place.data - to operate ondataMath.pow(double, double)public static double[] pow10(double... data)
data to the power of 10 in place.data - to operate ondataMath.pow(double, double)public static List<Double> log(List<Double> data)
data in place.data - to operate ondataMath.log10(double)public static double[] log(double... data)
data in place.data - to operate ondataMath.log10(double)public static List<Double> flip(List<Double> data)
data in place.data - to operate ondatapublic static double[] flip(double... data)
data in place.data - to operate ondatapublic static double sum(Collection<Double> data)
data. Method returns zero for an empty
data argument.data - to sumpublic static double sum(double... data)
data. Method returns zero for empty
data argument or no varargs.data - to sumpublic static double weightedSum(double[] data,
double[] weights)
data - to sumweights - to applypublic static double weightedSumLn(double[] data,
double[] weights)
data - to sum (natural log values)weights - to applypublic static double[] collapse(double[][] data)
data into a new 1-D array.data - to collapsedatapublic static double[][] collapse(double[][][] data)
data into the 2nd dimension
of a new 2-D array.data - to collapsedatapublic static double[] cumulate(double... data)
data in place.data - to operate ondatapublic static double[] transform(DoubleUnaryOperator function, double... data)
data by a DoubleUnaryOperator in place.function - to apply to datadata - to operate ondatapublic static List<Double> transform(DoubleUnaryOperator function, List<Double> data)
data by a DoubleUnaryOperator in place.function - to applydata - to operate ondatapublic static double[] transform(Function<Double,Double> function, double... data)
data by a function in place.function - to applydata - to operate ondatapublic static double[] transform(Range<Integer> range, DoubleUnaryOperator function, double... data)
data in a given Range by a
DoubleUnaryOperator in place.range - to apply the functionfunction - to apply to the datadata - to operate ondatapublic static double[] transform(int lower,
int upper,
DoubleUnaryOperator function,
double... data)
data in a given range, [minIndex,
maxIndex), by a DoubleUnaryOperator in place.lower - inclusive indexupper - exclusive indexfunction - to apply to the datadata - to operate ondatapublic static List<Double> normalize(List<Double> data)
data in place such that they sum to 1.data - to normalizedataIllegalArgumentException - if data is empty or no varargs are
supplied, contains values outside the range [0..+Inf), or
sums to a value outside the range (0..+Inf)public static double[] normalize(double... data)
data in place such that they sum to 1.data - to normalizedataIllegalArgumentException - if data is empty or no varargs are
supplied, contains values outside the range [0..+Inf), or
sums to a value outside the range (0..+Inf)public static List<Double> round(int scale, List<Double> data)
data in place to double values of a specified
scale (precision). Internally, this method uses the rounding and precision
functionality of BigDecimal.data - to operate onscale - decimal precisiondataMaths.round(double, int)public static double[] round(int scale,
double... data)
data in place to double values of a specified
scale (precision). Internally, this method uses the rounding and precision
functionality of BigDecimal.data - to operate onscale - decimal precisiondataMaths.round(double, int)public static double[] positivize(double... data)
Math.abs(min(data)) in place
if min < 0.data - to operate onpublic static double[] diff(double... data)
data. Method returns results in a new array that has
data.length - 1 where differences are computed per
data[i+1] - data[i].data - to differenceIllegalArgumentException - if data.length < 2public static double percentDiff(double test,
double target)
test and target, relative to
target, as a percent. If target == 0, method returns
0 if test == 0, otherwise Double.POSITIVE_INFINITY.test - valuetarget - value100 * abs(test - target) / targetIllegalArgumentException - if test or target are not
finite.public static double checkDelta(double min,
double max,
double Δ)
[min, max] and Δ that:
min, max, and Δ are finitemax > minΔ ≥ 0Δ > 0 for max > minΔ ≤ max - minmin - valuemax - valueΔ - discretization deltaΔ for use inlinepublic static double checkFinite(String label, double value)
value is finite.label - for value if check failsvalue - to checkIllegalArgumentException - if value is outside the range
(-Inf..+Inf)Doubles.isFinite(double)public static Collection<Double> checkFinite(Collection<Double> data)
data are finite.data - to validatedataIllegalArgumentException - if data is empty, or any elements
of data are outside the range (-Inf..+Inf)Doubles.isFinite(double)public static double[] checkFinite(double... data)
data are finite.data - to validatedataIllegalArgumentException - if data is empty or no varargs are
supplied, or any elements of data are outside the range
(-Inf..+Inf)Doubles.isFinite(double)public static double checkInRange(Range<Double> range, String label, double value)
value falls within the specified Range.range - of allowable valueslabel - for value if check failsvalue - to validatevalueIllegalArgumentException - if either range endpoint is NaN. A
range where both enpoints are NaN and at least one enpoint
is closed is permitted; only when both endpoints are
closed is the value NaN permitted to the exclusion of all
other values.public static Collection<Double> checkInRange(Range<Double> range, Collection<Double> data)
data fall within the specified Range
.range - of allowable valuesdata - to validatedatafor exception notespublic static double[] checkInRange(Range<Double> range, double... data)
data fall within the specified Range
.range - of allowable valuesdata - to validatedatafor exception notespublic static Collection<Double> checkSize(int min, Collection<Double> data)
data.size() ≥ min.datapublic static double[] checkSize(int min,
double[] data)
data.length ≥ min.datapublic static void checkSizes(Collection<Double> data1, Collection<Double> data2)
public static void checkSizes(double[] data1,
double[] data2)
public static void checkSizes(double[][] data1,
double[][] data2)
public static void checkSizes(double[][][] data1,
double[][][] data2)
public static double checkWeight(double weight)
0.0 < weight ≤ 1.0. This method does not permit zero-valued
weights.weightpublic static Collection<Double> checkWeights(Collection<Double> weights)
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001. This method permits zero-valued
weights.weights - to validateweightspublic static Collection<Double> checkWeights(Collection<Double> weights, boolean allowZero)
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001, optionally allowing zero-valued
weights.weights - to validateweightspublic static double[] checkWeights(double[] weights,
Boolean allowZero)
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001, optionally allowing zero-valued
weights.weights - to validateweightspublic static double[] checkWeights(double[] weights)
0.0 ≤ weight ≤ 1.0 and
sum(weights) = 1.0 ± 0.0001. This method permits zero-valued
weights.weights - to validateweightspublic static double[][] copyOf(double[][] data)
data - to copydatapublic static double[][][] copyOf(double[][][] data)
data - to copydatapublic static String toString(double[][] data)
data - to formatdatapublic static String toString(double[][][] data)
data - to formatdatapublic static double[] buildCleanSequence(double min,
double max,
double step,
boolean ascending,
int scale)
min and
ending at max. If (max - min) / step is not equivalent to
an integer, the last step in the sequence will be <step. Unlike
buildSequence(double, double, double, boolean), this method
returns a sequence where any 'odd' values due to rounding errors have been
removed, at least within the range of the specified scale
(precision or number of decimal places).min - sequence valuemax - sequence valuestep - sequence spacingascending - if true, descending if falsescale - the number of decimal places to preserveIllegalArgumentException - if min >= max, step <= 0 ,
or any arguments are Double.NaN,
Double.POSITIVE_INFINITY, or
Double.NEGATIVE_INFINITYpublic static double[] buildSequence(double min,
double max,
double step,
boolean ascending)
min and
ending at max. If (max - min) / step is not integer valued,
the last step in the sequence will be <step. If min == max,
then an array containing a single value is returned.min - sequence valuemax - sequence valuestep - sequence spacingascending - if true, descending if falseIllegalArgumentException - if min >= max, step <= 0 ,
or any arguments are Double.NaN,
Double.POSITIVE_INFINITY, or
Double.NEGATIVE_INFINITY@Deprecated public static XySequence combine(Iterable<XySequence> sequences)
sequences. The y-values returned are the set
of all supplied y-values. The x-values returned are the sum of the supplied
x-values. When summing, x-values for points outside the original domain of
a sequence are set to 0, while those inside the original domain are sampled
via linear interpolation.sequences - to combine