Class DoubleArrayMath
- java.lang.Object
-
- com.opengamma.strata.collect.DoubleArrayMath
-
public final class DoubleArrayMath extends Object
Contains utility methods for maths on double arrays.This utility is used throughout the system when working with double arrays.
-
-
Field Summary
Fields Modifier and Type Field Description static double[]
EMPTY_DOUBLE_ARRAY
An emptydouble
array.static Double[]
EMPTY_DOUBLE_OBJECT_ARRAY
An emptyDouble
array.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 adouble
array to aDouble
array.static double[]
toPrimitive(Double[] array)
Converts aDouble
array to adouble
array.
-
-
-
Field Detail
-
EMPTY_DOUBLE_ARRAY
public static final double[] EMPTY_DOUBLE_ARRAY
An emptydouble
array.
-
EMPTY_DOUBLE_OBJECT_ARRAY
public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY
An emptyDouble
array.
-
-
Method Detail
-
toObject
public static Double[] toObject(double[] array)
Converts adouble
array to aDouble
array.- Parameters:
array
- the array to convert- Returns:
- the converted array
-
toPrimitive
public static double[] toPrimitive(Double[] array)
Converts aDouble
array to adouble
array.Throws an exception if null is found.
- Parameters:
array
- the array to convert- Returns:
- the converted array
- Throws:
NullPointerException
- if null found
-
sum
public static double sum(double[] array)
Calculates the sum total of all the elements in the array.The input array is not mutated.
- Parameters:
array
- the array to sum- Returns:
- the sum total of all the elements
-
applyAddition
public static double[] applyAddition(double[] array, double valueToAdd)
Applies an addition to each element in the array, returning a new array.The result is always a new array. The input array is not mutated.
- Parameters:
array
- the input array, not mutatedvalueToAdd
- the value to add- Returns:
- the resulting array
-
applyMultiplication
public static double[] applyMultiplication(double[] array, double valueToMultiplyBy)
Applies a multiplication to each element in the array, returning a new array.The result is always a new array. The input array is not mutated.
- Parameters:
array
- the input array, not mutatedvalueToMultiplyBy
- the value to multiply by- Returns:
- the resulting array
-
apply
public static double[] apply(double[] array, DoubleUnaryOperator operator)
Applies an operator to each element in the array, returning a new array.The result is always a new array. The input array is not mutated.
- Parameters:
array
- the input array, not mutatedoperator
- the operator to use- Returns:
- the resulting array
-
mutateByAddition
public static void mutateByAddition(double[] array, double valueToAdd)
Adds a constant value to each element in the array by mutation.The input array is mutated.
- Parameters:
array
- the array to mutatevalueToAdd
- the value to add
-
mutateByAddition
public static void mutateByAddition(double[] array, double[] arrayToAdd)
Adds values in two arrays together, mutating the first array.The arrays must be the same length. Each value in
arrayToAdd
is added to the value at the corresponding index inarray
.- Parameters:
array
- the array to mutatearrayToAdd
- the array containing values to add
-
mutateByMultiplication
public static void mutateByMultiplication(double[] array, double valueToMultiplyBy)
Multiplies each element in the array by a value by mutation.The input array is mutated.
- Parameters:
array
- the array to mutatevalueToMultiplyBy
- the value to multiply by
-
mutateByMultiplication
public static void mutateByMultiplication(double[] array, double[] arrayToMultiplyBy)
Multiplies values in two arrays, mutating the first array.The arrays must be the same length. Each value in
array
is multiplied by the value at the corresponding index inarrayToMultiplyBy
.- Parameters:
array
- the array to mutatearrayToMultiplyBy
- the array containing values to multiply by
-
mutate
public static void mutate(double[] array, DoubleUnaryOperator operator)
Mutates each element in the array using an operator by mutation.The input array is mutated.
- Parameters:
array
- the array to mutateoperator
- the operator to use to perform the mutation
-
combineByAddition
public static double[] combineByAddition(double[] array1, double[] array2)
Combines two arrays, returning an array where each element is the sum of the two matching inputs.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.
- Parameters:
array1
- the first arrayarray2
- the second array- Returns:
- an array combining the two input arrays using the plus operator
-
combineByMultiplication
public static double[] combineByMultiplication(double[] array1, double[] array2)
Combines two arrays, returning an array where each element is the multiplication of the two matching inputs.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.
- Parameters:
array1
- the first arrayarray2
- the second array- Returns:
- an array combining the two input arrays using the multiply operator
-
combine
public 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.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.
- Parameters:
array1
- the first arrayarray2
- the second arrayoperator
- the operator to use when combining values- Returns:
- an array combining the two input arrays using the operator
-
combineLenient
public 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.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.
- Parameters:
array1
- the first arrayarray2
- the second arrayoperator
- the operator to use when combining values- Returns:
- an array combining the two input arrays using the operator
-
fuzzyEqualsZero
public static boolean fuzzyEqualsZero(double[] array, double tolerance)
Compares each element in the array to zero within a tolerance.An empty array returns true;
The input array is not mutated.
- Parameters:
array
- the array to checktolerance
- the tolerance to use- Returns:
- true if the array is effectively equal to zero
-
fuzzyEquals
public 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.If the arrays differ in length, false is returned.
The input arrays are not mutated.
- Parameters:
array1
- the first array to checkarray2
- the second array to checktolerance
- the tolerance to use- Returns:
- true if the arrays are effectively equal
-
sortPairs
public static void sortPairs(double[] keys, double[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.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.
- Parameters:
keys
- the array of keys to sortvalues
- the array of associated values to retain
-
reorderedCopy
public 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.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]
- Parameters:
values
- the array of valuespositions
- the array of positions- Returns:
- the reordered copy
- Throws:
IllegalArgumentException
- if any of the positions do not correspond to an index in the values
-
sortPairs
public static void sortPairs(double[] keys, int[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.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.
- Parameters:
keys
- the array of keys to sortvalues
- the array of associated values to retain
-
sortPairs
public static <V> void sortPairs(double[] keys, V[] values)
Sorts the two arrays, retaining the associated values with the sorted keys.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.
- Type Parameters:
V
- the type of the values- Parameters:
keys
- the array of keys to sortvalues
- the array of associated values to retain
-
-