Path: blob/main/contrib/llvm-project/libcxx/include/__random/normal_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_NORMAL_DISTRIBUTION_H9#define _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H1011#include <__config>12#include <__random/is_valid.h>13#include <__random/uniform_real_distribution.h>14#include <cmath>15#include <iosfwd>16#include <limits>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_PUSH_MACROS23#include <__undef_macros>2425_LIBCPP_BEGIN_NAMESPACE_STD2627template <class _RealType = double>28class _LIBCPP_TEMPLATE_VIS normal_distribution {29static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,30"RealType must be a supported floating-point type");3132public:33// types34typedef _RealType result_type;3536class _LIBCPP_TEMPLATE_VIS param_type {37result_type __mean_;38result_type __stddev_;3940public:41typedef normal_distribution distribution_type;4243_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __mean = 0, result_type __stddev = 1)44: __mean_(__mean), __stddev_(__stddev) {}4546_LIBCPP_HIDE_FROM_ABI result_type mean() const { return __mean_; }47_LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __stddev_; }4849friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {50return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;51}52friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }53};5455private:56param_type __p_;57result_type __v_;58bool __v_hot_;5960public:61// constructors and reset functions62#ifndef _LIBCPP_CXX03_LANG63_LIBCPP_HIDE_FROM_ABI normal_distribution() : normal_distribution(0) {}64_LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean, result_type __stddev = 1)65: __p_(param_type(__mean, __stddev)), __v_hot_(false) {}66#else67_LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)68: __p_(param_type(__mean, __stddev)), __v_hot_(false) {}69#endif70_LIBCPP_HIDE_FROM_ABI explicit normal_distribution(const param_type& __p) : __p_(__p), __v_hot_(false) {}71_LIBCPP_HIDE_FROM_ABI void reset() { __v_hot_ = false; }7273// generating functions74template <class _URNG>75_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {76return (*this)(__g, __p_);77}78template <class _URNG>79_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);8081// property functions82_LIBCPP_HIDE_FROM_ABI result_type mean() const { return __p_.mean(); }83_LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __p_.stddev(); }8485_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }86_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8788_LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }89_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }9091friend _LIBCPP_HIDE_FROM_ABI bool operator==(const normal_distribution& __x, const normal_distribution& __y) {92return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ && (!__x.__v_hot_ || __x.__v_ == __y.__v_);93}94friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const normal_distribution& __x, const normal_distribution& __y) {95return !(__x == __y);96}9798template <class _CharT, class _Traits, class _RT>99friend basic_ostream<_CharT, _Traits>&100operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x);101102template <class _CharT, class _Traits, class _RT>103friend basic_istream<_CharT, _Traits>&104operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x);105};106107template <class _RealType>108template <class _URNG>109_RealType normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {110static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");111result_type __up;112if (__v_hot_) {113__v_hot_ = false;114__up = __v_;115} else {116uniform_real_distribution<result_type> __uni(-1, 1);117result_type __u;118result_type __v;119result_type __s;120do {121__u = __uni(__g);122__v = __uni(__g);123__s = __u * __u + __v * __v;124} while (__s > 1 || __s == 0);125result_type __fp = std::sqrt(-2 * std::log(__s) / __s);126__v_ = __v * __fp;127__v_hot_ = true;128__up = __u * __fp;129}130return __up * __p.stddev() + __p.mean();131}132133template <class _CharT, class _Traits, class _RT>134_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&135operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x) {136__save_flags<_CharT, _Traits> __lx(__os);137typedef basic_ostream<_CharT, _Traits> _OStream;138__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);139_CharT __sp = __os.widen(' ');140__os.fill(__sp);141__os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_;142if (__x.__v_hot_)143__os << __sp << __x.__v_;144return __os;145}146147template <class _CharT, class _Traits, class _RT>148_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&149operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x) {150typedef normal_distribution<_RT> _Eng;151typedef typename _Eng::result_type result_type;152typedef typename _Eng::param_type param_type;153__save_flags<_CharT, _Traits> __lx(__is);154typedef basic_istream<_CharT, _Traits> _Istream;155__is.flags(_Istream::dec | _Istream::skipws);156result_type __mean;157result_type __stddev;158result_type __vp = 0;159bool __v_hot = false;160__is >> __mean >> __stddev >> __v_hot;161if (__v_hot)162__is >> __vp;163if (!__is.fail()) {164__x.param(param_type(__mean, __stddev));165__x.__v_hot_ = __v_hot;166__x.__v_ = __vp;167}168return __is;169}170171_LIBCPP_END_NAMESPACE_STD172173_LIBCPP_POP_MACROS174175#endif // _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H176177178