1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H
13 #include <__random/exponential_distribution.h>
14 #include <__random/is_valid.h>
15 #include <__random/uniform_real_distribution.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
25 #include <__undef_macros>
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 template<class _RealType
= double>
30 class _LIBCPP_TEMPLATE_VIS gamma_distribution
34 typedef _RealType result_type
;
36 class _LIBCPP_TEMPLATE_VIS param_type
41 typedef gamma_distribution distribution_type
;
43 _LIBCPP_INLINE_VISIBILITY
44 explicit param_type(result_type __alpha
= 1, result_type __beta
= 1)
45 : __alpha_(__alpha
), __beta_(__beta
) {}
47 _LIBCPP_INLINE_VISIBILITY
48 result_type
alpha() const {return __alpha_
;}
49 _LIBCPP_INLINE_VISIBILITY
50 result_type
beta() const {return __beta_
;}
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
);}
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
)) {}
72 _LIBCPP_INLINE_VISIBILITY
73 explicit gamma_distribution(result_type __alpha
= 1,
74 result_type __beta
= 1)
75 : __p_(param_type(__alpha
, __beta
)) {}
77 _LIBCPP_INLINE_VISIBILITY
78 explicit gamma_distribution(const param_type
& __p
)
80 _LIBCPP_INLINE_VISIBILITY
83 // generating functions
85 _LIBCPP_INLINE_VISIBILITY
86 result_type
operator()(_URNG
& __g
)
87 {return (*this)(__g
, __p_
);}
89 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
92 _LIBCPP_INLINE_VISIBILITY
93 result_type
alpha() const {return __p_
.alpha();}
94 _LIBCPP_INLINE_VISIBILITY
95 result_type
beta() const {return __p_
.beta();}
97 _LIBCPP_INLINE_VISIBILITY
98 param_type
param() const {return __p_
;}
99 _LIBCPP_INLINE_VISIBILITY
100 void param(const param_type
& __p
) {__p_
= __p
;}
102 _LIBCPP_INLINE_VISIBILITY
103 result_type
min() const {return 0;}
104 _LIBCPP_INLINE_VISIBILITY
105 result_type
max() const {return numeric_limits
<result_type
>::infinity();}
107 friend _LIBCPP_INLINE_VISIBILITY
108 bool operator==(const gamma_distribution
& __x
,
109 const gamma_distribution
& __y
)
110 {return __x
.__p_
== __y
.__p_
;}
111 friend _LIBCPP_INLINE_VISIBILITY
112 bool operator!=(const gamma_distribution
& __x
,
113 const gamma_distribution
& __y
)
114 {return !(__x
== __y
);}
117 template <class _RealType
>
118 template<class _URNG
>
120 gamma_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
)
122 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
123 result_type __a
= __p
.alpha();
124 uniform_real_distribution
<result_type
> __gen(0, 1);
125 exponential_distribution
<result_type
> __egen
;
131 const result_type __b
= __a
- 1;
132 const result_type __c
= 3 * __a
- result_type(0.75);
135 const result_type __u
= __gen(__g
);
136 const result_type __v
= __gen(__g
);
137 const result_type __w
= __u
* (1 - __u
);
140 const result_type __y
= _VSTD::sqrt(__c
/ __w
) *
141 (__u
- result_type(0.5));
145 const result_type __z
= 64 * __w
* __w
* __w
* __v
* __v
;
146 if (__z
<= 1 - 2 * __y
* __y
/ __x
)
148 if (_VSTD::log(__z
) <= 2 * (__b
* _VSTD::log(__x
/ __b
) - __y
))
158 const result_type __u
= __gen(__g
);
159 const result_type __es
= __egen(__g
);
162 __x
= _VSTD::pow(__u
, 1 / __a
);
168 const result_type __e
= -_VSTD::log((1-__u
)/__a
);
169 __x
= _VSTD::pow(1 - __a
+ __a
* __e
, 1 / __a
);
170 if (__x
<= __e
+ __es
)
175 return __x
* __p
.beta();
178 template <class _CharT
, class _Traits
, class _RT
>
179 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
180 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
181 const gamma_distribution
<_RT
>& __x
)
183 __save_flags
<_CharT
, _Traits
> __lx(__os
);
184 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
185 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
186 _OStream::scientific
);
187 _CharT __sp
= __os
.widen(' ');
189 __os
<< __x
.alpha() << __sp
<< __x
.beta();
193 template <class _CharT
, class _Traits
, class _RT
>
194 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
195 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
196 gamma_distribution
<_RT
>& __x
)
198 typedef gamma_distribution
<_RT
> _Eng
;
199 typedef typename
_Eng::result_type result_type
;
200 typedef typename
_Eng::param_type param_type
;
201 __save_flags
<_CharT
, _Traits
> __lx(__is
);
202 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
203 __is
.flags(_Istream::dec
| _Istream::skipws
);
206 __is
>> __alpha
>> __beta
;
208 __x
.param(param_type(__alpha
, __beta
));
212 _LIBCPP_END_NAMESPACE_STD
216 #endif // _LIBCPP___RANDOM_GAMMA_DISTRIBUTION_H