public final class DoubleArrayMath extends Object
This utility is used throughout the system when working with double arrays.
| Modifier and Type | Field and Description |
|---|---|
static double[] |
EMPTY_DOUBLE_ARRAY
An empty
double array. |
static Double[] |
EMPTY_DOUBLE_OBJECT_ARRAY
An empty
Double array. |
| Modifier and Type | Method and Description |
|---|---|
static double[] |
apply(double[] array,
DoubleUnaryOperator operator)
Applies an operator to each element in the array, returning a new array.
|
static double[] |
applyAddition(double[] array,
double valueToAdd)
Applies an addition to each element in the array, returning a new array.
|
static double[] |
applyMultiplication(double[] array,
double valueToMultiplyBy)
Applies a multiplication to each element in the array, returning a new array.
|
static double[] |
combine(double[] array1,
double[] array2,
DoubleBinaryOperator operator)
Combines two arrays, returning an array where each element is the combination of the two matching inputs.
|
static double[] |
combineByAddition(double[] array1,
double[] array2)
Combines two arrays, returning an array where each element is the sum of the two matching inputs.
|
static double[] |
combineByMultiplication(double[] array1,
double[] array2)
Combines two arrays, returning an array where each element is the multiplication of the two matching inputs.
|
static double[] |
combineLenient(double[] array1,
double[] array2,
DoubleBinaryOperator operator)
Combines two arrays, returning an array where each element is the combination of the two matching inputs.
|
static boolean |
fuzzyEquals(double[] array1,
double[] array2,
double tolerance)
Compares each element in the first array to the matching index in the second array within a tolerance.
|
static boolean |
fuzzyEqualsZero(double[] array,
double tolerance)
Compares each element in the array to zero within a tolerance.
|
static void |
mutate(double[] array,
DoubleUnaryOperator operator)
Mutates each element in the array using an operator by mutation.
|
static void |
mutateByAddition(double[] array,
double valueToAdd)
Adds a constant value to each element in the array by mutation.
|
static void |
mutateByAddition(double[] array,
double[] arrayToAdd)
Adds values in two arrays together, mutating the first array.
|
static void |
mutateByMultiplication(double[] array,
double valueToMultiplyBy)
Multiplies each element in the array by a value by mutation.
|
static void |
mutateByMultiplication(double[] array,
double[] arrayToMultiplyBy)
Multiplies values in two arrays, mutating the first array.
|
static double[] |
reorderedCopy(double[] values,
int[] positions)
Returns a copy of the first array in the order defined by the position values of the second array.
|
static void |
sortPairs(double[] keys,
double[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.
|
static void |
sortPairs(double[] keys,
int[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.
|
static <V> void |
sortPairs(double[] keys,
V[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.
|
static double |
sum(double[] array)
Calculates the sum total of all the elements in the array.
|
static Double[] |
toObject(double[] array)
Converts a
double array to a Double array. |
static double[] |
toPrimitive(Double[] array)
Converts a
Double array to a double array. |
public static final double[] EMPTY_DOUBLE_ARRAY
double array.public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY
Double array.public static Double[] toObject(double[] array)
double array to a Double array.array - the array to convertpublic static double[] toPrimitive(Double[] array)
Double array to a double array.
Throws an exception if null is found.
array - the array to convertNullPointerException - if null foundpublic static double sum(double[] array)
The input array is not mutated.
array - the array to sumpublic static double[] applyAddition(double[] array,
double valueToAdd)
The result is always a new array. The input array is not mutated.
array - the input array, not mutatedvalueToAdd - the value to addpublic static double[] applyMultiplication(double[] array,
double valueToMultiplyBy)
The result is always a new array. The input array is not mutated.
array - the input array, not mutatedvalueToMultiplyBy - the value to multiply bypublic static double[] apply(double[] array,
DoubleUnaryOperator operator)
The result is always a new array. The input array is not mutated.
array - the input array, not mutatedoperator - the operator to usepublic static void mutateByAddition(double[] array,
double valueToAdd)
The input array is mutated.
array - the array to mutatevalueToAdd - the value to addpublic static void mutateByAddition(double[] array,
double[] arrayToAdd)
The arrays must be the same length. Each value in arrayToAdd is added to the value at the
corresponding index in array.
array - the array to mutatearrayToAdd - the array containing values to addpublic static void mutateByMultiplication(double[] array,
double valueToMultiplyBy)
The input array is mutated.
array - the array to mutatevalueToMultiplyBy - the value to multiply bypublic static void mutateByMultiplication(double[] array,
double[] arrayToMultiplyBy)
The arrays must be the same length. Each value in array is multiplied by the value at the
corresponding index in arrayToMultiplyBy.
array - the array to mutatearrayToMultiplyBy - the array containing values to multiply bypublic static void mutate(double[] array,
DoubleUnaryOperator operator)
The input array is mutated.
array - the array to mutateoperator - the operator to use to perform the mutationpublic static double[] combineByAddition(double[] array1,
double[] array2)
Each element in the result will be the sum of the matching index in the two input arrays. The two input arrays must have the same length.
For example:
double[] array1 = {1, 5, 9};
double[] array2 = {2, 3, 2};
double[] result = DoubleArrayMath.combineByAddition(array1, array2);
// result contains {3, 8, 11}
The result is always a new array. The input arrays are not mutated.
array1 - the first arrayarray2 - the second arraypublic static double[] combineByMultiplication(double[] array1,
double[] array2)
Each element in the result will be the multiplication of the matching index in the two input arrays. The two input arrays must have the same length.
For example:
double[] array1 = {1, 5, 9};
double[] array2 = {2, 3, 4};
double[] result = DoubleArrayMath.combineByMultiplication(array1, array2);
// result contains {2, 15, 36}
The result is always a new array. The input arrays are not mutated.
array1 - the first arrayarray2 - the second arraypublic static double[] combine(double[] array1,
double[] array2,
DoubleBinaryOperator operator)
Each element in the result will be the combination of the matching index in the two input arrays using the operator. The two input arrays must have the same length.
The result is always a new array. The input arrays are not mutated.
array1 - the first arrayarray2 - the second arrayoperator - the operator to use when combining valuespublic static double[] combineLenient(double[] array1,
double[] array2,
DoubleBinaryOperator operator)
Each element in the result will be the combination of the matching index in the two input arrays using the operator. The result will have the length of the longest of the two inputs. Where one array is longer than the other, the values from the longer array will be used.
The result is always a new array. The input arrays are not mutated.
array1 - the first arrayarray2 - the second arrayoperator - the operator to use when combining valuespublic static boolean fuzzyEqualsZero(double[] array,
double tolerance)
An empty array returns true;
The input array is not mutated.
array - the array to checktolerance - the tolerance to usepublic static boolean fuzzyEquals(double[] array1,
double[] array2,
double tolerance)
If the arrays differ in length, false is returned.
The input arrays are not mutated.
array1 - the first array to checkarray2 - the second array to checktolerance - the tolerance to usepublic static void sortPairs(double[] keys,
double[] values)
The two arrays must be the same size and represent a pair of key to value. The sort order is determined by the array of keys. The position of each value is changed to match that of the sorted keys.
The input arrays are mutated.
keys - the array of keys to sortvalues - the array of associated values to retainpublic static double[] reorderedCopy(double[] values,
int[] positions)
The two arrays must be the same size. The order is determined by the array of positions. The result value at each entry is changed to the value at the position of the positions entry. It is not checked that the positions array does not contain duplicates.
The result is a new array. The input arrays are not mutated.
e.g
double[] values = { 1d, 5d, 10d };
int[] positions = { 2, 0, 1 };
reorderedCopy(values, positions); // returns [10d, 1d, 5d]
values - the array of valuespositions - the array of positionsIllegalArgumentException - if any of the positions do not correspond to an index in the valuespublic static void sortPairs(double[] keys,
int[] values)
The two arrays must be the same size and represent a pair of key to value. The sort order is determined by the array of keys. The position of each value is changed to match that of the sorted keys.
The input arrays are mutated.
keys - the array of keys to sortvalues - the array of associated values to retainpublic static <V> void sortPairs(double[] keys,
V[] values)
The two arrays must be the same size and represent a pair of key to value. The sort order is determined by the array of keys. The position of each value is changed to match that of the sorted keys.
The input arrays are mutated.
V - the type of the valueskeys - the array of keys to sortvalues - the array of associated values to retainCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.