aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimArray.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 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 reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
122  GUM_SCALAR base) const;
123 
124  /**
125  * @brief Adds a variable.
126  */
127  virtual void add(const DiscreteVariable& v);
128 
129  /**
130  * @brief Removes a variable.
131  */
132  virtual void erase(const DiscreteVariable& v);
133 
134  /**
135  * @brief Returns the real size of this MultiDimArray.
136  */
137  virtual Size realSize() const;
138 
139  /**
140  * @brief Fills the MultiDimArray with the given value.
141  */
142  virtual void fill(const GUM_SCALAR& d) const;
143 
144  /**
145  * @brief Returns the MultiDimArray name.
146  */
147  virtual const std::string& name() const;
148 
149  /// @}
150  // =========================================================================
151  /// @name Accessors / Modifiers
152  // =========================================================================
153  /// @{
154 
155  /**
156  * @brief Returns the element stored in the multidimArray at a given
157  * offset.
158  *
159  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
160  * variables in the multidim, and let x1,...,xk be their values
161  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
162  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
163  * offset we would get by using function get with instantiation
164  * {x1,...,xk}.
165  *
166  * @warning for efficiency reasons, this function does not check whether
167  * the element referenced by the offset actually belongs to the
168  * MultiDimArray. If you want this additional feature, use instead
169  * MultiDimArray::getByOffset(Idx).
170  *
171  * @param offset The offset of the returned value.
172  * @brief Returns the element stored in the multidimArray at a given
173  * offset.
174  */
175  const GUM_SCALAR& unsafeGet(Idx offset) const;
176 
177  /**
178  * @brief Returns the element stored in the multidimArray at a given
179  * offset.
180  *
181  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
182  * variables in the multidim, and let x1,...,xk be their values
183  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
184  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
185  * offset we would get by using function get with instantiation
186  * {x1,...,xk}.
187  *
188  * @param offset The offset of the returned value.
189  * @brief Returns the element stored in the multidimArray at a given
190  * offset.
191  *
192  * @throws OutOfBound Raised if the referenced element does not belong to
193  * the MultiDimArray.
194  */
195  const GUM_SCALAR& getByOffset(Idx offset) const;
196 
197  /**
198  * @brief Modifies the element stored in the multidimArray at a given
199  * offset.
200  *
201  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
202  * variables in the multidim, and let x1,...,xk be their values
203  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
204  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
205  * offset we would get by using function get with instantiation
206  * {x1,...,xk}.
207  *
208  * @warning for efficiency reasons, this function does not check whether
209  * the element referenced by the offset actually belongs to the
210  * MultiDimArray. If you want this additional feature, use instead
211  * function MultiDimArray::setByOffset(Idx, const GUM_SCARLAR&).
212  *
213  * @param offset The offset where to store the value.
214  * @param val The value to store.
215  */
216  void unsafeSet(Idx offset, const GUM_SCALAR& val);
217 
218  /**
219  * @brief Modifies the element stored in the multidimArray at a given
220  * offset.
221  *
222  * The offset is computed as follows: let S={X1,...,Xk} be the sequence of
223  * variables in the multidim, and let x1,...,xk be their values
224  * respectively. then the corresponding offset is x1 + |X1| * x2 + [X1| *
225  * |X2| * x3 + ... This thus corresponds to the internal MultiDimArray's
226  * offset we would get by using function get with instantiation
227  * {x1,...,xk}.
228  *
229  * @param offset The offset where to store the value.
230  * @param val The value to store.
231  *
232  * @throws OutOfBound Raised if the referenced element does not belong to
233  * the MultiDimArray.
234  */
235  void setByOffset(Idx offset, const GUM_SCALAR& val);
236 
237  /// @}
238 
239  protected:
240  /// The true data : the values is mutable since we can change the value /
241  /// in a const multiDimArray
243 
244  virtual void commitMultipleChanges_();
245 
246  virtual void commitMultipleChanges_(const GUM_SCALAR&);
247 
248  virtual GUM_SCALAR& get_(const Instantiation& i) const;
249 
250  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
251  };
252 
253 
254 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
255  extern template class MultiDimArray< double >;
256 #endif
257 
258 
259 } /* namespace gum */
260 
261 #include <agrum/tools/multidim/implementations/multiDimArray_tpl.h>
262 
263 #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:643
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.