aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
multiDimContainer_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/agrum.h>
31 #include <algorithm>
32 
33 
34 namespace gum {
35 
36  template < typename GUM_SCALAR >
39  MultiDimAdressable(std::forward< MultiDimAdressable >(from)) {
40  GUM_CONS_MOV(MultiDimContainer);
41  }
42 
43  // Default constructor
44  template < typename GUM_SCALAR >
47  GUM_CONSTRUCTOR(MultiDimContainer);
48  }
49 
50  // Copy constructor
51  template < typename GUM_SCALAR >
54  MultiDimAdressable(src) {
55  GUM_CONS_CPY(MultiDimContainer);
56  }
57  template < typename GUM_SCALAR >
60  const MultiDimContainer< GUM_SCALAR >& from) {
62  return *this;
63  }
64  template < typename GUM_SCALAR >
68  GUM_OP_MOV(MultiDimContainer);
69  MultiDimAdressable::operator=(std::forward< MultiDimAdressable >(from));
70  return *this;
71  }
72 
73  // destructor
74 
75  template < typename GUM_SCALAR >
77  GUM_DESTRUCTOR(MultiDimContainer);
78  }
79 
80  // an [] operator using a Instantiation as argument
81 
82  template < typename GUM_SCALAR >
83  INLINE GUM_SCALAR
85  return get(i);
86  }
87 
88  // an [] operator using a Instantiation as argument
89 
90  template < typename GUM_SCALAR >
92  const GUM_SCALAR& value) const {
93  _get(i) = value;
94  }
95 
96  // an [] operator using a Instantiation as argument
97 
98  template < typename GUM_SCALAR >
99  INLINE GUM_SCALAR
101  return _get(i);
102  }
103 
104  // display the content of an array
105 
106  template < typename GUM_SCALAR >
107  const std::string MultiDimContainer< GUM_SCALAR >::toString() const {
108  // we create a new instantiation and iterate over it to display the whole
109  // content of the array
110  if (this->nbrDim() == 0) { return "[]"; }
111 
112  std::stringstream ss;
113  Instantiation inst(const_cast< MultiDimContainer* >(this));
114 
115  bool first = true;
116 
117  for (inst.setFirst(); !inst.end(); ++inst) {
118  if (!first) { ss << " /"; }
119  first = false;
120 
121  ss << inst << " :: " << get(inst);
122  }
123 
124  return ss.str();
125  }
126 
127  // Test if this potential is equal to p.
128 
129  template < typename GUM_SCALAR >
131  const MultiDimContainer< GUM_SCALAR >& p) const {
132  if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
133  if (nbrDim() == 0) return true;
134 
136  var_iterator;
137 
138  for (var_iterator iter = variablesSequence().beginSafe();
139  iter != variablesSequence().endSafe();
140  ++iter) {
141  if (!p.variablesSequence().exists(*iter)) { return false; }
142  }
143  } else {
144  return false;
145  }
146 
147  Instantiation i(*this);
148 
150 
151  for (i.setFirst(); !i.end(); ++i) {
152  if (cmp(get(i), p.get(i))) { return false; }
153  }
154 
155  return true;
156  }
157 
158  // Test if this potential is different of p.
159 
160  template < typename GUM_SCALAR >
162  const MultiDimContainer< GUM_SCALAR >& p) const {
163  return !operator==(p);
164  }
165 
166  // automation fill with vector.
167  template < typename GUM_SCALAR >
169  const std::vector< GUM_SCALAR >& v) const {
170  if (domainSize() != v.size()) {
171  GUM_ERROR(SizeError, "Sizes do not match in populate");
172  }
173 
174  Size cpt = 0;
175 
176  Instantiation i(*this);
177 
178  for (i.setFirst(); !i.end(); ++i, ++cpt)
179  set(i, v[cpt]);
180  }
181 
182  template < typename GUM_SCALAR >
184  std::initializer_list< GUM_SCALAR > l) const {
185  if (domainSize() != l.size()) {
186  GUM_ERROR(SizeError, "Sizes do not match in populate");
187  }
188 
189  Instantiation i(*this);
190  // insert all the elements
191  for (const auto& elt: l) {
192  set(i, elt);
193  ++i;
194  }
195  }
196 
197  template < typename GUM_SCALAR >
199  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
200  Instantiation i(*this);
201  for (i.setFirst(); !i.end(); ++i) {
202  set(i, f(get(i)));
203  }
204  }
205 
206  template < typename GUM_SCALAR >
208  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
209  GUM_SCALAR base) const {
210  GUM_SCALAR tmp = base;
211  Instantiation i(*this);
212  for (i.setFirst(); !i.end(); ++i) {
213  tmp = f(tmp, get(i));
214  }
215  return tmp;
216  }
217 
218 
219  template < typename GUM_SCALAR >
221  const MultiDimContainer< GUM_SCALAR >& src, Instantiation* p_i) const {
222  if (src.domainSize() != domainSize()) {
223  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
224  }
225 
226  if (p_i == nullptr) { // if null, we just follow the same order
227  Instantiation i(src);
228  for (i.setFirst(); !i.end(); ++i) {
229  set(i, src[i]);
230  }
231  } else {
232  Instantiation i_dest(*this);
233  Instantiation i_src(src);
234  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end();
235  i_dest.incIn(*p_i), ++i_src) {
236  set(i_dest, src[i_src]);
237  }
238  }
239  }
240 
241  template < typename GUM_SCALAR >
243  const MultiDimContainer< GUM_SCALAR >& src, const Instantiation& imask) {
244  this->beginMultipleChanges();
245 
246  Size nbr = this->nbrDim();
247 
248  for (Idx i = 0; i < nbr; i++) {
249  this->erase(this->variable(0));
250  }
251 
252  for (Idx i = 0; i < src.nbrDim(); i++) {
253  if (!imask.contains(src.variable(i))) this->add(src.variable(i));
254  }
255 
256  if (this->nbrDim() == 0) { GUM_ERROR(FatalError, "Empty potential"); }
257 
258  this->endMultipleChanges();
259 
260  Instantiation inst(src);
261  inst.setVals(imask);
262  for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
263  set(inst, src[inst]);
264  }
265 
266  template < typename GUM_SCALAR >
268  const MultiDimContainer< GUM_SCALAR >& src) const {
269  if (src.domainSize() != domainSize()) {
270  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
271  }
272 
273  Instantiation i_dest(*this);
274  Instantiation i_src(src);
275 
276  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
277  set(i_dest, src[i_src]);
278  }
279  }
280 
281  // copy
282 
283  template < typename GUM_SCALAR >
285  const MultiDimContainer< GUM_SCALAR >& src) {
286  this->beginMultipleChanges();
287 
288  Size nbr = this->nbrDim();
289 
290  for (Idx i = 0; i < nbr; i++) {
291  this->erase(this->variable(0));
292  }
293 
294  for (Idx i = 0; i < src.nbrDim(); i++) {
295  this->add(src.variable(i));
296  }
297 
298  this->endMultipleChanges();
299  this->copyFrom(src);
300  }
301 
302  template < typename GUM_SCALAR >
304  return static_cast< MultiDimAdressable& >(*content());
305  }
306 
307  template < typename GUM_SCALAR >
308  INLINE const MultiDimAdressable&
310  return static_cast< const MultiDimAdressable& >(*content());
311  }
312 
313  // display the content of an array
314 
315  template < typename GUM_SCALAR >
316  std::ostream& operator<<(std::ostream& out,
317  const MultiDimContainer< GUM_SCALAR >& array) {
318  out << array.toString();
319  return out;
320  }
321 
322 } /* namespace gum */
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
Safe iterators for Sequence.
Definition: sequence.h:1206
virtual ~MultiDimContainer()
Destructor.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
STL namespace.
virtual GUM_SCALAR & _get(const Instantiation &i) const =0
Return a data, given a Instantiation.
virtual const std::string toString() const
Returns a representation of this MultiDimContainer.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
Indicate whether two elements are (almost) different or not.
Definition: utils_misc.h:155
virtual void copy(const MultiDimContainer< GUM_SCALAR > &src)
Removes all variables in this MultiDimContainer and copy the content of src, variables included...
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Changes the value pointed by i.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:626
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
compute lfold for this container
virtual Size domainSize() const =0
Returns the product of the variables domain size.
MultiDimAdressable & operator=(const MultiDimAdressable &from)
Default constructor.
bool operator!=(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is different of p.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
GUM_SCALAR operator[](const Instantiation &i) const
An [] operator using a Instantiation as argument.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
MultiDimContainer()
Default constructor.
void setFirst()
Assign the first values to the tuple of the Instantiation.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const
Apply a function on every element of the container.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
bool operator==(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is equal to p.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual MultiDimAdressable & getMasterRef()
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
virtual void populate(const std::vector< GUM_SCALAR > &v) const
Automatically fills this MultiDimContainer with the values in v.
bool end() const
Returns true if the Instantiation reached the end.
virtual void extractFrom(const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
MultiDimContainer & operator=(const MultiDimContainer< GUM_SCALAR > &src)
Default constructor.