128 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___RANDOM_STUDENT_T_DISTRIBUTION_H10#define _LIBCPP___CXX03___RANDOM_STUDENT_T_DISTRIBUTION_H11 12#include <__cxx03/__config>13#include <__cxx03/__random/gamma_distribution.h>14#include <__cxx03/__random/is_valid.h>15#include <__cxx03/__random/normal_distribution.h>16#include <__cxx03/cmath>17#include <__cxx03/iosfwd>18#include <__cxx03/limits>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif23 24_LIBCPP_PUSH_MACROS25#include <__cxx03/__undef_macros>26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29template <class _RealType = double>30class _LIBCPP_TEMPLATE_VIS student_t_distribution {31 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,32 "RealType must be a supported floating-point type");33 34public:35 // types36 typedef _RealType result_type;37 38 class _LIBCPP_TEMPLATE_VIS param_type {39 result_type __n_;40 41 public:42 typedef student_t_distribution distribution_type;43 44 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}45 46 _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }47 48 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {49 return __x.__n_ == __y.__n_;50 }51 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }52 };53 54private:55 param_type __p_;56 normal_distribution<result_type> __nd_;57 58public:59 // constructor and reset functions60 _LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(result_type __n = 1) : __p_(param_type(__n)) {}61 _LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(const param_type& __p) : __p_(__p) {}62 _LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); }63 64 // generating functions65 template <class _URNG>66 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {67 return (*this)(__g, __p_);68 }69 template <class _URNG>70 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);71 72 // property functions73 _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }74 75 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }76 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }77 78 _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }79 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }80 81 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const student_t_distribution& __x, const student_t_distribution& __y) {82 return __x.__p_ == __y.__p_;83 }84 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const student_t_distribution& __x, const student_t_distribution& __y) {85 return !(__x == __y);86 }87};88 89template <class _RealType>90template <class _URNG>91_RealType student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {92 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");93 gamma_distribution<result_type> __gd(__p.n() * .5, 2);94 return __nd_(__g) * std::sqrt(__p.n() / __gd(__g));95}96 97template <class _CharT, class _Traits, class _RT>98_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&99operator<<(basic_ostream<_CharT, _Traits>& __os, const student_t_distribution<_RT>& __x) {100 __save_flags<_CharT, _Traits> __lx(__os);101 typedef basic_ostream<_CharT, _Traits> _OStream;102 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);103 __os << __x.n();104 return __os;105}106 107template <class _CharT, class _Traits, class _RT>108_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&109operator>>(basic_istream<_CharT, _Traits>& __is, student_t_distribution<_RT>& __x) {110 typedef student_t_distribution<_RT> _Eng;111 typedef typename _Eng::result_type result_type;112 typedef typename _Eng::param_type param_type;113 __save_flags<_CharT, _Traits> __lx(__is);114 typedef basic_istream<_CharT, _Traits> _Istream;115 __is.flags(_Istream::dec | _Istream::skipws);116 result_type __n;117 __is >> __n;118 if (!__is.fail())119 __x.param(param_type(__n));120 return __is;121}122 123_LIBCPP_END_NAMESPACE_STD124 125_LIBCPP_POP_MACROS126 127#endif // _LIBCPP___CXX03___RANDOM_STUDENT_T_DISTRIBUTION_H128