public final class SloppyMath
extends java.lang.Object
SloppyMath
contains methods for performing basic
numeric operations. In some cases, such as max and min, they cut a few
corners in the implementation for the sake of efficiency. In particular, they
may not handle special notions like NaN and -0.0 correctly. This was the
origin of the class name, but some other operations are just useful math
additions, such as logSum.Modifier and Type | Field and Description |
---|---|
static double |
LOGTOLERANCE
If a difference is bigger than this in log terms, then the sum or
difference of them will just be the larger (to 12 or so decimal places
for double, and 7 or 8 for float).
|
Modifier and Type | Method and Description |
---|---|
static double |
abs(double x) |
static float |
addExp_Old(float[] logV)
Similar to logAdd, but without the final log.
|
static float |
addExp(float[] logV,
int lastIndex) |
static double |
approxExp(double val) |
static double |
approxLog(double val) |
static double |
approxPow(double a,
double b) |
static double |
chiSquare2by2(int k,
int n,
int r,
int m)
Find a 2x2 chi-square value.
|
static double |
exactBinomial(int k,
int n,
double p)
Find a one tailed exact binomial test probability.
|
static double |
exp(double logX) |
static double |
hypergeometric(int k,
int n,
int r,
int m)
Find a hypergeometric distribution.
|
static double |
intPow(double b,
int e)
exponentiation like we learned in grade school: multiply b by itself e
times.
|
static float |
intPow(float b,
int e)
exponentiation like we learned in grade school: multiply b by itself e
times.
|
static int |
intPow(int b,
int e)
exponentiation like we learned in grade school: multiply b by itself e
times.
|
static boolean |
isDangerous(double d)
Returns true if the argument is a "dangerous" double to have around,
namely one that is infinite, NaN or zero.
|
static boolean |
isDangerous(float d) |
static boolean |
isDiscreteProb(double d,
double tol) |
static boolean |
isGreater(double x,
double y) |
static boolean |
isVeryDangerous(double d)
Returns true if the argument is a "very dangerous" double to have around,
namely one that is infinite or NaN.
|
static double |
lambert(double v,
double u) |
static float |
logAdd_Old(float[] logV) |
static <T> double |
logAdd(Counter<T> counts) |
static double |
logAdd(double[] logV) |
static double |
logAdd(double[] logV,
int lastIndex) |
static double |
logAdd(double lx,
double ly)
Returns the log of the sum of two numbers, which are themselves input in
log form.
|
static double |
logAdd(float[] logV) |
static float |
logAdd(float[] logV,
int lastIndex) |
static float |
logAdd(float lx,
float ly)
Returns the log of the sum of two numbers, which are themselves input in
log form.
|
static double |
logAdd(java.util.List<java.lang.Double> logV) |
static void |
logNormalize(double[] logV) |
static double |
logSubtract(double a,
double b) |
static void |
main(java.lang.String[] args)
Tests the hypergeometric distribution code, or other cooccurrences provided
in this module.
|
static double |
max(double a,
double b)
Returns the greater of two
double values. |
static float |
max(float a,
float b)
Returns the greater of two
float values. |
static int |
max(int a,
int b,
int c)
Returns the minimum of three int values.
|
static double |
min(double a,
double b)
Returns the smaller of two
double values. |
static float |
min(float a,
float b)
Returns the smaller of two
float values. |
static int |
min(int a,
int b,
int c)
Returns the minimum of three int values.
|
static int |
nChooseK(int n,
int k)
Computes n choose k in an efficient way.
|
static double |
noNaNDivide(double num,
double denom) |
static double |
oneTailedFishersExact(int k,
int n,
int r,
int m)
Find a one-tailed Fisher's exact probability.
|
static double |
relativeDifferance(double a,
double b) |
static double |
unsafeAdd(double a,
double b) |
static double |
unsafeSubtract(double a,
double b) |
public static final double LOGTOLERANCE
public static double abs(double x)
public static double lambert(double v, double u)
public static int max(int a, int b, int c)
public static int min(int a, int b, int c)
public static float max(float a, float b)
float
values. That is, the
result is the argument closer to positive infinity. If the arguments have
the same value, the result is that same value. Does none of the special
checks for NaN or -0.0f that Math.max
does.a
- an argument.b
- another argument.a
and b
.public static double max(double a, double b)
double
values. That is, the
result is the argument closer to positive infinity. If the arguments have
the same value, the result is that same value. Does none of the special
checks for NaN or -0.0f that Math.max
does.a
- an argument.b
- another argument.a
and b
.public static float min(float a, float b)
float
values. That is, the
result is the value closer to negative infinity. If the arguments have
the same value, the result is that same value. Does none of the special
checks for NaN or -0.0f that Math.max
does.a
- an argument.b
- another argument.a
and b.
public static double min(double a, double b)
double
values. That is, the
result is the value closer to negative infinity. If the arguments have
the same value, the result is that same value. Does none of the special
checks for NaN or -0.0f that Math.max
does.a
- an argument.b
- another argument.a
and b
.public static boolean isDangerous(double d)
public static boolean isDangerous(float d)
public static boolean isGreater(double x, double y)
public static boolean isVeryDangerous(double d)
public static double relativeDifferance(double a, double b)
public static boolean isDiscreteProb(double d, double tol)
public static float logAdd(float lx, float ly)
lx
- First number, in log formly
- Second number, in log formpublic static double logAdd(double lx, double ly)
lx
- First number, in log formly
- Second number, in log formpublic static double logAdd(float[] logV)
public static void logNormalize(double[] logV)
public static double logAdd(double[] logV)
public static double logAdd(java.util.List<java.lang.Double> logV)
public static float logAdd_Old(float[] logV)
public static float logAdd(float[] logV, int lastIndex)
public static double logAdd(double[] logV, int lastIndex)
public static float addExp_Old(float[] logV)
logV
- public static float addExp(float[] logV, int lastIndex)
public static int nChooseK(int n, int k)
n
- k
- public static int intPow(int b, int e)
b
- basee
- exponentpublic static float intPow(float b, int e)
b
- basee
- exponentpublic static double intPow(double b, int e)
b
- basee
- exponentpublic static double hypergeometric(int k, int n, int r, int m)
k
- The number of black balls drawnn
- The total number of ballsr
- The number of black ballsm
- The number of balls drawnpublic static double exactBinomial(int k, int n, double p)
k
- number of successesn
- Number of trialsp
- Probability of a successpublic static double oneTailedFishersExact(int k, int n, int r, int m)
k
- The number of black balls drawnn
- The total number of ballsr
- The number of black ballsm
- The number of balls drawnpublic static double chiSquare2by2(int k, int n, int r, int m)
k
- The number of black balls drawnn
- The total number of ballsr
- The number of black ballsm
- The number of balls drawnpublic static double exp(double logX)
public static void main(java.lang.String[] args)
args
- Either none, and the log add rountines are tested, or the
following 4 arguments: k (cell), n (total), r (row), m (col)public static double noNaNDivide(double num, double denom)
public static double approxLog(double val)
public static double approxExp(double val)
public static double approxPow(double a, double b)
public static double logSubtract(double a, double b)
public static double unsafeSubtract(double a, double b)
public static double unsafeAdd(double a, double b)
public static <T> double logAdd(Counter<T> counts)