aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
potential.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Header of the Potential class.
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_POTENTIAL_H
28 #define GUM_POTENTIAL_H
29 
30 #include <vector>
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/core/set.h>
34 #include <agrum/tools/multidim/implementations/multiDimDecorator.h>
35 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
36 
37 namespace gum {
38 
39  // ==========================================================================
40  // === POTENTIAL ===
41  // ==========================================================================
42 
43  /**
44  * @class Potential
45  * @headerfile potential.h <agrum/tools/multidim/potential.h>
46  * @ingroup multidim_group
47  *
48  * @brief aGrUM's Potential is a multi-dimensional array with tensor operators.
49  * It is used to represent probabilities and utilities in aGrUMs'
50  * multidimensional (graphical) models.
51  *
52  * Using the decorator pattern, this representation is independent from the
53  * implementation of the multidimensional matrix.
54  *
55  * @tparam GUM_SCALAR The type of the scalar stored in this multidimensional
56  * matrix.
57  */
58  template < typename GUM_SCALAR >
60  public:
61  // =========================================================================
62  /// @name Constructors, Destructors and Copy
63  // =========================================================================
64  /// @{
65 
66  /**
67  * @brief Default constructor.
68  *
69  * Creates an empty null dimensional matrix with a MultiDimArray as
70  * its implementation.
71  */
72  Potential();
73 
74  /**
75  * @brief Creates an potential around aContent.
76  * @param aContent The implementation of this Potential.
77  */
78  explicit Potential(MultiDimImplementation< GUM_SCALAR >* aContent);
79 
80  /**
81  * @brief Copy constructor.
82  *
83  * The newly created Potential share the variables and the values
84  * from src, but no instantiation is associated to it. It allows to force
85  * the chosen implementation and to copy the data from src.
86  *
87  * @param aContent The implementation to use in this Potential.
88  * @param src The MultiDimContainer to copy.
89  */
90  Potential(MultiDimImplementation< GUM_SCALAR >* aContent,
91  const MultiDimContainer< GUM_SCALAR >& src);
92  /**
93  * @brief Copy constructor & assignment
94  */
95  Potential(const Potential< GUM_SCALAR >& src);
96  Potential< GUM_SCALAR >& operator=(const Potential< GUM_SCALAR >& src);
97 
98  /**
99  * move constructor & assignement
100  **/
101  Potential(Potential< GUM_SCALAR >&& from);
102  Potential< GUM_SCALAR >& operator=(Potential< GUM_SCALAR >&& src);
103 
104 
105  /**
106  * @brief Destructor.
107  */
108  ~Potential();
109 
110  /// @}
111  // ========================================================================
112  /// @name MultiDimContainer implementation
113  // ========================================================================
114  /// @{
115 
116  virtual Potential< GUM_SCALAR >* newFactory() const;
117 
118  /// @}
119  // ========================================================================
120  /// @name Class operation for Potential instances
121  // ========================================================================
122  ///@{
123  /** generate a random Potential with each parameter in [0,1]*/
124  const Potential< GUM_SCALAR >& random() const;
125 
126  /** generate a random Distribution in the Potential */
127  const Potential< GUM_SCALAR >& randomDistribution() const;
128 
129  /** generate a random CPT in the Potential */
130  const Potential< GUM_SCALAR >& randomCPT() const;
131 
132  /** add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
133  * @warning alpha in [0,1]
134  */
135  const Potential< GUM_SCALAR >& noising(GUM_SCALAR alpha) const;
136 
137  /**
138  * Projection using sum as operation (and implementation-optimized
139  * operations)
140  * @param del_vars is the set of vars to eliminate
141  */
142  Potential< GUM_SCALAR >
143  margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
144 
145  /**
146  * Projection using sum as operation (and implementation-optimized
147  * operations)
148  * @param kept_vars is the set of vars to keep
149  */
150  Potential< GUM_SCALAR >
151  margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
152 
153  /**
154  * Projection using multiplication as operation (and
155  * implementation-optimized operations)
156  * @param del_vars is the set of vars to eliminate
157  */
158  Potential< GUM_SCALAR >
159  margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
160 
161  /**
162  * Projection using multiplication as operation (and
163  * implementation-optimized operations)
164  * @param kept_vars is the set of vars to keep
165  */
166  Potential< GUM_SCALAR >
167  margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
168 
169  /**
170  * Projection using min as operation (and implementation-optimized
171  * operations)
172  * @param del_vars is the set of vars to eliminate
173  */
174  Potential< GUM_SCALAR >
175  margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
176 
177  /**
178  * Projection using min as operation (and implementation-optimized
179  * operations)
180  * @param kept_vars is the set of vars to keep
181  */
182  Potential< GUM_SCALAR >
183  margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
184 
185  /**
186  * Projection using max as operation (and implementation-optimized
187  * operations)
188  * @param del_vars is the set of vars to eliminate
189  */
190  Potential< GUM_SCALAR >
191  margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
192 
193  /**
194  * Projection using max as operation (and implementation-optimized
195  * operations)
196  * @param kept_vars is the set of vars to keep
197  */
198  Potential< GUM_SCALAR >
199  margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
200 
201  /**
202  * create a boolean-like potential using the predicate isNonZero
203  */
204  Potential< GUM_SCALAR > isNonZeroMap() const;
205 
206  /// sum of all elements in the Potential
207  GUM_SCALAR sum() const;
208  /// product of all elements in the Potential
209  GUM_SCALAR product() const;
210  /// max of all elements in the Potential
211  GUM_SCALAR max() const;
212  /// min of all elements in the Potential
213  GUM_SCALAR min() const;
214  /// max of all non one elements in the Potential
215  /// @warning can return 1 if no other value than 1 ...
216  GUM_SCALAR maxNonOne() const;
217  /// min of all non zero elements in the Potential
218  /// @warning can return 0 if no other value than 0 ...
219  GUM_SCALAR minNonZero() const;
220 
221  /// set of instantiation corresponding to the parameter v in the Potential
223  /// set of instantiation corresponding to the max in the Potential
224  Set< Instantiation > argmax() const;
225  /// set of instantiation corresponding to the min in the Potential
226  Set< Instantiation > argmin() const;
227 
228  /// entropy of the Potential
229  GUM_SCALAR entropy() const;
230 
231  /** create a new Potential with another order
232  * @throw InvalidArgument if not all and only the vars of the potential are
233  * in vars
234  */
235  Potential< GUM_SCALAR >
236  reorganize(const std::vector< const DiscreteVariable* >& vars) const;
237 
238  /** create a new Potential with another order
239  * @throw InvalidArgument if not all and only the vars of the potential are
240  * in vars
241  */
242  Potential< GUM_SCALAR >
243  reorganize(const std::vector< std::string >& vars) const;
244 
245  /** create a new Potential extracted from *this given a partial
246  * instantiation
247  */
248  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
249 
250  /** create a new Potential with a certain variable in first
251  * @throw InvalidArgument if the var is not in the potential
252  */
253  Potential< GUM_SCALAR > putFirst(const DiscreteVariable* var) const;
254 
255  /** create a new Potential with a certain variable in first
256  * @throw InvalidArgument if the var is not in the potential
257  */
258  Potential< GUM_SCALAR > putFirst(const std::string& varname) const;
259 
260  /**
261  * @brief copy a Potential data using name of variables and labels (not
262  * necessarily the same variables in the same orders)
263  *
264  * @warning a strict control on names of variables and labels are made
265  *
266  * @throw InvalidArgument if the Potential is not compatible with this
267  */
268  const Potential< GUM_SCALAR >&
269  fillWith(const Potential< GUM_SCALAR >& src) const;
270 
271  /**
272  * @brief copy a Potential data using the sequence of names in mapSrc to find
273  * the corresponding variables.
274  *
275  * For instance, to copy the potential P(A,B,C) in Q(D,E,A) with the mapping
276  * P.A<->Q.E, P.B<->Q.A, P.C<->Q.D (assuming that the corresponding variables
277  * have the same domain size and the order of labels):
278  *
279  * @code
280  * Q.fillWith(P,{"C","A","B"});
281  * @endcode
282  *
283  * @warning a strict control on names of variables and labels are made
284  *
285  * @throw InvalidArgument if the Potential is not compatible with this
286  * */
287  const Potential< GUM_SCALAR >&
288  fillWith(const Potential< GUM_SCALAR >& src,
289  const std::vector< std::string >& mapSrc) const;
290 
291  /**
292  * @brief Automatically fills the potential with the values in
293  * v.
294  *
295  * @param v Vector of values.
296  * @throw SizeError Raised if v size's does not matches this
297  * MultiDimContainer domain size.
298  */
299  const Potential< GUM_SCALAR >&
300  fillWith(const std::vector< GUM_SCALAR >& data) const;
301 
302  /**
303  * @brief Automatically fills this MultiDimContainer with the value v
304  *
305  * @param v contains the data.
306  */
307  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& val) const;
308 
309 
310  /**
311  * @brief Apply abs on every element of the container
312  */
313  const Potential< GUM_SCALAR >& abs() const;
314 
315  /**
316  * @brief apply $x^2$ on every element of the container
317  */
318  const Potential< GUM_SCALAR >& sq() const;
319 
320  /**
321  * @brief apply $log_2(x)$ on every element of the container
322  */
323  const Potential< GUM_SCALAR >& log2() const;
324 
325  /**
326  * @brief Create a new potential and apply abs on every element of the
327  * container
328  */
329  const Potential< GUM_SCALAR > new_abs() const;
330 
331  /**
332  * @brief Create a new potential and apply $x^2$ on every element of the
333  * container
334  */
335  const Potential< GUM_SCALAR > new_sq() const;
336 
337  /**
338  * @brief Create a new potential and apply $log_2(x)$ on every element of the
339  * container
340  */
341  const Potential< GUM_SCALAR > new_log2() const;
342 
343 
344  /**
345  * @brief normalisation of this do nothing if sum is 0
346  */
347  const Potential< GUM_SCALAR >& normalize() const;
348 
349  /**
350  * @brief compute KL divergence between this and p
351  * Checks the compatibility and then compute KL divergence
352  * @throws gum::InvalidArgument if p is not compatible with $this (dimension,
353  * variables)
354  * @throws gum::FatalError if a zero is found in p or this and not in the
355  * other.
356  */
357  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
358 
359  /**
360  * @brief normalisation of this as a CPT for the variable varId
361  *
362  * If the Potential is empty, the argument is not used.
363  *
364  * @throw FatalError it some distribution sums to 0, or if varId>=nbrDim()
365  */
366  const Potential< GUM_SCALAR >& normalizeAsCPT(const Idx& varId = 0) const;
367 
368  /**
369  * @brief multiply (each value of) *this by v
370  */
371  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
372 
373  /**
374  * @brief add v to (each value of) *this
375  */
376  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
377 
378  /**
379  * @brief the function to inverse (each value of) *this
380  */
381  const Potential< GUM_SCALAR >& inverse(void) const;
382 
383  /**
384  * @brief get a value at random from a 1-D distribution
385  */
386  Idx draw() const;
387 
388  ///@}
389 
390  // ========================================================================
391  /// @name Potential algebra operators
392  // ========================================================================
393  ///@{
394 
395 
396  /// the function to be used to add two Potentials
397  Potential< GUM_SCALAR > operator+(const Potential< GUM_SCALAR >& p2) const {
398  if (p2.empty())
399  return Potential< GUM_SCALAR >(*this).translate(p2.empty_value_);
400  if (this->empty())
401  return Potential< GUM_SCALAR >(p2).translate(this->empty_value_);
402 
403  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
404  }
405 
406  /// the function to be used to add a GUM_SCALAR to a Potential
407  Potential< GUM_SCALAR > operator+(const GUM_SCALAR& v) const {
408  return Potential< GUM_SCALAR >(*this).translate(v);
409  }
410 
411  /// the function to be used to subtract two Potentials
412  Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR >& p2) const {
413  if (p2.empty())
414  return Potential< GUM_SCALAR >(*this).translate(-p2.empty_value_);
415  if (this->empty()) {
416  auto p = Potential< GUM_SCALAR >(p2);
417  p.apply([this](GUM_SCALAR x) { return this->empty_value_ - x; });
418  return p;
419  }
420  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
421  }
422 
423  /// the function to be used to substract a GUM_SCALAR from a Potential
424  Potential< GUM_SCALAR > operator-(const GUM_SCALAR& v) const {
425  return Potential< GUM_SCALAR >(*this).translate(-v);
426  }
427 
428  /// the function to be used to multiply two Potentials
429  Potential< GUM_SCALAR > operator*(const Potential< GUM_SCALAR >& p2) const {
430  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2.empty_value_);
431  if (this->empty())
432  return Potential< GUM_SCALAR >(p2).scale(this->empty_value_);
433 
434  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
435  }
436 
437  /// the function to be used to multiply a Potential and a scalar
438  Potential< GUM_SCALAR > operator*(const GUM_SCALAR& v) const {
439  return Potential< GUM_SCALAR >(*this).scale(v);
440  }
441 
442  /// the function to be used to divide two Potentials
443  Potential< GUM_SCALAR > operator/(const Potential< GUM_SCALAR >& p2) const {
444  if (p2.empty())
445  return Potential< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
446  if (this->empty()) {
447  auto p = Potential< GUM_SCALAR >(p2);
448  p.apply([this](GUM_SCALAR x) { return this->empty_value_ / x; });
449  return p;
450  }
451  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
452  }
453  /// the function to be used to divide a Potential by a scalar
454  Potential< GUM_SCALAR > operator/(const GUM_SCALAR& v) const {
455  return Potential< GUM_SCALAR >(*this).scale(1 / v);
456  }
457 
458  Potential< GUM_SCALAR >& operator+=(const Potential< GUM_SCALAR >& r) {
459  *this = *this + r;
460  return *this;
461  }
462  Potential< GUM_SCALAR >& operator+=(const GUM_SCALAR& v) {
463  this->translate(v);
464  return *this;
465  }
466 
467  Potential< GUM_SCALAR >& operator*=(const Potential< GUM_SCALAR >& r) {
468  *this = *this * r;
469  return *this;
470  }
471  Potential< GUM_SCALAR >& operator*=(const GUM_SCALAR& v) {
472  this->scale(v);
473  return *this;
474  }
475 
476  Potential< GUM_SCALAR >& operator-=(const Potential< GUM_SCALAR >& r) {
477  *this = *this - r;
478  return *this;
479  }
480  Potential< GUM_SCALAR >& operator-=(const GUM_SCALAR& v) {
481  this->translate(-v);
482  return *this;
483  }
484 
485  Potential< GUM_SCALAR >& operator/=(const Potential< GUM_SCALAR >& r) {
486  *this = *this / r;
487  return *this;
488  }
489  Potential< GUM_SCALAR >& operator/=(const GUM_SCALAR& v) {
490  this->scale(1 / v);
491  return *this;
492  }
493 
494  bool operator==(const Potential< GUM_SCALAR >& r) const {
495  if (this->empty()) {
496  if (r.empty())
497  return this->empty_value_ == r.empty_value_;
498  else
499  return false;
500  } else {
501  if (r.empty())
502  return false;
503  else
504  return (*this->content_) == (*r.content_);
505  }
506  }
507 
508  bool operator!=(const Potential< GUM_SCALAR >& r) const {
509  return !operator==(r);
510  }
511 
512  Potential< GUM_SCALAR >& operator<<(const DiscreteVariable& v) {
513  this->add(v);
514  return *this;
515  }
516 
517  virtual std::string toString() const {
518  auto table = this->content();
519  if (table->nbrDim() == 0) { return "[]"; }
520  const Size colwidth = 6;
521  const Size numberwidth = 9;
522  const Size nbrLigMax = 6;
523 
524  std::stringstream ss;
525  ss << std::left << std::fixed << std::endl;
526  ss.precision(numberwidth - 5);
527 
528  const auto& var = table->variable(0);
529 
530  const Size nbparents = table->nbrDim() - 1;
531  const Size nbcol = var.domainSize();
532  const std::string maskparent(colwidth, '-');
533  const std::string masknumber(numberwidth, '-');
534 
535  if (nbparents > 0)
536  ss << std::setw(nbparents * (colwidth + 1) - 1) << " "
537  << "||";
538  ss << " " << std::setw(nbcol * (numberwidth + 1) - 3)
539  << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << "|";
540  ss << std::endl;
541 
542  if (nbparents > 0) {
543  for (Idx i = 1; i <= nbparents; i++)
544  ss << std::setw(colwidth)
545  << table->variable(i).name().substr(0, colwidth) << "|";
546  ss << "|";
547  }
548  for (Idx i = 0; i < nbcol; i++)
549  ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << "|";
550  ss << std::endl;
551 
552 
553  if (nbparents > 0) {
554  for (Idx i = 1; i <= nbparents; i++)
555  ss << maskparent << "|";
556  ss << "|";
557  }
558  for (Idx i = 0; i < nbcol; i++)
559  ss << masknumber << "|";
560  ss << std::endl;
561  Instantiation I(*table);
562 
563  auto drawligne = [&]() {
564  if (nbparents > 0) {
565  for (Idx i = 1; i <= nbparents; i++)
566  ss << std::setw(colwidth)
567  << table->variable(i).label(I.val(i)).substr(0, colwidth) << "|";
568  ss << "|";
569  }
570  for (I.setFirstVar(var); !I.end(); I.incVar(var))
571  ss << " " << std::setw(numberwidth - 1) << table->get(I) << "|";
572  I.setFirstVar(var);
573  ss << std::endl;
574  };
575 
576  Size nbrLig = table->domainSize() / var.domainSize();
577  if (nbrLig < nbrLigMax * 2 + 1) {
578  for (I.setFirst(); !I.end(); I.incNotVar(var))
579  drawligne();
580  } else {
581  Size cpt = 0;
582  for (I.setFirst(); !I.end(); I.incNotVar(var)) {
583  cpt++;
584  if (cpt > nbrLigMax) break;
585  drawligne();
586  }
587  ss << "[..." << nbrLig - nbrLigMax * 2 << " more line(s) ...]"
588  << std::endl;
589  I.setLast();
590  for (Idx revi = 1; revi < nbrLigMax; revi++)
591  I.decNotVar(var);
592  for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
593  drawligne();
594  }
595  }
596 
597  return ss.str();
598  }
599 
600  ///@}
601 
602  protected:
603  Set< const DiscreteVariable* >
604  complementVars_(const Set< const DiscreteVariable* >& del_vars) const;
605  };
606 
607 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
608  extern template class Potential< double >;
609 #endif
610 
611  template < typename GUM_SCALAR >
612  inline Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR >& arg) {
613  return arg.new_log2();
614  }
615 
616  template < typename GUM_SCALAR >
617  inline Potential< GUM_SCALAR > abs(const Potential< GUM_SCALAR >& arg) {
618  return arg.new_abs();
619  }
620 
621  template < typename GUM_SCALAR >
622  inline Potential< GUM_SCALAR > sq(const Potential< GUM_SCALAR >& arg) {
623  return arg.new_sq();
624  }
625 
626 } /* namespace gum */
627 
628 #include <agrum/tools/multidim/potential_tpl.h>
629 #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:397
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:59
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
Potential< GUM_SCALAR > operator/(const Potential< GUM_SCALAR > &p2) const
the function to be used to divide two Potentials
Definition: potential.h:443
const Potential< GUM_SCALAR > & fillWith(const GUM_SCALAR &val) const
Automatically fills this MultiDimContainer with the value v.
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
Potential< GUM_SCALAR > abs(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:617
GUM_SCALAR min() const
min of all elements in the Potential
Potential< GUM_SCALAR > reorganize(const std::vector< std::string > &vars) const
create a new Potential with another order
Potential(MultiDimImplementation< GUM_SCALAR > *aContent)
Creates an potential around aContent.
Definition: potential_tpl.h:45
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Potential< GUM_SCALAR > putFirst(const std::string &varname) const
create a new Potential with a certain variable in first
Potential< GUM_SCALAR > & operator-=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:476
Potential< GUM_SCALAR > & operator-=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:480
const Potential< GUM_SCALAR > & fillWith(const std::vector< GUM_SCALAR > &data) const
Automatically fills the potential with the values in v.
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]
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
const Potential< GUM_SCALAR > new_abs() const
Create a new potential and apply abs on every element of the container.
const Potential< GUM_SCALAR > new_log2() const
Create a new potential and apply $log_2(x)$ on every element of the container.
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:485
Potential< GUM_SCALAR > & operator*=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:467
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) ...
const Potential< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
Set< Instantiation > argmin() const
set of instantiation corresponding to the min in the Potential
Potential< GUM_SCALAR > operator/(const GUM_SCALAR &v) const
the function to be used to divide a Potential by a scalar
Definition: potential.h:454
Potential< GUM_SCALAR > sq(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:622
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
const Potential< GUM_SCALAR > new_sq() const
Create a new potential and apply $x^2$ on every element of the container.
GUM_SCALAR product() const
product of all elements in the Potential
Potential< GUM_SCALAR > & operator=(Potential< GUM_SCALAR > &&src)
Default constructor.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src, const std::vector< std::string > &mapSrc) const
copy a Potential data using the sequence of names in mapSrc to find the corresponding variables...
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
bool operator==(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:494
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:94
Potential< GUM_SCALAR > & operator+=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:462
GUM_SCALAR max() const
max of all elements in the Potential
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Definition: potential.h:412
Potential< GUM_SCALAR > & operator*=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:471
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)
virtual std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:517
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:458
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Potential(MultiDimImplementation< GUM_SCALAR > *aContent, const MultiDimContainer< GUM_SCALAR > &src)
Copy constructor.
Definition: potential_tpl.h:72
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:407
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:489
Potential(Potential< GUM_SCALAR > &&from)
move constructor & assignement
Definition: potential_tpl.h:64
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
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:429
Potential(const Potential< GUM_SCALAR > &src)
Copy constructor & assignment.
Definition: potential_tpl.h:53
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:508
Potential< GUM_SCALAR > operator*(const GUM_SCALAR &v) const
the function to be used to multiply a Potential and a scalar
Definition: potential.h:438
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:37
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:424
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:612
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)