Package elki.math

Class MathUtil


  • public final class MathUtil
    extends java.lang.Object
    A collection of math related utility functions.
    Since:
    0.1
    Author:
    Arthur Zimek, Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double DEG2RAD
      Constant for degrees to radians.
      static int[] EMPTY_INTS
      Empty integer array.
      static double EULERMASCHERONI
      Euler–Mascheroni constant.
      static double HALFPI
      Half the value of Pi.
      static double LOG_ONE_BY_SQRTTWOPI
      Precomputed value of log(1 / sqrt(2 * pi)) = -.5 * log(2*pi).
      static double LOG10
      Natural logarithm of 10.
      static double LOG2
      Logarithm of 2 to the basis e, for logarithm conversion.
      static double LOG3
      Logarithm of 3 to the basis e, for logarithm conversion.
      static double LOGLOG2
      log(log(2))
      static double LOGPI
      log(PI).
      static double LOGPIHALF
      log(PI) / 2.
      static double LOGSQRTTWOPI
      log(sqrt(2*PI)).
      static double LOGTWOPI
      log(2*PI).
      static double ONE_BY_LOG2
      1. / log(2)
      static double ONE_BY_SQRTPI
      Precomputed value of 1 / sqrt(pi).
      static double ONE_BY_SQRTTWOPI
      Precomputed value of 1 / sqrt(2 * pi).
      static double ONE_THIRD
      One third.
      static double ONEHALFPI
      1.5 times Pi.
      static double PISQUARE
      Pi squared
      static double QUARTERPI
      One quarter of Pi.
      static double RAD2DEG
      Constant for radians to degrees.
      static double SQRT2
      Square root of 2.
      static double SQRT3
      Square root of 3.
      static double SQRT5
      Square root of 5.
      static double SQRTHALF
      Square root of 0.5 == 1 / sqrt(2).
      static double SQRTHALFPI
      Constant for sqrt(pi/2)
      static double SQRTPI
      Square root of Pi.
      static double SQRTTHIRD
      Square root of one third.
      static double SQRTTWOPI
      Square root of two times Pi.
      static double TWOPI
      Two times Pi.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MathUtil()
      Fake constructor for static class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double approximateBinomialCoefficient​(int n, int k)
      Binomial coefficent, also known as "n choose k").
      static double approximateFactorial​(int n)
      Compute the Factorial of n, often written as c!
      static long binomialCoefficient​(long n, long k)
      Binomial coefficient, also known as "n choose k".
      static double clamp​(double value, double min, double max)
      Clamp values to a given minimum and maximum.
      static double deg2rad​(double deg)
      Convert Degree to Radians.
      static double exp​(double d)
      Delegate to FastMath.exp.
      static long factorial​(int n)
      Compute the Factorial of n, often written as c!
      static double floatToDoubleLower​(float f)
      Return the largest double that rounds up to this float.
      static double floatToDoubleUpper​(float f)
      Return the largest double that rounds down to this float.
      static int ipowi​(int x, int p)
      Fast loop for computing pow(x, p) for p >= 0 integer and x integer.
      static double log1mexp​(double x)
      More stable than FastMath.log(1 - FastMath.exp(x))
      static double log2​(double x)
      Compute the base 2 logarithm.
      static double max​(double a, double b)
      Binary max, without handling of special values.
      static double max​(double a, double b, double c)
      Ternary max, without handling of special values.
      static double max​(double a, double b, double c, double d)
      Quadrary max, without handling of special values.
      static int max​(int a, int b)
      Binary max.
      static int max​(int a, int b, int c)
      Ternary max.
      static int max​(int a, int b, int c, int d)
      Quadrary max, without handling of special values.
      static double min​(double a, double b)
      Binary min, without handling of special values.
      static double min​(double a, double b, double c)
      Ternary min, without handling of special values.
      static double min​(double a, double b, double c, double d)
      Quadrary min, without handling of special values.
      static int min​(int a, int b)
      Binary min, without handling of special values.
      static int min​(int a, int b, int c)
      Ternary min, without handling of special values.
      static int min​(int a, int b, int c, int d)
      Quadrary min, without handling of special values.
      static int nextAllOnesInt​(int x)
      Find the next larger number with all ones.
      static long nextAllOnesLong​(long x)
      Find the next larger number with all ones.
      static int nextPow2Int​(int x)
      Find the next power of 2.
      static long nextPow2Long​(long x)
      Find the next power of 2.
      static double normAngle​(double x)
      Normalize an angle to [0:2pi[
      static double powi​(double x, int p)
      Delegate to FastMath.powFast
      static double rad2deg​(double rad)
      Radians to Degree.
      static double[] randomDoubleArray​(int len, java.util.Random r)
      Produce an array of random numbers in [0:1].
      static int[] sequence​(int start, int end)
      Generate an array of integers.
      static long sumFirstIntegers​(long i)
      Compute the sum of the i first integers.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SQRTPI

        public static final double SQRTPI
        Square root of Pi.
      • SQRTTWOPI

        public static final double SQRTTWOPI
        Square root of two times Pi.
      • SQRTHALFPI

        public static final double SQRTHALFPI
        Constant for sqrt(pi/2)
      • SQRT2

        public static final double SQRT2
        Square root of 2.
      • SQRT3

        public static final double SQRT3
        Square root of 3.
      • SQRT5

        public static final double SQRT5
        Square root of 5.
      • SQRTHALF

        public static final double SQRTHALF
        Square root of 0.5 == 1 / sqrt(2).
      • ONE_BY_SQRTPI

        public static final double ONE_BY_SQRTPI
        Precomputed value of 1 / sqrt(pi).
      • ONE_BY_SQRTTWOPI

        public static final double ONE_BY_SQRTTWOPI
        Precomputed value of 1 / sqrt(2 * pi).
      • LOG_ONE_BY_SQRTTWOPI

        public static final double LOG_ONE_BY_SQRTTWOPI
        Precomputed value of log(1 / sqrt(2 * pi)) = -.5 * log(2*pi).
      • ONE_BY_LOG2

        public static final double ONE_BY_LOG2
        1. / log(2)
      • SQRTTHIRD

        public static final double SQRTTHIRD
        Square root of one third.
      • LOG2

        public static final double LOG2
        Logarithm of 2 to the basis e, for logarithm conversion.
      • LOG3

        public static final double LOG3
        Logarithm of 3 to the basis e, for logarithm conversion.
      • LOG10

        public static final double LOG10
        Natural logarithm of 10.
      • LOGPI

        public static final double LOGPI
        log(PI).
      • LOGPIHALF

        public static final double LOGPIHALF
        log(PI) / 2.
      • LOGTWOPI

        public static final double LOGTWOPI
        log(2*PI).
      • LOGSQRTTWOPI

        public static final double LOGSQRTTWOPI
        log(sqrt(2*PI)).
      • LOGLOG2

        public static final double LOGLOG2
        log(log(2))
      • DEG2RAD

        public static final double DEG2RAD
        Constant for degrees to radians.
        See Also:
        Constant Field Values
      • RAD2DEG

        public static final double RAD2DEG
        Constant for radians to degrees.
        See Also:
        Constant Field Values
      • EULERMASCHERONI

        public static final double EULERMASCHERONI
        Euler–Mascheroni constant.
        See Also:
        Constant Field Values
      • EMPTY_INTS

        public static final int[] EMPTY_INTS
        Empty integer array.
    • Constructor Detail

      • MathUtil

        private MathUtil()
        Fake constructor for static class.
    • Method Detail

      • log2

        public static double log2​(double x)
        Compute the base 2 logarithm.
        Parameters:
        x - X
        Returns:
        Logarithm base 2.
      • factorial

        public static long factorial​(int n)
        Compute the Factorial of n, often written as c! in mathematics.
        Parameters:
        n - Note: n >= 0
        Returns:
        n * (n-1) * (n-2) * ... * 1
      • binomialCoefficient

        public static long binomialCoefficient​(long n,
                                               long k)
        Binomial coefficient, also known as "n choose k".
        Parameters:
        n - Total number of samples. n > 0
        k - Number of elements to choose. n >= k, k >= 0
        Returns:
        n! / (k! * (n-k)!)
      • approximateFactorial

        public static double approximateFactorial​(int n)
        Compute the Factorial of n, often written as c! in mathematics.
        Parameters:
        n - Note: n >= 0
        Returns:
        n * (n-1) * (n-2) * ... * 1
      • approximateBinomialCoefficient

        public static double approximateBinomialCoefficient​(int n,
                                                            int k)
        Binomial coefficent, also known as "n choose k").
        Parameters:
        n - Total number of samples. n > 0
        k - Number of elements to choose. n >= k, k >= 0
        Returns:
        n! / (k! * (n-k)!)
      • sumFirstIntegers

        public static long sumFirstIntegers​(long i)
        Compute the sum of the i first integers.
        Parameters:
        i - maximum summand (inclusive)
        Returns:
        Sum
      • randomDoubleArray

        public static double[] randomDoubleArray​(int len,
                                                 java.util.Random r)
        Produce an array of random numbers in [0:1].
        Parameters:
        len - Length
        r - Random generator
        Returns:
        Array
      • deg2rad

        public static double deg2rad​(double deg)
        Convert Degree to Radians.

        This is essentially the same as Math.toRadians(double), but we keep it for now, it might be marginally faster, but certainly not slower.

        Parameters:
        deg - Degree value
        Returns:
        Radian value
      • rad2deg

        public static double rad2deg​(double rad)
        Radians to Degree.

        This is essentially the same as Math.toRadians(double), but we keep it for now, it might be marginally faster, but certainly not slower.

        Parameters:
        rad - Radians value
        Returns:
        Degree value
      • normAngle

        public static double normAngle​(double x)
        Normalize an angle to [0:2pi[
        Parameters:
        x - Input angle
        Returns:
        Normalized angle
      • nextPow2Int

        public static int nextPow2Int​(int x)
        Find the next power of 2.

        Classic bit operation, for signed 32-bit. Valid for positive integers only (0 otherwise).

        Parameters:
        x - original integer
        Returns:
        Next power of 2
      • nextPow2Long

        public static long nextPow2Long​(long x)
        Find the next power of 2.

        Classic bit operation, for signed 64-bit. Valid for positive integers only (0 otherwise).

        Parameters:
        x - original long integer
        Returns:
        Next power of 2
      • nextAllOnesInt

        public static int nextAllOnesInt​(int x)
        Find the next larger number with all ones.

        Classic bit operation, for signed 32-bit. Valid for positive integers only (-1 otherwise).

        Parameters:
        x - original integer
        Returns:
        Next number with all bits set
      • nextAllOnesLong

        public static long nextAllOnesLong​(long x)
        Find the next larger number with all ones.

        Classic bit operation, for signed 64-bit. Valid for positive integers only (-1 otherwise).

        Parameters:
        x - original long integer
        Returns:
        Next number with all bits set
      • floatToDoubleUpper

        public static double floatToDoubleUpper​(float f)
        Return the largest double that rounds down to this float.

        Note: Probably not always correct - subnormal values are quite tricky. So some of the bounds might not be tight.

        Parameters:
        f - Float value
        Returns:
        Double value
      • floatToDoubleLower

        public static double floatToDoubleLower​(float f)
        Return the largest double that rounds up to this float.

        Note: Probably not always correct - subnormal values are quite tricky. So some of the bounds might not be tight.

        Parameters:
        f - Float value
        Returns:
        Double value
      • log1mexp

        public static double log1mexp​(double x)
        More stable than FastMath.log(1 - FastMath.exp(x))
        Parameters:
        x - Value
        Returns:
        log(1-exp(x))
      • exp

        public static double exp​(double d)
        Delegate to FastMath.exp.
        Parameters:
        d - Value
        Returns:
        FastMath.exp(d)
      • powi

        public static double powi​(double x,
                                  int p)
        Delegate to FastMath.powFast
        Parameters:
        x - Base
        p - Exponent
        Returns:
        FastMath.powFast(x, p)
      • ipowi

        public static int ipowi​(int x,
                                int p)
        Fast loop for computing pow(x, p) for p >= 0 integer and x integer.
        Parameters:
        x - Base
        p - Exponent
        Returns:
        pow(x, p)
      • sequence

        public static int[] sequence​(int start,
                                     int end)
        Generate an array of integers.
        Parameters:
        start - First integer
        end - Last integer (exclusive!)
        Returns:
        Array of integers of length end-start
      • max

        public static double max​(double a,
                                 double b)
        Binary max, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.max(int, int). But usually, it should be written inline as (a >= b) ? a : b

        The result is asymmetric in case of Double.NaN:
        MathUtil.max(Double.NaN, 1.) is 1, but
        MathUtil.max(1., Double.NaN) is Double.NaN.

        Parameters:
        a - First value
        b - Second value
        Returns:
        Maximum
      • max

        public static double max​(double a,
                                 double b,
                                 double c)
        Ternary max, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.max(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        c - Third value
        Returns:
        Maximum
      • max

        public static double max​(double a,
                                 double b,
                                 double c,
                                 double d)
        Quadrary max, without handling of special values. Because of the lack of special case handling, this is faster than Math.max(int, int). But usually, it should be written inline.
        Parameters:
        a - First value
        b - Second value
        c - Third value
        d - Fourth value
        Returns:
        Maximum
      • max

        public static int max​(int a,
                              int b)
        Binary max. If possible, inline.
        Parameters:
        a - First value
        b - Second value
        Returns:
        Maximum
      • max

        public static int max​(int a,
                              int b,
                              int c)
        Ternary max. If possible, inline.
        Parameters:
        a - First value
        b - Second value
        c - Third value
        Returns:
        Maximum
      • max

        public static int max​(int a,
                              int b,
                              int c,
                              int d)
        Quadrary max, without handling of special values. Because of the lack of special case handling, this is faster than Math.max(int, int). But usually, it should be written inline.
        Parameters:
        a - First value
        b - Second value
        c - Third value
        d - Fourth value
        Returns:
        Maximum
      • min

        public static double min​(double a,
                                 double b)
        Binary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline as (a <= b) ? a : b

        The result is asymmetric in case of Double.NaN:
        MathUtil.min(Double.NaN, 1.) is 1, but
        MathUtil.min(1., Double.NaN) is Double.NaN.

        Parameters:
        a - First value
        b - Second value
        Returns:
        minimum
      • min

        public static double min​(double a,
                                 double b,
                                 double c)
        Ternary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        c - Third value
        Returns:
        minimum
      • min

        public static double min​(double a,
                                 double b,
                                 double c,
                                 double d)
        Quadrary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        c - Third value
        d - Fourth value
        Returns:
        minimum
      • min

        public static int min​(int a,
                              int b)
        Binary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        Returns:
        minimum
      • min

        public static int min​(int a,
                              int b,
                              int c)
        Ternary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        c - Third value
        Returns:
        minimum
      • min

        public static int min​(int a,
                              int b,
                              int c,
                              int d)
        Quadrary min, without handling of special values.

        Because of the lack of special case handling, this is faster than Math.min(int, int). But usually, it should be written inline.

        Parameters:
        a - First value
        b - Second value
        c - Third value
        d - Fourth value
        Returns:
        minimum
      • clamp

        public static double clamp​(double value,
                                   double min,
                                   double max)
        Clamp values to a given minimum and maximum.
        Parameters:
        value - True value
        min - Minimum
        max - Maximum
        Returns:
        value, but at least min and at most max