From 13d7b546c19e474d88425bbdcb671b0615d044e8 Mon Sep 17 00:00:00 2001 From: drepper Date: Tue, 4 Sep 2012 22:57:09 +0000 Subject: [PATCH] * libstdc++-v3/include/ext/random: Add __gnu_cxx::beta_distribution<> class. * libstdc++-v3/include/ext/random.tcc: Add out-of-line functions for __gnu_cxx::beta_distribution<>. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ operators/equal.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ operators/serialize.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ operators/inequal.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ cons/parms.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ cons/default.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ requirements/typedefs.cc: New file. * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ requirements/explicit_instantiation/1.cc: New file. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@190954 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 21 ++ libstdc++-v3/include/ext/random | 217 +++++++++++++++++++++ libstdc++-v3/include/ext/random.tcc | 100 ++++++++++ .../random/beta_distribution/cons/default.cc | 43 ++++ .../random/beta_distribution/cons/parms.cc | 43 ++++ .../random/beta_distribution/operators/equal.cc | 42 ++++ .../random/beta_distribution/operators/inequal.cc | 42 ++++ .../beta_distribution/operators/serialize.cc | 44 +++++ .../requirements/explicit_instantiation/1.cc | 26 +++ .../beta_distribution/requirements/typedefs.cc | 34 ++++ 10 files changed, 612 insertions(+) create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/default.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/parms.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/equal.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/inequal.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/serialize.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/explicit_instantiation/1.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/typedefs.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 74391d6b1d2..dd5c526e71c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,24 @@ +2012-09-04 Ulrich Drepper + + * libstdc++-v3/include/ext/random: Add __gnu_cxx::beta_distribution<> + class. + * libstdc++-v3/include/ext/random.tcc: Add out-of-line functions for + __gnu_cxx::beta_distribution<>. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + operators/equal.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + operators/serialize.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + operators/inequal.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + cons/parms.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + cons/default.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + requirements/typedefs.cc: New file. + * libstdc++-v3/testsuite/26_numerics/random/beta_distribution/ + requirements/explicit_instantiation/1.cc: New file. + 2012-09-04 Steven Bosscher PR bootstrap/54453 diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index 05cbc8fa493..9563e6a0500 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -374,6 +374,223 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 0x3bd2b64bU, 0x0c64b1e4U> sfmt216091_64; + + /** + * @brief A beta continuous distribution for random numbers. + * + * The formula for the beta probability density function is: + * @f[ + * p(x|\alpha,\beta) = \frac{1}{B(\alpha,\beta)} + * x^{\alpha - 1} (1 - x)^{\beta - 1} + * @f] + */ + template + class beta_distribution + { + static_assert(std::is_floating_point<_RealType>::value, + "template argument not a floating point type"); + + public: + /** The type of the range of the distribution. */ + typedef _RealType result_type; + /** Parameter type. */ + struct param_type + { + typedef beta_distribution<_RealType> distribution_type; + friend class beta_distribution<_RealType>; + + explicit + param_type(_RealType __alpha_val = _RealType(1), + _RealType __beta_val = _RealType(1)) + : _M_alpha(__alpha_val), _M_beta(__beta_val) + { + _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0)); + _GLIBCXX_DEBUG_ASSERT(_M_beta > _RealType(0)); + } + + _RealType + alpha() const + { return _M_alpha; } + + _RealType + beta() const + { return _M_beta; } + + friend bool + operator==(const param_type& __p1, const param_type& __p2) + { return (__p1._M_alpha == __p2._M_alpha + && __p1._M_beta == __p2._M_beta); } + + private: + void + _M_initialize(); + + _RealType _M_alpha; + _RealType _M_beta; + }; + + public: + /** + * @brief Constructs a beta distribution with parameters + * @f$\alpha@f$ and @f$\beta@f$. + */ + explicit + beta_distribution(_RealType __alpha_val = _RealType(1), + _RealType __beta_val = _RealType(1)) + : _M_param(__alpha_val, __beta_val) + { } + + explicit + beta_distribution(const param_type& __p) + : _M_param(__p) + { } + + /** + * @brief Resets the distribution state. + */ + void + reset() + { } + + /** + * @brief Returns the @f$\alpha@f$ of the distribution. + */ + _RealType + alpha() const + { return _M_param.alpha(); } + + /** + * @brief Returns the @f$\beta@f$ of the distribution. + */ + _RealType + beta() const + { return _M_param.beta(); } + + /** + * @brief Returns the parameter set of the distribution. + */ + param_type + param() const + { return _M_param; } + + /** + * @brief Sets the parameter set of the distribution. + * @param __param The new parameter set of the distribution. + */ + void + param(const param_type& __param) + { _M_param = __param; } + + /** + * @brief Returns the greatest lower bound value of the distribution. + */ + result_type + min() const + { return result_type(0); } + + /** + * @brief Returns the least upper bound value of the distribution. + */ + result_type + max() const + { return result_type(1); } + + /** + * @brief Generating functions. + */ + template + result_type + operator()(_UniformRandomNumberGenerator& __urng) + { return this->operator()(__urng, this->param()); } + + template + result_type + operator()(_UniformRandomNumberGenerator& __urng, + const param_type& __p); + + template + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng) + { this->__generate(__f, __t, __urng, this->param()); } + + template + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { this->__generate_impl(__f, __t, __urng, __p); } + + template + void + __generate(result_type* __f, result_type* __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { this->__generate_impl(__f, __t, __urng, __p); } + + /** + * @brief Return true if two beta distributions have the same + * parameters and the sequences that would be generated + * are equal. + */ + friend bool + operator==(const beta_distribution& __d1, + const beta_distribution& __d2) + { return __d1.param() == __d2.param(); } + + /** + * @brief Inserts a %beta_distribution random number distribution + * @p __x into the output stream @p __os. + * + * @param __os An output stream. + * @param __x A %beta_distribution random number distribution. + * + * @returns The output stream with the state of @p __x inserted or in + * an error state. + */ + template + friend std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const __gnu_cxx::beta_distribution<_RealType1>& __x); + + /** + * @brief Extracts a %beta_distribution random number distribution + * @p __x from the input stream @p __is. + * + * @param __is An input stream. + * @param __x A %beta_distribution random number generator engine. + * + * @returns The input stream with @p __x extracted or in an error state. + */ + template + friend std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>& __is, + __gnu_cxx::beta_distribution<_RealType1>& __x); + + private: + template + void + __generate_impl(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p); + + param_type _M_param; + }; + + /** + * @brief Return true if two beta distributions are different. + */ + template + inline bool + operator!=(const __gnu_cxx::beta_distribution<_RealType>& __d1, + const __gnu_cxx::beta_distribution<_RealType>& __d2) + { return !(__d1 == __d2); } + + + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/ext/random.tcc b/libstdc++-v3/include/ext/random.tcc index 2a6fde0208f..1776b0df163 100644 --- a/libstdc++-v3/include/ext/random.tcc +++ b/libstdc++-v3/include/ext/random.tcc @@ -438,6 +438,106 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __is; } + + /** + * Iteration method due to M.D. Jhnk. + * + * M.D. Jhnk, Erzeugung von betaverteilten und gammaverteilten + * Zufallszahlen, Metrika, Volume 8, 1964 + */ + template + template + typename beta_distribution<_RealType>::result_type + beta_distribution<_RealType>:: + operator()(_UniformRandomNumberGenerator& __urng, + const param_type& __param) + { + std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __aurng(__urng); + + result_type __x, __y; + do + { + __x = std::exp(std::log(__aurng()) / __param.alpha()); + __y = std::exp(std::log(__aurng()) / __param.beta()); + } + while (__x + __y > result_type(1)); + + return __x / (__x + __y); + } + + template + template + void + beta_distribution<_RealType>:: + __generate_impl(_OutputIterator __f, _OutputIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __param) + { + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>) + + std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __aurng(__urng); + + while (__f != __t) + { + result_type __x, __y; + do + { + __x = std::exp(std::log(__aurng()) / __param.alpha()); + __y = std::exp(std::log(__aurng()) / __param.beta()); + } + while (__x + __y > result_type(1)); + + *__f++ = __x / (__x + __y); + } + } + + template + std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const __gnu_cxx::beta_distribution<_RealType>& __x) + { + typedef std::basic_ostream<_CharT, _Traits> __ostream_type; + typedef typename __ostream_type::ios_base __ios_base; + + const typename __ios_base::fmtflags __flags = __os.flags(); + const _CharT __fill = __os.fill(); + const std::streamsize __precision = __os.precision(); + const _CharT __space = __os.widen(' '); + __os.flags(__ios_base::scientific | __ios_base::left); + __os.fill(__space); + __os.precision(std::numeric_limits<_RealType>::max_digits10); + + __os << __x.alpha() << __space << __x.beta(); + + __os.flags(__flags); + __os.fill(__fill); + __os.precision(__precision); + return __os; + } + + template + std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>& __is, + __gnu_cxx::beta_distribution<_RealType>& __x) + { + typedef std::basic_istream<_CharT, _Traits> __istream_type; + typedef typename __istream_type::ios_base __ios_base; + + const typename __ios_base::fmtflags __flags = __is.flags(); + __is.flags(__ios_base::dec | __ios_base::skipws); + + _RealType __alpha_val, __beta_val; + __is >> __alpha_val >> __beta_val; + __x.param(typename __gnu_cxx::beta_distribution<_RealType>:: + param_type(__alpha_val, __beta_val)); + + __is.flags(__flags); + return __is; + } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/default.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/default.cc new file mode 100644 index 00000000000..3aa2d851e98 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/default.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::beta_distribution<> u; + VERIFY( u.alpha() == 1.0 ); + VERIFY( u.beta() == 1.0 ); + VERIFY( u.min() == 0.0 ); + VERIFY( u.max() == 1.0 ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/parms.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/parms.cc new file mode 100644 index 00000000000..9d6c0b18da7 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/cons/parms.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::beta_distribution<> u(1.5, 3.0); + VERIFY( u.alpha() == 1.5 ); + VERIFY( u.beta() == 3.0 ); + VERIFY( u.min() == 0.0 ); + VERIFY( u.max() == 1.0 ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/equal.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/equal.cc new file mode 100644 index 00000000000..a31a6545d53 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/equal.cc @@ -0,0 +1,42 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2010-03-16 Paolo Carlini +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::beta_distribution u(1.5, 3.0), v, w; + + VERIFY( v == w ); + VERIFY( !(u == v) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/inequal.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/inequal.cc new file mode 100644 index 00000000000..d7eda3935bf --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/inequal.cc @@ -0,0 +1,42 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2010-03-16 Paolo Carlini +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::beta_distribution u(1.5, 3.0), v, w; + + VERIFY( u != v ); + VERIFY( !(v != w) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/serialize.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/serialize.cc new file mode 100644 index 00000000000..dd2fed8d590 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/operators/serialize.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2009-08-14 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void +test01() +{ + std::stringstream str; + __gnu_cxx::beta_distribution u(1.5, 3.0), v; + std::minstd_rand0 rng; + + u(rng); // advance + str << u; + + str >> v; +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/explicit_instantiation/1.cc new file mode 100644 index 00000000000..a572b1478ae --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/explicit_instantiation/1.cc @@ -0,0 +1,26 @@ +// { dg-do compile } +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// Copyright (C) 2012 Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +template class __gnu_cxx::beta_distribution; +template class __gnu_cxx::beta_distribution; +template class __gnu_cxx::beta_distribution; diff --git a/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/typedefs.cc b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/typedefs.cc new file mode 100644 index 00000000000..33b18ae535c --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/beta_distribution/requirements/typedefs.cc @@ -0,0 +1,34 @@ +// { dg-do compile } +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// 2012-09-04 Ulrich Drepper +// +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +void +test01() +{ + typedef __gnu_cxx::beta_distribution test_type; + + typedef test_type::result_type result_type; + typedef test_type::param_type param_type; +} -- 2.11.4.GIT