| 1 | // Copyright John Maddock 2006. |
| 2 | // Use, modification and distribution are subject to the |
| 3 | // Boost Software License, Version 1.0. (See accompanying file |
| 4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #ifndef BOOST_MATH_SF_BINOMIAL_HPP |
| 7 | #define BOOST_MATH_SF_BINOMIAL_HPP |
| 8 | |
| 9 | #ifdef _MSC_VER |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/math/special_functions/math_fwd.hpp> |
| 14 | #include <boost/math/special_functions/factorials.hpp> |
| 15 | #include <boost/math/special_functions/beta.hpp> |
| 16 | #include <boost/math/policies/error_handling.hpp> |
| 17 | #include <type_traits> |
| 18 | |
| 19 | namespace boost{ namespace math{ |
| 20 | |
| 21 | template <class T, class Policy> |
| 22 | T binomial_coefficient(unsigned n, unsigned k, const Policy& pol) |
| 23 | { |
| 24 | static_assert(!std::is_integral<T>::value, "Type T must not be an integral type" ); |
| 25 | BOOST_MATH_STD_USING |
| 26 | static const char* function = "boost::math::binomial_coefficient<%1%>(unsigned, unsigned)" ; |
| 27 | if(k > n) |
| 28 | return policies::raise_domain_error<T>( |
| 29 | function, |
| 30 | "The binomial coefficient is undefined for k > n, but got k = %1%." , |
| 31 | static_cast<T>(k), pol); |
| 32 | T result; |
| 33 | if((k == 0) || (k == n)) |
| 34 | return static_cast<T>(1); |
| 35 | if((k == 1) || (k == n-1)) |
| 36 | return static_cast<T>(n); |
| 37 | |
| 38 | if(n <= max_factorial<T>::value) |
| 39 | { |
| 40 | // Use fast table lookup: |
| 41 | result = unchecked_factorial<T>(n); |
| 42 | result /= unchecked_factorial<T>(n-k); |
| 43 | result /= unchecked_factorial<T>(k); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | // Use the beta function: |
| 48 | if(k < n - k) |
| 49 | result = static_cast<T>(k * beta(static_cast<T>(k), static_cast<T>(n-k+1), pol)); |
| 50 | else |
| 51 | result = static_cast<T>((n - k) * beta(static_cast<T>(k+1), static_cast<T>(n-k), pol)); |
| 52 | if(result == 0) |
| 53 | return policies::raise_overflow_error<T>(function, nullptr, pol); |
| 54 | result = 1 / result; |
| 55 | } |
| 56 | // convert to nearest integer: |
| 57 | return ceil(result - 0.5f); |
| 58 | } |
| 59 | // |
| 60 | // Type float can only store the first 35 factorials, in order to |
| 61 | // increase the chance that we can use a table driven implementation |
| 62 | // we'll promote to double: |
| 63 | // |
| 64 | template <> |
| 65 | inline float binomial_coefficient<float, policies::policy<> >(unsigned n, unsigned k, const policies::policy<>&) |
| 66 | { |
| 67 | typedef policies::normalise< |
| 68 | policies::policy<>, |
| 69 | policies::promote_float<true>, |
| 70 | policies::promote_double<false>, |
| 71 | policies::discrete_quantile<>, |
| 72 | policies::assert_undefined<> >::type forwarding_policy; |
| 73 | return policies::checked_narrowing_cast<float, forwarding_policy>(val: binomial_coefficient<double>(n, k, pol: forwarding_policy()), function: "boost::math::binomial_coefficient<%1%>(unsigned,unsigned)" ); |
| 74 | } |
| 75 | |
| 76 | template <class T> |
| 77 | inline T binomial_coefficient(unsigned n, unsigned k) |
| 78 | { |
| 79 | return binomial_coefficient<T>(n, k, policies::policy<>()); |
| 80 | } |
| 81 | |
| 82 | } // namespace math |
| 83 | } // namespace boost |
| 84 | |
| 85 | |
| 86 | #endif // BOOST_MATH_SF_BINOMIAL_HPP |
| 87 | |
| 88 | |
| 89 | |
| 90 | |