Path: blob/main/contrib/llvm-project/libcxx/include/__random/student_t_distribution.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H9#define _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H1011#include <__config>12#include <__random/gamma_distribution.h>13#include <__random/is_valid.h>14#include <__random/normal_distribution.h>15#include <cmath>16#include <iosfwd>17#include <limits>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_PUSH_MACROS24#include <__undef_macros>2526_LIBCPP_BEGIN_NAMESPACE_STD2728template <class _RealType = double>29class _LIBCPP_TEMPLATE_VIS student_t_distribution {30static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,31"RealType must be a supported floating-point type");3233public:34// types35typedef _RealType result_type;3637class _LIBCPP_TEMPLATE_VIS param_type {38result_type __n_;3940public:41typedef student_t_distribution distribution_type;4243_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}4445_LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }4647friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {48return __x.__n_ == __y.__n_;49}50friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }51};5253private:54param_type __p_;55normal_distribution<result_type> __nd_;5657public:58// constructor and reset functions59#ifndef _LIBCPP_CXX03_LANG60_LIBCPP_HIDE_FROM_ABI student_t_distribution() : student_t_distribution(1) {}61_LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(result_type __n) : __p_(param_type(__n)) {}62#else63_LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(result_type __n = 1) : __p_(param_type(__n)) {}64#endif65_LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(const param_type& __p) : __p_(__p) {}66_LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); }6768// generating functions69template <class _URNG>70_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {71return (*this)(__g, __p_);72}73template <class _URNG>74_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);7576// property functions77_LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }7879_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }80_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8182_LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }83_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }8485friend _LIBCPP_HIDE_FROM_ABI bool operator==(const student_t_distribution& __x, const student_t_distribution& __y) {86return __x.__p_ == __y.__p_;87}88friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const student_t_distribution& __x, const student_t_distribution& __y) {89return !(__x == __y);90}91};9293template <class _RealType>94template <class _URNG>95_RealType student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {96static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");97gamma_distribution<result_type> __gd(__p.n() * .5, 2);98return __nd_(__g) * std::sqrt(__p.n() / __gd(__g));99}100101template <class _CharT, class _Traits, class _RT>102_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&103operator<<(basic_ostream<_CharT, _Traits>& __os, const student_t_distribution<_RT>& __x) {104__save_flags<_CharT, _Traits> __lx(__os);105typedef basic_ostream<_CharT, _Traits> _OStream;106__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);107__os << __x.n();108return __os;109}110111template <class _CharT, class _Traits, class _RT>112_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&113operator>>(basic_istream<_CharT, _Traits>& __is, student_t_distribution<_RT>& __x) {114typedef student_t_distribution<_RT> _Eng;115typedef typename _Eng::result_type result_type;116typedef typename _Eng::param_type param_type;117__save_flags<_CharT, _Traits> __lx(__is);118typedef basic_istream<_CharT, _Traits> _Istream;119__is.flags(_Istream::dec | _Istream::skipws);120result_type __n;121__is >> __n;122if (!__is.fail())123__x.param(param_type(__n));124return __is;125}126127_LIBCPP_END_NAMESPACE_STD128129_LIBCPP_POP_MACROS130131#endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H132133134