Path: blob/main/contrib/llvm-project/libcxx/include/__random/chi_squared_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_CHI_SQUARED_DISTRIBUTION_H9#define _LIBCPP___RANDOM_CHI_SQUARED_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 chi_squared_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 __n_;3738public:39typedef chi_squared_distribution distribution_type;4041_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}4243_LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }4445friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {46return __x.__n_ == __y.__n_;47}48friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }49};5051private:52param_type __p_;5354public:55// constructor and reset functions56#ifndef _LIBCPP_CXX03_LANG57_LIBCPP_HIDE_FROM_ABI chi_squared_distribution() : chi_squared_distribution(1) {}58_LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n) : __p_(param_type(__n)) {}59#else60_LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n = 1) : __p_(param_type(__n)) {}61#endif62_LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(const param_type& __p) : __p_(__p) {}63_LIBCPP_HIDE_FROM_ABI void reset() {}6465// generating functions66template <class _URNG>67_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {68return (*this)(__g, __p_);69}70template <class _URNG>71_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {72return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);73}7475// property functions76_LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }7778_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }79_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8081_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }82_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }8384friend _LIBCPP_HIDE_FROM_ABI bool85operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {86return __x.__p_ == __y.__p_;87}88friend _LIBCPP_HIDE_FROM_ABI bool89operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {90return !(__x == __y);91}92};9394template <class _CharT, class _Traits, class _RT>95_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&96operator<<(basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution<_RT>& __x) {97__save_flags<_CharT, _Traits> __lx(__os);98typedef basic_ostream<_CharT, _Traits> _OStream;99__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);100__os << __x.n();101return __os;102}103104template <class _CharT, class _Traits, class _RT>105_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&106operator>>(basic_istream<_CharT, _Traits>& __is, chi_squared_distribution<_RT>& __x) {107typedef chi_squared_distribution<_RT> _Eng;108typedef typename _Eng::result_type result_type;109typedef typename _Eng::param_type param_type;110__save_flags<_CharT, _Traits> __lx(__is);111typedef basic_istream<_CharT, _Traits> _Istream;112__is.flags(_Istream::dec | _Istream::skipws);113result_type __n;114__is >> __n;115if (!__is.fail())116__x.param(param_type(__n));117return __is;118}119120_LIBCPP_END_NAMESPACE_STD121122_LIBCPP_POP_MACROS123124#endif // _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H125126127