aGrUM  0.17.2
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  // todo how to optimize copy of content ?
60  // GUM_CONS_CPY not here because in called Potential
61  // GUM_CONS_CPY( Potential );
62  }
63 
65  template < typename GUM_SCALAR >
67  MultiDimDecorator< GUM_SCALAR >(
68  std::forward< MultiDimDecorator< GUM_SCALAR > >(from)) {
69  GUM_CONS_MOV(Potential);
70  }
71 
72  // complex copy constructor : we choose the implementation
73  template < typename GUM_SCALAR >
77  MultiDimDecorator< GUM_SCALAR >(aContent) {
78  // for debugging purposes
79  GUM_CONSTRUCTOR(Potential);
80 
81  if (!src.empty()) {
82  this->beginMultipleChanges();
83 
84  for (Idx i = 0; i < src.variablesSequence().size(); i++) {
85  this->add(*(src.variablesSequence()[i]));
86  }
87 
88  this->endMultipleChanges();
89  this->content()->copyFrom(*src.content());
90  }
91  }
92 
93  // operator = copy
94  template < typename GUM_SCALAR >
97  GUM_OP_CPY(Potential);
98  if (&src == this) return *this;
100  return *this;
101  }
102 
103  // operator = move
104  template < typename GUM_SCALAR >
107  GUM_OP_MOV(Potential);
108  if (&src == this) return *this;
110  std::forward< MultiDimDecorator< GUM_SCALAR > >(src));
111  return *this;
112  }
113 
114  // destructor
115 
116  template < typename GUM_SCALAR >
118  // for debugging purposes
119  GUM_DESTRUCTOR(Potential);
120  }
121 
122  template < typename GUM_SCALAR >
124  return new Potential< GUM_SCALAR >(
125  static_cast< MultiDimImplementation< GUM_SCALAR >* >(
126  this->content()->newFactory()));
127  }
128 
129  // sum of all elements in this
130  template < typename GUM_SCALAR >
131  INLINE GUM_SCALAR Potential< GUM_SCALAR >::sum() const {
132  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
133  return this->_empty_value;
134  }
135  return gum::projectSum(*this->content());
136  }
137  // product of all elements in this
138  template < typename GUM_SCALAR >
139  INLINE GUM_SCALAR Potential< GUM_SCALAR >::product() const {
140  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
141  return this->_empty_value;
142  }
143  return gum::projectProduct(*this->content());
144  }
145  // max of all elements in this
146  template < typename GUM_SCALAR >
147  INLINE GUM_SCALAR Potential< GUM_SCALAR >::max() const {
148  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
149  return this->_empty_value;
150  }
151  return gum::projectMax(*this->content());
152  }
153  // min of all elements in this
154  template < typename GUM_SCALAR >
155  INLINE GUM_SCALAR Potential< GUM_SCALAR >::min() const {
156  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
157  return this->_empty_value;
158  }
159  return gum::projectMin(*this->content());
160  }
161 
162  // max of all non one elements in this
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  if (res == static_cast< GUM_SCALAR >(1))
180  GUM_ERROR(NotFound, "No other value than 1");
181 
182  return res;
183  }
184 
185  // min of all non zero elements in this
186  template < typename GUM_SCALAR >
187  INLINE GUM_SCALAR Potential< GUM_SCALAR >::minNonZero() const {
188  GUM_SCALAR res;
189 
190  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
191  res = this->_empty_value;
192  } else {
193  res = this->reduce(
194  [](GUM_SCALAR z, GUM_SCALAR p) {
195  return (p == static_cast< GUM_SCALAR >(0))
196  ? z
197  : (z == static_cast< GUM_SCALAR >(0)) ? p : (p < z ? p : z);
198  },
199  static_cast< GUM_SCALAR >(0));
200  }
201 
202  if (res == static_cast< GUM_SCALAR >(0))
203  GUM_ERROR(NotFound, "No other value than 1");
204 
205  return res;
206  }
207 
208  // entropy of this
209  template < typename GUM_SCALAR >
210  INLINE GUM_SCALAR Potential< GUM_SCALAR >::entropy() const {
211  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
212  return static_cast< GUM_SCALAR >(0);
213  }
214 
215  return this->reduce(
216  [](GUM_SCALAR z, GUM_SCALAR p) {
217  return (p == 0.0) ? z : (z - p * std::log2(p));
218  },
219  0.0);
220  }
221 
222  template < typename GUM_SCALAR >
223  INLINE const Potential< GUM_SCALAR >&
224  Potential< GUM_SCALAR >::fillWith(const std::vector< GUM_SCALAR >& v) const {
225  this->populate(v);
226  return *this;
227  }
228 
229  template < typename GUM_SCALAR >
230  INLINE const Potential< GUM_SCALAR >&
231  Potential< GUM_SCALAR >::fillWith(const GUM_SCALAR& v) const {
232  this->fill(v);
233  return *this;
234  }
235 
236  template < typename GUM_SCALAR >
237  INLINE const Potential< GUM_SCALAR >&
239  if (src.domainSize() != this->domainSize()) {
240  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
241  }
242  gum::Set< std::string > son; // set of names
243  for (const auto& v: src.variablesSequence()) {
244  son.insert(v->name());
245  }
246  for (const auto& v: this->variablesSequence()) {
247  if (!son.contains(v->name())) {
249  "Variable <" << v->name() << "> not present in src.");
250  }
251  // we check size, labels and order of labels in the same time
252  if (v->toString() != src.variable(v->name()).toString()) {
254  "Variables <" << v->name() << "> are not identical.");
255  }
256  }
257 
258  Instantiation Isrc(src);
259  Instantiation Idst(*this);
260  for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
261  for (Idx i = 0; i < this->nbrDim(); i++) {
262  Idst.chgVal(Isrc.variable(i).name(), Isrc.val(i));
263  }
264  this->set(Idst, src.get(Isrc));
265  }
266 
267  return *this;
268  }
269 
270  template < typename GUM_SCALAR >
272  const Potential< GUM_SCALAR >& src,
273  const std::vector< std::string >& mapSrc) const {
274  if (src.nbrDim() != this->nbrDim()) {
275  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
276  }
277  if (src.nbrDim() != mapSrc.size()) {
279  "Potential and vector have not the same dimension.");
280  }
281  Instantiation Isrc;
282  for (Idx i = 0; i < src.nbrDim(); i++) {
283  if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
285  "Variables " << mapSrc[i] << " (in the argument) and "
286  << this->variable(i).name()
287  << " have not the same dimension.");
288  } else {
289  Isrc.add(src.variable(mapSrc[i]));
290  }
291  }
292  Instantiation Idst(*this);
293  for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
294  this->set(Idst, src.get(Isrc));
295  }
296 
297  return *this;
298  }
299 
300  template < typename GUM_SCALAR >
302  this->apply([](GUM_SCALAR x) { return x * x; });
303  return *this;
304  }
305 
306  template < typename GUM_SCALAR >
308  this->apply([](GUM_SCALAR x) { return std::log2(x); });
309  return *this;
310  }
311 
312  template < typename GUM_SCALAR >
313  INLINE GUM_SCALAR
315  if (this->nbrDim() != p.nbrDim())
316  GUM_ERROR(
318  "BNdistance between potentials with different numbers of dimensions");
319  for (const auto var: p.variablesSequence()) {
320  if (!this->contains(*var))
322  "A variable in the argument does not belong to the potential.");
323  }
324  for (const auto var: this->variablesSequence()) {
325  if (!p.contains(*var))
326  GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.");
327  }
328 
329  Instantiation inst(*this);
330  GUM_SCALAR res = static_cast< GUM_SCALAR >(0);
331  for (inst.setFirst(); !inst.end(); inst.inc()) {
332  GUM_SCALAR x = this->get(inst);
333  GUM_SCALAR y = p.get(inst);
334  if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
335  continue;
336 
337  if (static_cast< GUM_SCALAR >(0) == y)
338  // we know that x!=0;
340  "The argument has a 0 at " << inst
341  << " while the potential has not.")
342 
343  res += x * std::log2(x / y);
344  }
345  return res;
346  }
347 
348  template < typename GUM_SCALAR >
350  this->apply([](GUM_SCALAR x) {
351  if (x >= 0)
352  return x;
353  else
354  return -x;
355  });
356  return *this;
357  }
358 
359  // normalisation of this
360  // do nothing is sum is 0
361  template < typename GUM_SCALAR >
362  INLINE const Potential< GUM_SCALAR >&
364  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
365  if (this->_empty_value != static_cast< GUM_SCALAR >(0))
366  this->_empty_value = static_cast< GUM_SCALAR >(1.0);
367  } else {
368  GUM_SCALAR s = sum();
369 
370  if (s != (GUM_SCALAR)0) {
371  this->apply([s](GUM_SCALAR x) { return x / s; });
372  }
373  }
374  return *this;
375  }
376 
377  template < typename GUM_SCALAR >
378  INLINE const Potential< GUM_SCALAR >&
380  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
381  if (this->_empty_value != static_cast< GUM_SCALAR >(0)) {
382  this->_empty_value = static_cast< GUM_SCALAR >(1.0);
383  } else {
385  "Normalization for a potential that sum to 0 in " << *this);
386  }
387  } else {
388  Instantiation inst(*this);
389  const auto& v = this->variable(0);
390 
391  for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
392  GUM_SCALAR s = (GUM_SCALAR)0.0;
393  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
394  s += this->get(inst);
395  if (s == (GUM_SCALAR)0.0) {
397  "Normalization for a potential that sum to 0 in " << *this);
398  }
399  if (s != (GUM_SCALAR)1.0) {
400  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
401  this->set(inst, this->get(inst) / s);
402  }
403  inst.setFirstVar(v); // to remove inst.end()
404  }
405  }
406  return *this;
407  }
408 
409  template < typename GUM_SCALAR >
410  INLINE const Potential< GUM_SCALAR >&
411  Potential< GUM_SCALAR >::scale(GUM_SCALAR v) const {
412  this->apply([v](GUM_SCALAR x) { return x * v; });
413  return *this;
414  }
415 
416  template < typename GUM_SCALAR >
417  INLINE const Potential< GUM_SCALAR >&
419  this->apply([v](GUM_SCALAR x) { return x + v; });
420  return *this;
421  }
422 
423  template < typename GUM_SCALAR >
425  this->apply([](GUM_SCALAR x) { return 1 / x; });
426  return *this;
427  }
428 
429  template < typename GUM_SCALAR >
431  const Set< const DiscreteVariable* >& del_vars) const {
432  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
434  }
435  return Potential< GUM_SCALAR >(gum::projectSum(*this->content(), del_vars));
436  }
437 
438  template < typename GUM_SCALAR >
440  const Set< const DiscreteVariable* >& del_vars) const {
441  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
443  }
445  gum::projectProduct(*this->content(), del_vars));
446  }
447 
448  template < typename GUM_SCALAR >
450  const Set< const DiscreteVariable* >& del_vars) const {
451  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
453  }
454  return Potential< GUM_SCALAR >(gum::projectMin(*this->content(), del_vars));
455  }
456 
457  template < typename GUM_SCALAR >
459  const Set< const DiscreteVariable* >& del_vars) const {
460  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
462  }
463  return Potential< GUM_SCALAR >(gum::projectMax(*this->content(), del_vars));
464  }
465  template < typename GUM_SCALAR >
467  const Set< const DiscreteVariable* >& kept_vars) const {
468  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
470  }
472  gum::projectSum(*this->content(), _complementVars(kept_vars)));
473  }
474 
475  template < typename GUM_SCALAR >
477  const Set< const DiscreteVariable* >& kept_vars) const {
478  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
480  }
482  gum::projectProduct(*this->content(), _complementVars(kept_vars)));
483  }
484 
485  template < typename GUM_SCALAR >
487  const Set< const DiscreteVariable* >& kept_vars) const {
488  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
490  }
492  gum::projectMin(*this->content(), _complementVars(kept_vars)));
493  }
494 
495  template < typename GUM_SCALAR >
497  const Set< const DiscreteVariable* >& kept_vars) const {
498  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
500  }
502  gum::projectMax(*this->content(), _complementVars(kept_vars)));
503  }
504 
505  template < typename GUM_SCALAR >
507  auto p = Potential< GUM_SCALAR >(*this);
508  p.apply([](GUM_SCALAR x) {
509  if (x != static_cast< GUM_SCALAR >(0))
510  return static_cast< GUM_SCALAR >(1);
511  else
512  return static_cast< GUM_SCALAR >(0);
513  });
514  return p;
515  }
516 
517  template < typename GUM_SCALAR >
519  const Set< const DiscreteVariable* >& vars) const {
521 
522  for (const auto x: this->variablesSequence())
523  if (!vars.contains(x)) cplt.insert(x);
524 
525  return cplt;
526  }
527 
528  template < typename GUM_SCALAR >
530  const std::vector< const DiscreteVariable* >& vars) const {
531  if (vars.size() != this->nbrDim())
533  "The vector contains " << vars.size() << " variables instead of "
534  << this->nbrDim() << ".");
535  for (const auto var: vars) {
536  if (!this->contains(*var))
538  "A variable in the vector does not belong to the potential.");
539  }
540 
543  for (const auto var: vars)
544  p.add(*var);
545  p.endMultipleChanges();
546  p.copyFrom(*this, nullptr); // copy *this in p using the same order
547 
548  return p;
549  }
550 
551  template < typename GUM_SCALAR >
554  if (!this->contains(*var)) {
556  "The variable to put first does not belong to the potential");
557  }
558  if (&(this->variable(0)) == var) return Potential< GUM_SCALAR >(*this);
559 
560  std::vector< const DiscreteVariable* > vars;
561  vars.push_back(var);
562  for (Idx i = 0; i < this->nbrDim(); i++)
563  if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
564 
565  return this->reorganize(vars);
566  }
567 
568  template < typename GUM_SCALAR >
572  p.extractFrom(*this, inst);
573 
574  return p;
575  }
576 
577  template < typename GUM_SCALAR >
579  if (this->nbrDim() != 1) {
580  GUM_ERROR(FatalError, "To draw from a potential, the dimension must be 1")
581  }
582 
583  GUM_SCALAR r = static_cast< GUM_SCALAR >(randomProba());
584  Instantiation Ip(*this);
585  for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
586  r -= this->get(Ip);
587  if (r <= 0) return Ip.val(0);
588  }
589  return this->variable(0).domainSize() - 1;
590  }
591 
592  template < typename GUM_SCALAR >
593  std::ostream& operator<<(std::ostream& out,
594  const Potential< GUM_SCALAR >& array) {
595  out << array.toString();
596  return out;
597  }
598 
599  // argmax of all elements in this
600  template < typename GUM_SCALAR >
602  Instantiation I(*this);
604 
605  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
606  return res;
607  }
608  for (I.setFirst(); !I.end(); ++I) {
609  if (this->get(I) == v) res.insert(I);
610  }
611  return res;
612  }
613  // argmax of all elements in this
614  template < typename GUM_SCALAR >
616  return findAll(max());
617  }
618  // argmin of all elements in this
619  template < typename GUM_SCALAR >
621  return findAll(min());
622  }
623 
624  template < typename GUM_SCALAR >
626  std::vector< GUM_SCALAR > v;
627 
628  GUM_SCALAR sum;
629  v.reserve(this->domainSize());
630  do {
631  sum = 0.0;
632  for (Size i = 0; i < this->domainSize(); ++i) {
633  auto r = (GUM_SCALAR)randomProba();
634  v.push_back(r);
635  sum += r;
636  }
637  } while (sum == 0.0);
638 
639  this->fillWith(v);
640  return *this;
641  }
642 
643  template < typename GUM_SCALAR >
644  INLINE const Potential< GUM_SCALAR >&
646  return this->random().normalize();
647  }
648 
649  template < typename GUM_SCALAR >
650  INLINE const Potential< GUM_SCALAR >&
652  return this->random().normalizeAsCPT();
653  }
654 
655  template < typename GUM_SCALAR >
657  Potential< GUM_SCALAR >::noising(GUM_SCALAR alpha) const {
658  if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
659  GUM_ERROR(InvalidArgument, "alpha must be in [0,1]");
660  }
661  Potential< GUM_SCALAR > noise(*this);
662  return fillWith(scale(1 - alpha) + noise.randomCPT().scale(alpha))
663  .normalizeAsCPT();
664  }
665 
666 } /* namespace gum */
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et 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:583
~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.
MultiDimImplementation< GUM_SCALAR > * _content
The true 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().
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
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.
GUM_SCALAR _empty_value
value of the MultiDimDecorator if no dimension.
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
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 () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
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.
Set< const DiscreteVariable *> _complementVars(const Set< const DiscreteVariable * > &del_vars) const
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
const Potential< GUM_SCALAR > & normalizeAsCPT() const
normalisation of this as a CPT
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 () et 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:626
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:96
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 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:479
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
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
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:615
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)