SSJ
V. 2.6.

umontreal.iro.lecuyer.util
Class Num

java.lang.Object
  extended by umontreal.iro.lecuyer.util.Num

public class Num
extends Object

This class provides a few constants and some methods to compute numerical quantities such as factorials, combinations, gamma functions, and so on.


Field Summary
static int DBL_DIG
          Number of decimal digits of precision in a double.
static double DBL_EPSILON
          Difference between 1.0 and the smallest double greater than 1.0.
static int DBL_MAX_10_EXP
          Largest int x such that 10x is representable (approximately) as a double.
static int DBL_MAX_EXP
          Largest int x such that 2x-1 is representable (approximately) as a double.
static double DBL_MIN
          Smallest normalized positive floating-point double.
static int DBL_MIN_EXP
          Smallest int x such that 2x-1 is representable (approximately) as a normalised double.
static double EBASE
          The constant e.
static double EULER
          The Euler-Mascheroni constant.
static double ILN2
          The values of 1/ln 2.
static double IRAC2
          The value of 1/(2)1/2.
static double LN_DBL_MIN
          Natural logarithm of DBL_MIN.
static double LN2
          The values of ln 2.
static double MAXINTDOUBLE
          Largest integer n0 = 253 such that any integer n <= n0 is represented exactly as a double.
static double MAXTWOEXP
          Powers of 2 up to MAXTWOEXP are stored exactly in the array TWOEXP.
static double RAC2
          The value of (2)1/2.
static double[] TEN_NEG_POW
          Contains the precomputed negative powers of 10.
static double[] TWOEXP
          Contains the precomputed positive powers of 2.
 
Method Summary
static double bernoulliPoly(int n, double x)
          Evaluates the Bernoulli polynomial Bn(x) of degree n at x.
static double besselK025(double x)
          Returns the value of K1/4(x), where Ka is the modified Bessel's function of the second kind.
static double[][] calcMatStirling(int m, int n)
          Computes and returns the Stirling numbers of the second kind
static double combination(int n, int s)
          Returns the number of different combinations of s objects amongst n.
static double digamma(double x)
          Returns the value of the logarithmic derivative of the Gamma function ψ(x) = Γ'(x)/Γ(x).
static double erf(double x)
          Returns the value of erf(x), the error function.
static double erfc(double x)
          Returns the value of erfc(x), the complementary error function.
static double erfcInv(double u)
          Returns the value of erfc -1(u), the inverse of the complementary error function.
static double erfInv(double u)
          Returns the value of erf -1(u), the inverse of the error function.
static double evalCheby(double[] a, int n, double x)
          Evaluates a series of Chebyshev polynomials Tj at x over the basic interval [- 1,  1].
static double evalChebyStar(double[] a, int n, double x)
          Evaluates a series of shifted Chebyshev polynomials Tj* at x over the basic interval [0,  1].
static double expBesselK1(double x, double y)
          Returns the value of exK1(y), where K1 is the modified Bessel function of the second kind of order 1.
static double factoPow(int n)
          Returns the value of factorial(n)/nn.
static double factorial(int n)
          Returns the value of factorial n.
static double gammaRatioHalf(double x)
          Returns the value of the ratio Γ(x + 1/2)/Γ(x) of two gamma functions.
static int gcd(int x, int y)
          Returns the greatest common divisor (gcd) of x and y.
static long gcd(long x, long y)
          Returns the greatest common divisor (gcd) of x and y.
static double harmonic(long n)
          Computes the n-th harmonic number Hn = ∑j=1n1/j.
static double harmonic2(long n)
          .
static double lnBeta(double lam, double nu)
          Computes the natural logarithm of the Beta function B(λ, ν).
static double lnCombination(int n, int s)
          Returns the natural logarithm ofnumber of different combinations of s objects amongst n.
static double lnFactorial(int n)
          Returns the value of the natural logarithm of factorial n.
static double lnFactorial(long n)
          Returns the value of the natural logarithm of factorial n.
static double lnGamma(double x)
          Returns the natural logarithm of the gamma function Γ(x) evaluated at x.
static double log2(double x)
          Returns log2(x).
static double sumKahan(double[] A, int n)
          Implementation of the Kahan summation algorithm.
static double tetragamma(double x)
          Returns the value of the tetragamma function d2ψ(x)/d2x, the second derivative of the digamma function, evaluated at x.
static double trigamma(double x)
          Returns the value of the trigamma function (x)/dx, the derivative of the digamma function, evaluated at x.
static double volumeSphere(double p, int t)
          Returns the volume V of a sphere of radius 1 in t dimensions using the norm Lp.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DBL_EPSILON

public static final double DBL_EPSILON
Difference between 1.0 and the smallest double greater than 1.0.

See Also:
Constant Field Values

DBL_MAX_EXP

