Class MathUtil
- java.lang.Object
-
- elki.math.MathUtil
-
public final class MathUtil extends java.lang.ObjectA collection of math related utility functions.- Since:
- 0.1
- Author:
- Arthur Zimek, Erich Schubert
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDEG2RADConstant for degrees to radians.static int[]EMPTY_INTSEmpty integer array.static doubleEULERMASCHERONIEuler–Mascheroni constant.static doubleHALFPIHalf the value of Pi.static doubleLOG_ONE_BY_SQRTTWOPIPrecomputed value of log(1 / sqrt(2 * pi)) = -.5 * log(2*pi).static doubleLOG10Natural logarithm of 10.static doubleLOG2Logarithm of 2 to the basis e, for logarithm conversion.static doubleLOG3Logarithm of 3 to the basis e, for logarithm conversion.static doubleLOGLOG2log(log(2))static doubleLOGPIlog(PI).static doubleLOGPIHALFlog(PI) / 2.static doubleLOGSQRTTWOPIlog(sqrt(2*PI)).static doubleLOGTWOPIlog(2*PI).static doubleONE_BY_LOG21. / log(2)static doubleONE_BY_SQRTPIPrecomputed value of 1 / sqrt(pi).static doubleONE_BY_SQRTTWOPIPrecomputed value of 1 / sqrt(2 * pi).static doubleONE_THIRDOne third.static doubleONEHALFPI1.5 times Pi.static doublePISQUAREPi squaredstatic doubleQUARTERPIOne quarter of Pi.static doubleRAD2DEGConstant for radians to degrees.static doubleSQRT2Square root of 2.static doubleSQRT3Square root of 3.static doubleSQRT5Square root of 5.static doubleSQRTHALFSquare root of 0.5 == 1 / sqrt(2).static doubleSQRTHALFPIConstant for sqrt(pi/2)static doubleSQRTPISquare root of Pi.static doubleSQRTTHIRDSquare root of one third.static doubleSQRTTWOPISquare root of two times Pi.static doubleTWOPITwo times Pi.
-
Constructor Summary
Constructors Modifier Constructor Description privateMathUtil()Fake constructor for static class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleapproximateBinomialCoefficient(int n, int k)Binomial coefficent, also known as "n choose k").static doubleapproximateFactorial(int n)Compute the Factorial of n, often written asc!static longbinomialCoefficient(long n, long k)Binomial coefficient, also known as "n choose k".static doubleclamp(double value, double min, double max)Clamp values to a given minimum and maximum.static doubledeg2rad(double deg)Convert Degree to Radians.static doubleexp(double d)Delegate to FastMath.exp.static longfactorial(int n)Compute the Factorial of n, often written asc!static doublefloatToDoubleLower(float f)Return the largest double that rounds up to this float.static doublefloatToDoubleUpper(float f)Return the largest double that rounds down to this float.static intipowi(int x, int p)Fast loop for computingpow(x, p)forp >= 0integer and x integer.static doublelog1mexp(double x)More stable thanFastMath.log(1 - FastMath.exp(x))static doublelog2(double x)Compute the base 2 logarithm.static doublemax(double a, double b)Binary max, without handling of special values.static doublemax(double a, double b, double c)Ternary max, without handling of special values.static doublemax(double a, double b, double c, double d)Quadrary max, without handling of special values.static intmax(int a, int b)Binary max.static intmax(int a, int b, int c)Ternary max.static intmax(int a, int b, int c, int d)Quadrary max, without handling of special values.static doublemin(double a, double b)Binary min, without handling of special values.static doublemin(double a, double b, double c)Ternary min, without handling of special values.static doublemin(double a, double b, double c, double d)Quadrary min, without handling of special values.static intmin(int a, int b)Binary min, without handling of special values.static intmin(int a, int b, int c)Ternary min, without handling of special values.static intmin(int a, int b, int c, int d)Quadrary min, without handling of special values.static intnextAllOnesInt(int x)Find the next larger number with all ones.static longnextAllOnesLong(long x)Find the next larger number with all ones.static intnextPow2Int(int x)Find the next power of 2.static longnextPow2Long(long x)Find the next power of 2.static doublenormAngle(double x)Normalize an angle to [0:2pi[static doublepowi(double x, int p)Delegate to FastMath.powFaststatic doublerad2deg(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 longsumFirstIntegers(long i)Compute the sum of the i first integers.
-
-
-
Field Detail
-
TWOPI
public static final double TWOPI
Two times Pi.- See Also:
- Constant Field Values
-
HALFPI
public static final double HALFPI
Half the value of Pi.- See Also:
- Constant Field Values
-
QUARTERPI
public static final double QUARTERPI
One quarter of Pi.- See Also:
- Constant Field Values
-
ONEHALFPI
public static final double ONEHALFPI
1.5 times Pi.- See Also:
- Constant Field Values
-
PISQUARE
public static final double PISQUARE
Pi squared- See Also:
- Constant Field Values
-
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)
-
ONE_THIRD
public static final double ONE_THIRD
One third.- See Also:
- Constant Field Values
-
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.
-
-
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 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 (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- Lengthr- 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 thanFastMath.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- Basep- Exponent- Returns:
FastMath.powFast(x, p)
-
ipowi
public static int ipowi(int x, int p)Fast loop for computingpow(x, p)forp >= 0integer and x integer.- Parameters:
x- Basep- Exponent- Returns:
pow(x, p)
-
sequence
public static int[] sequence(int start, int end)Generate an array of integers.- Parameters:
start- First integerend- 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 : bThe result is asymmetric in case of
Double.NaN:
MathUtil.max(Double.NaN, 1.)is 1, but
MathUtil.max(1., Double.NaN)isDouble.NaN.- Parameters:
a- First valueb- 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 valueb- Second valuec- 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 thanMath.max(int, int). But usually, it should be written inline.- Parameters:
a- First valueb- Second valuec- Third valued- Fourth value- Returns:
- Maximum
-
max
public static int max(int a, int b)Binary max. If possible, inline.- Parameters:
a- First valueb- Second value- Returns:
- Maximum
-
max
public static int max(int a, int b, int c)Ternary max. If possible, inline.- Parameters:
a- First valueb- Second valuec- 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 thanMath.max(int, int). But usually, it should be written inline.- Parameters:
a- First valueb- Second valuec- Third valued- 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 : bThe result is asymmetric in case of
Double.NaN:
MathUtil.min(Double.NaN, 1.)is 1, but
MathUtil.min(1., Double.NaN)isDouble.NaN.- Parameters:
a- First valueb- 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 valueb- Second valuec- 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 valueb- Second valuec- Third valued- 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 valueb- 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 valueb- Second valuec- 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 valueb- Second valuec- Third valued- 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 valuemin- Minimummax- Maximum- Returns:
value, but at leastminand at mostmax
-
-