Class Arithmetic
- All Implemented Interfaces:
Serializable
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final doubleprotected static final doubleprotected static final double[]protected static final double[]protected static final doubleprotected static final long[]protected static final doubleprotected static final doubleprotected static final doubleprotected static final doubleprotected static final doubleprotected static final double -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMakes this class non instantiable, but still let's others inherit from it. -
Method Summary
Modifier and TypeMethodDescriptionstatic doublebinomial(double n, long k) Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k".static doublebinomial(long n, long k) Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k".static longceil(double value) Returns the smallestlong >= value.static doublechbevl(double x, double[] coef, int N) Evaluates the series of Chebyshev polynomials Ti at argument x/2.static doublefactorial(int k) Instantly returns the factorial k!.static longfloor(double value) Returns the largestlong <= value.static doublelog(double base, double value) Returns logbasevalue.static doublelog10(double value) Returns log10value.static doublelog2(double value) Returns log2value.static doublelogFactorial(int k) Returns log(k!).static longlongFactorial(int k) Instantly returns the factorial k!.static doublestirlingCorrection(int k) Returns the StirlingCorrection.
-
Field Details
-
MACHEP
protected static final double MACHEP- See Also:
-
MAXLOG
protected static final double MAXLOG- See Also:
-
MINLOG
protected static final double MINLOG- See Also:
-
MAXGAM
protected static final double MAXGAM- See Also:
-
SQTPI
protected static final double SQTPI- See Also:
-
SQRTH
protected static final double SQRTH- See Also:
-
LOGPI
protected static final double LOGPI- See Also:
-
big
protected static final double big- See Also:
-
biginv
protected static final double biginv- See Also:
-
logFactorials
protected static final double[] logFactorials -
longFactorials
protected static final long[] longFactorials -
doubleFactorials
protected static final double[] doubleFactorials
-
-
Constructor Details
-
Arithmetic
protected Arithmetic()Makes this class non instantiable, but still let's others inherit from it.
-
-
Method Details
-
binomial
public static double binomial(double n, long k) Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". The binomial coefficient is defined as (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ).- kinvalid input: '<'0: 0.
- k==0: 1.
- k==1: n.
- else: (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ).
- k==0: 1.
- Returns:
- the binomial coefficient.
- kinvalid input: '<'0: 0.
-
binomial
public static double binomial(long n, long k) Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". The binomial coefficient is defined as- kinvalid input: '<'0: 0.
- k==0 || k==n: 1.
- k==1 || k==n-1: n.
- else: (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ).
- k==0 || k==n: 1.
- Returns:
- the binomial coefficient.
- kinvalid input: '<'0: 0.
-
ceil
public static long ceil(double value) Returns the smallestlong >= value.- Examples:
1.0 -> 1, 1.2 -> 2, 1.9 -> 2. This method is safer than using (long) Math.ceil(value), because of possible rounding error. - Examples:
-
chbevl
Evaluates the series of Chebyshev polynomials Ti at argument x/2. The series is given byN-1 - ' y = > coef[i] T (x/2) - i i=0Coefficients are stored in reverse order, i.e. the zero order term is last in the array. Note N is the number of coefficients, not the order.If coefficients are for the interval a to b, x must have been transformed to x -> 2(2x - b - a)/(b-a) before entering the routine. This maps x from (a, b) to (-1, 1), over which the Chebyshev polynomials are defined.
If the coefficients are for the inverted interval, in which (a, b) is mapped to (1/b, 1/a), the transformation required is x -> 2(2ab/x - b - a)/(b-a). If b is infinity, this becomes x -> 4a/x - 1.
SPEED:
Taking advantage of the recurrence properties of the Chebyshev polynomials, the routine requires one more addition per loop than evaluating a nested polynomial of the same degree.
- Parameters:
x- argument to the polynomial.coef- the coefficients of the polynomial.N- the number of coefficients.- Throws:
ArithmeticException
-
factorial
public static double factorial(int k) Instantly returns the factorial k!.- Parameters:
k- must hold k >= 0.
-
floor
public static long floor(double value) Returns the largestlong <= value.- Examples:
1.0 -> 1, 1.2 -> 1, 1.9 -> 1- 2.0 -> 2, 2.2 -> 2, 2.9 -> 2
- This method is safer than using (long) Math.floor(value), because of possible rounding error.
- Examples:
-
log
public static double log(double base, double value) Returns logbasevalue. -
log10
public static double log10(double value) Returns log10value. -
log2
public static double log2(double value) Returns log2value. -
logFactorial
public static double logFactorial(int k) Returns log(k!). Tries to avoid overflows. For kinvalid input: '<'30 simply looks up a table in O(1). For k>=30 uses stirlings approximation.- Parameters:
k- must hold k >= 0.
-
longFactorial
Instantly returns the factorial k!.- Parameters:
k- must hold k >= 0 invalid input: '&'invalid input: '&' k < 21.- Throws:
IllegalArgumentException
-
stirlingCorrection
public static double stirlingCorrection(int k) Returns the StirlingCorrection.Correction term of the Stirling approximation for log(k!) (series in 1/k, or table values for small k) with int parameter k.
log k! = (k + 1/2)log(k + 1) - (k + 1) + (1/2)log(2Pi) + stirlingCorrection(k + 1)
log k! = (k + 1/2)log(k) - k + (1/2)log(2Pi) + stirlingCorrection(k)
-