|
SoftNet-Consult Java Utility Library | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.softnetConsult.utils.math.MathTools
public final class MathTools
This class is a collection of static mathematical utility methods and functions.
This product includes software developed by the
SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.
File: MathTools.java
Library API version: "2.02"
Java compliance version: "1.5"
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:
1. Redistributions of source code must retain the above
acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the
SoftNet-Consult Java Utility Library project, the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived
software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library
project and its contributors.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Field Summary | |
---|---|
private static int[] |
factorialsInt
Lookup tables of factorials that can be represented as an int. |
private static long[] |
factorialsLong
Lookup tables of factorials that can be represented as a long. |
private static double[] |
gammaCoefficients
Used for fast calculation of the gamma function. |
private static double |
ln2Pi
Constant used for the canculation of the natural logarithm of the gamma function. |
private static int[] |
pow10
Powers of 10 as integers for fast fetching. |
private static long[] |
pow10L
Powers of 10 as longs for fast fetchng. |
private static double |
sqrt2Pi
Constant used for the calculation of the gamma function. |
Constructor Summary | |
---|---|
private |
MathTools()
Prevents instances of this class from being created as this class contains only static utility methods. |
Method Summary | ||
---|---|---|
static
|
compare(TX x,
TY y)
Compares the specified parameters. |
|
static double |
factorial(double x)
Computes an approximation to the factorial of a specified number using the fact that x! = gamma(x + 1) . |
|
static int |
factorial(int x)
Computes the factorial of a specified number. |
|
static long |
factorial(long x)
Computes the factorial of a specified number. |
|
static double |
gamma(double x)
Computes an approximation to the gamma function. |
|
static double |
lnFactorial(double x)
Computes an approximation to the natural logarithm of the factorial of a specified number. |
|
static double |
lnGamma(double x)
Computes an approximation to the natiral logarithm of the gamma function. |
|
static double |
log(double base,
double x)
Returns the logarithm of the specified value to the specified base. |
|
static double |
poisson(double lambda,
int k)
Probability mass function of the Poisson distribution. |
|
static int |
pow10(int exp)
Raises 10 to a positive integer power. |
|
static long |
pow10L(int exp)
Raises 10 to a positive integer power. |
|
static double |
round(double val,
int pow10)
Rounds the specified number to the nearest specified power of 10. |
|
static float |
round(float val,
int pow10)
Rounds the specified number to the nearest specified power of 10. |
|
static int |
round(int val,
int pow10)
Rounds the specified integer to the nearest specified power of 10. For example: round(12345, 0) = 12345 round(12345, 1) = 12340 round(12345, 2) = 12300 round(12345, 3) = 12000 round(12345, 4) = 10000 round(12345, 5) = 0 round(54321, 5) = 100000 |
|
static long |
round(long val,
int pow10)
Rounds the specified long integer to the nearest specified power of 10. For example: round(12345, 0) = 12345 round(12345, 1) = 12340 round(12345, 2) = 12300 round(12345, 3) = 12000 round(12345, 4) = 10000 round(12345, 5) = 0 round(54321, 5) = 100000 |
|
static double |
transToUnitInterval(double value,
double valueRangeMin,
double valueRangeMax)
Linearly transforms the specified value value from the specified value range
interval [valueRangeMin, valueRangeMax] into the unit interval [0, 1] . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final int[] pow10
private static final long[] pow10L
private static final int[] factorialsInt
private static final long[] factorialsLong
private static final double[] gammaCoefficients
private static final double sqrt2Pi
private static final double ln2Pi
Constructor Detail |
---|
private MathTools()
Method Detail |
---|
public static double gamma(double x)
x
- The real parameter to the gamma function.
x
.public static double lnGamma(double x)
Double.NaN
.
x
- A positive real number.
ln(gamma(x))
.public static int factorial(int x)
x
- A number between 0 and 12 inclusive (the factorial of larger numbers cannot be represented as int).
x!
.public static long factorial(long x)
x
- A number between 0 and 20 inclusive (the factorial of larger numbers cannot be represented as long).
x!
.public static double factorial(double x)
x! = gamma(x + 1)
.
x
- A number.
x!
calculated as MathTools.gamma(x + 1.0)
.gamma(double)
public static double lnFactorial(double x)
1.0
this method uses a direct approximation to the natural
logarithm of the gamma function (i.e. ln(x!) = MathTools.lnGamma(x + 1.0))
;
for 1.0
and smaller values this method explicitly
computes the factorial using MathTools.factorial(x)
and then takes the natural
logarithm using java.lang.Math.log()
.
x
- A number larger than -1
.
ln(x!)
.lnGamma(double)
,
factorial(double)
public static double poisson(double lambda, int k)
lambda
- LAMBDA-Poisson parameter.k
- K-Poisson parameter.
public static double transToUnitInterval(double value, double valueRangeMin, double valueRangeMax)
value
from the specified value range
interval [valueRangeMin, valueRangeMax]
into the unit interval [0, 1]
.
value
- The value to transform.valueRangeMin
- Inclusive minimum of the value's original range.valueRangeMax
- Inclusive maximum of the value's original range.
public static double log(double base, double x)
base
- The base for the logarithm.x
- The value to take the logarithm of.
x
to base base
.public static <TX extends java.lang.Number,TY extends java.lang.Number> int compare(TX x, TY y)
compareTo
methods if both
parameters are of same type and if that type is a standard Java type that inherits
directly from Number
and implements Comparable
. If this fails,
then the values returned by doubleValue()
method are compared.
TX
- A Number
type.TY
- A Number
type.x
- A numbery
- A number.
public static int pow10(int exp)
exp
- Power to which to raise 10.
exp
is negative - 0
, otherwise 10^exp
.
java.lang.IllegalArgumentException
- If exp > 9
as the result would be too
large to be represented in a Java int
.public static long pow10L(int exp)
exp
- Power to which to raise 10.
exp
is negative - 0
, otherwise 10^exp
.
java.lang.IllegalArgumentException
- If exp > 18
as the result would be too
large to be represented in a Java long
.public static int round(int val, int pow10)
round(12345, 0)
= 12345 round(12345, 1)
= 12340 round(12345, 2)
= 12300 round(12345, 3)
= 12000 round(12345, 4)
= 10000 round(12345, 5)
= 0 round(54321, 5)
= 100000
val
- A number.pow10
- The power of 10 to which to round,
only non-negative positive values are permitted.
val
that is a multiple of 10^pow10
.
Midway rounding is towards the even number.public static long round(long val, int pow10)
round(12345, 0)
= 12345 round(12345, 1)
= 12340 round(12345, 2)
= 12300 round(12345, 3)
= 12000 round(12345, 4)
= 10000 round(12345, 5)
= 0 round(54321, 5)
= 100000
val
- A number.pow10
- The power of 10 to which to round,
only non-negative positive values are permitted.
val
that is a multiple of 10^pow10
.
Midway rounding is towards the even number.public static double round(double val, int pow10)
round(12345.12345, 0)
= 12345.0 round(12345.12345, 1)
= 12340.0 round(12345.12345, 2)
= 12300.0 round(12345.12345, 3)
= 12000.0 round(12345.12345, 4)
= 10000.0 round(12345.12345, 5)
= 0.0 round(54321.0, 5)
= 100000.0 round(12345.12345, -1)
= 12345.1 round(12345.12345, -2)
= 12345.12 round(12345.12345, -3)
= 12345.123 round(12345.12345, -4)
= 12345.1234 round(12345.12345, -5)
= 12345.12345 round(12345.12345, -6)
= 12345.12345
val
- A number.pow10
- The power of 10 to which to round.
val
that is a multiple of 10^pow10
.
Midway rounding is towards the even number.public static float round(float val, int pow10)
round(12345.12345, 0)
= 12345.0 round(12345.12345, 1)
= 12340.0 round(12345.12345, 2)
= 12300.0 round(12345.12345, 3)
= 12000.0 round(12345.12345, 4)
= 10000.0 round(12345.12345, 5)
= 0.0 round(54321.0, 5)
= 100000.0 round(12345.12345, -1)
= 12345.1 round(12345.12345, -2)
= 12345.12 round(12345.12345, -3)
= 12345.123 round(12345.12345, -4)
= 12345.1234 round(12345.12345, -5)
= 12345.12345 round(12345.12345, -6)
= 12345.12345
val
- A number.pow10
- The power of 10 to which to round.
val
that is a multiple of 10^pow10
.
Midway rounding is towards the even number.
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |