File indexing completed on 2025-01-18 09:51:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
0014 #define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
0015
0016 #include <iosfwd>
0017 #include <boost/limits.hpp>
0018
0019 #include <boost/random/detail/config.hpp>
0020 #include <boost/random/gamma_distribution.hpp>
0021
0022 namespace boost {
0023 namespace random {
0024
0025
0026
0027
0028
0029
0030
0031
0032 template<class RealType = double>
0033 class chi_squared_distribution {
0034 public:
0035 typedef RealType result_type;
0036 typedef RealType input_type;
0037
0038 class param_type {
0039 public:
0040 typedef chi_squared_distribution distribution_type;
0041
0042
0043
0044
0045
0046
0047 explicit param_type(RealType n_arg = RealType(1))
0048 : _n(n_arg)
0049 {}
0050
0051 RealType n() const { return _n; }
0052 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
0053
0054 template<class CharT, class Traits>
0055 friend std::basic_ostream<CharT,Traits>&
0056 operator<<(std::basic_ostream<CharT,Traits>& os,
0057 const param_type& parm)
0058 {
0059 os << parm._n;
0060 return os;
0061 }
0062
0063
0064 template<class CharT, class Traits>
0065 friend std::basic_istream<CharT,Traits>&
0066 operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
0067 {
0068 is >> parm._n;
0069 return is;
0070 }
0071 #endif
0072
0073 friend bool operator==(const param_type& lhs, const param_type& rhs)
0074 {
0075 return lhs._n == rhs._n;
0076 }
0077
0078 friend bool operator!=(const param_type& lhs, const param_type& rhs)
0079 {
0080 return !(lhs == rhs);
0081 }
0082 private:
0083 RealType _n;
0084 };
0085
0086
0087
0088
0089
0090
0091
0092 explicit chi_squared_distribution(RealType n_arg = RealType(1))
0093 : _impl(static_cast<RealType>(n_arg / 2))
0094 {
0095 }
0096
0097
0098
0099
0100
0101 explicit chi_squared_distribution(const param_type& parm)
0102 : _impl(static_cast<RealType>(parm.n() / 2))
0103 {
0104 }
0105
0106
0107
0108
0109
0110 template<class URNG>
0111 RealType operator()(URNG& urng)
0112 {
0113 return 2 * _impl(urng);
0114 }
0115
0116
0117
0118
0119
0120 template<class URNG>
0121 RealType operator()(URNG& urng, const param_type& parm) const
0122 {
0123 return chi_squared_distribution(parm)(urng);
0124 }
0125
0126
0127 RealType n() const { return 2 * _impl.alpha(); }
0128
0129
0130 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
0131
0132 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
0133 { return (std::numeric_limits<RealType>::infinity)(); }
0134
0135
0136 param_type param() const { return param_type(n()); }
0137
0138 void param(const param_type& parm)
0139 {
0140 typedef gamma_distribution<RealType> impl_type;
0141 typename impl_type::param_type impl_parm(static_cast<RealType>(parm.n() / 2));
0142 _impl.param(impl_parm);
0143 }
0144
0145
0146
0147
0148
0149 void reset() { _impl.reset(); }
0150
0151 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
0152
0153 template<class CharT, class Traits>
0154 friend std::basic_ostream<CharT,Traits>&
0155 operator<<(std::basic_ostream<CharT,Traits>& os,
0156 const chi_squared_distribution& c2d)
0157 {
0158 os << c2d.param();
0159 return os;
0160 }
0161
0162
0163 template<class CharT, class Traits>
0164 friend std::basic_istream<CharT,Traits>&
0165 operator>>(std::basic_istream<CharT,Traits>& is,
0166 chi_squared_distribution& c2d)
0167 {
0168 c2d.read(is);
0169 return is;
0170 }
0171 #endif
0172
0173
0174
0175 friend bool operator==(const chi_squared_distribution& lhs,
0176 const chi_squared_distribution& rhs)
0177 {
0178 return lhs._impl == rhs._impl;
0179 }
0180
0181
0182 friend bool operator!=(const chi_squared_distribution& lhs,
0183 const chi_squared_distribution& rhs)
0184 {
0185 return !(lhs == rhs);
0186 }
0187
0188 private:
0189
0190
0191
0192 template<class CharT, class Traits>
0193 void read(std::basic_istream<CharT, Traits>& is) {
0194 param_type parm;
0195 if(is >> parm) {
0196 param(parm);
0197 }
0198 }
0199
0200 gamma_distribution<RealType> _impl;
0201
0202
0203 };
0204
0205 }
0206
0207 }
0208
0209 #endif