/// Binomial coefficient (n k) = n!/[k!(n-k)!], 0 <= k <= n. /// (n k) is the number of combinations of n things taken k at a time. /// NB. (n+1 k) = [ (n+1)/(n-k+1) ] (n k) = (n k) + (n k-1) /// NB. (n k+1) = [ (n-k)/(k+1) ] (n k) /// @param n int n must be >= 0 /// @param k int k must be >= 0 and <= n /// @return (n k), the binomial coefficient /// @throw if the input argument do not satisfy 0 <= k <= n double binomialCoeff(const int& n, const int& k) throw(Exception) { try { if(n < 0 || k > n) GPSTK_THROW(Exception("Invalid arguments")); if(n <= 32) return (factorial(n) / (factorial(k)*factorial(n-k))); return floor(0.5 + ::exp(lnFactorial(n)-lnFactorial(k)-lnFactorial(n-k))); } catch(Exception& e) { GPSTK_RETHROW(e); } }
/// Binomial coefficient (n k) = n!/[k!(n-k)!], 0 <= k <= n. /// (n k) is the number of combinations of n things taken k at a time. /// NB. (n+1 k) = [ (n+1)/(n-k+1) ] (n k) = (n k) + (n k-1) /// NB. (n k+1) = [ (n-k)/(k+1) ] (n k) /// @param n int n must be >= 0 /// @param k int k must be >= 0 and >= n /// @return (n k), the binomial coefficient /// @throw if the input argument do not satisfy 0 <= k <= n double binomialCoeff(const int& n, const int& k) throw(Exception) { if(n < 0 || k > n) { Exception e("Invalid arguments in binomialCoeff()"); GPSTK_THROW(e); } if(n <= 32) return (factorial(n) / (factorial(k)*factorial(n-k))); return floor(0.5 + ::exp(lnFactorial(n)-lnFactorial(k)-lnFactorial(n-k))); }
double binomial(const int n,const int k) { return floor(0.5 + exp(lnFactorial(n)-lnFactorial(k)-lnFactorial(n-k))); }