public class DoubleUtils extends Object
Provides extra functionality for double
primitives.
Modifier and Type | Method and Description |
---|---|
static double[] |
arrayOf(double... values)
Generates a
double[] from comma-separated values. |
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(Collection<Double> collection)
Returns the average value of a collection.
|
static double |
average(double[] array)
Returns the average value of an array.
|
static <A> double |
average(Map<A,Double> map)
Returns the average of the map values.
|
static double |
averageNonZeros(double[] array)
Returns the average value only among non-zero values
|
static double[] |
ceil(double[] array)
Rounds up each element of a given
double array. |
static double[] |
concatenate(double[]... arrays)
Concatenates a series of arrays.
|
static double[] |
constantArray(int N,
double value)
Returns an array filled with a given value.
|
static <A> Map<A,Double> |
constantMap(Set<A> identifiers,
double value)
Returns a map filled with a given value.
|
static boolean |
contains(double[] array,
double value)
Checks if an input array contains a given value
|
static boolean |
containsAll(double[] container,
double[] array)
Checks if an array contains all numbers from another one.
|
static boolean |
containsAny(double[] container,
double[] array)
Checks whether any element of an array is present in another one.
|
static <A extends Number> |
convertArray2Map(Set<A> identifiers,
double[] array)
Returns a map containing the association of each identifier with its corresponding
value within the array (in linear order).
|
static <A extends Number,B extends Number> |
convertMatrix2Map(Set<A> rowIdentifiers,
Set<B> columnIdentifiers,
DoubleMatrix2D matrix)
Returns a map containing the association of each identifier pair with its corresponding
value within the table (in linear order).
|
static <A extends Number,B extends Number> |
convertTable2Map(Set<A> rowIdentifiers,
Set<B> columnIdentifiers,
double[][] table)
Returns a map containing the association of each identifier pair with its corresponding
value within the table (in linear order).
|
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 <A> Map<A,Double> |
divide(Map<A,Double> map,
double value)
Divides all elements in a map by a scalar.
|
static <A> Map<A,Double> |
divide(Map<A,Double> map1,
Map<A,Double> map2)
Returns the element-wise quotient of two maps.
|
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[] |
floor(double[] array)
Rounds down each element of a given
double array. |
static String |
formatAndRemoveTrailingZeros(double value,
int numDecimals)
Converts a
double to String (using '%f' modifier) and remove trailing zeros. |
static double |
gcd(Collection<Double> collection)
Computes the greatest common divisor of an input collection.
|
static double |
gcd(double[] array)
Computes the greatest common divisor of a double array.
|
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 List<Double> |
listOf(double... values)
Returns a list of the comma-separated input values.
|
static int[] |
maxIndexes(double[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum value is found.
|
static int[] |
maxIndexes(double[] array,
Constants.SearchType searchType,
double precisionFactor)
Returns the position(s) in which the maximum value is found.
|
static <A> Set<A> |
maxIndexes(Map<A,Double> map,
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 int[][] |
maxMinIndexes(double[] array,
Constants.SearchType searchType,
double precisionFactor)
Returns the position(s) in which the maximum/minimum values are found.
|
static double[] |
maxMinValues(Collection<Double> collection)
Returns the maximum/minimum values of an input collection.
|
static double[] |
maxMinValues(double[] array)
Returns the maximum/minimum values of an input array.
|
static <A> double[] |
maxMinValues(Map<A,Double> map)
Returns the maximum/minimum values of an input array.
|
static double |
maxValue(Collection<Double> collection)
Returns the maximum value in the input collection.
|
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 <A> double |
maxValue(Map<A,Double> map)
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 <A> Set<A> |
minIndexes(Map<A,Double> map,
Constants.SearchType searchType)
Returns the position(s) in which the minimum value is found.
|
static double |
minValue(Collection<Double> collection)
Returns the minimum value in the input collection.
|
static double |
minValue(double[] array)
Returns the minimum value in the input array.
|
static double |
minValue(double[][] matrix)
Returns the minimum value in the input matrix.
|
static <A> double |
minValue(Map<A,Double> map)
Returns the minimum value in the input map.
|
static double[][] |
mult(double[][] matrix,
double value)
Multiplies all elements in a matrix by a scalar.
|
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 <A> Map<A,Double> |
mult(Map<A,Double> map,
double value)
Multiplies all elements in an array by a scalar.
|
static <A> Map<A,Double> |
mult(Map<A,Double> map1,
Map<A,Double> map2)
Returns the element-wise product of two maps.
|
static double[] |
ones(int N)
Returns an array filled with ones.
|
static <A> Map<A,Double> |
ones(Set<A> identifiers)
Returns a map filled with ones.
|
static DoubleMatrix2D |
read2DMatrixFromFile(File file)
Reads a 2D matrix from a file.
|
static void |
reverse(double[] array)
Reverses the order of the elements of the input array (it will be overriden).
|
static double[] |
round(double[] array,
int decimals)
Rounds a number to the nearest
double given the number of
required decimal places. |
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 <A> double |
scalarProduct(Map<A,Double> map1,
Map<A,Double> map2)
Scalar product of two maps.
|
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 Set<Double> |
setOf(double... values)
Returns a list of the comma-separated input values.
|
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(Collection<Double> collection)
Returns the standard deviation of a collection using the Welford's method.
|
static double |
std(double[] array)
Returns the standard deviation of an array using the Welford's method.
|
static <A> double |
std(Map<A,Double> map)
Returns the standard deviation of values of a map using the Welford's method.
|
static double[] |
substract(double[] array1,
double[] array2)
Returns the element-wise substraction of two arrays.
|
static double |
sum(Collection<Double> collection)
Returns the sum of all elements in the input collection.
|
static double |
sum(double[] array)
Returns the sum of all elements in the input array.
|
static double |
sum(double[][] matrix)
Returns the sum of all elements in the input matrix.
|
static double[] |
sum(double[] array1,
double[] array2)
Returns the element-wise sum of two arrays.
|
static <A> double |
sum(Map<A,Double> map)
Returns the sum of all elements in the input collection.
|
static <A> Map<A,Double> |
sum(Map<A,Double> map1,
Map<A,Double> map2)
Returns the element-wise sum of two maps.
|
static double[] |
toArray(Collection<Double> list)
Converts a collection (
List , Set ...) of Double objects to a double array. |
static double[] |
toArray(String[] list)
Converts an array of
String with the values, into 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 String |
toString(double[] array)
Converts the array into an String of space separated values.
|
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.
|
static <A> Map<A,Double> |
zeros(Set<A> identifiers)
Returns a map filled with zeros.
|
public static double[] arrayOf(double... values)
double[]
from comma-separated values.values
- Comma-separated double
valuesdouble[]
public 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 double average(Collection<Double> collection)
collection
- Input collectioncollection
is empty)public static double average(double[] array)
array
- Input arrayarray
is empty)public static <A> double average(Map<A,Double> map)
A
- Key typemap
- Input mapmap
is empty)public static double averageNonZeros(double[] array)
array
- Input arraypublic static double[] ceil(double[] array)
double
array.array
- Array to be rounded uppublic static double[] concatenate(double[]... arrays)
arrays
- List of arrayspublic static double[] constantArray(int N, double value)
N
- Number of elementsvalue
- Value for all elementsN
with the given valuepublic static <A> Map<A,Double> constantMap(Set<A> identifiers, double value)
A
- Key typeidentifiers
- Set of map keysvalue
- Value for all elementspublic static boolean contains(double[] array, double value)
array
- Input arrayvalue
- Value to searchtrue
if value
is present in array
, and false
otherwise. If array
is empty, it will return false
public static boolean containsAll(double[] container, double[] array)
container
- Container arrayarray
- Array with elements to be checkedtrue
if all elements in array
are present in container
, and false
otherwise. If container
is empty, it will return false
public static boolean containsAny(double[] container, double[] array)
intersection(array1, array2).length == 0
.container
- Container arrayarray
- Array with elements to be checkedtrue
if any element in array
is present in container
, and false
otherwise. If container
is empty, it will return false
public static <A extends Number> Map<A,Double> convertArray2Map(Set<A> identifiers, double[] array)
A
- Key typeidentifiers
- Set of identifier in ascending iterator order (duplicated and null values are not allowed)array
- Input arraypublic static <A extends Number,B extends Number> Map<Pair<A,B>,Double> convertMatrix2Map(Set<A> rowIdentifiers, Set<B> columnIdentifiers, DoubleMatrix2D matrix)
A
- Row key typeB
- Column key typerowIdentifiers
- Set of row identifiers in ascending iterator order (null values are not allowed)columnIdentifiers
- Set of column identifiers in ascending iterator order (duplicated and null values are not allowed)matrix
- Input matrixpublic static <A extends Number,B extends Number> Map<Pair<A,B>,Double> convertTable2Map(Set<A> rowIdentifiers, Set<B> columnIdentifiers, double[][] table)
A
- Row key typeB
- Column key typerowIdentifiers
- Set of row identifiers in ascending iterator order (null values are not allowed)columnIdentifiers
- Set of column identifiers in ascending iterator order (duplicated and null values are not allowed)table
- Input tablepublic static double[] copy(double[] array)
array
.array
- Input arrayarray
public static double[][] copy(double[][] array)
array
.array
- Input arrayarray
public static double[] divide(double[] array, double value)
array
- Input arrayvalue
- Scalarpublic static double[] divide(double[] array1, double[] array2)
array1
- Numeratorarray2
- Denominatorpublic static <A> Map<A,Double> divide(Map<A,Double> map, double value)
A
- Key typemap
- Input mapvalue
- Scalarpublic static <A> Map<A,Double> divide(Map<A,Double> map1, Map<A,Double> map2)
A
- Key typemap1
- Input map 1map2
- Input map 2public static double[] divideNonSingular(double[] array1, double[] array2)
array1
- Numeratorarray2
- Denominatorpublic 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 occurrences are returnedpublic static double[] floor(double[] array)
double
array.array
- Array to be rounded downpublic static String formatAndRemoveTrailingZeros(double value, int numDecimals)
double
to String
(using '%f' modifier) and remove trailing zeros.value
- Input valuenumDecimals
- Maximum number of decimal digitspublic static double gcd(Collection<Double> collection)
collection
- Input collectiongcd(0, 0)
is equal to zero and gcd([])
is equal to onepublic 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 List<Double> listOf(double... values)
values
- Comma-separated input valuespublic static int[] maxIndexes(double[] array, Constants.SearchType searchType)
array
- Input arraysearchType
- Indicates whether the first, the last, or all maximum positions are returnedpublic static int[] maxIndexes(double[] array, Constants.SearchType searchType, double precisionFactor)
array
- Input arraysearchType
- Indicates whether the first, the last, or all maximum positions are returnedprecisionFactor
- Tolerance factor (values with absolute difference lower or equal than precisionFactor
will be considered equal)public static <A> Set<A> maxIndexes(Map<A,Double> map, Constants.SearchType searchType)
A
- Key typemap
- Input mapsearchType
- Indicates whether the first, the last, or all maximum positions are returnedpublic 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 int[][] maxMinIndexes(double[] array, Constants.SearchType searchType, double precisionFactor)
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 returnedprecisionFactor
- Tolerance factor (values with absolute difference lower or equal than precisionFactor
will be considered equal)public static double[] maxMinValues(Collection<Double> collection)
collection
- Input collectionpublic static double[] maxMinValues(double[] array)
array
- Input arraypublic static <A> double[] maxMinValues(Map<A,Double> map)
A
- map keymap
- Input mappublic static double maxValue(Collection<Double> collection)
collection
- Input collectionpublic static double maxValue(double[] array)
array
- Input arraypublic static double maxValue(double[][] array)
array
- Input arraypublic static <A> double maxValue(Map<A,Double> map)
A
- Key typemap
- Input mappublic static int[] minIndexes(double[] array, Constants.SearchType searchType)
array
- Input arraysearchType
- Indicates whether the first, the last, or all minimum positions are returnedpublic static <A> Set<A> minIndexes(Map<A,Double> map, Constants.SearchType searchType)
A
- Key typemap
- Input mapsearchType
- Indicates whether the first, the last, or all minimum positions are returnedpublic static double minValue(Collection<Double> collection)
collection
- Input collectionpublic static double minValue(double[] array)
array
- Input arraypublic static double minValue(double[][] matrix)
matrix
- Input matrixpublic static <A> double minValue(Map<A,Double> map)
A
- Key typemap
- Input mappublic static double[][] mult(double[][] matrix, double value)
matrix
- Input arrayvalue
- Scalarpublic 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 <A> Map<A,Double> mult(Map<A,Double> map, double value)
A
- Key typemap
- Input mapvalue
- Scalarpublic static <A> Map<A,Double> mult(Map<A,Double> map1, Map<A,Double> map2)
A
- Key typemap1
- Input map 1map2
- Input map 2public static double[] ones(int N)
N
- Number of elementsN
public static <A> Map<A,Double> ones(Set<A> identifiers)
A
- Key typeidentifiers
- Set of map keyspublic static DoubleMatrix2D read2DMatrixFromFile(File file)
file
- Input filepublic static void reverse(double[] array)
array
- Input arraypublic static double[] round(double[] array, int decimals)
double
given the number of
required decimal places.array
- Array to be roundeddecimals
- Decimal placespublic static double round(double number, int decimals)
double
given the number of
required decimal places.number
- Number to be roundeddecimals
- Decimal placespublic static double scalarProduct(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static <A> double scalarProduct(Map<A,Double> map1, Map<A,Double> map2)
A
- Key typemap1
- Input map 1map2
- Input map 2public 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 Set<Double> setOf(double... values)
values
- Comma-separated input valuespublic 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 std(Collection<Double> collection)
collection
- Collection of numberscollection
is emptypublic static double std(double[] array)
array
- Input arraypublic static <A> double std(Map<A,Double> map)
A
- Key typemap
- Input mappublic static double[] substract(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static double sum(Collection<Double> collection)
collection
- Input collectionpublic static double sum(double[] array)
array
- Input arraypublic static double sum(double[][] matrix)
matrix
- Input matrixpublic static double[] sum(double[] array1, double[] array2)
array1
- Input array 1array2
- Input array 2public static <A> double sum(Map<A,Double> map)
A
- Key typemap
- Input mappublic static <A> Map<A,Double> sum(Map<A,Double> map1, Map<A,Double> map2)
A
- Key typemap1
- Input map 1map2
- Input map 2public static double[] toArray(Collection<Double> list)
List
, Set
...) of Double
objects to a double
array.list
- Input listint
arraypublic static double[] toArray(String[] list)
String
with the values, into a double
array. Uses Double.parseDouble
to parse the number, an can raise the exceptions
that this method raiseslist
- Input listpublic static int[] toIntArray(double[] array)
double
array to an int
arrayarray
- Input arrayint
arraypublic static List<Double> toList(double[] array)
double
array to a list.array
- Input arrayDouble
objectspublic static String toString(double[] array)
array
- Input arraypublic 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
Copyright © 2018. All rights reserved.