|
SuanShu, a Java numerical and statistical library | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectcom.numericalmethod.suanshu.number.DoubleUtils
public class DoubleUtils
This class collects the utility functions to manipulate data of types double and int.
These functions are static and stateless.
| Nested Class Summary | |
|---|---|
static class |
DoubleUtils.RoundingScheme
the schemes available to round a number. |
| Method Summary | |
|---|---|
static double[] |
abs(double[] doubles)
Get the absolute values. |
static double |
absoluteError(double x1,
double x0)
Compute the absolute difference between x1 and x0. |
static boolean |
allZeros(double[] d,
double epsilon)
Check if a double[] array consists of all 0s, entry-by-entry. |
static boolean |
anyZero(double[] d,
double epsilon)
Check if a double[] array has any 0. |
static double[] |
ArrayList2doubleArray(java.util.ArrayList<java.lang.Double> list)
Convert an ArrayList |
static int[] |
ArrayList2intArray(java.util.ArrayList<java.lang.Integer> list)
Convert an ArrayList |
static int |
compare(double d1,
double d2)
Compare two doubles. |
static int |
compare(double d1,
double d2,
double precision)
Compare two doubles. |
static double[] |
concat(double[]... arr)
Concatenate arrays of doubles into one double[] array. |
static java.util.List<java.lang.Double> |
doubleArray2ArrayList(double... arr)
Convert an ArrayList to a double[] array. |
static int[] |
doubleArray2intArray(double... arr)
Convert a double[] array to an int[] array. |
static java.lang.String |
doubleArray2StringArray(double... arr)
Print out the numbers to a String buffer. |
static boolean |
equal(double[][] d1,
double[][] d2,
double precision)
Check if two 2D double[][] arrays are close enough, hence equal, entry-by-entry. |
static boolean |
equal(double[] d1,
double[] d2,
double precision)
Check if two double arrays are close enough, hence equal, entry-by-entry. |
static boolean |
equal(double d1,
double d2,
double precision)
Check if two doubles are close enough, hence equal. |
static boolean |
equal(int[] d1,
int[] d2)
Check if two int[] arrays are equal, entry-by-entry. |
static double[] |
foreach(double[] doubles,
UnivariateRealFunction f)
Get a double array in which each element is the result of applying the function f to the corresponding element in doubles. |
static java.util.ArrayList<java.lang.Integer> |
intArray2ArrayList(int[] ary)
Convert an int[] to ArrayList |
static double[] |
intArray2doubleArray(int... arr)
Convert an int[] array to a double[] array. |
static boolean |
isN(double x)
Check if a double is a number, i.e., it is not ∞ or NaN. |
static boolean |
isPow2(int n)
Check if an integer is a power of 2. |
static boolean |
isThereAnyDuplicate(double... arr)
Check if a double[] array contains any duplicates. |
static double |
max(double... doubles)
Get the maximum of the values. |
static int |
maxIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
Get the index to the maximum of the values. |
static int |
maxIndex(double... doubles)
Get the index to the maximum of the values. |
static double |
min(double... doubles)
Get the minimum of the values. |
static int |
minIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
Get the index to the minimum of the values. |
static int |
minIndex(double... doubles)
Get the index to 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 dir)
Round up or down a double to an integer. |
static double |
round(double d,
int scale)
Round a double to the precision specified. |
static int[] |
shellsort(double... arr)
Sort the input array using Shell sort. |
static double |
sum(double... doubles)
Get the sum of the values. |
static int |
sum(int... integers)
Get the sum of the values. |
static java.lang.String |
toString(double[][] arr)
Convert a double[][] array to the format that we can copy and paste to re-create the array in Java code. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static int compare(double d1,
double d2)
We use this function instead of Double.compare, because we want to treat 0.0 and -0.0 as the same.
For instance, we want to avoid that
Double.compare(0.0, -0.0) returns 1, and
DoubleUtils.compare(0.0, -0.0) returns 0.
d1 - a doubled2 - a double
d1 is numerically equal to d2, 1 if d1 > d2, -1 if d1 < d2
public static int compare(double d1,
double d2,
double precision)
d1 - a doubled2 - a doubleprecision - the precision used for the comparison, e.g., 1e-9
d1 is close enough to d2, 1 if d1 > d2, -1 if d1 < d2
public static boolean equal(double d1,
double d2,
double precision)
d1 - a doubled2 - a doubleprecision - the precision used for the comparison, e.g., 1e-9
true iff d1 is close enough to d2, false otherwise
public static boolean equal(double[] d1,
double[] d2,
double precision)
d1 - a double[]d2 - a double[]precision - the precision used for the comparison, e.g., 1e-9
true iff all entries in d1 are close enough to all entries in d2, false otherwise
public static boolean equal(double[][] d1,
double[][] d2,
double precision)
d1 - a double[][]d2 - a double[][]precision - the precision used for the comparison, e.g., 1e-9
true iff all entries in d1 are close enough to all entries in d2, false otherwise
public static boolean equal(int[] d1,
int[] d2)
d1 - a int[]d2 - a int[]
true iff all entries in d1 are the same as all entries in d2, false otherwisepublic static double max(double... doubles)
doubles - an array of doubles
public static double min(double... doubles)
doubles - an array of doubles
public static int maxIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
moveOnTies - 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 of doubles
public static int maxIndex(double... doubles)
doubles - an array of doubles
public static int minIndex(boolean moveOnTies,
int from,
int to,
double... doubles)
moveOnTies - 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 of doubles
public static int minIndex(double... doubles)
doubles - an array of doubles
public static double sum(double... doubles)
doubles - an array of doubles
public static int sum(int... integers)
integers - an array of ints
public static double[] abs(double[] doubles)
doubles - an array of doubles
public static double[] foreach(double[] doubles,
UnivariateRealFunction f)
f to the corresponding element in doubles.
doubles - an array of doublesf - the function to be applied to each element
public static double[] concat(double[]... arr)
The merged array is not sorted.
arr - an array of double[] arrays
public static void reverse(double... arr)
A common usage is to call this after Arrays.sort to sort the array in descending order.
arr - a double[] arraypublic static void reverse(int... arr)
A common usage is to call this after Arrays.sort to sort the array in descending order.
arr - an int[] arraypublic static int[] shellsort(double... arr)
arr - a double[] array
arr, according to which the inputs are sorted in ascending order
public static boolean allZeros(double[] d,
double epsilon)
d - doubles or a double[]epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
true iff all entries in d are close enough to 0, false otherwise
public static boolean anyZero(double[] d,
double epsilon)
d - doubles or a double[]epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
true iff an entry in d are close enough to 0, false otherwisepublic static boolean isN(double x)
∞ or NaN.
x - a double
true iff x is not ∞ or NaNpublic static boolean isPow2(int n)
n - an integer
true iff n is a power of 2.public static boolean isThereAnyDuplicate(double... arr)
arr - a double[] array
true iff there is a duplicate in the arraypublic static java.lang.String doubleArray2StringArray(double... arr)
arr - a double[] array
public static int[] doubleArray2intArray(double... arr)
arr - a double[] array
public static double[] intArray2doubleArray(int... arr)
arr - an int[] array
public static double[] ArrayList2doubleArray(java.util.ArrayList<java.lang.Double> list)
list - an ArrayListpublic static java.util.List<java.lang.Double> doubleArray2ArrayList(double... arr)
arr - a double[] array
public static int[] ArrayList2intArray(java.util.ArrayList<java.lang.Integer> list)
list - an ArrayListpublic static java.util.ArrayList<java.lang.Integer> intArray2ArrayList(int[] ary)
ary - an int[] array
public static double round(double d,
DoubleUtils.RoundingScheme dir)
d - the double to be roundeddir - round up or down or using Java default
public static double round(double d,
int scale)
d - a doublescale - number of decimal points
public static double absoluteError(double x1,
double x0)
x1 and x0.
This function is symmetric.
ε = | x1 - x0 |
x1 - x1x0 - x0
public static double relativeError(double x1,
double x0)
{x1, x0}.
This function is asymmetric.
ε = | (x1 - x0) / x0 | = | x1/x0 - 1 |
x1 - x1x0 - x0
public static java.lang.String toString(double[][] arr)
arr - a double[][]
|
SuanShu, a Java numerical and statistical library | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||