public static final int DBL_MAX_EXP
Largest int x such that 2x-1 is representable (approximately) as a double.

See Also:
Constant Field Values

DBL_MIN_EXP

public static final int DBL_MIN_EXP
Smallest int x such that 2x-1 is representable (approximately) as a normalised double.

See Also:
Constant Field Values

DBL_MAX_10_EXP

public static final int DBL_MAX_10_EXP
Largest int x such that 10x is representable (approximately) as a double.

See Also:
Constant Field Values

DBL_MIN

public static final double DBL_MIN
Smallest normalized positive floating-point double.

See Also:
Constant Field Values

LN_DBL_MIN

public static final double LN_DBL_MIN
Natural logarithm of DBL_MIN.

See Also:
Constant Field Values

DBL_DIG

public static final int DBL_DIG
Number of decimal digits of precision in a double.

See Also:
Constant Field Values

EBASE

public static final double EBASE
The constant e.

See Also:
Constant Field Values

EULER

public static final double EULER
The Euler-Mascheroni constant.

See Also:
Constant Field Values

RAC2

public static final double RAC2
The value of (2)1/2.

See Also:
Constant Field Values

IRAC2

public static final double IRAC2
The value of 1/(2)1/2.

See Also:
Constant Field Values

LN2

public static final double LN2
The values of ln 2.

See Also:
Constant Field Values

ILN2

public static final double ILN2
The values of 1/ln 2.

See Also:
Constant Field Values

MAXINTDOUBLE

public static final double MAXINTDOUBLE
Largest integer n0 = 253 such that any integer n <= n0 is represented exactly as a double.

See Also:
Constant Field Values

MAXTWOEXP

public static final double MAXTWOEXP
Powers of 2 up to MAXTWOEXP are stored exactly in the array TWOEXP.

See Also:
Constant Field Values

TWOEXP

public static final double[] TWOEXP
Contains the precomputed positive powers of 2. One has TWOEXP[j]= 2j, for j = 0,..., 64.


TEN_NEG_POW

public static final double[] TEN_NEG_POW
Contains the precomputed negative powers of 10. One has TEN_NEG_POW[j]= 10-j, for j = 0,…, 16.

Method Detail

gcd

public static int gcd(int x,
                      int y)
Returns the greatest common divisor (gcd) of x and y.

Parameters:
x - integer
y - integer
Returns:
the GCD of x and y

gcd

public static long gcd(long x,
                       long y)
Returns the greatest common divisor (gcd) of x and y.

Parameters:
x - integer
y - integer
Returns:
the GCD of x and y

combination

public static double combination(int n,
                                 int s)
Returns the number of different combinations of s objects amongst n.

Parameters:
n - total number of objects
s - number of chosen objects on a combination
Returns:
the combination of s objects amongst n

lnCombination

public static double lnCombination(int n,
                                   int s)
Returns the natural logarithm ofnumber of different combinations of s objects amongst n.

Parameters:
n - total number of objects
s - number of chosen objects on a combination
Returns:
the natural log of the combination

factorial

public static double factorial(int n)
Returns the value of factorial n.

Parameters:
n - the integer for which the factorial must be computed
Returns:
the value of n!

lnFactorial

public static double lnFactorial(int n)
Returns the value of the natural logarithm of factorial n. Gives 16 decimals of precision (relative error < 0.5×10-15).

Parameters:
n - argument of the log-factorial
Returns:
natural logarithm of n factorial

lnFactorial

public static double lnFactorial(long n)
Returns the value of the natural logarithm of factorial n. Gives 16 decimals of precision (relative error < 0.5×10-15).

Parameters:
n - argument of the log-factorial
Returns:
natural logarithm of n factorial

factoPow

public static double factoPow(int n)
Returns the value of factorial(n)/nn.

Parameters:
n - integer
Returns:
the value of n!/nn

calcMatStirling

public static double[][] calcMatStirling(int m,
                                         int n)
Computes and returns the Stirling numbers of the second kind

Parameters:
m - number of rows of the allocated matrix
n - number of columns of the allocated matrix
Returns:
the matrix of Stirling numbers

log2

public static double log2(double x)
Returns log2(x).

Parameters:
x - the value for which the logarithm must be computed
Returns:
the value of log2(x)

lnGamma

public static double lnGamma(double x)
Returns the natural logarithm of the gamma function Γ(x) evaluated at x. Gives 16 decimals of precision, but is implemented only for x > 0.

Parameters:
x - the value for which the lnGamma function must be computed
Returns:
the natural logarithm of the gamma function

lnBeta

public static double lnBeta(double lam,
                            double nu)
Computes the natural logarithm of the Beta function B(λ, ν). It is defined in terms of the Gamma function as

B(λ, ν) = 1#1

with lam = λ and nu = ν.


digamma

