aGrUM  0.13.3
potential.h
Go to the documentation of this file.
1 /************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #ifndef GUM_POTENTIAL_H
26 #define GUM_POTENTIAL_H
27 
28 #include <vector>
29 
30 #include <agrum/agrum.h>
31 #include <agrum/core/set.h>
34 
35 namespace gum {
36 
37  // ==========================================================================
38  // === POTENTIAL ===
39  // ==========================================================================
40 
56  template < typename GUM_SCALAR >
57  class Potential : public MultiDimDecorator< GUM_SCALAR > {
58  public:
59  // =========================================================================
61  // =========================================================================
63 
70  Potential();
71 
77 
95 
101 
102 
106  ~Potential();
107 
109  // ========================================================================
111  // ========================================================================
113 
114  virtual Potential< GUM_SCALAR >* newFactory() const;
115 
117  // ========================================================================
119  // ========================================================================
121 
128  margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
129 
136  margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
137 
144  margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
145 
152  margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
153 
160  margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
161 
168  margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
169 
176  margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
177 
184  margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
185 
190 
192  GUM_SCALAR sum() const;
194  GUM_SCALAR product() const;
196  GUM_SCALAR max() const;
198  GUM_SCALAR min() const;
201  GUM_SCALAR maxNonOne() const;
204  GUM_SCALAR minNonZero() const;
205 
207  Set< Instantiation > findAll(GUM_SCALAR v) const;
212 
214  GUM_SCALAR entropy() const;
215 
221  reorganize(const std::vector< const DiscreteVariable* >& vars) const;
222 
226  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
227 
232 
242  fillWith(const Potential< GUM_SCALAR >& src) const;
243 
262  const std::vector< std::string >& mapSrc) const;
263 
273  fillWith(const std::vector< GUM_SCALAR >& v) const;
274 
280  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& v) const;
281 
282 
286  const Potential< GUM_SCALAR >& abs() const;
287 
291  const Potential< GUM_SCALAR >& normalize() const;
292 
296  const Potential< GUM_SCALAR >& sq() const;
297 
306  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
307 
312  void normalizeAsCPT() const;
313 
317  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
318 
322  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
323 
327  Idx draw() const;
328 
330 
331  // ========================================================================
333  // ========================================================================
335 
336 
339  if (p2.empty())
341  if (this->empty())
343 
344  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
345  }
346 
349  if (p2.empty())
351  if (this->empty()) {
352  auto p = Potential< GUM_SCALAR >(p2);
353  p.apply([this](GUM_SCALAR x) { return this->_empty_value - x; });
354  return p;
355  }
356  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
357  }
358 
361  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2._empty_value);
362  if (this->empty())
363  return Potential< GUM_SCALAR >(p2).scale(this->_empty_value);
364 
365  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
366  }
367 
370  if (p2.empty())
371  return Potential< GUM_SCALAR >(*this).scale(1 / p2._empty_value);
372  if (this->empty()) {
373  auto p = Potential< GUM_SCALAR >(p2);
374  p.apply([this](GUM_SCALAR x) { return this->_empty_value / x; });
375  return p;
376  }
377  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
378  }
379 
381  *this = *this + r;
382  return *this;
383  }
384 
386  *this = *this * r;
387  return *this;
388  }
389 
391  *this = *this - r;
392  return *this;
393  }
394 
396  *this = *this / r;
397  return *this;
398  }
399 
400  bool operator==(const Potential< GUM_SCALAR >& r) const {
401  if (this->empty()) {
402  if (r.empty())
403  return this->_empty_value == r._empty_value;
404  else
405  return false;
406  } else {
407  if (r.empty())
408  return false;
409  else
410  return (*this->_content) == (*r._content);
411  }
412  }
413 
414  bool operator!=(const Potential< GUM_SCALAR >& r) const {
415  return !operator==(r);
416  }
417 
418  virtual const std::string toString() const {
420  }
421 
423 
424  protected:
426  _complementVars(const Set< const DiscreteVariable* >& del_vars) const;
427  };
428 
429 
430  extern template class Potential< float >;
431  extern template class Potential< double >;
432 
433 
434 } /* namespace gum */
435 
437 #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:338
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
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:369
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
Sets of elements (i.e.
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:390
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:395
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:385
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) ...
gum is the global namespace for all aGrUM entities
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
Headers of gum::MultiDimImplementation.
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:162
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:400
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:93
GUM_SCALAR max() const
max of all elements in the Potential
Headers for MultiDimDecorator.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
Implementation of the Potential class.
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Definition: potential.h:348
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:380
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
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:418
<agrum/multidim/multiDimImplementation.h>
Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using max as operation (and implementation-optimized operations)
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:360
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:414
unsigned long Idx
Type for indexes.
Definition: types.h:43
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
Potential()
Default constructor.
Definition: potential_tpl.h:35
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)