| 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_POISSON_DISTRIBUTION_H |
| 10 | #define _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__math/abs.h> |
| 14 | #include <__math/exponential_functions.h> |
| 15 | #include <__math/logarithms.h> |
| 16 | #include <__math/roots.h> |
| 17 | #include <__math/rounding_functions.h> |
| 18 | #include <__math/traits.h> |
| 19 | #include <__random/clamp_to_integral.h> |
| 20 | #include <__random/exponential_distribution.h> |
| 21 | #include <__random/is_valid.h> |
| 22 | #include <__random/normal_distribution.h> |
| 23 | #include <__random/uniform_real_distribution.h> |
| 24 | #include <iosfwd> |
| 25 | #include <limits> |
| 26 | |
| 27 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 | # pragma GCC system_header |
| 29 | #endif |
| 30 | |
| 31 | _LIBCPP_PUSH_MACROS |
| 32 | #include <__undef_macros> |
| 33 | |
| 34 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 35 | |
| 36 | template <class _IntType = int> |
| 37 | class poisson_distribution { |
| 38 | static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type" ); |
| 39 | |
| 40 | public: |
| 41 | // types |
| 42 | typedef _IntType result_type; |
| 43 | |
| 44 | class param_type { |
| 45 | double __mean_; |
| 46 | double __s_; |
| 47 | double __d_; |
| 48 | double __l_; |
| 49 | double __omega_; |
| 50 | double __c0_; |
| 51 | double __c1_; |
| 52 | double __c2_; |
| 53 | double __c3_; |
| 54 | double __c_; |
| 55 | |
| 56 | public: |
| 57 | typedef poisson_distribution distribution_type; |
| 58 | |
| 59 | _LIBCPP_HIDE_FROM_ABI explicit param_type(double __mean = 1.0); |
| 60 | |
| 61 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double mean() const { return __mean_; } |
| 62 | |
| 63 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { |
| 64 | return __x.__mean_ == __y.__mean_; |
| 65 | } |
| 66 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } |
| 67 | |
| 68 | friend class poisson_distribution; |
| 69 | }; |
| 70 | |
| 71 | private: |
| 72 | param_type __p_; |
| 73 | |
| 74 | public: |
| 75 | // constructors and reset functions |
| 76 | #ifndef _LIBCPP_CXX03_LANG |
| 77 | _LIBCPP_HIDE_FROM_ABI poisson_distribution() : poisson_distribution(1.0) {} |
| 78 | _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean) : __p_(__mean) {} |
| 79 | #else |
| 80 | _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
| 81 | #endif |
| 82 | _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
| 83 | _LIBCPP_HIDE_FROM_ABI void reset() {} |
| 84 | |
| 85 | // generating functions |
| 86 | template <class _URNG> |
| 87 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
| 88 | return (*this)(__g, __p_); |
| 89 | } |
| 90 | template <class _URNG> |
| 91 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); |
| 92 | |
| 93 | // property functions |
| 94 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double mean() const { return __p_.mean(); } |
| 95 | |
| 96 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } |
| 97 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } |
| 98 | |
| 99 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } |
| 100 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); } |
| 101 | |
| 102 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const poisson_distribution& __x, const poisson_distribution& __y) { |
| 103 | return __x.__p_ == __y.__p_; |
| 104 | } |
| 105 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const poisson_distribution& __x, const poisson_distribution& __y) { |
| 106 | return !(__x == __y); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | template <class _IntType> |
| 111 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
| 112 | // According to the standard `inf` is a valid input, but it causes the |
| 113 | // distribution to hang, so we replace it with the maximum representable |
| 114 | // mean. |
| 115 | : __mean_(__math::isinf(x: __mean) ? numeric_limits<double>::max() : __mean) { |
| 116 | if (__mean_ < 10) { |
| 117 | __s_ = 0; |
| 118 | __d_ = 0; |
| 119 | __l_ = __math::exp(x: -__mean_); |
| 120 | __omega_ = 0; |
| 121 | __c3_ = 0; |
| 122 | __c2_ = 0; |
| 123 | __c1_ = 0; |
| 124 | __c0_ = 0; |
| 125 | __c_ = 0; |
| 126 | } else { |
| 127 | __s_ = __math::sqrt(x: __mean_); |
| 128 | __d_ = 6 * __mean_ * __mean_; |
| 129 | __l_ = __math::trunc(x: __mean_ - 1.1484); |
| 130 | __omega_ = .3989423 / __s_; |
| 131 | double __b1 = .4166667E-1 / __mean_; |
| 132 | double __b2 = .3 * __b1 * __b1; |
| 133 | __c3_ = .1428571 * __b1 * __b2; |
| 134 | __c2_ = __b2 - 15. * __c3_; |
| 135 | __c1_ = __b1 - 6. * __b2 + 45. * __c3_; |
| 136 | __c0_ = 1. - __b1 + 3. * __b2 - 15. * __c3_; |
| 137 | __c_ = .1069 / __mean_; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | template <class _IntType> |
| 142 | template <class _URNG> |
| 143 | _IntType poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) { |
| 144 | static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "" ); |
| 145 | double __tx; |
| 146 | uniform_real_distribution<double> __urd; |
| 147 | if (__pr.__mean_ < 10) { |
| 148 | __tx = 0; |
| 149 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx) |
| 150 | __p *= __urd(__urng); |
| 151 | } else { |
| 152 | double __difmuk; |
| 153 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 154 | double __u; |
| 155 | if (__g > 0) { |
| 156 | __tx = __math::trunc(x: __g); |
| 157 | if (__tx >= __pr.__l_) |
| 158 | return std::__clamp_to_integral<result_type>(__tx); |
| 159 | __difmuk = __pr.__mean_ - __tx; |
| 160 | __u = __urd(__urng); |
| 161 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
| 162 | return std::__clamp_to_integral<result_type>(__tx); |
| 163 | } |
| 164 | exponential_distribution<double> __edist; |
| 165 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) { |
| 166 | double __e; |
| 167 | if (__using_exp_dist || __g <= 0) { |
| 168 | double __t; |
| 169 | do { |
| 170 | __e = __edist(__urng); |
| 171 | __u = __urd(__urng); |
| 172 | __u += __u - 1; |
| 173 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 174 | } while (__t <= -.6744); |
| 175 | __tx = __math::trunc(__pr.__mean_ + __pr.__s_ * __t); |
| 176 | __difmuk = __pr.__mean_ - __tx; |
| 177 | __using_exp_dist = true; |
| 178 | } |
| 179 | double __px; |
| 180 | double __py; |
| 181 | if (__tx < 10 && __tx >= 0) { |
| 182 | const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; |
| 183 | __px = -__pr.__mean_; |
| 184 | __py = __math::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)]; |
| 185 | } else { |
| 186 | double __del = .8333333E-1 / __tx; |
| 187 | __del -= 4.8 * __del * __del * __del; |
| 188 | double __v = __difmuk / __tx; |
| 189 | if (__math::abs(x: __v) > 0.25) |
| 190 | __px = __tx * __math::log(x: 1 + __v) - __difmuk - __del; |
| 191 | else |
| 192 | __px = __tx * __v * __v * |
| 193 | (((((((.1250060 * __v + -.1384794) * __v + .1421878) * __v + -.1661269) * __v + .2000118) * __v + |
| 194 | -.2500068) * |
| 195 | __v + |
| 196 | .3333333) * |
| 197 | __v + |
| 198 | -.5) - |
| 199 | __del; |
| 200 | __py = .3989423 / __math::sqrt(x: __tx); |
| 201 | } |
| 202 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 203 | double __r2 = __r * __r; |
| 204 | double __fx = -0.5 * __r2; |
| 205 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 206 | if (__using_exp_dist) { |
| 207 | if (__pr.__c_ * __math::abs(x: __u) <= __py * __math::exp(x: __px + __e) - __fy * __math::exp(x: __fx + __e)) |
| 208 | break; |
| 209 | } else { |
| 210 | if (__fy - __u * __fy <= __py * __math::exp(x: __px - __fx)) |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return std::__clamp_to_integral<result_type>(__tx); |
| 216 | } |
| 217 | |
| 218 | template <class _CharT, class _Traits, class _IntType> |
| 219 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 220 | operator<<(basic_ostream<_CharT, _Traits>& __os, const poisson_distribution<_IntType>& __x) { |
| 221 | __save_flags<_CharT, _Traits> __lx(__os); |
| 222 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 223 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); |
| 224 | return __os << __x.mean(); |
| 225 | } |
| 226 | |
| 227 | template <class _CharT, class _Traits, class _IntType> |
| 228 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 229 | operator>>(basic_istream<_CharT, _Traits>& __is, poisson_distribution<_IntType>& __x) { |
| 230 | typedef poisson_distribution<_IntType> _Eng; |
| 231 | typedef typename _Eng::param_type param_type; |
| 232 | __save_flags<_CharT, _Traits> __lx(__is); |
| 233 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 234 | __is.flags(_Istream::dec | _Istream::skipws); |
| 235 | double __mean; |
| 236 | __is >> __mean; |
| 237 | if (!__is.fail()) |
| 238 | __x.param(param_type(__mean)); |
| 239 | return __is; |
| 240 | } |
| 241 | |
| 242 | _LIBCPP_END_NAMESPACE_STD |
| 243 | |
| 244 | _LIBCPP_POP_MACROS |
| 245 | |
| 246 | #endif // _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H |
| 247 | |