xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__random/gamma_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_GAMMA_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_GAMMA_DISTRIBUTION_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/exponential_distribution.h>
14 #include <__cxx03/__random/is_valid.h>
15 #include <__cxx03/__random/uniform_real_distribution.h>
16 #include <__cxx03/cmath>
17 #include <__cxx03/iosfwd>
18 #include <__cxx03/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 <__cxx03/__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template <class _RealType = double>
30 class _LIBCPP_TEMPLATE_VIS gamma_distribution {
31   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32                 "RealType must be a supported floating-point type");
33 
34 public:
35   // types
36   typedef _RealType result_type;
37 
38   class _LIBCPP_TEMPLATE_VIS param_type {
39     result_type __alpha_;
40     result_type __beta_;
41 
42   public:
43     typedef gamma_distribution distribution_type;
44 
45     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __alpha = 1, result_type __beta = 1)
__alpha_(__alpha)46         : __alpha_(__alpha), __beta_(__beta) {}
47 
alpha()48     _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __alpha_; }
beta()49     _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __beta_; }
50 
51     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
52       return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;
53     }
54     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
55   };
56 
57 private:
58   param_type __p_;
59 
60 public:
61   // constructors and reset functions
62   _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
__p_(param_type (__alpha,__beta))63       : __p_(param_type(__alpha, __beta)) {}
gamma_distribution(const param_type & __p)64   _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(const param_type& __p) : __p_(__p) {}
reset()65   _LIBCPP_HIDE_FROM_ABI void reset() {}
66 
67   // generating functions
68   template <class _URNG>
operator()69   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
70     return (*this)(__g, __p_);
71   }
72   template <class _URNG>
73   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
74 
75   // property functions
alpha()76   _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __p_.alpha(); }
beta()77   _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __p_.beta(); }
78 
param()79   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)80   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
81 
min()82   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()83   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
84 
85   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const gamma_distribution& __x, const gamma_distribution& __y) {
86     return __x.__p_ == __y.__p_;
87   }
88   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const gamma_distribution& __x, const gamma_distribution& __y) {
89     return !(__x == __y);
90   }
91 };
92 
93 template <class _RealType>
94 template <class _URNG>
operator()95 _RealType gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
96   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
97   result_type __a = __p.alpha();
98   uniform_real_distribution<result_type> __gen(0, 1);
99   exponential_distribution<result_type> __egen;
100   result_type __x;
101   if (__a == 1)
102     __x = __egen(__g);
103   else if (__a > 1) {
104     const result_type __b = __a - 1;
105     const result_type __c = 3 * __a - result_type(0.75);
106     while (true) {
107       const result_type __u = __gen(__g);
108       const result_type __v = __gen(__g);
109       const result_type __w = __u * (1 - __u);
110       if (__w != 0) {
111         const result_type __y = std::sqrt(__c / __w) * (__u - result_type(0.5));
112         __x                   = __b + __y;
113         if (__x >= 0) {
114           const result_type __z = 64 * __w * __w * __w * __v * __v;
115           if (__z <= 1 - 2 * __y * __y / __x)
116             break;
117           if (std::log(__z) <= 2 * (__b * std::log(__x / __b) - __y))
118             break;
119         }
120       }
121     }
122   } else // __a < 1
123   {
124     while (true) {
125       const result_type __u  = __gen(__g);
126       const result_type __es = __egen(__g);
127       if (__u <= 1 - __a) {
128         __x = std::pow(__u, 1 / __a);
129         if (__x <= __es)
130           break;
131       } else {
132         const result_type __e = -std::log((1 - __u) / __a);
133         __x                   = std::pow(1 - __a + __a * __e, 1 / __a);
134         if (__x <= __e + __es)
135           break;
136       }
137     }
138   }
139   return __x * __p.beta();
140 }
141 
142 template <class _CharT, class _Traits, class _RT>
143 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
144 operator<<(basic_ostream<_CharT, _Traits>& __os, const gamma_distribution<_RT>& __x) {
145   __save_flags<_CharT, _Traits> __lx(__os);
146   typedef basic_ostream<_CharT, _Traits> _OStream;
147   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
148   _CharT __sp = __os.widen(' ');
149   __os.fill(__sp);
150   __os << __x.alpha() << __sp << __x.beta();
151   return __os;
152 }
153 
154 template <class _CharT, class _Traits, class _RT>
155 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
156 operator>>(basic_istream<_CharT, _Traits>& __is, gamma_distribution<_RT>& __x) {
157   typedef gamma_distribution<_RT> _Eng;
158   typedef typename _Eng::result_type result_type;
159   typedef typename _Eng::param_type param_type;
160   __save_flags<_CharT, _Traits> __lx(__is);
161   typedef basic_istream<_CharT, _Traits> _Istream;
162   __is.flags(_Istream::dec | _Istream::skipws);
163   result_type __alpha;
164   result_type __beta;
165   __is >> __alpha >> __beta;
166   if (!__is.fail())
167     __x.param(param_type(__alpha, __beta));
168   return __is;
169 }
170 
171 _LIBCPP_END_NAMESPACE_STD
172 
173 _LIBCPP_POP_MACROS
174 
175 #endif // _LIBCPP___CXX03___RANDOM_GAMMA_DISTRIBUTION_H
176