student_t_distribution.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_STUDENT_T_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/gamma_distribution.h>
  12. #include <__random/normal_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 student_t_distribution
  24. {
  25. public:
  26. // types
  27. typedef _RealType result_type;
  28. class _LIBCPP_TEMPLATE_VIS param_type
  29. {
  30. result_type __n_;
  31. public:
  32. typedef student_t_distribution distribution_type;
  33. _LIBCPP_INLINE_VISIBILITY
  34. explicit param_type(result_type __n = 1) : __n_(__n) {}
  35. _LIBCPP_INLINE_VISIBILITY
  36. result_type n() const {return __n_;}
  37. friend _LIBCPP_INLINE_VISIBILITY
  38. bool operator==(const param_type& __x, const param_type& __y)
  39. {return __x.__n_ == __y.__n_;}
  40. friend _LIBCPP_INLINE_VISIBILITY
  41. bool operator!=(const param_type& __x, const param_type& __y)
  42. {return !(__x == __y);}
  43. };
  44. private:
  45. param_type __p_;
  46. normal_distribution<result_type> __nd_;
  47. public:
  48. // constructor and reset functions
  49. #ifndef _LIBCPP_CXX03_LANG
  50. _LIBCPP_INLINE_VISIBILITY
  51. student_t_distribution() : student_t_distribution(1) {}
  52. _LIBCPP_INLINE_VISIBILITY
  53. explicit student_t_distribution(result_type __n)
  54. : __p_(param_type(__n)) {}
  55. #else
  56. _LIBCPP_INLINE_VISIBILITY
  57. explicit student_t_distribution(result_type __n = 1)
  58. : __p_(param_type(__n)) {}
  59. #endif
  60. _LIBCPP_INLINE_VISIBILITY
  61. explicit student_t_distribution(const param_type& __p)
  62. : __p_(__p) {}
  63. _LIBCPP_INLINE_VISIBILITY
  64. void reset() {__nd_.reset();}
  65. // generating functions
  66. template<class _URNG>
  67. _LIBCPP_INLINE_VISIBILITY
  68. result_type operator()(_URNG& __g)
  69. {return (*this)(__g, __p_);}
  70. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  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 -numeric_limits<result_type>::infinity();}
  80. _LIBCPP_INLINE_VISIBILITY
  81. result_type max() const {return numeric_limits<result_type>::infinity();}
  82. friend _LIBCPP_INLINE_VISIBILITY
  83. bool operator==(const student_t_distribution& __x,
  84. const student_t_distribution& __y)
  85. {return __x.__p_ == __y.__p_;}
  86. friend _LIBCPP_INLINE_VISIBILITY
  87. bool operator!=(const student_t_distribution& __x,
  88. const student_t_distribution& __y)
  89. {return !(__x == __y);}
  90. };
  91. template <class _RealType>
  92. template<class _URNG>
  93. _RealType
  94. student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  95. {
  96. gamma_distribution<result_type> __gd(__p.n() * .5, 2);
  97. return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
  98. }
  99. template <class _CharT, class _Traits, class _RT>
  100. basic_ostream<_CharT, _Traits>&
  101. operator<<(basic_ostream<_CharT, _Traits>& __os,
  102. const student_t_distribution<_RT>& __x)
  103. {
  104. __save_flags<_CharT, _Traits> __lx(__os);
  105. typedef basic_ostream<_CharT, _Traits> _OStream;
  106. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  107. _OStream::scientific);
  108. __os << __x.n();
  109. return __os;
  110. }
  111. template <class _CharT, class _Traits, class _RT>
  112. basic_istream<_CharT, _Traits>&
  113. operator>>(basic_istream<_CharT, _Traits>& __is,
  114. student_t_distribution<_RT>& __x)
  115. {
  116. typedef student_t_distribution<_RT> _Eng;
  117. typedef typename _Eng::result_type result_type;
  118. typedef typename _Eng::param_type param_type;
  119. __save_flags<_CharT, _Traits> __lx(__is);
  120. typedef basic_istream<_CharT, _Traits> _Istream;
  121. __is.flags(_Istream::dec | _Istream::skipws);
  122. result_type __n;
  123. __is >> __n;
  124. if (!__is.fail())
  125. __x.param(param_type(__n));
  126. return __is;
  127. }
  128. _LIBCPP_END_NAMESPACE_STD
  129. _LIBCPP_POP_MACROS
  130. #endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H