43#ifndef DOXYGEN_SHOULD_SKIP_THIS
53 template <
typename T_TICKS >
57 _is_empirical = aDRV._is_empirical;
58 _ticks_ = aDRV._ticks_;
61 template <
typename T_TICKS >
63 if (target < _ticks_[0])
return static_cast< Idx >(0);
64 if (target > _ticks_[_ticks_.size() - 1])
return static_cast< Idx >(_ticks_.size() - 2);
66 const Idx res = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
67 if (res + 1 >= _ticks_.size())
return static_cast< Idx >(_ticks_.size() - 2);
68 if (_ticks_[res] == target)
return res;
73 template <
typename T_TICKS >
75 const Idx ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
76 if (ind + 1 >= _ticks_.size()) {
77 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
79 if (_ticks_[ind] == target)
return ind;
81 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
84 template <
typename T_TICKS >
86 const Size ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
87 if (ind >= _ticks_.size()) {
return false; }
88 return (_ticks_[ind] == target);
91 template <
typename T_TICKS >
93 const std::string& aDesc) :
95 GUM_CONSTRUCTOR(DiscretizedVariable);
96 _is_empirical =
false;
100 template <
typename T_TICKS >
101 INLINE DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const std::string& aName,
102 const std::string& aDesc,
103 const std::vector< T_TICKS >& ticks,
105 IDiscretizedVariable(aName, aDesc) {
106 GUM_CONSTRUCTOR(DiscretizedVariable)
107 _is_empirical = is_empirical;
108 _ticks_.reserve(ticks.size());
109 for (
const auto tick: ticks) {
113 if (!isTick(tick)) { _ticks_.push_back(tick); }
115 std::sort(_ticks_.begin(), _ticks_.end());
118 template <
typename T_TICKS >
119 DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const DiscretizedVariable< T_TICKS >& aDRV) :
120 IDiscretizedVariable(aDRV) {
121 GUM_CONS_CPY(DiscretizedVariable);
125 template <
typename T_TICKS >
126 DiscretizedVariable< T_TICKS >::~DiscretizedVariable() {
127 GUM_DESTRUCTOR(DiscretizedVariable);
130 template <
typename T_TICKS >
131 DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone()
const {
132 return new DiscretizedVariable< T_TICKS >(*
this);
135 template <
typename T_TICKS >
136 INLINE DiscretizedVariable< T_TICKS >&
137 DiscretizedVariable< T_TICKS >::operator=(
const DiscretizedVariable< T_TICKS >& aDRV) {
142 template <
typename T_TICKS >
143 DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::addTick(
const T_TICKS& aTick) {
152 _ticks_.push_back(aTick);
153 std::sort(_ticks_.begin(), _ticks_.end());
158 template <
typename T_TICKS >
159 INLINE
void DiscretizedVariable< T_TICKS >::eraseTicks() {
163 template <
typename T_TICKS >
164 INLINE std::string DiscretizedVariable< T_TICKS >::label(Idx i)
const {
165 std::stringstream ss;
169 if ((i == 0) && _is_empirical) ss <<
"(";
172 ss << std::format(
"{};{}", _ticks_[i], _ticks_[i + 1]);
174 if (i == _ticks_.size() - 2) {
175 if (_is_empirical) ss <<
")";
187 template <
typename T_TICKS >
188 INLINE
double DiscretizedVariable< T_TICKS >::numerical(Idx index)
const {
189 if (index >= _ticks_.size() - 1) {
192 const auto& a =
static_cast< double >(_ticks_[index]);
193 const auto& b =
static_cast< double >(_ticks_[index + 1]);
195 return (b + a) / 2.0;
203 template <
typename T_TICKS >
204 INLINE
double DiscretizedVariable< T_TICKS >::draw(Idx indice)
const {
205 if (indice >= _ticks_.size() - 1) {
208 const auto& a =
static_cast< double >(_ticks_[indice]);
209 const auto& b =
static_cast< double >(_ticks_[indice + 1]);
212 if (indice < _ticks_.size() - 2) {
216 if (p == b) p = (b - a) / 2;
222 template <
typename T_TICKS >
223 INLINE Idx DiscretizedVariable< T_TICKS >::index(
const std::string& label)
const {
227 std::istringstream i(label);
230 if (target < _ticks_[0]) {
231 if (_ticks_[0] - target < 1e-10)
return 0;
232 if (_is_empirical)
return 0;
235 "less than first range (< " << _ticks_[0] <<
") for " << target <<
" in "
239 if (
const auto size = _ticks_.size(); target > _ticks_[size - 1]) {
240 if (target - _ticks_[size - 1] < 1e-10)
return size - 2;
241 if (_is_empirical)
return size - 2;
244 "more than last range (> " << _ticks_[size - 1] <<
") for " << target <<
" in "
245 << *
this <<
":" << target - _ticks_[size - 1])
252 std::istringstream ii(label);
257 if (!(ii >> c1 >> target >> c2 >> t2 >> c3)) {
262 const std::string s1{
"[]()"};
263 if (
const std::string s2{
",;"}; s1.find(c1) == std::string::npos
264 || (s1.find(c3) == std::string::npos)
265 || (s2.find(c2) == std::string::npos)) {
269 const Idx it1 = pos_(target);
271 if ((it1 + 1 >= _ticks_.size()) || (t2 != _ticks_[it1 + 1])) {
278 template <
typename T_TICKS >
279 INLINE
bool DiscretizedVariable< T_TICKS >::_checkSameDomain_(
const gum::Variable& aRV)
const {
281 const auto& cv =
static_cast< const DiscretizedVariable< T_TICKS >&
>(aRV);
282 if (domainSize() != cv.domainSize())
return false;
283 return cv._ticks_ == _ticks_ && cv._is_empirical == _is_empirical;
286 template <
typename T_TICKS >
287 INLINE Idx DiscretizedVariable< T_TICKS >::closestIndex(
double val)
const {
288 if (val <= _ticks_[0]) {
return 0; }
289 if (val >= _ticks_[_ticks_.size() - 1]) {
return _ticks_.size() - 2; }
290 return pos_(
static_cast< T_TICKS
>(val));
297 template <
typename T_TICKS >
298 INLINE Size DiscretizedVariable< T_TICKS >::domainSize()
const {
299 return (_ticks_.size() < 2) ?
static_cast< Size
>(0) : static_cast< Size >(_ticks_.size() - 1);
302 template <
typename T_TICKS >
303 INLINE
VarType DiscretizedVariable< T_TICKS >::varType()
const {
304 return VarType::DISCRETIZED;
307 template <
typename T_TICKS >
308 INLINE
const T_TICKS& DiscretizedVariable< T_TICKS >::tick(Idx i)
const {
309 if (i >= _ticks_.size()) {
316 template <
typename T_TICKS >
317 std::string DiscretizedVariable< T_TICKS >::domain()
const {
321 if (domainSize() > 0) {
324 for (Idx i = 1; i < domainSize(); ++i) {
335 template <
typename T_TICKS >
336 INLINE
const std::vector< T_TICKS >& DiscretizedVariable< T_TICKS >::ticks()
const {
337 return this->_ticks_;
340 template <
typename T_TICKS >
341 INLINE std::vector< double > DiscretizedVariable< T_TICKS >::ticksAsDoubles()
const {
342 const std::size_t size = _ticks_.size();
343 std::vector< double > ticks(size);
344 for (
auto i =
static_cast< std::size_t
>(0); i < size; ++i)
345 ticks[i] =
static_cast< double >(_ticks_[i]);
349 template <
typename T_TICKS >
350 std::string DiscretizedVariable< T_TICKS >::toFast()
const {
354 if (_is_empirical) s <<
"+";
356 for (
const auto& t: _ticks_) {
357 if (!first) s <<
",";
359 s << std::format(
"{}", t);
Exception : default in label.
Class for discretized random variable.
Idx index(const std::string &label) const override
from the label to its index in var.
DiscretizedVariable(const std::string &aName, const std::string &aDesc)
Constructor.
bool isTick(const T_TICKS &aTick) const
void copy_(const DiscretizedVariable< T_TICKS > &aDRV)
make a copy
Idx pos_(const T_TICKS &target) const
search the class of target (internally use dichotomy_)
A base class for discretized variables, independent of the ticks type.
Exception : the element we looked for cannot be found.
Exception : out of bound.
Base class for every random variable.
void copy_(const Variable &aRV)
protected copy
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
gum is the global namespace for all aGrUM entities
Contains useful methods for random stuff.