gammaln

Natural logarithm of the gamma function.

Category: Mathematics > Elementary Math

Syntax

Y = gammaln(X)

Returns log(gamma(X)) for each element, computed so that it does not overflow.

Description

gamma(172) already overflows, but its logarithm is only a few hundred. gammaln works with the logarithm throughout, so it handles arguments of any practical size. This is how large binomial coefficients and factorial ratios are computed without overflow: exp(gammaln(n+1) - gammaln(k+1) - gammaln(n-k+1)).

Input arguments

X
Real array.

Output arguments

Y
Array of the same size as X.

Example

Beyond the range of gamma

gammaln([10 100 1000])
exp(gammaln(51) - gammaln(26) - gammaln(26))
Output
ans = 1×3

   1.0e+03 *

    0.0128    0.3591    5.9052
ans = 1.2641e+14

See also

gamma factorial nchoosek psi