
Hi, the binomial coefficients are usually used with integers, but it is also possible to get the binomial coefficient of real values (cf. http://en.wikipedia.org/wiki/Binomial_coefficient ). Maybe this could be added to the boost math library. Here's a sample implementation using standard g++: template <class T> T Choose(T n, T k) { return exp(lgamma(n + 1.0) - lgamma(k + 1.0) - lgamma(n - k + 1.0)); } Examples using double or long double type: C(10.000000, 3.000000) = 120.000000000000 C(10.000000, 3.250000) = 143.236519908866 C(10.000000, 3.500000) = 166.719334273254 C(10.000000, 3.750000) = 189.361086655990 C(10.000000, 4.000000) = 210.000000000000 C(10.500000, 3.000000) = 141.312500000000 C(10.500000, 3.250000) = 171.461136142499 C(10.500000, 3.500000) = 202.979003906250 C(10.500000, 3.750000) = 234.620815915449 C(10.500000, 4.000000) = 264.960937500000

G'day Ralf. Quoting "Ralf M." <rm@amitrader.com>:
the binomial coefficients are usually used with integers, but it is also possible to get the binomial coefficient of real values (cf. http://en.wikipedia.org/wiki/Binomial_coefficient ).
Maybe this could be added to the boost math library. Here's a sample implementation using standard g++:
template <class T> T Choose(T n, T k) { return exp(lgamma(n + 1.0) - lgamma(k + 1.0) - lgamma(n - k + 1.0)); }
Boost.Math almost has it: #include <boost/math/special_functions/beta.hpp> template<typename RealType> RealType binomial(RealType n, RealType k) { return 1.0 / ((n+1) * boost::math::beta(n - k + 1, k + 1)); } There is also binomial_distribution and beta_distribution if that's what you're really after. Cheers, Andrew Bromage
participants (2)
-
ajb@spamcop.net
-
Ralf M.