14824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 24824e7fdSDimitry Andric // 34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64824e7fdSDimitry Andric // 74824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 84824e7fdSDimitry Andric 94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 130eae32dcSDimitry Andric #include <__random/clamp_to_integral.h> 144824e7fdSDimitry Andric #include <__random/exponential_distribution.h> 1581ad6265SDimitry Andric #include <__random/is_valid.h> 164824e7fdSDimitry Andric #include <__random/normal_distribution.h> 174824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h> 184824e7fdSDimitry Andric #include <cmath> 194824e7fdSDimitry Andric #include <iosfwd> 204824e7fdSDimitry Andric #include <limits> 214824e7fdSDimitry Andric 224824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 234824e7fdSDimitry Andric # pragma GCC system_header 244824e7fdSDimitry Andric #endif 254824e7fdSDimitry Andric 264824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 274824e7fdSDimitry Andric #include <__undef_macros> 284824e7fdSDimitry Andric 294824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 304824e7fdSDimitry Andric 314824e7fdSDimitry Andric template <class _IntType = int> 32*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS poisson_distribution { 33fcaf7f86SDimitry Andric static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type"); 34*cb14a3feSDimitry Andric 354824e7fdSDimitry Andric public: 364824e7fdSDimitry Andric // types 374824e7fdSDimitry Andric typedef _IntType result_type; 384824e7fdSDimitry Andric 39*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS param_type { 404824e7fdSDimitry Andric double __mean_; 414824e7fdSDimitry Andric double __s_; 424824e7fdSDimitry Andric double __d_; 434824e7fdSDimitry Andric double __l_; 444824e7fdSDimitry Andric double __omega_; 454824e7fdSDimitry Andric double __c0_; 464824e7fdSDimitry Andric double __c1_; 474824e7fdSDimitry Andric double __c2_; 484824e7fdSDimitry Andric double __c3_; 494824e7fdSDimitry Andric double __c_; 504824e7fdSDimitry Andric 514824e7fdSDimitry Andric public: 524824e7fdSDimitry Andric typedef poisson_distribution distribution_type; 534824e7fdSDimitry Andric 5406c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit param_type(double __mean = 1.0); 554824e7fdSDimitry Andric 56*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI double mean() const { return __mean_; } 574824e7fdSDimitry Andric 58*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { 59*cb14a3feSDimitry Andric return __x.__mean_ == __y.__mean_; 60*cb14a3feSDimitry Andric } 61*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } 624824e7fdSDimitry Andric 634824e7fdSDimitry Andric friend class poisson_distribution; 644824e7fdSDimitry Andric }; 654824e7fdSDimitry Andric 664824e7fdSDimitry Andric private: 674824e7fdSDimitry Andric param_type __p_; 684824e7fdSDimitry Andric 694824e7fdSDimitry Andric public: 704824e7fdSDimitry Andric // constructors and reset functions 714824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG 72*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI poisson_distribution() : poisson_distribution(1.0) {} 73*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean) : __p_(__mean) {} 744824e7fdSDimitry Andric #else 75*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} 764824e7fdSDimitry Andric #endif 77*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(const param_type& __p) : __p_(__p) {} 78*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void reset() {} 794824e7fdSDimitry Andric 804824e7fdSDimitry Andric // generating functions 814824e7fdSDimitry Andric template <class _URNG> 82*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { 83*cb14a3feSDimitry Andric return (*this)(__g, __p_); 84*cb14a3feSDimitry Andric } 8506c3fb27SDimitry Andric template <class _URNG> 8606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); 874824e7fdSDimitry Andric 884824e7fdSDimitry Andric // property functions 89*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI double mean() const { return __p_.mean(); } 904824e7fdSDimitry Andric 91*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } 92*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } 934824e7fdSDimitry Andric 94*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } 95*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); } 964824e7fdSDimitry Andric 97*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const poisson_distribution& __x, const poisson_distribution& __y) { 98*cb14a3feSDimitry Andric return __x.__p_ == __y.__p_; 99*cb14a3feSDimitry Andric } 100*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const poisson_distribution& __x, const poisson_distribution& __y) { 101*cb14a3feSDimitry Andric return !(__x == __y); 102*cb14a3feSDimitry Andric } 1034824e7fdSDimitry Andric }; 1044824e7fdSDimitry Andric 1054824e7fdSDimitry Andric template <class _IntType> 1064824e7fdSDimitry Andric poisson_distribution<_IntType>::param_type::param_type(double __mean) 1074824e7fdSDimitry Andric // According to the standard `inf` is a valid input, but it causes the 1084824e7fdSDimitry Andric // distribution to hang, so we replace it with the maximum representable 1094824e7fdSDimitry Andric // mean. 110*cb14a3feSDimitry Andric : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) { 111*cb14a3feSDimitry Andric if (__mean_ < 10) { 1124824e7fdSDimitry Andric __s_ = 0; 1134824e7fdSDimitry Andric __d_ = 0; 1145f757f3fSDimitry Andric __l_ = std::exp(-__mean_); 1154824e7fdSDimitry Andric __omega_ = 0; 1164824e7fdSDimitry Andric __c3_ = 0; 1174824e7fdSDimitry Andric __c2_ = 0; 1184824e7fdSDimitry Andric __c1_ = 0; 1194824e7fdSDimitry Andric __c0_ = 0; 1204824e7fdSDimitry Andric __c_ = 0; 121*cb14a3feSDimitry Andric } else { 1225f757f3fSDimitry Andric __s_ = std::sqrt(__mean_); 1234824e7fdSDimitry Andric __d_ = 6 * __mean_ * __mean_; 1245f757f3fSDimitry Andric __l_ = std::trunc(__mean_ - 1.1484); 1254824e7fdSDimitry Andric __omega_ = .3989423 / __s_; 12606c3fb27SDimitry Andric double __b1 = .4166667E-1 / __mean_; 12706c3fb27SDimitry Andric double __b2 = .3 * __b1 * __b1; 12806c3fb27SDimitry Andric __c3_ = .1428571 * __b1 * __b2; 12906c3fb27SDimitry Andric __c2_ = __b2 - 15. * __c3_; 13006c3fb27SDimitry Andric __c1_ = __b1 - 6. * __b2 + 45. * __c3_; 13106c3fb27SDimitry Andric __c0_ = 1. - __b1 + 3. * __b2 - 15. * __c3_; 1324824e7fdSDimitry Andric __c_ = .1069 / __mean_; 1334824e7fdSDimitry Andric } 1344824e7fdSDimitry Andric } 1354824e7fdSDimitry Andric 1364824e7fdSDimitry Andric template <class _IntType> 1374824e7fdSDimitry Andric template <class _URNG> 138*cb14a3feSDimitry Andric _IntType poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) { 13981ad6265SDimitry Andric static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 1404824e7fdSDimitry Andric double __tx; 1414824e7fdSDimitry Andric uniform_real_distribution<double> __urd; 142*cb14a3feSDimitry Andric if (__pr.__mean_ < 10) { 1434824e7fdSDimitry Andric __tx = 0; 1444824e7fdSDimitry Andric for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx) 1454824e7fdSDimitry Andric __p *= __urd(__urng); 146*cb14a3feSDimitry Andric } else { 1474824e7fdSDimitry Andric double __difmuk; 1484824e7fdSDimitry Andric double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); 1494824e7fdSDimitry Andric double __u; 150*cb14a3feSDimitry Andric if (__g > 0) { 1515f757f3fSDimitry Andric __tx = std::trunc(__g); 1524824e7fdSDimitry Andric if (__tx >= __pr.__l_) 1535f757f3fSDimitry Andric return std::__clamp_to_integral<result_type>(__tx); 1544824e7fdSDimitry Andric __difmuk = __pr.__mean_ - __tx; 1554824e7fdSDimitry Andric __u = __urd(__urng); 1564824e7fdSDimitry Andric if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) 1575f757f3fSDimitry Andric return std::__clamp_to_integral<result_type>(__tx); 1584824e7fdSDimitry Andric } 1594824e7fdSDimitry Andric exponential_distribution<double> __edist; 160*cb14a3feSDimitry Andric for (bool __using_exp_dist = false; true; __using_exp_dist = true) { 1614824e7fdSDimitry Andric double __e; 162*cb14a3feSDimitry Andric if (__using_exp_dist || __g <= 0) { 1634824e7fdSDimitry Andric double __t; 164*cb14a3feSDimitry Andric do { 1654824e7fdSDimitry Andric __e = __edist(__urng); 1664824e7fdSDimitry Andric __u = __urd(__urng); 1674824e7fdSDimitry Andric __u += __u - 1; 1684824e7fdSDimitry Andric __t = 1.8 + (__u < 0 ? -__e : __e); 1694824e7fdSDimitry Andric } while (__t <= -.6744); 1705f757f3fSDimitry Andric __tx = std::trunc(__pr.__mean_ + __pr.__s_ * __t); 1714824e7fdSDimitry Andric __difmuk = __pr.__mean_ - __tx; 1724824e7fdSDimitry Andric __using_exp_dist = true; 1734824e7fdSDimitry Andric } 1744824e7fdSDimitry Andric double __px; 1754824e7fdSDimitry Andric double __py; 176*cb14a3feSDimitry Andric if (__tx < 10 && __tx >= 0) { 177*cb14a3feSDimitry Andric const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; 1784824e7fdSDimitry Andric __px = -__pr.__mean_; 1795f757f3fSDimitry Andric __py = std::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)]; 180*cb14a3feSDimitry Andric } else { 1814824e7fdSDimitry Andric double __del = .8333333E-1 / __tx; 1824824e7fdSDimitry Andric __del -= 4.8 * __del * __del * __del; 1834824e7fdSDimitry Andric double __v = __difmuk / __tx; 1845f757f3fSDimitry Andric if (std::abs(__v) > 0.25) 1855f757f3fSDimitry Andric __px = __tx * std::log(1 + __v) - __difmuk - __del; 1864824e7fdSDimitry Andric else 187*cb14a3feSDimitry Andric __px = __tx * __v * __v * 188*cb14a3feSDimitry Andric (((((((.1250060 * __v + -.1384794) * __v + .1421878) * __v + -.1661269) * __v + .2000118) * __v + 189*cb14a3feSDimitry Andric -.2500068) * 190*cb14a3feSDimitry Andric __v + 191*cb14a3feSDimitry Andric .3333333) * 192*cb14a3feSDimitry Andric __v + 193*cb14a3feSDimitry Andric -.5) - 194*cb14a3feSDimitry Andric __del; 1955f757f3fSDimitry Andric __py = .3989423 / std::sqrt(__tx); 1964824e7fdSDimitry Andric } 1974824e7fdSDimitry Andric double __r = (0.5 - __difmuk) / __pr.__s_; 1984824e7fdSDimitry Andric double __r2 = __r * __r; 1994824e7fdSDimitry Andric double __fx = -0.5 * __r2; 200*cb14a3feSDimitry Andric double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * __r2 + __pr.__c1_) * __r2 + __pr.__c0_); 201*cb14a3feSDimitry Andric if (__using_exp_dist) { 202*cb14a3feSDimitry Andric if (__pr.__c_ * std::abs(__u) <= __py * std::exp(__px + __e) - __fy * std::exp(__fx + __e)) 2034824e7fdSDimitry Andric break; 204*cb14a3feSDimitry Andric } else { 2055f757f3fSDimitry Andric if (__fy - __u * __fy <= __py * std::exp(__px - __fx)) 2064824e7fdSDimitry Andric break; 2074824e7fdSDimitry Andric } 2084824e7fdSDimitry Andric } 2094824e7fdSDimitry Andric } 2105f757f3fSDimitry Andric return std::__clamp_to_integral<result_type>(__tx); 2114824e7fdSDimitry Andric } 2124824e7fdSDimitry Andric 2134824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType> 214bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 215*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const poisson_distribution<_IntType>& __x) { 2164824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__os); 2174824e7fdSDimitry Andric typedef basic_ostream<_CharT, _Traits> _OStream; 218*cb14a3feSDimitry Andric __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); 2194824e7fdSDimitry Andric return __os << __x.mean(); 2204824e7fdSDimitry Andric } 2214824e7fdSDimitry Andric 2224824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType> 223bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 224*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, poisson_distribution<_IntType>& __x) { 2254824e7fdSDimitry Andric typedef poisson_distribution<_IntType> _Eng; 2264824e7fdSDimitry Andric typedef typename _Eng::param_type param_type; 2274824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__is); 2284824e7fdSDimitry Andric typedef basic_istream<_CharT, _Traits> _Istream; 2294824e7fdSDimitry Andric __is.flags(_Istream::dec | _Istream::skipws); 2304824e7fdSDimitry Andric double __mean; 2314824e7fdSDimitry Andric __is >> __mean; 2324824e7fdSDimitry Andric if (!__is.fail()) 2334824e7fdSDimitry Andric __x.param(param_type(__mean)); 2344824e7fdSDimitry Andric return __is; 2354824e7fdSDimitry Andric } 2364824e7fdSDimitry Andric 2374824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 2384824e7fdSDimitry Andric 2394824e7fdSDimitry Andric _LIBCPP_POP_MACROS 2404824e7fdSDimitry Andric 2414824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H 242