Boost logo

Boost Users :

Subject: [Boost-users] [Math] Fractional binomial_coefficient
From: Ralf M. (rm_at_[hidden])
Date: 2009-08-31 12:30:42


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net