SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number
Class DoubleUtils

java.lang.Object
  extended by com.numericalmethod.suanshu.number.DoubleUtils

public class DoubleUtils
extends java.lang.Object

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 to a double[] array.
static int[] ArrayList2intArray(java.util.ArrayList<java.lang.Integer> list)
          Convert an ArrayList to a int[] array.
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

compare

public static int compare(double d1,
                          double d2)
Compare two doubles.

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.

Parameters:
d1 - a double
d2 - a double
Returns:
0 if d1 is numerically equal to d2, 1 if d1 > d2, -1 if d1 < d2

compare

public static int compare(double d1,
                          double d2,
                          double precision)
Compare two doubles.

Parameters:
d1 - a double
d2 - a double
precision - the precision used for the comparison, e.g., 1e-9
Returns:
0 if d1 is close enough to d2, 1 if d1 > d2, -1 if d1 < d2

equal

public static boolean equal(double d1,
                            double d2,
                            double precision)
Check if two doubles are close enough, hence equal.

Parameters:
d1 - a double
d2 - a double
precision - the precision used for the comparison, e.g., 1e-9
Returns:
true iff d1 is close enough to d2, false otherwise

equal

public static boolean equal(double[] d1,
                            double[] d2,
                            double precision)
Check if two double arrays are close enough, hence equal, entry-by-entry.

Parameters:
d1 - a double[]
d2 - a double[]
precision - the precision used for the comparison, e.g., 1e-9
Returns:
true iff all entries in d1 are close enough to all entries in d2, false otherwise

equal

public static boolean equal(double[][] d1,
                            double[][] d2,
                            double precision)
Check if two 2D double[][] arrays are close enough, hence equal, entry-by-entry.

Parameters:
d1 - a double[][]
d2 - a double[][]
precision - the precision used for the comparison, e.g., 1e-9
Returns:
true iff all entries in d1 are close enough to all entries in d2, false otherwise

equal

public static boolean equal(int[] d1,
                            int[] d2)
Check if two int[] arrays are equal, entry-by-entry.

Parameters:
d1 - a int[]
d2 - a int[]
Returns:
true iff all entries in d1 are the same as all entries in d2, false otherwise

max

public static double max(double... doubles)
Get the maximum of the values.

Parameters:
doubles - an array of doubles
Returns:
the biggest of the inputs

min

public static double min(double... doubles)
Get the minimum of the values.

Parameters:
doubles - an array of doubles
Returns:
the smallest of the inputs

maxIndex

public static int maxIndex(boolean moveOnTies,
                           int from,
                           int to,
                           double... doubles)
Get the index to the maximum of the values.

Parameters:
moveOnTies - prefer the later one on ties
from - the initial index of the range to be considered
to - 1 after the last index of the range to be considered
doubles - an array of doubles
Returns:
the index to the biggest of the inputs

maxIndex

public static int maxIndex(double... doubles)
Get the index to the maximum of the values.

Parameters:
doubles - an array of doubles
Returns:
the index to the biggest of the inputs

minIndex

public static int minIndex(boolean moveOnTies,
                           int from,
                           int to,
                           double... doubles)
Get the index to the minimum of the values.

Parameters:
moveOnTies - prefer the later one on ties
from - the initial index of the range to be considered
to - 1 after the last index of the range to be considered
doubles - an array of doubles
Returns:
the index to the smallest of the inputs

minIndex

public static int minIndex(double... doubles)
Get the index to the minimum of the values.

Parameters:
doubles - an array of doubles
Returns:
the index to the smallest of the inputs

sum

public static double sum(double... doubles)
Get the sum of the values.

Parameters:
doubles - an array of doubles
Returns:
the sum of the inputs

sum

public static int sum(int... integers)
Get the sum of the values.

Parameters:
integers - an array of ints
Returns:
the sum of the inputs

abs

public static double[] abs(double[] doubles)
Get the absolute values.

Parameters:
doubles - an array of doubles
Returns:
the absolute values of the inputs

foreach

public 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.

Parameters:
doubles - an array of doubles
f - the function to be applied to each element
Returns:
the function results of the inputs

concat

public static double[] concat(double[]... arr)
Concatenate arrays of doubles into one double[] array.

