aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
continuousVariable.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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
61  = -std::numeric_limits< GUM_SCALAR >::infinity(),
62  GUM_SCALAR upper_bound
63  = std::numeric_limits< GUM_SCALAR >::infinity());
64 
65  /// Copy Constructor.
66  ContinuousVariable(const ContinuousVariable< GUM_SCALAR >& from);
67 
68  /// generalized copy constructor
69  template < typename TX_VAL >
70  ContinuousVariable(const ContinuousVariable< TX_VAL >& from);
71 
72  /// move constructor
73  ContinuousVariable(ContinuousVariable< GUM_SCALAR >&& from);
74 
75  /// destructor
76  virtual ~ContinuousVariable();
77 
78  /// Copy Factory.
79  /// @return Returns a pointer on a new copy of this.
80  virtual ContinuousVariable< GUM_SCALAR >* clone() const;
81 
82  /// @}
83 
84 
85  // ##########################################################################
86  /// @name Operators
87  // ##########################################################################
88 
89  /// @{
90 
91  /// copy operator
92  ContinuousVariable< GUM_SCALAR >&
93  operator=(const ContinuousVariable< GUM_SCALAR >& from);
94 
95  /// generalized copy operator
96  template < typename TX_VAL >
97  ContinuousVariable< GUM_SCALAR >&
98  operator=(const ContinuousVariable< TX_VAL >& from);
99 
100  /// move operator
101  ContinuousVariable< GUM_SCALAR >&
102  operator=(ContinuousVariable< GUM_SCALAR >&& from);
103 
104  /// returns the T_VAL corresponding to a string
105  /** @throw OutOfBounds is raised if the value does not belong to the
106  * domain of the variable
107  * @throw TypeError if the string cannot be converted into a T_VAL */
108  GUM_SCALAR operator[](const std::string& str) const;
109 
110  /// @}
111 
112 
113  // ##########################################################################
114  /// @name Accessors / Modifiers
115  // ##########################################################################
116 
117  /// @{
118 
119  /// returns the lower bound of the domain of the variable
120  GUM_SCALAR lowerBound() const;
121 
122  /// returns the lower bound of the domain of the variable as a double
123  virtual double lowerBoundAsDouble() const;
124 
125  /// returns the upper bound of the domain of the variable
126  GUM_SCALAR upperBound() const;
127 
128  /// returns the upper bound of the domain of the variable as a double
129  virtual double upperBoundAsDouble() const;
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  void setLowerBound(const GUM_SCALAR& new_bound);
135 
136  /// updates the lower bound of the domain of the variable
137  /** @throw OutOfBounds is raised if the new bound is higher than the
138  * current upper bound. */
139  virtual void setLowerBoundFromDouble(const double 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  void setUpperBound(const GUM_SCALAR& new_bound);
145 
146  /// updates the lower bound of the domain of the variable
147  /** @throw OutOfBounds is raised if the new bound is lower than the
148  * current lower bound */
149  virtual void setUpperBoundFromDouble(const double new_bound);
150 
151  /// returns a string containing the value of the variable passed in argument
152  /**
153  * @param value the value of the variable we wish to return
154  * @throw OutOfBounds is raised if the value does not belong to the
155  * domain of the variable
156  */
157  virtual std::string label(const GUM_SCALAR& value) const;
158 
159  /// Returns true if the param belongs to the domain of the variable
160  bool belongs(const GUM_SCALAR& value) const;
161 
162  /// returns the domain of the variable as a string
163  virtual const std::string domain() const;
164 
165  /// returns the type of the variable
166  virtual VarType varType() const;
167 
168  /// string version of *this
169  std::string toString() const;
170 
171  /// string version of *this using description attribute instead of name.
172  std::string toStringWithDescription() const;
173 
174  /// @}
175 
176 
177  private:
178  // the lower bound.
179  GUM_SCALAR lower_bound__;
180 
181  // the upper bound.
182  GUM_SCALAR upper_bound__;
183 
184  template < typename TX_VAL >
185  friend class ContinuousVariable;
186  };
187 
188 
189  /// for friendly displaying the content of the variable
190  template < typename T_VAL >
191  std::ostream& operator<<(std::ostream&, const ContinuousVariable< T_VAL >&);
192 
193 
194  // specialized operator[] for real numbers
195  template <>
196  float ContinuousVariable< float >::operator[](const std::string& str) const;
197 
198  template <>
199  double ContinuousVariable< double >::operator[](const std::string& str) const;
200 
201 
202 } /* namespace gum */
203 
204 // always include the template implementation
205 #include <agrum/tools/variables/continuousVariable_tpl.h>
206 
207 #ifndef GUM_NO_INLINE
208 # include <agrum/tools/variables/continuousVariable_inl.h>
209 #endif /* GUM_NO_INLINE */
210 
211 #endif /* GUM_CONTINUOUS_VARIABLE_H */