1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H
11344cef66SArthur O'Dwyer
12344cef66SArthur O'Dwyer #include <__config>
1381eda008SLouis Dionne #include <__random/clamp_to_integral.h>
14344cef66SArthur O'Dwyer #include <__random/exponential_distribution.h>
15a3255f21SArthur O'Dwyer #include <__random/is_valid.h>
16344cef66SArthur O'Dwyer #include <__random/normal_distribution.h>
17344cef66SArthur O'Dwyer #include <__random/uniform_real_distribution.h>
18344cef66SArthur O'Dwyer #include <cmath>
19344cef66SArthur O'Dwyer #include <iosfwd>
20344cef66SArthur O'Dwyer #include <limits>
21344cef66SArthur O'Dwyer
22344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23344cef66SArthur O'Dwyer # pragma GCC system_header
24344cef66SArthur O'Dwyer #endif
25344cef66SArthur O'Dwyer
26344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
27344cef66SArthur O'Dwyer #include <__undef_macros>
28344cef66SArthur O'Dwyer
29344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
30344cef66SArthur O'Dwyer
31344cef66SArthur O'Dwyer template <class _IntType = int>
32*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS poisson_distribution {
3307e984bcSLouis Dionne static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
34*9783f28cSLouis Dionne
35344cef66SArthur O'Dwyer public:
36344cef66SArthur O'Dwyer // types
37344cef66SArthur O'Dwyer typedef _IntType result_type;
38344cef66SArthur O'Dwyer
39*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS param_type {
40344cef66SArthur O'Dwyer double __mean_;
41344cef66SArthur O'Dwyer double __s_;
42344cef66SArthur O'Dwyer double __d_;
43344cef66SArthur O'Dwyer double __l_;
44344cef66SArthur O'Dwyer double __omega_;
45344cef66SArthur O'Dwyer double __c0_;
46344cef66SArthur O'Dwyer double __c1_;
47344cef66SArthur O'Dwyer double __c2_;
48344cef66SArthur O'Dwyer double __c3_;
49344cef66SArthur O'Dwyer double __c_;
50344cef66SArthur O'Dwyer
51344cef66SArthur O'Dwyer public:
52344cef66SArthur O'Dwyer typedef poisson_distribution distribution_type;
53344cef66SArthur O'Dwyer
5483ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI explicit param_type(double __mean = 1.0);
55344cef66SArthur O'Dwyer
mean()56*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double mean() const { return __mean_; }
57344cef66SArthur O'Dwyer
58*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
59*9783f28cSLouis Dionne return __x.__mean_ == __y.__mean_;
60*9783f28cSLouis Dionne }
61*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
62344cef66SArthur O'Dwyer
63344cef66SArthur O'Dwyer friend class poisson_distribution;
64344cef66SArthur O'Dwyer };
65344cef66SArthur O'Dwyer
66344cef66SArthur O'Dwyer private:
67344cef66SArthur O'Dwyer param_type __p_;
68344cef66SArthur O'Dwyer
69344cef66SArthur O'Dwyer public:
70344cef66SArthur O'Dwyer // constructors and reset functions
71344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
poisson_distribution()72*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI poisson_distribution() : poisson_distribution(1.0) {}
poisson_distribution(double __mean)73*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean) : __p_(__mean) {}
74344cef66SArthur O'Dwyer #else
75*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
76344cef66SArthur O'Dwyer #endif
poisson_distribution(const param_type & __p)77*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
reset()78*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void reset() {}
79344cef66SArthur O'Dwyer
80344cef66SArthur O'Dwyer // generating functions
81344cef66SArthur O'Dwyer template <class _URNG>
operator()82*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
83*9783f28cSLouis Dionne return (*this)(__g, __p_);
84*9783f28cSLouis Dionne }
8583ce1397SNikolas Klauser template <class _URNG>
8683ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
87344cef66SArthur O'Dwyer
88344cef66SArthur O'Dwyer // property functions
mean()89*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double mean() const { return __p_.mean(); }
90344cef66SArthur O'Dwyer
param()91*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)92*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
93344cef66SArthur O'Dwyer
min()94*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()95*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
96344cef66SArthur O'Dwyer
97*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const poisson_distribution& __x, const poisson_distribution& __y) {
98*9783f28cSLouis Dionne return __x.__p_ == __y.__p_;
99*9783f28cSLouis Dionne }
100*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const poisson_distribution& __x, const poisson_distribution& __y) {
101*9783f28cSLouis Dionne return !(__x == __y);
102*9783f28cSLouis Dionne }
103344cef66SArthur O'Dwyer };
104344cef66SArthur O'Dwyer
105344cef66SArthur O'Dwyer template <class _IntType>
param_type(double __mean)106344cef66SArthur O'Dwyer poisson_distribution<_IntType>::param_type::param_type(double __mean)
107344cef66SArthur O'Dwyer // According to the standard `inf` is a valid input, but it causes the
108344cef66SArthur O'Dwyer // distribution to hang, so we replace it with the maximum representable
109344cef66SArthur O'Dwyer // mean.
110*9783f28cSLouis Dionne : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) {
111*9783f28cSLouis Dionne if (__mean_ < 10) {
112344cef66SArthur O'Dwyer __s_ = 0;
113344cef66SArthur O'Dwyer __d_ = 0;
11477a00c0dSLouis Dionne __l_ = std::exp(-__mean_);
115344cef66SArthur O'Dwyer __omega_ = 0;
116344cef66SArthur O'Dwyer __c3_ = 0;
117344cef66SArthur O'Dwyer __c2_ = 0;
118344cef66SArthur O'Dwyer __c1_ = 0;
119344cef66SArthur O'Dwyer __c0_ = 0;
120344cef66SArthur O'Dwyer __c_ = 0;
121*9783f28cSLouis Dionne } else {
12277a00c0dSLouis Dionne __s_ = std::sqrt(__mean_);
123344cef66SArthur O'Dwyer __d_ = 6 * __mean_ * __mean_;
12477a00c0dSLouis Dionne __l_ = std::trunc(__mean_ - 1.1484);
125344cef66SArthur O'Dwyer __omega_ = .3989423 / __s_;
126d05f8895SNikolas Klauser double __b1 = .4166667E-1 / __mean_;
127d05f8895SNikolas Klauser double __b2 = .3 * __b1 * __b1;
128d05f8895SNikolas Klauser __c3_ = .1428571 * __b1 * __b2;
129d05f8895SNikolas Klauser __c2_ = __b2 - 15. * __c3_;
130d05f8895SNikolas Klauser __c1_ = __b1 - 6. * __b2 + 45. * __c3_;
131d05f8895SNikolas Klauser __c0_ = 1. - __b1 + 3. * __b2 - 15. * __c3_;
132344cef66SArthur O'Dwyer __c_ = .1069 / __mean_;
133344cef66SArthur O'Dwyer }
134344cef66SArthur O'Dwyer }
135344cef66SArthur O'Dwyer
136344cef66SArthur O'Dwyer template <class _IntType>
137344cef66SArthur O'Dwyer template <class _URNG>
operator()138*9783f28cSLouis Dionne _IntType poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {
1397624552eSArthur O'Dwyer static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
140344cef66SArthur O'Dwyer double __tx;
141344cef66SArthur O'Dwyer uniform_real_distribution<double> __urd;
142*9783f28cSLouis Dionne if (__pr.__mean_ < 10) {
143344cef66SArthur O'Dwyer __tx = 0;
144344cef66SArthur O'Dwyer for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)
145344cef66SArthur O'Dwyer __p *= __urd(__urng);
146*9783f28cSLouis Dionne } else {
147344cef66SArthur O'Dwyer double __difmuk;
148344cef66SArthur O'Dwyer double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
149344cef66SArthur O'Dwyer double __u;
150*9783f28cSLouis Dionne if (__g > 0) {
15177a00c0dSLouis Dionne __tx = std::trunc(__g);
152344cef66SArthur O'Dwyer if (__tx >= __pr.__l_)
15377a00c0dSLouis Dionne return std::__clamp_to_integral<result_type>(__tx);
154344cef66SArthur O'Dwyer __difmuk = __pr.__mean_ - __tx;
155344cef66SArthur O'Dwyer __u = __urd(__urng);
156344cef66SArthur O'Dwyer if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
15777a00c0dSLouis Dionne return std::__clamp_to_integral<result_type>(__tx);
158344cef66SArthur O'Dwyer }
159344cef66SArthur O'Dwyer exponential_distribution<double> __edist;
160*9783f28cSLouis Dionne for (bool __using_exp_dist = false; true; __using_exp_dist = true) {
161344cef66SArthur O'Dwyer double __e;
162*9783f28cSLouis Dionne if (__using_exp_dist || __g <= 0) {
163344cef66SArthur O'Dwyer double __t;
164*9783f28cSLouis Dionne do {
165344cef66SArthur O'Dwyer __e = __edist(__urng);
166344cef66SArthur O'Dwyer __u = __urd(__urng);
167344cef66SArthur O'Dwyer __u += __u - 1;
168344cef66SArthur O'Dwyer __t = 1.8 + (__u < 0 ? -__e : __e);
169344cef66SArthur O'Dwyer } while (__t <= -.6744);
17077a00c0dSLouis Dionne __tx = std::trunc(__pr.__mean_ + __pr.__s_ * __t);
171344cef66SArthur O'Dwyer __difmuk = __pr.__mean_ - __tx;
172344cef66SArthur O'Dwyer __using_exp_dist = true;
173344cef66SArthur O'Dwyer }
174344cef66SArthur O'Dwyer double __px;
175344cef66SArthur O'Dwyer double __py;
176*9783f28cSLouis Dionne if (__tx < 10 && __tx >= 0) {
177*9783f28cSLouis Dionne const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
178344cef66SArthur O'Dwyer __px = -__pr.__mean_;
17977a00c0dSLouis Dionne __py = std::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];
180*9783f28cSLouis Dionne } else {
181344cef66SArthur O'Dwyer double __del = .8333333E-1 / __tx;
182344cef66SArthur O'Dwyer __del -= 4.8 * __del * __del * __del;
183344cef66SArthur O'Dwyer double __v = __difmuk / __tx;
18477a00c0dSLouis Dionne if (std::abs(__v) > 0.25)
18577a00c0dSLouis Dionne __px = __tx * std::log(1 + __v) - __difmuk - __del;
186344cef66SArthur O'Dwyer else
187*9783f28cSLouis Dionne __px = __tx * __v * __v *
188*9783f28cSLouis Dionne (((((((.1250060 * __v + -.1384794) * __v + .1421878) * __v + -.1661269) * __v + .2000118) * __v +
189*9783f28cSLouis Dionne -.2500068) *
190*9783f28cSLouis Dionne __v +
191*9783f28cSLouis Dionne .3333333) *
192*9783f28cSLouis Dionne __v +
193*9783f28cSLouis Dionne -.5) -
194*9783f28cSLouis Dionne __del;
19577a00c0dSLouis Dionne __py = .3989423 / std::sqrt(__tx);
196344cef66SArthur O'Dwyer }
197344cef66SArthur O'Dwyer double __r = (0.5 - __difmuk) / __pr.__s_;
198344cef66SArthur O'Dwyer double __r2 = __r * __r;
199344cef66SArthur O'Dwyer double __fx = -0.5 * __r2;
200*9783f28cSLouis Dionne double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
201*9783f28cSLouis Dionne if (__using_exp_dist) {
202*9783f28cSLouis Dionne if (__pr.__c_ * std::abs(__u) <= __py * std::exp(__px + __e) - __fy * std::exp(__fx + __e))
203344cef66SArthur O'Dwyer break;
204*9783f28cSLouis Dionne } else {
20577a00c0dSLouis Dionne if (__fy - __u * __fy <= __py * std::exp(__px - __fx))
206344cef66SArthur O'Dwyer break;
207344cef66SArthur O'Dwyer }
208344cef66SArthur O'Dwyer }
209344cef66SArthur O'Dwyer }
21077a00c0dSLouis Dionne return std::__clamp_to_integral<result_type>(__tx);
211344cef66SArthur O'Dwyer }
212344cef66SArthur O'Dwyer
213344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IntType>
21480c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
215*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const poisson_distribution<_IntType>& __x) {
216344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__os);
217344cef66SArthur O'Dwyer typedef basic_ostream<_CharT, _Traits> _OStream;
218*9783f28cSLouis Dionne __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
219344cef66SArthur O'Dwyer return __os << __x.mean();
220344cef66SArthur O'Dwyer }
221344cef66SArthur O'Dwyer
222344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IntType>
22380c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
224*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, poisson_distribution<_IntType>& __x) {
225344cef66SArthur O'Dwyer typedef poisson_distribution<_IntType> _Eng;
226344cef66SArthur O'Dwyer typedef typename _Eng::param_type param_type;
227344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__is);
228344cef66SArthur O'Dwyer typedef basic_istream<_CharT, _Traits> _Istream;
229344cef66SArthur O'Dwyer __is.flags(_Istream::dec | _Istream::skipws);
230344cef66SArthur O'Dwyer double __mean;
231344cef66SArthur O'Dwyer __is >> __mean;
232344cef66SArthur O'Dwyer if (!__is.fail())
233344cef66SArthur O'Dwyer __x.param(param_type(__mean));
234344cef66SArthur O'Dwyer return __is;
235344cef66SArthur O'Dwyer }
236344cef66SArthur O'Dwyer
237344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
238344cef66SArthur O'Dwyer
239344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
240344cef66SArthur O'Dwyer
241344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H
242