xref: /llvm-project/libcxx/include/__random/weibull_distribution.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
13344cef66SArthur O'Dwyer #include <__random/exponential_distribution.h>
14727fef79SNhat Nguyen #include <__random/is_valid.h>
15344cef66SArthur O'Dwyer #include <cmath>
16344cef66SArthur O'Dwyer #include <iosfwd>
17344cef66SArthur O'Dwyer #include <limits>
18344cef66SArthur O'Dwyer 
19344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20344cef66SArthur O'Dwyer #  pragma GCC system_header
21344cef66SArthur O'Dwyer #endif
22344cef66SArthur O'Dwyer 
23344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
24344cef66SArthur O'Dwyer #include <__undef_macros>
25344cef66SArthur O'Dwyer 
26344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
27344cef66SArthur O'Dwyer 
28344cef66SArthur O'Dwyer template <class _RealType = double>
29*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS weibull_distribution {
30727fef79SNhat Nguyen   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31727fef79SNhat Nguyen                 "RealType must be a supported floating-point type");
32727fef79SNhat Nguyen 
33344cef66SArthur O'Dwyer public:
34344cef66SArthur O'Dwyer   // types
35344cef66SArthur O'Dwyer   typedef _RealType result_type;
36344cef66SArthur O'Dwyer 
37*9783f28cSLouis Dionne   class _LIBCPP_TEMPLATE_VIS param_type {
38344cef66SArthur O'Dwyer     result_type __a_;
39344cef66SArthur O'Dwyer     result_type __b_;
40*9783f28cSLouis Dionne 
41344cef66SArthur O'Dwyer   public:
42344cef66SArthur O'Dwyer     typedef weibull_distribution distribution_type;
43344cef66SArthur O'Dwyer 
__a_(__a)44*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 1, result_type __b = 1) : __a_(__a), __b_(__b) {}
45344cef66SArthur O'Dwyer 
a()46*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
b()47*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
48344cef66SArthur O'Dwyer 
49*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
50*9783f28cSLouis Dionne       return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
51*9783f28cSLouis Dionne     }
52*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
53344cef66SArthur O'Dwyer   };
54344cef66SArthur O'Dwyer 
55344cef66SArthur O'Dwyer private:
56344cef66SArthur O'Dwyer   param_type __p_;
57344cef66SArthur O'Dwyer 
58344cef66SArthur O'Dwyer public:
59344cef66SArthur O'Dwyer   // constructor and reset functions
60344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
weibull_distribution()61*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI weibull_distribution() : weibull_distribution(1) {}
62*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(result_type __a, result_type __b = 1)
__p_(param_type (__a,__b))63344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
64344cef66SArthur O'Dwyer #else
65*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
66344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
67344cef66SArthur O'Dwyer #endif
weibull_distribution(const param_type & __p)68*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(const param_type& __p) : __p_(__p) {}
reset()69*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void reset() {}
70344cef66SArthur O'Dwyer 
71344cef66SArthur O'Dwyer   // generating functions
72344cef66SArthur O'Dwyer   template <class _URNG>
operator()73*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
74*9783f28cSLouis Dionne     return (*this)(__g, __p_);
75*9783f28cSLouis Dionne   }
76344cef66SArthur O'Dwyer   template <class _URNG>
operator()77*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
78*9783f28cSLouis Dionne     return __p.b() * std::pow(exponential_distribution<result_type>()(__g), 1 / __p.a());
79*9783f28cSLouis Dionne   }
80344cef66SArthur O'Dwyer 
81344cef66SArthur O'Dwyer   // property functions
a()82*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
b()83*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
84344cef66SArthur O'Dwyer 
param()85*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)86*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
87344cef66SArthur O'Dwyer 
min()88*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()89*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
90344cef66SArthur O'Dwyer 
91*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const weibull_distribution& __x, const weibull_distribution& __y) {
92*9783f28cSLouis Dionne     return __x.__p_ == __y.__p_;
93*9783f28cSLouis Dionne   }
94*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const weibull_distribution& __x, const weibull_distribution& __y) {
95*9783f28cSLouis Dionne     return !(__x == __y);
96*9783f28cSLouis Dionne   }
97344cef66SArthur O'Dwyer };
98344cef66SArthur O'Dwyer 
99344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
10080c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
101*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const weibull_distribution<_RT>& __x) {
102344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__os);
103344cef66SArthur O'Dwyer   typedef basic_ostream<_CharT, _Traits> _OStream;
104*9783f28cSLouis Dionne   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
105344cef66SArthur O'Dwyer   _CharT __sp = __os.widen(' ');
106344cef66SArthur O'Dwyer   __os.fill(__sp);
107344cef66SArthur O'Dwyer   __os << __x.a() << __sp << __x.b();
108344cef66SArthur O'Dwyer   return __os;
109344cef66SArthur O'Dwyer }
110344cef66SArthur O'Dwyer 
111344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
11280c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
113*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, weibull_distribution<_RT>& __x) {
114344cef66SArthur O'Dwyer   typedef weibull_distribution<_RT> _Eng;
115344cef66SArthur O'Dwyer   typedef typename _Eng::result_type result_type;
116344cef66SArthur O'Dwyer   typedef typename _Eng::param_type param_type;
117344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__is);
118344cef66SArthur O'Dwyer   typedef basic_istream<_CharT, _Traits> _Istream;
119344cef66SArthur O'Dwyer   __is.flags(_Istream::dec | _Istream::skipws);
120344cef66SArthur O'Dwyer   result_type __a;
121344cef66SArthur O'Dwyer   result_type __b;
122344cef66SArthur O'Dwyer   __is >> __a >> __b;
123344cef66SArthur O'Dwyer   if (!__is.fail())
124344cef66SArthur O'Dwyer     __x.param(param_type(__a, __b));
125344cef66SArthur O'Dwyer   return __is;
126344cef66SArthur O'Dwyer }
127344cef66SArthur O'Dwyer 
128344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
129344cef66SArthur O'Dwyer 
130344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
131344cef66SArthur O'Dwyer 
132344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
133