fisher_f_distribution.h 4.8 KB

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