Path: blob/main/contrib/llvm-project/libcxx/include/__random/poisson_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_POISSON_DISTRIBUTION_H9#define _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H1011#include <__config>12#include <__random/clamp_to_integral.h>13#include <__random/exponential_distribution.h>14#include <__random/is_valid.h>15#include <__random/normal_distribution.h>16#include <__random/uniform_real_distribution.h>17#include <cmath>18#include <iosfwd>19#include <limits>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425_LIBCPP_PUSH_MACROS26#include <__undef_macros>2728_LIBCPP_BEGIN_NAMESPACE_STD2930template <class _IntType = int>31class _LIBCPP_TEMPLATE_VIS poisson_distribution {32static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");3334public:35// types36typedef _IntType result_type;3738class _LIBCPP_TEMPLATE_VIS param_type {39double __mean_;40double __s_;41double __d_;42double __l_;43double __omega_;44double __c0_;45double __c1_;46double __c2_;47double __c3_;48double __c_;4950public:51typedef poisson_distribution distribution_type;5253_LIBCPP_HIDE_FROM_ABI explicit param_type(double __mean = 1.0);5455_LIBCPP_HIDE_FROM_ABI double mean() const { return __mean_; }5657friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {58return __x.__mean_ == __y.__mean_;59}60friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }6162friend class poisson_distribution;63};6465private:66param_type __p_;6768public:69// constructors and reset functions70#ifndef _LIBCPP_CXX03_LANG71_LIBCPP_HIDE_FROM_ABI poisson_distribution() : poisson_distribution(1.0) {}72_LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean) : __p_(__mean) {}73#else74_LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}75#endif76_LIBCPP_HIDE_FROM_ABI explicit poisson_distribution(const param_type& __p) : __p_(__p) {}77_LIBCPP_HIDE_FROM_ABI void reset() {}7879// generating functions80template <class _URNG>81_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {82return (*this)(__g, __p_);83}84template <class _URNG>85_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);8687// property functions88_LIBCPP_HIDE_FROM_ABI double mean() const { return __p_.mean(); }8990_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }91_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }9293_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }94_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }9596friend _LIBCPP_HIDE_FROM_ABI bool operator==(const poisson_distribution& __x, const poisson_distribution& __y) {97return __x.__p_ == __y.__p_;98}99friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const poisson_distribution& __x, const poisson_distribution& __y) {100return !(__x == __y);101}102};103104template <class _IntType>105poisson_distribution<_IntType>::param_type::param_type(double __mean)106// According to the standard `inf` is a valid input, but it causes the107// distribution to hang, so we replace it with the maximum representable108// mean.109: __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) {110if (__mean_ < 10) {111__s_ = 0;112__d_ = 0;113__l_ = std::exp(-__mean_);114__omega_ = 0;115__c3_ = 0;116__c2_ = 0;117__c1_ = 0;118__c0_ = 0;119__c_ = 0;120} else {121__s_ = std::sqrt(__mean_);122__d_ = 6 * __mean_ * __mean_;123__l_ = std::trunc(__mean_ - 1.1484);124__omega_ = .3989423 / __s_;125double __b1 = .4166667E-1 / __mean_;126double __b2 = .3 * __b1 * __b1;127__c3_ = .1428571 * __b1 * __b2;128__c2_ = __b2 - 15. * __c3_;129__c1_ = __b1 - 6. * __b2 + 45. * __c3_;130__c0_ = 1. - __b1 + 3. * __b2 - 15. * __c3_;131__c_ = .1069 / __mean_;132}133}134135template <class _IntType>136template <class _URNG>137_IntType poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {138static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");139double __tx;140uniform_real_distribution<double> __urd;141if (__pr.__mean_ < 10) {142__tx = 0;143for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)144__p *= __urd(__urng);145} else {146double __difmuk;147double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);148double __u;149if (__g > 0) {150__tx = std::trunc(__g);151if (__tx >= __pr.__l_)152return std::__clamp_to_integral<result_type>(__tx);153__difmuk = __pr.__mean_ - __tx;154__u = __urd(__urng);155if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)156return std::__clamp_to_integral<result_type>(__tx);157}158exponential_distribution<double> __edist;159for (bool __using_exp_dist = false; true; __using_exp_dist = true) {160double __e;161if (__using_exp_dist || __g <= 0) {162double __t;163do {164__e = __edist(__urng);165__u = __urd(__urng);166__u += __u - 1;167__t = 1.8 + (__u < 0 ? -__e : __e);168} while (__t <= -.6744);169__tx = std::trunc(__pr.__mean_ + __pr.__s_ * __t);170__difmuk = __pr.__mean_ - __tx;171__using_exp_dist = true;172}173double __px;174double __py;175if (__tx < 10 && __tx >= 0) {176const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};177__px = -__pr.__mean_;178__py = std::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];179} else {180double __del = .8333333E-1 / __tx;181__del -= 4.8 * __del * __del * __del;182double __v = __difmuk / __tx;183if (std::abs(__v) > 0.25)184__px = __tx * std::log(1 + __v) - __difmuk - __del;185else186__px = __tx * __v * __v *187(((((((.1250060 * __v + -.1384794) * __v + .1421878) * __v + -.1661269) * __v + .2000118) * __v +188-.2500068) *189__v +190.3333333) *191__v +192-.5) -193__del;194__py = .3989423 / std::sqrt(__tx);195}196double __r = (0.5 - __difmuk) / __pr.__s_;197double __r2 = __r * __r;198double __fx = -0.5 * __r2;199double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * __r2 + __pr.__c1_) * __r2 + __pr.__c0_);200if (__using_exp_dist) {201if (__pr.__c_ * std::abs(__u) <= __py * std::exp(__px + __e) - __fy * std::exp(__fx + __e))202break;203} else {204if (__fy - __u * __fy <= __py * std::exp(__px - __fx))205break;206}207}208}209return std::__clamp_to_integral<result_type>(__tx);210}211212template <class _CharT, class _Traits, class _IntType>213_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&214operator<<(basic_ostream<_CharT, _Traits>& __os, const poisson_distribution<_IntType>& __x) {215__save_flags<_CharT, _Traits> __lx(__os);216typedef basic_ostream<_CharT, _Traits> _OStream;217__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);218return __os << __x.mean();219}220221template <class _CharT, class _Traits, class _IntType>222_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&223operator>>(basic_istream<_CharT, _Traits>& __is, poisson_distribution<_IntType>& __x) {224typedef poisson_distribution<_IntType> _Eng;225typedef typename _Eng::param_type param_type;226__save_flags<_CharT, _Traits> __lx(__is);227typedef basic_istream<_CharT, _Traits> _Istream;228__is.flags(_Istream::dec | _Istream::skipws);229double __mean;230__is >> __mean;231if (!__is.fail())232__x.param(param_type(__mean));233return __is;234}235236_LIBCPP_END_NAMESPACE_STD237238_LIBCPP_POP_MACROS239240#endif // _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H241242243