aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
numericalDiscreteVariable_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
48#include <algorithm>
49#include <ostream>
50#include <sstream>
51#include <string>
52
53#include <agrum/agrum.h>
54
57
59
60// to ease IDE parsers
62
63#ifndef DOXYGEN_SHOULD_SKIP_THIS
64
65namespace gum {
66
68 INLINE NumericalDiscreteVariable::NumericalDiscreteVariable(const std::string& aName,
69 const std::string& aDesc) :
70 DiscreteVariable(aName, aDesc) {
71 // for debugging purposes
72 GUM_CONSTRUCTOR(NumericalDiscreteVariable);
73 }
74
76 INLINE
77 NumericalDiscreteVariable::NumericalDiscreteVariable(const NumericalDiscreteVariable& from) :
78 DiscreteVariable(from), _domain_(from._domain_) {
79 // for debugging purposes
80 GUM_CONS_CPY(NumericalDiscreteVariable);
81 }
82
84 INLINE NumericalDiscreteVariable::NumericalDiscreteVariable(NumericalDiscreteVariable&& from) :
85 DiscreteVariable(std::move(from)), _domain_(std::move(from._domain_)) {
86 from._domain_.clear();
87 // for debugging purposes
88 GUM_CONS_MOV(NumericalDiscreteVariable)
89 }
90
92 INLINE NumericalDiscreteVariable* NumericalDiscreteVariable::clone() const {
93 return new NumericalDiscreteVariable(*this);
94 }
95
97 INLINE NumericalDiscreteVariable::~NumericalDiscreteVariable() {
98 GUM_DESTRUCTOR(NumericalDiscreteVariable);
99 }
100
102 INLINE NumericalDiscreteVariable&
103 NumericalDiscreteVariable::operator=(const NumericalDiscreteVariable& from) {
104 // avoid self assignment
105 if (&from != this) {
106 DiscreteVariable::operator=(from);
107 _domain_ = from._domain_;
108 }
109
110 return *this;
111 }
112
114 INLINE NumericalDiscreteVariable&
115 NumericalDiscreteVariable::operator=(NumericalDiscreteVariable&& from) {
116 // avoid self assignment
117 if (&from != this) {
118 DiscreteVariable::operator=(std::move(from));
119 _domain_ = std::move(from._domain_);
120 from._domain_.clear();
121 }
122
123 return *this;
124 }
125
127 INLINE Size NumericalDiscreteVariable::domainSize() const { return _domain_.size(); }
128
130 INLINE VarType NumericalDiscreteVariable::varType() const { return VarType::NUMERICAL; }
131
133 INLINE Idx NumericalDiscreteVariable::index(const std::string& aLabel) const {
134 const auto x = std::stod(aLabel);
135 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
136
137 if (ind != _domain_.size() && _domain_[ind] == x) {
138 return ind;
139 } else {
140 GUM_ERROR(NotFound, "label '" << aLabel << "' is unknown in " << toString());
141 }
142 }
143
144 INLINE Idx NumericalDiscreteVariable::closestIndex(double val) const {
145 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
146
147 if (ind == _domain_.size()) return _domain_.size() - 1;
148 if (ind == 0) return 0;
149
150 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
151 return ind;
152 } else {
153 return ind - 1;
154 }
155 }
156
158 INLINE std::string NumericalDiscreteVariable::label(Idx i) const {
159 // note that if i is outside the domain, Sequence _domain_ will raise
160 // an exception
161 if (i < 0 || i >= _domain_.size())
162 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".")
163 return _generateLabel_(_domain_[i]);
164 }
165
167 INLINE double NumericalDiscreteVariable::numerical(Idx i) const {
168 if (i < 0 || i >= _domain_.size())
169 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".");
170 return _domain_[i];
171 }
172
174 INLINE const std::vector< double >& NumericalDiscreteVariable::numericalDomain() const {
175 return _domain_;
176 }
177
178 INLINE bool NumericalDiscreteVariable::isValue(double value) const {
179 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
180 return (ind != _domain_.size() && _domain_[ind] == value);
181 }
182
184 INLINE void NumericalDiscreteVariable::changeValue(double old_value, double new_value) {
185 if (!gum::isfinite< double >(new_value)) {
187 "Value '" << new_value << "' is not allowed for variable " << name())
188 }
189 if (!isValue(old_value)) return;
190 if (isValue(new_value)) {
192 "Value" << new_value << " already belongs to the domain of the variable");
193 }
194 eraseValue(old_value);
195 addValue(new_value);
196 }
197
199 INLINE void NumericalDiscreteVariable::eraseValue(double value) {
200 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
201 if (ind < _domain_.size() && _domain_[ind] == value) { _domain_.erase(_domain_.begin() + ind); }
202 }
203
205 INLINE void NumericalDiscreteVariable::eraseValues() { _domain_.clear(); }
206
207 INLINE bool NumericalDiscreteVariable::_checkSameDomain_(const gum::Variable& aRV) const {
208 // we can assume that aRV is a IntegerVariable
209 const auto& cv = static_cast< const NumericalDiscreteVariable& >(aRV);
210 if (domainSize() != cv.domainSize()) return false;
211 return cv._domain_ == _domain_;
212 }
213
214 INLINE std::string NumericalDiscreteVariable::closestLabel(double val) const {
215 return label(closestIndex(val));
216 }
217
218 INLINE void NumericalDiscreteVariable::addValue(double value) {
219 if (!gum::isfinite< double >(value)) {
221 "Value '" << value << "' is not allowed for variable " << toString())
222 }
223 if (isValue(value)) {
225 "Value " << value << " already belongs to the domain of the variable "
226 << toString());
227 }
228 _domain_.push_back(value);
229 std::sort(_domain_.begin(), _domain_.end());
230 }
231
232 INLINE std::string NumericalDiscreteVariable::_generateLabel_(double f) const {
233 return compact_tostr(f);
234 }
235
236 INLINE std::string NumericalDiscreteVariable::toFast() const {
237 std::stringstream s;
238 s << name() << domain();
239 return s.str();
240 }
241} /* namespace gum */
242
243#endif /* DOXYGEN SHOULD SKIP THIS */
Exception : default in label.
Base class for discrete random variable.
Exception : a similar element already exists.
Exception : the element we looked for cannot be found.
NumericalDiscreteVariable(const std::string &aName, const std::string &aDesc="")
constructor
Exception : out of bound.
Base class for every random variable.
Definition variable.h:79
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::string compact_tostr(T value)
Returns a path to a unique file name.
Class hash tables iterators.
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
bool isfinite(T arg)
Definition math_utils.h:75
VarType
Definition variable.h:60
STL namespace.
Base class for numerical discrete random variables.