gamma_distribution.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
  26. "RealType must be a supported floating-point type");
  27. public:
  28. // types
  29. typedef _RealType result_type;
  30. class _LIBCPP_TEMPLATE_VIS param_type {
  31. result_type __alpha_;
  32. result_type __beta_;
  33. public:
  34. typedef gamma_distribution distribution_type;
  35. _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __alpha = 1, result_type __beta = 1)
  36. : __alpha_(__alpha), __beta_(__beta) {}
  37. _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __alpha_; }
  38. _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __beta_; }
  39. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  40. return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;
  41. }
  42. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  43. };
  44. private:
  45. param_type __p_;
  46. public:
  47. // constructors and reset functions
  48. #ifndef _LIBCPP_CXX03_LANG
  49. _LIBCPP_HIDE_FROM_ABI gamma_distribution() : gamma_distribution(1) {}
  50. _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
  51. : __p_(param_type(__alpha, __beta)) {}
  52. #else
  53. _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
  54. : __p_(param_type(__alpha, __beta)) {}
  55. #endif
  56. _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(const param_type& __p) : __p_(__p) {}
  57. _LIBCPP_HIDE_FROM_ABI void reset() {}
  58. // generating functions
  59. template <class _URNG>
  60. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  61. return (*this)(__g, __p_);
  62. }
  63. template <class _URNG>
  64. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  65. // property functions
  66. _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __p_.alpha(); }
  67. _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __p_.beta(); }
  68. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  69. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  70. _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
  71. _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
  72. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const gamma_distribution& __x, const gamma_distribution& __y) {
  73. return __x.__p_ == __y.__p_;
  74. }
  75. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const gamma_distribution& __x, const gamma_distribution& __y) {
  76. return !(__x == __y);
  77. }
  78. };
  79. template <class _RealType>
  80. template <class _URNG>
  81. _RealType gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
  82. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  83. result_type __a = __p.alpha();
  84. uniform_real_distribution<result_type> __gen(0, 1);
  85. exponential_distribution<result_type> __egen;
  86. result_type __x;
  87. if (__a == 1)
  88. __x = __egen(__g);
  89. else if (__a > 1) {
  90. const result_type __b = __a - 1;
  91. const result_type __c = 3 * __a - result_type(0.75);
  92. while (true) {
  93. const result_type __u = __gen(__g);
  94. const result_type __v = __gen(__g);
  95. const result_type __w = __u * (1 - __u);
  96. if (__w != 0) {
  97. const result_type __y = std::sqrt(__c / __w) * (__u - result_type(0.5));
  98. __x = __b + __y;
  99. if (__x >= 0) {
  100. const result_type __z = 64 * __w * __w * __w * __v * __v;
  101. if (__z <= 1 - 2 * __y * __y / __x)
  102. break;
  103. if (std::log(__z) <= 2 * (__b * std::log(__x / __b) - __y))
  104. break;
  105. }
  106. }
  107. }
  108. } else // __a < 1
  109. {
  110. while (true) {
  111. const result_type __u = __gen(__g);
  112. const result_type __es = __egen(__g);
  113. if (__u <= 1 - __a) {
  114. __x = std::pow(__u, 1 / __a);
  115. if (__x <= __es)
  116. break;
  117. } else {
  118. const result_type __e = -std::log((1 - __u) / __a);
  119. __x = std::pow(1 - __a + __a * __e, 1 / __a);
  120. if (__x <= __e + __es)
  121. break;
  122. }
  123. }
  124. }
  125. return __x * __p.beta();
  126. }
  127. template <class _CharT, class _Traits, class _RT>
  128. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  129. operator<<(basic_ostream<_CharT, _Traits>& __os, const gamma_distribution<_RT>& __x) {
  130. __save_flags<_CharT, _Traits> __lx(__os);
  131. typedef basic_ostream<_CharT, _Traits> _OStream;
  132. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  133. _CharT __sp = __os.widen(' ');
  134. __os.fill(__sp);
  135. __os << __x.alpha() << __sp << __x.beta();
  136. return __os;
  137. }
  138. template <class _CharT, class _Traits, class _RT>
  139. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  140. operator>>(basic_istream<_CharT, _Traits>& __is, gamma_distribution<_RT>& __x) {
  141. typedef gamma_distribution<_RT> _Eng;
  142. typedef typename _Eng::result_type result_type;
  143. typedef typename _Eng::param_type param_type;
  144. __save_flags<_CharT, _Traits> __lx(__is);
  145. typedef basic_istream<_CharT, _Traits> _Istream;
  146. __is.flags(_Istream::dec | _Istream::skipws);
  147. result_type __alpha;
  148. result_type __beta;
  149. __is >> __alpha >> __beta;
  150. if (!__is.fail())
  151. __x.param(param_type(__alpha, __beta));
  152. return __is;
  153. }
  154. _LIBCPP_END_NAMESPACE_STD
  155. _LIBCPP_POP_MACROS
  156. #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H