Path: blob/main/contrib/llvm-project/libcxx/include/__random/fisher_f_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_FISHER_F_DISTRIBUTION_H9#define _LIBCPP___RANDOM_FISHER_F_DISTRIBUTION_H1011#include <__config>12#include <__random/gamma_distribution.h>13#include <__random/is_valid.h>14#include <iosfwd>15#include <limits>1617#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18# pragma GCC system_header19#endif2021_LIBCPP_PUSH_MACROS22#include <__undef_macros>2324_LIBCPP_BEGIN_NAMESPACE_STD2526template <class _RealType = double>27class _LIBCPP_TEMPLATE_VIS fisher_f_distribution {28static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,29"RealType must be a supported floating-point type");3031public:32// types33typedef _RealType result_type;3435class _LIBCPP_TEMPLATE_VIS param_type {36result_type __m_;37result_type __n_;3839public:40typedef fisher_f_distribution distribution_type;4142_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __m = 1, result_type __n = 1) : __m_(__m), __n_(__n) {}4344_LIBCPP_HIDE_FROM_ABI result_type m() const { return __m_; }45_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.__m_ == __y.__m_ && __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_;5556public:57// constructor and reset functions58#ifndef _LIBCPP_CXX03_LANG59_LIBCPP_HIDE_FROM_ABI fisher_f_distribution() : fisher_f_distribution(1) {}60_LIBCPP_HIDE_FROM_ABI explicit fisher_f_distribution(result_type __m, result_type __n = 1)61: __p_(param_type(__m, __n)) {}62#else63_LIBCPP_HIDE_FROM_ABI explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)64: __p_(param_type(__m, __n)) {}65#endif66_LIBCPP_HIDE_FROM_ABI explicit fisher_f_distribution(const param_type& __p) : __p_(__p) {}67_LIBCPP_HIDE_FROM_ABI void reset() {}6869// generating functions70template <class _URNG>71_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {72return (*this)(__g, __p_);73}74template <class _URNG>75_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);7677// property functions78_LIBCPP_HIDE_FROM_ABI result_type m() const { return __p_.m(); }79_LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }8081_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }82_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8384_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }85_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }8687friend _LIBCPP_HIDE_FROM_ABI bool operator==(const fisher_f_distribution& __x, const fisher_f_distribution& __y) {88return __x.__p_ == __y.__p_;89}90friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const fisher_f_distribution& __x, const fisher_f_distribution& __y) {91return !(__x == __y);92}93};9495template <class _RealType>96template <class _URNG>97_RealType fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {98static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");99gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));100gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));101return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));102}103104template <class _CharT, class _Traits, class _RT>105_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&106operator<<(basic_ostream<_CharT, _Traits>& __os, const fisher_f_distribution<_RT>& __x) {107__save_flags<_CharT, _Traits> __lx(__os);108typedef basic_ostream<_CharT, _Traits> _OStream;109__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);110_CharT __sp = __os.widen(' ');111__os.fill(__sp);112__os << __x.m() << __sp << __x.n();113return __os;114}115116template <class _CharT, class _Traits, class _RT>117_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&118operator>>(basic_istream<_CharT, _Traits>& __is, fisher_f_distribution<_RT>& __x) {119typedef fisher_f_distribution<_RT> _Eng;120typedef typename _Eng::result_type result_type;121typedef typename _Eng::param_type param_type;122__save_flags<_CharT, _Traits> __lx(__is);123typedef basic_istream<_CharT, _Traits> _Istream;124__is.flags(_Istream::dec | _Istream::skipws);125result_type __m;126result_type __n;127__is >> __m >> __n;128if (!__is.fail())129__x.param(param_type(__m, __n));130return __is;131}132133_LIBCPP_END_NAMESPACE_STD134135_LIBCPP_POP_MACROS136137#endif // _LIBCPP___RANDOM_FISHER_F_DISTRIBUTION_H138139140