46#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template <
typename T_TICKS >
60 _is_empirical = aDRV._is_empirical;
61 _ticks_ = aDRV._ticks_;
64 template <
typename T_TICKS >
66 if (_ticks_.empty()) {
GUM_ERROR(OutOfBounds,
"No tick defined for variable " << name()) }
68 if (target < _ticks_[0])
return static_cast< Idx >(0);
69 if (target > _ticks_[_ticks_.size() - 1])
return static_cast< Idx >(_ticks_.size() - 2);
71 const Idx res = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
72 if (res + 1 >= _ticks_.size())
return static_cast< Idx >(_ticks_.size() - 2);
73 if (_ticks_[res] == target)
return res;
78 template <
typename T_TICKS >
80 const Idx ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
81 if (ind + 1 >= _ticks_.size()) {
82 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
84 if (_ticks_[ind] == target)
return ind;
86 GUM_ERROR(OutOfBounds, target <<
" is not a tick in " << *
this)
89 template <
typename T_TICKS >
91 const Size ind = std::lower_bound(_ticks_.begin(), _ticks_.end(), target) - _ticks_.begin();
92 if (ind >= _ticks_.size()) {
return false; }
93 return (_ticks_[ind] == target);
96 template <
typename T_TICKS >
98 std::string_view aDesc) :
100 GUM_CONSTRUCTOR(DiscretizedVariable);
101 _is_empirical =
false;
105 template <
typename T_TICKS >
106 DiscretizedVariable< T_TICKS >::DiscretizedVariable(std::string_view aName,
107 std::string_view aDesc,
108 const std::vector< T_TICKS >& ticks,
110 IDiscretizedVariable(aName, aDesc) {
111 GUM_CONSTRUCTOR(DiscretizedVariable)
112 _is_empirical = is_empirical;
113 _ticks_.reserve(ticks.size());
114 for (
const auto tick: ticks) {
118 if (!isTick(tick)) { _ticks_.push_back(tick); }
120 std::sort(_ticks_.begin(), _ticks_.end());
123 template <
typename T_TICKS >
124 DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const DiscretizedVariable< T_TICKS >& aDRV) :
125 IDiscretizedVariable(aDRV) {
126 GUM_CONS_CPY(DiscretizedVariable);
130 template <
typename T_TICKS >
131 DiscretizedVariable< T_TICKS >::~DiscretizedVariable() {
132 GUM_DESTRUCTOR(DiscretizedVariable);
135 template <
typename T_TICKS >
136 DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone()
const {
137 return new DiscretizedVariable< T_TICKS >(*
this);
140 template <
typename T_TICKS >
141 DiscretizedVariable< T_TICKS >&
142 DiscretizedVariable< T_TICKS >::operator=(
const DiscretizedVariable< T_TICKS >& aDRV) {
147 template <
typename T_TICKS >
148 DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::addTick(
const T_TICKS& aTick) {
157 _ticks_.push_back(aTick);
158 std::sort(_ticks_.begin(), _ticks_.end());
163 template <
typename T_TICKS >
164 void DiscretizedVariable< T_TICKS >::eraseTicks() {
168 template <
typename T_TICKS >
169 std::string DiscretizedVariable< T_TICKS >::label(Idx i)
const {
172 const char open = ((i == 0) && _is_empirical) ?
'(' :
'[';
173 const char close = (i == _ticks_.size() - 2) ? (_is_empirical ?
')' :
']') :
'[';
175 return std::format(
"{}{};{}{}", open, _ticks_[i], _ticks_[i + 1], close);
183 template <
typename T_TICKS >
184 double DiscretizedVariable< T_TICKS >::numerical(Idx index)
const {
185 if (index >= _ticks_.size() - 1) {
188 const auto& a =
static_cast< double >(_ticks_[index]);
189 const auto& b =
static_cast< double >(_ticks_[index + 1]);
191 return (b + a) / 2.0;
199 template <
typename T_TICKS >
200 double DiscretizedVariable< T_TICKS >::draw(Idx indice)
const {
201 if (indice >= _ticks_.size() - 1) {
204 const auto& a =
static_cast< double >(_ticks_[indice]);
205 const auto& b =
static_cast< double >(_ticks_[indice + 1]);
208 if (indice < _ticks_.size() - 2) {
212 if (p == b) p = (b - a) / 2;
218 template <
typename T_TICKS >
219 Idx DiscretizedVariable< T_TICKS >::index(std::string_view label)
const {
223 std::istringstream i(std::string{label});
227 if (target < _ticks_[0]) {
228 if (_ticks_[0] - target < epsilon * (1.0 + std::abs(
static_cast< double >(_ticks_[0]))))
230 if (_is_empirical)
return 0;
233 "less than first range (< " << _ticks_[0] <<
") for " << target <<
" in "
237 if (
const auto size = _ticks_.size(); target > _ticks_[size - 1]) {
238 if (target - _ticks_[size - 1]
239 < epsilon * (1.0 + std::abs(
static_cast< double >(_ticks_[size - 1])))) {
246 "more than last range (> " << _ticks_[size - 1] <<
") for " << target <<
" in "
247 << *
this <<
":" << target - _ticks_[size - 1])
254 std::istringstream ii(std::string{label});
259 if (!(ii >> c1 >> target >> c2 >> t2 >> c3)) {
264 const std::string s1{
"[]()"};
265 if (
const std::string s2{
",;"}; s1.find(c1) == std::string::npos
266 || (s1.find(c3) == std::string::npos)
267 || (s2.find(c2) == std::string::npos)) {
271 const Idx it1 = pos_(target);
273 if ((it1 + 1 >= _ticks_.size()) || (t2 != _ticks_[it1 + 1])) {
280 template <
typename T_TICKS >
281 bool DiscretizedVariable< T_TICKS >::_checkSameDomain_(
const gum::Variable& aRV)
const {
283 const auto& cv =
static_cast< const DiscretizedVariable< T_TICKS >&
>(aRV);
284 if (domainSize() != cv.domainSize()) {
return false; }
285 return cv._ticks_ == _ticks_ && cv._is_empirical == _is_empirical;
288 template <
typename T_TICKS >
289 Idx DiscretizedVariable< T_TICKS >::closestIndex(
double val)
const {
290 if (val <= _ticks_[0]) {
return 0; }
291 if (val >= _ticks_[_ticks_.size() - 1]) {
return _ticks_.size() - 2; }
292 return pos_(
static_cast< T_TICKS
>(val));
299 template <
typename T_TICKS >
300 Size DiscretizedVariable< T_TICKS >::domainSize()
const {
301 return (_ticks_.size() < 2) ?
static_cast< Size
>(0) : static_cast< Size >(_ticks_.size() - 1);
304 template <
typename T_TICKS >
305 VarType DiscretizedVariable< T_TICKS >::varType()
const {
306 return VarType::DISCRETIZED;
309 template <
typename T_TICKS >
310 const T_TICKS& DiscretizedVariable< T_TICKS >::tick(Idx i)
const {
311 if (i >= _ticks_.size()) {
318 template <
typename T_TICKS >
319 std::string DiscretizedVariable< T_TICKS >::domain()
const {
320 std::string result =
"<";
322 if (domainSize() > 0) {
325 for (Idx i = 1; i < domainSize(); ++i) {
336 template <
typename T_TICKS >
337 const std::vector< T_TICKS >& DiscretizedVariable< T_TICKS >::ticks()
const {
338 return this->_ticks_;
341 template <
typename T_TICKS >
342 std::vector< double > DiscretizedVariable< T_TICKS >::ticksAsDoubles()
const {
343 const std::size_t size = _ticks_.size();
344 std::vector< double > ticks(size);
345 for (
auto i =
static_cast< std::size_t
>(0); i < size; ++i)
346 ticks[i] =
static_cast< double >(_ticks_[i]);
350 template <
typename T_TICKS >
351 std::string DiscretizedVariable< T_TICKS >::toFast()
const {
352 std::string result = name();
353 if (_is_empirical) result +=
"+";
356 for (
const auto& t: _ticks_) {
357 if (!first) result +=
",";
359 result += std::format(
"{}", t);
365 template <
typename T_TICKS >
366 std::string DiscretizedVariable< T_TICKS >::stype()
const {
367 return "Discretized";
double epsilonFine() const noexcept
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
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
GUM_SHARED_PUBLIC double randomProba()
Returns a random double between 0 and 1 included (i.e.
gum is the global namespace for all aGrUM entities
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
Contains useful methods for random stuff.