student_t_distribution.h 4.8 KB

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