31 #include <agrum/agrum.h> 32 #include <agrum/tools/variables/continuousVariable.h> 34 #ifndef DOXYGEN_SHOULD_SKIP_THIS 40 template <
typename GUM_SCALAR >
41 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
const std::string& aName,
42 const std::string& aDesc,
43 GUM_SCALAR lower_bound,
44 GUM_SCALAR upper_bound) :
45 IContinuousVariable(aName, aDesc),
46 _lower_bound_(lower_bound), _upper_bound_(upper_bound) {
47 if (_lower_bound_ > _upper_bound_) { std::swap(_upper_bound_, _lower_bound_); }
48 GUM_CONSTRUCTOR(ContinuousVariable);
53 template <
typename GUM_SCALAR >
54 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
55 const ContinuousVariable< GUM_SCALAR >& from) :
56 IContinuousVariable(from),
57 _lower_bound_(from._lower_bound_), _upper_bound_(from._upper_bound_) {
58 GUM_CONS_CPY(ContinuousVariable);
63 template <
typename GUM_SCALAR >
64 template <
typename TX_VAL >
65 INLINE ContinuousVariable< GUM_SCALAR >::ContinuousVariable(
66 const ContinuousVariable< TX_VAL >& from) :
67 IContinuousVariable(from),
68 _lower_bound_(GUM_SCALAR(from._lower_bound_)), _upper_bound_(GUM_SCALAR(from._upper_bound_)) {
69 GUM_CONS_CPY(ContinuousVariable);
74 template <
typename GUM_SCALAR >
76 ContinuousVariable< GUM_SCALAR >::ContinuousVariable(ContinuousVariable< GUM_SCALAR >&& from) :
77 IContinuousVariable(std::move(from)),
78 _lower_bound_(from._lower_bound_), _upper_bound_(from._upper_bound_) {
79 GUM_CONS_MOV(ContinuousVariable);
84 template <
typename GUM_SCALAR >
85 INLINE ContinuousVariable< GUM_SCALAR >::~ContinuousVariable() {
86 GUM_DESTRUCTOR(ContinuousVariable);
91 template <
typename GUM_SCALAR >
92 INLINE ContinuousVariable< GUM_SCALAR >* ContinuousVariable< GUM_SCALAR >::clone()
const {
93 return new ContinuousVariable< GUM_SCALAR >(*
this);
98 template <
typename GUM_SCALAR >
99 INLINE ContinuousVariable< GUM_SCALAR >&
100 ContinuousVariable< GUM_SCALAR >::operator=(
const ContinuousVariable< GUM_SCALAR >& from) {
101 IContinuousVariable::operator=(from);
102 _lower_bound_ = from._lower_bound_;
103 _upper_bound_ = from._upper_bound_;
109 template <
typename GUM_SCALAR >
110 template <
typename TX_VAL >
111 INLINE ContinuousVariable< GUM_SCALAR >&
112 ContinuousVariable< GUM_SCALAR >::operator=(
const ContinuousVariable< TX_VAL >& from) {
113 IContinuousVariable::operator=(from);
114 _lower_bound_ = GUM_SCALAR(from._lower_bound_);
115 _upper_bound_ = GUM_SCALAR(from._upper_bound_);
121 template <
typename GUM_SCALAR >
122 INLINE ContinuousVariable< GUM_SCALAR >&
123 ContinuousVariable< GUM_SCALAR >::operator=(ContinuousVariable< GUM_SCALAR >&& from) {
124 IContinuousVariable::operator=(std::move(from));
125 _lower_bound_ = from._lower_bound_;
126 _upper_bound_ = from._upper_bound_;
132 template <
typename GUM_SCALAR >
133 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::operator[](
const std::string& str)
const {
134 std::istringstream stream(str);
141 GUM_ERROR(OutOfBounds,
"the value does not delong to the domain of the variable")
146 template <
typename GUM_SCALAR >
147 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::lowerBound()
const {
148 return _lower_bound_;
153 template <
typename GUM_SCALAR >
154 INLINE
double ContinuousVariable< GUM_SCALAR >::lowerBoundAsDouble()
const {
155 return (
double)_lower_bound_;
160 template <
typename GUM_SCALAR >
161 INLINE GUM_SCALAR ContinuousVariable< GUM_SCALAR >::upperBound()
const {
162 return _upper_bound_;
167 template <
typename GUM_SCALAR >
168 INLINE
double ContinuousVariable< GUM_SCALAR >::upperBoundAsDouble()
const {
169 return (
double)_upper_bound_;
174 template <
typename GUM_SCALAR >
175 INLINE
void ContinuousVariable< GUM_SCALAR >::setLowerBound(
const GUM_SCALAR& new_bound) {
176 if (new_bound <= _upper_bound_)
177 _lower_bound_ = new_bound;
179 GUM_ERROR(OutOfBounds,
"the new lower bound would be higher than the upper bound")
184 template <
typename GUM_SCALAR >
185 INLINE
void ContinuousVariable< GUM_SCALAR >::setLowerBoundFromDouble(
const double new_bound) {
186 setLowerBound((GUM_SCALAR)new_bound);
191 template <
typename GUM_SCALAR >
192 INLINE
void ContinuousVariable< GUM_SCALAR >::setUpperBound(
const GUM_SCALAR& new_bound) {
193 if (new_bound >= _lower_bound_)
194 _upper_bound_ = new_bound;
196 GUM_ERROR(OutOfBounds,
"the new upper bound would be lower than the lower bound")
201 template <
typename GUM_SCALAR >
202 INLINE
void ContinuousVariable< GUM_SCALAR >::setUpperBoundFromDouble(
const double new_bound) {
203 setUpperBound((GUM_SCALAR)new_bound);
208 template <
typename GUM_SCALAR >
209 INLINE VarType ContinuousVariable< GUM_SCALAR >::varType()
const {
210 return VarType::Continuous;
215 template <
typename GUM_SCALAR >
216 INLINE std::string ContinuousVariable< GUM_SCALAR >::label(
const GUM_SCALAR& value)
const {
217 if (belongs(value))
return std::to_string(value);
218 GUM_ERROR(OutOfBounds,
"the value does not belong to the domain of the variable")
223 template <
typename GUM_SCALAR >
224 INLINE
bool ContinuousVariable< GUM_SCALAR >::belongs(
const GUM_SCALAR& value)
const {
225 return (value >= _lower_bound_) && (value <= _upper_bound_);
230 template <
typename GUM_SCALAR >
231 INLINE
const std::string ContinuousVariable< GUM_SCALAR >::domain()
const {
232 std::ostringstream stream;
233 stream <<
'[' << _lower_bound_ <<
';' << _upper_bound_ <<
']';
239 template <
typename GUM_SCALAR >
240 INLINE std::string ContinuousVariable< GUM_SCALAR >::toString()
const {
241 std::string str(
this->name());
248 template <
typename GUM_SCALAR >
249 INLINE std::string ContinuousVariable< GUM_SCALAR >::toStringWithDescription()
const {
250 std::string str(
this->description());
257 template <
typename GUM_SCALAR >
258 std::ostream& operator<<(std::ostream& stream,
const ContinuousVariable< GUM_SCALAR >& var) {
259 return stream << var.toString();