public static double digamma(double x)
Returns the value of the logarithmic derivative of the Gamma function ψ(x) = Γ'(x)/Γ(x).


trigamma

public static double trigamma(double x)
Returns the value of the trigamma function (x)/dx, the derivative of the digamma function, evaluated at x.


tetragamma

public static double tetragamma(double x)
Returns the value of the tetragamma function d2ψ(x)/d2x, the second derivative of the digamma function, evaluated at x.


gammaRatioHalf

public static double gammaRatioHalf(double x)
Returns the value of the ratio Γ(x + 1/2)/Γ(x) of two gamma functions. This ratio is evaluated in a numerically stable way. Restriction: x > 0.


sumKahan

public static double sumKahan(double[] A,
                              int n)
Implementation of the Kahan summation algorithm. Sums the first n elements of A and returns the sum. This algorithm is more precise than the naive algorithm. See http://en.wikipedia.org/wiki/Kahan_summation_algorithm.


harmonic

public static double harmonic(long n)
Computes the n-th harmonic number Hn = ∑j=1n1/j.


harmonic2

public static double harmonic2(long n)
. Computes the sum

2#2  3#3,

where the symbol means that the term with j = 0 is excluded from the sum.


volumeSphere

public static double volumeSphere(double p,
                                  int t)
Returns the volume V of a sphere of radius 1 in t dimensions using the norm Lp. It is given by the formula

V = ([2Γ(1 + 1/p)]t)/Γ(1 + t/p),        p > 0,

where Γ is the gamma function. The case of the sup norm L is obtained by choosing p = 0. Restrictions: p >=  0 and t >= 1.

Parameters:
p - index of the used norm
t - number of dimensions
Returns:
the volume of a sphere

bernoulliPoly

public static double bernoulliPoly(int n,
                                   double x)
Evaluates the Bernoulli polynomial Bn(x) of degree n at x. Only degrees n <= 8 are programmed for now. The first Bernoulli polynomials of even degree are:
B0(x) = 1  
B2(x) = x2 - x + 1/6  
B4(x) = x4 -2x3 + x2 - 1/30  
B6(x) = x6 -3x5 +5x4/2 - x2/2 + 1/42  
B8(x) = x8 -4x7 +14x6/3 - 7x4/3 + 2x2/3 - 1/30.  


evalCheby

public static double evalCheby(double[] a,
                               int n,
                               double x)
Evaluates a series of Chebyshev polynomials Tj at x over the basic interval [- 1,  1]. It uses the method of Clenshaw, i.e., computes and returns

y = 4#4 + ∑j=1najTj(x).

Parameters:
a - coefficients of the polynomials
n - largest degree of polynomials
x - the parameter of the Tj functions
Returns:
the value of a series of Chebyshev polynomials Tj.

evalChebyStar

public static double evalChebyStar(double[] a,
                                   int n,
                                   double x)
Evaluates a series of shifted Chebyshev polynomials Tj* at x over the basic interval [0,  1]. It uses the method of Clenshaw, i.e., computes and returns

y = [tex2html_wrap_indisplay816] + ∑j=1najTj*(x).

Parameters:
a - coefficients of the polynomials
n - largest degree of polynomials
x - the parameter of the Tj* functions
Returns:
the value of a series of Chebyshev polynomials Tj*.

besselK025

public static double besselK025(double x)
Returns the value of K1/4(x), where Ka is the modified Bessel's function of the second kind. The relative error on the returned value is less than 0.5×10-6 for x > 10-300.

Parameters:
x - value at which the function is calculated
Returns:
the value of K1/4(x)

expBesselK1

public static double expBesselK1(double x,
                                 double y)
Returns the value of exK1(y), where K1 is the modified Bessel function of the second kind of order 1. Restriction: y > 0.


erf

public static double erf(double x)
Returns the value of erf(x), the error function. It is defined as

erf(x) = 2/[(π)1/2]∫0xdt e-t2.

Parameters:
x - value at which the function is calculated
Returns:
the value of erf(x)

erfc

public static double erfc(double x)
Returns the value of erfc(x), the complementary error function. It is defined as

erfc(x) = 2/[(π)1/2]∫xdt e-t2.

Parameters:
x - value at which the function is calculated
Returns:
the value of erfc(x)

erfInv

public static double erfInv(double u)
Returns the value of erf -1(u), the inverse of the error function. If u =  erf(x), then x =  erf -1(u).

Parameters:
u - value at which the function is calculated
Returns:
the value of erfInv(u)

erfcInv

public static double erfcInv(double u)
Returns the value of erfc -1(u), the inverse of the complementary error function. If u =  erfc(x), then x =  erfc -1(u).

Parameters:
u - value at which the function is calculated
Returns:
the value of erfcInv(u)

SSJ
V. 2.6.

To submit a bug or ask questions, send an e-mail to Pierre L'Ecuyer.