extreme_value_distribution.h 5.0 KB

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