exponential_distribution.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/generate_canonical.h>
  12. #include <__random/uniform_real_distribution.h>
  13. #include <cmath>
  14. #include <iosfwd>
  15. #include <limits>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template<class _RealType = double>
  23. class _LIBCPP_TEMPLATE_VIS exponential_distribution
  24. {
  25. public:
  26. // types
  27. typedef _RealType result_type;
  28. class _LIBCPP_TEMPLATE_VIS param_type
  29. {
  30. result_type __lambda_;
  31. public:
  32. typedef exponential_distribution distribution_type;
  33. _LIBCPP_INLINE_VISIBILITY
  34. explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
  35. _LIBCPP_INLINE_VISIBILITY
  36. result_type lambda() const {return __lambda_;}
  37. friend _LIBCPP_INLINE_VISIBILITY
  38. bool operator==(const param_type& __x, const param_type& __y)
  39. {return __x.__lambda_ == __y.__lambda_;}
  40. friend _LIBCPP_INLINE_VISIBILITY
  41. bool operator!=(const param_type& __x, const param_type& __y)
  42. {return !(__x == __y);}
  43. };
  44. private:
  45. param_type __p_;
  46. public:
  47. // constructors and reset functions
  48. #ifndef _LIBCPP_CXX03_LANG
  49. _LIBCPP_INLINE_VISIBILITY
  50. exponential_distribution() : exponential_distribution(1) {}
  51. _LIBCPP_INLINE_VISIBILITY
  52. explicit exponential_distribution(result_type __lambda)
  53. : __p_(param_type(__lambda)) {}
  54. #else
  55. _LIBCPP_INLINE_VISIBILITY
  56. explicit exponential_distribution(result_type __lambda = 1)
  57. : __p_(param_type(__lambda)) {}
  58. #endif
  59. _LIBCPP_INLINE_VISIBILITY
  60. explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
  61. _LIBCPP_INLINE_VISIBILITY
  62. void reset() {}
  63. // generating functions
  64. template<class _URNG>
  65. _LIBCPP_INLINE_VISIBILITY
  66. result_type operator()(_URNG& __g)
  67. {return (*this)(__g, __p_);}
  68. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  69. // property functions
  70. _LIBCPP_INLINE_VISIBILITY
  71. result_type lambda() const {return __p_.lambda();}
  72. _LIBCPP_INLINE_VISIBILITY
  73. param_type param() const {return __p_;}
  74. _LIBCPP_INLINE_VISIBILITY
  75. void param(const param_type& __p) {__p_ = __p;}
  76. _LIBCPP_INLINE_VISIBILITY
  77. result_type min() const {return 0;}
  78. _LIBCPP_INLINE_VISIBILITY
  79. result_type max() const {return numeric_limits<result_type>::infinity();}
  80. friend _LIBCPP_INLINE_VISIBILITY
  81. bool operator==(const exponential_distribution& __x,
  82. const exponential_distribution& __y)
  83. {return __x.__p_ == __y.__p_;}
  84. friend _LIBCPP_INLINE_VISIBILITY
  85. bool operator!=(const exponential_distribution& __x,
  86. const exponential_distribution& __y)
  87. {return !(__x == __y);}
  88. };
  89. template <class _RealType>
  90. template<class _URNG>
  91. _RealType
  92. exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  93. {
  94. return -_VSTD::log
  95. (
  96. result_type(1) -
  97. _VSTD::generate_canonical<result_type,
  98. numeric_limits<result_type>::digits>(__g)
  99. )
  100. / __p.lambda();
  101. }
  102. template <class _CharT, class _Traits, class _RealType>
  103. basic_ostream<_CharT, _Traits>&
  104. operator<<(basic_ostream<_CharT, _Traits>& __os,
  105. const exponential_distribution<_RealType>& __x)
  106. {
  107. __save_flags<_CharT, _Traits> __lx(__os);
  108. typedef basic_ostream<_CharT, _Traits> _OStream;
  109. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  110. _OStream::scientific);
  111. return __os << __x.lambda();
  112. }
  113. template <class _CharT, class _Traits, class _RealType>
  114. basic_istream<_CharT, _Traits>&
  115. operator>>(basic_istream<_CharT, _Traits>& __is,
  116. exponential_distribution<_RealType>& __x)
  117. {
  118. typedef exponential_distribution<_RealType> _Eng;
  119. typedef typename _Eng::result_type result_type;
  120. typedef typename _Eng::param_type param_type;
  121. __save_flags<_CharT, _Traits> __lx(__is);
  122. typedef basic_istream<_CharT, _Traits> _Istream;
  123. __is.flags(_Istream::dec | _Istream::skipws);
  124. result_type __lambda;
  125. __is >> __lambda;
  126. if (!__is.fail())
  127. __x.param(param_type(__lambda));
  128. return __is;
  129. }
  130. _LIBCPP_END_NAMESPACE_STD
  131. _LIBCPP_POP_MACROS
  132. #endif // _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H