public class DoubleUtils
extends Object
Provides extra functionality for double
primitives.
Constructor and Description |
---|
DoubleUtils() |
Modifier and Type | Method and Description |
---|---|
static Double[] |
asObjectArray(double[] array)
Converts from a
double array to a Double array. |
static double[] |
asPrimitiveArray(Double[] array)
Converts from a
Double array to a double array. |
static double |
average(double[] array)
Returns the average value of an array.
|
static double |
averageNonZeros(double[] array)
Returns the average value only among non-zero values
|
static double[] |
concatenate(double[]... arrays)
Concatenates a series of arrays.
|
static boolean |
contains(double[] array,
double value)
Checks if an input array contains a given value
|
static boolean |
containsAll(double[] array1,
double[] array2)
Checks if an array contains all numbers from another.
|
static boolean |
containsAny(double[] array1,
double[] array2)
Checks whether any element of an array is present in another.
|
static double[] |
copy(double[] array)
Returns a deep copy of the input
array . |
static double[][] |
copy(double[][] array)
Returns a deep copy of the input
array . |
static double[] |
divide(double[] array,
double value)
Divides all elements in an array by a scalar.
|
static double[] |
divide(double[] array1,
double[] array2)
Divides two arrays element-to-element
|
static double[] |
divideNonSingular(double[] array1,
double[] array2)
Divides two arrays element-to-element, but when numerator and denominator = 0, returns 0 instead of a singularity (NaN)
|
static int[] |
find(double[] array,
double value,
Constants.SearchType searchType)
Returns the position(s) where a given value can be found into an array.
|
static double |
gcd(double[] array)
Computes the greatest common divisor of a double array.
|
static Comparator<double[]> |
getMatrixSorter(int columnId,
Constants.OrderingType orderingType)
Sorts a 2D
double array based on a given column. |
static double[] |
intersect(double[]... arrays)
Returns the intersection vector of a series of input arrays.
|
static boolean |
isEqualWithinAbsoluteTolerance(double pattern,
double value,
double tolerance)
Checks if a given value is within a tolerance margin compared to a given pattern.
|
static boolean |
isEqualWithinRelativeTolerance(double pattern,
double value,
double tolerance)
Checks if the relative difference between a given value and a pattern is within a tolerance margin.
|
static String |
join(double[] array,
String separator)
Joins the elements in an input array using a given separator.
|
static int[] |
maxIndexes(double[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum value is found.
|
static int[][] |
maxMinIndexes(double[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum/minimum values are found.
|
static double[] |
maxMinValues(double[] array)
Returns the maximum/minimum values of an input array.
|
static double |
maxValue(double[] array)
Returns the maximum value in the input array.
|
static double |
maxValue(double[][] array)
Returns the maximum value in the input array.
|
static int[] |
minIndexes(double[] array,
Constants.SearchType searchType)
Returns the position(s) in which the minimum value is found.
|
static double |
minValue(double[] array)
Returns the minimum value in the input array.
|
static double[] |
mult(double[] array,
double value)
Multiplies all elements in an array by a scalar.
|
static double[] |
mult(double[] array1,
double[] array2)
Multiplies two arrays element-to-element.
|
static double[] |
ones(int N)
Returns an array filled with zeros.
|
static void |
reverse(double[] array)
Reverses the order of the elements of the input array (it will be overriden).
|
static double |
round(double number,
int decimals)
Rounds a number to the closest
double given the number of
required decimal places. |
static double |
scalarProduct(double[] array1,
double[] array2)
Scalar product of two vectors.
|
static double[] |
select(double[] array,
int[] indexes)
Returns a set of selected elements from an input array.
|
static double[] |
selectColumn(double[][] array,
int column)
Returns a column of a bidimensional input array.
|
static double[] |
selectRow(double[][] array,
int row)
Returns a row of a bidimensional input array.
|
static double[] |
setdiff(double[]... arrays)
Returns the elements contained in the first array, but not any of the others.
|
static void |
sort(double[] array,
Constants.OrderingType orderingType)
Sorts the input array (it will be overriden).
|
static int[] |
sortIndexes(double[] array,
Constants.OrderingType orderingType)
Sorts indexes of the
array into ascending/descending order in a stable way. |
static double |
std(double[] array)
Returns the standard deviation of an array.
|
static double[] |
substract(double[] array1,
double[] array2)
Returns the element-wise substraction of two arrays.
|
static double |
sum(double[] array)
Returns the sum of all elements in the array.
|
static double[] |
sum(double[] array1,
double[] array2)
Returns the element-wise sum of two arrays.
|
static double[] |
toArray(Collection<Double> list)
Converts a collection (
List , Set ...) of Double objects to a double array. |
static int[] |
toIntArray(double[] array)
Converts a
double array to an int array |
static List<Double> |
toList(double[] array)
Converts from a
double array to a list. |
static double[] |
union(double[]... arrays)
Returns an array with all elements in input arrays (no repetitions).
|
static double[] |
unique(double[] array)
Returns the same values of the input
array but with no repetitions. |
static double[] |
zeros(int N)
Returns an array filled with zeros.
|
public static double[] concatenate(double[]... arrays)
arrays
- List of arrayspublic static double std(double[] array)
array
- Input arraypublic static double averageNonZeros(double[] array)
array
- Input arraypublic static double average(double[] array)
array
- Input arraypublic static Double[] asObjectArray(double[] array)
double
array to a Double
array.array
- double
arrayDouble
arraypublic static double[] asPrimitiveArray(Double[] array)
Double
array to a double
array.array
- Double
arraydouble
arraypublic static boolean contains(double[] array, double value)
array
- Input arrayvalue
- Value to searchtrue
if value
is present in array
, and false otherwisepublic static boolean containsAll(double[] array1, double[] array2)
array1
- Container arrayarray2
- Array with elements to be checkedtrue
if value
is present in array
, and false otherwisepublic static boolean containsAny(double[] array1, double[] array2)
intersection(array1, array2).length == 0
.array1
- Container arrayarray2
- Array with elements to be checkedtrue
if any number in array1
is present in array2
, and false otherwisepublic static double[] copy(double[] array)
array
.array
- Input arrayarray
public static double[][] copy(double[][] array)
array
.array
- Input arrayarray
public static int[] find(double[] array, double value, Constants.SearchType searchType)
array
- Input arrayvalue
- Value to be searched forsearchType
- Indicates whether the first, the last, or all minimum positions are returnedpublic static double gcd(double[] array)
array
- Input arraygcd(0, 0)
is equal to zero and gcd([])
is equal to onepublic static double[] intersect(double[]... arrays)
arrays
- Vector of input arrayspublic static boolean isEqualWithinAbsoluteTolerance(double pattern, double value, double tolerance)
pattern
- Pattern valuevalue
- Value to compare with patterntolerance
- Tolerance margintrue
if value
is within the tolerance, and false otherwisepublic static boolean isEqualWithinRelativeTolerance(double pattern, double value, double tolerance)
pattern
- Pattern valuevalue
- Value to compare with patterntolerance
- Tolerance margintrue
if value
is within the tolerance, and false otherwisepublic static String join(double[] array, String separator)
Arrays.toString()
.array
- Input arrayseparator
- SeparatorString
representation of the input array
public static double maxValue(double[] array)
array
- Input arraypublic static int[] maxIndexes(double[] array, Constants.SearchType searchType)
array
- Input arraysearchType
- Indicates whether the first, the last, or all maximum positions are returnedpublic static double[] maxMinValues(double[] array)
array
- Input arraypublic static double maxValue(double[][] array)
array
- Input arraypublic static int[][] maxMinIndexes(double[] array, Constants.SearchType searchType)
Returns the position(s) in which the maximum/minimum values are found.
out[0]
are the maximum positions, while out[1]
are the minimum positions
array
- Input arraysearchType
- Indicates whether the first, the last, or all maximum positions are returnedpublic static double minValue(double[] array)
array
- Input arraypublic static int[] minIndexes(double[] array, Constants.SearchType searchType)
array
- Input arraysearchType
- Indicates whether the first, the last, or all minimum positions are returnedpublic static double[] ones(int N)
N
- Number of elementsN
public static void reverse(double[] array)
array
- Input arraypublic static double[] select(double[] array, int[] indexes)
array
- Input arrayindexes
- Position of elements to be selected.indexes
public static double[] selectColumn(double[][] array, int column)
array
- Input arraycolumn
- Columnpublic static double[] selectRow(double[][] array, int row)
array
- Input arrayrow
- Rowpublic static double[] setdiff(double[]... arrays)
arrays
- Input arrayspublic static void sort(double[] array, Constants.OrderingType orderingType)
array
- Input arrayorderingType
- Ascending or descendingpublic static int[] sortIndexes(double[] array, Constants.OrderingType orderingType)
array
into ascending/descending order in a stable way. Stable means that index order doesn't change if values are the same.array
- Array to be sortedorderingType
- Ascending or descendingpublic static double[] toArray(Collection<Double> list)
List
, Set
...) of Double
objects to a double
array.list
- Input listint
arraypublic static List<Double> toList(double[] array)
double
array to a list.array
- Input arrayDouble
objectspublic static double[] union(double[]... arrays)
arrays
- Input arrayspublic static double[] unique(double[] array)
array
but with no repetitions. There is no order guarantee.array
- Input arrayarray
but with no repetitionspublic static double[] zeros(int N)
N
- Number of elementsN
public static int[] toIntArray(double[] array)
double
array to an int
arrayarray
- Input arrayint
arraypublic static double sum(double[] array)
array
- Input arraypublic static double[] sum(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static double[] mult(double[] array, double value)
array
- Input arrayvalue
- Scalarpublic static double[] mult(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static double[] substract(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static double[] divide(double[] array1, double[] array2)
array1
- Numeratorarray2
- Denominatorpublic static double[] divide(double[] array, double value)
array
- Input arrayvalue
- Scalarpublic static double[] divideNonSingular(double[] array1, double[] array2)
array1
- Numeratorarray2
- Denominatorpublic static double scalarProduct(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static Comparator<double[]> getMatrixSorter(int columnId, Constants.OrderingType orderingType)
double
array based on a given column.columnId
- Column identifierorderingType
- Ascending or descendingArrays.sort
public static double round(double number, int decimals)
double
given the number of
required decimal places.number
- Number to be roundeddecimals
- Decimal places