public final class MathUtil extends Object
Modifier and Type | Field and Description |
---|---|
static double |
DEG2RAD
Constant for degrees to radians.
|
static double |
HALFPI
Half the value of 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
Math.log(Math.PI).
|
static double |
LOGPIHALF
Math.log(Math.PI) / 2.
|
static double |
LOGSQRTTWOPI
Math.log(Math.sqrt(2*Math.PI)).
|
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 |
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 |
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 |
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 |
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 |
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 |
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 BigInteger |
factorial(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 |
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 int |
ipowi(int x,
int p)
Fast loop for computing
Math.pow(x, p) for p >= 0 integer and x
integer. |
static double |
log1mexp(double x)
More stable than
Math.log(1 - Math.exp(x)) |
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 |
powi(double x,
int p)
Fast loop for computing
Math.pow(x, p) for p >= 0 integer. |
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.
|
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 SQRT5
public static final double SQRTHALF
public static final double ONE_BY_SQRTPI
public static final double ONE_BY_SQRTTWOPI
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 LOGSQRTTWOPI
public static final double LOGLOG2
public static final double DEG2RAD
public static final double RAD2DEG
public static double fastHypot(double a, double b)
Math.hypot(double, double)
, since the
latter is significantly slower (but maybe has a higher precision).a
- first cathetusb
- second cathetussqrt(a<sup>2</sup> + b<sup>2</sup>)
public static double fastHypot3(double a, double b, double c)
Math.hypot(double, double)
, since the
latter is significantly slower (but has a higher precision).a
- first cathetusb
- second cathetusc
- second cathetussqrt(a<sup>2</sup> + b<sup>2</sup> + c<sup>2</sup>)
public static double mahalanobisDistance(Matrix weightMatrix, Vector o1_minus_o2)
weightMatrix
- Weight Matrixo1_minus_o2
- Delta vectorpublic static double mahalanobisDistance(double[][] weightMatrix, double[] o1_minus_o2)
weightMatrix
- Weight Matrixo1_minus_o2
- Delta vectorpublic static double pearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y)
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
x
- first FeatureVectory
- second FeatureVectorpublic static double weightedPearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y, double[] weights)
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
x
- first FeatureVectory
- second FeatureVectorweights
- Weightspublic static double weightedPearsonCorrelationCoefficient(NumberVector<?> x, NumberVector<?> y, NumberVector<?> weights)
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
x
- first FeatureVectory
- second FeatureVectorweights
- Weightspublic static double pearsonCorrelationCoefficient(double[] x, double[] y)
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
x
- first FeatureVectory
- second FeatureVectorpublic static double weightedPearsonCorrelationCoefficient(double[] x, double[] y, double[] weights)
Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
x
- first FeatureVectory
- second FeatureVectorweights
- Weightspublic static BigInteger factorial(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)
Binomial coefficient, also known as "n choose 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 summandpublic static double[] randomDoubleArray(int len)
len
- Lengthpublic static double[] randomDoubleArray(int len, Random r)
len
- Lengthr
- Random generatorpublic static double deg2rad(double deg)
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)
Math.toRadians(double)
, but we keep it for
now, it might be marginally faster, but certainly not slower.rad
- Radians valuepublic static double angle(Vector v1, Vector v2)
v1
- first vectorv2
- second vectorpublic static double angle(double[] v1, double[] v2)
v1
- first vectorv2
- second vectorpublic static double angle(Vector v1, Vector v2, Vector o)
v1
- first vectorv2
- second vectoro
- Originpublic static double angle(double[] v1, double[] v2, double[] o)
v1
- first vectorv2
- second vectoro
- Originpublic static double normAngle(double x)
x
- Input anglepublic static double sinToCos(double angle, double sin)
angle
- Input angle xsin
- Sine of x.public static double cosToSin(double angle, double cos)
angle
- Input angle xcos
- Cosine of x.public static int nextPow2Int(int x)
x
- original integerpublic static long nextPow2Long(long x)
x
- original long integerpublic static int nextAllOnesInt(int x)
x
- original integerpublic static long nextAllOnesLong(long x)
x
- original long integerpublic static double floatToDoubleUpper(float f)
f
- Float valuepublic static double floatToDoubleLower(float f)
f
- Float valuepublic static double log1mexp(double x)
Math.log(1 - Math.exp(x))
x
- Valuepublic static double powi(double x, int p)
Math.pow(x, p)
for p >= 0 integer.x
- Basep
- ExponentMath.pow(x, p)
public static int ipowi(int x, int p)
Math.pow(x, p)
for p >= 0 integer and x
integer.x
- Basep
- ExponentMath.pow(x, p)