aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
50#include <sstream>
51#include <utility>
52
53#include <agrum/agrum.h>
54
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60
61
63 template < GUM_Numeric GUM_SCALAR >
65 std::string_view aDesc,
66 GUM_SCALAR lower_bound,
67 GUM_SCALAR upper_bound) :
68 IContinuousVariable(aName, aDesc), _lower_bound_(lower_bound), _upper_bound_(upper_bound) {
69 if (_lower_bound_ > _upper_bound_) { std::swap(_upper_bound_, _lower_bound_); }
70 GUM_CONSTRUCTOR(ContinuousVariable);
71 }
72
74 template < GUM_Numeric GUM_SCALAR >
75 ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
76 const ContinuousVariable< GUM_SCALAR >& from) :
77 IContinuousVariable(from), _lower_bound_(from._lower_bound_),
78 _upper_bound_(from._upper_bound_) {
79 GUM_CONS_CPY(ContinuousVariable);
80 }
81
83 template < GUM_Numeric GUM_SCALAR >
84 template < typename TX_VAL >
85 ContinuousVariable< GUM_SCALAR >::ContinuousVariable(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 < GUM_Numeric GUM_SCALAR >
93 ContinuousVariable< GUM_SCALAR >::ContinuousVariable(ContinuousVariable< GUM_SCALAR >&& from) :
94 IContinuousVariable(std::move(from)), _lower_bound_(from._lower_bound_),
95 _upper_bound_(from._upper_bound_) {
96 GUM_CONS_MOV(ContinuousVariable);
97 }
98
100 template < GUM_Numeric GUM_SCALAR >
101 ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
102 GUM_DESTRUCTOR(ContinuousVariable);
103 }
104
106 template < GUM_Numeric GUM_SCALAR >
107 ContinuousVariable< GUM_SCALAR >* ContinuousVariable< GUM_SCALAR >::clone() const {
108 return new ContinuousVariable< GUM_SCALAR >(*this);
109 }
110
112 template < GUM_Numeric GUM_SCALAR >
113 ContinuousVariable< GUM_SCALAR >&
114 ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< GUM_SCALAR >& from) {
115 IContinuousVariable::operator=(from);
116 _lower_bound_ = from._lower_bound_;
117 _upper_bound_ = from._upper_bound_;
118 return *this;
119 }
120
122 template < GUM_Numeric GUM_SCALAR >
123 template < typename TX_VAL >
124 ContinuousVariable< GUM_SCALAR >&
125 ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< TX_VAL >& from) {
126 IContinuousVariable::operator=(from);
127 _lower_bound_ = GUM_SCALAR(from._lower_bound_);
128 _upper_bound_ = GUM_SCALAR(from._upper_bound_);
129 return *this;
130 }
131
133 template < GUM_Numeric GUM_SCALAR >
134 ContinuousVariable< GUM_SCALAR >&
135 ContinuousVariable< GUM_SCALAR >::operator=(ContinuousVariable< GUM_SCALAR >&& from) {
136 IContinuousVariable::operator=(std::move(from));
137 _lower_bound_ = from._lower_bound_;
138 _upper_bound_ = from._upper_bound_;
139 return *this;
140 }
141
143 template < GUM_Numeric GUM_SCALAR >
144 GUM_SCALAR ContinuousVariable< GUM_SCALAR >::operator[](std::string_view str) const {
145 std::istringstream stream{std::string(str)};
146 GUM_SCALAR value;
147 stream >> value;
148
149 if (belongs(value)) return value;
150 else GUM_ERROR(OutOfBounds, "the value does not delong to the domain of the variable")
151 }
152
154 template < GUM_Numeric GUM_SCALAR >
155 GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
156 return _lower_bound_;
157 }
158
160 template < GUM_Numeric GUM_SCALAR >
161 double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble() const {
162 return (double)_lower_bound_;
163 }
164
166 template < GUM_Numeric GUM_SCALAR >
167 GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
168 return _upper_bound_;
169 }
170
172 template < GUM_Numeric GUM_SCALAR >
173 double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble() const {
174 return (double)_upper_bound_;
175 }
176
178 template < GUM_Numeric GUM_SCALAR >
179 void ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
180 if (new_bound <= _upper_bound_) _lower_bound_ = new_bound;
181 else GUM_ERROR(OutOfBounds, "the new lower bound would be higher than the upper bound")
182 }
183
185 template < GUM_Numeric GUM_SCALAR >
186 void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(const double new_bound) {
187 setLowerBound((GUM_SCALAR)new_bound);
188 }
189
191 template < GUM_Numeric GUM_SCALAR >
192 void ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
193 if (new_bound >= _lower_bound_) _upper_bound_ = new_bound;
194 else GUM_ERROR(OutOfBounds, "the new upper bound would be lower than the lower bound")
195 }
196
198 template < GUM_Numeric GUM_SCALAR >
199 void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(const double new_bound) {
200 setUpperBound((GUM_SCALAR)new_bound);
201 }
202
204 template < GUM_Numeric GUM_SCALAR >
205 VarType ContinuousVariable< GUM_SCALAR >::varType() const {
206 return VarType::CONTINUOUS;
207 }
208
210 template < GUM_Numeric GUM_SCALAR >
211 std::string ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
212 if (belongs(value)) return std::to_string(value);
213 GUM_ERROR(OutOfBounds, "the value does not belong to the domain of the variable")
214 }
215
217 template < GUM_Numeric GUM_SCALAR >
218 bool ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
219 return (value >= _lower_bound_) && (value <= _upper_bound_);
220 }
221
223 template < GUM_Numeric GUM_SCALAR >
224 std::string ContinuousVariable< GUM_SCALAR >::domain() const {
225 return std::format("[{};{}]", _lower_bound_, _upper_bound_);
226 }
227
229 template < GUM_Numeric GUM_SCALAR >
230 bool ContinuousVariable< GUM_SCALAR >::_checkSameDomain_(const gum::Variable& aRV) const {
231 // we can assume that aRV is a ContinuousVariable
232 const auto& cv = static_cast< const ContinuousVariable< GUM_SCALAR >& >(aRV);
233 return cv._lower_bound_ == _lower_bound_ && cv._upper_bound_ == _upper_bound_;
234 }
235
237 template < GUM_Numeric GUM_SCALAR >
238 std::string ContinuousVariable< GUM_SCALAR >::toString() const {
239 std::string str(this->name());
240 str += domain();
241 return str;
242 }
243
245 template < GUM_Numeric GUM_SCALAR >
246 std::string ContinuousVariable< GUM_SCALAR >::toStringWithDescription() const {
247 std::string str(this->description());
248 str += domain();
249 return str;
250 }
251
253 template < GUM_Numeric GUM_SCALAR >
254 std::ostream& operator<<(std::ostream& stream, const ContinuousVariable< GUM_SCALAR >& var) {
255 return stream << var.toString();
256 }
257
258
259} /* namespace gum */
260
261
262#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:81
Header of ContinuousVariable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:62
STL namespace.
std::ostream & operator<<(std::ostream &out, const TiXmlNode &base)
Definition tinyxml.cpp:1516