xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/weibull_distribution.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__config>
134824e7fdSDimitry Andric #include <__random/exponential_distribution.h>
14*5f757f3fSDimitry Andric #include <__random/is_valid.h>
154824e7fdSDimitry Andric #include <cmath>
164824e7fdSDimitry Andric #include <iosfwd>
174824e7fdSDimitry Andric #include <limits>
184824e7fdSDimitry Andric 
194824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
204824e7fdSDimitry Andric #  pragma GCC system_header
214824e7fdSDimitry Andric #endif
224824e7fdSDimitry Andric 
234824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
244824e7fdSDimitry Andric #include <__undef_macros>
254824e7fdSDimitry Andric 
264824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
274824e7fdSDimitry Andric 
284824e7fdSDimitry Andric template<class _RealType = double>
294824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS weibull_distribution
304824e7fdSDimitry Andric {
31*5f757f3fSDimitry Andric   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32*5f757f3fSDimitry Andric                 "RealType must be a supported floating-point type");
33*5f757f3fSDimitry Andric 
344824e7fdSDimitry Andric public:
354824e7fdSDimitry Andric     // types
364824e7fdSDimitry Andric     typedef _RealType result_type;
374824e7fdSDimitry Andric 
384824e7fdSDimitry Andric     class _LIBCPP_TEMPLATE_VIS param_type
394824e7fdSDimitry Andric     {
404824e7fdSDimitry Andric         result_type __a_;
414824e7fdSDimitry Andric         result_type __b_;
424824e7fdSDimitry Andric     public:
434824e7fdSDimitry Andric         typedef weibull_distribution distribution_type;
444824e7fdSDimitry Andric 
45*5f757f3fSDimitry Andric         _LIBCPP_HIDE_FROM_ABI
464824e7fdSDimitry Andric         explicit param_type(result_type __a = 1, result_type __b = 1)
474824e7fdSDimitry Andric             : __a_(__a), __b_(__b) {}
484824e7fdSDimitry Andric 
49*5f757f3fSDimitry Andric         _LIBCPP_HIDE_FROM_ABI
504824e7fdSDimitry Andric         result_type a() const {return __a_;}
51*5f757f3fSDimitry Andric         _LIBCPP_HIDE_FROM_ABI
524824e7fdSDimitry Andric         result_type b() const {return __b_;}
534824e7fdSDimitry Andric 
54*5f757f3fSDimitry Andric         friend _LIBCPP_HIDE_FROM_ABI
554824e7fdSDimitry Andric             bool operator==(const param_type& __x, const param_type& __y)
564824e7fdSDimitry Andric             {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
57*5f757f3fSDimitry Andric         friend _LIBCPP_HIDE_FROM_ABI
584824e7fdSDimitry Andric             bool operator!=(const param_type& __x, const param_type& __y)
594824e7fdSDimitry Andric             {return !(__x == __y);}
604824e7fdSDimitry Andric     };
614824e7fdSDimitry Andric 
624824e7fdSDimitry Andric private:
634824e7fdSDimitry Andric     param_type __p_;
644824e7fdSDimitry Andric 
654824e7fdSDimitry Andric public:
664824e7fdSDimitry Andric     // constructor and reset functions
674824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG
68*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
694824e7fdSDimitry Andric     weibull_distribution() : weibull_distribution(1) {}
70*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
714824e7fdSDimitry Andric     explicit weibull_distribution(result_type __a, result_type __b = 1)
724824e7fdSDimitry Andric         : __p_(param_type(__a, __b)) {}
734824e7fdSDimitry Andric #else
74*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
754824e7fdSDimitry Andric     explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
764824e7fdSDimitry Andric         : __p_(param_type(__a, __b)) {}
774824e7fdSDimitry Andric #endif
78*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
794824e7fdSDimitry Andric     explicit weibull_distribution(const param_type& __p)
804824e7fdSDimitry Andric         : __p_(__p) {}
81*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
824824e7fdSDimitry Andric     void reset() {}
834824e7fdSDimitry Andric 
844824e7fdSDimitry Andric     // generating functions
854824e7fdSDimitry Andric     template<class _URNG>
86*5f757f3fSDimitry Andric         _LIBCPP_HIDE_FROM_ABI
874824e7fdSDimitry Andric         result_type operator()(_URNG& __g)
884824e7fdSDimitry Andric         {return (*this)(__g, __p_);}
894824e7fdSDimitry Andric     template<class _URNG>
90*5f757f3fSDimitry Andric         _LIBCPP_HIDE_FROM_ABI
914824e7fdSDimitry Andric         result_type operator()(_URNG& __g, const param_type& __p)
924824e7fdSDimitry Andric         {return __p.b() *
93*5f757f3fSDimitry Andric             std::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
944824e7fdSDimitry Andric 
954824e7fdSDimitry Andric     // property functions
96*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
974824e7fdSDimitry Andric     result_type a() const {return __p_.a();}
98*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
994824e7fdSDimitry Andric     result_type b() const {return __p_.b();}
1004824e7fdSDimitry Andric 
101*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
1024824e7fdSDimitry Andric     param_type param() const {return __p_;}
103*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
1044824e7fdSDimitry Andric     void param(const param_type& __p) {__p_ = __p;}
1054824e7fdSDimitry Andric 
106*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
1074824e7fdSDimitry Andric     result_type min() const {return 0;}
108*5f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
1094824e7fdSDimitry Andric     result_type max() const {return numeric_limits<result_type>::infinity();}
1104824e7fdSDimitry Andric 
111*5f757f3fSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI
1124824e7fdSDimitry Andric         bool operator==(const weibull_distribution& __x,
1134824e7fdSDimitry Andric                         const weibull_distribution& __y)
1144824e7fdSDimitry Andric         {return __x.__p_ == __y.__p_;}
115*5f757f3fSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI
1164824e7fdSDimitry Andric         bool operator!=(const weibull_distribution& __x,
1174824e7fdSDimitry Andric                         const weibull_distribution& __y)
1184824e7fdSDimitry Andric         {return !(__x == __y);}
1194824e7fdSDimitry Andric };
1204824e7fdSDimitry Andric 
1214824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT>
122bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1234824e7fdSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os,
1244824e7fdSDimitry Andric            const weibull_distribution<_RT>& __x)
1254824e7fdSDimitry Andric {
1264824e7fdSDimitry Andric     __save_flags<_CharT, _Traits> __lx(__os);
1274824e7fdSDimitry Andric     typedef basic_ostream<_CharT, _Traits> _OStream;
1284824e7fdSDimitry Andric     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
1294824e7fdSDimitry Andric                _OStream::scientific);
1304824e7fdSDimitry Andric     _CharT __sp = __os.widen(' ');
1314824e7fdSDimitry Andric     __os.fill(__sp);
1324824e7fdSDimitry Andric     __os << __x.a() << __sp << __x.b();
1334824e7fdSDimitry Andric     return __os;
1344824e7fdSDimitry Andric }
1354824e7fdSDimitry Andric 
1364824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT>
137bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1384824e7fdSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is,
1394824e7fdSDimitry Andric            weibull_distribution<_RT>& __x)
1404824e7fdSDimitry Andric {
1414824e7fdSDimitry Andric     typedef weibull_distribution<_RT> _Eng;
1424824e7fdSDimitry Andric     typedef typename _Eng::result_type result_type;
1434824e7fdSDimitry Andric     typedef typename _Eng::param_type param_type;
1444824e7fdSDimitry Andric     __save_flags<_CharT, _Traits> __lx(__is);
1454824e7fdSDimitry Andric     typedef basic_istream<_CharT, _Traits> _Istream;
1464824e7fdSDimitry Andric     __is.flags(_Istream::dec | _Istream::skipws);
1474824e7fdSDimitry Andric     result_type __a;
1484824e7fdSDimitry Andric     result_type __b;
1494824e7fdSDimitry Andric     __is >> __a >> __b;
1504824e7fdSDimitry Andric     if (!__is.fail())
1514824e7fdSDimitry Andric         __x.param(param_type(__a, __b));
1524824e7fdSDimitry Andric     return __is;
1534824e7fdSDimitry Andric }
1544824e7fdSDimitry Andric 
1554824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1564824e7fdSDimitry Andric 
1574824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1584824e7fdSDimitry Andric 
1594824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H
160