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_WEIBULL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/exponential_distribution.h>
14 #include <__cxx03/__random/is_valid.h>
15 #include <__cxx03/cmath>
16 #include <__cxx03/iosfwd>
17 #include <__cxx03/limits>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
24 #include <__cxx03/__undef_macros>
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 template <class _RealType
= double>
29 class _LIBCPP_TEMPLATE_VIS weibull_distribution
{
30 static_assert(__libcpp_random_is_valid_realtype
<_RealType
>::value
,
31 "RealType must be a supported floating-point type");
35 typedef _RealType result_type
;
37 class _LIBCPP_TEMPLATE_VIS param_type
{
42 typedef weibull_distribution distribution_type
;
44 _LIBCPP_HIDE_FROM_ABI
explicit param_type(result_type __a
= 1, result_type __b
= 1) : __a_(__a
), __b_(__b
) {}
46 _LIBCPP_HIDE_FROM_ABI result_type
a() const { return __a_
; }
47 _LIBCPP_HIDE_FROM_ABI result_type
b() const { return __b_
; }
49 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const param_type
& __x
, const param_type
& __y
) {
50 return __x
.__a_
== __y
.__a_
&& __x
.__b_
== __y
.__b_
;
52 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const param_type
& __x
, const param_type
& __y
) { return !(__x
== __y
); }
59 // constructor and reset functions
60 #ifndef _LIBCPP_CXX03_LANG
61 _LIBCPP_HIDE_FROM_ABI
weibull_distribution() : weibull_distribution(1) {}
62 _LIBCPP_HIDE_FROM_ABI
explicit weibull_distribution(result_type __a
, result_type __b
= 1)
63 : __p_(param_type(__a
, __b
)) {}
65 _LIBCPP_HIDE_FROM_ABI
explicit weibull_distribution(result_type __a
= 1, result_type __b
= 1)
66 : __p_(param_type(__a
, __b
)) {}
68 _LIBCPP_HIDE_FROM_ABI
explicit weibull_distribution(const param_type
& __p
) : __p_(__p
) {}
69 _LIBCPP_HIDE_FROM_ABI
void reset() {}
71 // generating functions
72 template <class _URNG
>
73 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
) {
74 return (*this)(__g
, __p_
);
76 template <class _URNG
>
77 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
) {
78 return __p
.b() * std::pow(exponential_distribution
<result_type
>()(__g
), 1 / __p
.a());
82 _LIBCPP_HIDE_FROM_ABI result_type
a() const { return __p_
.a(); }
83 _LIBCPP_HIDE_FROM_ABI result_type
b() const { return __p_
.b(); }
85 _LIBCPP_HIDE_FROM_ABI param_type
param() const { return __p_
; }
86 _LIBCPP_HIDE_FROM_ABI
void param(const param_type
& __p
) { __p_
= __p
; }
88 _LIBCPP_HIDE_FROM_ABI result_type
min() const { return 0; }
89 _LIBCPP_HIDE_FROM_ABI result_type
max() const { return numeric_limits
<result_type
>::infinity(); }
91 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const weibull_distribution
& __x
, const weibull_distribution
& __y
) {
92 return __x
.__p_
== __y
.__p_
;
94 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const weibull_distribution
& __x
, const weibull_distribution
& __y
) {
99 template <class _CharT
, class _Traits
, class _RT
>
100 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
101 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const weibull_distribution
<_RT
>& __x
) {
102 __save_flags
<_CharT
, _Traits
> __lx(__os
);
103 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
104 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
| _OStream::scientific
);
105 _CharT __sp
= __os
.widen(' ');
107 __os
<< __x
.a() << __sp
<< __x
.b();
111 template <class _CharT
, class _Traits
, class _RT
>
112 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
113 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, weibull_distribution
<_RT
>& __x
) {
114 typedef weibull_distribution
<_RT
> _Eng
;
115 typedef typename
_Eng::result_type result_type
;
116 typedef typename
_Eng::param_type param_type
;
117 __save_flags
<_CharT
, _Traits
> __lx(__is
);
118 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
119 __is
.flags(_Istream::dec
| _Istream::skipws
);
124 __x
.param(param_type(__a
, __b
));
128 _LIBCPP_END_NAMESPACE_STD
132 #endif // _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H