45#ifndef DOXYGEN_SHOULD_SKIP_THIS
55 template <
typename T_TICKS >
59 _is_empirical = aDRV._is_empirical;
60 _ticks_ = aDRV._ticks_;
63 template <
typename T_TICKS >
65 if (_ticks_.empty()) {
GUM_ERROR(OutOfBounds,
"No tick defined for variable " << name()) }
67 if (target < _ticks_[0])
return static_cast< Idx >(0);
68 if (target > _ticks_[_ticks_.size() - 1])
return static_cast< Idx >(_ticks_.size() - 2);
70 const Idx res = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
71 if (res + 1 >= _ticks_.size())
return static_cast< Idx >(_ticks_.size() - 2);
72 if (_ticks_[res] == target)
return res;
77 template <
typename T_TICKS >
79 const Idx ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
80 if (ind + 1 >= _ticks_.size()) {
81 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
83 if (_ticks_[ind] == target)
return ind;
85 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
88 template <
typename T_TICKS >
90 const Size ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
91 if (ind >= _ticks_.size()) {
return false; }
92 return (_ticks_[ind] == target);
95 template <
typename T_TICKS >
97 std::string_view aDesc) :
99 GUM_CONSTRUCTOR(DiscretizedVariable);
100 _is_empirical =
false;
104 template <
typename T_TICKS >
105 DiscretizedVariable< T_TICKS >::DiscretizedVariable(std::string_view aName,
106 std::string_view aDesc,
107 const std::vector< T_TICKS >& ticks,
109 IDiscretizedVariable(aName, aDesc) {
110 GUM_CONSTRUCTOR(DiscretizedVariable)
111 _is_empirical = is_empirical;
112 _ticks_.reserve(ticks.size());
113 for (
const auto tick: ticks) {
117 if (!isTick(tick)) { _ticks_.push_back(tick); }
119 std::sort(_ticks_.begin(), _ticks_.end());
122 template <
typename T_TICKS >
123 DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const DiscretizedVariable< T_TICKS >& aDRV) :
124 IDiscretizedVariable(aDRV) {
125 GUM_CONS_CPY(DiscretizedVariable);
129 template <
typename T_TICKS >
130 DiscretizedVariable< T_TICKS >::~DiscretizedVariable() {
131 GUM_DESTRUCTOR(DiscretizedVariable);
134 template <
typename T_TICKS >
135 DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone()
const {
136 return new DiscretizedVariable< T_TICKS >(*
this);
139 template <
typename T_TICKS >
140 DiscretizedVariable< T_TICKS >&
141 DiscretizedVariable< T_TICKS >::operator=(
const DiscretizedVariable< T_TICKS >& aDRV) {
146 template <
typename T_TICKS >
147 DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::addTick(
const T_TICKS& aTick) {
156 _ticks_.push_back(aTick);
157 std::sort(_ticks_.begin(), _ticks_.end());
162 template <
typename T_TICKS >
163 void DiscretizedVariable< T_TICKS >::eraseTicks() {
167 template <
typename T_TICKS >
168 std::string DiscretizedVariable< T_TICKS >::label(Idx i)
const {
171 const char open = ((i == 0) && _is_empirical) ?
'(' :
'[';
172 const char close = (i == _ticks_.size() - 2) ? (_is_empirical ?
')' :
']') :
'[';
174 return std::format(
"{}{};{}{}", open, _ticks_[i], _ticks_[i + 1], close);
182 template <
typename T_TICKS >
183 double DiscretizedVariable< T_TICKS >::numerical(Idx index)
const {
184 if (index >= _ticks_.size() - 1) {
187 const auto& a =
static_cast< double >(_ticks_[index]);
188 const auto& b =
static_cast< double >(_ticks_[index + 1]);
190 return (b + a) / 2.0;
198 template <
typename T_TICKS >
199 double DiscretizedVariable< T_TICKS >::draw(Idx indice)
const {
200 if (indice >= _ticks_.size() - 1) {
203 const auto& a =
static_cast< double >(_ticks_[indice]);
204 const auto& b =
static_cast< double >(_ticks_[indice + 1]);
207 if (indice < _ticks_.size() - 2) {
211 if (p == b) p = (b - a) / 2;
217 template <
typename T_TICKS >
218 Idx DiscretizedVariable< T_TICKS >::index(std::string_view label)
const {
222 std::istringstream i(std::string{label});
225 if (target < _ticks_[0]) {
226 if (_ticks_[0] - target < 1e-10 * (1.0 + std::abs(
static_cast< double >(_ticks_[0]))))
228 if (_is_empirical)
return 0;
231 "less than first range (< " << _ticks_[0] <<
") for " << target <<
" in "
235 if (
const auto size = _ticks_.size(); target > _ticks_[size - 1]) {
236 if (target - _ticks_[size - 1]
237 < 1e-10 * (1.0 + std::abs(
static_cast< double >(_ticks_[size - 1])))) {
244 "more than last range (> " << _ticks_[size - 1] <<
") for " << target <<
" in "
245 << *
this <<
":" << target - _ticks_[size - 1])
252 std::istringstream ii(std::string{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 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 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 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 VarType DiscretizedVariable< T_TICKS >::varType()
const {
304 return VarType::DISCRETIZED;
307 template <
typename T_TICKS >
308 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 {
318 std::string result =
"<";
320 if (domainSize() > 0) {
323 for (Idx i = 1; i < domainSize(); ++i) {
334 template <
typename T_TICKS >
335 const std::vector< T_TICKS >& DiscretizedVariable< T_TICKS >::ticks()
const {
336 return this->_ticks_;
339 template <
typename T_TICKS >
340 std::vector< double > DiscretizedVariable< T_TICKS >::ticksAsDoubles()
const {
341 const std::size_t size = _ticks_.size();
342 std::vector< double > ticks(size);
343 for (
auto i =
static_cast< std::size_t
>(0); i < size; ++i)
344 ticks[i] =
static_cast< double >(_ticks_[i]);
348 template <
typename T_TICKS >
349 std::string DiscretizedVariable< T_TICKS >::toFast()
const {
350 std::string result = name();
351 if (_is_empirical) result +=
"+";
354 for (
const auto& t: _ticks_) {
355 if (!first) result +=
",";
357 result += std::format(
"{}", t);
363 template <
typename T_TICKS >
364 std::string DiscretizedVariable< T_TICKS >::stype()
const {
365 return "Discretized";
Exception : default in label.
Class for discretized random variable.
DiscretizedVariable(std::string_view aName, std::string_view 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_)
Idx index(std::string_view label) const override
from the label to its index in var.
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.