jsat.math
Class SpecialMath
- java.lang.Object
-
- jsat.math.SpecialMath
-
public class SpecialMath extends java.lang.ObjectThis class provides static methods for computing accurate approximations to many special functions.
All methods should return absolute differences of less than 10-9 for all reasonable values. Unreasonable values would be those in areas of high change (such as those approaching positive / negative infinity).
-
-
Field Summary
Fields Modifier and Type Field and Description static doubleEULER_MASCHERONI
-
Constructor Summary
Constructors Constructor and Description SpecialMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static doublebernoulli(int n)Computes an approximation to the n'th Bernoulli number Bn.static doublebeta(double z, double w)Computes the Beta function B(z,w)static doublebetaIncReg(double x, double a, double b)Computes the regularized incomplete beta function, Ix(a, b).static doubledigamma(double x)Computes the value of the digamma function, Ψ(x), which is the derivative oflnGamma(double).static doubleerf(double x)static doubleerfc(double x)static doublegamma(double z)The gamma function is a generalization of the factorial function.static doublegammaIncLow(double a, double z)Computes the lower incomplete gamma function, γ(a,z).static doublegammaIncUp(double a, double z)Computes the incomplete gamma function, Γ(a,z).static doublegammaP(double a, double z)Returns the regularized gamma function P(a,z) = γ(a,z)/Γ(a).static doublegammaPSeries(double a, double z)static doublegammaQ(double a, double z)Computes the regularized gamma function Q(a,z) = Γ(a,z)/Γ(a).static doubleharmonic(double n)Computes the n'th harmonic number Hn.static doubleharmonic(double n, double m)Computes the generalized n'th harmonic number of the m'th order Hn(m).static doubleinvBetaIncReg(double p, double a, double b)Computes the inverse of the incomplete beta function, Ip-1(a,b), such thatIx(a, b)= p.static doubleinvErf(double x)static doubleinvErfc(double x)static doubleinvGammaP(double p, double a)Finds the value x such thatP(a,x)= p.static doubleinvXlnX(double y)static doublelnBeta(double z, double w)static doublelnGamma(double z)Computes the natural logarithm ofgamma(double).static doublereLnBn(int n)Computes the real part of the natural logarithm of the Bernoulli numbers.static doublezeta(double x)Computes the Riemann zeta function ζ(x) for some value of x
This method may return:
Double.NaNfor x = 1 (would be complex infinity)
NOTE: This method is not yet complete in terms of accuracy.static doublezeta(double x, double a)Implements the Hurwitz zeta function ζ(x, a)
The relative error of this implementation is less than 1e-4 for most positive values of x, though is lower for negative values of x.
-
-
-
Field Detail
-
EULER_MASCHERONI
public static final double EULER_MASCHERONI
- See Also:
- Constant Field Values
-
-
Method Detail
-
invXlnX
public static double invXlnX(double y)
-
gamma
public static double gamma(double z)
The gamma function is a generalization of the factorial function. This method provides the gamma function for values from -Infinity to Infinity.
Special Values:Double.NaNreturned if z = 0 or z =Double.NEGATIVE_INFINITY
- Parameters:
z- any real value- Returns:
- Γ(z)
-
lnGamma
public static double lnGamma(double z)
Computes the natural logarithm ofgamma(double). This method is more numerically stable than taking the log of the result of Γ(z).
Special Values:Double.POSITIVE_INFINITYreturned if z ≤ 0Double.NaNreturned if z =Double.NEGATIVE_INFINITY
- Parameters:
z- any real number value- Returns:
- Log(Γ(z))
-
digamma
public static double digamma(double x)
Computes the value of the digamma function, Ψ(x), which is the derivative oflnGamma(double).
This method may return:
-
Double.NaNfor zero and negative integer values (would be complex infinity)
This method should be accurate to an absolute difference of 10-14 for all values that are not near an asymptote.- Parameters:
x- the value to compute the digamma function at- Returns:
- the value of Ψ(x)
-
-
zeta
public static double zeta(double x)
Computes the Riemann zeta function ζ(x) for some value of x
This method may return:
-
Double.NaNfor x = 1 (would be complex infinity)
NOTE: This method is not yet complete in terms of accuracy.
- For x < -0.5, the values returned will be of the correct magnitude - but are not very accurate
- For x in [-0.5, 2.5], the values returned will be of reasonable accurate (absolute difference around 10-7), unless it is very close to 1
- For x > 2.5, the result will be very accurate (absolute difference less than 10-14
- Parameters:
x- a real valued input- Returns:
- ζ(x)
-
-
zeta
public static double zeta(double x, double a)Implements the Hurwitz zeta function ζ(x, a)
The relative error of this implementation is less than 1e-4 for most positive values of x, though is lower for negative values of x. Accuracy improves as a increases.- Parameters:
x- the first argument of the Hurwitz zeta function/a- the second argument, must be positive in all cases.- Returns:
- the value of ζ(x, a), or
Double.NaNif the result would be a Complex Infinity
-
harmonic
public static double harmonic(double n)
Computes the n'th harmonic number Hn.
Note that the relative error of this method is less than 10-6 for the entire range, and decreases as n increases.- Parameters:
n- any non-negative value- Returns:
- the value of Hn, of
Double.NaNif n < 0
-
harmonic
public static double harmonic(double n, double m)Computes the generalized n'th harmonic number of the m'th order Hn(m).- Parameters:
n- any non-negative valuem- the harmonic order- Returns:
- the value of Hn(m)
-
reLnBn
public static double reLnBn(int n)
Computes the real part of the natural logarithm of the Bernoulli numbers.
The Bernoulli zeros for odd n will returnDouble.NEGATIVE_INFINITYand for any value less than 0 will returnDouble.NaN.
Currently only accurate to an absolute difference of 10-11- Parameters:
n- the integer Bernoulli value to obtain an approximation of- Returns:
- Re(Log(Bn))
-
bernoulli
public static double bernoulli(int n)
Computes an approximation to the n'th Bernoulli number Bn. The Bernoulli numbers grow in value rapidly, and so the accuracy of this method decays quickly. n > 20 should have the correct order of magnitude, but may not have many significant figures.reLnBn(int)should be used instead when possible.- Parameters:
n- the bernoulli number to compute- Returns:
- Bn
-
erf
public static double erf(double x)
-
invErf
public static double invErf(double x)
-
erfc
public static double erfc(double x)
-
invErfc
public static double invErfc(double x)
-
beta
public static double beta(double z, double w)Computes the Beta function B(z,w)- Parameters:
z-w-- Returns:
- B(z,w)
-
lnBeta
public static double lnBeta(double z, double w)
-
betaIncReg
public static double betaIncReg(double x, double a, double b)Computes the regularized incomplete beta function, Ix(a, b). The result of which is always in the range [0, 1]- Parameters:
x- any value in the range [0, 1]a- any value ≥ 0b- any value ≥ 0- Returns:
- the result in a range of [0,1]
-
invBetaIncReg
public static double invBetaIncReg(double p, double a, double b)Computes the inverse of the incomplete beta function, Ip-1(a,b), such thatIx(a, b)= p. The returned value, x, will always be in the range [0,1]. The input p, must also be in the range [0,1].- Parameters:
p- any value in the range [0,1]a- any value ≥ 0b- any value ≥ 0- Returns:
- the value x, such that
Ix(a, b)will return p.
-
gammaQ
public static double gammaQ(double a, double z)Computes the regularized gamma function Q(a,z) = Γ(a,z)/Γ(a).
This method is more numerically stable and accurate than computing it via the direct method, and is always in the range [0,1].
Note: The this method returnsDouble.NaNfor a<0, though real values of Q(a,z) do exist- Parameters:
a- any value ≥ 0z- any value > 0- Returns:
- Q(a,z)
-
gammaPSeries
public static double gammaPSeries(double a, double z)
-
gammaP
public static double gammaP(double a, double z)Returns the regularized gamma function P(a,z) = γ(a,z)/Γ(a).
This method is more numerically stable and accurate than computing it via the direct method, and is always in the range [0,1].
Note: The this method returnsDouble.NaNfor a<0, though real values of P(a,z) do exist- Parameters:
a- any value ≥ 0z- any value > 0- Returns:
- P(a,z)
-
invGammaP
public static double invGammaP(double p, double a)Finds the value x such thatP(a,x)= p.- Parameters:
a- any real valuep- and value in the range [0, 1]- Returns:
- the inverse
-
gammaIncUp
public static double gammaIncUp(double a, double z)Computes the incomplete gamma function, Γ(a,z).
ReturnsDouble.NaNfor z ≤ 0- Parameters:
a- any value (-∞, ∞)z- any value > 0- Returns:
- Γ(a,z)
-
gammaIncLow
public static double gammaIncLow(double a, double z)Computes the lower incomplete gamma function, γ(a,z).
ReturnsDouble.NaNfor z ≤ 0- Parameters:
a- any value (-∞, ∞)z- any value > 0- Returns:
- γ(a,z)
-
-
DataMelt 3.0 © DataMelt by jWork.ORG