Computes the logarithm of the absolute value of the binomial coefficient.
Valid for any real n and for integer k.
k will be rounded if it is not an integer.
340829
function lchoose(n, k) {
      k = Math.round(k);
      if (utils.hasNaN(n, k)) { return NaN; }
      if (k < 0) { return -Infinity; }
      if (k === 0) { return 0; }
      if (k === 1) { return Math.log(Math.abs(n)); }
      if (n < 0) { return lchoose(-n + k - 1, k); }
      if (n === Math.round(n)) {
         if (n < k) { return -Infinity; }
         if (n - k < 2) { return lchoose(n, n - k); }

         return lfastchoose(n, k);
      }
      if (n < k - 1) { return lfastchoose2(n, k); }

      return lfastchoose(n, k);
   }
569422
function abs()
    {
        $temp = new Math_BigInteger();

        switch (MATH_BIGINTEGER_MODE) {
            case MATH_BIGINTEGER_MODE_GMP:
                $temp->value = gmp_abs($this->value);
                break;
            case MATH_BIGINTEGER_MODE_BCMATH:
                $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value;
                break;
            default:
                $temp->value = $this->value;
        }

        return $temp;
    }
867563
function loggamma(x) {
        var v = 1.0,
            w = 1.0 / (x * x),
            ret;
        while (x < N) {
            v = v * x;
            x = x + 1;
        }
        ret = B16 / (16 * 15);
        ret = ret * w + B14 / (14 * 13);
        ret = ret * w + B12 / (12 * 11);
        ret = ret * w + B10 / (10 *  9);
        ret = ret * w + B8  / (8 *  7);
        ret = ret * w + B6  / (6 *  5);
        ret = ret * w + B4  / (4 *  3);
        ret = ret * w + B2  / (2 *  1);
        ret = ret / x + 0.5 * LOG_2PI - Math.log(v) - x + (x - 0.5) * Math.log(x);
        return ret;
    }
312424
public static double log(double d, double base) {
        if(base==1.0 || base<=0.0) {
            throw new IllegalArgumentException("Invalid base for logarithm.");
        }
        return Math.log(d)/Math.log(base);
    }
639029
def log_ncr(a, b):
    
    val = gammaln(a+1) - gammaln(a-b+1) - gammaln(b+1)
    return val
298999
public int power(int x, int n) {
    assert(x >= 0 && x < getFieldSize());
    if (n == 0) {
      return 1;
    }
    if (x == 0) {
      return 0;
    }
    x = logTable[x] * n;
    if (x < primitivePeriod) {
      return powTable[x];
    }
    x = x % primitivePeriod;
    return powTable[x];
  }
1018307
public double logLikelihood() {
    double loglik = 0;
    for (int i = 0; i < scores.length; i++) {
      loglik += Math.log(classes[i] == 0 ? 1 - scores[i] : scores[i]);
    }
    return loglik;
  }
551252
def binomial_coefficient(k)
	    (0...k).inject(1) {|m,i| (m * (degree - i)) / (i + 1) }
	end
596002
private Rational doubleSum(int n) {
        Rational resul = Rational.ZERO;
        for (int k = 0; k <= n; k++) {
            Rational jsum = Rational.ZERO;
            BigInteger bin = BigInteger.ONE;
            for (int j = 0; j <= k; j++) {
                BigInteger jpown = BigInteger.valueOf(j).pow(n);
                if (j % 2 == 0) {
                    jsum = jsum.add(bin.multiply(jpown));
                } else {
                    jsum = jsum.subtract(bin.multiply(jpown));
                }
                /* update binomial(k,j) recursively
                 */
                bin = bin.multiply(BigInteger.valueOf(k - j)).divide(BigInteger.valueOf(j + 1));
            }
            resul = resul.add(jsum.divide(BigInteger.valueOf(k + 1)));
        }
        return resul;
    }
927642
public static double choose(int n, int k) {
        if (n < 0 || k < 0) {
            throw new IllegalArgumentException(String.format("Invalid n = %d, k = %d", n, k));
        }

        if (n < k) {
            return 0.0;
        }

        return Math.floor(0.5 + Math.exp(logChoose(n, k)));
    }
625598
public static double log(double x) {
    x = Math.max(0,x);
    double val = x == 0 ? MIN_LOG : Math.max(MIN_LOG, Math.log(x));
//    if (val == MIN_LOG) Log.warn("Log underflow: log(" + x + ") truncated to " + MIN_LOG);
    return val;
  }
686869
public static Rational binomial(Rational n, int m) {
        if (m == 0) {
            return Rational.ONE;
        }
        Rational bin = n;
        for (int i = 2; i <= m; i++) {
            bin = bin.multiply(n.subtract(i - 1)).divide(i);
        }
        return bin;
    }
149241
def log_sum_exp_ab(a, b):
    
    if a > b:
        return a + np.log(1. + np.exp(b - a))
    else:
        return b + np.log(1. + np.exp(a - b))
188354
def binomial(n,k):
    
    if n==k: return 1
    assert n>k, "Attempting to call binomial(%d,%d)" % (n,k)
    return factorial(n)//(factorial(k)*factorial(n-k))
305355
def _log_P(self, X , Z, k):
        
        r_ = self._r_
        Z_ = self._Z_
        # zero case
        if k == 0 :
            log_P = Z['log_p'] + log_K(X, Z, r_[k])
        # recursice case
        else :
            P_zk_z = np.exp( self._log_P(Z_[k], Z, k-1) )
            P_z_zk = np.exp( self._log_P(Z, Z_[k], k-1) ) 
            # flag
            if P_zk_z <= P_z_zk :
                self._N_is_0 = True
                log_P = -np.inf
            else : 
                log_P = np.log( P_zk_z - P_z_zk ) + log_K(X, Z, r_[k])
        return log_P
1107618
def log(self, x, base=2):
        """"""
        return self._format_result(log(float(x), base))
113298
def logn2(n, p):
    """"""
    with precision(p):
        extra = 10
        while True:
            with precision(p+extra):
                # use extra precision for intermediate step
                log2upper = log2(n, RoundTowardPositive)
                log2lower = log2(n, RoundTowardNegative)

            lower = div(1, log2upper, RoundTowardNegative)
            upper = div(1, log2lower, RoundTowardPositive)

            # if lower and upper are adjacent (or equal) we're done
            if next_up(lower) == upper:
                return (Fraction(*lower.as_integer_ratio()),
                        Fraction(*upper.as_integer_ratio()))

            # otherwise, increase the precision and try again
            extra += 10