xref: /freebsd/contrib/llvm-project/libcxx/include/__random/gamma_distribution.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <__random/exponential_distribution.h>
14 #include <__random/is_valid.h>
15 #include <__random/uniform_real_distribution.h>
16 #include <cmath>
17 #include <iosfwd>
18 #include <limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template<class _RealType = double>
30 class _LIBCPP_TEMPLATE_VIS gamma_distribution
31 {
32 public:
33     // types
34     typedef _RealType result_type;
35 
36     class _LIBCPP_TEMPLATE_VIS param_type
37     {
38         result_type __alpha_;
39         result_type __beta_;
40     public:
41         typedef gamma_distribution distribution_type;
42 
43         _LIBCPP_INLINE_VISIBILITY
44         explicit param_type(result_type __alpha = 1, result_type __beta = 1)
45             : __alpha_(__alpha), __beta_(__beta) {}
46 
47         _LIBCPP_INLINE_VISIBILITY
48         result_type alpha() const {return __alpha_;}
49         _LIBCPP_INLINE_VISIBILITY
50         result_type beta() const {return __beta_;}
51 
52         friend _LIBCPP_INLINE_VISIBILITY
53             bool operator==(const param_type& __x, const param_type& __y)
54             {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
55         friend _LIBCPP_INLINE_VISIBILITY
56             bool operator!=(const param_type& __x, const param_type& __y)
57             {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_INLINE_VISIBILITY
67     gamma_distribution() : gamma_distribution(1) {}
68     _LIBCPP_INLINE_VISIBILITY
69     explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
70         : __p_(param_type(__alpha, __beta)) {}
71 #else
72     _LIBCPP_INLINE_VISIBILITY
73     explicit gamma_distribution(result_type __alpha = 1,
74                                 result_type __beta = 1)
75         : __p_(param_type(__alpha, __beta)) {}
76 #endif
77     _LIBCPP_INLINE_VISIBILITY
78     explicit gamma_distribution(const param_type& __p)
79         : __p_(__p) {}
80     _LIBCPP_INLINE_VISIBILITY
81     void reset() {}
82 
83     // generating functions
84     template<class _URNG>
85         _LIBCPP_INLINE_VISIBILITY
86         result_type operator()(_URNG& __g)
87         {return (*this)(__g, __p_);}
88     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
89 
90     // property functions
91     _LIBCPP_INLINE_VISIBILITY
92     result_type alpha() const {return __p_.alpha();}
93     _LIBCPP_INLINE_VISIBILITY
94     result_type beta() const {return __p_.beta();}
95 
96     _LIBCPP_INLINE_VISIBILITY
97     param_type param() const {return __p_;}
98     _LIBCPP_INLINE_VISIBILITY
99     void param(const param_type& __p) {__p_ = __p;}
100 
101     _LIBCPP_INLINE_VISIBILITY
102     result_type min() const {return 0;}
103     _LIBCPP_INLINE_VISIBILITY
104     result_type max() const {return numeric_limits<result_type>::infinity();}
105 
106     friend _LIBCPP_INLINE_VISIBILITY
107         bool operator==(const gamma_distribution& __x,
108                         const gamma_distribution& __y)
109         {return __x.__p_ == __y.__p_;}
110     friend _LIBCPP_INLINE_VISIBILITY
111         bool operator!=(const gamma_distribution& __x,
112                         const gamma_distribution& __y)
113         {return !(__x == __y);}
114 };
115 
116 template <class _RealType>
117 template<class _URNG>
118 _RealType
119 gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
120 {
121     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
122     result_type __a = __p.alpha();
123     uniform_real_distribution<result_type> __gen(0, 1);
124     exponential_distribution<result_type> __egen;
125     result_type __x;
126     if (__a == 1)
127         __x = __egen(__g);
128     else if (__a > 1)
129     {
130         const result_type __b = __a - 1;
131         const result_type __c = 3 * __a - result_type(0.75);
132         while (true)
133         {
134             const result_type __u = __gen(__g);
135             const result_type __v = __gen(__g);
136             const result_type __w = __u * (1 - __u);
137             if (__w != 0)
138             {
139                 const result_type __y = _VSTD::sqrt(__c / __w) *
140                                         (__u - result_type(0.5));
141                 __x = __b + __y;
142                 if (__x >= 0)
143                 {
144                     const result_type __z = 64 * __w * __w * __w * __v * __v;
145                     if (__z <= 1 - 2 * __y * __y / __x)
146                         break;
147                     if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
148                         break;
149                 }
150             }
151         }
152     }
153     else  // __a < 1
154     {
155         while (true)
156         {
157             const result_type __u = __gen(__g);
158             const result_type __es = __egen(__g);
159             if (__u <= 1 - __a)
160             {
161                 __x = _VSTD::pow(__u, 1 / __a);
162                 if (__x <= __es)
163                     break;
164             }
165             else
166             {
167                 const result_type __e = -_VSTD::log((1-__u)/__a);
168                 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
169                 if (__x <= __e + __es)
170                     break;
171             }
172         }
173     }
174     return __x * __p.beta();
175 }
176 
177 template <class _CharT, class _Traits, class _RT>
178 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
179 operator<<(basic_ostream<_CharT, _Traits>& __os,
180            const gamma_distribution<_RT>& __x)
181 {
182     __save_flags<_CharT, _Traits> __lx(__os);
183     typedef basic_ostream<_CharT, _Traits> _OStream;
184     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
185                _OStream::scientific);
186     _CharT __sp = __os.widen(' ');
187     __os.fill(__sp);
188     __os << __x.alpha() << __sp << __x.beta();
189     return __os;
190 }
191 
192 template <class _CharT, class _Traits, class _RT>
193 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
194 operator>>(basic_istream<_CharT, _Traits>& __is,
195            gamma_distribution<_RT>& __x)
196 {
197     typedef gamma_distribution<_RT> _Eng;
198     typedef typename _Eng::result_type result_type;
199     typedef typename _Eng::param_type param_type;
200     __save_flags<_CharT, _Traits> __lx(__is);
201     typedef basic_istream<_CharT, _Traits> _Istream;
202     __is.flags(_Istream::dec | _Istream::skipws);
203     result_type __alpha;
204     result_type __beta;
205     __is >> __alpha >> __beta;
206     if (!__is.fail())
207         __x.param(param_type(__alpha, __beta));
208     return __is;
209 }
210 
211 _LIBCPP_END_NAMESPACE_STD
212 
213 _LIBCPP_POP_MACROS
214 
215 #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H
216