public final class MathUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static double |
DEG2RAD
Constant for degrees to radians.
|
static int[] |
EMPTY_INTS
Empty integer array.
|
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.
|
Modifier | Constructor and Description |
---|---|
private |
MathUtil()
Fake constructor for static class.
|
Modifier and Type | Method and 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 |
deg2rad(double deg)
Convert Degree to Radians.
|
static double |
exp(double d)
Delegate to FastMath.exp.
|
static java.math.BigInteger |
factorial(java.math.BigInteger n)
Compute the Factorial of n, often written as
c! |
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.
|
public static final double TWOPI
public static final double HALFPI
public static final double QUARTERPI
public static final double ONEHALFPI
public static final double PISQUARE
public static final double SQRTPI
public static final double SQRTTWOPI
public static final double SQRTHALFPI
public static final double SQRT2
public static final double SQRT3
public static final double SQRT5
public static final double SQRTHALF
public static final double ONE_BY_SQRTPI
public static final double ONE_BY_SQRTTWOPI
public static final double LOG_ONE_BY_SQRTTWOPI
public static final double ONE_BY_LOG2
public static final double ONE_THIRD
public static final double SQRTTHIRD
public static final double LOG2
public static final double LOG3
public static final double LOG10
public static final double LOGPI
public static final double LOGPIHALF
public static final double LOGTWOPI
public static final double LOGSQRTTWOPI
public static final double LOGLOG2
public static final double DEG2RAD
public static final double RAD2DEG
public static final int[] EMPTY_INTS
public static double log2(double x)
x
- Xpublic static java.math.BigInteger factorial(java.math.BigInteger n)
c!
in
mathematics.
Use this method if for large values of n
.
n
- Note: n >= 0. This BigInteger
n
will be 0
after this method finishes.public static long factorial(int n)
c!
in
mathematics.n
- Note: n >= 0public static long binomialCoefficient(long n, long k)
n
- Total number of samples. n > 0k
- Number of elements to choose. n >= k
,
k >= 0
public static double approximateFactorial(int n)
c!
in
mathematics.n
- Note: n >= 0public static double approximateBinomialCoefficient(int n, int k)
n
- Total number of samples. n > 0k
- Number of elements to choose. n >= k
,
k >= 0
public static long sumFirstIntegers(long i)
i
- maximum summand (inclusive)public static double[] randomDoubleArray(int len, java.util.Random r)
len
- Lengthr
- Random generatorpublic static double deg2rad(double deg)
This is essentially the same as Math.toRadians(double)
, but we keep it for
now, it might be marginally faster, but certainly not slower.
deg
- Degree valuepublic static double rad2deg(double rad)
This is essentially the same as Math.toRadians(double)
, but we keep it for
now, it might be marginally faster, but certainly not slower.
rad
- Radians valuepublic static double normAngle(double x)
x
- Input anglepublic static int nextPow2Int(int x)
Classic bit operation, for signed 32-bit. Valid for positive integers only (0 otherwise).
x
- original integerpublic static long nextPow2Long(long x)
Classic bit operation, for signed 64-bit. Valid for positive integers only (0 otherwise).
x
- original long integerpublic static int nextAllOnesInt(int x)
Classic bit operation, for signed 32-bit. Valid for positive integers only (-1 otherwise).
x
- original integerpublic static long nextAllOnesLong(long x)
Classic bit operation, for signed 64-bit. Valid for positive integers only (-1 otherwise).
x
- original long integerpublic static double floatToDoubleUpper(float f)
Note: Probably not always correct - subnormal values are quite tricky. So some of the bounds might not be tight.
f
- Float valuepublic static double floatToDoubleLower(float f)
Note: Probably not always correct - subnormal values are quite tricky. So some of the bounds might not be tight.
f
- Float valuepublic static double log1mexp(double x)
FastMath.log(1 - FastMath.exp(x))
x
- Valuepublic static double exp(double d)
d
- Valuepublic static double powi(double x, int p)
x
- Basep
- ExponentFastMath.powFast(x, p)
public static int ipowi(int x, int p)
pow(x, p)
for p >= 0
integer and x integer.x
- Basep
- Exponentpow(x, p)
public static int[] sequence(int start, int end)
start
- First integerend
- Last integer (exclusive!)public static double max(double a, double b)
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
.
a
- First valueb
- Second valuepublic static double max(double a, double b, double c)
Because of the lack of special case handling, this is faster than
Math.max(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuec
- Third valuepublic static double max(double a, double b, double c, double d)
Math.max(int, int)
. But usually, it should be written inline.a
- First valueb
- Second valuec
- Third valued
- Fourth valuepublic static int max(int a, int b)
a
- First valueb
- Second valuepublic static int max(int a, int b, int c)
a
- First valueb
- Second valuec
- Third valuepublic static int max(int a, int b, int c, int d)
Math.max(int, int)
. But usually, it should be written inline.a
- First valueb
- Second valuec
- Third valued
- Fourth valuepublic static double min(double a, double b)
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
.
a
- First valueb
- Second valuepublic static double min(double a, double b, double c)
Because of the lack of special case handling, this is faster than
Math.min(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuec
- Third valuepublic static double min(double a, double b, double c, double d)
Because of the lack of special case handling, this is faster than
Math.min(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuec
- Third valued
- Fourth valuepublic static int min(int a, int b)
Because of the lack of special case handling, this is faster than
Math.min(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuepublic static int min(int a, int b, int c)
Because of the lack of special case handling, this is faster than
Math.min(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuec
- Third valuepublic static int min(int a, int b, int c, int d)
Because of the lack of special case handling, this is faster than
Math.min(int, int)
. But usually, it should be written inline.
a
- First valueb
- Second valuec
- Third valued
- Fourth valueCopyright © 2019 ELKI Development Team. License information.