31 #include <agrum/agrum.h> 32 #include <agrum/tools/variables/continuousVariable.h> 34 #ifndef DOXYGEN_SHOULD_SKIP_THIS 40 template <
typename GUM_SCALAR >
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);
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);
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);
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);
86 template <
typename GUM_SCALAR >
87 INLINE ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
88 GUM_DESTRUCTOR(ContinuousVariable);
93 template <
typename GUM_SCALAR >
94 INLINE ContinuousVariable< GUM_SCALAR >*
95 ContinuousVariable< GUM_SCALAR >::clone()
const {
96 return new ContinuousVariable< GUM_SCALAR >(*
this);
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__;
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__);
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__;
138 template <
typename GUM_SCALAR >
140 ContinuousVariable< GUM_SCALAR >::operator[](
const std::string& str)
const {
141 std::istringstream stream(str);
148 GUM_ERROR(OutOfBounds,
149 "the value does not delong to the domain of the variable");
154 template <
typename GUM_SCALAR >
155 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound()
const {
156 return lower_bound__;
161 template <
typename GUM_SCALAR >
162 INLINE
double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble()
const {
163 return (
double)lower_bound__;
168 template <
typename GUM_SCALAR >
169 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound()
const {
170 return upper_bound__;
175 template <
typename GUM_SCALAR >
176 INLINE
double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble()
const {
177 return (
double)upper_bound__;
182 template <
typename GUM_SCALAR >
184 ContinuousVariable< GUM_SCALAR >::setLowerBound(
const GUM_SCALAR& new_bound) {
185 if (new_bound <= upper_bound__)
186 lower_bound__ = new_bound;
188 GUM_ERROR(OutOfBounds,
189 "the new lower bound would be higher than the upper bound");
194 template <
typename GUM_SCALAR >
195 INLINE
void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(
196 const double new_bound) {
197 setLowerBound((GUM_SCALAR)new_bound);
202 template <
typename GUM_SCALAR >
204 ContinuousVariable< GUM_SCALAR >::setUpperBound(
const GUM_SCALAR& new_bound) {
205 if (new_bound >= lower_bound__)
206 upper_bound__ = new_bound;
208 GUM_ERROR(OutOfBounds,
209 "the new upper bound would be lower than the lower bound");
214 template <
typename GUM_SCALAR >
215 INLINE
void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(
216 const double new_bound) {
217 setUpperBound((GUM_SCALAR)new_bound);
222 template <
typename GUM_SCALAR >
223 INLINE VarType ContinuousVariable< GUM_SCALAR >::varType()
const {
224 return VarType::Continuous;
229 template <
typename GUM_SCALAR >
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");
239 template <
typename GUM_SCALAR >
241 ContinuousVariable< GUM_SCALAR >::belongs(
const GUM_SCALAR& value)
const {
242 return (value >= lower_bound__) && (value <= upper_bound__);
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__ <<
']';
256 template <
typename GUM_SCALAR >
257 INLINE std::string ContinuousVariable< GUM_SCALAR >::toString()
const {
258 std::string str(
this->name());
265 template <
typename GUM_SCALAR >
267 ContinuousVariable< GUM_SCALAR >::toStringWithDescription()
const {
268 std::string str(
this->description());
275 template <
typename GUM_SCALAR >
276 std::ostream& operator<<(std::ostream& stream,
277 const ContinuousVariable< GUM_SCALAR >& var) {
278 return stream << var.toString();