# lnfact¶

## Purpose¶

Computes the natural log of the factorial function and can be used to compute log gamma.

## Format¶

y = lnfact(x)
Parameters

x (NxK matrix or N-dimensional array) – all elements must be positive.

Returns

y (NxK matrix) – containing the natural log of the factorial of each of the elements in x.

## Examples¶

x = { 100, 500, 1000 };
y = lnfact(x);

    363.73938
y = 2611.3305
5912.1282


## Remarks¶

For integer x, this is (approximately) ln(x!). However, the computation is done using a formula, and the function is defined for non integer x.

In most formulae in which the factorial operator appears, it is possible to avoid computing the factorial directly, and to use lnfact() instead. The advantage of this is that lnfact does not have the overflow problems that the factorial (!) operator has.

For $$x > 1$$, this function has at least 6 digit accuracy, for $$x > 4$$ it has at least 9 digit accuracy, and for $$x > 10$$ it has at least 12 digit accuracy. For $$0 < x < 1$$, accuracy is not known completely but is probably at least 6 digits.

Sometimes log gamma is required instead of log factorial. These functions are related by:

lngamma(x) = lnfact(x - 1);


## Technical Notes¶

For $$x > 1$$, Stirling’s formula is used.

For $$0 < x <= 1$$, ln(gamma(x+1)) is used.

lnfact.src