Module math

Class MathUtil

java.lang.Object
de.lmu.ifi.dbs.elki.math.MathUtil

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

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Constant for degrees to radians.
    static final double
    Half the value of Pi.
    static final double
    Natural logarithm of 10.
    static final double
    Logarithm of 2 to the basis e, for logarithm conversion.
    static final double
    Math.log(Math.PI).
    static final double
    Math.log(Math.PI) / 2.
    static final double
    Math.log(Math.sqrt(2*Math.PI)).
    static final double
    Precomputed value of 1 / sqrt(pi).
    static final double
    1.5 times Pi.
    static final double
    Constant for radians to degrees.
    static final double
    Square root of 2.
    static final double
    Square root of 5.
    static final double
    Square root of 0.5 == 1 / sqrt(2).
    static final double
    Square root of Pi.
    static final double
    Square root of two times Pi.
    static final double
    Two times Pi.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    angle(double[] v1, double[] v2)
    Compute the angle between two vectors.
    static double
    angle(double[] v1, double[] v2, double[] o)
    Compute the angle between two vectors.
    static double
    angle(Vector v1, Vector v2)
    Compute the angle between two vectors.
    static double
    angle(Vector v1, Vector v2, Vector o)
    Compute the angle between two vectors.
    static double
    Binomial coefficent, also known as "n choose k").
    static double
    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
    cosToSin(double angle, double cos)
    Fast way of computing sin(x) from x and cos(x).
    static double
    deg2rad(double deg)
    Convert Degree to Radians.
    static long
    factorial(int n)
    Compute the Factorial of n, often written as c!
    static BigInteger
    Compute the Factorial of n, often written as c!
    static double
    fastHypot(double a, double b)
    Computes the square root of the sum of the squared arguments without under or overflow.
    static double
    fastHypot3(double a, double b, double c)
    Computes the square root of the sum of the squared arguments without under or overflow.
    static double
    Return the largest double that rounds up to this float.
    static double
    Return the largest double that rounds down to this float.
    static double
    mahalanobisDistance(double[][] weightMatrix, double[] o1_minus_o2)
    Compute the Mahalanobis distance using the given weight matrix.
    static double
    mahalanobisDistance(Matrix weightMatrix, Vector o1_minus_o2)
    Compute the Mahalanobis distance using the given weight matrix.
    static int
    Find the next larger number with all ones.
    static long
    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
    pearsonCorrelationCoefficient(double[] x, double[] y)
    Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
    static double
    Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
    static double
    rad2deg(double rad)
    Radians to Degree.
    static double[]
    Produce an array of random numbers in [0:1].
    static double[]
    Produce an array of random numbers in [0:1].
    static double
    sinToCos(double angle, double sin)
    Fast way of computing cos(x) from x and sin(x).
    static long
    Compute the sum of the i first integers.
    static double
    weightedPearsonCorrelationCoefficient(double[] x, double[] y, double[] weights)
    Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
    static double
    Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
    static double
    Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

    Methods inherited from class java.lang.Object

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

    • TWOPI

      public static final double TWOPI
      Two times Pi.
      See Also:
    • HALFPI

      public static final double HALFPI
      Half the value of Pi.
      See Also:
    • ONEHALFPI

      public static final double ONEHALFPI
      1.5 times Pi.
      See Also:
    • SQRTPI

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

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

      public static final double SQRT2
      Square root of 2.
    • 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).
    • LOG2

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

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

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

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

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

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

      public static final double RAD2DEG
      Constant for radians to degrees.
      See Also:
  • Method Details

    • fastHypot

      public static double fastHypot(double a, double b)
      Computes the square root of the sum of the squared arguments without under or overflow. Note: this code is not redundant to Math.hypot(double, double), since the latter is significantly slower (but maybe has a higher precision).
      Parameters:
      a - first cathetus
      b - second cathetus
      Returns:
      sqrt(a<sup>2</sup> + b<sup>2</sup>)
    • fastHypot3

      public static double fastHypot3(double a, double b, double c)
      Computes the square root of the sum of the squared arguments without under or overflow. Note: this code is not redundant to Math.hypot(double, double), since the latter is significantly slower (but has a higher precision).
      Parameters:
      a - first cathetus
      b - second cathetus
      c - second cathetus
      Returns:
      sqrt(a<sup>2</sup> + b<sup>2</sup> + c<sup>2</sup>)
    • mahalanobisDistance

      public static double mahalanobisDistance(Matrix weightMatrix, Vector o1_minus_o2)
      Compute the Mahalanobis distance using the given weight matrix.
      Parameters:
      weightMatrix - Weight Matrix
      o1_minus_o2 - Delta vector
      Returns:
      Mahalanobis distance
    • mahalanobisDistance

      public static double mahalanobisDistance(double[][] weightMatrix, double[] o1_minus_o2)
      Compute the Mahalanobis distance using the given weight matrix.
      Parameters:
      weightMatrix - Weight Matrix
      o1_minus_o2 - Delta vector
      Returns:
      Mahalanobis distance
    • pearsonCorrelationCoefficient

      public static double pearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y)

      Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

      Parameters:
      x - first FeatureVector
      y - second FeatureVector
      Returns:
      the Pearson product-moment correlation coefficient for x and y
    • weightedPearsonCorrelationCoefficient

      public static double weightedPearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y, double[] weights)

      Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

      Parameters:
      x - first FeatureVector
      y - second FeatureVector
      weights - Weights
      Returns:
      the Pearson product-moment correlation coefficient for x and y
    • weightedPearsonCorrelationCoefficient

      public static double weightedPearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y, NumberVector<?> weights)

      Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

      Parameters:
      x - first FeatureVector
      y - second FeatureVector
      weights - Weights
      Returns:
      the Pearson product-moment correlation coefficient for x and y
    • pearsonCorrelationCoefficient

      public static double pearsonCorrelationCoefficient(double[] x, double[] y)

      Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

      Parameters:
      x - first FeatureVector
      y - second FeatureVector
      Returns:
      the Pearson product-moment correlation coefficient for x and y
    • weightedPearsonCorrelationCoefficient

      public static double weightedPearsonCorrelationCoefficient(double[] x, double[] y, double[] weights)

      Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

      Parameters:
      x - first FeatureVector
      y - second FeatureVector
      weights - Weights
      Returns:
      the Pearson product-moment correlation coefficient for x and y
    • factorial

      public static BigInteger factorial(BigInteger n)
      Compute the Factorial of n, often written as c! in mathematics.

      Use this method if for large values of n.

      Parameters:
      n - Note: n >= 0. This BigInteger n will be 0 after this method finishes.
      Returns:
      n * (n-1) * (n-2) * ... * 1
    • 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
      Returns:
      Sum
    • randomDoubleArray

      public static double[] randomDoubleArray(int len)
      Produce an array of random numbers in [0:1].
      Parameters:
      len - Length
      Returns:
      Array
    • randomDoubleArray

      public static double[] randomDoubleArray(int len, 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
    • angle

      public static double angle(Vector v1, Vector v2)
      Compute the angle between two vectors.
      Parameters:
      v1 - first vector
      v2 - second vector
      Returns:
      Angle
    • angle

      public static double angle(double[] v1, double[] v2)
      Compute the angle between two vectors.
      Parameters:
      v1 - first vector
      v2 - second vector
      Returns:
      Angle
    • angle

      public static double angle(Vector v1, Vector v2, Vector o)
      Compute the angle between two vectors.
      Parameters:
      v1 - first vector
      v2 - second vector
      o - Origin
      Returns:
      Angle
    • angle

      public static double angle(double[] v1, double[] v2, double[] o)
      Compute the angle between two vectors.
      Parameters:
      v1 - first vector
      v2 - second vector
      o - Origin
      Returns:
      Angle
    • normAngle

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

      public static double sinToCos(double angle, double sin)
      Fast way of computing cos(x) from x and sin(x).
      Parameters:
      angle - Input angle x
      sin - Sine of x.
      Returns:
      Cosine of x
    • cosToSin

      public static double cosToSin(double angle, double cos)
      Fast way of computing sin(x) from x and cos(x).
      Parameters:
      angle - Input angle x
      cos - Cosine of x.
      Returns:
      Sine of x
    • 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