Package dev.nm.number

Class DoubleUtils


  • public final class DoubleUtils
    extends Object
    These are the utility functions to manipulate double and int.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  DoubleUtils.ifelse
      Return a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is true or false.
      static class  DoubleUtils.RoundingScheme
      the available schemes to round a number
      static interface  DoubleUtils.which
      Decide whether x satisfies the boolean test.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String CSV_SEPARATOR
      The default separator for CSV file parsing.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double absoluteError​(double x1, double x0)
      Compute the absolute difference between x1 and x0.
      static double bound​(double value, double lower, double upper)
      Bounds a given value by a given range.
      static double[] collection2DoubleArray​(Collection<? extends Number> numbers)
      Convert a collection of numbers to a double array.
      static int[] collection2IntArray​(Collection<Integer> integers)
      Convert a collection of Integers to an int array.
      static long[] collection2LongArray​(Collection<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 double[][] copy2D​(double[][] raw)
      Copies a 2D array.
      static double[] cumsum​(double[] arr)
      Gets the cumulative sums of the elements in an array.
      static int[] cumsum​(int[] arr)
      Gets the cumulative sums of the elements in an array.
      static double[] diff​(double[] arr)
      Gets the first differences of an array.
      static double[][] diff​(double[][] arr)
      Gets the first differences of an array of vectors.
      static double[][] diff​(double[][] arr, int lag, int order)
      Gets the lagged and iterated differences of vectors.
      static double[] diff​(double[] arr, int lag, int order)
      Gets the lagged and iterated differences.
      static int[] doubleArray2intArray​(double... arr)
      Convert a double array to an int array, rounding down if necessary.
      static List<Double> doubleArray2List​(double... arr)
      Convert a double array to a list.
      static double doubleMod​(double x, double y)
      Double modulo, analogous to x % y when x and y are ints.
      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 univariate 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[] ifelse​(double[] arr, DoubleUtils.ifelse selection)
      Return a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is true or false.
      static double[] intArray2doubleArray​(int... arr)
      Convert an int array to a double array.
      static List<Integer> intArray2List​(int[] arr)
      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 &infin; 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 void leftShift​(double... arr)
      Perform a in-memory left-shift (by 1 cell} to an array.
      static void leftShift​(double[] arr, int k)
      Perform a in-memory right-shift (by k cells} to an array.
      static double[] leftShiftCopy​(double... arr)
      Get a left shifted (by 1 cell) copy of an array.
      static double[] leftShiftCopy​(double[] arr, int k)
      Get a left shifted (by k cells) copy of an array.
      static int maxIndex​(boolean moveOnTies, int from, int to, double... doubles)
      Get the index of the maximum of the values, skipping Double.NaN.
      static int maxIndex​(double... doubles)
      Get the index of the maximum of the values, skipping Double.NaN.
      static int minIndex​(boolean moveOnTies, int from, int to, double... doubles)
      Get the index of the minimum of the values, skipping Double.NaN.
      static int minIndex​(double... doubles)
      Get the index of the minimum of the values, skipping Double.NaN.
      static double[] noNaN​(double[] doubles)
      Remove the NaN from an array.
      static int[] order​(double[] arr)
      Returns a permutation which rearranges its first argument into ascending or descending order.
      static int[] order​(double[] arr, boolean ascending)
      Returns a permutation which rearranges its first argument into ascending or descending order.
      static double[] readCSV1d​(InputStream stream)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array.
      static double[] readCSV1d​(InputStream stream, boolean hasIndex, boolean hasHeading)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array.
      static double[] readCSV1d​(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array, with a given separator which overrides the default separator.
      static double[] readCSV1d​(String fileName)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array.
      static double[] readCSV1d​(String fileName, boolean hasIndex, boolean hasHeading)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array.
      static double[] readCSV1d​(String fileName, boolean hasIndex, boolean hasHeading, String separator)
      Read a single-column CSV file (output by write.csv from R) into a 1-D double array, with a given separator which overrides the default separator.
      static double[][] readCSV2d​(InputStream stream)
      Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array.
      static double[][] readCSV2d​(InputStream stream, boolean hasIndex, boolean hasHeading)
      Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array.
      static double[][] readCSV2d​(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)
      Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array, with a given separator which overrides the default separator.
      static double[][] readCSV2d​(String fileName)
      Read a multi-column CSV file (output by write.csv from R) into a 2-D double array.
      static double[][] readCSV2d​(String fileName, boolean hasIndex, boolean hasHeading)
      Read a multi-column CSV file (output by write.csv from R) into a 2-D double array.
      static double[][] readCSV2d​(String fileName, boolean hasIndex, boolean hasHeading, String separator)
      Read a multi-column CSV file (output by write.csv from R) into a 2-D double array, with a given separator which overrides the default separator.
      static double relativeError​(double x1, double x0)
      Compute the relative error for {x1, x0}.
      static double[] rep​(double value, int times)
      Generates an array of doubles of repeated values.
      static int[] rep​(int value, int times)
      Generates an array of ints of repeated values.
      static void reverse​(double... arr)
      Reverse a double array.
      static void reverse​(int... arr)
      Reverse an int array.
      static double[] reverseCopy​(double... arr)
      Get a reversed copy of a double array.
      static int[] reverseCopy​(int... arr)
      Get a reversed copy of a int array.
      static void reverseRange​(double[] array, int fromIndex, int toIndex)
      Reverses a range of elements in an array.
      static void rightShift​(double... arr)
      Perform a in-memory right-shift (by 1 cell} to an array.
      static void rightShift​(double[] arr, int k)
      Perform a in-memory right-shift (by k cells} to an array.
      static double[] rightShiftCopy​(double... arr)
      Get a right shifted (by 1 cell) copy of an array.
      static double[] rightShiftCopy​(double[] arr, int k)
      Get a right shifted (by k cells) copy of an array.
      static double round​(double d, int scale)
      Round a number to the precision specified.
      static double round​(double d, DoubleUtils.RoundingScheme scheme)
      Round up or down a number to an integer.
      static double[] select​(double[] arr, DoubleUtils.which test)
      Select the array elements which satisfy the boolean test.
      static int[] select​(int[] arr, DoubleUtils.which test)
      Select the array elements which satisfy the boolean test.
      static double[] seq​(double from, double to, double inc)
      Generates a sequence of doubles from from up to to with increments inc.
      static double[] seq​(double from, double to, int n)
      Generate a sequence of n equi-spaced double values, from start to end (inclusive).
      static double[] seq​(int n, double start, double inc)
      Generate a sequence of double values with a given start value and a given constant increment.
      static int[] seq​(int from, int to)
      Generates a sequence of ints from from up to to with increments 1.
      static int[] seq​(int from, int to, int inc)
      Generates a sequence of ints from from up to to with increments inc.
      static int[] shellsort​(double... arr)
      Sort an array using Shell sort.
      static double[] subarray​(double[] arr, int[] indices)
      Get a sub-array of the original array with the given indices.
      static int[] subarray​(int[] arr, int[] indices)
      Get a sub-array of the original array with the given indices.
      static double[] toPrimitive​(Double[] arr)
      Convert a Double array to a primitive double array.
      static String toString​(double... arr)
      Print out numbers to a string.
      static String toString​(double[][] arr)
      Print out a 2D array, double[][] to a string.
      static int[] which​(double[] arr, DoubleUtils.which test)
      Get the indices of the array elements which satisfy the boolean test.
      static int[] which​(int[] arr, DoubleUtils.which test)
      Get the indices of the array elements which satisfy the boolean test.
    • Field Detail

    • Method Detail

      • compare

        public static int compare​(double d1,
                                  double d2,
                                  double epsilon)
        Compares two 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.
        Parameters:
        d1 - a double
        d2 - a double
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        0 if d1 is close enough to d2; 1 if d1 > d2; -1 if d1 ≤ d2
      • isZero

        public static boolean isZero​(double d,
                                     double epsilon)
        Check if d is zero.
        Parameters:
        d - a double
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if d is close enough to 0
      • isPositive

        public static boolean isPositive​(double d,
                                         double epsilon)
        Check if d is positive.
        Parameters:
        d - a double
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if d is bigger than 0 by epsilon
      • isNegative

        public static boolean isNegative​(double d,
                                         double epsilon)
        Check if d is negative.
        Parameters:
        d - a double
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if d is smaller than 0 by epsilon
      • equal

        public static boolean equal​(double d1,
                                    double d2,
                                    double epsilon)
        Check if two doubles are close enough, hence equal.
        Parameters:
        d1 - a double
        d2 - a double
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if d1 is close enough to d2, false otherwise
      • equal

        public static boolean equal​(double[] d1,
                                    double[] d2,
                                    double epsilon)
        Check if two double arrays are close enough, hence equal, entry-by-entry.
        Parameters:
        d1 - a double[]
        d2 - a double[]
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if all entries in d1 are close enough to all entries in d2, false otherwise
      • equal

        public static boolean equal​(double[][] d1,
                                    double[][] d2,
                                    double epsilon)
        Check if two 2D arrays, double[][], are close enough, hence equal, entry-by-entry.
        Parameters:
        d1 - a double[][]
        d2 - a double[][]
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if 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, int[], are equal, entry-by-entry.
        Parameters:
        d1 - an int array
        d2 - an int array
        Returns:
        true if all entries in d1 are the same as all entries in d2, false otherwise
      • maxIndex

        public static int maxIndex​(boolean moveOnTies,
                                   int from,
                                   int to,
                                   double... doubles)
        Get the index of the maximum of the values, skipping Double.NaN.
        Parameters:
        moveOnTies - true if 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, double[]
        Returns:
        the index of the biggest number
      • maxIndex

        public static int maxIndex​(double... doubles)
        Get the index of the maximum of the values, skipping Double.NaN.
        Parameters:
        doubles - an array, double[]
        Returns:
        the index of the biggest number
      • minIndex

        public static int minIndex​(boolean moveOnTies,
                                   int from,
                                   int to,
                                   double... doubles)
        Get the index of the minimum of the values, skipping Double.NaN.
        Parameters:
        moveOnTies - true if 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, double[]
        Returns:
        the index of the smallest number
      • minIndex

        public static int minIndex​(double... doubles)
        Get the index of the minimum of the values, skipping Double.NaN.
        Parameters:
        doubles - an array, double[]
        Returns:
        the index of the smallest number
      • foreach

        public static double[] foreach​(double[] doubles,
                                       UnivariateRealFunction f)
        Apply a univariate function f to each element in an array. For more complicated cases, use BruteForce.
        Parameters:
        doubles - an array, double[]
        f - a function to be applied to each element
        Returns:
        the function outputs
      • concat

        public static double[] concat​(double[]... arr)
        Concatenate an array of arrays into one array. The concatenated array is not sorted.
        Parameters:
        arr - an array, double[]
        Returns:
        the concatenated array
      • reverse

        public static void reverse​(double... arr)
        Reverse a double array.
        Parameters:
        arr - an array, double[]
      • reverseRange

        public static void reverseRange​(double[] array,
                                        int fromIndex,
                                        int toIndex)
        Reverses a range of elements in an array.
        Parameters:
        array - the array
        fromIndex - the index of the first element (inclusive)
        toIndex - the index of the last element (exclusive)
      • reverseCopy

        public static double[] reverseCopy​(double... arr)
        Get a reversed copy of a double array.
        Parameters:
        arr - an array, double[]
        Returns:
        the reversed copy
      • reverse

        public static void reverse​(int... arr)
        Reverse an int array.
        Parameters:
        arr - an array, int[]
      • reverseCopy

        public static int[] reverseCopy​(int... arr)
        Get a reversed copy of a int array.
        Parameters:
        arr - an array, int[]
        Returns:
        the reversed copy
      • leftShiftCopy

        public static double[] leftShiftCopy​(double... arr)
        Get a left shifted (by 1 cell) copy of an array. The rightmost element is filled by a 0.
        Parameters:
        arr - an array, double[]
        Returns:
        a left shifted array
      • leftShiftCopy

        public static double[] leftShiftCopy​(double[] arr,
                                             int k)
        Get a left shifted (by k cells) copy of an array. The rightmost elements are filled by zeros.
        Parameters:
        arr - an array, double[]
        k - the number of cells to be shifted
        Returns:
        a left shifted array
      • leftShift

        public static void leftShift​(double... arr)
        Perform a in-memory left-shift (by 1 cell} to an array. The leftmost element is filled by a zero. No temporary array will be created during the operation.
        Parameters:
        arr - an array, double[]
      • leftShift

        public static void leftShift​(double[] arr,
                                     int k)
        Perform a in-memory right-shift (by k cells} to an array. The leftmost elements are filled by zeros. No temporary array will be created during the operation.
        Parameters:
        arr - an array, double[]
        k - the number of cells to be shifted
      • rightShiftCopy

        public static double[] rightShiftCopy​(double... arr)
        Get a right shifted (by 1 cell) copy of an array. The leftmost element is filled by a 0.
        Parameters:
        arr - an array, double[]
        Returns:
        a right shifted array
      • rightShiftCopy

        public static double[] rightShiftCopy​(double[] arr,
                                              int k)
        Get a right shifted (by k cells) copy of an array. The leftmost elements are filled by zeros.
        Parameters:
        arr - an array, double[]
        k - the number of cells to be shifted
        Returns:
        a right shifted array
      • rightShift

        public static void rightShift​(double... arr)
        Perform a in-memory right-shift (by 1 cell} to an array. The leftmost element is filled by a zero. No temporary array will be created during the operation.
        Parameters:
        arr - an array, double[]
      • rightShift

        public static void rightShift​(double[] arr,
                                      int k)
        Perform a in-memory right-shift (by k cells} to an array. The leftmost elements are filled by zeros. No temporary array will be created during the operation.
        Parameters:
        arr - an array, double[]
        k - the number of cells to be shifted
      • shellsort

        public static int[] shellsort​(double... arr)
        Sort an array using Shell sort.
        Parameters:
        arr - an array, double[]
        Returns:
        the order of the original array; side effect: the input arr is sorted in ascending order
        See Also:
        Wikipedia: Shell sort
      • isAllZeros

        public static boolean isAllZeros​(double[] d,
                                         double epsilon)
        Check if a double array contains only 0s, entry-by-entry.
        Parameters:
        d - a double array
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if all entries in d are close enough to 0, false otherwise
      • hasZero

        public static boolean hasZero​(double[] d,
                                      double epsilon)
        Check if a double array has any 0.
        Parameters:
        d - a double array
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if an entry in d are close enough to 0, false otherwise
      • isNumber

        public static boolean isNumber​(double x)
        Check if a double is a number, i.e., it is not &infin; or NaN.
        Parameters:
        x - a double
        Returns:
        true if x is not &infin; or NaN
      • isPow2

        public static boolean isPow2​(int n)
        Check if an integer is a power of 2.
        Parameters:
        n - an integer
        Returns:
        true if n is a power of 2.
      • hasDuplicate

        public static boolean hasDuplicate​(double[] arr,
                                           double epsilon)
        Check if a double array contains any duplicates.
        Parameters:
        arr - a double array
        epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
        Returns:
        true if there is a duplicate
      • doubleArray2intArray

        public static int[] doubleArray2intArray​(double... arr)
        Convert a double array to an int array, rounding down if necessary.
        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
      • collection2DoubleArray

        public static double[] collection2DoubleArray​(Collection<? extends Number> numbers)
        Convert a collection of numbers to a double array.
        Parameters:
        numbers - the collection of numbers
        Returns:
        a double array containing the numbers in the input collection
      • doubleArray2List

        public static List<Double> doubleArray2List​(double... arr)
        Convert a double array to a list.
        Parameters:
        arr - a double array
        Returns:
        a list of the numbers
      • collection2IntArray

        public static int[] collection2IntArray​(Collection<Integer> integers)
        Convert a collection of Integers to an int array.
        Parameters:
        integers - a collection of integers
        Returns:
        an int array of the integers
      • collection2LongArray

        public static long[] collection2LongArray​(Collection<Long> integers)
        Convert a collection of Longs to a long array.
        Parameters:
        integers - a collection of long integers
        Returns:
        a long array of the long integers
      • intArray2List

        public static List<Integer> intArray2List​(int[] arr)
        Convert an int array to a list.
        Parameters:
        arr - an int array
        Returns:
        a list of the integers
      • toPrimitive

        public static double[] toPrimitive​(Double[] arr)
        Convert a Double array to a primitive double array. Double.NaN is used when an input Double instance is null.
        Parameters:
        arr - a Double array
        Returns:
        the primitive array
      • round

        public static double round​(double d,
                                   DoubleUtils.RoundingScheme scheme)
        Round up or down a number to an integer.
        Parameters:
        d - a number
        scheme - the rounding scheme
        Returns:
        a near integer in double
      • round

        public static double round​(double d,
                                   int scale)
        Round a number to the precision specified.
        Parameters:
        d - a number
        scale - the number of decimal points
        Returns:
        an approximation of the number
      • 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 String toString​(double... arr)
        Print out numbers to a string.
        Parameters:
        arr - a double array
        Returns:
        a String representation of the numbers
      • toString

        public static String toString​(double[][] arr)
        Print out a 2D array, double[][] to a string.
        Parameters:
        arr - a double[][]
        Returns:
        the string representation of the array
      • rep

        public static double[] rep​(double value,
                                   int times)
        Generates an array of doubles of repeated values.
        Parameters:
        value - the repeated value
        times - the length of the array
        Returns:
        an array of repetitions of value
      • rep

        public static int[] rep​(int value,
                                int times)
        Generates an array of ints of repeated values.
        Parameters:
        value - the repeated value
        times - the length of the array
        Returns:
        an array of repetitions of value
      • seq

        public static double[] seq​(int n,
                                   double start,
                                   double inc)
        Generate a sequence of double values with a given start value and a given constant increment.
        Parameters:
        n - the total number of values
        start - the first value
        inc - the increment
        Returns:
        the sequence
      • seq

        public static double[] seq​(double from,
                                   double to,
                                   double inc)
        Generates a sequence of doubles from from up to to with increments inc. The last number in the sequence is smaller than or equal to to for positive inc. The last number in the sequence is bigger than or equal to to for negative inc.
        Parameters:
        from - the first number in the sequence
        to - the bound of the sequence
        inc - the increment
        Returns:
        a sequence of doubles
      • seq

        public static double[] seq​(double from,
                                   double to,
                                   int n)
        Generate a sequence of n equi-spaced double values, from start to end (inclusive).
        Parameters:
        from - the first value
        to - the last value
        n - the total number of values
        Returns:
        an array of n equi-spaced values
      • seq

        public static int[] seq​(int from,
                                int to,
                                int inc)
        Generates a sequence of ints from from up to to with increments inc. The last number in the sequence is smaller than or equal to to for positive inc. The last number in the sequence is bigger than or equal to to for negative inc.
        Parameters:
        from - the first number in the sequence
        to - the bound of the sequence
        inc - the increment
        Returns:
        a sequence of ints
      • seq

        public static int[] seq​(int from,
                                int to)
        Generates a sequence of ints from from up to to with increments 1. That is, [from, to], inclusively.
        Parameters:
        from - the first number in the sequence
        to - the bound of the sequence
        Returns:
        a sequence of ints
      • diff

        public static double[] diff​(double[] arr,
                                    int lag,
                                    int order)
        Gets the lagged and iterated differences.
        Parameters:
        arr - an array, double[]
        lag - an integer indicating which lag to use
        order - an integer indicating the order of the difference. This is the number of times diff is applied to the data. E.g., diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).
        Returns:
        the lagged and iterated differences
      • diff

        public static double[] diff​(double[] arr)
        Gets the first differences of an array.
        Parameters:
        arr - an array, double[]
        Returns:
        the first differences
      • diff

        public static double[][] diff​(double[][] arr,
                                      int lag,
                                      int order)
        Gets the lagged and iterated differences of vectors.
        Parameters:
        arr - a double[][]; row view; must not be jagged
        lag - an integer indicating which lag to use
        order - an integer indicating the order of the difference. This is the number of times diff is applied to the data. E.g., diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).
        Returns:
        the lagged and iterated differences
      • diff

        public static double[][] diff​(double[][] arr)
        Gets the first differences of an array of vectors.
        Parameters:
        arr - a double[][]; row view; must not be jagged
        Returns:
        the first differences
      • cumsum

        public static double[] cumsum​(double[] arr)
        Gets the cumulative sums of the elements in an array.
        Parameters:
        arr - an array, double[]
        Returns:
        cumsum
      • cumsum

        public static int[] cumsum​(int[] arr)
        Gets the cumulative sums of the elements in an array.
        Parameters:
        arr - an array, int[]
        Returns:
        cumsum
      • ifelse

        public static double[] ifelse​(double[] arr,
                                      DoubleUtils.ifelse selection)
        Return a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is true or false.
        Parameters:
        arr - an array
        selection - the boolean test to decide true or false and return values
        Returns:
        "yes" or "no" return values
      • which

        public static int[] which​(double[] arr,
                                  DoubleUtils.which test)
        Get the indices of the array elements which satisfy the boolean test.
        Parameters:
        arr - an array. double[]
        test - the boolean test to decide true or false (which to select)
        Returns:
        the indices of the satisfying elements
      • which

        public static int[] which​(int[] arr,
                                  DoubleUtils.which test)
        Get the indices of the array elements which satisfy the boolean test.
        Parameters:
        arr - an array. int[]
        test - the boolean test to decide true or false (which to select)
        Returns:
        the indices of the satisfying elements
      • select

        public static double[] select​(double[] arr,
                                      DoubleUtils.which test)
        Select the array elements which satisfy the boolean test. R does not have a 'select' function. The R-equivalent is arr[which(...]}.
        Parameters:
        arr - an array, double[]
        test - the boolean test to determine which elements to be selected
        Returns:
        the satisfying elements
      • select

        public static int[] select​(int[] arr,
                                   DoubleUtils.which test)
        Select the array elements which satisfy the boolean test. R does not have a 'select' function. The R-equivalent is arr[which(...]}.
        Parameters:
        arr - an array, int[]
        test - the boolean test to determine which elements to be selected
        Returns:
        the satisfying elements
      • noNaN

        public static double[] noNaN​(double[] doubles)
        Remove the NaN from an array.
        Parameters:
        doubles - an array, double[]
        Returns:
        the same array but with NaNs removed
      • subarray

        public static double[] subarray​(double[] arr,
                                        int[] indices)
        Get a sub-array of the original array with the given indices. The R-equivalent is arr[indices].
        Parameters:
        arr - an array, double[]
        indices - an array of indices to select
        Returns:
        arr[indices].
      • subarray

        public static int[] subarray​(int[] arr,
                                     int[] indices)
        Get a sub-array of the original array with the given indices. The R-equivalent is arr[indices].
        Parameters:
        arr - an array, int[]
        indices - an array of indices to select
        Returns:
        arr[indices].
      • order

        public static int[] order​(double[] arr,
                                  boolean ascending)
        Returns a permutation which rearranges its first argument into ascending or descending order. The R-equivalent is order(...}.
        Parameters:
        arr - an array, int[]
        ascending - true if arranging elements in ascending order; false if descending order
        Returns:
        the order of the original array
      • order

        public static int[] order​(double[] arr)
        Returns a permutation which rearranges its first argument into ascending or descending order. The R-equivalent is order(...}.
        Parameters:
        arr - an array, int[]
        Returns:
        the order of the original array
      • readCSV2d

        public static double[][] readCSV2d​(String fileName)
                                    throws IOException
        Read a multi-column CSV file (output by write.csv from R) into a 2-D double array. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use the default separator.
        Parameters:
        fileName - the name of the CSV file
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV2d

        public static double[][] readCSV2d​(String fileName,
                                           boolean hasIndex,
                                           boolean hasHeading)
                                    throws IOException
        Read a multi-column CSV file (output by write.csv from R) into a 2-D double array. The default separator is used.
        Parameters:
        fileName - the name of the CSV file
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV2d

        public static double[][] readCSV2d​(String fileName,
                                           boolean hasIndex,
                                           boolean hasHeading,
                                           String separator)
                                    throws IOException
        Read a multi-column CSV file (output by write.csv from R) into a 2-D double array, with a given separator which overrides the default separator.
        Parameters:
        fileName - the name of the CSV file
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        separator - the separator
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV2d

        public static double[][] readCSV2d​(InputStream stream)
                                    throws IOException
        Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use the default separator.
        Parameters:
        stream - the CSV input stream
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV2d

        public static double[][] readCSV2d​(InputStream stream,
                                           boolean hasIndex,
                                           boolean hasHeading)
                                    throws IOException
        Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array. The default separator is used.
        Parameters:
        stream - the CSV input stream
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV2d

        public static double[][] readCSV2d​(InputStream stream,
                                           boolean hasIndex,
                                           boolean hasHeading,
                                           String separator)
                                    throws IOException
        Read a multi-column CSV stream (output by write.csv from R) into a 2-D double array, with a given separator which overrides the default separator.
        Parameters:
        stream - the CSV input stream
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        separator - the separator
        Returns:
        the numbers in the CSV file as a double[][]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(String fileName)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use the default separator.
        Parameters:
        fileName - the name of the CSV file
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(String fileName,
                                         boolean hasIndex,
                                         boolean hasHeading)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array. The default separator is used.
        Parameters:
        fileName - the name of the CSV file
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(String fileName,
                                         boolean hasIndex,
                                         boolean hasHeading,
                                         String separator)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array, with a given separator which overrides the default separator.
        Parameters:
        fileName - the name of the CSV file
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        separator - the separator
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(InputStream stream)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use the default separator.
        Parameters:
        stream - the CSV input stream
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(InputStream stream,
                                         boolean hasIndex,
                                         boolean hasHeading)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array. The default separator is used.
        Parameters:
        stream - the CSV input stream
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • readCSV1d

        public static double[] readCSV1d​(InputStream stream,
                                         boolean hasIndex,
                                         boolean hasHeading,
                                         String separator)
                                  throws IOException
        Read a single-column CSV file (output by write.csv from R) into a 1-D double array, with a given separator which overrides the default separator.
        Parameters:
        stream - the CSV input stream
        hasIndex - whether or not the file contains an index column
        hasHeading - whether or not the file contains a heading row
        separator - the separator
        Returns:
        the numbers in the CSV file as a double[]
        Throws:
        IOException - when error has occurred while reading the file
      • copy2D

        public static double[][] copy2D​(double[][] raw)
        Copies a 2D array.
        Parameters:
        raw - a 2D array
        Returns:
        a copy of the a 2D array
      • doubleMod

        public static double doubleMod​(double x,
                                       double y)
        Double modulo, analogous to x % y when x and y are ints.
        Parameters:
        x - a double
        y - a double
        Returns:
        x % y
      • bound

        public static double bound​(double value,
                                   double lower,
                                   double upper)
        Bounds a given value by a given range. This is equivalent to
         
         Math.min(Math.max(value, lower), upper)
         
         
        Parameters:
        value - the original value
        lower - the lower bound of the range
        upper - the upper bound of the range
        Returns:
        the bounded value