extreme_value_distribution.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/is_valid.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 extreme_value_distribution {
  24. static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
  25. "RealType must be a supported floating-point type");
  26. public:
  27. // types
  28. typedef _RealType result_type;
  29. class _LIBCPP_TEMPLATE_VIS param_type {
  30. result_type __a_;
  31. result_type __b_;
  32. public:
  33. typedef extreme_value_distribution distribution_type;
  34. _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
  35. _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
  36. _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
  37. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  38. return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
  39. }
  40. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  41. };
  42. private:
  43. param_type __p_;
  44. public:
  45. // constructor and reset functions
  46. #ifndef _LIBCPP_CXX03_LANG
  47. _LIBCPP_HIDE_FROM_ABI extreme_value_distribution() : extreme_value_distribution(0) {}
  48. _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(result_type __a, result_type __b = 1)
  49. : __p_(param_type(__a, __b)) {}
  50. #else
  51. _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
  52. : __p_(param_type(__a, __b)) {}
  53. #endif
  54. _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(const param_type& __p) : __p_(__p) {}
  55. _LIBCPP_HIDE_FROM_ABI void reset() {}
  56. // generating functions
  57. template <class _URNG>
  58. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  59. return (*this)(__g, __p_);
  60. }
  61. template <class _URNG>
  62. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  63. // property functions
  64. _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
  65. _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
  66. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  67. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  68. _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
  69. _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
  70. friend _LIBCPP_HIDE_FROM_ABI bool
  71. operator==(const extreme_value_distribution& __x, const extreme_value_distribution& __y) {
  72. return __x.__p_ == __y.__p_;
  73. }
  74. friend _LIBCPP_HIDE_FROM_ABI bool
  75. operator!=(const extreme_value_distribution& __x, const extreme_value_distribution& __y) {
  76. return !(__x == __y);
  77. }
  78. };
  79. template <class _RealType>
  80. template <class _URNG>
  81. _RealType extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
  82. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  83. return __p.a() - __p.b() * std::log(-std::log(1 - uniform_real_distribution<result_type>()(__g)));
  84. }
  85. template <class _CharT, class _Traits, class _RT>
  86. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  87. operator<<(basic_ostream<_CharT, _Traits>& __os, const extreme_value_distribution<_RT>& __x) {
  88. __save_flags<_CharT, _Traits> __lx(__os);
  89. typedef basic_ostream<_CharT, _Traits> _OStream;
  90. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  91. _CharT __sp = __os.widen(' ');
  92. __os.fill(__sp);
  93. __os << __x.a() << __sp << __x.b();
  94. return __os;
  95. }
  96. template <class _CharT, class _Traits, class _RT>
  97. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  98. operator>>(basic_istream<_CharT, _Traits>& __is, extreme_value_distribution<_RT>& __x) {
  99. typedef extreme_value_distribution<_RT> _Eng;
  100. typedef typename _Eng::result_type result_type;
  101. typedef typename _Eng::param_type param_type;
  102. __save_flags<_CharT, _Traits> __lx(__is);
  103. typedef basic_istream<_CharT, _Traits> _Istream;
  104. __is.flags(_Istream::dec | _Istream::skipws);
  105. result_type __a;
  106. result_type __b;
  107. __is >> __a >> __b;
  108. if (!__is.fail())
  109. __x.param(param_type(__a, __b));
  110. return __is;
  111. }
  112. _LIBCPP_END_NAMESPACE_STD
  113. _LIBCPP_POP_MACROS
  114. #endif // _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H