The merged array is not sorted.

Parameters:
arr - an array of double[] arrays
Returns:
the concatenated double[] array

reverse

public static void reverse(double... arr)
Reverse a double[] array.

A common usage is to call this after Arrays.sort to sort the array in descending order.

Parameters:
arr - a double[] array

reverse

public static void reverse(int... arr)
Reverse an int[] array.

A common usage is to call this after Arrays.sort to sort the array in descending order.

Parameters:
arr - an int[] array

shellsort

public static int[] shellsort(double... arr)
Sort the input array using Shell sort.

Parameters:
arr - a double[] array
Returns:
a permutation of arr, according to which the inputs are sorted in ascending order
See Also:
Wikipedia: Shell sort

allZeros

public static boolean allZeros(double[] d,
                               double epsilon)
Check if a double[] array consists of all 0s, entry-by-entry.

Parameters:
d - doubles or a double[]
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
Returns:
true iff all entries in d are close enough to 0, false otherwise

anyZero

public static boolean anyZero(double[] d,
                              double epsilon)
Check if a double[] array has any 0.

Parameters:
d - doubles or a double[]
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
Returns:
true iff an entry in d are close enough to 0, false otherwise

isN

public static boolean isN(double x)
Check if a double is a number, i.e., it is not or NaN.

Parameters:
x - a double
Returns:
true iff x is not or NaN

isPow2

public static boolean isPow2(int n)
Check if an integer is a power of 2.

Parameters:
n - an integer
Returns:
true iff n is a power of 2.

isThereAnyDuplicate

public static boolean isThereAnyDuplicate(double... arr)
Check if a double[] array contains any duplicates.

Parameters:
arr - a double[] array
Returns:
true iff there is a duplicate in the array

doubleArray2StringArray

public static java.lang.String doubleArray2StringArray(double... arr)
Print out the numbers to a String buffer.

Parameters:
arr - a double[] array
Returns:
a String of the numbers

doubleArray2intArray

public static int[] doubleArray2intArray(double... arr)
Convert a double[] array to an int[] array.

Parameters:
arr - a double[] array
Returns:
an int[] array

intArray2doubleArray

public static double[] intArray2doubleArray(int... arr)
Convert an int[] array to a double[] array.

Parameters:
arr - an int[] array
Returns:
a double[] array

ArrayList2doubleArray

public static double[] ArrayList2doubleArray(java.util.ArrayList<java.lang.Double> list)
Convert an ArrayList to a double[] array.

Parameters:
list - an ArrayList
Returns:
a double[] array

doubleArray2ArrayList

public static java.util.List<java.lang.Double> doubleArray2ArrayList(double... arr)
Convert an ArrayList to a double[] array.

Parameters:
arr - a double[] array
Returns:
an ArrayList

ArrayList2intArray

public static int[] ArrayList2intArray(java.util.ArrayList<java.lang.Integer> list)
Convert an ArrayList to a int[] array.

Parameters:
list - an ArrayList
Returns:
a int[] array

intArray2ArrayList

public static java.util.ArrayList<java.lang.Integer> intArray2ArrayList(int[] ary)
Convert an int[] to ArrayList.

Parameters:
ary - an int[] array
Returns:
an ArrayList

round

public static double round(double d,
                           DoubleUtils.RoundingScheme dir)
Round up or down a double to an integer.

Parameters:
d - the double to be rounded
dir - round up or down or using Java default
Returns:
a near integer represented in double

round

public static double round(double d,
                           int scale)
Round a double to the precision specified.

Parameters:
d - a double
scale - number of decimal points
Returns:
a rounded double

absoluteError

public static double absoluteError(double x1,
                                   double x0)
Compute the absolute difference between x1 and x0. This function is symmetric.
ε = | x1 - x0 |

Parameters:
x1 - x1
x0 - x0
Returns:
the absolute error

relativeError

public static double relativeError(double x1,
                                   double x0)
Compute the relative error for {x1, x0}. This function is asymmetric.
ε = | (x1 - x0) / x0 | = | x1/x0 - 1 |

Parameters:
x1 - x1
x0 - x0
Returns:
the relative error

toString

public 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.

Parameters:
arr - a double[][]
Returns:
the String representation of the array

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.