brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 3e502b6 Raw
121 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_CHI_SQUARED_DISTRIBUTION_H10#define _LIBCPP___CXX03___RANDOM_CHI_SQUARED_DISTRIBUTION_H11 12#include <__cxx03/__config>13#include <__cxx03/__random/gamma_distribution.h>14#include <__cxx03/__random/is_valid.h>15#include <__cxx03/iosfwd>16#include <__cxx03/limits>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19#  pragma GCC system_header20#endif21 22_LIBCPP_PUSH_MACROS23#include <__cxx03/__undef_macros>24 25_LIBCPP_BEGIN_NAMESPACE_STD26 27template <class _RealType = double>28class _LIBCPP_TEMPLATE_VIS chi_squared_distribution {29  static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,30                "RealType must be a supported floating-point type");31 32public:33  // types34  typedef _RealType result_type;35 36  class _LIBCPP_TEMPLATE_VIS param_type {37    result_type __n_;38 39  public:40    typedef chi_squared_distribution distribution_type;41 42    _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}43 44    _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }45 46    friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {47      return __x.__n_ == __y.__n_;48    }49    friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }50  };51 52private:53  param_type __p_;54 55public:56  // constructor and reset functions57  _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n = 1) : __p_(param_type(__n)) {}58  _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(const param_type& __p) : __p_(__p) {}59  _LIBCPP_HIDE_FROM_ABI void reset() {}60 61  // generating functions62  template <class _URNG>63  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {64    return (*this)(__g, __p_);65  }66  template <class _URNG>67  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {68    return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);69  }70 71  // property functions72  _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }73 74  _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }75  _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }76 77  _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }78  _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }79 80  friend _LIBCPP_HIDE_FROM_ABI bool81  operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {82    return __x.__p_ == __y.__p_;83  }84  friend _LIBCPP_HIDE_FROM_ABI bool85  operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {86    return !(__x == __y);87  }88};89 90template <class _CharT, class _Traits, class _RT>91_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&92operator<<(basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution<_RT>& __x) {93  __save_flags<_CharT, _Traits> __lx(__os);94  typedef basic_ostream<_CharT, _Traits> _OStream;95  __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);96  __os << __x.n();97  return __os;98}99 100template <class _CharT, class _Traits, class _RT>101_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&102operator>>(basic_istream<_CharT, _Traits>& __is, chi_squared_distribution<_RT>& __x) {103  typedef chi_squared_distribution<_RT> _Eng;104  typedef typename _Eng::result_type result_type;105  typedef typename _Eng::param_type param_type;106  __save_flags<_CharT, _Traits> __lx(__is);107  typedef basic_istream<_CharT, _Traits> _Istream;108  __is.flags(_Istream::dec | _Istream::skipws);109  result_type __n;110  __is >> __n;111  if (!__is.fail())112    __x.param(param_type(__n));113  return __is;114}115 116_LIBCPP_END_NAMESPACE_STD117 118_LIBCPP_POP_MACROS119 120#endif // _LIBCPP___CXX03___RANDOM_CHI_SQUARED_DISTRIBUTION_H121