aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
continuousVariable.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Header of ContinuousVariable.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_CONTINUOUS_VARIABLE_H
29 #define GUM_CONTINUOUS_VARIABLE_H
30 
31 #include <iostream>
32 #include <limits>
33 #include <sstream>
34 #include <string>
35 
36 #include <agrum/agrum.h>
37 #include <agrum/tools/variables/IContinuousVariable.h>
38 
39 namespace gum {
40 
41  /**
42  * @class ContinuousVariable
43  * @brief Defines a continuous random variable.
44  */
45  template < typename GUM_SCALAR = float >
46  class ContinuousVariable: public IContinuousVariable {
47  public:
48  // ##########################################################################
49  /// @name Constructors / Destructors
50  // ##########################################################################
51 
52  /// @{
53 
54  /// Default constructor
55  /** It is possible to create ContinuousVariable with lower_bound > upper_bound.
56  * In this case, the min and the max are swapped.
57  * By default, the range of the variable is (-inf,+inf). */
58  ContinuousVariable(const std::string& aName,
59  const std::string& aDesc,
60  GUM_SCALAR lower_bound = -std::numeric_limits< GUM_SCALAR >::infinity(),
61  GUM_SCALAR upper_bound = std::numeric_limits< GUM_SCALAR >::infinity());
62 
63  /// Copy Constructor.
64  ContinuousVariable(const ContinuousVariable< GUM_SCALAR >& from);
65 
66  /// generalized copy constructor
67  template < typename TX_VAL >
68  ContinuousVariable(const ContinuousVariable< TX_VAL >& from);
69 
70  /// move constructor
71  ContinuousVariable(ContinuousVariable< GUM_SCALAR >&& from);
72 
73  /// destructor
74  virtual ~ContinuousVariable();
75 
76  /// Copy Factory.
77  /// @return Returns a pointer on a new copy of this.
78  virtual ContinuousVariable< GUM_SCALAR >* clone() const;
79 
80  /// @}
81 
82 
83  // ##########################################################################
84  /// @name Operators
85  // ##########################################################################
86 
87  /// @{
88 
89  /// copy operator
90  ContinuousVariable< GUM_SCALAR >& operator=(const ContinuousVariable< GUM_SCALAR >& from);
91 
92  /// generalized copy operator
93  template < typename TX_VAL >
94  ContinuousVariable< GUM_SCALAR >& operator=(const ContinuousVariable< TX_VAL >& from);
95 
96  /// move operator
97  ContinuousVariable< GUM_SCALAR >& operator=(ContinuousVariable< GUM_SCALAR >&& from);
98 
99  /// returns the T_VAL corresponding to a string
100  /** @throw OutOfBounds is raised if the value does not belong to the
101  * domain of the variable
102  * @throw TypeError if the string cannot be converted into a T_VAL */
103  GUM_SCALAR operator[](const std::string& str) const;
104 
105  /// @}
106 
107 
108  // ##########################################################################
109  /// @name Accessors / Modifiers
110  // ##########################################################################
111 
112  /// @{
113 
114  /// returns the lower bound of the domain of the variable
115  GUM_SCALAR lowerBound() const;
116 
117  /// returns the lower bound of the domain of the variable as a double
118  virtual double lowerBoundAsDouble() const;
119 
120  /// returns the upper bound of the domain of the variable
121  GUM_SCALAR upperBound() const;
122 
123  /// returns the upper bound of the domain of the variable as a double
124  virtual double upperBoundAsDouble() const;
125 
126  /// updates the lower bound of the domain of the variable
127  /** @throw OutOfBounds is raised if the new bound is higher than the
128  * current upper bound. */
129  void setLowerBound(const GUM_SCALAR& new_bound);
130 
131  /// updates the lower bound of the domain of the variable
132  /** @throw OutOfBounds is raised if the new bound is higher than the
133  * current upper bound. */
134  virtual void setLowerBoundFromDouble(const double new_bound);
135 
136  /// updates the lower bound of the domain of the variable
137  /** @throw OutOfBounds is raised if the new bound is lower than the
138  * current lower bound */
139  void setUpperBound(const GUM_SCALAR& new_bound);
140 
141  /// updates the lower bound of the domain of the variable
142  /** @throw OutOfBounds is raised if the new bound is lower than the
143  * current lower bound */
144  virtual void setUpperBoundFromDouble(const double new_bound);
145 
146  /// returns a string containing the value of the variable passed in argument
147  /**
148  * @param value the value of the variable we wish to return
149  * @throw OutOfBounds is raised if the value does not belong to the
150  * domain of the variable
151  */
152  virtual std::string label(const GUM_SCALAR& value) const;
153 
154  /// Returns true if the param belongs to the domain of the variable
155  bool belongs(const GUM_SCALAR& value) const;
156 
157  /// returns the domain of the variable as a string
158  virtual const std::string domain() const;
159 
160  /// returns the type of the variable
161  virtual VarType varType() const;
162 
163  /// string version of *this
164  std::string toString() const;
165 
166  /// string version of *this using description attribute instead of name.
167  std::string toStringWithDescription() const;
168 
169  /// @}
170 
171 
172  private:
173  // the lower bound.
174  GUM_SCALAR _lower_bound_;
175 
176  // the upper bound.
177  GUM_SCALAR _upper_bound_;
178 
179  template < typename TX_VAL >
180  friend class ContinuousVariable;
181  };
182 
183 
184  /// for friendly displaying the content of the variable
185  template < typename T_VAL >
186  std::ostream& operator<<(std::ostream&, const ContinuousVariable< T_VAL >&);
187 
188 
189  // specialized operator[] for real numbers
190  template <>
191  float ContinuousVariable< float >::operator[](const std::string& str) const;
192 
193  template <>
194  double ContinuousVariable< double >::operator[](const std::string& str) const;
195 
196 
197 } /* namespace gum */
198 
199 // always include the template implementation
200 #include <agrum/tools/variables/continuousVariable_tpl.h>
201 
202 #ifndef GUM_NO_INLINE
203 # include <agrum/tools/variables/continuousVariable_inl.h>
204 #endif /* GUM_NO_INLINE */
205 
206 #endif /* GUM_CONTINUOUS_VARIABLE_H */