binomial.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef BOOST_MATH_SF_BINOMIAL_HPP
  6. #define BOOST_MATH_SF_BINOMIAL_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/special_functions/factorials.hpp>
  12. #include <boost/math/special_functions/beta.hpp>
  13. #include <boost/math/policies/error_handling.hpp>
  14. #include <type_traits>
  15. namespace boost{ namespace math{
  16. template <class T, class Policy>
  17. T binomial_coefficient(unsigned n, unsigned k, const Policy& pol)
  18. {
  19. static_assert(!std::is_integral<T>::value, "Type T must not be an integral type");
  20. BOOST_MATH_STD_USING
  21. static const char* function = "boost::math::binomial_coefficient<%1%>(unsigned, unsigned)";
  22. if(k > n)
  23. return policies::raise_domain_error<T>(function, "The binomial coefficient is undefined for k > n, but got k = %1%.", static_cast<T>(k), pol);
  24. T result; // LCOV_EXCL_LINE
  25. if((k == 0) || (k == n))
  26. return static_cast<T>(1);
  27. if((k == 1) || (k == n-1))
  28. return static_cast<T>(n);
  29. if(n <= max_factorial<T>::value)
  30. {
  31. // Use fast table lookup:
  32. result = unchecked_factorial<T>(n);
  33. result /= unchecked_factorial<T>(n-k);
  34. result /= unchecked_factorial<T>(k);
  35. }
  36. else
  37. {
  38. // Use the beta function:
  39. if(k < n - k)
  40. result = static_cast<T>(k * beta(static_cast<T>(k), static_cast<T>(n-k+1), pol));
  41. else
  42. result = static_cast<T>((n - k) * beta(static_cast<T>(k+1), static_cast<T>(n-k), pol));
  43. if(result == 0)
  44. return policies::raise_overflow_error<T>(function, nullptr, pol);
  45. result = 1 / result;
  46. }
  47. // convert to nearest integer:
  48. return ceil(result - 0.5f);
  49. }
  50. //
  51. // Type float can only store the first 35 factorials, in order to
  52. // increase the chance that we can use a table driven implementation
  53. // we'll promote to double:
  54. //
  55. template <>
  56. inline float binomial_coefficient<float, policies::policy<> >(unsigned n, unsigned k, const policies::policy<>&)
  57. {
  58. typedef policies::normalise<
  59. policies::policy<>,
  60. policies::promote_float<true>,
  61. policies::promote_double<false>,
  62. policies::discrete_quantile<>,
  63. policies::assert_undefined<> >::type forwarding_policy;
  64. return policies::checked_narrowing_cast<float, forwarding_policy>(binomial_coefficient<double>(n, k, forwarding_policy()), "boost::math::binomial_coefficient<%1%>(unsigned,unsigned)");
  65. }
  66. template <class T>
  67. inline T binomial_coefficient(unsigned n, unsigned k)
  68. {
  69. return binomial_coefficient<T>(n, k, policies::policy<>());
  70. }
  71. } // namespace math
  72. } // namespace boost
  73. #endif // BOOST_MATH_SF_BINOMIAL_HPP