aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
potential.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 > margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
143 
144  /**
145  * Projection using sum as operation (and implementation-optimized
146  * operations)
147  * @param kept_vars is the set of vars to keep
148  */
149  Potential< GUM_SCALAR > margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
150 
151  /**
152  * Projection using multiplication as operation (and
153  * implementation-optimized operations)
154  * @param del_vars is the set of vars to eliminate
155  */
156  Potential< GUM_SCALAR > margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
157 
158  /**
159  * Projection using multiplication as operation (and
160  * implementation-optimized operations)
161  * @param kept_vars is the set of vars to keep
162  */
163  Potential< GUM_SCALAR > margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
164 
165  /**
166  * Projection using min as operation (and implementation-optimized
167  * operations)
168  * @param del_vars is the set of vars to eliminate
169  */
170  Potential< GUM_SCALAR > margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
171 
172  /**
173  * Projection using min as operation (and implementation-optimized
174  * operations)
175  * @param kept_vars is the set of vars to keep
176  */
177  Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
178 
179  /**
180  * Projection using max as operation (and implementation-optimized
181  * operations)
182  * @param del_vars is the set of vars to eliminate
183  */
184  Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
185 
186  /**
187  * Projection using max as operation (and implementation-optimized
188  * operations)
189  * @param kept_vars is the set of vars to keep
190  */
191  Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
192 
193  /**
194  * create a boolean-like potential using the predicate isNonZero
195  */
196  Potential< GUM_SCALAR > isNonZeroMap() const;
197 
198  /// sum of all elements in the Potential
199  GUM_SCALAR sum() const;
200  /// product of all elements in the Potential
201  GUM_SCALAR product() const;
202  /// max of all elements in the Potential
203  GUM_SCALAR max() const;
204  /// min of all elements in the Potential
205  GUM_SCALAR min() const;
206  /// max of all non one elements in the Potential
207  /// @warning can return 1 if no other value than 1 ...
208  GUM_SCALAR maxNonOne() const;
209  /// min of all non zero elements in the Potential
210  /// @warning can return 0 if no other value than 0 ...
211  GUM_SCALAR minNonZero() const;
212 
213  /// set of instantiation corresponding to the parameter v in the Potential
215  /// set of instantiation corresponding to the max in the Potential
216  Set< Instantiation > argmax() const;
217  /// set of instantiation corresponding to the min in the Potential
218  Set< Instantiation > argmin() const;
219 
220  /// entropy of the Potential
221  GUM_SCALAR entropy() const;
222 
223  /** create a new Potential with another order
224  * @throw InvalidArgument if not all and only the vars of the potential are
225  * in vars
226  */
227  Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable* >& vars) const;
228 
229  /** create a new Potential with another order
230  * @throw InvalidArgument if not all and only the vars of the potential are
231  * in vars
232  */
233  Potential< GUM_SCALAR > reorganize(const std::vector< std::string >& vars) const;
234 
235  /** create a new Potential extracted from *this given a partial
236  * instantiation
237  */
238  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
239 
240  /** create a new Potential with a certain variable in first
241  * @throw InvalidArgument if the var is not in the potential
242  */
243  Potential< GUM_SCALAR > putFirst(const DiscreteVariable* var) const;
244 
245  /** create a new Potential with a certain variable in first
246  * @throw InvalidArgument if the var is not in the potential
247  */
248  Potential< GUM_SCALAR > putFirst(const std::string& varname) const;
249 
250  /**
251  * @brief copy a Potential data using name of variables and labels (not
252  * necessarily the same variables in the same orders)
253  *
254  * @warning a strict control on names of variables and labels are made
255  *
256  * @throw InvalidArgument if the Potential is not compatible with this
257  */
258  const Potential< GUM_SCALAR >& fillWith(const Potential< GUM_SCALAR >& src) const;
259 
260  /**
261  * @brief copy a Potential data using the sequence of names in mapSrc to find
262  * the corresponding variables.
263  *
264  * For instance, to copy the potential P(A,B,C) in Q(D,E,A) with the mapping
265  * P.A<->Q.E, P.B<->Q.A, P.C<->Q.D (assuming that the corresponding variables
266  * have the same domain size and the order of labels):
267  *
268  * @code
269  * Q.fillWith(P,{"C","A","B"});
270  * @endcode
271  *
272  * @warning a strict control on names of variables and labels are made
273  *
274  * @throw InvalidArgument if the Potential is not compatible with this
275  * */
276  const Potential< GUM_SCALAR >& fillWith(const Potential< GUM_SCALAR >& src,
277  const std::vector< std::string >& mapSrc) const;
278 
279  /**
280  * @brief Automatically fills the potential with the values in
281  * v.
282  *
283  * @param v Vector of values.
284  * @throw SizeError Raised if v size's does not matches this
285  * MultiDimContainer domain size.
286  */
287  const Potential< GUM_SCALAR >& fillWith(const std::vector< GUM_SCALAR >& data) const;
288 
289  /**
290  * @brief Automatically fills this MultiDimContainer with the value v
291  *
292  * @param v contains the data.
293  */
294  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& val) const;
295 
296 
297  /**
298  * @brief Apply abs on every element of the container
299  */
300  const Potential< GUM_SCALAR >& abs() const;
301 
302  /**
303  * @brief apply $x^2$ on every element of the container
304  */
305  const Potential< GUM_SCALAR >& sq() const;
306 
307  /**
308  * @brief apply $log_2(x)$ on every element of the container
309  */
310  const Potential< GUM_SCALAR >& log2() const;
311 
312  /**
313  * @brief Create a new potential and apply abs on every element of the
314  * container
315  */
316  const Potential< GUM_SCALAR > new_abs() const;
317 
318  /**
319  * @brief Create a new potential and apply $x^2$ on every element of the
320  * container
321  */
322  const Potential< GUM_SCALAR > new_sq() const;
323 
324  /**
325  * @brief Create a new potential and apply $log_2(x)$ on every element of the
326  * container
327  */
328  const Potential< GUM_SCALAR > new_log2() const;
329 
330 
331  /**
332  * @brief normalisation of this do nothing if sum is 0
333  */
334  const Potential< GUM_SCALAR >& normalize() const;
335 
336  /**
337  * @brief compute KL divergence between this and p
338  * Checks the compatibility and then compute KL divergence
339  * @throws gum::InvalidArgument if p is not compatible with $this (dimension,
340  * variables)
341  * @throws gum::FatalError if a zero is found in p or this and not in the
342  * other.
343  */
344  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
345 
346  /**
347  * @brief normalisation of this as a CPT for the variable varId
348  *
349  * If the Potential is empty, the argument is not used.
350  *
351  * @throw FatalError it some distribution sums to 0, or if varId>=nbrDim()
352  */
353  const Potential< GUM_SCALAR >& normalizeAsCPT(const Idx& varId = 0) const;
354 
355  /**
356  * @brief multiply (each value of) *this by v
357  */
358  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
359 
360  /**
361  * @brief add v to (each value of) *this
362  */
363  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
364 
365  /**
366  * @brief the function to inverse (each value of) *this
367  */
368  const Potential< GUM_SCALAR >& inverse(void) const;
369 
370  /**
371  * @brief get a value at random from a 1-D distribution
372  */
373  Idx draw() const;
374 
375  ///@}
376 
377  // ========================================================================
378  /// @name Potential algebra operators
379  // ========================================================================
380  ///@{
381 
382 
383  /// the function to be used to add two Potentials
384  Potential< GUM_SCALAR > operator+(const Potential< GUM_SCALAR >& p2) const {
385  if (p2.empty()) return Potential< GUM_SCALAR >(*this).translate(p2.empty_value_);
386  if (this->empty()) return Potential< GUM_SCALAR >(p2).translate(this->empty_value_);
387 
388  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
389  }
390 
391  /// the function to be used to add a GUM_SCALAR to a Potential
392  Potential< GUM_SCALAR > operator+(const GUM_SCALAR& v) const {
393  return Potential< GUM_SCALAR >(*this).translate(v);
394  }
395 
396  /// the function to be used to subtract two Potentials
397  Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR >& p2) const {
398  if (p2.empty()) return Potential< GUM_SCALAR >(*this).translate(-p2.empty_value_);
399  if (this->empty()) {
400  auto p = Potential< GUM_SCALAR >(p2);
401  p.apply([this](GUM_SCALAR x) { return this->empty_value_ - x; });
402  return p;
403  }
404  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
405  }
406 
407  /// the function to be used to substract a GUM_SCALAR from a Potential
408  Potential< GUM_SCALAR > operator-(const GUM_SCALAR& v) const {
409  return Potential< GUM_SCALAR >(*this).translate(-v);
410  }
411 
412  /// the function to be used to multiply two Potentials
413  Potential< GUM_SCALAR > operator*(const Potential< GUM_SCALAR >& p2) const {
414  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2.empty_value_);
415  if (this->empty()) return Potential< GUM_SCALAR >(p2).scale(this->empty_value_);
416 
417  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
418  }
419 
420  /// the function to be used to multiply a Potential and a scalar
421  Potential< GUM_SCALAR > operator*(const GUM_SCALAR& v) const {
422  return Potential< GUM_SCALAR >(*this).scale(v);
423  }
424 
425  /// the function to be used to divide two Potentials
426  Potential< GUM_SCALAR > operator/(const Potential< GUM_SCALAR >& p2) const {
427  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
428  if (this->empty()) {
429  auto p = Potential< GUM_SCALAR >(p2);
430  p.apply([this](GUM_SCALAR x) { return this->empty_value_ / x; });
431  return p;
432  }
433  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
434  }
435  /// the function to be used to divide a Potential by a scalar
436  Potential< GUM_SCALAR > operator/(const GUM_SCALAR& v) const {
437  return Potential< GUM_SCALAR >(*this).scale(1 / v);
438  }
439 
440  Potential< GUM_SCALAR >& operator+=(const Potential< GUM_SCALAR >& r) {
441  *this = *this + r;
442  return *this;
443  }
444  Potential< GUM_SCALAR >& operator+=(const GUM_SCALAR& v) {
445  this->translate(v);
446  return *this;
447  }
448 
449  Potential< GUM_SCALAR >& operator*=(const Potential< GUM_SCALAR >& r) {
450  *this = *this * r;
451  return *this;
452  }
453  Potential< GUM_SCALAR >& operator*=(const GUM_SCALAR& v) {
454  this->scale(v);
455  return *this;
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(1 / v);
473  return *this;
474  }
475 
476  bool operator==(const Potential< GUM_SCALAR >& r) const {
477  if (this->empty()) {
478  if (r.empty())
479  return this->empty_value_ == r.empty_value_;
480  else
481  return false;
482  } else {
483  if (r.empty())
484  return false;
485  else
486  return (*this->content_) == (*r.content_);
487  }
488  }
489 
490  bool operator!=(const Potential< GUM_SCALAR >& r) const { return !operator==(r); }
491 
492  Potential< GUM_SCALAR >& operator<<(const DiscreteVariable& v) {
493  this->add(v);
494  return *this;
495  }
496 
497  virtual std::string toString() const {
498  auto table = this->content();
499  if (table->nbrDim() == 0) { return "[]"; }
500  const Size colwidth = 6;
501  const Size numberwidth = 9;
502  const Size nbrLigMax = 6;
503 
504  std::stringstream ss;
505  ss << std::left << std::fixed << std::endl;
506  ss.precision(numberwidth - 5);
507 
508  const auto& var = table->variable(0);
509 
510  const Size nbparents = table->nbrDim() - 1;
511  const Size nbcol = var.domainSize();
512  const std::string maskparent(colwidth, '-');
513  const std::string masknumber(numberwidth, '-');
514 
515  if (nbparents > 0)
516  ss << std::setw(nbparents * (colwidth + 1) - 1) << " "
517  << "||";
518  ss << " " << std::setw(nbcol * (numberwidth + 1) - 3)
519  << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << "|";
520  ss << std::endl;
521 
522  if (nbparents > 0) {
523  for (Idx i = 1; i <= nbparents; i++)
524  ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth) << "|";
525  ss << "|";
526  }
527  for (Idx i = 0; i < nbcol; i++)
528  ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << "|";
529  ss << std::endl;
530 
531 
532  if (nbparents > 0) {
533  for (Idx i = 1; i <= nbparents; i++)
534  ss << maskparent << "|";
535  ss << "|";
536  }
537  for (Idx i = 0; i < nbcol; i++)
538  ss << masknumber << "|";
539  ss << std::endl;
540  Instantiation I(*table);
541 
542  auto drawligne = [&]() {
543  if (nbparents > 0) {
544  for (Idx i = 1; i <= nbparents; i++)
545  ss << std::setw(colwidth) << table->variable(i).label(I.val(i)).substr(0, colwidth)
546  << "|";
547  ss << "|";
548  }
549  for (I.setFirstVar(var); !I.end(); I.incVar(var))
550  ss << " " << std::setw(numberwidth - 1) << table->get(I) << "|";
551  I.setFirstVar(var);
552  ss << std::endl;
553  };
554 
555  Size nbrLig = table->domainSize() / var.domainSize();
556  if (nbrLig < nbrLigMax * 2 + 1) {
557  for (I.setFirst(); !I.end(); I.incNotVar(var))
558  drawligne();
559  } else {
560  Size cpt = 0;
561  for (I.setFirst(); !I.end(); I.incNotVar(var)) {
562  cpt++;
563  if (cpt > nbrLigMax) break;
564  drawligne();
565  }
566  ss << "[..." << nbrLig - nbrLigMax * 2 << " more line(s) ...]" << std::endl;
567  I.setLast();
568  for (Idx revi = 1; revi < nbrLigMax; revi++)
569  I.decNotVar(var);
570  for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
571  drawligne();
572  }
573  }
574 
575  return ss.str();
576  }
577 
578  ///@}
579 
580  protected:
581  Set< const DiscreteVariable* >
582  complementVars_(const Set< const DiscreteVariable* >& del_vars) const;
583  };
584 
585 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
586  extern template class Potential< double >;
587 #endif
588 
589  template < typename GUM_SCALAR >
590  inline Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR >& arg) {
591  return arg.new_log2();
592  }
593 
594  template < typename GUM_SCALAR >
595  inline Potential< GUM_SCALAR > abs(const Potential< GUM_SCALAR >& arg) {
596  return arg.new_abs();
597  }
598 
599  template < typename GUM_SCALAR >
600  inline Potential< GUM_SCALAR > sq(const Potential< GUM_SCALAR >& arg) {
601  return arg.new_sq();
602  }
603 
604 } /* namespace gum */
605 
606 #include <agrum/tools/multidim/potential_tpl.h>
607 #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:384
~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:426
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:595
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:44
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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:458
Potential< GUM_SCALAR > & operator-=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:462
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:467
Potential< GUM_SCALAR > & operator*=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:449
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:436
Potential< GUM_SCALAR > sq(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:600
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.
Definition: potential_tpl.h:98
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:476
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:89
Potential< GUM_SCALAR > & operator+=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:444
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:397
Potential< GUM_SCALAR > & operator*=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:453
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:497
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:440
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:69
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:392
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:471
Potential(Potential< GUM_SCALAR > &&from)
move constructor & assignement
Definition: potential_tpl.h:62
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:413
Potential(const Potential< GUM_SCALAR > &src)
Copy constructor & assignment.
Definition: potential_tpl.h:51
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:490
Potential< GUM_SCALAR > operator*(const GUM_SCALAR &v) const
the function to be used to multiply a Potential and a scalar
Definition: potential.h:421
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:408
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:590
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)