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