Example usage for org.apache.commons.math.util MathUtils binomialCoefficientDouble

List of usage examples for org.apache.commons.math.util MathUtils binomialCoefficientDouble

Introduction

In this page you can find the example usage for org.apache.commons.math.util MathUtils binomialCoefficientDouble.

Prototype

public static double binomialCoefficientDouble(final int n, final int k) 

Source Link

Document

Returns a <code>double</code> representation of the <a href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial Coefficient</a>, "<code>n choose k</code>", the number of <code>k</code>-element subsets that can be selected from an <code>n</code>-element set.

Usage

From source file:org.renjin.MathExt.java

public static double choose(double n, int k) {
    /*// ww  w. ja v  a 2s .  c  om
     * Because gamma(a+1) = factorial(a)
     * we use gamma(n+1) /(gamma(n-k+1) * gamma(k+1)) instead of
     * Binomial(n,k) = n! / ((n-k)! * k!) for non-integer n values.
     * 
     */
    if (k < 0) {
        return (0);
    } else if (k == 0) {
        return (1);
    } else if ((int) n == n) {
        return (MathUtils.binomialCoefficientDouble((int) n, k));
    } else {
        return (MathExt.gamma(n + 1) / (MathExt.gamma(n - k + 1) * MathExt.gamma(k + 1)));
    }
}

From source file:org.renjin.primitives.MathExt.java

@Internal
@DataParallel/* w  w w  .ja  v a2 s . c  om*/
public static double choose(double n, int k) {
    /*
     * Because gamma(a+1) = factorial(a)
     * we use gamma(n+1) /(gamma(n-k+1) * gamma(k+1)) instead of
     * Binomial(n,k) = n! / ((n-k)! * k!) for non-integer n values.
     * 
     */
    if (k < 0) {
        return (0);
    } else if (k == 0) {
        return (1);
    } else if ((int) n == n) {
        return (MathUtils.binomialCoefficientDouble((int) n, k));
    } else {
        return (MathExt.gamma(n + 1) / (MathExt.gamma(n - k + 1) * MathExt.gamma(k + 1)));
    }
}

From source file:r.base.MathExt.java

@Primitive
public static double choose(@Recycle double n, @Recycle int k) {
    /*//www.  j  av a 2s. c om
     * Because gamma(a+1) = factorial(a)
     * we use gamma(n+1) /(gamma(n-k+1) * gamma(k+1)) instead of
     * Binomial(n,k) = n! / ((n-k)! * k!) for non-integer n values.
     * 
     */
    if (k < 0) {
        return (0);
    } else if (k == 0) {
        return (1);
    } else if ((int) n == n) {
        return (MathUtils.binomialCoefficientDouble((int) n, k));
    } else {
        return (MathExt.gamma(n + 1) / (MathExt.gamma(n - k + 1) * MathExt.gamma(k + 1)));
    }
}