| 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 | |
| 9 | #ifndef _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H |
| 10 | #define _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__math/exponential_functions.h> |
| 14 | #include <__math/gamma.h> |
| 15 | #include <__math/logarithms.h> |
| 16 | #include <__math/roots.h> |
| 17 | #include <__random/exponential_distribution.h> |
| 18 | #include <__random/is_valid.h> |
| 19 | #include <__random/uniform_real_distribution.h> |
| 20 | #include <iosfwd> |
| 21 | #include <limits> |
| 22 | |
| 23 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 24 | # pragma GCC system_header |
| 25 | #endif |
| 26 | |
| 27 | _LIBCPP_PUSH_MACROS |
| 28 | #include <__undef_macros> |
| 29 | |
| 30 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 31 | |
| 32 | template <class _RealType = double> |
| 33 | class gamma_distribution { |
| 34 | static_assert(__libcpp_random_is_valid_realtype<_RealType>::value, |
| 35 | "RealType must be a supported floating-point type" ); |
| 36 | |
| 37 | public: |
| 38 | // types |
| 39 | typedef _RealType result_type; |
| 40 | |
| 41 | class param_type { |
| 42 | result_type __alpha_; |
| 43 | result_type __beta_; |
| 44 | |
| 45 | public: |
| 46 | typedef gamma_distribution distribution_type; |
| 47 | |
| 48 | _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 49 | : __alpha_(__alpha), __beta_(__beta) {} |
| 50 | |
| 51 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __alpha_; } |
| 52 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __beta_; } |
| 53 | |
| 54 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { |
| 55 | return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_; |
| 56 | } |
| 57 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } |
| 58 | }; |
| 59 | |
| 60 | private: |
| 61 | param_type __p_; |
| 62 | |
| 63 | public: |
| 64 | // constructors and reset functions |
| 65 | #ifndef _LIBCPP_CXX03_LANG |
| 66 | _LIBCPP_HIDE_FROM_ABI gamma_distribution() : gamma_distribution(1) {} |
| 67 | _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha, result_type __beta = 1) |
| 68 | : __p_(param_type(__alpha, __beta)) {} |
| 69 | #else |
| 70 | _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 71 | : __p_(param_type(__alpha, __beta)) {} |
| 72 | #endif |
| 73 | _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(const param_type& __p) : __p_(__p) {} |
| 74 | _LIBCPP_HIDE_FROM_ABI void reset() {} |
| 75 | |
| 76 | // generating functions |
| 77 | template <class _URNG> |
| 78 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
| 79 | return (*this)(__g, __p_); |
| 80 | } |
| 81 | template <class _URNG> |
| 82 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); |
| 83 | |
| 84 | // property functions |
| 85 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __p_.alpha(); } |
| 86 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __p_.beta(); } |
| 87 | |
| 88 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } |
| 89 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } |
| 90 | |
| 91 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } |
| 92 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); } |
| 93 | |
| 94 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const gamma_distribution& __x, const gamma_distribution& __y) { |
| 95 | return __x.__p_ == __y.__p_; |
| 96 | } |
| 97 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const gamma_distribution& __x, const gamma_distribution& __y) { |
| 98 | return !(__x == __y); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | template <class _RealType> |
| 103 | template <class _URNG> |
| 104 | _RealType gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) { |
| 105 | static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "" ); |
| 106 | result_type __a = __p.alpha(); |
| 107 | uniform_real_distribution<result_type> __gen(0, 1); |
| 108 | exponential_distribution<result_type> __egen; |
| 109 | result_type __x; |
| 110 | if (__a == 1) |
| 111 | __x = __egen(__g); |
| 112 | else if (__a > 1) { |
| 113 | const result_type __b = __a - 1; |
| 114 | const result_type __c = 3 * __a - result_type(0.75); |
| 115 | while (true) { |
| 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 | const result_type __y = __math::sqrt(__c / __w) * (__u - result_type(0.5)); |
| 121 | __x = __b + __y; |
| 122 | if (__x >= 0) { |
| 123 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 124 | if (__z <= 1 - 2 * __y * __y / __x) |
| 125 | break; |
| 126 | if (__math::log(__z) <= 2 * (__b * __math::log(__x / __b) - __y)) |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } else // __a < 1 |
| 132 | { |
| 133 | while (true) { |
| 134 | const result_type __u = __gen(__g); |
| 135 | const result_type __es = __egen(__g); |
| 136 | if (__u <= 1 - __a) { |
| 137 | __x = __math::pow(__u, 1 / __a); |
| 138 | if (__x <= __es) |
| 139 | break; |
| 140 | } else { |
| 141 | const result_type __e = -__math::log((1 - __u) / __a); |
| 142 | __x = __math::pow(1 - __a + __a * __e, 1 / __a); |
| 143 | if (__x <= __e + __es) |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | return __x * __p.beta(); |
| 149 | } |
| 150 | |
| 151 | template <class _CharT, class _Traits, class _RT> |
| 152 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 153 | operator<<(basic_ostream<_CharT, _Traits>& __os, const gamma_distribution<_RT>& __x) { |
| 154 | __save_flags<_CharT, _Traits> __lx(__os); |
| 155 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 156 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); |
| 157 | _CharT __sp = __os.widen(' '); |
| 158 | __os.fill(__sp); |
| 159 | __os << __x.alpha() << __sp << __x.beta(); |
| 160 | return __os; |
| 161 | } |
| 162 | |
| 163 | template <class _CharT, class _Traits, class _RT> |
| 164 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 165 | operator>>(basic_istream<_CharT, _Traits>& __is, gamma_distribution<_RT>& __x) { |
| 166 | typedef gamma_distribution<_RT> _Eng; |
| 167 | typedef typename _Eng::result_type result_type; |
| 168 | typedef typename _Eng::param_type param_type; |
| 169 | __save_flags<_CharT, _Traits> __lx(__is); |
| 170 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 171 | __is.flags(_Istream::dec | _Istream::skipws); |
| 172 | result_type __alpha; |
| 173 | result_type __beta; |
| 174 | __is >> __alpha >> __beta; |
| 175 | if (!__is.fail()) |
| 176 | __x.param(param_type(__alpha, __beta)); |
| 177 | return __is; |
| 178 | } |
| 179 | |
| 180 | _LIBCPP_END_NAMESPACE_STD |
| 181 | |
| 182 | _LIBCPP_POP_MACROS |
| 183 | |
| 184 | #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H |
| 185 | |