negative_binomial_distribution.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/bernoulli_distribution.h>
  12. #include <__random/gamma_distribution.h>
  13. #include <__random/is_valid.h>
  14. #include <__random/poisson_distribution.h>
  15. #include <iosfwd>
  16. #include <limits>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template<class _IntType = int>
  24. class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
  25. {
  26. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  27. public:
  28. // types
  29. typedef _IntType result_type;
  30. class _LIBCPP_TEMPLATE_VIS param_type
  31. {
  32. result_type __k_;
  33. double __p_;
  34. public:
  35. typedef negative_binomial_distribution distribution_type;
  36. _LIBCPP_INLINE_VISIBILITY
  37. explicit param_type(result_type __k = 1, double __p = 0.5)
  38. : __k_(__k), __p_(__p) {}
  39. _LIBCPP_INLINE_VISIBILITY
  40. result_type k() const {return __k_;}
  41. _LIBCPP_INLINE_VISIBILITY
  42. double p() const {return __p_;}
  43. friend _LIBCPP_INLINE_VISIBILITY
  44. bool operator==(const param_type& __x, const param_type& __y)
  45. {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
  46. friend _LIBCPP_INLINE_VISIBILITY
  47. bool operator!=(const param_type& __x, const param_type& __y)
  48. {return !(__x == __y);}
  49. };
  50. private:
  51. param_type __p_;
  52. public:
  53. // constructor and reset functions
  54. #ifndef _LIBCPP_CXX03_LANG
  55. _LIBCPP_INLINE_VISIBILITY
  56. negative_binomial_distribution() : negative_binomial_distribution(1) {}
  57. _LIBCPP_INLINE_VISIBILITY
  58. explicit negative_binomial_distribution(result_type __k, double __p = 0.5)
  59. : __p_(__k, __p) {}
  60. #else
  61. _LIBCPP_INLINE_VISIBILITY
  62. explicit negative_binomial_distribution(result_type __k = 1,
  63. double __p = 0.5)
  64. : __p_(__k, __p) {}
  65. #endif
  66. _LIBCPP_INLINE_VISIBILITY
  67. explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
  68. _LIBCPP_INLINE_VISIBILITY
  69. void reset() {}
  70. // generating functions
  71. template<class _URNG>
  72. _LIBCPP_INLINE_VISIBILITY
  73. result_type operator()(_URNG& __g)
  74. {return (*this)(__g, __p_);}
  75. template<class _URNG>
  76. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  77. // property functions
  78. _LIBCPP_INLINE_VISIBILITY
  79. result_type k() const {return __p_.k();}
  80. _LIBCPP_INLINE_VISIBILITY
  81. double p() const {return __p_.p();}
  82. _LIBCPP_INLINE_VISIBILITY
  83. param_type param() const {return __p_;}
  84. _LIBCPP_INLINE_VISIBILITY
  85. void param(const param_type& __p) {__p_ = __p;}
  86. _LIBCPP_INLINE_VISIBILITY
  87. result_type min() const {return 0;}
  88. _LIBCPP_INLINE_VISIBILITY
  89. result_type max() const {return numeric_limits<result_type>::max();}
  90. friend _LIBCPP_INLINE_VISIBILITY
  91. bool operator==(const negative_binomial_distribution& __x,
  92. const negative_binomial_distribution& __y)
  93. {return __x.__p_ == __y.__p_;}
  94. friend _LIBCPP_INLINE_VISIBILITY
  95. bool operator!=(const negative_binomial_distribution& __x,
  96. const negative_binomial_distribution& __y)
  97. {return !(__x == __y);}
  98. };
  99. template <class _IntType>
  100. template<class _URNG>
  101. _IntType
  102. negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
  103. {
  104. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  105. result_type __k = __pr.k();
  106. double __p = __pr.p();
  107. // When the number of bits in _IntType is small, we are too likely to
  108. // overflow __f below to use this technique.
  109. if (__k <= 21 * __p && sizeof(_IntType) > 1)
  110. {
  111. bernoulli_distribution __gen(__p);
  112. result_type __f = 0;
  113. result_type __s = 0;
  114. while (__s < __k)
  115. {
  116. if (__gen(__urng))
  117. ++__s;
  118. else
  119. ++__f;
  120. }
  121. _LIBCPP_ASSERT_UNCATEGORIZED(__f >= 0,
  122. "std::negative_binomial_distribution should never produce negative values. "
  123. "This is almost certainly a signed integer overflow issue on __f.");
  124. return __f;
  125. }
  126. return poisson_distribution<result_type>(gamma_distribution<double>
  127. (__k, (1-__p)/__p)(__urng))(__urng);
  128. }
  129. template <class _CharT, class _Traits, class _IntType>
  130. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  131. operator<<(basic_ostream<_CharT, _Traits>& __os,
  132. const negative_binomial_distribution<_IntType>& __x)
  133. {
  134. __save_flags<_CharT, _Traits> __lx(__os);
  135. typedef basic_ostream<_CharT, _Traits> _OStream;
  136. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  137. _OStream::scientific);
  138. _CharT __sp = __os.widen(' ');
  139. __os.fill(__sp);
  140. return __os << __x.k() << __sp << __x.p();
  141. }
  142. template <class _CharT, class _Traits, class _IntType>
  143. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  144. operator>>(basic_istream<_CharT, _Traits>& __is,
  145. negative_binomial_distribution<_IntType>& __x)
  146. {
  147. typedef negative_binomial_distribution<_IntType> _Eng;
  148. typedef typename _Eng::result_type result_type;
  149. typedef typename _Eng::param_type param_type;
  150. __save_flags<_CharT, _Traits> __lx(__is);
  151. typedef basic_istream<_CharT, _Traits> _Istream;
  152. __is.flags(_Istream::dec | _Istream::skipws);
  153. result_type __k;
  154. double __p;
  155. __is >> __k >> __p;
  156. if (!__is.fail())
  157. __x.param(param_type(__k, __p));
  158. return __is;
  159. }
  160. _LIBCPP_END_NAMESPACE_STD
  161. _LIBCPP_POP_MACROS
  162. #endif // _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H