aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
potential_tpl.h
Go to the documentation of this file.
1 
29 #include <agrum/agrum.h>
32 
33 namespace gum {
34 
35  // Default constructor: creates an empty null dimensional matrix
36  // choose a MultiDimArray<> as decorated implementation
37  template < typename GUM_SCALAR >
39  MultiDimDecorator< GUM_SCALAR >(new MultiDimArray< GUM_SCALAR >(),
40  GUM_SCALAR(1)) {
41  GUM_CONSTRUCTOR(Potential);
42  }
43 
44  // constructor using aContent as content
45  template < typename GUM_SCALAR >
48  MultiDimDecorator< GUM_SCALAR >(aContent, GUM_SCALAR(1)) {
49  // for debugging purposes
50  GUM_CONSTRUCTOR(Potential);
51  }
52  // copy constructor
53  template < typename GUM_SCALAR >
55  Potential< GUM_SCALAR >(static_cast< MultiDimImplementation< GUM_SCALAR >* >(
56  src.content()->newFactory()),
57  *(src.content())) {
58  this->empty_value_ = src.empty_value_;
59  // GUM_CONS_CPY not here because in called Potential
60  // GUM_CONS_CPY( Potential );
61  }
62 
64  template < typename GUM_SCALAR >
66  MultiDimDecorator< GUM_SCALAR >(
67  std::forward< MultiDimDecorator< GUM_SCALAR > >(from)) {
68  GUM_CONS_MOV(Potential);
69  }
70 
71  // complex copy constructor : we choose the implementation
72  template < typename GUM_SCALAR >
76  MultiDimDecorator< GUM_SCALAR >(aContent) {
77  // for debugging purposes
78  GUM_CONSTRUCTOR(Potential);
79 
80  if (!src.empty()) {
81  this->beginMultipleChanges();
82 
83  for (Idx i = 0; i < src.variablesSequence().size(); i++) {
84  this->add(*(src.variablesSequence()[i]));
85  }
86 
87  this->endMultipleChanges();
88  this->content()->copyFrom(*src.content());
89  }
90  }
91 
92  // operator = copy
93  template < typename GUM_SCALAR >
96  GUM_OP_CPY(Potential);
97  if (&src == this) return *this;
99  return *this;
100  }
101 
102  // operator = move
103  template < typename GUM_SCALAR >
106  GUM_OP_MOV(Potential);
107  if (&src == this) return *this;
109  std::forward< MultiDimDecorator< GUM_SCALAR > >(src));
110  return *this;
111  }
112 
113  // destructor
114 
115  template < typename GUM_SCALAR >
117  // for debugging purposes
118  GUM_DESTRUCTOR(Potential);
119  }
120 
121  template < typename GUM_SCALAR >
123  return new Potential< GUM_SCALAR >(
124  static_cast< MultiDimImplementation< GUM_SCALAR >* >(
125  this->content()->newFactory()));
126  }
127 
128  // sum of all elements in this
129  template < typename GUM_SCALAR >
130  INLINE GUM_SCALAR Potential< GUM_SCALAR >::sum() const {
131  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
132  return this->empty_value_;
133  }
134  return gum::projectSum(*this->content());
135  }
136  // product of all elements in this
137  template < typename GUM_SCALAR >
138  INLINE GUM_SCALAR Potential< GUM_SCALAR >::product() const {
139  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
140  return this->empty_value_;
141  }
142  return gum::projectProduct(*this->content());
143  }
144  // max of all elements in this
145  template < typename GUM_SCALAR >
146  INLINE GUM_SCALAR Potential< GUM_SCALAR >::max() const {
147  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
148  return this->empty_value_;
149  }
150  return gum::projectMax(*this->content());
151  }
152  // min of all elements in this
153  template < typename GUM_SCALAR >
154  INLINE GUM_SCALAR Potential< GUM_SCALAR >::min() const {
155  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
156  return this->empty_value_;
157  }
158  return gum::projectMin(*this->content());
159  }
160 
161  // max of all non one elements in this
162  // warning can return 1 if no other value than 1 ...
163  template < typename GUM_SCALAR >
165  GUM_SCALAR res;
166 
167  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
168  res = this->empty_value_;
169  } else {
170  res = this->reduce(
171  [](GUM_SCALAR z, GUM_SCALAR p) {
172  return (p == static_cast< GUM_SCALAR >(1))
173  ? z
174  : (z == static_cast< GUM_SCALAR >(1)) ? p : (p > z ? p : z);
175  },
176  static_cast< GUM_SCALAR >(1));
177  }
178 
179  return res;
180  }
181 
182  // min of all non zero elements in this
183  // warning can return 0 if no other value than 0 ...
184  template < typename GUM_SCALAR >
185  INLINE GUM_SCALAR Potential< GUM_SCALAR >::minNonZero() const {
186  GUM_SCALAR res;
187 
188  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
189  res = this->empty_value_;
190  } else {
191  res = this->reduce(
192  [](GUM_SCALAR z, GUM_SCALAR p) {
193  return (p == static_cast< GUM_SCALAR >(0))
194  ? z
195  : (z == static_cast< GUM_SCALAR >(0)) ? p : (p < z ? p : z);
196  },
197  static_cast< GUM_SCALAR >(0));
198  }
199  return res;
200  }
201 
202  // entropy of this
203  template < typename GUM_SCALAR >
204  INLINE GUM_SCALAR Potential< GUM_SCALAR >::entropy() const {
205  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
206  return static_cast< GUM_SCALAR >(0);
207  }
208 
209  return this->reduce(
210  [](GUM_SCALAR z, GUM_SCALAR p) {
211  return (p == 0.0) ? z : (z - p * std::log2(p));
212  },
213  0.0);
214  }
215 
216  template < typename GUM_SCALAR >
217  INLINE const Potential< GUM_SCALAR >&
218  Potential< GUM_SCALAR >::fillWith(const std::vector< GUM_SCALAR >& v) const {
219  this->populate(v);
220  return *this;
221  }
222 
223  template < typename GUM_SCALAR >
224  INLINE const Potential< GUM_SCALAR >&
225  Potential< GUM_SCALAR >::fillWith(const GUM_SCALAR& v) const {
226  this->fill(v);
227  return *this;
228  }
229 
230  template < typename GUM_SCALAR >
231  INLINE const Potential< GUM_SCALAR >&
233  if (src.domainSize() != this->domainSize()) {
234  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
235  }
236  gum::Set< std::string > son; // set of names
237  for (const auto& v: src.variablesSequence()) {
238  son.insert(v->name());
239  }
240  for (const auto& v: this->variablesSequence()) {
241  if (!son.contains(v->name())) {
243  "Variable <" << v->name() << "> not present in src.");
244  }
245  // we check size, labels and order of labels in the same time
246  if (v->toString() != src.variable(v->name()).toString()) {
248  "Variables <" << v->name() << "> are not identical.");
249  }
250  }
251 
252  Instantiation Isrc(src);
253  Instantiation Idst(*this);
254  for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
255  for (Idx i = 0; i < this->nbrDim(); i++) {
256  Idst.chgVal(Isrc.variable(i).name(), Isrc.val(i));
257  }
258  this->set(Idst, src.get(Isrc));
259  }
260 
261  return *this;
262  }
263 
264  template < typename GUM_SCALAR >
266  const Potential< GUM_SCALAR >& src,
267  const std::vector< std::string >& mapSrc) const {
268  if (src.nbrDim() != this->nbrDim()) {
269  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
270  }
271  if (src.nbrDim() != mapSrc.size()) {
273  "Potential and vector have not the same dimension.");
274  }
275  Instantiation Isrc;
276  for (Idx i = 0; i < src.nbrDim(); i++) {
277  if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
279  "Variables " << mapSrc[i] << " (in the argument) and "
280  << this->variable(i).name()
281  << " have not the same dimension.");
282  } else {
283  Isrc.add(src.variable(mapSrc[i]));
284  }
285  }
286  Instantiation Idst(*this);
287  for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
288  this->set(Idst, src.get(Isrc));
289  }
290 
291  return *this;
292  }
293 
294  template < typename GUM_SCALAR >
296  this->apply([](GUM_SCALAR x) { return x * x; });
297  return *this;
298  }
299 
300  template < typename GUM_SCALAR >
302  this->apply([](GUM_SCALAR x) { return std::log2(x); });
303  return *this;
304  }
305 
306  template < typename GUM_SCALAR >
307  INLINE GUM_SCALAR
309  if (this->nbrDim() != p.nbrDim())
310  GUM_ERROR(
312  "BNdistance between potentials with different numbers of dimensions");
313  for (const auto var: p.variablesSequence()) {
314  if (!this->contains(*var))
316  "A variable in the argument does not belong to the potential.");
317  }
318  for (const auto var: this->variablesSequence()) {
319  if (!p.contains(*var))
320  GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.");
321  }
322 
323  Instantiation inst(*this);
324  GUM_SCALAR res = static_cast< GUM_SCALAR >(0);
325  for (inst.setFirst(); !inst.end(); inst.inc()) {
326  GUM_SCALAR x = this->get(inst);
327  GUM_SCALAR y = p.get(inst);
328  if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
329  continue;
330 
331  if (static_cast< GUM_SCALAR >(0) == y)
332  // we know that x!=0;
334  "The argument has a 0 at " << inst
335  << " while the potential has not.")
336 
337  res += x * std::log2(x / y);
338  }
339  return res;
340  }
341 
342  template < typename GUM_SCALAR >
344  this->apply([](GUM_SCALAR x) {
345  if (x >= 0)
346  return x;
347  else
348  return -x;
349  });
350  return *this;
351  }
352 
353  // normalisation of this
354  // do nothing is sum is 0
355  template < typename GUM_SCALAR >
356  INLINE const Potential< GUM_SCALAR >&
358  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
359  if (this->empty_value_ != static_cast< GUM_SCALAR >(0))
360  this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
361  } else {
362  GUM_SCALAR s = sum();
363 
364  if (s != (GUM_SCALAR)0) {
365  this->apply([s](GUM_SCALAR x) { return x / s; });
366  }
367  }
368  return *this;
369  }
370 
371  template < typename GUM_SCALAR >
372  INLINE const Potential< GUM_SCALAR >&
374  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
375  if (this->empty_value_ != static_cast< GUM_SCALAR >(0)) {
376  this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
377  } else {
379  "Normalization for a potential that sum to 0 in " << *this);
380  }
381  } else {
382  if (varId >= this->nbrDim()) {
383  GUM_ERROR(FatalError, varId << " is not a position for " << *this);
384  }
385  Instantiation inst(*this);
386  const auto& v = this->variable(varId);
387 
388  for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
389  GUM_SCALAR s = (GUM_SCALAR)0.0;
390  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
391  s += this->get(inst);
392  if (s == (GUM_SCALAR)0.0) {
394  "Normalization for a potential that sum to 0 in " << *this);
395  }
396  if (s != (GUM_SCALAR)1.0) {
397  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
398  this->set(inst, this->get(inst) / s);
399  }
400  inst.setFirstVar(v); // to remove inst.end()
401  }
402  }
403  return *this;
404  }
405 
406  template < typename GUM_SCALAR >
407  INLINE const Potential< GUM_SCALAR >&
408  Potential< GUM_SCALAR >::scale(GUM_SCALAR v) const {
409  this->apply([v](GUM_SCALAR x) { return x * v; });
410  return *this;
411  }
412 
413  template < typename GUM_SCALAR >
414  INLINE const Potential< GUM_SCALAR >&
416  this->apply([v](GUM_SCALAR x) { return x + v; });
417  return *this;
418  }
419 
420  template < typename GUM_SCALAR >
422  this->apply([](GUM_SCALAR x) { return 1 / x; });
423  return *this;
424  }
425 
426  template < typename GUM_SCALAR >
428  const Set< const DiscreteVariable* >& del_vars) const {
429  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
431  }
432  return Potential< GUM_SCALAR >(gum::projectSum(*this->content(), del_vars));
433  }
434 
435  template < typename GUM_SCALAR >
437  const Set< const DiscreteVariable* >& del_vars) const {
438  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
440  }
442  gum::projectProduct(*this->content(), del_vars));
443  }
444 
445  template < typename GUM_SCALAR >
447  const Set< const DiscreteVariable* >& del_vars) const {
448  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
450  }
451  return Potential< GUM_SCALAR >(gum::projectMin(*this->content(), del_vars));
452  }
453 
454  template < typename GUM_SCALAR >
456  const Set< const DiscreteVariable* >& del_vars) const {
457  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
459  }
460  return Potential< GUM_SCALAR >(gum::projectMax(*this->content(), del_vars));
461  }
462  template < typename GUM_SCALAR >
464  const Set< const DiscreteVariable* >& kept_vars) const {
465  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
467  }
469  gum::projectSum(*this->content(), complementVars_(kept_vars)));
470  }
471 
472  template < typename GUM_SCALAR >
474  const Set< const DiscreteVariable* >& kept_vars) const {
475  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
477  }
479  gum::projectProduct(*this->content(), complementVars_(kept_vars)));
480  }
481 
482  template < typename GUM_SCALAR >
484  const Set< const DiscreteVariable* >& kept_vars) const {
485  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
487  }
489  gum::projectMin(*this->content(), complementVars_(kept_vars)));
490  }
491 
492  template < typename GUM_SCALAR >
494  const Set< const DiscreteVariable* >& kept_vars) const {
495  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
497  }
499  gum::projectMax(*this->content(), complementVars_(kept_vars)));
500  }
501 
502  template < typename GUM_SCALAR >
504  auto p = Potential< GUM_SCALAR >(*this);
505  p.apply([](GUM_SCALAR x) {
506  if (x != static_cast< GUM_SCALAR >(0))
507  return static_cast< GUM_SCALAR >(1);
508  else
509  return static_cast< GUM_SCALAR >(0);
510  });
511  return p;
512  }
513 
514  template < typename GUM_SCALAR >
516  const Set< const DiscreteVariable* >& vars) const {
518 
519  for (const auto x: this->variablesSequence())
520  if (!vars.contains(x)) cplt.insert(x);
521 
522  return cplt;
523  }
524 
525  template < typename GUM_SCALAR >
527  const std::vector< const DiscreteVariable* >& vars) const {
528  if (vars.size() != this->nbrDim())
530  "The vector contains " << vars.size() << " variables instead of "
531  << this->nbrDim() << ".");
532  for (const auto var: vars) {
533  if (!this->contains(*var))
535  "A variable in the vector does not belong to the potential.");
536  }
537 
540  for (const auto var: vars)
541  p.add(*var);
542  p.endMultipleChanges();
543  p.copyFrom(*this, nullptr); // copy *this in p using the same order
544 
545  return p;
546  }
547 
548  template < typename GUM_SCALAR >
550  const std::vector< std::string >& vars) const {
551  std::vector< const DiscreteVariable* > res;
552 
554  for (gum::Idx i = 0; i < this->nbrDim(); i++)
555  namesToVars.insert(this->variable(i).name(), &(this->variable(i)));
556 
557  for (const auto& name: vars) {
558  if (!namesToVars.exists(name)) {
560  "'" << name
561  << "' is a not a name of a variable in this potential");
562  }
563  res.push_back(namesToVars[name]);
564  }
565  return reorganize(res);
566  }
567 
568  template < typename GUM_SCALAR >
571  if (!this->contains(*var)) {
573  "The variable to put first does not belong to the potential");
574  }
575  if (&(this->variable(0)) == var) return Potential< GUM_SCALAR >(*this);
576 
577  std::vector< const DiscreteVariable* > vars;
578  vars.push_back(var);
579  for (Idx i = 0; i < this->nbrDim(); i++)
580  if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
581 
582  return this->reorganize(vars);
583  }
584 
585  template < typename GUM_SCALAR >
587  Potential< GUM_SCALAR >::putFirst(const std::string& varname) const {
588  const DiscreteVariable* var = nullptr;
589 
590  for (gum::Idx i = 0; i < this->nbrDim(); i++)
591  if (this->variable(i).name() == varname) {
592  var = &(this->variable(i));
593  break;
594  }
595  if (var == nullptr)
597  "The variable '"
598  << varname
599  << "' to put first does not belong to the potential");
600  return this->putFirst(var);
601  }
602 
603  template < typename GUM_SCALAR >
607  p.extractFrom(*this, inst);
608 
609  return p;
610  }
611 
612  template < typename GUM_SCALAR >
614  if (this->nbrDim() != 1) {
615  GUM_ERROR(FatalError, "To draw from a potential, the dimension must be 1")
616  }
617 
618  GUM_SCALAR r = static_cast< GUM_SCALAR >(randomProba());
619  Instantiation Ip(*this);
620  for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
621  r -= this->get(Ip);
622  if (r <= 0) return Ip.val(0);
623  }
624  return this->variable(0).domainSize() - 1;
625  }
626 
627  template < typename GUM_SCALAR >
628  std::ostream& operator<<(std::ostream& out,
629  const Potential< GUM_SCALAR >& array) {
630  out << array.toString();
631  return out;
632  }
633 
634  // argmax of all elements in this
635  template < typename GUM_SCALAR >
637  Instantiation I(*this);
639 
640  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
641  return res;
642  }
643  for (I.setFirst(); !I.end(); ++I) {
644  if (this->get(I) == v) res.insert(I);
645  }
646  return res;
647  }
648  // argmax of all elements in this
649  template < typename GUM_SCALAR >
651  return findAll(max());
652  }
653  // argmin of all elements in this
654  template < typename GUM_SCALAR >
656  return findAll(min());
657  }
658 
659  template < typename GUM_SCALAR >
661  if (this->domainSize() == 0) return *this;
662 
663  std::vector< GUM_SCALAR > v;
664  GUM_SCALAR sum;
665  v.reserve(this->domainSize());
666  sum = 0.0;
667  for (Size i = 0; i < this->domainSize(); ++i) {
668  auto r = (GUM_SCALAR)randomProba();
669  v.push_back(r);
670  sum += r;
671  }
672  if (sum == 0.0)
673  v[gum::randomValue(this->domainSize())] = 1.0; // a 1 somewhere
674 
675  this->fillWith(v);
676  return *this;
677  }
678 
679  template < typename GUM_SCALAR >
680  INLINE const Potential< GUM_SCALAR >&
682  return this->random().normalize();
683  }
684 
685  template < typename GUM_SCALAR >
686  INLINE const Potential< GUM_SCALAR >&
688  return this->random().normalizeAsCPT();
689  }
690 
691  template < typename GUM_SCALAR >
693  Potential< GUM_SCALAR >::noising(GUM_SCALAR alpha) const {
694  if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
695  GUM_ERROR(InvalidArgument, "alpha must be in [0,1]");
696  }
697  Potential< GUM_SCALAR > noise(*this);
698  return fillWith(scale(1 - alpha) + noise.randomCPT().scale(alpha))
699  .normalizeAsCPT();
700  }
701 
702 } /* namespace gum */
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual bool empty() const =0
Returns true if no var is in *this.
virtual Size domainSize() const final
Returns the product of the variables domain size.
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
GUM_SCALAR min() const
min of all elements in the Potential
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
double randomProba()
Returns a random double between 0 and 1 included (i.e.
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
STL namespace.
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
const Potential< GUM_SCALAR > & inverse(void) const
the function to inverse (each value of) *this
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Base class for discrete random variable.
GUM_SCALAR KL(const Potential< GUM_SCALAR > &p) const
compute KL divergence between this and p Checks the compatibility and then compute KL divergence ...
Potential< GUM_SCALAR > margProdOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
const Potential< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
Set< Instantiation > argmin() const
set of instantiation corresponding to the min in the Potential
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
virtual bool empty() const final
Returns true if no var is in *this.
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
GUM_SCALAR product() const
product of all elements in the Potential
virtual Size domainSize() const =0
Potential< GUM_SCALAR > margProdIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
const Potential< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
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:631
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
void inc()
Operator increment.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
Idx draw() const
get a value at random from a 1-D distribution
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
Potential< GUM_SCALAR > margSumIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using sum as operation (and implementation-optimized operations)
Set< Instantiation > argmax() const
set of instantiation corresponding to the max in the Potential
Potential< GUM_SCALAR > & operator=(const Potential< GUM_SCALAR > &src)
Default constructor.
Definition: potential_tpl.h:95
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:54
GUM_SCALAR max() const
max of all elements in the Potential
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
const Potential< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using max as operation (and implementation-optimized operations)
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
GUM_SCALAR entropy() const
entropy of the Potential
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
Decorator design pattern in order to separate implementations from multidimensional matrix concepts...
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
GUM_SCALAR sum() const
sum of all elements in the Potential
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
virtual const std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:494
void setFirst()
Assign the first values to the tuple of the Instantiation.
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
<agrum/tools/multidim/multiDimImplementation.h>
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using max as operation (and implementation-optimized operations)
Size Idx
Type for indexes.
Definition: types.h:53
GUM_SCALAR maxNonOne() const
max of all non one elements in the Potential
Potential< GUM_SCALAR > margMinOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using min as operation (and implementation-optimized operations)
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
const Potential< GUM_SCALAR > & noising(GUM_SCALAR alpha) const
add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
const std::string & name() const
returns the name of the variable
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
const Potential< GUM_SCALAR > & randomDistribution() const
generate a random Distribution in the Potential
Potential()
Default constructor.
Definition: potential_tpl.h:38
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
Potential< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Potential with a certain variable in first
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
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.
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)