aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
rangeVariable_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
49#include <cmath>
50#include <sstream>
51
52// to ease IDE parsing
54
55namespace gum {
56
57 // Copy Factory.
58 // @return Returns a pointer on a new copy of this.
59 INLINE RangeVariable* RangeVariable::clone() const { return new RangeVariable(*this); }
60
61 // returns the size of the random discrete variable domain
63 return (_maxBound_ < _minBound_) ? Size(0) : Size(_maxBound_ + 1 - _minBound_);
64 }
65
66 // Get the indice-th label. This method is pure virtual.
67 // @param indice the index of the label we wish to return
68 // @throw OutOfBound
69 INLINE std::string RangeVariable::label(Idx indice) const {
70 long target = static_cast< long >(indice) + _minBound_;
71 if (belongs(target)) {
72 std::stringstream strBuff;
73 strBuff << target;
74 return strBuff.str();
75 } else {
76 GUM_ERROR(OutOfBounds, "Indice out of bounds.")
77 }
78 }
79
80 INLINE
81 double RangeVariable::numerical(Idx indice) const {
82 return double(_minBound_ + static_cast< long >(indice));
83 }
84
85 INLINE Idx RangeVariable::index(const std::string& label) const {
86 std::istringstream i(label);
87 long target;
88
89 if (!(i >> target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
90
91 if (!belongs(target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
92
93 return static_cast< Idx >(target - _minBound_);
94 }
95
96 INLINE Idx RangeVariable::closestIndex(double val) const {
97 int res = static_cast< int >(std::rint(val));
98 if (res - val == 0.5) res--;
99
100 if (res < _minBound_) {
101 return 0;
102 } else if (res > _maxBound_) {
103 return domainSize() - 1;
104 } else {
105 return res - _minBound_;
106 }
107 }
108
109 // Returns the lower bound.
110 INLINE long RangeVariable::minVal() const { return _minBound_; }
111
112 // Set a new value for the lower bound.
114
115 // Returns the upper bound.
116 INLINE long RangeVariable::maxVal() const { return _maxBound_; }
117
118 // Set a new value of the upper bound.
120
121 // Returns true if the param belongs to the variable's interval.
122 INLINE bool RangeVariable::belongs(long val) const {
123 return ((_minBound_ <= val) && (val <= _maxBound_));
124 }
125
126 // Copy operator
127 // @param aRV to be copied
128 // @return a ref to *this
132 return *this;
133 }
134
136
137 INLINE bool RangeVariable::_checkSameDomain_(const gum::Variable& aRV) const {
138 // we can assume that aRV is a RangeVariable
139 const auto& cv = static_cast< const RangeVariable& >(aRV);
140 if (_minBound_ != cv._minBound_) return false;
141 if (_maxBound_ != cv._maxBound_) return false;
142 return true;
143 }
144} /* namespace gum */
Exception : the element we looked for cannot be found.
Exception : out of bound.
void setMaxVal(long maxVal)
Set a new value of the upper bound.
RangeVariable * clone() const final
Copy Factory.
Idx index(const std::string &) const final
Idx closestIndex(double val) const final
returns the closest index of the value
std::string label(Idx index) const final
Get the index-th label.
long maxVal() const
Returns the upper bound.
long _minBound_
The upper bound.
bool belongs(long val) const
Returns true if the param belongs to the variable's interval.
bool _checkSameDomain_(const Variable &aRV) const final
check the domain
void setMinVal(long minVal)
Set a new value for the lower bound.
Size domainSize() const final
returns the size of the random discrete variable domain
VarType varType() const final
returns the type of variable
RangeVariable & operator=(const RangeVariable &aRV)
Copy operator.
double numerical(Idx index) const final
get a numerical representation of the index-the value.
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
long minVal() const
Returns the lower bound.
long _maxBound_
The upper bound.
Base class for every random variable.
Definition variable.h:79
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:60
Header of gumRangeVariable.