aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
continuousVariable_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief ContinuousVariable.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #include <sstream>
29 #include <utility>
30 
31 #include <agrum/agrum.h>
32 #include <agrum/tools/variables/continuousVariable.h>
33 
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 
36 namespace gum {
37 
38 
39  /// Default constructor
40  template < typename GUM_SCALAR >
41  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(const std::string& aName,
42  const std::string& aDesc,
43  GUM_SCALAR lower_bound,
44  GUM_SCALAR upper_bound) :
45  IContinuousVariable(aName, aDesc),
46  _lower_bound_(lower_bound), _upper_bound_(upper_bound) {
47  if (_lower_bound_ > _upper_bound_) { std::swap(_upper_bound_, _lower_bound_); }
48  GUM_CONSTRUCTOR(ContinuousVariable);
49  }
50 
51 
52  /// Copy Constructor.
53  template < typename GUM_SCALAR >
54  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
55  const ContinuousVariable< GUM_SCALAR >& from) :
56  IContinuousVariable(from),
57  _lower_bound_(from._lower_bound_), _upper_bound_(from._upper_bound_) {
58  GUM_CONS_CPY(ContinuousVariable);
59  }
60 
61 
62  /// generalized copy constructor
63  template < typename GUM_SCALAR >
64  template < typename TX_VAL >
65  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
66  const ContinuousVariable< TX_VAL >& from) :
67  IContinuousVariable(from),
68  _lower_bound_(GUM_SCALAR(from._lower_bound_)), _upper_bound_(GUM_SCALAR(from._upper_bound_)) {
69  GUM_CONS_CPY(ContinuousVariable);
70  }
71 
72 
73  /// move constructor
74  template < typename GUM_SCALAR >
75  INLINE
76  ContinuousVariable< GUM_SCALAR >::ContinuousVariable(ContinuousVariable< GUM_SCALAR >&& from) :
77  IContinuousVariable(std::move(from)),
78  _lower_bound_(from._lower_bound_), _upper_bound_(from._upper_bound_) {
79  GUM_CONS_MOV(ContinuousVariable);
80  }
81 
82 
83  /// destructor
84  template < typename GUM_SCALAR >
85  INLINE ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
86  GUM_DESTRUCTOR(ContinuousVariable);
87  }
88 
89 
90  /// Copy Factory.
91  template < typename GUM_SCALAR >
92  INLINE ContinuousVariable< GUM_SCALAR >* ContinuousVariable< GUM_SCALAR >::clone() const {
93  return new ContinuousVariable< GUM_SCALAR >(*this);
94  }
95 
96 
97  /// copy operator
98  template < typename GUM_SCALAR >
99  INLINE ContinuousVariable< GUM_SCALAR >&
100  ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< GUM_SCALAR >& from) {
101  IContinuousVariable::operator=(from);
102  _lower_bound_ = from._lower_bound_;
103  _upper_bound_ = from._upper_bound_;
104  return *this;
105  }
106 
107 
108  /// generalized copy operator
109  template < typename GUM_SCALAR >
110  template < typename TX_VAL >
111  INLINE ContinuousVariable< GUM_SCALAR >&
112  ContinuousVariable< GUM_SCALAR >::operator=(const ContinuousVariable< TX_VAL >& from) {
113  IContinuousVariable::operator=(from);
114  _lower_bound_ = GUM_SCALAR(from._lower_bound_);
115  _upper_bound_ = GUM_SCALAR(from._upper_bound_);
116  return *this;
117  }
118 
119 
120  /// move operator
121  template < typename GUM_SCALAR >
122  INLINE ContinuousVariable< GUM_SCALAR >&
123  ContinuousVariable< GUM_SCALAR >::operator=(ContinuousVariable< GUM_SCALAR >&& from) {
124  IContinuousVariable::operator=(std::move(from));
125  _lower_bound_ = from._lower_bound_;
126  _upper_bound_ = from._upper_bound_;
127  return *this;
128  }
129 
130 
131  /// returns the GUM_SCALAR corresponding to a string (unspecialized version)
132  template < typename GUM_SCALAR >
133  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::operator[](const std::string& str) const {
134  std::istringstream stream(str);
135  GUM_SCALAR value;
136  stream >> value;
137 
138  if (belongs(value))
139  return value;
140  else
141  GUM_ERROR(OutOfBounds, "the value does not delong to the domain of the variable")
142  }
143 
144 
145  /// returns the lower bound of the domain of the variable
146  template < typename GUM_SCALAR >
147  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
148  return _lower_bound_;
149  }
150 
151 
152  /// returns the lower bound of the domain of the variable as a double
153  template < typename GUM_SCALAR >
154  INLINE double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble() const {
155  return (double)_lower_bound_;
156  }
157 
158 
159  /// returns the upper bound of the domain of the variable
160  template < typename GUM_SCALAR >
161  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
162  return _upper_bound_;
163  }
164 
165 
166  /// returns the upper bound of the domain of the variable
167  template < typename GUM_SCALAR >
168  INLINE double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble() const {
169  return (double)_upper_bound_;
170  }
171 
172 
173  /// updates the lower bound of the domain of the variable
174  template < typename GUM_SCALAR >
175  INLINE void ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
176  if (new_bound <= _upper_bound_)
177  _lower_bound_ = new_bound;
178  else
179  GUM_ERROR(OutOfBounds, "the new lower bound would be higher than the upper bound")
180  }
181 
182 
183  /// updates the lower bound of the domain of the variable
184  template < typename GUM_SCALAR >
185  INLINE void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(const double new_bound) {
186  setLowerBound((GUM_SCALAR)new_bound);
187  }
188 
189 
190  /// updates the lower bound of the domain of the variable
191  template < typename GUM_SCALAR >
192  INLINE void ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
193  if (new_bound >= _lower_bound_)
194  _upper_bound_ = new_bound;
195  else
196  GUM_ERROR(OutOfBounds, "the new upper bound would be lower than the lower bound")
197  }
198 
199 
200  /// updates the lower bound of the domain of the variable
201  template < typename GUM_SCALAR >
202  INLINE void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(const double new_bound) {
203  setUpperBound((GUM_SCALAR)new_bound);
204  }
205 
206 
207  /// returns the type of the variable
208  template < typename GUM_SCALAR >
209  INLINE VarType ContinuousVariable< GUM_SCALAR >::varType() const {
210  return VarType::Continuous;
211  }
212 
213 
214  /// returns a string containing the value of the variable passed in argument
215  template < typename GUM_SCALAR >
216  INLINE std::string ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
217  if (belongs(value)) return std::to_string(value);
218  GUM_ERROR(OutOfBounds, "the value does not belong to the domain of the variable")
219  }
220 
221 
222  /// Returns true if the param belongs to the domain of the variable
223  template < typename GUM_SCALAR >
224  INLINE bool ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
225  return (value >= _lower_bound_) && (value <= _upper_bound_);
226  }
227 
228 
229  /// returns the domain of the variable as a string
230  template < typename GUM_SCALAR >
231  INLINE const std::string ContinuousVariable< GUM_SCALAR >::domain() const {
232  std::ostringstream stream;
233  stream << '[' << _lower_bound_ << ';' << _upper_bound_ << ']';
234  return stream.str();
235  }
236 
237 
238  /// string version of *this
239  template < typename GUM_SCALAR >
240  INLINE std::string ContinuousVariable< GUM_SCALAR >::toString() const {
241  std::string str(this->name());
242  str += domain();
243  return str;
244  }
245 
246 
247  /// string version of *this using description attribute instead of name.
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 
255 
256  /// for friendly displaying the content of the variable
257  template < typename GUM_SCALAR >
258  std::ostream& operator<<(std::ostream& stream, const ContinuousVariable< GUM_SCALAR >& var) {
259  return stream << var.toString();
260  }
261 
262 
263 } /* namespace gum */
264 
265 
266 #endif /* DOXYGEN_SHOULD_SKIP_THIS */