aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimArray.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 the MultiDimArray class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 #ifndef GUM_MULTI_DIM_ARRAY_H
29 #define GUM_MULTI_DIM_ARRAY_H
30 
31 #include <agrum/agrum.h>
32 #include <agrum/tools/multidim/implementations/multiDimWithOffset.h>
33 #include <vector>
34 
35 namespace gum {
36 
37  template < typename GUM_SCALAR >
39 
40  // ===========================================================================
41  // === GUM_MULTI_DIM_ARRAY ===
42  // ===========================================================================
43  /**
44  * @class MultiDimArray
45  * @headerfile multiDimArray.h <agrum/tools/multidim/multiDimArray.h>
46  * @ingroup multidim_group
47  *
48  * @brief Multidimensional matrix stored as an array in memory.
49  * @tparam GUM_SCALAR The type of the scalars stroed in the multidimensional
50  * table.
51  */
52  template < typename GUM_SCALAR >
54  public:
55  template < typename GUM_SCALARBIS >
56  friend class MultiDimBijArray;
57  // =========================================================================
58  /// @name Constructors, destructors and copy
59  // =========================================================================
60  /// @{
61 
62  /**
63  * @brief Default constructor.
64  *
65  * Creates an empty null dimensional matrix
66  */
67  MultiDimArray();
68 
69  /**
70  * @brief Copy constructor.
71  *
72  * The newly created matrix contains the same variables and the same values
73  * as src, but no instantiation is associated to it.
74  *
75  * @param src The MultiDimArray to copy.
76  */
77  MultiDimArray(const MultiDimArray< GUM_SCALAR >& src);
78 
79  /**
80  * @todo : operator= for MultiDimArray
81  *
82  * @brief Copy operator.
83  * @param src The MultiDimArray to copy.
84  * @return Returns this MultiDimArray.
85  MultiDimArray<GUM_SCALAR>&
86  operator=( const MultiDimArray<GUM_SCALAR>& src );
87  */
88 
89  /**
90  * @brief Class destructor.
91  *
92  * Note that, when the multidimensional array is removed src memory, its
93  * variables are not removed as well.
94  */
95  virtual ~MultiDimArray();
96 
97  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const;
98 
99  /// }
100  // =========================================================================
101  /// @name Inherited methods
102  // =========================================================================
103  /// @{
104 
105  /**
106  * @brief Copy from a other MultiDimContainer.
107  */
108  virtual void copyFrom(const MultiDimContainer< GUM_SCALAR >& src) const;
109 
110  /**
111  * @brief Apply a function on every element of the container
112  * @param f the function to apply
113  */
114  virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const;
115 
116  /**
117  * @brief compute lfold for this container
118  * @param f the function to apply
119  * @param base the initial value
120  */
121  virtual GUM_SCALAR
122  reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
123  GUM_SCALAR base) const;
124 
125  /**
126  * @brief Adds a variable.
127  */
128  virtual void add(const DiscreteVariable& v);
129 
130  /**
131  * @brief Removes a variable.
132  */
133  virtual void erase(const DiscreteVariable& v);
134 
135  /**
136  * @brief Returns the real size of this MultiDimArray.
137  */
138  virtual Size realSize() const;
139 
140  /**
141  * @brief Fills the MultiDimArray with the given value.
142  */
143  virtual void fill(const GUM_SCALAR& d) const;
144 
145  /**
146  * @brief Returns the MultiDimArray name.
147  */
148  virtual const std::string& name() const;
149 
150  /// @}
151  // =========================================================================
152  /// @name Accessors / Modifiers
153  // =========================================================================
154  /// @{
155 
156  /**
157  * @brief Returns the element stored in the multidimArray at a given
158  * offset.
159  *
160  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
161  * variables in the multidim, and let x1,...,xk be their values
162  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
163  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
164  * offset we would get by using function get with instantiation
165  * {x1,...,xk}.
166  *
167  * @warning for efficiency reasons, this function does not check whether
168  * the element referenced by the offset actually belongs to the
169  * MultiDimArray. If you want this additional feature, use instead
170  * MultiDimArray::getByOffset(Idx).
171  *
172  * @param offset The offset of the returned value.
173  * @brief Returns the element stored in the multidimArray at a given
174  * offset.
175  */
176  const GUM_SCALAR& unsafeGet(Idx offset) const;
177 
178  /**
179  * @brief Returns the element stored in the multidimArray at a given
180  * offset.
181  *
182  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
183  * variables in the multidim, and let x1,...,xk be their values
184  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
185  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
186  * offset we would get by using function get with instantiation
187  * {x1,...,xk}.
188  *
189  * @param offset The offset of the returned value.
190  * @brief Returns the element stored in the multidimArray at a given
191  * offset.
192  *
193  * @throws OutOfBound Raised if the referenced element does not belong to
194  * the MultiDimArray.
195  */
196  const GUM_SCALAR& getByOffset(Idx offset) const;
197 
198  /**
199  * @brief Modifies the element stored in the multidimArray at a given
200  * offset.
201  *
202  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
203  * variables in the multidim, and let x1,...,xk be their values
204  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
205  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
206  * offset we would get by using function get with instantiation
207  * {x1,...,xk}.
208  *
209  * @warning for efficiency reasons, this function does not check whether
210  * the element referenced by the offset actually belongs to the
211  * MultiDimArray. If you want this additional feature, use instead
212  * function MultiDimArray::setByOffset(Idx, const GUM_SCARLAR&).
213  *
214  * @param offset The offset where to store the value.
215  * @param val The value to store.
216  */
217  void unsafeSet(Idx offset, const GUM_SCALAR& val);
218 
219  /**
220  * @brief Modifies the element stored in the multidimArray at a given
221  * offset.
222  *
223  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
224  * variables in the multidim, and let x1,...,xk be their values
225  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
226  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
227  * offset we would get by using function get with instantiation
228  * {x1,...,xk}.
229  *
230  * @param offset The offset where to store the value.
231  * @param val The value to store.
232  *
233  * @throws OutOfBound Raised if the referenced element does not belong to
234  * the MultiDimArray.
235  */
236  void setByOffset(Idx offset, const GUM_SCALAR& val);
237 
238  /// @}
239 
240  protected:
241  /// The true data : the values is mutable since we can change the value /
242  /// in a const multiDimArray
244 
245  virtual void commitMultipleChanges_();
246 
247  virtual void commitMultipleChanges_(const GUM_SCALAR&);
248 
249  virtual GUM_SCALAR& get_(const Instantiation& i) const;
250 
251  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
252  };
253 
254 
255 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
256  extern template class MultiDimArray< double >;
257 #endif
258 
259 
260 } /* namespace gum */
261 
262 #include <agrum/tools/multidim/implementations/multiDimArray_tpl.h>
263 
264 #endif /* GUM_MULTI_DIM_ARRAY_H */
virtual Size realSize() const
Returns the real size of this MultiDimArray.
virtual void erase(const DiscreteVariable &v)
Removes a variable.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
Replace variable x by y.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Copy from a other MultiDimContainer.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const
Apply a function on every element of the container.
void unsafeSet(Idx offset, const GUM_SCALAR &val)
Modifies the element stored in the multidimArray at a given offset.
virtual GUM_SCALAR & get_(const Instantiation &i) const
Return a data, given a Instantiation.
virtual ~MultiDimArray()
Copy operator.
virtual void commitMultipleChanges_(const GUM_SCALAR &)
Synchronize content after MultipleChanges.
void setByOffset(Idx offset, const GUM_SCALAR &val)
Modifies the element stored in the multidimArray at a given offset.
std::vector< GUM_SCALAR > values_
The true data : the values is mutable since we can change the value / in a const multiDimArray.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
compute lfold for this container
virtual void commitMultipleChanges_()
Synchronize content after MultipleChanges.
virtual void fill(const GUM_SCALAR &d) const
Fills the MultiDimArray with the given value.
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:53
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Default constructor.
virtual const std::string & name() const
Returns the MultiDimArray name.
const GUM_SCALAR & getByOffset(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
friend class MultiDimBijArray
Definition: multiDimArray.h:56
MultiDimArray()
Default constructor.
const GUM_SCALAR & unsafeGet(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
virtual void add(const DiscreteVariable &v)
Adds a variable.
MultiDimArray(const MultiDimArray< GUM_SCALAR > &src)
Copy constructor.