File indexing completed on 2025-01-18 09:51:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP
0014 #define BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP
0015
0016 #include <cassert>
0017 #include <istream>
0018 #include <iosfwd>
0019 #include <boost/random/detail/operators.hpp>
0020 #include <boost/random/exponential_distribution.hpp>
0021
0022 namespace boost {
0023 namespace random {
0024
0025
0026
0027
0028
0029
0030
0031 template<class RealType = double>
0032 class laplace_distribution {
0033 public:
0034 typedef RealType result_type;
0035 typedef RealType input_type;
0036
0037 class param_type {
0038 public:
0039 typedef laplace_distribution distribution_type;
0040
0041
0042
0043
0044
0045 explicit param_type(RealType mean_arg = RealType(0.0),
0046 RealType beta_arg = RealType(1.0))
0047 : _mean(mean_arg), _beta(beta_arg)
0048 {}
0049
0050
0051 RealType mean() const { return _mean; }
0052
0053 RealType beta() const { return _beta; }
0054
0055
0056 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0057 { os << parm._mean << ' ' << parm._beta; return os; }
0058
0059
0060 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0061 { is >> parm._mean >> std::ws >> parm._beta; return is; }
0062
0063
0064 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0065 { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; }
0066
0067
0068 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0069
0070 private:
0071 RealType _mean;
0072 RealType _beta;
0073 };
0074
0075
0076
0077
0078 explicit laplace_distribution(RealType mean_arg = RealType(0.0),
0079 RealType beta_arg = RealType(1.0))
0080 : _mean(mean_arg), _beta(beta_arg)
0081 {}
0082
0083 explicit laplace_distribution(const param_type& parm)
0084 : _mean(parm.mean()), _beta(parm.beta())
0085 {}
0086
0087
0088
0089
0090
0091 template<class URNG>
0092 RealType operator()(URNG& urng) const
0093 {
0094 RealType exponential = exponential_distribution<RealType>()(urng);
0095 if(uniform_01<RealType>()(urng) < 0.5)
0096 exponential = -exponential;
0097 return _mean + _beta * exponential;
0098 }
0099
0100
0101
0102
0103
0104 template<class URNG>
0105 RealType operator()(URNG& urng, const param_type& parm) const
0106 {
0107 return laplace_distribution(parm)(urng);
0108 }
0109
0110
0111 RealType mean() const { return _mean; }
0112
0113 RealType beta() const { return _beta; }
0114
0115
0116 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0117 { return RealType(-std::numeric_limits<RealType>::infinity()); }
0118
0119 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0120 { return RealType(std::numeric_limits<RealType>::infinity()); }
0121
0122
0123 param_type param() const { return param_type(_mean, _beta); }
0124
0125 void param(const param_type& parm)
0126 {
0127 _mean = parm.mean();
0128 _beta = parm.beta();
0129 }
0130
0131
0132
0133
0134
0135 void reset() { }
0136
0137
0138 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, laplace_distribution, wd)
0139 {
0140 os << wd.param();
0141 return os;
0142 }
0143
0144
0145 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, laplace_distribution, wd)
0146 {
0147 param_type parm;
0148 if(is >> parm) {
0149 wd.param(parm);
0150 }
0151 return is;
0152 }
0153
0154
0155
0156
0157
0158 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(laplace_distribution, lhs, rhs)
0159 { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; }
0160
0161
0162
0163
0164
0165 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(laplace_distribution)
0166
0167 private:
0168 RealType _mean;
0169 RealType _beta;
0170 };
0171
0172 }
0173 }
0174
0175 #endif