diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 98d35510d31..42576bf29b4 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2012-09-24 Edward Smith-Rowland <3dw4rd@verizon.net> + + * include/ext/random: Add __gnu_cxx::k_distribution<> class. + * include/ext/random.tcc: Add out-of-line functions for + __gnu_cxx::k_distribution<>. + * testsuite/ext/random/k_distribution/operators/equal.cc: New file. + * testsuite/ext/random/k_distribution/operators/serialize.cc: New file. + * testsuite/ext/random/k_distribution/operators/inequal.cc: New file. + * testsuite/ext/random/k_distribution/cons/parms.cc: New file. + * testsuite/ext/random/k_distribution/cons/default.cc: New file. + * testsuite/ext/random/k_distribution/requirements/typedefs.cc: + New file. + * testsuite/ext/random/k_distribution/requirements/ + explicit_instantiation/1.cc: New file. + 2012-09-24 François Dumont PR libstdc++/44436 diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index 97bd01467d5..c7321a99623 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -1596,6 +1596,250 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const pareto_distribution<_RealType>& __d2) { return !(__d1 == __d2); } + + /** + * @brief A K continuous distribution for random numbers. + * + * The formula for the K probability density function is + * @f[ + * p(x|\lambda, \mu, \nu) = \frac{2}{x} + * \left(\frac{\lambda\nu x}{\mu}\right)^{\frac{\lambda + \nu}{2}} + * \frac{1}{\Gamma(\lambda)\Gamma(\nu)} + * K_{\nu - \lambda}\left(2\sqrt{\frac{\lambda\nu x}{\mu}}\right) + * @f] + * where @f$I_0(z)@f$ is the modified Bessel function of the second kind + * of order @f$\nu - \lambda@f$ and @f$\lambda > 0@f$, @f$\mu > 0@f$ + * and @f$\nu > 0@f$. + * + * + * + * + * + * + *
Distribution Statistics
Mean@f$\mu@f$
Variance@f$\mu^2\frac{\lambda + \nu + 1}{\lambda\nu}@f$
Range@f$[0, \infty)@f$
+ */ + template + class + k_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 k_distribution distribution_type; + + param_type(result_type __lambda_val = result_type(1), + result_type __mu_val = result_type(1), + result_type __nu_val = result_type(1)) + : _M_lambda(__lambda_val), _M_mu(__mu_val), _M_nu(__nu_val) + { + _GLIBCXX_DEBUG_ASSERT(_M_lambda > result_type(0)); + _GLIBCXX_DEBUG_ASSERT(_M_mu > result_type(0)); + _GLIBCXX_DEBUG_ASSERT(_M_nu > result_type(0)); + } + + result_type + lambda() const + { return _M_lambda; } + + result_type + mu() const + { return _M_mu; } + + result_type + nu() const + { return _M_nu; } + + friend bool + operator==(const param_type& __p1, const param_type& __p2) + { return __p1._M_lambda == __p2._M_lambda + && __p1._M_mu == __p2._M_mu + && __p1._M_nu == __p2._M_nu; } + + private: + void _M_initialize(); + + result_type _M_lambda; + result_type _M_mu; + result_type _M_nu; + }; + + /** + * @brief Constructors. + */ + explicit + k_distribution(result_type __lambda_val = result_type(1), + result_type __mu_val = result_type(1), + result_type __nu_val = result_type(1)) + : _M_param(__lambda_val, __mu_val, __nu_val), + _M_gd1(__lambda_val, result_type(1) / __lambda_val), + _M_gd2(__nu_val, __mu_val / __nu_val) + { } + + explicit + k_distribution(const param_type& __p) + : _M_param(__p), + _M_gd1(__p.lambda(), result_type(1) / __p.lambda()), + _M_gd2(__p.nu(), __p.mu() / __p.nu()) + { } + + /** + * @brief Resets the distribution state. + */ + void + reset() + { + _M_gd1.reset(); + _M_gd2.reset(); + } + + /** + * @brief Return the parameters of the distribution. + */ + result_type + lambda() const + { return _M_param.lambda(); } + + result_type + mu() const + { return _M_param.mu(); } + + result_type + nu() const + { return _M_param.nu(); } + + /** + * @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 std::numeric_limits::max(); } + + /** + * @brief Generating functions. + */ + template + result_type + operator()(_UniformRandomNumberGenerator&); + + template + result_type + operator()(_UniformRandomNumberGenerator&, const param_type&); + + 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 K distributions have + * the same parameters and the sequences that would + * be generated are equal. + */ + friend bool + operator==(const k_distribution& __d1, + const k_distribution& __d2) + { return (__d1.param() == __d2.param() + && __d1._M_gd1 == __d2._M_gd1 + && __d1._M_gd2 == __d2._M_gd2); } + + /** + * @brief Inserts a %k_distribution random number distribution + * @p __x into the output stream @p __os. + * + * @param __os An output stream. + * @param __x A %k_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>&, + const k_distribution<_RealType1>&); + + /** + * @brief Extracts a %k_distribution random number distribution + * @p __x from the input stream @p __is. + * + * @param __is An input stream. + * @param __x A %k_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>&, + k_distribution<_RealType1>&); + + private: + template + void + __generate_impl(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p); + + param_type _M_param; + + std::gamma_distribution _M_gd1; + std::gamma_distribution _M_gd2; + }; + + /** + * @brief Return true if two K distributions are not equal. + */ + template + inline bool + operator!=(const k_distribution<_RealType>& __d1, + const k_distribution<_RealType>& __d2) + { return !(__d1 == __d2); } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace __gnu_cxx diff --git a/libstdc++-v3/include/ext/random.tcc b/libstdc++-v3/include/ext/random.tcc index 137b6588dc9..86bb67fedf9 100644 --- a/libstdc++-v3/include/ext/random.tcc +++ b/libstdc++-v3/include/ext/random.tcc @@ -912,6 +912,103 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __is; } + + template + template + typename k_distribution<_RealType>::result_type + k_distribution<_RealType>:: + operator()(_UniformRandomNumberGenerator& __urng) + { + result_type __x = this->_M_gd1(__urng); + result_type __y = this->_M_gd2(__urng); + return std::sqrt(__x * __y); + } + + template + template + typename k_distribution<_RealType>::result_type + k_distribution<_RealType>:: + operator()(_UniformRandomNumberGenerator& __urng, + const param_type& __p) + { + typename std::gamma_distribution::param_type + __p1(__p.lambda(), result_type(1) / __p.lambda()), + __p2(__p.nu(), __p.mu() / __p.nu()); + result_type __x = this->_M_gd1(__p1, __urng); + result_type __y = this->_M_gd2(__p2, __urng); + return std::sqrt(__x * __y); + } + + template + template + void + k_distribution<_RealType>:: + __generate_impl(_OutputIterator __f, _OutputIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>) + + typename std::gamma_distribution::param_type + __p1(__p.lambda(), result_type(1) / __p.lambda()), + __p2(__p.nu(), __p.mu() / __p.nu()); + while (__f != __t) + { + result_type __x = this->_M_gd1(__p1, __urng); + result_type __y = this->_M_gd2(__p2, __urng); + *__f++ = std::sqrt(__x * __y); + } + } + + template + std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const k_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.lambda() << __space << __x.mu() << __space << __x.nu(); + __os << __space << __x._M_gd1; + __os << __space << __x._M_gd2; + + __os.flags(__flags); + __os.fill(__fill); + __os.precision(__precision); + return __os; + } + + template + std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>& __is, + k_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 __lambda_val, __mu_val, __nu_val; + __is >> __lambda_val >> __mu_val >> __nu_val; + __is >> __x._M_gd1; + __is >> __x._M_gd2; + __x.param(typename k_distribution<_RealType>:: + param_type(__lambda_val, __mu_val, __nu_val)); + + __is.flags(__flags); + return __is; + } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/cons/default.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/cons/default.cc new file mode 100644 index 00000000000..3a7ade894d9 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/cons/default.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.4.8.3.* Class template k_distribution [rand.dist.ext.k] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::k_distribution<> u; + VERIFY( u.lambda() == 1.0 ); + VERIFY( u.mu() == 1.0 ); + VERIFY( u.nu() == 1.0 ); + VERIFY( u.min() == 0.0 ); + typedef __gnu_cxx::k_distribution<>::result_type result_type; + VERIFY( u.max() == std::numeric_limits::max() ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/cons/parms.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/cons/parms.cc new file mode 100644 index 00000000000..396be380fcb --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/cons/parms.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.4.8.3.* Class template k_distribution [rand.dist.ext.k] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::k_distribution<> u(2.0, 1.5, 3.0); + VERIFY( u.lambda() == 2.0 ); + VERIFY( u.mu() == 1.5 ); + VERIFY( u.nu() == 3.0 ); + VERIFY( u.min() == 0.0 ); + typedef __gnu_cxx::k_distribution<>::result_type result_type; + VERIFY( u.max() == std::numeric_limits::max() ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/operators/equal.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/equal.cc new file mode 100644 index 00000000000..51e1081a08b --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/equal.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.5.8.4.5 Class template k_distribution [rand.dist.ext.k] + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::k_distribution u(2.0, 1.5, 3.0), v, w; + + VERIFY( v == w ); + VERIFY( !(u == v) ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/operators/inequal.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/inequal.cc new file mode 100644 index 00000000000..082ed281262 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/inequal.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.5.8.4.5 Class template k_distribution [rand.dist.ext.k] + +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::k_distribution u(2.0, 1.5, 3.0), v, w; + + VERIFY( u != v ); + VERIFY( !(v != w) ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/operators/serialize.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/serialize.cc new file mode 100644 index 00000000000..a7647adadc0 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/operators/serialize.cc @@ -0,0 +1,51 @@ +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.4.8.3.* Class template k_distribution [rand.dist.ext.k] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::stringstream str; + __gnu_cxx::k_distribution u(2.0, 1.5, 3.0), v; + std::minstd_rand0 rng; + + u(rng); // advance + str << u; + + str >> v; + VERIFY( u == v ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/requirements/explicit_instantiation/1.cc new file mode 100644 index 00000000000..539c7748af5 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_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::k_distribution; +template class __gnu_cxx::k_distribution; +template class __gnu_cxx::k_distribution; diff --git a/libstdc++-v3/testsuite/ext/random/k_distribution/requirements/typedefs.cc b/libstdc++-v3/testsuite/ext/random/k_distribution/requirements/typedefs.cc new file mode 100644 index 00000000000..2286fbbddfe --- /dev/null +++ b/libstdc++-v3/testsuite/ext/random/k_distribution/requirements/typedefs.cc @@ -0,0 +1,36 @@ +// { dg-do compile } +// { dg-options "-std=c++11" } +// { dg-require-cstdint "" } +// +// 2012-09-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> +// +// 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 +// . + +// 26.4.8.3.* Class template k_distribution [rand.dist.ext.k] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include + +void +test01() +{ + typedef __gnu_cxx::k_distribution test_type; + + typedef test_type::result_type result_type; + typedef test_type::param_type param_type; +}