extreme_value_distribution.h 5.2 KB

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