aGrUM  0.15.1
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 
131  margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
132 
139  margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
140 
147  margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
148 
155  margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
156 
163  margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
164 
171  margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
172 
179  margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
180 
187  margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
188 
193 
195  GUM_SCALAR sum() const;
197  GUM_SCALAR product() const;
199  GUM_SCALAR max() const;
201  GUM_SCALAR min() const;
204  GUM_SCALAR maxNonOne() const;
207  GUM_SCALAR minNonZero() const;
208 
210  Set< Instantiation > findAll(GUM_SCALAR v) const;
215 
217  GUM_SCALAR entropy() const;
218 
224  reorganize(const std::vector< const DiscreteVariable* >& vars) const;
225 
229  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
230 
235 
245  fillWith(const Potential< GUM_SCALAR >& src) const;
246 
265  const std::vector< std::string >& mapSrc) const;
266 
276  fillWith(const std::vector< GUM_SCALAR >& v) const;
277 
283  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& v) const;
284 
285 
289  const Potential< GUM_SCALAR >& abs() const;
290 
294  const Potential< GUM_SCALAR >& normalize() const;
295 
299  const Potential< GUM_SCALAR >& sq() const;
300 
309  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
310 
315  void normalizeAsCPT() const;
316 
320  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
321 
325  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
326 
330  Idx draw() const;
331 
333 
334  // ========================================================================
336  // ========================================================================
338 
339 
342  if (p2.empty())
344  if (this->empty())
346 
347  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
348  }
349 
352  if (p2.empty())
354  if (this->empty()) {
355  auto p = Potential< GUM_SCALAR >(p2);
356  p.apply([this](GUM_SCALAR x) { return this->_empty_value - x; });
357  return p;
358  }
359  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
360  }
361 
364  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2._empty_value);
365  if (this->empty())
366  return Potential< GUM_SCALAR >(p2).scale(this->_empty_value);
367 
368  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
369  }
370 
373  if (p2.empty())
374  return Potential< GUM_SCALAR >(*this).scale(1 / p2._empty_value);
375  if (this->empty()) {
376  auto p = Potential< GUM_SCALAR >(p2);
377  p.apply([this](GUM_SCALAR x) { return this->_empty_value / x; });
378  return p;
379  }
380  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
381  }
382 
384  *this = *this + r;
385  return *this;
386  }
387 
389  *this = *this * r;
390  return *this;
391  }
392 
394  *this = *this - r;
395  return *this;
396  }
397 
399  *this = *this / r;
400  return *this;
401  }
402 
403  bool operator==(const Potential< GUM_SCALAR >& r) const {
404  if (this->empty()) {
405  if (r.empty())
406  return this->_empty_value == r._empty_value;
407  else
408  return false;
409  } else {
410  if (r.empty())
411  return false;
412  else
413  return (*this->_content) == (*r._content);
414  }
415  }
416 
417  bool operator!=(const Potential< GUM_SCALAR >& r) const {
418  return !operator==(r);
419  }
420 
421  virtual const std::string toString() const {
423  }
424 
426 
427  protected:
429  _complementVars(const Set< const DiscreteVariable* >& del_vars) const;
430  };
431 
432 
433 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
434  extern template class Potential< double >;
435 #endif
436 
437 
438 } /* namespace gum */
439 
441 #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:341
~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:372
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:393
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
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:398
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:388
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
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:403
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:351
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:383
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void normalizeAsCPT() const
normalisation of this as a CPT
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:421
<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:363
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:417
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
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)