123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- #ifndef BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
- #define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
- #include <iosfwd>
- #include <boost/limits.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/gamma_distribution.hpp>
- namespace boost {
- namespace random {
- template<class RealType = double>
- class chi_squared_distribution {
- public:
- typedef RealType result_type;
- typedef RealType input_type;
- class param_type {
- public:
- typedef chi_squared_distribution distribution_type;
-
- explicit param_type(RealType n_arg = RealType(1))
- : _n(n_arg)
- {}
-
- RealType n() const { return _n; }
- #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
-
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os,
- const param_type& parm)
- {
- os << parm._n;
- return os;
- }
-
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
- {
- is >> parm._n;
- return is;
- }
- #endif
-
- friend bool operator==(const param_type& lhs, const param_type& rhs)
- {
- return lhs._n == rhs._n;
- }
-
- friend bool operator!=(const param_type& lhs, const param_type& rhs)
- {
- return !(lhs == rhs);
- }
- private:
- RealType _n;
- };
-
-
- explicit chi_squared_distribution(RealType n_arg = RealType(1))
- : _impl(static_cast<RealType>(n_arg / 2))
- {
- }
-
-
- explicit chi_squared_distribution(const param_type& parm)
- : _impl(static_cast<RealType>(parm.n() / 2))
- {
- }
-
-
- template<class URNG>
- RealType operator()(URNG& urng)
- {
- return 2 * _impl(urng);
- }
-
-
- template<class URNG>
- RealType operator()(URNG& urng, const param_type& parm) const
- {
- return chi_squared_distribution(parm)(urng);
- }
-
- RealType n() const { return 2 * _impl.alpha(); }
-
- RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
-
- RealType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
- { return (std::numeric_limits<RealType>::infinity)(); }
-
- param_type param() const { return param_type(n()); }
-
- void param(const param_type& parm)
- {
- typedef gamma_distribution<RealType> impl_type;
- typename impl_type::param_type impl_parm(static_cast<RealType>(parm.n() / 2));
- _impl.param(impl_parm);
- }
-
- void reset() { _impl.reset(); }
- #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
-
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os,
- const chi_squared_distribution& c2d)
- {
- os << c2d.param();
- return os;
- }
-
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is,
- chi_squared_distribution& c2d)
- {
- c2d.read(is);
- return is;
- }
- #endif
-
- friend bool operator==(const chi_squared_distribution& lhs,
- const chi_squared_distribution& rhs)
- {
- return lhs._impl == rhs._impl;
- }
-
- friend bool operator!=(const chi_squared_distribution& lhs,
- const chi_squared_distribution& rhs)
- {
- return !(lhs == rhs);
- }
- private:
-
- template<class CharT, class Traits>
- void read(std::basic_istream<CharT, Traits>& is) {
- param_type parm;
- if(is >> parm) {
- param(parm);
- }
- }
- gamma_distribution<RealType> _impl;
-
- };
- }
- }
- #endif
|