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_BINOMIAL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
13 #include <__random/uniform_real_distribution.h>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #pragma GCC system_header
22 #include <__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template<class _IntType
= int>
27 class _LIBCPP_TEMPLATE_VIS binomial_distribution
31 typedef _IntType result_type
;
33 class _LIBCPP_TEMPLATE_VIS param_type
41 typedef binomial_distribution distribution_type
;
43 explicit param_type(result_type __t
= 1, double __p
= 0.5);
45 _LIBCPP_INLINE_VISIBILITY
46 result_type
t() const {return __t_
;}
47 _LIBCPP_INLINE_VISIBILITY
48 double p() const {return __p_
;}
50 friend _LIBCPP_INLINE_VISIBILITY
51 bool operator==(const param_type
& __x
, const param_type
& __y
)
52 {return __x
.__t_
== __y
.__t_
&& __x
.__p_
== __y
.__p_
;}
53 friend _LIBCPP_INLINE_VISIBILITY
54 bool operator!=(const param_type
& __x
, const param_type
& __y
)
55 {return !(__x
== __y
);}
57 friend class binomial_distribution
;
64 // constructors and reset functions
65 #ifndef _LIBCPP_CXX03_LANG
66 _LIBCPP_INLINE_VISIBILITY
67 binomial_distribution() : binomial_distribution(1) {}
68 _LIBCPP_INLINE_VISIBILITY
69 explicit binomial_distribution(result_type __t
, double __p
= 0.5)
70 : __p_(param_type(__t
, __p
)) {}
72 _LIBCPP_INLINE_VISIBILITY
73 explicit binomial_distribution(result_type __t
= 1, double __p
= 0.5)
74 : __p_(param_type(__t
, __p
)) {}
76 _LIBCPP_INLINE_VISIBILITY
77 explicit binomial_distribution(const param_type
& __p
) : __p_(__p
) {}
78 _LIBCPP_INLINE_VISIBILITY
81 // generating functions
83 _LIBCPP_INLINE_VISIBILITY
84 result_type
operator()(_URNG
& __g
)
85 {return (*this)(__g
, __p_
);}
86 template<class _URNG
> result_type
operator()(_URNG
& __g
, const param_type
& __p
);
89 _LIBCPP_INLINE_VISIBILITY
90 result_type
t() const {return __p_
.t();}
91 _LIBCPP_INLINE_VISIBILITY
92 double p() const {return __p_
.p();}
94 _LIBCPP_INLINE_VISIBILITY
95 param_type
param() const {return __p_
;}
96 _LIBCPP_INLINE_VISIBILITY
97 void param(const param_type
& __p
) {__p_
= __p
;}
99 _LIBCPP_INLINE_VISIBILITY
100 result_type
min() const {return 0;}
101 _LIBCPP_INLINE_VISIBILITY
102 result_type
max() const {return t();}
104 friend _LIBCPP_INLINE_VISIBILITY
105 bool operator==(const binomial_distribution
& __x
,
106 const binomial_distribution
& __y
)
107 {return __x
.__p_
== __y
.__p_
;}
108 friend _LIBCPP_INLINE_VISIBILITY
109 bool operator!=(const binomial_distribution
& __x
,
110 const binomial_distribution
& __y
)
111 {return !(__x
== __y
);}
114 #ifndef _LIBCPP_MSVCRT_LIKE
115 extern "C" double lgamma_r(double, int *);
118 inline _LIBCPP_INLINE_VISIBILITY
double __libcpp_lgamma(double __d
) {
119 #if defined(_LIBCPP_MSVCRT_LIKE)
123 return lgamma_r(__d
, &__sign
);
127 template<class _IntType
>
128 binomial_distribution
<_IntType
>::param_type::param_type(result_type __t
, double __p
)
129 : __t_(__t
), __p_(__p
)
131 if (0 < __p_
&& __p_
< 1)
133 __r0_
= static_cast<result_type
>((__t_
+ 1) * __p_
);
134 __pr_
= _VSTD::exp(__libcpp_lgamma(__t_
+ 1.) -
135 __libcpp_lgamma(__r0_
+ 1.) -
136 __libcpp_lgamma(__t_
- __r0_
+ 1.) + __r0_
* _VSTD::log(__p_
) +
137 (__t_
- __r0_
) * _VSTD::log(1 - __p_
));
138 __odds_ratio_
= __p_
/ (1 - __p_
);
142 // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
143 // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
144 template<class _IntType
>
145 template<class _URNG
>
147 binomial_distribution
<_IntType
>::operator()(_URNG
& __g
, const param_type
& __pr
)
149 if (__pr
.__t_
== 0 || __pr
.__p_
== 0)
153 uniform_real_distribution
<double> __gen
;
154 double __u
= __gen(__g
) - __pr
.__pr_
;
157 double __pu
= __pr
.__pr_
;
159 result_type __ru
= __pr
.__r0_
;
160 result_type __rd
= __ru
;
166 __pd
*= __rd
/ (__pr
.__odds_ratio_
* (__pr
.__t_
- __rd
+ 1));
175 if (__ru
<= __pr
.__t_
)
177 __pu
*= (__pr
.__t_
- __ru
+ 1) * __pr
.__odds_ratio_
/ __ru
;
188 template <class _CharT
, class _Traits
, class _IntType
>
189 basic_ostream
<_CharT
, _Traits
>&
190 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
191 const binomial_distribution
<_IntType
>& __x
)
193 __save_flags
<_CharT
, _Traits
> __lx(__os
);
194 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
195 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
196 _OStream::scientific
);
197 _CharT __sp
= __os
.widen(' ');
199 return __os
<< __x
.t() << __sp
<< __x
.p();
202 template <class _CharT
, class _Traits
, class _IntType
>
203 basic_istream
<_CharT
, _Traits
>&
204 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
205 binomial_distribution
<_IntType
>& __x
)
207 typedef binomial_distribution
<_IntType
> _Eng
;
208 typedef typename
_Eng::result_type result_type
;
209 typedef typename
_Eng::param_type param_type
;
210 __save_flags
<_CharT
, _Traits
> __lx(__is
);
211 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
212 __is
.flags(_Istream::dec
| _Istream::skipws
);
217 __x
.param(param_type(__t
, __p
));
221 _LIBCPP_END_NAMESPACE_STD
225 #endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H