Class Math2


  • public final class Math2
    extends Object

    Commonly needed math routines and methods that work over arrays and iterable.

    Author:
    David B. Bracewell
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double LOG_2
      The constant LOG_2.
    • Field Detail

      • LOG_2

        public static final double LOG_2
        The constant LOG_2.
    • Method Detail

      • auc

        public static double auc​(double[] x,
                                 double[] y)
        Calculates the AUC (Area Under the Curve)
        Parameters:
        x - the x
        y - the y
        Returns:
        the AUC
      • clip

        public static double clip​(double value,
                                  double min,
                                  double max)

        Clips a value to ensure it falls between the lower or upper bound of range.

        Parameters:
        value - the value to clip
        min - the lower bound of the range
        max - the upper bound of the range
        Returns:
        the clipped value
      • log2

        public static double log2​(double number)
        Calculates the base 2 log of a given number
        Parameters:
        number - the number to calculate the base 2 log of
        Returns:
        the base 2 log of the given number
      • maxBy

        public static <E> E maxBy​(E e1,
                                  E e2,
                                  Comparator<E> comparator)
      • minBy

        public static <E> E minBy​(E e1,
                                  E e2,
                                  Comparator<E> comparator)
      • rescale

        public static double rescale​(double value,
                                     double originalMin,
                                     double originalMax,
                                     double newMin,
                                     double newMax)

        Rescales a value from an old range to a new range, e.g. change the value 2 in a 1 to 5 scale to the value 3.25 in a 1 to 10 scale

        Parameters:
        value - the value to rescale
        originalMin - the lower bound of the original range
        originalMax - the upper bound of the original range
        newMin - the lower bound of the new range
        newMax - the upper bound of the new range
        Returns:
        the given value rescaled to fall between newMin and new Max
        Throws:
        IllegalArgumentException - if originalMax <= originalMin or newMax <= newMin
      • safeLog

        public static double safeLog​(double d)
        Safe log double.
        Parameters:
        d - the d
        Returns:
        the double
      • sum

        public static double sum​(Iterable<? extends Number> values)

        Sums the numbers in a given iterable treating them as doubles.

        Parameters:
        values - the iterable of numbers to sum
        Returns:
        the sum of the iterable
        Throws:
        NullPointerException - if the values are null
      • sum

        public static double sum​(double... values)

        Sums the numbers in the given array.

        Parameters:
        values - the values to sum
        Returns:
        the sum of the values
        Throws:
        NullPointerException - if the values are null
      • sum

        public static int sum​(int... values)

        Sums the numbers in the given array.

        Parameters:
        values - the values to sum
        Returns:
        the sum of the values
        Throws:
        NullPointerException - if the values are null
      • sum

        public static long sum​(long... values)

        Sums the numbers in the given array.

        Parameters:
        values - the values to sum
        Returns:
        the sum of the values
        Throws:
        NullPointerException - if the values are null
      • summaryStatistics

        public static EnhancedDoubleStatistics summaryStatistics​(double... values)

        Calculates the summary statistics for the values in the given array.

        Parameters:
        values - the values to calculate summary statistics over
        Returns:
        the summary statistics of the given array
        Throws:
        NullPointerException - if the values are null
      • summaryStatistics

        public static EnhancedDoubleStatistics summaryStatistics​(int... values)

        Calculates the summary statistics for the values in the given array.

        Parameters:
        values - the values to calculate summary statistics over
        Returns:
        the summary statistics of the given array
        Throws:
        NullPointerException - if the values are null
      • summaryStatistics

        public static EnhancedDoubleStatistics summaryStatistics​(long... values)

        Calculates the summary statistics for the values in the given array.

        Parameters:
        values - the values to calculate summary statistics over
        Returns:
        the summary statistics of the given array
        Throws:
        NullPointerException - if the values are null
      • summaryStatistics

        public static EnhancedDoubleStatistics summaryStatistics​(Iterable<? extends Number> values)

        Calculates the summary statistics for the values in the given iterable.

        Parameters:
        values - the values to calculate summary statistics over
        Returns:
        the summary statistics of the given iterable
        Throws:
        NullPointerException - if the iterable is null
      • tryParseDouble

        public static Double tryParseDouble​(String string)
        Try to parse a string into a double returning null if a parse error occurs
        Parameters:
        string - the string to parse
        Returns:
        the parsed Double value or null if not parsable
      • tryParseFloat

        public static Float tryParseFloat​(String string)
        Try to parse a string into a float returning null if a parse error occurs
        Parameters:
        string - the string to parse
        Returns:
        the parsed Float value or null if not parsable
      • tryParseInteger

        public static Integer tryParseInteger​(String string)
        Try to parse a string into a integer returning null if a parse error occurs
        Parameters:
        string - the string to parse
        Returns:
        the parsed Integer value or null if not parsable
      • tryParseLong

        public static Long tryParseLong​(String string)
        Try to parse a string into a long returning null if a parse error occurs
        Parameters:
        string - the string to parse
        Returns:
        the parsed long value or null if not parsable