aGrUM  0.14.2
continuousVariable_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #include <sstream>
27 #include <utility>
28 
29 #include <agrum/agrum.h>
31 
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 
34 namespace gum {
35 
36 
38  template < typename GUM_SCALAR >
39  INLINE
41  const std::string& aDesc,
42  GUM_SCALAR lower_bound,
43  GUM_SCALAR upper_bound) :
44  IContinuousVariable(aName, aDesc),
45  __lower_bound(lower_bound), __upper_bound(upper_bound) {
47  GUM_CONSTRUCTOR(ContinuousVariable);
48  }
49 
50 
52  template < typename GUM_SCALAR >
54  const ContinuousVariable< GUM_SCALAR >& from) :
55  IContinuousVariable(from),
57  GUM_CONS_CPY(ContinuousVariable);
58  }
59 
60 
62  template < typename GUM_SCALAR >
63  template < typename TX_VAL >
65  const ContinuousVariable< TX_VAL >& from) :
66  IContinuousVariable(from),
67  __lower_bound(GUM_SCALAR(from.__lower_bound)),
68  __upper_bound(GUM_SCALAR(from.__upper_bound)) {
69  GUM_CONS_CPY(ContinuousVariable);
70  }
71 
72 
74  template < typename GUM_SCALAR >
76  ContinuousVariable< GUM_SCALAR >&& from) :
77  IContinuousVariable(std::move(from)),
79  GUM_CONS_MOV(ContinuousVariable);
80  }
81 
82 
84  template < typename GUM_SCALAR >
86  GUM_DESTRUCTOR(ContinuousVariable);
87  }
88 
89 
91  template < typename GUM_SCALAR >
92  INLINE ContinuousVariable< GUM_SCALAR >*
94  return new ContinuousVariable< GUM_SCALAR >(*this);
95  }
96 
97 
99  template < typename GUM_SCALAR >
100  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
101  operator=(const ContinuousVariable< GUM_SCALAR >& from) {
103  __lower_bound = from.__lower_bound;
104  __upper_bound = from.__upper_bound;
105  return *this;
106  }
107 
108 
110  template < typename GUM_SCALAR >
111  template < typename TX_VAL >
112  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
113  operator=(const ContinuousVariable< TX_VAL >& from) {
115  __lower_bound = GUM_SCALAR(from.__lower_bound);
116  __upper_bound = GUM_SCALAR(from.__upper_bound);
117  return *this;
118  }
119 
120 
122  template < typename GUM_SCALAR >
123  INLINE ContinuousVariable< GUM_SCALAR >& ContinuousVariable< GUM_SCALAR >::
124  operator=(ContinuousVariable< GUM_SCALAR >&& from) {
125  IContinuousVariable::operator=(std::move(from));
126  __lower_bound = from.__lower_bound;
127  __upper_bound = from.__upper_bound;
128  return *this;
129  }
130 
131 
133  template < typename GUM_SCALAR >
134  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::
135  operator[](const std::string& str) const {
136  std::istringstream stream(str);
137  GUM_SCALAR value;
138  stream >> value;
139 
140  if (belongs(value))
141  return value;
142  else
143  GUM_ERROR(OutOfBounds,
144  "the value does not delong to the domain of the variable");
145  }
146 
147 
149  template < typename GUM_SCALAR >
150  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
151  return __lower_bound;
152  }
153 
154 
156  template < typename GUM_SCALAR >
158  return (double)__lower_bound;
159  }
160 
161 
163  template < typename GUM_SCALAR >
164  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
165  return __upper_bound;
166  }
167 
168 
170  template < typename GUM_SCALAR >
172  return (double)__upper_bound;
173  }
174 
175 
177  template < typename GUM_SCALAR >
178  INLINE void
179  ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
180  if (new_bound <= __upper_bound)
181  __lower_bound = new_bound;
182  else
183  GUM_ERROR(OutOfBounds,
184  "the new lower bound would be higher than the upper bound");
185  }
186 
187 
189  template < typename GUM_SCALAR >
191  const double new_bound) {
192  setLowerBound((GUM_SCALAR)new_bound);
193  }
194 
195 
197  template < typename GUM_SCALAR >
198  INLINE void
199  ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
200  if (new_bound >= __lower_bound)
201  __upper_bound = new_bound;
202  else
203  GUM_ERROR(OutOfBounds,
204  "the new upper bound would be lower than the lower bound");
205  }
206 
207 
209  template < typename GUM_SCALAR >
211  const double new_bound) {
212  setUpperBound((GUM_SCALAR)new_bound);
213  }
214 
215 
217  template < typename GUM_SCALAR >
219  return VarType::Continuous;
220  }
221 
222 
224  template < typename GUM_SCALAR >
225  INLINE std::string
226  ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
227  if (belongs(value)) return std::to_string(value);
228  GUM_ERROR(OutOfBounds,
229  "the value does not belong to the domain of the variable");
230  }
231 
232 
234  template < typename GUM_SCALAR >
235  INLINE bool
236  ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
237  return (value >= __lower_bound) && (value <= __upper_bound);
238  }
239 
240 
242  template < typename GUM_SCALAR >
243  INLINE const std::string ContinuousVariable< GUM_SCALAR >::domain() const {
244  std::ostringstream stream;
245  stream << '[' << __lower_bound << ';' << __upper_bound << ']';
246  return stream.str();
247  }
248 
249 
251  template < typename GUM_SCALAR >
252  INLINE const std::string ContinuousVariable< GUM_SCALAR >::toString() const {
253  std::string str(this->name());
254  str += domain();
255  return str;
256  }
257 
258 
260  template < typename GUM_SCALAR >
261  INLINE const std::string
263  std::string str(this->description());
264  str += domain();
265  return str;
266  }
267 
268 
270  template < typename GUM_SCALAR >
271  std::ostream& operator<<(std::ostream& stream,
272  const ContinuousVariable< GUM_SCALAR >& var) {
273  return stream << var.toString();
274  }
275 
276 
277 } /* namespace gum */
278 
279 
280 #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
Header of ContinuousVariable.
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
gum is the global namespace for all aGrUM entities
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:583
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
VarType
Definition: variable.h:38
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:52
GUM_SCALAR upperBound() const
returns the upper bound of the domain of the variable
virtual ~ContinuousVariable()
destructor