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_EXPONENTIAL_DISTRIBUTION_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 134824e7fdSDimitry Andric #include <__random/generate_canonical.h> 1481ad6265SDimitry Andric #include <__random/is_valid.h> 154824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h> 164824e7fdSDimitry Andric #include <cmath> 174824e7fdSDimitry Andric #include <iosfwd> 184824e7fdSDimitry Andric #include <limits> 194824e7fdSDimitry Andric 204824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 214824e7fdSDimitry Andric # pragma GCC system_header 224824e7fdSDimitry Andric #endif 234824e7fdSDimitry Andric 244824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 254824e7fdSDimitry Andric #include <__undef_macros> 264824e7fdSDimitry Andric 274824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 284824e7fdSDimitry Andric 294824e7fdSDimitry Andric template <class _RealType = double> 30*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS exponential_distribution { 315f757f3fSDimitry Andric static_assert(__libcpp_random_is_valid_realtype<_RealType>::value, 325f757f3fSDimitry Andric "RealType must be a supported floating-point type"); 335f757f3fSDimitry Andric 344824e7fdSDimitry Andric public: 354824e7fdSDimitry Andric // types 364824e7fdSDimitry Andric typedef _RealType result_type; 374824e7fdSDimitry Andric 38*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS param_type { 394824e7fdSDimitry Andric result_type __lambda_; 40*cb14a3feSDimitry Andric 414824e7fdSDimitry Andric public: 424824e7fdSDimitry Andric typedef exponential_distribution distribution_type; 434824e7fdSDimitry Andric 44*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} 454824e7fdSDimitry Andric 46*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __lambda_; } 474824e7fdSDimitry Andric 48*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { 49*cb14a3feSDimitry Andric return __x.__lambda_ == __y.__lambda_; 50*cb14a3feSDimitry Andric } 51*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } 524824e7fdSDimitry Andric }; 534824e7fdSDimitry Andric 544824e7fdSDimitry Andric private: 554824e7fdSDimitry Andric param_type __p_; 564824e7fdSDimitry Andric 574824e7fdSDimitry Andric public: 584824e7fdSDimitry Andric // constructors and reset functions 594824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG 60*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI exponential_distribution() : exponential_distribution(1) {} 61*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda) : __p_(param_type(__lambda)) {} 624824e7fdSDimitry Andric #else 63*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda = 1) : __p_(param_type(__lambda)) {} 644824e7fdSDimitry Andric #endif 65*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(const param_type& __p) : __p_(__p) {} 66*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void reset() {} 674824e7fdSDimitry Andric 684824e7fdSDimitry Andric // generating functions 694824e7fdSDimitry Andric template <class _URNG> 70*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { 71*cb14a3feSDimitry Andric return (*this)(__g, __p_); 72*cb14a3feSDimitry Andric } 7306c3fb27SDimitry Andric template <class _URNG> 7406c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); 754824e7fdSDimitry Andric 764824e7fdSDimitry Andric // property functions 77*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __p_.lambda(); } 784824e7fdSDimitry Andric 79*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } 80*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } 814824e7fdSDimitry Andric 82*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } 83*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); } 844824e7fdSDimitry Andric 85*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool 86*cb14a3feSDimitry Andric operator==(const exponential_distribution& __x, const exponential_distribution& __y) { 87*cb14a3feSDimitry Andric return __x.__p_ == __y.__p_; 88*cb14a3feSDimitry Andric } 89*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool 90*cb14a3feSDimitry Andric operator!=(const exponential_distribution& __x, const exponential_distribution& __y) { 91*cb14a3feSDimitry Andric return !(__x == __y); 92*cb14a3feSDimitry Andric } 934824e7fdSDimitry Andric }; 944824e7fdSDimitry Andric 954824e7fdSDimitry Andric template <class _RealType> 964824e7fdSDimitry Andric template <class _URNG> 97*cb14a3feSDimitry Andric _RealType exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) { 9881ad6265SDimitry Andric static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 99*cb14a3feSDimitry Andric return -std::log(result_type(1) - std::generate_canonical<result_type, numeric_limits<result_type>::digits>(__g)) / 100*cb14a3feSDimitry Andric __p.lambda(); 1014824e7fdSDimitry Andric } 1024824e7fdSDimitry Andric 1034824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RealType> 104bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 105*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const exponential_distribution<_RealType>& __x) { 1064824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__os); 1074824e7fdSDimitry Andric typedef basic_ostream<_CharT, _Traits> _OStream; 108*cb14a3feSDimitry Andric __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); 1094824e7fdSDimitry Andric return __os << __x.lambda(); 1104824e7fdSDimitry Andric } 1114824e7fdSDimitry Andric 1124824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RealType> 113bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 114*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, exponential_distribution<_RealType>& __x) { 1154824e7fdSDimitry Andric typedef exponential_distribution<_RealType> _Eng; 1164824e7fdSDimitry Andric typedef typename _Eng::result_type result_type; 1174824e7fdSDimitry Andric typedef typename _Eng::param_type param_type; 1184824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__is); 1194824e7fdSDimitry Andric typedef basic_istream<_CharT, _Traits> _Istream; 1204824e7fdSDimitry Andric __is.flags(_Istream::dec | _Istream::skipws); 1214824e7fdSDimitry Andric result_type __lambda; 1224824e7fdSDimitry Andric __is >> __lambda; 1234824e7fdSDimitry Andric if (!__is.fail()) 1244824e7fdSDimitry Andric __x.param(param_type(__lambda)); 1254824e7fdSDimitry Andric return __is; 1264824e7fdSDimitry Andric } 1274824e7fdSDimitry Andric 1284824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 1294824e7fdSDimitry Andric 1304824e7fdSDimitry Andric _LIBCPP_POP_MACROS 1314824e7fdSDimitry Andric 1324824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H 133