public class DoubleUtils
extends java.lang.Object
double and int.| Modifier and Type | Class and Description |
|---|---|
static class |
DoubleUtils.RoundingScheme
the schemes available to round a number.
|
| Modifier and Type | Method and Description |
|---|---|
static double |
absoluteError(double x1,
double x0)
Compute the absolute difference between
x1 and x0. |
static double[] |
collection2DoubleArray(java.util.Collection<? extends java.lang.Number> numbers)
Convert a collection of numbers to a
double array. |
static int[] |
collection2IntArray(java.util.Collection<java.lang.Integer> integers)
Convert a collection of
Integers to an int array. |
static long[] |
collection2LongArray(java.util.Collection<java.lang.Long> integers)
Convert a collection of
Longs to a long array. |
static int |
compare(double d1,
double d2,
double epsilon)
Compares two
doubles up to a precision. |
static double[] |
concat(double[]... arr)
Concatenate an array of arrays into one array.
|
static int[] |
doubleArray2intArray(double... arr)
Convert a
double array to an int array, rounding down if necessary. |
static java.util.List<java.lang.Double> |
doubleArray2List(double... arr)
Convert a
double array to a list. |
static boolean |
equal(double[][] d1,
double[][] d2,
double epsilon)
Check if two 2D arrays,
double[][], are close enough, hence equal, entry-by-entry. |
static boolean |
equal(double[] d1,
double[] d2,
double epsilon)
Check if two
double arrays are close enough, hence equal, entry-by-entry. |
static boolean |
equal(double d1,
double d2,
double epsilon)
Check if two
doubles are close enough, hence equal. |
static boolean |
equal(int[] d1,
int[] d2)
Check if two
int arrays, int[], are equal, entry-by-entry. |
static double[] |
foreach(double[] doubles,
UnivariateRealFunction f)
Apply a function f to each element in an array.
|
static boolean |
hasDuplicate(double[] arr,
double epsilon)
Check if a
double array contains any duplicates. |
static boolean |
hasZero(double[] d,
double epsilon)
Check if a
double array has any 0. |
static double[] |
intArray2doubleArray(int... arr)
Convert an
int array to a double array. |
static java.util.List<java.lang.Integer> |
intArray2List(int[] ary)
Convert an
int array to a list. |
static boolean |
isAllZeros(double[] d,
double epsilon)
Check if a
double array contains only 0s, entry-by-entry. |
static boolean |
isNegative(double d,
double epsilon)
Check if
d is negative. |
static boolean |
isNumber(double x)
Check if a
double is a number, i.e., it is not ∞ or NaN. |
static boolean |
isPositive(double d,
double epsilon)
Check if
d is positive. |
static boolean |
isPow2(int n)
Check if an integer is a power of 2.
|
static boolean |
isZero(double d,
double epsilon)
Check if
d is zero. |
static int |
maxIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
Get the index of the maximum of the values.
|
static int |
maxIndex(double... doubles)
Get the index of the maximum of the values.
|
static int |
minIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
Get the index of the minimum of the values.
|
static int |
minIndex(double... doubles)
Get the index of the minimum of the values.
|
static double |
relativeError(double x1,
double x0)
Compute the relative error for {x1, x0}.
|
static void |
reverse(double... arr)
Reverse a
double array. |
static void |
reverse(int... arr)
Reverse an
int array. |
static double |
round(double d,
DoubleUtils.RoundingScheme scheme)
Round up or down a number to an integer.
|
static double |
round(double d,
int scale)
Round a number to the precision specified.
|
static int[] |
shellsort(double... arr)
Sort an array using Shell sort.
|
static java.lang.String |
toString(double... arr)
Print out numbers to a string.
|
static java.lang.String |
toString(double[][] arr)
Print out a 2D array,
double[][] to a string. |
public static int compare(double d1,
double d2,
double epsilon)
doubles up to a precision.
This implementation is preferred to Double.compare(double, double) because
our implementation returns 0 (equality), i.e.,
DoubleUtils.compare(0.0, -0.0) returns 0;
Double.compare(0.0, -0.0) returns 1.d1 - a doubled2 - a doubleepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0d1 is close enough to d2; 1 if d1 > d2; -1 if d1 ≤ d2public static boolean isZero(double d,
double epsilon)
d is zero.d - a doubleepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if d is close enough to 0public static boolean isPositive(double d,
double epsilon)
d is positive.d - a doubleepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if d is bigger than 0 by epsilonpublic static boolean isNegative(double d,
double epsilon)
d is negative.d - a doubleepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if d is smaller than 0 by epsilonpublic static boolean equal(double d1,
double d2,
double epsilon)
doubles are close enough, hence equal.d1 - a doubled2 - a doubleepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if d1 is close enough to d2, false otherwisepublic static boolean equal(double[] d1,
double[] d2,
double epsilon)
double arrays are close enough, hence equal, entry-by-entry.d1 - a double[]d2 - a double[]epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if all entries in d1 are close enough to all entries in d2, false otherwisepublic static boolean equal(double[][] d1,
double[][] d2,
double epsilon)
double[][], are close enough, hence equal, entry-by-entry.d1 - a double[][]d2 - a double[][]epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if all entries in d1 are close enough to all entries in d2, false otherwisepublic static boolean equal(int[] d1,
int[] d2)
int arrays, int[], are equal, entry-by-entry.d1 - an int arrayd2 - an int arraytrue if all entries in d1 are the same as all entries in d2, false otherwisepublic static int maxIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
moveOnTies - true if prefer the later one on tiesfrom - the initial index of the range to be consideredto - 1 after the last index of the range to be considereddoubles - an array, double[]public static int maxIndex(double... doubles)
doubles - an array, double[]public static int minIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
moveOnTies - true if prefer the later one on tiesfrom - the initial index of the range to be consideredto - 1 after the last index of the range to be considereddoubles - an array, double[]public static int minIndex(double... doubles)
doubles - an array, double[]public static double[] foreach(double[] doubles,
UnivariateRealFunction f)
doubles - an array, double[]f - a function to be applied to each elementpublic static double[] concat(double[]... arr)
arr - an array, double[]public static void reverse(double... arr)
double array.arr - an array, double[]public static void reverse(int... arr)
int array.arr - an array, int[]public static int[] shellsort(double... arr)
arr - an array, double[]public static boolean isAllZeros(double[] d,
double epsilon)
double array contains only 0s, entry-by-entry.d - a double arrayepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if all entries in d are close enough to 0, false otherwisepublic static boolean hasZero(double[] d,
double epsilon)
double array has any 0.d - a double arrayepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if an entry in d are close enough to 0, false otherwisepublic static boolean isNumber(double x)
double is a number, i.e., it is not ∞ or NaN.x - a doubletrue if x is not ∞ or NaNpublic static boolean isPow2(int n)
n - an integertrue if n is a power of 2.public static boolean hasDuplicate(double[] arr,
double epsilon)
double array contains any duplicates.arr - a double arrayepsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0true if there is a duplicatepublic static int[] doubleArray2intArray(double... arr)
double array to an int array, rounding down if necessary.arr - a double arrayint arraypublic static double[] intArray2doubleArray(int... arr)
int array to a double array.arr - an int arraydouble arraypublic static double[] collection2DoubleArray(java.util.Collection<? extends java.lang.Number> numbers)
double array.numbers - the collection of numbersdouble array containing the numbers in the input collectionpublic static java.util.List<java.lang.Double> doubleArray2List(double... arr)
double array to a list.arr - a double arraypublic static int[] collection2IntArray(java.util.Collection<java.lang.Integer> integers)
Integers to an int array.integers - a collection of integersint array of the integerspublic static long[] collection2LongArray(java.util.Collection<java.lang.Long> integers)
Longs to a long array.integers - a collection of long integerslong array of the long integerspublic static java.util.List<java.lang.Integer> intArray2List(int[] ary)
int array to a list.ary - an int arraypublic static double round(double d,
DoubleUtils.RoundingScheme scheme)
d - a numberscheme - the rounding schemedoublepublic static double round(double d,
int scale)
d - a numberscale - the number of decimal pointspublic static double absoluteError(double x1,
double x0)
x1 and x0. This function is symmetric.
ε = | x1 - x0 |
x1 - x1x0 - x0public static double relativeError(double x1,
double x0)
ε = | (x1 - x0) / x0 | = | x1/x0 - 1 |
x1 - x1x0 - x0public static java.lang.String toString(double... arr)
arr - a double arrayString representation of the numberspublic static java.lang.String toString(double[][] arr)
double[][] to a string.arr - a double[][]