Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r68375 - in trunk: boost/random libs/random/test
From: steven_at_[hidden]
Date: 2011-01-22 21:14:27


Author: steven_watanabe
Date: 2011-01-22 21:14:26 EST (Sat, 22 Jan 2011)
New Revision: 68375
URL: http://svn.boost.org/trac/boost/changeset/68375

Log:
Update bernoulli_distribution to match C++0x.
Added:
   trunk/libs/random/test/test_bernoulli.cpp (contents, props changed)
   trunk/libs/random/test/test_bernoulli_distribution.cpp (contents, props changed)
Text files modified:
   trunk/boost/random/bernoulli_distribution.hpp | 229 +++++++++++++++++++++++++++------------
   trunk/libs/random/test/Jamfile.v2 | 2
   2 files changed, 162 insertions(+), 69 deletions(-)

Modified: trunk/boost/random/bernoulli_distribution.hpp
==============================================================================
--- trunk/boost/random/bernoulli_distribution.hpp (original)
+++ trunk/boost/random/bernoulli_distribution.hpp 2011-01-22 21:14:26 EST (Sat, 22 Jan 2011)
@@ -1,6 +1,7 @@
 /* boost random/bernoulli_distribution.hpp header file
  *
  * Copyright Jens Maurer 2000-2001
+ * Copyright Steven Watanabe 2011
  * Distributed under the Boost Software License, Version 1.0. (See
  * accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -17,10 +18,12 @@
 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
 
 #include <cassert>
-#include <iostream>
+#include <iosfwd>
 #include <boost/random/detail/config.hpp>
+#include <boost/random/detail/operators.hpp>
 
 namespace boost {
+namespace random {
 
 /**
  * Instantiations of class template \bernoulli_distribution model a
@@ -32,78 +35,166 @@
 class bernoulli_distribution
 {
 public:
- // In principle, this could work with both integer and floating-point
- // types. Generating floating-point random numbers in the first
- // place is probably more expensive, so use integer as input.
- typedef int input_type;
- typedef bool result_type;
-
- /**
- * Constructs a \bernoulli_distribution object.
- * p is the parameter of the distribution.
- *
- * Requires: 0 <= p <= 1
- */
- explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
- : _p(p_arg)
- {
- assert(_p >= 0);
- assert(_p <= 1);
- }
-
- // compiler-generated copy ctor and assignment operator are fine
-
- /**
- * Returns: The "p" parameter of the distribution.
- */
- RealType p() const { return _p; }
- /**
- * Effects: Subsequent uses of the distribution do not depend
- * on values produced by any engine prior to invoking reset.
- */
- void reset() { }
-
- /**
- * Returns: a random variate distributed according to the
- * \bernoulli_distribution.
- */
- template<class Engine>
- result_type operator()(Engine& eng)
- {
- if(_p == RealType(0))
- return false;
- else
- return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
- }
-
-#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
- /**
- * Writes the parameters of the distribution to a @c std::ostream.
- */
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
- {
- os << bd._p;
- return os;
- }
-
- /**
- * Reads the parameters of the distribution from a @c std::istream.
- */
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
- {
- is >> std::ws >> bd._p;
- return is;
- }
-#endif
+ // In principle, this could work with both integer and floating-point
+ // types. Generating floating-point random numbers in the first
+ // place is probably more expensive, so use integer as input.
+ typedef int input_type;
+ typedef bool result_type;
+
+ class param_type
+ {
+ public:
+
+ typedef bernoulli_distribution distribution_type;
+
+ /**
+ * Constructs the parameters of the distribution.
+ *
+ * Requires: 0 <= p <= 1
+ */
+ explicit param_type(RealType p_arg = RealType(0.5))
+ : _p(p_arg)
+ {
+ assert(_p >= 0);
+ assert(_p <= 1);
+ }
+
+ /** Returns the p parameter of the distribution. */
+ RealType p() const { return _p; }
+
+ /** Writes the parameters to a std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
+ {
+ os << parm._p;
+ return os;
+ }
+
+ /** Reads the parameters from a std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
+ {
+ is >> parm._p;
+ return is;
+ }
+
+ /** Returns true if the two sets of parameters are equal. */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
+ { return lhs._p == rhs._p; }
+
+ /** Returns true if the two sets of parameters are different. */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
+
+ private:
+ RealType _p;
+ };
+
+ /**
+ * Constructs a \bernoulli_distribution object.
+ * p is the parameter of the distribution.
+ *
+ * Requires: 0 <= p <= 1
+ */
+ explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
+ : _p(p_arg)
+ {
+ assert(_p >= 0);
+ assert(_p <= 1);
+ }
+ /**
+ * Constructs \bernoulli_distribution from its parameters
+ */
+ explicit bernoulli_distribution(const param_type& parm)
+ : _p(parm.p()) {}
+
+ // compiler-generated copy ctor and assignment operator are fine
+
+ /**
+ * Returns: The "p" parameter of the distribution.
+ */
+ RealType p() const { return _p; }
+
+ /** Returns the smallest value that the distribution can produce. */
+ bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const
+ { return false; }
+ /** Returns the largest value that the distribution can produce. */
+ bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const
+ { return true; }
+
+ /** Returns the parameters of the distribution. */
+ param_type param() const { return param_type(_p); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm) { _p = parm.p(); }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset() { }
+
+ /**
+ * Returns: a random variate distributed according to the
+ * \bernoulli_distribution.
+ */
+ template<class Engine>
+ bool operator()(Engine& eng) const
+ {
+ if(_p == RealType(0))
+ return false;
+ else
+ return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
+ }
+
+ /**
+ * Returns: a random variate distributed according to the
+ * \bernoulli_distribution with parameters specified by param.
+ */
+ template<class Engine>
+ bool operator()(Engine& eng, const param_type& parm) const
+ {
+ return bernoulli_distribution(parm)(eng);
+ }
+
+ /**
+ * Writes the parameters of the distribution to a @c std::ostream.
+ */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd)
+ {
+ os << bd._p;
+ return os;
+ }
+
+ /**
+ * Reads the parameters of the distribution from a @c std::istream.
+ */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd)
+ {
+ is >> bd._p;
+ return is;
+ }
+
+ /**
+ * Returns true iff the two distributions will produce identical
+ * sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs)
+ { return lhs._p == rhs._p; }
+
+ /**
+ * Returns true iff the two distributions will produce different
+ * sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution)
 
 private:
- RealType _p;
+
+ /// \cond
+ RealType _p;
+ /// \endcond
 };
 
+} // namespace random
+
+using random::bernoulli_distribution;
+
 } // namespace boost
 
 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP

Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2011-01-22 21:14:26 EST (Sat, 22 Jan 2011)
@@ -75,6 +75,8 @@
 run test_piecewise_linear_distribution.cpp /boost//unit_test_framework ;
 run test_exponential.cpp ;
 run test_exponential_distribution.cpp /boost//unit_test_framework ;
+run test_bernoulli.cpp ;
+run test_bernoulli_distribution.cpp /boost//unit_test_framework ;
 
 # run nondet_random_speed.cpp ;
 # run random_device.cpp ;

Added: trunk/libs/random/test/test_bernoulli.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_bernoulli.cpp 2011-01-22 21:14:26 EST (Sat, 22 Jan 2011)
@@ -0,0 +1,108 @@
+/* test_bernoulli.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/bernoulli_distribution.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/math/distributions/binomial.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/exception/diagnostic_information.hpp>
+#include <vector>
+#include <iostream>
+#include <numeric>
+
+#include "chi_squared_test.hpp"
+
+bool do_test(double p, long long max) {
+ std::cout << "running bernoulli(" << p << ")" << " " << max << " times: " << std::flush;
+
+ boost::math::binomial expected(max, p);
+
+ boost::random::bernoulli_distribution<> dist(p);
+ boost::mt19937 gen;
+ long long count = 0;
+ for(long long i = 0; i < max; ++i) {
+ if(dist(gen)) ++count;
+ }
+
+ double prob = cdf(expected, count);
+
+ bool result = prob < 0.99 && prob > 0.01;
+ const char* err = result? "" : "*";
+ std::cout << std::setprecision(17) << prob << err << std::endl;
+
+ std::cout << std::setprecision(6);
+
+ return result;
+}
+
+bool do_tests(int repeat, long long trials) {
+ boost::mt19937 gen;
+ boost::uniform_01<> rdist;
+ int errors = 0;
+ for(int i = 0; i < repeat; ++i) {
+ if(!do_test(rdist(gen), trials)) {
+ ++errors;
+ }
+ }
+ if(errors != 0) {
+ std::cout << "*** " << errors << " errors detected ***" << std::endl;
+ }
+ return errors == 0;
+}
+
+int usage() {
+ std::cerr << "Usage: test_bernoulli_distribution -r <repeat> -t <trials>" << std::endl;
+ return 2;
+}
+
+template<class T>
+bool handle_option(int& argc, char**& argv, char opt, T& value) {
+ if(argv[0][1] == opt && argc > 1) {
+ --argc;
+ ++argv;
+ value = boost::lexical_cast<T>(argv[0]);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+int main(int argc, char** argv) {
+ int repeat = 10;
+ long long trials = 1000000ll;
+
+ if(argc > 0) {
+ --argc;
+ ++argv;
+ }
+ while(argc > 0) {
+ if(argv[0][0] != '-') return usage();
+ else if(!handle_option(argc, argv, 'r', repeat)
+ && !handle_option(argc, argv, 't', trials)) {
+ return usage();
+ }
+ --argc;
+ ++argv;
+ }
+
+ try {
+ if(do_tests(repeat, trials)) {
+ return 0;
+ } else {
+ return EXIT_FAILURE;
+ }
+ } catch(...) {
+ std::cerr << boost::current_exception_diagnostic_information() << std::endl;
+ return EXIT_FAILURE;
+ }
+}

Added: trunk/libs/random/test/test_bernoulli_distribution.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_bernoulli_distribution.cpp 2011-01-22 21:14:26 EST (Sat, 22 Jan 2011)
@@ -0,0 +1,32 @@
+/* test_bernoulli_distribution.cpp
+ *
+ * Copyright Steven Watanabe 2010
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/bernoulli_distribution.hpp>
+
+#define BOOST_RANDOM_DISTRIBUTION boost::random::bernoulli_distribution<>
+#define BOOST_RANDOM_ARG1 p
+#define BOOST_RANDOM_ARG1_DEFAULT 0.5
+#define BOOST_RANDOM_ARG1_VALUE 0.25
+
+#define BOOST_RANDOM_DIST0_MIN false
+#define BOOST_RANDOM_DIST0_MAX true
+#define BOOST_RANDOM_DIST1_MIN false
+#define BOOST_RANDOM_DIST1_MAX true
+
+#define BOOST_RANDOM_TEST1_PARAMS (0.0)
+#define BOOST_RANDOM_TEST1_MIN false
+#define BOOST_RANDOM_TEST1_MAX false
+
+#define BOOST_RANDOM_TEST2_PARAMS (1.0)
+#define BOOST_RANDOM_TEST2_MIN true
+#define BOOST_RANDOM_TEST2_MAX true
+
+#include "test_distribution.ipp"


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk