aGrUM  0.16.0
continuousVariable_tpl.h
Go to the documentation of this file.
1 
29 #include <sstream>
30 #include <utility>
31 
32 #include <agrum/agrum.h>
34 
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 
37 namespace gum {
38 
39 
41  template < typename GUM_SCALAR >
42  INLINE
44  const std::string& aDesc,
45  GUM_SCALAR lower_bound,
46  GUM_SCALAR upper_bound) :
47  IContinuousVariable(aName, aDesc),
48  __lower_bound(lower_bound), __upper_bound(upper_bound) {
50  GUM_CONSTRUCTOR(ContinuousVariable);
51  }
52 
53 
55  template < typename GUM_SCALAR >
57  const ContinuousVariable< GUM_SCALAR >& from) :
58  IContinuousVariable(from),
60  GUM_CONS_CPY(ContinuousVariable);
61  }
62 
63 
65  template < typename GUM_SCALAR >
66  template < typename TX_VAL >
68  const ContinuousVariable< TX_VAL >& from) :
69  IContinuousVariable(from),
70  __lower_bound(GUM_SCALAR(from.__lower_bound)),
71  __upper_bound(GUM_SCALAR(from.__upper_bound)) {
72  GUM_CONS_CPY(ContinuousVariable);
73  }
74 
75 
77  template < typename GUM_SCALAR >
79  ContinuousVariable< GUM_SCALAR >&& from) :
80  IContinuousVariable(std::move(from)),
82  GUM_CONS_MOV(ContinuousVariable);
83  }
84 
85 
87  template < typename GUM_SCALAR >
89  GUM_DESTRUCTOR(ContinuousVariable);
90  }
91 
92 
94  template < typename GUM_SCALAR >
95  INLINE ContinuousVariable< GUM_SCALAR >*
97  return new ContinuousVariable< GUM_SCALAR >(*this);
98  }
99 
100 
102  template < typename GUM_SCALAR >
103  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
104  operator=(const ContinuousVariable< GUM_SCALAR >& from) {
106  __lower_bound = from.__lower_bound;
107  __upper_bound = from.__upper_bound;
108  return *this;
109  }
110 
111 
113  template < typename GUM_SCALAR >
114  template < typename TX_VAL >
115  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
116  operator=(const ContinuousVariable< TX_VAL >& from) {
118  __lower_bound = GUM_SCALAR(from.__lower_bound);
119  __upper_bound = GUM_SCALAR(from.__upper_bound);
120  return *this;
121  }
122 
123 
125  template < typename GUM_SCALAR >
126  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
127  operator=(ContinuousVariable< GUM_SCALAR >&& from) {
128  IContinuousVariable::operator=(std::move(from));
129  __lower_bound = from.__lower_bound;
130  __upper_bound = from.__upper_bound;
131  return *this;
132  }
133 
134 
136  template < typename GUM_SCALAR >
137  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::
138  operator[](const std::string& str) const {
139  std::istringstream stream(str);
140  GUM_SCALAR value;
141  stream >> value;
142 
143  if (belongs(value))
144  return value;
145  else
146  GUM_ERROR(OutOfBounds,
147  "the value does not delong to the domain of the variable");
148  }
149 
150 
152  template < typename GUM_SCALAR >
153  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
154  return __lower_bound;
155  }
156 
157 
159  template < typename GUM_SCALAR >
161  return (double)__lower_bound;
162  }
163 
164 
166  template < typename GUM_SCALAR >
167  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
168  return __upper_bound;
169  }
170 
171 
173  template < typename GUM_SCALAR >
175  return (double)__upper_bound;
176  }
177 
178 
180  template < typename GUM_SCALAR >
181  INLINE void
182  ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
183  if (new_bound <= __upper_bound)
184  __lower_bound = new_bound;
185  else
186  GUM_ERROR(OutOfBounds,
187  "the new lower bound would be higher than the upper bound");
188  }
189 
190 
192  template < typename GUM_SCALAR >
194  const double new_bound) {
195  setLowerBound((GUM_SCALAR)new_bound);
196  }
197 
198 
200  template < typename GUM_SCALAR >
201  INLINE void
202  ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
203  if (new_bound >= __lower_bound)
204  __upper_bound = new_bound;
205  else
206  GUM_ERROR(OutOfBounds,
207  "the new upper bound would be lower than the lower bound");
208  }
209 
210 
212  template < typename GUM_SCALAR >
214  const double new_bound) {
215  setUpperBound((GUM_SCALAR)new_bound);
216  }
217 
218 
220  template < typename GUM_SCALAR >
222  return VarType::Continuous;
223  }
224 
225 
227  template < typename GUM_SCALAR >
228  INLINE std::string
229  ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
230  if (belongs(value)) return std::to_string(value);
231  GUM_ERROR(OutOfBounds,
232  "the value does not belong to the domain of the variable");
233  }
234 
235 
237  template < typename GUM_SCALAR >
238  INLINE bool
239  ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
240  return (value >= __lower_bound) && (value <= __upper_bound);
241  }
242 
243 
245  template < typename GUM_SCALAR >
246  INLINE const std::string ContinuousVariable< GUM_SCALAR >::domain() const {
247  std::ostringstream stream;
248  stream << '[' << __lower_bound << ';' << __upper_bound << ']';
249  return stream.str();
250  }
251 
252 
254  template < typename GUM_SCALAR >
255  INLINE const std::string ContinuousVariable< GUM_SCALAR >::toString() const {
256  std::string str(this->name());
257  str += domain();
258  return str;
259  }
260 
261 
263  template < typename GUM_SCALAR >
264  INLINE const std::string
266  std::string str(this->description());
267  str += domain();
268  return str;
269  }
270 
271 
273  template < typename GUM_SCALAR >
274  std::ostream& operator<<(std::ostream& stream,
275  const ContinuousVariable< GUM_SCALAR >& var) {
276  return stream << var.toString();
277  }
278 
279 
280 } /* namespace gum */
281 
282 
283 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
const std::string toStringWithDescription() const
string version of *this using description attribute instead of name.
virtual std::string label(const GUM_SCALAR &value) const
returns a string containing the value of the variable passed in argument
IContinuousVariable & operator=(const IContinuousVariable &from)
copy operator
GUM_SCALAR operator[](const std::string &str) const
returns the T_VAL corresponding to a string
virtual double upperBoundAsDouble() const
returns the upper bound of the domain of the variable as a double
virtual double lowerBoundAsDouble() const
returns the lower bound of the domain of the variable as a double
STL namespace.
const std::string toString() const
string version of *this
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
ContinuousVariable< GUM_SCALAR > & operator=(const ContinuousVariable< GUM_SCALAR > &from)
copy operator
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual const std::string domain() const
returns the domain of the variable as a string
GUM_SCALAR lowerBound() const
returns the lower bound of the domain of the variable
virtual void setLowerBoundFromDouble(const double new_bound)
updates the lower bound of the domain of the variable
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
VarType
Definition: variable.h:41
IContinuousVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
virtual void setUpperBoundFromDouble(const double new_bound)
updates the lower bound of the domain of the variable
bool belongs(const GUM_SCALAR &value) const
Returns true if the param belongs to the domain of the variable.
const std::string & description() const
returns the description of the variable
void setUpperBound(const GUM_SCALAR &new_bound)
updates the lower bound of the domain of the variable
void setLowerBound(const GUM_SCALAR &new_bound)
updates the lower bound of the domain of the variable
virtual ContinuousVariable< GUM_SCALAR > * clone() const
Copy Factory.
const std::string & name() const
returns the name of the variable
virtual VarType varType() const
returns the type of the variable
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
GUM_SCALAR upperBound() const
returns the upper bound of the domain of the variable
virtual ~ContinuousVariable()
destructor