|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r68727 - in trunk: boost/random boost/random/detail libs/random/test
From: steven_at_[hidden]
Date: 2011-02-08 12:46:50
Author: steven_watanabe
Date: 2011-02-08 12:46:48 EST (Tue, 08 Feb 2011)
New Revision: 68727
URL: http://svn.boost.org/trac/boost/changeset/68727
Log:
Update additive_combine.
Added:
trunk/libs/random/test/test_ecuyer1988.cpp (contents, props changed)
Text files modified:
trunk/boost/random/additive_combine.hpp | 391 ++++++++++++++++++++++-----------------
trunk/boost/random/detail/const_mod.hpp | 2
trunk/libs/random/test/Jamfile.v2 | 2
3 files changed, 224 insertions(+), 171 deletions(-)
Modified: trunk/boost/random/additive_combine.hpp
==============================================================================
--- trunk/boost/random/additive_combine.hpp (original)
+++ trunk/boost/random/additive_combine.hpp 2011-02-08 12:46:48 EST (Tue, 08 Feb 2011)
@@ -16,18 +16,21 @@
#ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP
#define BOOST_RANDOM_ADDITIVE_COMBINE_HPP
-#include <iostream>
+#include <istream>
+#include <iosfwd>
#include <algorithm> // for std::min and std::max
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <boost/random/detail/config.hpp>
+#include <boost/random/detail/operators.hpp>
+#include <boost/random/detail/seed.hpp>
#include <boost/random/linear_congruential.hpp>
namespace boost {
namespace random {
/**
- * An instantiation of class template \additive_combine model a
+ * An instantiation of class template @c additive_combine_engine models a
* \pseudo_random_number_generator. It combines two multiplicative
* \linear_congruential_engine number generators, i.e. those with @c c = 0.
* It is described in
@@ -38,179 +41,223 @@
* @endblockquote
*
* The template parameters MLCG1 and MLCG2 shall denote two different
- * \linear_congruential_engine number generators, each with c = 0. Each invocation
- * returns a random number X(n) := (MLCG1(n) - MLCG2(n)) mod (m1 - 1), where
- * m1 denotes the modulus of MLCG1.
- *
- * The template parameter @c val is the validation value checked by validation.
+ * \linear_congruential_engine number generators, each with c = 0. Each
+ * invocation returns a random number
+ * X(n) := (MLCG1(n) - MLCG2(n)) mod (m1 - 1),
+ * where m1 denotes the modulus of MLCG1.
*/
-template<class MLCG1, class MLCG2,
-#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
- typename MLCG1::result_type
-#else
- int32_t
-#endif
- val>
-class additive_combine
+template<class MLCG1, class MLCG2>
+class additive_combine_engine
{
public:
- typedef MLCG1 first_base;
- typedef MLCG2 second_base;
- typedef typename MLCG1::result_type result_type;
-
- // Required by old Boost.Random concept
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
- /**
- * Returns: The smallest value that the generator can produce
- */
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 1; }
- /**
- * Returns: The largest value that the generator can produce
- */
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_mlcg1.max)()-1; }
-
- /**
- * Constructs an \additive_combine generator using the
- * default constructors of the two base generators.
- */
- additive_combine() : _mlcg1(), _mlcg2() { }
- /**
- * Constructs an \additive_combine generator, using seed as
- * the constructor argument for both base generators.
- */
- explicit additive_combine(result_type aseed)
- : _mlcg1(aseed), _mlcg2(aseed) { }
- /**
- * Constructs an \additive_combine generator, using
- * @c seed1 and @c seed2 as the constructor argument to
- * the first and second base generators, respectively.
- */
- additive_combine(typename MLCG1::result_type seed1,
- typename MLCG2::result_type seed2)
- : _mlcg1(seed1), _mlcg2(seed2) { }
- /**
- * Contructs an \additive_combine generator with
- * values from the range defined by the input iterators first
- * and last. first will be modified to point to the element
- * after the last one used.
- *
- * Throws: @c std::invalid_argument if the input range is too small.
- *
- * Exception Safety: Basic
- */
- template<class It> additive_combine(It& first, It last)
- : _mlcg1(first, last), _mlcg2(first, last) { }
-
- /**
- * Seeds an \additive_combine generator using the default
- * seeds of the two base generators.
- */
- void seed()
- {
- _mlcg1.seed();
- _mlcg2.seed();
- }
-
- /**
- * Seeds an \additive_combine generator, using @c seed as the
- * seed for both base generators.
- */
- void seed(result_type aseed)
- {
- _mlcg1.seed(aseed);
- _mlcg2.seed(aseed);
- }
-
- /**
- * Seeds an \additive_combine generator, using @c seed1 and @c seed2 as
- * the seeds to the first and second base generators, respectively.
- */
- void seed(typename MLCG1::result_type seed1,
- typename MLCG2::result_type seed2)
- {
- _mlcg1.seed(seed1);
- _mlcg2.seed(seed2);
- }
-
- /**
- * Seeds an \additive_combine generator with
- * values from the range defined by the input iterators first
- * and last. first will be modified to point to the element
- * after the last one used.
- *
- * Throws: @c std::invalid_argument if the input range is too small.
- *
- * Exception Safety: Basic
- */
- template<class It> void seed(It& first, It last)
- {
- _mlcg1.seed(first, last);
- _mlcg2.seed(first, last);
- }
-
- /**
- * Returns: the next value of the generator
- */
- result_type operator()() {
- result_type z = _mlcg1() - _mlcg2();
- if(z < 1)
- z += MLCG1::modulus-1;
- return z;
- }
-
- static bool validation(result_type x) { return val == x; }
-
-#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
-
-#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
- /**
- * Writes the state of an \additive_combine generator to a @c
- * std::ostream. The textual representation of an \additive_combine
- * generator is the textual representation of the first base
- * generator followed by the textual representation of the
- * second base generator.
- */
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const additive_combine& r)
- { os << r._mlcg1 << " " << r._mlcg2; return os; }
-
- /**
- * Reads the state of an \additive_combine generator from a
- * @c std::istream.
- */
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, additive_combine& r)
- { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
+ typedef MLCG1 first_base;
+ typedef MLCG2 second_base;
+ typedef typename MLCG1::result_type result_type;
+
+ // Required by old Boost.Random concept
+ BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+ /**
+ * Returns the smallest value that the generator can produce
+ */
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return 1; }
+ /**
+ * Returns the largest value that the generator can produce
+ */
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return MLCG1::modulus-1; }
+
+ /**
+ * Constructs an @c additive_combine_engine using the
+ * default constructors of the two base generators.
+ */
+ additive_combine_engine() : _mlcg1(), _mlcg2() { }
+ /**
+ * Constructs an @c additive_combine_engine, using seed as
+ * the constructor argument for both base generators.
+ */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(additive_combine_engine,
+ result_type, seed_arg)
+ : _mlcg1(seed_arg), _mlcg2(seed_arg) { }
+ /**
+ * Constructs an @c additive_combine_engine, using seq as
+ * the constructor argument for both base generators.
+ *
+ * @xmlwarning
+ * The semantics of this function are liable to change.
+ * A @c seed_seq is designed to generate all the seeds
+ * in one shot, but this seeds the two base engines
+ * independantly and probably ends up giving the same
+ * sequence to both.
+ * @endxmlwarning
+ */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(additive_combine_engine,
+ SeedSeq, seq)
+ : _mlcg1(seq), _mlcg2(seq) { }
+ /**
+ * Constructs an @c additive_combine_engine, using
+ * @c seed1 and @c seed2 as the constructor argument to
+ * the first and second base generators, respectively.
+ */
+ additive_combine_engine(typename MLCG1::result_type seed1,
+ typename MLCG2::result_type seed2)
+ : _mlcg1(seed1), _mlcg2(seed2) { }
+ /**
+ * Contructs an @c additive_combine_engine with
+ * values from the range defined by the input iterators first
+ * and last. first will be modified to point to the element
+ * after the last one used.
+ *
+ * Throws: @c std::invalid_argument if the input range is too small.
+ *
+ * Exception Safety: Basic
+ */
+ template<class It> additive_combine_engine(It& first, It last)
+ : _mlcg1(first, last), _mlcg2(first, last) { }
+
+ /**
+ * Seeds an @c additive_combine_engine using the default
+ * seeds of the two base generators.
+ */
+ void seed()
+ {
+ _mlcg1.seed();
+ _mlcg2.seed();
+ }
+
+ /**
+ * Seeds an @c additive_combine_engine, using @c seed as the
+ * seed for both base generators.
+ */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(additive_combine_engine,
+ result_type, seed_arg)
+ {
+ _mlcg1.seed(seed_arg);
+ _mlcg2.seed(seed_arg);
+ }
+
+ /**
+ * Seeds an @c additive_combine_engine, using @c seq to
+ * seed both base generators.
+ *
+ * See the warning on the corresponding constructor.
+ */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(additive_combine_engine,
+ result_type, seq)
+ {
+ _mlcg1.seed(seq);
+ _mlcg2.seed(seq);
+ }
+
+ /**
+ * Seeds an @c additive_combine generator, using @c seed1 and @c seed2 as
+ * the seeds to the first and second base generators, respectively.
+ */
+ void seed(typename MLCG1::result_type seed1,
+ typename MLCG2::result_type seed2)
+ {
+ _mlcg1.seed(seed1);
+ _mlcg2.seed(seed2);
+ }
+
+ /**
+ * Seeds an @c additive_combine_engine with
+ * values from the range defined by the input iterators first
+ * and last. first will be modified to point to the element
+ * after the last one used.
+ *
+ * Throws: @c std::invalid_argument if the input range is too small.
+ *
+ * Exception Safety: Basic
+ */
+ template<class It> void seed(It& first, It last)
+ {
+ _mlcg1.seed(first, last);
+ _mlcg2.seed(first, last);
+ }
+
+ /** Returns the next value of the generator. */
+ result_type operator()() {
+ result_type z = _mlcg1() - _mlcg2();
+ if(z < 1)
+ z += MLCG1::modulus-1;
+ return z;
+ }
+
+ /** Fills a range with random values */
+ template<class Iter>
+ void generate(Iter first, Iter last)
+ {
+ for(; first != last; ++first) {
+ *first = (*this)();
+ }
+ }
+
+#ifndef BOOST_NO_LONG_LONG
+ /** Advances the state of the generator by @c z. */
+ void discard(boost::ulong_long_type z)
+ {
+ _mlcg1.discard(z);
+ _mlcg2.discard(z);
+ }
#endif
- /**
- * Returns: true iff the two \additive_combine generators will
- * produce the same sequence of values.
- */
- friend bool operator==(const additive_combine& x, const additive_combine& y)
- { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
- /**
- * Returns: true iff the two \additive_combine generators will
- * produce different sequences of values.
- */
- friend bool operator!=(const additive_combine& x, const additive_combine& y)
- { return !(x == y); }
-#else
- // Use a member function; Streamable concept not supported.
- bool operator==(const additive_combine& rhs) const
- { return _mlcg1 == rhs._mlcg1 && _mlcg2 == rhs._mlcg2; }
- bool operator!=(const additive_combine& rhs) const
- { return !(*this == rhs); }
-#endif
+ /**
+ * Writes the state of an @c additive_combine_engine to a @c
+ * std::ostream. The textual representation of an @c
+ * additive_combine_engine is the textual representation of
+ * the first base generator followed by the textual representation
+ * of the second base generator.
+ */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, additive_combine_engine, r)
+ { os << r._mlcg1 << " " << r._mlcg2; return os; }
+
+ /**
+ * Reads the state of an @c additive_combine_engine from a
+ * @c std::istream.
+ */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, additive_combine_engine, r)
+ { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
+
+ /**
+ * Returns: true iff the two @c additive_combine_engines will
+ * produce the same sequence of values.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(additive_combine_engine, x, y)
+ { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
+ /**
+ * Returns: true iff the two @c additive_combine_engines will
+ * produce different sequences of values.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(additive_combine_engine)
private:
- MLCG1 _mlcg1;
- MLCG2 _mlcg2;
+ MLCG1 _mlcg1;
+ MLCG2 _mlcg2;
};
-} // namespace random
+#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
+template<class MLCG1, class MLCG2>
+const bool additive_combine_engine<MLCG1, MLCG2>::has_fixed_range;
+#endif
+
+/// \cond
+/** Provided for backwards compatibility. */
+template<class MLCG1, class MLCG2, typename MLCG1::result_type val = 0>
+class additive_combine : public additive_combine_engine<MLCG1, MLCG2>
+{
+ typedef additive_combine_engine<MLCG1, MLCG2> base_t;
+public:
+ typedef typename base_t::result_type result_type;
+ additive_combine() {}
+ template<class T>
+ additive_combine(T& arg) : base_t(arg) {}
+ template<class T>
+ additive_combine(const T& arg) : base_t(arg) {}
+ template<class It>
+ additive_combine(It& first, It last) : base_t(first, last) {}
+};
+/// \endcond
/**
* The specialization \ecuyer1988 was suggested in
@@ -220,10 +267,14 @@
* Communications of the ACM, Vol. 31, No. 6, June 1988, pp. 742-749, 774
* @endblockquote
*/
-typedef random::additive_combine<
- random::linear_congruential_engine<int32_t, 40014, 0, 2147483563>,
- random::linear_congruential_engine<int32_t, 40692, 0, 2147483399>,
- 2060321752> ecuyer1988;
+typedef additive_combine_engine<
+ linear_congruential_engine<int32_t, 40014, 0, 2147483563>,
+ linear_congruential_engine<int32_t, 40692, 0, 2147483399>
+> ecuyer1988;
+
+} // namespace random
+
+using random::ecuyer1988;
} // namespace boost
Modified: trunk/boost/random/detail/const_mod.hpp
==============================================================================
--- trunk/boost/random/detail/const_mod.hpp (original)
+++ trunk/boost/random/detail/const_mod.hpp 2011-02-08 12:46:48 EST (Tue, 08 Feb 2011)
@@ -89,6 +89,8 @@
return mult_small(a, x);
else if(traits::is_signed && (m%a < m/a))
return mult_schrage(a, x);
+ else if(x == 1)
+ return a;
else {
// difficult
assert(!"const_mod::mult with a too large");
Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2011-02-08 12:46:48 EST (Tue, 08 Feb 2011)
@@ -20,7 +20,6 @@
rand48
minstd_rand0
minstd_rand
- ecuyer1988
kreutzer1986
hellekalek1995
mt11213b
@@ -44,6 +43,7 @@
}
run test_mt19937.cpp /boost//unit_test_framework ;
+run test_ecuyer1988.cpp /boost//unit_test_framework ;
run test_seed_seq.cpp /boost//unit_test_framework ;
Added: trunk/libs/random/test/test_ecuyer1988.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_ecuyer1988.cpp 2011-02-08 12:46:48 EST (Tue, 08 Feb 2011)
@@ -0,0 +1,16 @@
+/* test_ecuyer1988.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/additive_combine.hpp>
+
+#define BOOST_RANDOM_URNG boost::random::ecuyer1988
+
+#include "test_generator.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