gamma_distribution.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_GAMMA_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/exponential_distribution.h>
  12. #include <__random/is_valid.h>
  13. #include <__random/uniform_real_distribution.h>
  14. #include <cmath>
  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 _RealType = double>
  24. class _LIBCPP_TEMPLATE_VIS gamma_distribution
  25. {
  26. public:
  27. // types
  28. typedef _RealType result_type;
  29. class _LIBCPP_TEMPLATE_VIS param_type
  30. {
  31. result_type __alpha_;
  32. result_type __beta_;
  33. public:
  34. typedef gamma_distribution distribution_type;
  35. _LIBCPP_INLINE_VISIBILITY
  36. explicit param_type(result_type __alpha = 1, result_type __beta = 1)
  37. : __alpha_(__alpha), __beta_(__beta) {}
  38. _LIBCPP_INLINE_VISIBILITY
  39. result_type alpha() const {return __alpha_;}
  40. _LIBCPP_INLINE_VISIBILITY
  41. result_type beta() const {return __beta_;}
  42. friend _LIBCPP_INLINE_VISIBILITY
  43. bool operator==(const param_type& __x, const param_type& __y)
  44. {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
  45. friend _LIBCPP_INLINE_VISIBILITY
  46. bool operator!=(const param_type& __x, const param_type& __y)
  47. {return !(__x == __y);}
  48. };
  49. private:
  50. param_type __p_;
  51. public:
  52. // constructors and reset functions
  53. #ifndef _LIBCPP_CXX03_LANG
  54. _LIBCPP_INLINE_VISIBILITY
  55. gamma_distribution() : gamma_distribution(1) {}
  56. _LIBCPP_INLINE_VISIBILITY
  57. explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
  58. : __p_(param_type(__alpha, __beta)) {}
  59. #else
  60. _LIBCPP_INLINE_VISIBILITY
  61. explicit gamma_distribution(result_type __alpha = 1,
  62. result_type __beta = 1)
  63. : __p_(param_type(__alpha, __beta)) {}
  64. #endif
  65. _LIBCPP_INLINE_VISIBILITY
  66. explicit gamma_distribution(const param_type& __p)
  67. : __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 alpha() const {return __p_.alpha();}
  80. _LIBCPP_INLINE_VISIBILITY
  81. result_type beta() const {return __p_.beta();}
  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>::infinity();}
  90. friend _LIBCPP_INLINE_VISIBILITY
  91. bool operator==(const gamma_distribution& __x,
  92. const gamma_distribution& __y)
  93. {return __x.__p_ == __y.__p_;}
  94. friend _LIBCPP_INLINE_VISIBILITY
  95. bool operator!=(const gamma_distribution& __x,
  96. const gamma_distribution& __y)
  97. {return !(__x == __y);}
  98. };
  99. template <class _RealType>
  100. template<class _URNG>
  101. _RealType
  102. gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  103. {
  104. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  105. result_type __a = __p.alpha();
  106. uniform_real_distribution<result_type> __gen(0, 1);
  107. exponential_distribution<result_type> __egen;
  108. result_type __x;
  109. if (__a == 1)
  110. __x = __egen(__g);
  111. else if (__a > 1)
  112. {
  113. const result_type __b = __a - 1;
  114. const result_type __c = 3 * __a - result_type(0.75);
  115. while (true)
  116. {
  117. const result_type __u = __gen(__g);
  118. const result_type __v = __gen(__g);
  119. const result_type __w = __u * (1 - __u);
  120. if (__w != 0)
  121. {
  122. const result_type __y = _VSTD::sqrt(__c / __w) *
  123. (__u - result_type(0.5));
  124. __x = __b + __y;
  125. if (__x >= 0)
  126. {
  127. const result_type __z = 64 * __w * __w * __w * __v * __v;
  128. if (__z <= 1 - 2 * __y * __y / __x)
  129. break;
  130. if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. else // __a < 1
  137. {
  138. while (true)
  139. {
  140. const result_type __u = __gen(__g);
  141. const result_type __es = __egen(__g);
  142. if (__u <= 1 - __a)
  143. {
  144. __x = _VSTD::pow(__u, 1 / __a);
  145. if (__x <= __es)
  146. break;
  147. }
  148. else
  149. {
  150. const result_type __e = -_VSTD::log((1-__u)/__a);
  151. __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
  152. if (__x <= __e + __es)
  153. break;
  154. }
  155. }
  156. }
  157. return __x * __p.beta();
  158. }
  159. template <class _CharT, class _Traits, class _RT>
  160. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  161. operator<<(basic_ostream<_CharT, _Traits>& __os,
  162. const gamma_distribution<_RT>& __x)
  163. {
  164. __save_flags<_CharT, _Traits> __lx(__os);
  165. typedef basic_ostream<_CharT, _Traits> _OStream;
  166. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  167. _OStream::scientific);
  168. _CharT __sp = __os.widen(' ');
  169. __os.fill(__sp);
  170. __os << __x.alpha() << __sp << __x.beta();
  171. return __os;
  172. }
  173. template <class _CharT, class _Traits, class _RT>
  174. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  175. operator>>(basic_istream<_CharT, _Traits>& __is,
  176. gamma_distribution<_RT>& __x)
  177. {
  178. typedef gamma_distribution<_RT> _Eng;
  179. typedef typename _Eng::result_type result_type;
  180. typedef typename _Eng::param_type param_type;
  181. __save_flags<_CharT, _Traits> __lx(__is);
  182. typedef basic_istream<_CharT, _Traits> _Istream;
  183. __is.flags(_Istream::dec | _Istream::skipws);
  184. result_type __alpha;
  185. result_type __beta;
  186. __is >> __alpha >> __beta;
  187. if (!__is.fail())
  188. __x.param(param_type(__alpha, __beta));
  189. return __is;
  190. }
  191. _LIBCPP_END_NAMESPACE_STD
  192. _LIBCPP_POP_MACROS
  193. #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H