java.lang.Object
de.lmu.ifi.dbs.elki.math.MathUtil
A collection of math related utility functions.
- Author:
- Arthur Zimek, Erich Schubert
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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 TypeMethodDescriptionstatic 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
Compute the angle between two vectors.static double
Compute the angle between two vectors.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 asc!
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 asc!
static BigInteger
Compute the Factorial of n, often written asc!
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
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 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
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
pearsonCorrelationCoefficient
(double[] x, double[] y) Provides the Pearson product-moment correlation coefficient for two FeatureVectors.static double
pearsonCorrelationCoefficient
(NumberVector<?> x, NumberVector<?> y) Provides the Pearson product-moment correlation coefficient for two FeatureVectors.static double
rad2deg
(double rad) Radians to Degree.static double[]
randomDoubleArray
(int len) Produce an array of random numbers in [0:1].static double[]
randomDoubleArray
(int len, Random r) 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
sumFirstIntegers
(long i) 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
weightedPearsonCorrelationCoefficient
(NumberVector<?> x, NumberVector<?> y, double[] weights) Provides the Pearson product-moment correlation coefficient for two FeatureVectors.static double
weightedPearsonCorrelationCoefficient
(NumberVector<?> x, NumberVector<?> y, NumberVector<?> weights) Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
-
Field Details
-
TWOPI
public static final double TWOPITwo times Pi.- See Also:
-
HALFPI
public static final double HALFPIHalf the value of Pi.- See Also:
-
ONEHALFPI
public static final double ONEHALFPI1.5 times Pi.- See Also:
-
SQRTPI
public static final double SQRTPISquare root of Pi. -
SQRTTWOPI
public static final double SQRTTWOPISquare root of two times Pi. -
SQRT2
public static final double SQRT2Square root of 2. -
SQRT5
public static final double SQRT5Square root of 5. -
SQRTHALF
public static final double SQRTHALFSquare root of 0.5 == 1 / sqrt(2). -
ONE_BY_SQRTPI
public static final double ONE_BY_SQRTPIPrecomputed value of 1 / sqrt(pi). -
LOG2
public static final double LOG2Logarithm of 2 to the basis e, for logarithm conversion. -
LOG10
public static final double LOG10Natural logarithm of 10. -
LOGPI
public static final double LOGPIMath.log(Math.PI). -
LOGPIHALF
public static final double LOGPIHALFMath.log(Math.PI) / 2. -
LOGSQRTTWOPI
public static final double LOGSQRTTWOPIMath.log(Math.sqrt(2*Math.PI)). -
DEG2RAD
public static final double DEG2RADConstant for degrees to radians.- See Also:
-
RAD2DEG
public static final double RAD2DEGConstant 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 toMath.hypot(double, double)
, since the latter is significantly slower (but maybe has a higher precision).- Parameters:
a
- first cathetusb
- 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 toMath.hypot(double, double)
, since the latter is significantly slower (but has a higher precision).- Parameters:
a
- first cathetusb
- second cathetusc
- second cathetus- Returns:
sqrt(a<sup>2</sup> + b<sup>2</sup> + c<sup>2</sup>)
-
mahalanobisDistance
Compute the Mahalanobis distance using the given weight matrix.- Parameters:
weightMatrix
- Weight Matrixo1_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 Matrixo1_minus_o2
- Delta vector- Returns:
- Mahalanobis distance
-
pearsonCorrelationCoefficient
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
- Parameters:
x
- first FeatureVectory
- 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 FeatureVectory
- second FeatureVectorweights
- 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 FeatureVectory
- second FeatureVectorweights
- 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 FeatureVectory
- 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 FeatureVectory
- second FeatureVectorweights
- Weights- Returns:
- the Pearson product-moment correlation coefficient for x and y
-
factorial
Compute the Factorial of n, often written asc!
in mathematics.Use this method if for large values of
n
.- Parameters:
n
- Note: n >= 0. ThisBigInteger
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 asc!
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 > 0k
- 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 asc!
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 > 0k
- 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
Produce an array of random numbers in [0:1].- Parameters:
len
- Lengthr
- Random generator- Returns:
- Array
-
deg2rad
public static double deg2rad(double deg) Convert Degree to Radians. This is essentially the same asMath.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 asMath.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
Compute the angle between two vectors.- Parameters:
v1
- first vectorv2
- second vector- Returns:
- Angle
-
angle
public static double angle(double[] v1, double[] v2) Compute the angle between two vectors.- Parameters:
v1
- first vectorv2
- second vector- Returns:
- Angle
-
angle
Compute the angle between two vectors.- Parameters:
v1
- first vectorv2
- second vectoro
- Origin- Returns:
- Angle
-
angle
public static double angle(double[] v1, double[] v2, double[] o) Compute the angle between two vectors.- Parameters:
v1
- first vectorv2
- second vectoro
- 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 xsin
- 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 xcos
- 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
-