aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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 >&
105  const ContinuousVariable< GUM_SCALAR >& from) {
107  __lower_bound = from.__lower_bound;
108  __upper_bound = from.__upper_bound;
109  return *this;
110  }
111 
112 
114  template < typename GUM_SCALAR >
115  template < typename TX_VAL >
116  INLINE ContinuousVariable< GUM_SCALAR >&
118  const ContinuousVariable< TX_VAL >& from) {
120  __lower_bound = GUM_SCALAR(from.__lower_bound);
121  __upper_bound = GUM_SCALAR(from.__upper_bound);
122  return *this;
123  }
124 
125 
127  template < typename GUM_SCALAR >
128  INLINE ContinuousVariable< GUM_SCALAR >&
130  ContinuousVariable< GUM_SCALAR >&& from) {
131  IContinuousVariable::operator=(std::move(from));
132  __lower_bound = from.__lower_bound;
133  __upper_bound = from.__upper_bound;
134  return *this;
135  }
136 
137 
139  template < typename GUM_SCALAR >
140  INLINE GUM_SCALAR
141  ContinuousVariable< GUM_SCALAR >::operator[](const std::string& str) const {
142  std::istringstream stream(str);
143  GUM_SCALAR value;
144  stream >> value;
145 
146  if (belongs(value))
147  return value;
148  else
149  GUM_ERROR(OutOfBounds,
150  "the value does not delong to the domain of the variable");
151  }
152 
153 
155  template < typename GUM_SCALAR >
156  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound() const {
157  return __lower_bound;
158  }
159 
160 
162  template < typename GUM_SCALAR >
164  return (double)__lower_bound;
165  }
166 
167 
169  template < typename GUM_SCALAR >
170  INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound() const {
171  return __upper_bound;
172  }
173 
174 
176  template < typename GUM_SCALAR >
178  return (double)__upper_bound;
179  }
180 
181 
183  template < typename GUM_SCALAR >
184  INLINE void
185  ContinuousVariable< GUM_SCALAR >::setLowerBound(const GUM_SCALAR& new_bound) {
186  if (new_bound <= __upper_bound)
187  __lower_bound = new_bound;
188  else
189  GUM_ERROR(OutOfBounds,
190  "the new lower bound would be higher than the upper bound");
191  }
192 
193 
195  template < typename GUM_SCALAR >
197  const double new_bound) {
198  setLowerBound((GUM_SCALAR)new_bound);
199  }
200 
201 
203  template < typename GUM_SCALAR >
204  INLINE void
205  ContinuousVariable< GUM_SCALAR >::setUpperBound(const GUM_SCALAR& new_bound) {
206  if (new_bound >= __lower_bound)
207  __upper_bound = new_bound;
208  else
209  GUM_ERROR(OutOfBounds,
210  "the new upper bound would be lower than the lower bound");
211  }
212 
213 
215  template < typename GUM_SCALAR >
217  const double new_bound) {
218  setUpperBound((GUM_SCALAR)new_bound);
219  }
220 
221 
223  template < typename GUM_SCALAR >
225  return VarType::Continuous;
226  }
227 
228 
230  template < typename GUM_SCALAR >
231  INLINE std::string
232  ContinuousVariable< GUM_SCALAR >::label(const GUM_SCALAR& value) const {
233  if (belongs(value)) return std::to_string(value);
234  GUM_ERROR(OutOfBounds,
235  "the value does not belong to the domain of the variable");
236  }
237 
238 
240  template < typename GUM_SCALAR >
241  INLINE bool
242  ContinuousVariable< GUM_SCALAR >::belongs(const GUM_SCALAR& value) const {
243  return (value >= __lower_bound) && (value <= __upper_bound);
244  }
245 
246 
248  template < typename GUM_SCALAR >
249  INLINE const std::string ContinuousVariable< GUM_SCALAR >::domain() const {
250  std::ostringstream stream;
251  stream << '[' << __lower_bound << ';' << __upper_bound << ']';
252  return stream.str();
253  }
254 
255 
257  template < typename GUM_SCALAR >
258  INLINE const std::string ContinuousVariable< GUM_SCALAR >::toString() const {
259  std::string str(this->name());
260  str += domain();
261  return str;
262  }
263 
264 
266  template < typename GUM_SCALAR >
267  INLINE const std::string
269  std::string str(this->description());
270  str += domain();
271  return str;
272  }
273 
274 
276  template < typename GUM_SCALAR >
277  std::ostream& operator<<(std::ostream& stream,
278  const ContinuousVariable< GUM_SCALAR >& var) {
279  return stream << var.toString();
280  }
281 
282 
283 } /* namespace gum */
284 
285 
286 #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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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:626
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