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_WEIBULL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/exponential_distribution.h>
14 #include <cmath>
15 #include <iosfwd>
16 #include <limits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template<class _RealType = double>
28 class _LIBCPP_TEMPLATE_VIS weibull_distribution
29 {
30 public:
31     // types
32     typedef _RealType result_type;
33 
34     class _LIBCPP_TEMPLATE_VIS param_type
35     {
36         result_type __a_;
37         result_type __b_;
38     public:
39         typedef weibull_distribution distribution_type;
40 
41         _LIBCPP_INLINE_VISIBILITY
42         explicit param_type(result_type __a = 1, result_type __b = 1)
43             : __a_(__a), __b_(__b) {}
44 
45         _LIBCPP_INLINE_VISIBILITY
46         result_type a() const {return __a_;}
47         _LIBCPP_INLINE_VISIBILITY
48         result_type b() const {return __b_;}
49 
50         friend _LIBCPP_INLINE_VISIBILITY
51             bool operator==(const param_type& __x, const param_type& __y)
52             {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
53         friend _LIBCPP_INLINE_VISIBILITY
54             bool operator!=(const param_type& __x, const param_type& __y)
55             {return !(__x == __y);}
56     };
57 
58 private:
59     param_type __p_;
60 
61 public:
62     // constructor and reset functions
63 #ifndef _LIBCPP_CXX03_LANG
64     _LIBCPP_INLINE_VISIBILITY
65     weibull_distribution() : weibull_distribution(1) {}
66     _LIBCPP_INLINE_VISIBILITY
67     explicit weibull_distribution(result_type __a, result_type __b = 1)
68         : __p_(param_type(__a, __b)) {}
69 #else
70     _LIBCPP_INLINE_VISIBILITY
71     explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
72         : __p_(param_type(__a, __b)) {}
73 #endif
74     _LIBCPP_INLINE_VISIBILITY
75     explicit weibull_distribution(const param_type& __p)
76         : __p_(__p) {}
77     _LIBCPP_INLINE_VISIBILITY
78     void reset() {}
79 
80     // generating functions
81     template<class _URNG>
82         _LIBCPP_INLINE_VISIBILITY
83         result_type operator()(_URNG& __g)
84         {return (*this)(__g, __p_);}
85     template<class _URNG>
86         _LIBCPP_INLINE_VISIBILITY
87         result_type operator()(_URNG& __g, const param_type& __p)
88         {return __p.b() *
89             _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
90 
91     // property functions
92     _LIBCPP_INLINE_VISIBILITY
93     result_type a() const {return __p_.a();}
94     _LIBCPP_INLINE_VISIBILITY
95     result_type b() const {return __p_.b();}
96 
97     _LIBCPP_INLINE_VISIBILITY
98     param_type param() const {return __p_;}
99     _LIBCPP_INLINE_VISIBILITY
100     void param(const param_type& __p) {__p_ = __p;}
101 
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();}
106 
107     friend _LIBCPP_INLINE_VISIBILITY
108         bool operator==(const weibull_distribution& __x,
109                         const weibull_distribution& __y)
110         {return __x.__p_ == __y.__p_;}
111     friend _LIBCPP_INLINE_VISIBILITY
112         bool operator!=(const weibull_distribution& __x,
113                         const weibull_distribution& __y)
114         {return !(__x == __y);}
115 };
116 
117 template <class _CharT, class _Traits, class _RT>
118 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
119 operator<<(basic_ostream<_CharT, _Traits>& __os,
120            const weibull_distribution<_RT>& __x)
121 {
122     __save_flags<_CharT, _Traits> __lx(__os);
123     typedef basic_ostream<_CharT, _Traits> _OStream;
124     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
125                _OStream::scientific);
126     _CharT __sp = __os.widen(' ');
127     __os.fill(__sp);
128     __os << __x.a() << __sp << __x.b();
129     return __os;
130 }
131 
132 template <class _CharT, class _Traits, class _RT>
133 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
134 operator>>(basic_istream<_CharT, _Traits>& __is,
135            weibull_distribution<_RT>& __x)
136 {
137     typedef weibull_distribution<_RT> _Eng;
138     typedef typename _Eng::result_type result_type;
139     typedef typename _Eng::param_type param_type;
140     __save_flags<_CharT, _Traits> __lx(__is);
141     typedef basic_istream<_CharT, _Traits> _Istream;
142     __is.flags(_Istream::dec | _Istream::skipws);
143     result_type __a;
144     result_type __b;
145     __is >> __a >> __b;
146     if (!__is.fail())
147         __x.param(param_type(__a, __b));
148     return __is;
149 }
150 
151 _LIBCPP_END_NAMESPACE_STD
152 
153 _LIBCPP_POP_MACROS
154 
155 #endif // _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
156