brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.9 KiB · c44ecbf Raw
171 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_NORMAL_DISTRIBUTION_H10#define _LIBCPP___CXX03___RANDOM_NORMAL_DISTRIBUTION_H11 12#include <__cxx03/__config>13#include <__cxx03/__random/is_valid.h>14#include <__cxx03/__random/uniform_real_distribution.h>15#include <__cxx03/cmath>16#include <__cxx03/iosfwd>17#include <__cxx03/limits>18 19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20#  pragma GCC system_header21#endif22 23_LIBCPP_PUSH_MACROS24#include <__cxx03/__undef_macros>25 26_LIBCPP_BEGIN_NAMESPACE_STD27 28template <class _RealType = double>29class _LIBCPP_TEMPLATE_VIS normal_distribution {30  static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,31                "RealType must be a supported floating-point type");32 33public:34  // types35  typedef _RealType result_type;36 37  class _LIBCPP_TEMPLATE_VIS param_type {38    result_type __mean_;39    result_type __stddev_;40 41  public:42    typedef normal_distribution distribution_type;43 44    _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __mean = 0, result_type __stddev = 1)45        : __mean_(__mean), __stddev_(__stddev) {}46 47    _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __mean_; }48    _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __stddev_; }49 50    friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {51      return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;52    }53    friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }54  };55 56private:57  param_type __p_;58  result_type __v_;59  bool __v_hot_;60 61public:62  // constructors and reset functions63  _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)64      : __p_(param_type(__mean, __stddev)), __v_hot_(false) {}65  _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(const param_type& __p) : __p_(__p), __v_hot_(false) {}66  _LIBCPP_HIDE_FROM_ABI void reset() { __v_hot_ = false; }67 68  // generating functions69  template <class _URNG>70  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {71    return (*this)(__g, __p_);72  }73  template <class _URNG>74  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);75 76  // property functions77  _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __p_.mean(); }78  _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __p_.stddev(); }79 80  _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }81  _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }82 83  _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }84  _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }85 86  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const normal_distribution& __x, const normal_distribution& __y) {87    return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ && (!__x.__v_hot_ || __x.__v_ == __y.__v_);88  }89  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const normal_distribution& __x, const normal_distribution& __y) {90    return !(__x == __y);91  }92 93  template <class _CharT, class _Traits, class _RT>94  friend basic_ostream<_CharT, _Traits>&95  operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x);96 97  template <class _CharT, class _Traits, class _RT>98  friend basic_istream<_CharT, _Traits>&99  operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x);100};101 102template <class _RealType>103template <class _URNG>104_RealType normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {105  static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");106  result_type __up;107  if (__v_hot_) {108    __v_hot_ = false;109    __up     = __v_;110  } else {111    uniform_real_distribution<result_type> __uni(-1, 1);112    result_type __u;113    result_type __v;114    result_type __s;115    do {116      __u = __uni(__g);117      __v = __uni(__g);118      __s = __u * __u + __v * __v;119    } while (__s > 1 || __s == 0);120    result_type __fp = std::sqrt(-2 * std::log(__s) / __s);121    __v_             = __v * __fp;122    __v_hot_         = true;123    __up             = __u * __fp;124  }125  return __up * __p.stddev() + __p.mean();126}127 128template <class _CharT, class _Traits, class _RT>129_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&130operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x) {131  __save_flags<_CharT, _Traits> __lx(__os);132  typedef basic_ostream<_CharT, _Traits> _OStream;133  __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);134  _CharT __sp = __os.widen(' ');135  __os.fill(__sp);136  __os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_;137  if (__x.__v_hot_)138    __os << __sp << __x.__v_;139  return __os;140}141 142template <class _CharT, class _Traits, class _RT>143_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&144operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x) {145  typedef normal_distribution<_RT> _Eng;146  typedef typename _Eng::result_type result_type;147  typedef typename _Eng::param_type param_type;148  __save_flags<_CharT, _Traits> __lx(__is);149  typedef basic_istream<_CharT, _Traits> _Istream;150  __is.flags(_Istream::dec | _Istream::skipws);151  result_type __mean;152  result_type __stddev;153  result_type __vp = 0;154  bool __v_hot     = false;155  __is >> __mean >> __stddev >> __v_hot;156  if (__v_hot)157    __is >> __vp;158  if (!__is.fail()) {159    __x.param(param_type(__mean, __stddev));160    __x.__v_hot_ = __v_hot;161    __x.__v_     = __vp;162  }163  return __is;164}165 166_LIBCPP_END_NAMESPACE_STD167 168_LIBCPP_POP_MACROS169 170#endif // _LIBCPP___CXX03___RANDOM_NORMAL_DISTRIBUTION_H171