@CodeReview(reviewer="Kevin R. Dixon", date="2008-02-26", changesNeeded=false, comments={"Minor changes, log2 uses callback to main log() method.","Otherwise, looks fine."}) public class MathUtil extends java.lang.Object
MathUtil
class implements mathematical utility functions.Constructor and Description |
---|
MathUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
binomialCoefficient(int N,
int k)
Returns the binomial coefficient for "N choose k".
|
static int |
checkedAdd(int a,
int b)
Safely checks for underflow/overflow before adding two integers.
|
static int |
checkedMultiply(int a,
int b)
Safely checks for overflow before multiplying two integers.
|
static double |
expMinus1Plus(double x)
Computes exp(x - 1).
|
protected static double |
incompleteBetaContinuedFraction(double a,
double b,
double x)
Evaluates the continued fraction of the incomplete beta function.
|
static double |
incompleteGammaContinuedFraction(double a,
double x)
Returns the incomplete Gamma function using the continued
fraction expansion evaluation using Lentz's method
|
protected static double |
incompleteGammaSeriesExpansion(double a,
double x)
Computes the series expansion approximation to the incomplete
gamma function.
|
static double |
log(double x,
double base)
Returns the log of the given base of the given value,
y=log_b(x) such that x=b^y
|
static double |
log1MinusExp(double x)
Computes log(1 - exp(x)).
|
static double |
log1Plus(double x)
Computes log(1 + x).
|
static double |
log1PlusExp(double x)
Computes log(1 + exp(x)).
|
static double |
log2(double x)
Returns the base-2 logarithm of the given value.
|
static double |
logBetaFunction(double a,
double b)
Compute the natural logarithm of the Beta Function.
|
static double |
logBinomialCoefficient(int N,
int k)
Computes the natural logarithm of the binomial coefficient.
|
static double |
logFactorial(int n)
Returns the natural logarithm of n factorial log(n!) =
log(n*(n-1)*...*3*2*1)
|
static double |
logGammaFunction(double input)
Computes the logarithm of the Gamma function.
|
static double |
logMultinomialBetaFunction(Vector input)
Evaluates the natural logarithm of the multinomial beta function
for the given input vector.
|
static double |
lowerIncompleteGammaFunction(double a,
double x)
Computes the Lower incomplete gamma function.
|
static double |
regularizedIncompleteBetaFunction(double a,
double b,
double x)
Computes the regularized incomplete Beta function.
|
public static double log(double x, double base)
x
- The value.base
- The base for the logarithmpublic static double log2(double x)
x
- The value.@PublicationReference(author="Wikipedia",title="Gamma Function",type=WebPage,year=2010,url="http://en.wikipedia.org/wiki/Gamma_function") @PublicationReference(author="jdhedden",title="Bug in 2nd edition version of gammln()",type=WebPage,year=2005,url="http://www.numerical-recipes.com/forum/showthread.php?t=606") public static double logGammaFunction(double input)
input
- Input to evaluate the Natural Logarithm of the Gamma Function@PublicationReference(author="Wikipedia",title="Incomplete gamma function",type=WebPage,year=2010,url="http://en.wikipedia.org/wiki/Incomplete_gamma_function") @PublicationReference(author={"William H. Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"},title="Numerical Recipes in C, Second Edition",type=Book,year=1992,pages=218,notes="Function gammap",url="http://www.nrbook.com/a/bookcpdf.php") public static double lowerIncompleteGammaFunction(double a, double x)
a
- Degrees of Freedomx
- Input value@PublicationReference(author={"William H. Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"}, title="Numerical Recipes in C, Second Edition", type=Book, year=1992, pages={218,219}, url="http://www.nrbook.com/a/bookcpdf.php", notes="Function gser()") protected static double incompleteGammaSeriesExpansion(double a, double x)
a
- Degrees of Freedomx
- Input value@PublicationReference(author={"William H. Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"}, title="Numerical Recipes in C, Second Edition", type=Book, year=1992, pages={216,219}, url="http://www.nrbook.com/a/bookcpdf.php") public static double incompleteGammaContinuedFraction(double a, double x)
a
- Degrees of Freedomx
- Input value@PublicationReference(author="Wikipedia", title="Binomial coefficient", type=WebPage, year=2010, url="http://en.wikipedia.org/wiki/Binomial_coefficient") public static int binomialCoefficient(int N, int k)
N
- Total number of objects in the bagk
- Total number of objects to choose, must be less than or equal
to Npublic static double logBinomialCoefficient(int N, int k)
N
- Total number of objects in the bagk
- Total number of objects to choose, must be less than or equal
to Npublic static double logFactorial(int n)
n
- Parameter for choose for n factorial@PublicationReference(author="Wikipedia", title="Beta function", type=WebPage, year=2010, url="http://en.wikipedia.org/wiki/Beta_function") public static double logBetaFunction(double a, double b)
a
- First parameter to the Beta functionb
- Second parameter to the Beta function@PublicationReference(author="Wikipedia",title="Beta function, Incomplete Beta function",type=WebPage,year=2010,url="http://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function") @PublicationReference(author={"William H. Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"},title="Numerical Recipes in C, Second Edition",type=Book,year=1992,pages=227,notes="Function betai",url="http://www.nrbook.com/a/bookcpdf.php") public static double regularizedIncompleteBetaFunction(double a, double b, double x)
a
- Parameter a to the Beta functionb
- Parameter b to the Beta functionx
- Parameter x to for the integral from 0 to x@PublicationReference(author={"William H. Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"}, title="Numerical Recipes in C, Second Edition", type=Book, year=1992, pages=227, notes="Incomplete Beta Function continued fraction terms for Lentz\'s method", url="http://www.nrbook.com/a/bookcpdf.php") protected static double incompleteBetaContinuedFraction(double a, double b, double x)
a
- Parameter a to the beta continued fractionb
- Parameter b to the beta continued fractionx
- Parameter x to the beta continued fraction@PublicationReference(author="Wikipedia", title="Dirichlet distribution", type=WebPage, year=2009, url="http://en.wikipedia.org/wiki/Dirichlet_distribution", notes="Multinomial Beta Function found in the \"Probability density function\" section.") public static double logMultinomialBetaFunction(Vector input)
input
- Input vector to consider.@PublicationReference(author={"Tov Are","Paul van Keep","Mike Cowlishaw","Pierre Baillargeon","Bill Wilkinson","Patricia Shanahan","Joseph Bowbeer","Charles Thomas","Joel Crisp","Eric Nagler","Daniel Leuck","William Brogden","Yves Bossu","Chad Loder"}, title="Java Gotchas", type=WebPage, year=2011, url="http://202.38.93.17/bookcd/285/1.iso/faq/gloss/gotchas.html#OVERFLOW", notes="") public static int checkedAdd(int a, int b) throws java.lang.ArithmeticException
ArithmeticException
is thrown.a
- The first integer to addb
- The second integer to addjava.lang.ArithmeticException
- If an underflow or overflow will occur upon adding a and b@PublicationReference(author={"Tov Are","Paul van Keep","Mike Cowlishaw","Pierre Baillargeon","Bill Wilkinson","Patricia Shanahan","Joseph Bowbeer","Charles Thomas","Joel Crisp","Eric Nagler","Daniel Leuck","William Brogden","Yves Bossu","Chad Loder"}, title="Java Gotchas", type=WebPage, year=2011, url="http://202.38.93.17/bookcd/285/1.iso/faq/gloss/gotchas.html#OVERFLOW", notes="") public static int checkedMultiply(int a, int b) throws java.lang.ArithmeticException
ArithmeticException
is thrown.a
- The first integer to multiplyb
- The second integer to multiplyjava.lang.ArithmeticException
- If an overflow will occur upon multiplying a and bpublic static double log1Plus(double x)
x
- The value.public static double expMinus1Plus(double x)
x
- The value.@PublicationReference(title="Accurately Computing log(1 \u2212 exp(\u2212|a|)): Assessed by the Rmpfr package", author="Martin Machler", year=2012, type=WebPage, url="http://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf") public static double log1MinusExp(double x)
x
- The value.@PublicationReference(title="Accurately Computing log(1 \u2212 exp(\u2212|a|)): Assessed by the Rmpfr package", author="Martin Machler", year=2012, type=WebPage, url="http://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf") public static double log1PlusExp(double x)
x
- The value.