aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
continuousVariable_tpl.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 <sstream>
50#include <utility>
51
52#include <agrum/agrum.h>
53
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58namespace gum {
59
60
62 template < typename GUM_SCALAR >
63 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(const std::string& aName,
64 const std::string& aDesc,
65 GUM_SCALAR lower_bound,
66 GUM_SCALAR upper_bound) :
67 IContinuousVariable(aName, aDesc), _lower_bound_(lower_bound), _upper_bound_(upper_bound) {
68 if (_lower_bound_ > _upper_bound_) { std::swap(_upper_bound_, _lower_bound_); }
69 GUM_CONSTRUCTOR(ContinuousVariable);
70 }
71
73 template < typename GUM_SCALAR >
74 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
75 const ContinuousVariable< GUM_SCALAR >& from) :
76 IContinuousVariable(from), _lower_bound_(from._lower_bound_),
77 _upper_bound_(from._upper_bound_) {
78 GUM_CONS_CPY(ContinuousVariable);
79 }
80
82 template < typename GUM_SCALAR >
83 template < typename TX_VAL >
84 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
85 const ContinuousVariable< TX_VAL >& from) :
86 IContinuousVariable(from), _lower_bound_(GUM_SCALAR(from._lower_bound_)),
87 _upper_bound_(GUM_SCALAR(from._upper_bound_)) {
88 GUM_CONS_CPY(ContinuousVariable);
89 }
90
92 template < typename GUM_SCALAR >
93 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
94 ContinuousVariable< GUM_SCALAR >&& from) :
95 IContinuousVariable(std::move(from)), _lower_bound_(from._lower_bound_),
96 _upper_bound_(from._upper_bound_) {
97 GUM_CONS_MOV(ContinuousVariable);
98 }
99
101 template < typename GUM_SCALAR >
102 INLINE ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
103 GUM_DESTRUCTOR(ContinuousVariable);
104 }
105
107 template < typename GUM_SCALAR >
108 INLINE ContinuousVariable< GUM_SCALAR >* ContinuousVariable< GUM_SCALAR >::clone() const {
109 return new ContinuousVariable< GUM_SCALAR >(*this);
110 }
111
113 template < typename GUM_SCALAR >
114 INLINE ContinuousVariable< GUM_SCALAR >&
115 ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< GUM_SCALAR >& from) {
116 IContinuousVariable::operator=(from);
117 _lower_bound_ = from._lower_bound_;
118 _upper_bound_ = from._upper_bound_;
119 return *this;
120 }
121
123 template < typename GUM_SCALAR >
124 template < typename TX_VAL >
125 INLINE ContinuousVariable< GUM_SCALAR >&
126 ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< TX_VAL >& from) {
127 IContinuousVariable::operator=(from);
128 _lower_bound_ = GUM_SCALAR(from._lower_bound_);
129 _upper_bound_ = GUM_SCALAR(from._upper_bound_);
130 return *this;
131 }
132
134 template < typename GUM_SCALAR >
135 INLINE ContinuousVariable< GUM_SCALAR >&
136 ContinuousVariable< GUM_SCALAR >::operator=(ContinuousVariable< GUM_SCALAR >&& from) {
137 IContinuousVariable::operator=(std::move(from));
138 _lower_bound_ = from._lower_bound_;
139 _upper_bound_ = from._upper_bound_;
140 return *this;
141 }
142
144 template < typename GUM_SCALAR >
145 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::operator[](const std::string& str) const {
146 std::istringstream stream(str);
147 GUM_SCALAR value;
148 stream >> value;
149
150 if (belongs(value)) return value;
151 else GUM_ERROR(OutOfBounds, "the value does not delong to the domain of the variable")
152 }
153
155 template < typename GUM_SCALAR >
156 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
157 return _lower_bound_;
158 }
159
161 template < typename GUM_SCALAR >
162 INLINE double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble() const {
163 return (double)_lower_bound_;
164 }
165
167 template < typename GUM_SCALAR >
168 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
169 return _upper_bound_;
170 }
171
173 template < typename GUM_SCALAR >
174 INLINE double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble() const {
175 return (double)_upper_bound_;
176 }
177
179 template < typename GUM_SCALAR >
180 INLINE void ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
181 if (new_bound <= _upper_bound_) _lower_bound_ = new_bound;
182 else GUM_ERROR(OutOfBounds, "the new lower bound would be higher than the upper bound")
183 }
184
186 template < typename GUM_SCALAR >
187 INLINE void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(const double new_bound) {
188 setLowerBound((GUM_SCALAR)new_bound);
189 }
190
192 template < typename GUM_SCALAR >
193 INLINE void ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
194 if (new_bound >= _lower_bound_) _upper_bound_ = new_bound;
195 else GUM_ERROR(OutOfBounds, "the new upper bound would be lower than the lower bound")
196 }
197
199 template < typename GUM_SCALAR >
200 INLINE void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(const double new_bound) {
201 setUpperBound((GUM_SCALAR)new_bound);
202 }
203
205 template < typename GUM_SCALAR >
206 INLINE VarType ContinuousVariable< GUM_SCALAR >::varType() const {
207 return VarType::CONTINUOUS;
208 }
209
211 template < typename GUM_SCALAR >
212 INLINE std::string ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
213 if (belongs(value)) return std::to_string(value);
214 GUM_ERROR(OutOfBounds, "the value does not belong to the domain of the variable")
215 }
216
218 template < typename GUM_SCALAR >
219 INLINE bool ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
220 return (value >= _lower_bound_) && (value <= _upper_bound_);
221 }
222
224 template < typename GUM_SCALAR >
225 INLINE std::string ContinuousVariable< GUM_SCALAR >::domain() const {
226 std::ostringstream stream;
227 stream << '[' << _lower_bound_ << ';' << _upper_bound_ << ']';
228 return stream.str();
229 }
230
232 template < typename GUM_SCALAR >
233 INLINE bool ContinuousVariable< GUM_SCALAR >::_checkSameDomain_(const gum::Variable& aRV) const {
234 // we can assume that aRV is a ContinuousVariable
235 const auto& cv = static_cast< const ContinuousVariable< GUM_SCALAR >& >(aRV);
236 return cv._lower_bound_ == _lower_bound_ && cv._upper_bound_ == _upper_bound_;
237 }
238
240 template < typename GUM_SCALAR >
241 INLINE std::string ContinuousVariable< GUM_SCALAR >::toString() const {
242 std::string str(this->name());
243 str += domain();
244 return str;
245 }
246
248 template < typename GUM_SCALAR >
249 INLINE std::string ContinuousVariable< GUM_SCALAR >::toStringWithDescription() const {
250 std::string str(this->description());
251 str += domain();
252 return str;
253 }
254
256 template < typename GUM_SCALAR >
257 std::ostream& operator<<(std::ostream& stream, const ContinuousVariable< GUM_SCALAR >& var) {
258 return stream << var.toString();
259 }
260
261
262} /* namespace gum */
263
264
265#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A base class for continuous variables, independent of the GUM_SCALAR type.
Exception : out of bound.
Base class for every random variable.
Definition variable.h:79
Header of ContinuousVariable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:60
STL namespace.
std::ostream & operator<<(std::ostream &out, const TiXmlNode &base)
Definition tinyxml.cpp:1516