logGamma static method

double logGamma(
  1. double x
)

Gamma function ported from the apache math package.

This should be removed if the apache math lib gets in use by HortonMachine.

Returns the natural logarithm of the gamma function Γ(x).

The implementation of this method is based on:

@param x Value. @return log(Γ(x))

Implementation

static double logGamma(double x) {
  double ret;

  if (x.isNaN || (x <= 0.0)) {
    ret = double.nan;
  } else {
    var g = 607.0 / 128.0;

    var sum = 0.0;
    for (var i = LANCZOS.length - 1; i > 0; --i) {
      sum = sum + (LANCZOS[i] / (x + i));
    }
    sum = sum + LANCZOS[0];

    var tmp = x + g + .5;
    ret =
        ((x + .5) * math.log(tmp)) - tmp + HALF_LOG_2_PI + math.log(sum / x);
  }

  return ret;
}