chi_squared_distribution.h 4.4 KB

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