aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
continuousVariable_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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
42  ContinuousVariable< GUM_SCALAR >::ContinuousVariable(const std::string& aName,
43  const std::string& aDesc,
44  GUM_SCALAR lower_bound,
45  GUM_SCALAR upper_bound) :
46  IContinuousVariable(aName, aDesc),
47  lower_bound__(lower_bound), upper_bound__(upper_bound) {
48  if (lower_bound__ > upper_bound__) { std::swap(upper_bound__, lower_bound__); }
49  GUM_CONSTRUCTOR(ContinuousVariable);
50  }
51 
52 
53  /// Copy Constructor.
54  template < typename GUM_SCALAR >
55  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
56  const ContinuousVariable< GUM_SCALAR >& from) :
57  IContinuousVariable(from),
58  lower_bound__(from.lower_bound__), upper_bound__(from.upper_bound__) {
59  GUM_CONS_CPY(ContinuousVariable);
60  }
61 
62 
63  /// generalized copy constructor
64  template < typename GUM_SCALAR >
65  template < typename TX_VAL >
66  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
67  const ContinuousVariable< TX_VAL >& from) :
68  IContinuousVariable(from),
69  lower_bound__(GUM_SCALAR(from.lower_bound__)),
70  upper_bound__(GUM_SCALAR(from.upper_bound__)) {
71  GUM_CONS_CPY(ContinuousVariable);
72  }
73 
74 
75  /// move constructor
76  template < typename GUM_SCALAR >
77  INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
78  ContinuousVariable< GUM_SCALAR >&& from) :
79  IContinuousVariable(std::move(from)),
80  lower_bound__(from.lower_bound__), upper_bound__(from.upper_bound__) {
81  GUM_CONS_MOV(ContinuousVariable);
82  }
83 
84 
85  /// destructor
86  template < typename GUM_SCALAR >
87  INLINE ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
88  GUM_DESTRUCTOR(ContinuousVariable);
89  }
90 
91 
92  /// Copy Factory.
93  template < typename GUM_SCALAR >
94  INLINE ContinuousVariable< GUM_SCALAR >*
95  ContinuousVariable< GUM_SCALAR >::clone() const {
96  return new ContinuousVariable< GUM_SCALAR >(*this);
97  }
98 
99 
100  /// copy operator
101  template < typename GUM_SCALAR >
102  INLINE ContinuousVariable< GUM_SCALAR >&
103  ContinuousVariable< GUM_SCALAR >::operator=(
104  const ContinuousVariable< GUM_SCALAR >& from) {
105  IContinuousVariable::operator=(from);
106  lower_bound__ = from.lower_bound__;
107  upper_bound__ = from.upper_bound__;
108  return *this;
109  }
110 
111 
112  /// generalized copy operator
113  template < typename GUM_SCALAR >
114  template < typename TX_VAL >
115  INLINE ContinuousVariable< GUM_SCALAR >&
116  ContinuousVariable< GUM_SCALAR >::operator=(
117  const ContinuousVariable< TX_VAL >& from) {
118  IContinuousVariable::operator=(from);
119  lower_bound__ = GUM_SCALAR(from.lower_bound__);
120  upper_bound__ = GUM_SCALAR(from.upper_bound__);
121  return *this;
122  }
123 
124 
125  /// move operator
126  template < typename GUM_SCALAR >
127  INLINE ContinuousVariable< GUM_SCALAR >&
128  ContinuousVariable< GUM_SCALAR >::operator=(
129  ContinuousVariable< GUM_SCALAR >&& from) {
130  IContinuousVariable::operator=(std::move(from));
131  lower_bound__ = from.lower_bound__;
132  upper_bound__ = from.upper_bound__;
133  return *this;
134  }
135 
136 
137  /// returns the GUM_SCALAR corresponding to a string (unspecialized version)
138  template < typename GUM_SCALAR >
139  INLINE GUM_SCALAR
140  ContinuousVariable< GUM_SCALAR >::operator[](const std::string& str) const {
141  std::istringstream stream(str);
142  GUM_SCALAR value;
143  stream >> value;
144 
145  if (belongs(value))
146  return value;
147  else
148  GUM_ERROR(OutOfBounds,
149  "the value does not delong to the domain of the variable");
150  }
151 
152 
153  /// returns the lower bound of the domain of the variable
154  template < typename GUM_SCALAR >
155  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
156  return lower_bound__;
157  }
158 
159 
160  /// returns the lower bound of the domain of the variable as a double
161  template < typename GUM_SCALAR >
162  INLINE double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble() const {
163  return (double)lower_bound__;
164  }
165 
166 
167  /// returns the upper bound of the domain of the variable
168  template < typename GUM_SCALAR >
169  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
170  return upper_bound__;
171  }
172 
173 
174  /// returns the upper bound of the domain of the variable
175  template < typename GUM_SCALAR >
176  INLINE double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble() const {
177  return (double)upper_bound__;
178  }
179 
180 
181  /// updates the lower bound of the domain of the variable
182  template < typename GUM_SCALAR >
183  INLINE void
184  ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
185  if (new_bound <= upper_bound__)
186  lower_bound__ = new_bound;
187  else
188  GUM_ERROR(OutOfBounds,
189  "the new lower bound would be higher than the upper bound");
190  }
191 
192 
193  /// updates the lower bound of the domain of the variable
194  template < typename GUM_SCALAR >
195  INLINE void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(
196  const double new_bound) {
197  setLowerBound((GUM_SCALAR)new_bound);
198  }
199 
200 
201  /// updates the lower bound of the domain of the variable
202  template < typename GUM_SCALAR >
203  INLINE void
204  ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
205  if (new_bound >= lower_bound__)
206  upper_bound__ = new_bound;
207  else
208  GUM_ERROR(OutOfBounds,
209  "the new upper bound would be lower than the lower bound");
210  }
211 
212 
213  /// updates the lower bound of the domain of the variable
214  template < typename GUM_SCALAR >
215  INLINE void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(
216  const double new_bound) {
217  setUpperBound((GUM_SCALAR)new_bound);
218  }
219 
220 
221  /// returns the type of the variable
222  template < typename GUM_SCALAR >
223  INLINE VarType ContinuousVariable< GUM_SCALAR >::varType() const {
224  return VarType::Continuous;
225  }
226 
227 
228  /// returns a string containing the value of the variable passed in argument
229  template < typename GUM_SCALAR >
230  INLINE std::string
231  ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
232  if (belongs(value)) return std::to_string(value);
233  GUM_ERROR(OutOfBounds,
234  "the value does not belong to the domain of the variable");
235  }
236 
237 
238  /// Returns true if the param belongs to the domain of the variable
239  template < typename GUM_SCALAR >
240  INLINE bool
241  ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
242  return (value >= lower_bound__) && (value <= upper_bound__);
243  }
244 
245 
246  /// returns the domain of the variable as a string
247  template < typename GUM_SCALAR >
248  INLINE const std::string ContinuousVariable< GUM_SCALAR >::domain() const {
249  std::ostringstream stream;
250  stream << '[' << lower_bound__ << ';' << upper_bound__ << ']';
251  return stream.str();
252  }
253 
254 
255  /// string version of *this
256  template < typename GUM_SCALAR >
257  INLINE std::string ContinuousVariable< GUM_SCALAR >::toString() const {
258  std::string str(this->name());
259  str += domain();
260  return str;
261  }
262 
263 
264  /// string version of *this using description attribute instead of name.
265  template < typename GUM_SCALAR >
266  INLINE std::string
267  ContinuousVariable< GUM_SCALAR >::toStringWithDescription() const {
268  std::string str(this->description());
269  str += domain();
270  return str;
271  }
272 
273 
274  /// for friendly displaying the content of the variable
275  template < typename GUM_SCALAR >
276  std::ostream& operator<<(std::ostream& stream,
277  const ContinuousVariable< GUM_SCALAR >& var) {
278  return stream << var.toString();
279  }
280 
281 
282 } /* namespace gum */
283 
284 
285 #endif /* DOXYGEN_SHOULD_SKIP_THIS */