gamma_distribution.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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> result_type operator()(_URNG& __g, const param_type& __p);
  76. // property functions
  77. _LIBCPP_INLINE_VISIBILITY
  78. result_type alpha() const {return __p_.alpha();}
  79. _LIBCPP_INLINE_VISIBILITY
  80. result_type beta() const {return __p_.beta();}
  81. _LIBCPP_INLINE_VISIBILITY
  82. param_type param() const {return __p_;}
  83. _LIBCPP_INLINE_VISIBILITY
  84. void param(const param_type& __p) {__p_ = __p;}
  85. _LIBCPP_INLINE_VISIBILITY
  86. result_type min() const {return 0;}
  87. _LIBCPP_INLINE_VISIBILITY
  88. result_type max() const {return numeric_limits<result_type>::infinity();}
  89. friend _LIBCPP_INLINE_VISIBILITY
  90. bool operator==(const gamma_distribution& __x,
  91. const gamma_distribution& __y)
  92. {return __x.__p_ == __y.__p_;}
  93. friend _LIBCPP_INLINE_VISIBILITY
  94. bool operator!=(const gamma_distribution& __x,
  95. const gamma_distribution& __y)
  96. {return !(__x == __y);}
  97. };
  98. template <class _RealType>
  99. template<class _URNG>
  100. _RealType
  101. gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  102. {
  103. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  104. result_type __a = __p.alpha();
  105. uniform_real_distribution<result_type> __gen(0, 1);
  106. exponential_distribution<result_type> __egen;
  107. result_type __x;
  108. if (__a == 1)
  109. __x = __egen(__g);
  110. else if (__a > 1)
  111. {
  112. const result_type __b = __a - 1;
  113. const result_type __c = 3 * __a - result_type(0.75);
  114. while (true)
  115. {
  116. const result_type __u = __gen(__g);
  117. const result_type __v = __gen(__g);
  118. const result_type __w = __u * (1 - __u);
  119. if (__w != 0)
  120. {
  121. const result_type __y = _VSTD::sqrt(__c / __w) *
  122. (__u - result_type(0.5));
  123. __x = __b + __y;
  124. if (__x >= 0)
  125. {
  126. const result_type __z = 64 * __w * __w * __w * __v * __v;
  127. if (__z <= 1 - 2 * __y * __y / __x)
  128. break;
  129. if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. else // __a < 1
  136. {
  137. while (true)
  138. {
  139. const result_type __u = __gen(__g);
  140. const result_type __es = __egen(__g);
  141. if (__u <= 1 - __a)
  142. {
  143. __x = _VSTD::pow(__u, 1 / __a);
  144. if (__x <= __es)
  145. break;
  146. }
  147. else
  148. {
  149. const result_type __e = -_VSTD::log((1-__u)/__a);
  150. __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
  151. if (__x <= __e + __es)
  152. break;
  153. }
  154. }
  155. }
  156. return __x * __p.beta();
  157. }
  158. template <class _CharT, class _Traits, class _RT>
  159. basic_ostream<_CharT, _Traits>&
  160. operator<<(basic_ostream<_CharT, _Traits>& __os,
  161. const gamma_distribution<_RT>& __x)
  162. {
  163. __save_flags<_CharT, _Traits> __lx(__os);
  164. typedef basic_ostream<_CharT, _Traits> _OStream;
  165. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  166. _OStream::scientific);
  167. _CharT __sp = __os.widen(' ');
  168. __os.fill(__sp);
  169. __os << __x.alpha() << __sp << __x.beta();
  170. return __os;
  171. }
  172. template <class _CharT, class _Traits, class _RT>
  173. basic_istream<_CharT, _Traits>&
  174. operator>>(basic_istream<_CharT, _Traits>& __is,
  175. gamma_distribution<_RT>& __x)
  176. {
  177. typedef gamma_distribution<_RT> _Eng;
  178. typedef typename _Eng::result_type result_type;
  179. typedef typename _Eng::param_type param_type;
  180. __save_flags<_CharT, _Traits> __lx(__is);
  181. typedef basic_istream<_CharT, _Traits> _Istream;
  182. __is.flags(_Istream::dec | _Istream::skipws);
  183. result_type __alpha;
  184. result_type __beta;
  185. __is >> __alpha >> __beta;
  186. if (!__is.fail())
  187. __x.param(param_type(__alpha, __beta));
  188. return __is;
  189. }
  190. _LIBCPP_END_NAMESPACE_STD
  191. _LIBCPP_POP_MACROS
  192. #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H