aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/tr1/random
diff options
context:
space:
mode:
authorPaolo Carlini <pcarlini@suse.de>2006-08-18 17:15:43 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2006-08-18 17:15:43 +0000
commit42031254bfae5d55420bf8c5d57572c42d1b5d17 (patch)
tree9dc901883bf70756a7b2ba08e5c8c3928f3b54f1 /libstdc++-v3/include/tr1/random
parent7867a3f7392e23f92f207c0cdec444b9c1c6f433 (diff)
downloadgcc-42031254bfae5d55420bf8c5d57572c42d1b5d17.zip
gcc-42031254bfae5d55420bf8c5d57572c42d1b5d17.tar.gz
gcc-42031254bfae5d55420bf8c5d57572c42d1b5d17.tar.bz2
random (class binomial_distribution<>): Add.
2006-08-18 Paolo Carlini <pcarlini@suse.de> * include/tr1/random (class binomial_distribution<>): Add. * include/tr1/random.tcc (binomial_distribution<>::operator(), operator<<(std::basic_ostream<>&, const binomial_distribution<>&), operator>>(std::basic_istream<>&, binomial_distribution<>&, binomial_distribution<>::_M_waiting(), binomial_distribution<>:: _M_initialize()): Define. * testsuite/tr1/5_numerical_facilities/random/binomial_distribution/ requirements/typedefs.cc: New. * include/tr1/random (geometric_distribution<>:: geometric_distribution(const _RealType&)): Fix DEBUG_ASSERT limits. * include/tr1/random (poisson_distribution): Add normal_distribution member, adjust consistently; minor tweaks and rearrangements of the arithmetic. (operator>>(std::basic_istream<>&, poisson_distribution<>&)): Move out of line. * include/tr1/random.tcc: Adjust. * include/tr1/random.tcc (normal_distribution<>::operator()): Minor tweaks. From-SVN: r116245
Diffstat (limited to 'libstdc++-v3/include/tr1/random')
-rw-r--r--libstdc++-v3/include/tr1/random150
1 files changed, 138 insertions, 12 deletions
diff --git a/libstdc++-v3/include/tr1/random b/libstdc++-v3/include/tr1/random
index ef4390a..b769bbd 100644
--- a/libstdc++-v3/include/tr1/random
+++ b/libstdc++-v3/include/tr1/random
@@ -1588,9 +1588,9 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
// constructors and member function
explicit
geometric_distribution(const _RealType& __p = _RealType(0.5))
- : _M_p(__p), _M_log_p(std::log(_M_p))
+ : _M_p(__p), _M_log_p(std::log(__p))
{
- _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
+ _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
}
/**
@@ -1649,6 +1649,9 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
};
+ template<typename _RealType>
+ class normal_distribution;
+
/**
* @brief A discrete Poisson random number distribution.
*
@@ -1665,6 +1668,12 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const poisson_distribution<_IntType, _RealType>& __x);
+ template<typename _IntType, typename _RealType,
+ typename _CharT, typename _Traits>
+ std::basic_istream<_CharT, _Traits>&
+ operator>>(std::basic_istream<_CharT, _Traits>& __is,
+ poisson_distribution<_IntType, _RealType>& __x);
+
template<typename _IntType, typename _RealType>
class poisson_distribution
{
@@ -1676,7 +1685,7 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
// constructors and member function
explicit
poisson_distribution(const _RealType& __mean = _RealType(1))
- : _M_mean(__mean)
+ : _M_mean(__mean), _M_nd()
{
_GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
_M_initialize();
@@ -1690,7 +1699,8 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
{ return _M_mean; }
void
- reset() { }
+ reset()
+ { _M_nd.reset(); }
template<class _UniformRandomNumberGenerator>
result_type
@@ -1721,27 +1731,143 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
*
* @returns The input stream with @p __x extracted or in an error state.
*/
- template<typename _CharT, typename _Traits>
+ template<typename _IntType1, typename _RealType1,
+ typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
- poisson_distribution& __x)
- {
- __is >> __x._M_mean;
- __x._M_initialize();
- return __is;
- }
+ poisson_distribution<_IntType1, _RealType1>& __x);
private:
void
_M_initialize();
+ // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
+ normal_distribution<_RealType> _M_nd;
+
_RealType _M_mean;
+
// _M_lm_thr hosts either log(mean) or the threshold of the simple
// method.
_RealType _M_lm_thr;
#if _GLIBCXX_USE_C99_MATH_TR1
- _RealType _M_lfm, _M_sm, _M_d, _M_scx4, _M_2cx, _M_c2b, _M_cb;
+ _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
+#endif
+ };
+
+
+ /**
+ * @brief A discrete binomial random number distribution.
+ *
+ * The formula for the binomial probability mass function is
+ * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
+ * and @f$ p @f$ are the parameters of the distribution.
+ */
+ template<typename _IntType = int, typename _RealType = double>
+ class binomial_distribution;
+
+ template<typename _IntType, typename _RealType,
+ typename _CharT, typename _Traits>
+ std::basic_ostream<_CharT, _Traits>&
+ operator<<(std::basic_ostream<_CharT, _Traits>& __os,
+ const binomial_distribution<_IntType, _RealType>& __x);
+
+ template<typename _IntType, typename _RealType,
+ typename _CharT, typename _Traits>
+ std::basic_istream<_CharT, _Traits>&
+ operator>>(std::basic_istream<_CharT, _Traits>& __is,
+ binomial_distribution<_IntType, _RealType>& __x);
+
+ template<typename _IntType, typename _RealType>
+ class binomial_distribution
+ {
+ public:
+ // types
+ typedef _RealType input_type;
+ typedef _IntType result_type;
+
+ // constructors and member function
+ explicit
+ binomial_distribution(_IntType __t = 1,
+ const _RealType& __p = _RealType(0.5))
+ : _M_t(__t), _M_p(__p), _M_nd()
+ {
+ _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
+ _M_initialize();
+ }
+
+ /**
+ * Gets the distribution @p t parameter.
+ */
+ _IntType
+ t() const
+ { return _M_t; }
+
+ /**
+ * Gets the distribution @p p parameter.
+ */
+ _RealType
+ p() const
+ { return _M_p; }
+
+ void
+ reset()
+ { _M_nd.reset(); }
+
+ template<class _UniformRandomNumberGenerator>
+ result_type
+ operator()(_UniformRandomNumberGenerator& __urng);
+
+ /**
+ * Inserts a %binomial_distribution random number distribution
+ * @p __x into the output stream @p __os.
+ *
+ * @param __os An output stream.
+ * @param __x A %binomial_distribution random number distribution.
+ *
+ * @returns The output stream with the state of @p __x inserted or in
+ * an error state.
+ */
+ template<typename _IntType1, typename _RealType1,
+ typename _CharT, typename _Traits>
+ friend std::basic_ostream<_CharT, _Traits>&
+ operator<<(std::basic_ostream<_CharT, _Traits>& __os,
+ const binomial_distribution<_IntType1, _RealType1>& __x);
+
+ /**
+ * Extracts a %binomial_distribution random number distribution
+ * @p __x from the input stream @p __is.
+ *
+ * @param __is An input stream.
+ * @param __x A %binomial_distribution random number generator engine.
+ *
+ * @returns The input stream with @p __x extracted or in an error state.
+ */
+ template<typename _IntType1, typename _RealType1,
+ typename _CharT, typename _Traits>
+ friend std::basic_istream<_CharT, _Traits>&
+ operator>>(std::basic_istream<_CharT, _Traits>& __is,
+ binomial_distribution<_IntType1, _RealType1>& __x);
+
+ private:
+ void
+ _M_initialize();
+
+ template<class _UniformRandomNumberGenerator>
+ result_type
+ _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
+
+ // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
+ normal_distribution<_RealType> _M_nd;
+
+ _IntType _M_t;
+ _RealType _M_p;
+
+ _RealType _M_q;
+#if _GLIBCXX_USE_C99_MATH_TR1
+ _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
+ _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
#endif
+ bool _M_easy;
};
/* @} */ // group tr1_random_distributions_discrete