aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
potential.h
Go to the documentation of this file.
1 
28 #ifndef GUM_POTENTIAL_H
29 #define GUM_POTENTIAL_H
30 
31 #include <vector>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/set.h>
37 
38 namespace gum {
39 
40  // ==========================================================================
41  // === POTENTIAL ===
42  // ==========================================================================
43 
59  template < typename GUM_SCALAR >
60  class Potential: public MultiDimDecorator< GUM_SCALAR > {
61  public:
62  // =========================================================================
64  // =========================================================================
66 
73  Potential();
74 
80 
98 
104 
105 
109  ~Potential();
110 
112  // ========================================================================
114  // ========================================================================
116 
117  virtual Potential< GUM_SCALAR >* newFactory() const;
118 
120  // ========================================================================
122  // ========================================================================
124 
125  const Potential< GUM_SCALAR >& random() const;
126 
129 
131  const Potential< GUM_SCALAR >& randomCPT() const;
132 
136  const Potential< GUM_SCALAR >& noising(GUM_SCALAR alpha) const;
137 
144  margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
145 
152  margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
153 
160  margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
161 
168  margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
169 
176  margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
177 
184  margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
185 
192  margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
193 
200  margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
201 
206 
208  GUM_SCALAR sum() const;
210  GUM_SCALAR product() const;
212  GUM_SCALAR max() const;
214  GUM_SCALAR min() const;
217  GUM_SCALAR maxNonOne() const;
220  GUM_SCALAR minNonZero() const;
221 
223  Set< Instantiation > findAll(GUM_SCALAR v) const;
228 
230  GUM_SCALAR entropy() const;
231 
237  reorganize(const std::vector< const DiscreteVariable* >& vars) const;
238 
242  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
243 
248 
258  fillWith(const Potential< GUM_SCALAR >& src) const;
259 
278  const std::vector< std::string >& mapSrc) const;
279 
289  fillWith(const std::vector< GUM_SCALAR >& v) const;
290 
296  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& v) const;
297 
298 
302  const Potential< GUM_SCALAR >& abs() const;
303 
307  const Potential< GUM_SCALAR >& normalize() const;
308 
312  const Potential< GUM_SCALAR >& sq() const;
313 
317  const Potential< GUM_SCALAR >& log2() const;
318 
327  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
328 
334 
338  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
339 
343  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
344 
348  const Potential< GUM_SCALAR >& inverse(void) const;
349 
353  Idx draw() const;
354 
356 
357  // ========================================================================
359  // ========================================================================
361 
362 
365  if (p2.empty())
367  if (this->empty())
369 
370  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
371  }
372 
374  Potential< GUM_SCALAR > operator+(const GUM_SCALAR& v) const {
375  return Potential< GUM_SCALAR >(*this).translate(v);
376  }
377 
380  if (p2.empty())
382  if (this->empty()) {
383  auto p = Potential< GUM_SCALAR >(p2);
384  p.apply([this](GUM_SCALAR x) { return this->_empty_value - x; });
385  return p;
386  }
387  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
388  }
389 
391  Potential< GUM_SCALAR > operator-(const GUM_SCALAR& v) const {
392  return Potential< GUM_SCALAR >(*this).translate(-v);
393  }
394 
397  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2._empty_value);
398  if (this->empty())
399  return Potential< GUM_SCALAR >(p2).scale(this->_empty_value);
400 
401  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
402  }
403 
405  Potential< GUM_SCALAR > operator*(const GUM_SCALAR& v) const {
406  return Potential< GUM_SCALAR >(*this).scale(v);
407  }
408 
411  if (p2.empty())
412  return Potential< GUM_SCALAR >(*this).scale(1 / p2._empty_value);
413  if (this->empty()) {
414  auto p = Potential< GUM_SCALAR >(p2);
415  p.apply([this](GUM_SCALAR x) { return this->_empty_value / x; });
416  return p;
417  }
418  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
419  }
421  Potential< GUM_SCALAR > operator/(const GUM_SCALAR& v) const {
422  return Potential< GUM_SCALAR >(*this).scale(1 / v);
423  }
424 
426  *this = *this + r;
427  return *this;
428  }
429  Potential< GUM_SCALAR >& operator+=(const GUM_SCALAR& v) {
430  this->translate(v);
431  return *this;
432  }
433 
435  *this = *this * r;
436  return *this;
437  }
438  Potential< GUM_SCALAR >& operator*=(const GUM_SCALAR& v) {
439  this->scale(v);
440  return *this;
441  }
442 
444  *this = *this - r;
445  return *this;
446  }
447  Potential< GUM_SCALAR >& operator-=(const GUM_SCALAR& v) {
448  this->translate(-v);
449  return *this;
450  }
451 
453  *this = *this / r;
454  return *this;
455  }
456  Potential< GUM_SCALAR >& operator/=(const GUM_SCALAR& v) {
457  this->scale(1 / v);
458  return *this;
459  }
460 
461  bool operator==(const Potential< GUM_SCALAR >& r) const {
462  if (this->empty()) {
463  if (r.empty())
464  return this->_empty_value == r._empty_value;
465  else
466  return false;
467  } else {
468  if (r.empty())
469  return false;
470  else
471  return (*this->_content) == (*r._content);
472  }
473  }
474 
475  bool operator!=(const Potential< GUM_SCALAR >& r) const {
476  return !operator==(r);
477  }
478 
479  virtual const std::string toString() const {
481  }
482 
484 
485  protected:
487  _complementVars(const Set< const DiscreteVariable* >& del_vars) const;
488  };
489 
490 
491 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
492  extern template class Potential< double >;
493 #endif
494 
495 
496 } /* namespace gum */
497 
499 #endif /* GUM_POTENTIAL_H */
Potential< GUM_SCALAR > operator+(const Potential< GUM_SCALAR > &p2) const
the function to be used to add two Potentials
Definition: potential.h:364
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
MultiDimImplementation< GUM_SCALAR > * _content
The true container.
Potential< GUM_SCALAR > operator/(const Potential< GUM_SCALAR > &p2) const
the function to be used to divide two Potentials
Definition: potential.h:410
virtual const std::string toString() const
Default implementation of MultiDimContainer::set().
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
GUM_SCALAR min() const
min of all elements in the Potential
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Potential< GUM_SCALAR > & operator-=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:443
Potential< GUM_SCALAR > & operator-=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:447
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
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
const Potential< GUM_SCALAR > & inverse(void) const
the function to inverse (each value of) *this
Potential< GUM_SCALAR > & operator/=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:452
Base class for discrete random variable.
Potential< GUM_SCALAR > & operator*=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:434
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) ...
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
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Potential< GUM_SCALAR > operator/(const GUM_SCALAR &v) const
the function to be used to divide a Potential by a scalar
Definition: potential.h:421
virtual bool empty() const final
Returns true if no var is in *this.
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
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
GUM_SCALAR product() const
product of all elements in the Potential
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) ...
const Potential< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
bool operator==(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:461
Idx draw() const
get a value at random from a 1-D distribution
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
Potential< GUM_SCALAR > & operator+=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:429
GUM_SCALAR max() const
max of all elements in the Potential
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Definition: potential.h:379
Potential< GUM_SCALAR > & operator*=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:438
const Potential< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using max as operation (and implementation-optimized operations)
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)
Potential< GUM_SCALAR > & operator+=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:425
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...
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Potential< GUM_SCALAR > operator+(const GUM_SCALAR &v) const
the function to be used to add a GUM_SCALAR to a Potential
Definition: potential.h:374
GUM_SCALAR sum() const
sum of all elements in the Potential
Potential< GUM_SCALAR > & operator/=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:456
virtual const std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:479
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
<agrum/tools/multidim/multiDimImplementation.h>
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)
Potential< GUM_SCALAR > operator*(const Potential< GUM_SCALAR > &p2) const
the function to be used to multiply two Potentials
Definition: potential.h:396
const Potential< GUM_SCALAR > & noising(GUM_SCALAR alpha) const
add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
bool operator!=(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:475
Potential< GUM_SCALAR > operator*(const GUM_SCALAR &v) const
the function to be used to multiply a Potential and a scalar
Definition: potential.h:405
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
Potential< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Potential with a certain variable in first
Potential< GUM_SCALAR > operator-(const GUM_SCALAR &v) const
the function to be used to substract a GUM_SCALAR from a Potential
Definition: potential.h:391
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)