123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- #ifndef BOOST_RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_HPP_INCLUDED
- #define BOOST_RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_HPP_INCLUDED
- #include <vector>
- #include <numeric>
- #include <boost/assert.hpp>
- #include <boost/random/uniform_real.hpp>
- #include <boost/random/discrete_distribution.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/detail/vector_io.hpp>
- #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
- #include <initializer_list>
- #endif
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- namespace boost {
- namespace random {
- template<class RealType = double, class WeightType = double>
- class piecewise_constant_distribution {
- public:
- typedef std::size_t input_type;
- typedef RealType result_type;
- class param_type {
- public:
- typedef piecewise_constant_distribution distribution_type;
-
- param_type()
- {
- _weights.push_back(WeightType(1));
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- }
-
- template<class IntervalIter, class WeightIter>
- param_type(IntervalIter intervals_first, IntervalIter intervals_last,
- WeightIter weight_first)
- : _intervals(intervals_first, intervals_last)
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- _weights.push_back(WeightType(1));
- } else {
- _weights.reserve(_intervals.size() - 1);
- for(std::size_t i = 0; i < _intervals.size() - 1; ++i) {
- _weights.push_back(*weight_first++);
- }
- }
- }
- #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
-
- template<class T, class F>
- param_type(const std::initializer_list<T>& il, F f)
- : _intervals(il.begin(), il.end())
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- _weights.push_back(WeightType(1));
- } else {
- _weights.reserve(_intervals.size() - 1);
- for(std::size_t i = 0; i < _intervals.size() - 1; ++i) {
- RealType midpoint = (_intervals[i] + _intervals[i + 1]) / 2;
- _weights.push_back(f(midpoint));
- }
- }
- }
- #endif
-
- template<class IntervalRange, class WeightRange>
- param_type(const IntervalRange& intervals_arg,
- const WeightRange& weights_arg)
- : _intervals(boost::begin(intervals_arg), boost::end(intervals_arg)),
- _weights(boost::begin(weights_arg), boost::end(weights_arg))
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- _weights.push_back(WeightType(1));
- }
- }
-
- template<class F>
- param_type(std::size_t nw, RealType xmin, RealType xmax, F f)
- {
- std::size_t n = (nw == 0) ? 1 : nw;
- double delta = (xmax - xmin) / n;
- BOOST_ASSERT(delta > 0);
- for(std::size_t k = 0; k < n; ++k) {
- _weights.push_back(f(xmin + k*delta + delta/2));
- _intervals.push_back(xmin + k*delta);
- }
- _intervals.push_back(xmax);
- }
-
- std::vector<RealType> intervals() const { return _intervals; }
-
- std::vector<RealType> densities() const
- {
- RealType sum = std::accumulate(_weights.begin(), _weights.end(),
- static_cast<RealType>(0));
- std::vector<RealType> result;
- result.reserve(_weights.size());
- for(std::size_t i = 0; i < _weights.size(); ++i) {
- RealType width = _intervals[i + 1] - _intervals[i];
- result.push_back(_weights[i] / (sum * width));
- }
- return result;
- }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
- {
- detail::print_vector(os, parm._intervals);
- detail::print_vector(os, parm._weights);
- return os;
- }
-
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
- {
- std::vector<RealType> new_intervals;
- std::vector<WeightType> new_weights;
- detail::read_vector(is, new_intervals);
- detail::read_vector(is, new_weights);
- if(is) {
- parm._intervals.swap(new_intervals);
- parm._weights.swap(new_weights);
- }
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
- {
- return lhs._intervals == rhs._intervals
- && lhs._weights == rhs._weights;
- }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
- private:
- friend class piecewise_constant_distribution;
- std::vector<RealType> _intervals;
- std::vector<WeightType> _weights;
- };
-
- piecewise_constant_distribution()
- {
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- }
-
- template<class IntervalIter, class WeightIter>
- piecewise_constant_distribution(IntervalIter first_interval,
- IntervalIter last_interval,
- WeightIter first_weight)
- : _intervals(first_interval, last_interval)
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- } else {
- std::vector<WeightType> actual_weights;
- actual_weights.reserve(_intervals.size() - 1);
- for(std::size_t i = 0; i < _intervals.size() - 1; ++i) {
- actual_weights.push_back(*first_weight++);
- }
- typedef discrete_distribution<std::size_t, WeightType> bins_type;
- typename bins_type::param_type bins_param(actual_weights);
- _bins.param(bins_param);
- }
- }
- #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
-
- template<class T, class F>
- piecewise_constant_distribution(std::initializer_list<T> il, F f)
- : _intervals(il.begin(), il.end())
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- } else {
- std::vector<WeightType> actual_weights;
- actual_weights.reserve(_intervals.size() - 1);
- for(std::size_t i = 0; i < _intervals.size() - 1; ++i) {
- RealType midpoint = (_intervals[i] + _intervals[i + 1]) / 2;
- actual_weights.push_back(f(midpoint));
- }
- typedef discrete_distribution<std::size_t, WeightType> bins_type;
- typename bins_type::param_type bins_param(actual_weights);
- _bins.param(bins_param);
- }
- }
- #endif
-
- template<class IntervalsRange, class WeightsRange>
- piecewise_constant_distribution(const IntervalsRange& intervals_arg,
- const WeightsRange& weights_arg)
- : _bins(weights_arg),
- _intervals(boost::begin(intervals_arg), boost::end(intervals_arg))
- {
- if(_intervals.size() < 2) {
- _intervals.clear();
- _intervals.push_back(RealType(0));
- _intervals.push_back(RealType(1));
- }
- }
-
- template<class F>
- piecewise_constant_distribution(std::size_t nw,
- RealType xmin,
- RealType xmax,
- F f)
- : _bins(nw, xmin, xmax, f)
- {
- if(nw == 0) { nw = 1; }
- RealType delta = (xmax - xmin) / nw;
- _intervals.reserve(nw + 1);
- for(std::size_t i = 0; i < nw; ++i) {
- _intervals.push_back(xmin + i * delta);
- }
- _intervals.push_back(xmax);
- }
-
- explicit piecewise_constant_distribution(const param_type& parm)
- : _bins(parm._weights),
- _intervals(parm._intervals)
- {
- }
-
- template<class URNG>
- RealType operator()(URNG& urng) const
- {
- std::size_t i = _bins(urng);
- return uniform_real<RealType>(_intervals[i], _intervals[i+1])(urng);
- }
-
-
- template<class URNG>
- RealType operator()(URNG& urng, const param_type& parm) const
- {
- return piecewise_constant_distribution(parm)(urng);
- }
-
-
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return _intervals.front(); }
-
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return _intervals.back(); }
-
- std::vector<RealType> densities() const
- {
- std::vector<RealType> result(_bins.probabilities());
- for(std::size_t i = 0; i < result.size(); ++i) {
- result[i] /= (_intervals[i+1] - _intervals[i]);
- }
- return(result);
- }
-
- std::vector<RealType> intervals() const { return _intervals; }
-
- param_type param() const
- {
- return param_type(_intervals, _bins.probabilities());
- }
-
- void param(const param_type& parm)
- {
- std::vector<RealType> new_intervals(parm._intervals);
- typedef discrete_distribution<std::size_t, WeightType> bins_type;
- typename bins_type::param_type bins_param(parm._weights);
- _bins.param(bins_param);
- _intervals.swap(new_intervals);
- }
-
-
- void reset() { _bins.reset(); }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(
- os, piecewise_constant_distribution, pcd)
- {
- os << pcd.param();
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(
- is, piecewise_constant_distribution, pcd)
- {
- param_type parm;
- if(is >> parm) {
- pcd.param(parm);
- }
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(
- piecewise_constant_distribution, lhs, rhs)
- {
- return lhs._bins == rhs._bins && lhs._intervals == rhs._intervals;
- }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(piecewise_constant_distribution)
- private:
- discrete_distribution<std::size_t, WeightType> _bins;
- std::vector<RealType> _intervals;
- };
- }
- }
- #endif
|