diff options
9 files changed, 618 insertions, 15 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0a405375e65..33489e2cc9a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,21 @@ +2012-09-14 Edward Smith-Rowland <3dw4rd@verizon.net> + + * include/ext/random: Add __gnu_cxx::rice_distribution<> class. + * include/ext/random.tcc: Add out-of-line functions for + __gnu_cxx::rice_distribution<>. + * testsuite/26_numerics/random/rice_distribution/ + operators/equal.cc: New file. + * testsuite/26_numerics/random/rice_distribution/ + operators/serialize.cc: New file. + * testsuite/26_numerics/random/rice_distribution/ + operators/inequal.cc: New file. + * testsuite/26_numerics/random/rice_distribution/ + cons/parms.cc: New file. + * testsuite/26_numerics/random/rice_distribution/ + cons/default.cc: New file. + * testsuite/26_numerics/random/rice_distribution/ + requirements/typedefs.cc: New file. + 2012-09-14 David Edelsohn <dje.gcc@gmail.com> * configure: Regenerated. diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index 6bb438a8558..3011f39109a 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -532,16 +532,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { 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. * @@ -582,13 +572,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; /** + * @brief Return true if two beta distributions have the same + * parameters and the sequences that would be generated + * are equal. + */ + template<typename _RealType> + inline bool + operator==(const __gnu_cxx::beta_distribution<_RealType>& __d1, + const __gnu_cxx::beta_distribution<_RealType>& __d2) + { return __d1.param() == __d2.param(); } + + /** * @brief Return true if two beta distributions are different. */ - template<typename _RealType> - inline bool - operator!=(const __gnu_cxx::beta_distribution<_RealType>& __d1, - const __gnu_cxx::beta_distribution<_RealType>& __d2) - { return !(__d1 == __d2); } + template<typename _RealType> + inline bool + operator!=(const __gnu_cxx::beta_distribution<_RealType>& __d1, + const __gnu_cxx::beta_distribution<_RealType>& __d2) + { return !(__d1 == __d2); } /** @@ -896,6 +897,249 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return !(__d1 == __d2); } + /** + * @brief A Rice continuous distribution for random numbers. + * + * The formula for the Rice probability density function is + * @f[ + * p(x|\nu,\sigma) = \frac{x}{\sigma^2} + * \exp\left(-\frac{x^2+\nu^2}{2\sigma^2}\right) + * I_0\left(\frac{x \nu}{\sigma^2}\right) + * @f] + * where @f$I_0(z)@f$ is the modified Bessel function of the first kind + * of order 0 and @f$\nu >= 0@f$ and @f$\sigma > 0@f$. + * + * <table border=1 cellpadding=10 cellspacing=0> + * <caption align=top>Distribution Statistics</caption> + * <tr><td>Mean</td><td>@f$\sqrt{\pi/2}L_{1/2}(-\nu^2/2\sigma^2)@f$</td></tr> + * <tr><td>Variance</td><td>@f$2\sigma^2 + \nu^2 + * + (\pi\sigma^2/2)L^2_{1/2}(-\nu^2/2\sigma^2)@f$</td></tr> + * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr> + * </table> + * where @f$L_{1/2}(x)@f$ is the Laguerre polynomial of order 1/2. + */ + template<typename _RealType = double> + class + rice_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 rice_distribution<result_type> distribution_type; + + param_type(result_type __nu = result_type(0), + result_type __sigma = result_type(1)) + : _M_nu(__nu), _M_sigma(__sigma) + { + _GLIBCXX_DEBUG_ASSERT(_M_nu >= result_type(0)); + _GLIBCXX_DEBUG_ASSERT(_M_sigma > result_type(0)); + } + + result_type + nu() const + { return _M_nu; } + + result_type + sigma() const + { return _M_sigma; } + + friend bool + operator==(const param_type& __p1, const param_type& __p2) + { return __p1._M_nu == __p2._M_nu + && __p1._M_sigma == __p2._M_sigma; } + + private: + void _M_initialize(); + + result_type _M_nu; + result_type _M_sigma; + }; + + /** + * @brief Constructors. + */ + explicit + rice_distribution(result_type __nu = result_type(0), + result_type __sigma = result_type(1)) + : _M_param(__nu, __sigma), + _M_ndx(__nu, __sigma), + _M_ndy(result_type(0), __sigma) + { } + + explicit + rice_distribution(const param_type& __p) + : _M_param(__p), + _M_ndx(__p.nu(), __p.sigma()), + _M_ndy(result_type(0), __p.sigma()) + { } + + /** + * @brief Resets the distribution state. + */ + void + reset() + { + _M_ndx.reset(); + _M_ndy.reset(); + } + + /** + * @brief Return the parameters of the distribution. + */ + result_type + nu() const + { return _M_param.nu(); } + + result_type + sigma() const + { return _M_param.sigma(); } + + /** + * @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<result_type>::max(); } + + /** + * @brief Generating functions. + */ + template<typename _UniformRandomNumberGenerator> + result_type + operator()(_UniformRandomNumberGenerator& __urng) + { + result_type __x = this->_M_ndx(__urng); + result_type __y = this->_M_ndy(__urng); + return std::hypot(__x, __y); + } + + template<typename _UniformRandomNumberGenerator> + result_type + operator()(_UniformRandomNumberGenerator& __urng, + const param_type& __p) + { + typename std::normal_distribution<result_type>::param_type + __px(__p.nu(), __p.sigma()), __py(result_type(0), __p.sigma()); + result_type __x = this->_M_ndx(__px, __urng); + result_type __y = this->_M_ndy(__py, __urng); + return std::hypot(__x, __y); + } + + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng) + { this->__generate(__f, __t, __urng, this->param()); } + + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { this->__generate_impl(__f, __t, __urng, __p); } + + template<typename _UniformRandomNumberGenerator> + 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 Rice distributions have + * the same parameters and the sequences that would + * be generated are equal. + */ + template<typename _RealType1> + friend bool + operator==(const rice_distribution<_RealType1>& __d1, + const rice_distribution<_RealType1>& __d2) + { return (__d1.param() == __d2.param() + && __d1._M_ndx == __d2._M_ndx + && __d1._M_ndy == __d2._M_ndy); } + + /** + * @brief Inserts a %rice_distribution random number distribution + * @p __x into the output stream @p __os. + * + * @param __os An output stream. + * @param __x A %rice_distribution random number distribution. + * + * @returns The output stream with the state of @p __x inserted or in + * an error state. + */ + template<typename _RealType1, typename _CharT, typename _Traits> + friend std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>&, + const rice_distribution<_RealType1>&); + + /** + * @brief Extracts a %rice_distribution random number distribution + * @p __x from the input stream @p __is. + * + * @param __is An input stream. + * @param __x A %rice_distribution random number + * generator engine. + * + * @returns The input stream with @p __x extracted or in an error state. + */ + template<typename _RealType1, typename _CharT, typename _Traits> + friend std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>&, + rice_distribution<_RealType1>&); + + private: + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate_impl(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p); + + param_type _M_param; + + std::normal_distribution<result_type> _M_ndx; + std::normal_distribution<result_type> _M_ndy; + }; + + /** + * @brief Return true if two Rice distributions are not equal. + */ + template<typename _RealType1> + inline bool + operator!=(const rice_distribution<_RealType1>& __d1, + const rice_distribution<_RealType1>& __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 988dc222829..6aa049a4f87 100644 --- a/libstdc++-v3/include/ext/random.tcc +++ b/libstdc++-v3/include/ext/random.tcc @@ -751,6 +751,78 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } + template<typename _RealType> + template<typename _OutputIterator, + typename _UniformRandomNumberGenerator> + void + rice_distribution<_RealType>:: + __generate_impl(_OutputIterator __f, _OutputIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>) + + std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __aurng(__urng); + + while (__f != __t) + { + typename std::normal_distribution<result_type>::param_type + __px(__p.nu(), __p.sigma()), __py(result_type(0), __p.sigma()); + result_type __x = this->_M_ndx(__px, __urng); + result_type __y = this->_M_ndy(__py, __urng); + *__f++ = std::hypot(__x, __y); + } + } + + template<typename _RealType, typename _CharT, typename _Traits> + std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const rice_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.nu() << __space << __x.sigma(); + __os << __space << __x._M_ndx; + __os << __space << __x._M_ndy; + + __os.flags(__flags); + __os.fill(__fill); + __os.precision(__precision); + return __os; + } + + template<typename _RealType, typename _CharT, typename _Traits> + std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>& __is, + rice_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 __nu, __sigma; + __is >> __nu >> __sigma; + __is >> __x._M_ndx; + __is >> __x._M_ndy; + __x.param(typename rice_distribution<_RealType>:: + param_type(__nu, __sigma)); + + __is.flags(__flags); + return __is; + } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/default.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/default.cc new file mode 100644 index 00000000000..3a49140fcb9 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/default.cc @@ -0,0 +1,47 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.4.8.3.* Class template rice_distribution [rand.dist.ext.rice] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include <ext/random> +#include <testsuite_hooks.h> + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::rice_distribution<> u; + VERIFY( u.nu() == 0.0 ); + VERIFY( u.sigma() == 1.0 ); + VERIFY( u.min() == 0.0 ); + typedef __gnu_cxx::rice_distribution<>::result_type result_type; + VERIFY( u.max() == std::numeric_limits<result_type>::max() ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/parms.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/parms.cc new file mode 100644 index 00000000000..fa79ef521ae --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/cons/parms.cc @@ -0,0 +1,47 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.4.8.3.* Class template rice_distribution [rand.dist.ext.rice] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include <ext/random> +#include <testsuite_hooks.h> + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::rice_distribution<> u(1.5, 3.0); + VERIFY( u.nu() == 1.5 ); + VERIFY( u.sigma() == 3.0 ); + VERIFY( u.min() == 0.0 ); + typedef __gnu_cxx::rice_distribution<>::result_type result_type; + VERIFY( u.max() == std::numeric_limits<result_type>::max() ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/equal.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/equal.cc new file mode 100644 index 00000000000..2539d6ed42b --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/equal.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.5.8.4.5 Class template rice_distribution [rand.dist.ext.rice] + +#include <ext/random> +#include <testsuite_hooks.h> + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::rice_distribution<double> u(2.0, 3.0), v, w; + + VERIFY( v == w ); + VERIFY( !(u == v) ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/inequal.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/inequal.cc new file mode 100644 index 00000000000..a3f15124af9 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/inequal.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.5.8.4.5 Class template rice_distribution [rand.dist.ext.rice] + +#include <ext/random> +#include <testsuite_hooks.h> + +void +test01() +{ + bool test __attribute__((unused)) = true; + + __gnu_cxx::rice_distribution<double> u(2.0, 3.0), v, w; + + VERIFY( u != v ); + VERIFY( !(v != w) ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/serialize.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/serialize.cc new file mode 100644 index 00000000000..9d39c3098e5 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/operators/serialize.cc @@ -0,0 +1,51 @@ +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.4.8.3.* Class template rice_distribution [rand.dist.ext.rice] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include <ext/random> +#include <sstream> +#include <testsuite_hooks.h> + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::stringstream str; + __gnu_cxx::rice_distribution<double> u(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/26_numerics/random/rice_distribution/requirements/typedefs.cc b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/requirements/typedefs.cc new file mode 100644 index 00000000000..b524a7b5aa3 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/rice_distribution/requirements/typedefs.cc @@ -0,0 +1,36 @@ +// { dg-do compile } +// { dg-options "-std=c++0x" } +// { dg-require-cstdint "" } +// +// 2012-01-28 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 +// <http://www.gnu.org/licenses/>. + +// 26.4.8.3.* Class template rice_distribution [rand.dist.ext.rice] +// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] + +#include <ext/random> + +void +test01() +{ + typedef __gnu_cxx::rice_distribution<double> test_type; + + typedef test_type::result_type result_type; + typedef test_type::param_type param_type; +} |