aGrUM  0.16.0
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/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 
322  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
323 
329 
333  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
334 
338  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
339 
343  Idx draw() const;
344 
346 
347  // ========================================================================
349  // ========================================================================
351 
352 
355  if (p2.empty())
357  if (this->empty())
359 
360  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
361  }
362 
365  if (p2.empty())
367  if (this->empty()) {
368  auto p = Potential< GUM_SCALAR >(p2);
369  p.apply([this](GUM_SCALAR x) { return this->_empty_value - x; });
370  return p;
371  }
372  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
373  }
374 
377  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2._empty_value);
378  if (this->empty())
379  return Potential< GUM_SCALAR >(p2).scale(this->_empty_value);
380 
381  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
382  }
383 
386  if (p2.empty())
387  return Potential< GUM_SCALAR >(*this).scale(1 / p2._empty_value);
388  if (this->empty()) {
389  auto p = Potential< GUM_SCALAR >(p2);
390  p.apply([this](GUM_SCALAR x) { return this->_empty_value / x; });
391  return p;
392  }
393  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
394  }
395 
397  *this = *this + r;
398  return *this;
399  }
400 
402  *this = *this * r;
403  return *this;
404  }
405 
407  *this = *this - r;
408  return *this;
409  }
410 
412  *this = *this / r;
413  return *this;
414  }
415 
416  bool operator==(const Potential< GUM_SCALAR >& r) const {
417  if (this->empty()) {
418  if (r.empty())
419  return this->_empty_value == r._empty_value;
420  else
421  return false;
422  } else {
423  if (r.empty())
424  return false;
425  else
426  return (*this->_content) == (*r._content);
427  }
428  }
429 
430  bool operator!=(const Potential< GUM_SCALAR >& r) const {
431  return !operator==(r);
432  }
433 
434  virtual const std::string toString() const {
436  }
437 
439 
440  protected:
442  _complementVars(const Set< const DiscreteVariable* >& del_vars) const;
443  };
444 
445 
446 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
447  extern template class Potential< double >;
448 #endif
449 
450 
451 } /* namespace gum */
452 
454 #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:354
~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:385
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:406
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
create a new potential multiplied by v from *this
Potential< GUM_SCALAR > & operator/=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:411
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:401
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:416
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
GUM_SCALAR max() const
max of all elements in the Potential
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Definition: potential.h:364
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:396
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 sum() const
sum of all elements in the Potential
virtual const std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:434
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
<agrum/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:376
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
create a new potential added with v from *this
bool operator!=(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:430
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 > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)