aGrUM  0.14.2
potential_tpl.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  ***************************************************************************/
26 #include <agrum/agrum.h>
27 #include <agrum/core/math/math.h>
29 
30 namespace gum {
31 
32  // Default constructor: creates an empty null dimensional matrix
33  // choose a MultiDimArray<> as decorated implementation
34  template < typename GUM_SCALAR >
36  MultiDimDecorator< GUM_SCALAR >(new MultiDimArray< GUM_SCALAR >(),
37  GUM_SCALAR(1)) {
38  GUM_CONSTRUCTOR(Potential);
39  }
40 
41  // constructor using aContent as content
42  template < typename GUM_SCALAR >
45  MultiDimDecorator< GUM_SCALAR >(aContent, GUM_SCALAR(1)) {
46  // for debugging purposes
47  GUM_CONSTRUCTOR(Potential);
48  }
49  // copy constructor
50  template < typename GUM_SCALAR >
52  Potential< GUM_SCALAR >(static_cast< MultiDimImplementation< GUM_SCALAR >* >(
53  src.content()->newFactory()),
54  *(src.content())) {
55  this->_empty_value = src._empty_value;
56  // todo how to optimize copy of content ?
57  // GUM_CONS_CPY not here because in called Potential
58  // GUM_CONS_CPY( Potential );
59  }
60 
62  template < typename GUM_SCALAR >
64  MultiDimDecorator< GUM_SCALAR >(
65  std::forward< MultiDimDecorator< GUM_SCALAR > >(from)) {
66  GUM_CONS_MOV(Potential);
67  }
68 
69  // complex copy constructor : we choose the implementation
70  template < typename GUM_SCALAR >
74  MultiDimDecorator< GUM_SCALAR >(aContent) {
75  // for debugging purposes
76  GUM_CONSTRUCTOR(Potential);
77 
78  if (!src.empty()) {
79  this->beginMultipleChanges();
80 
81  for (Idx i = 0; i < src.variablesSequence().size(); i++) {
82  this->add(*(src.variablesSequence()[i]));
83  }
84 
85  this->endMultipleChanges();
86  this->content()->copyFrom(*src.content());
87  }
88  }
89 
90  // operator = copy
91  template < typename GUM_SCALAR >
95  GUM_OP_CPY(Potential);
96  return *this;
97  }
98 
99  // operator = move
100  template < typename GUM_SCALAR >
104  std::forward< MultiDimDecorator< GUM_SCALAR > >(src));
105  GUM_OP_MOV(Potential);
106  return *this;
107  }
108 
109  // destructor
110 
111  template < typename GUM_SCALAR >
113  // for debugging purposes
114  GUM_DESTRUCTOR(Potential);
115  }
116 
117  template < typename GUM_SCALAR >
119  return new Potential< GUM_SCALAR >(
120  static_cast< MultiDimImplementation< GUM_SCALAR >* >(
121  this->content()->newFactory()));
122  }
123 
124  // sum of all elements in this
125  template < typename GUM_SCALAR >
126  INLINE GUM_SCALAR Potential< GUM_SCALAR >::sum() const {
127  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
128  return this->_empty_value;
129  }
130  return gum::projectSum(*this->content());
131  }
132  // product of all elements in this
133  template < typename GUM_SCALAR >
134  INLINE GUM_SCALAR Potential< GUM_SCALAR >::product() const {
135  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
136  return this->_empty_value;
137  }
138  return gum::projectProduct(*this->content());
139  }
140  // max of all elements in this
141  template < typename GUM_SCALAR >
142  INLINE GUM_SCALAR Potential< GUM_SCALAR >::max() const {
143  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
144  return this->_empty_value;
145  }
146  return gum::projectMax(*this->content());
147  }
148  // min of all elements in this
149  template < typename GUM_SCALAR >
150  INLINE GUM_SCALAR Potential< GUM_SCALAR >::min() const {
151  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
152  return this->_empty_value;
153  }
154  return gum::projectMin(*this->content());
155  }
156 
157  // max of all non one elements in this
158  template < typename GUM_SCALAR >
160  GUM_SCALAR res;
161 
162  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
163  res = this->_empty_value;
164  } else {
165  res = this->reduce(
166  [](GUM_SCALAR z, GUM_SCALAR p) {
167  return (p == static_cast< GUM_SCALAR >(1))
168  ? z
169  : (z == static_cast< GUM_SCALAR >(1)) ? p : (p > z ? p : z);
170  },
171  static_cast< GUM_SCALAR >(1));
172  }
173 
174  if (res == static_cast< GUM_SCALAR >(1))
175  GUM_ERROR(NotFound, "No other value than 1");
176 
177  return res;
178  }
179 
180  // min of all non zero elements in this
181  template < typename GUM_SCALAR >
182  INLINE GUM_SCALAR Potential< GUM_SCALAR >::minNonZero() const {
183  GUM_SCALAR res;
184 
185  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
186  res = this->_empty_value;
187  } else {
188  res = this->reduce(
189  [](GUM_SCALAR z, GUM_SCALAR p) {
190  return (p == static_cast< GUM_SCALAR >(0))
191  ? z
192  : (z == static_cast< GUM_SCALAR >(0)) ? p : (p < z ? p : z);
193  },
194  static_cast< GUM_SCALAR >(0));
195  }
196 
197  if (res == static_cast< GUM_SCALAR >(0))
198  GUM_ERROR(NotFound, "No other value than 1");
199 
200  return res;
201  }
202 
203  // entropy of this
204  template < typename GUM_SCALAR >
205  INLINE GUM_SCALAR Potential< GUM_SCALAR >::entropy() const {
206  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
207  return static_cast< GUM_SCALAR >(0);
208  }
209 
210  return this->reduce(
211  [](GUM_SCALAR z, GUM_SCALAR p) {
212  return (p == 0.0) ? z : (z - p * log2(p));
213  },
214  0.0);
215  }
216 
217  template < typename GUM_SCALAR >
218  INLINE const Potential< GUM_SCALAR >&
219  Potential< GUM_SCALAR >::fillWith(const std::vector< GUM_SCALAR >& v) const {
220  this->populate(v);
221  return *this;
222  }
223 
224  template < typename GUM_SCALAR >
225  INLINE const Potential< GUM_SCALAR >&
226  Potential< GUM_SCALAR >::fillWith(const GUM_SCALAR& v) const {
227  this->fill(v);
228  return *this;
229  }
230 
231  template < typename GUM_SCALAR >
232  INLINE const Potential< GUM_SCALAR >&
234  if (src.domainSize() != this->domainSize()) {
235  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
236  }
237  gum::Set< std::string > son; // set of names
238  for (const auto& v : src.variablesSequence()) {
239  son.insert(v->name());
240  }
241  for (const auto& v : this->variablesSequence()) {
242  if (!son.contains(v->name())) {
244  "Variable <" << v->name() << "> not present in src.");
245  }
246  // we check size, labels and order of labels in the same time
247  if (v->toString() != src.variable(v->name()).toString()) {
249  "Variables <" << v->name() << "> are not identical.");
250  }
251  }
252 
253  Instantiation Isrc(src);
254  Instantiation Idst(*this);
255  for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
256  for (Idx i = 0; i < this->nbrDim(); i++) {
257  Idst.chgVal(Isrc.variable(i).name(), Isrc.val(i));
258  }
259  this->set(Idst, src.get(Isrc));
260  }
261 
262  return *this;
263  }
264 
265  template < typename GUM_SCALAR >
267  const Potential< GUM_SCALAR >& src,
268  const std::vector< std::string >& mapSrc) const {
269  if (src.nbrDim() != this->nbrDim()) {
270  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
271  }
272  if (src.nbrDim() != mapSrc.size()) {
274  "Potential and vector have not the same dimension.");
275  }
276  Instantiation Isrc;
277  for (Idx i = 0; i < src.nbrDim(); i++) {
278  if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
280  "Variables " << mapSrc[i] << " (in the argument) and "
281  << this->variable(i).name()
282  << " have not the same dimension.");
283  } else {
284  Isrc.add(src.variable(mapSrc[i]));
285  }
286  }
287  Instantiation Idst(*this);
288  for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
289  this->set(Idst, src.get(Isrc));
290  }
291 
292  return *this;
293  }
294 
295  template < typename GUM_SCALAR >
297  this->apply([](GUM_SCALAR x) { return x * x; });
298  return *this;
299  }
300 
301  template < typename GUM_SCALAR >
302  INLINE GUM_SCALAR
304  if (this->nbrDim() != p.nbrDim())
305  GUM_ERROR(
307  "BNdistance between potentials with different numbers of dimensions");
308  for (const auto var : p.variablesSequence()) {
309  if (!this->contains(*var))
311  "A variable in the argument does not belong to the potential.");
312  }
313  for (const auto var : this->variablesSequence()) {
314  if (!p.contains(*var))
315  GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.");
316  }
317 
318  Instantiation inst(*this);
319  GUM_SCALAR res = static_cast< GUM_SCALAR >(0);
320  for (inst.setFirst(); !inst.end(); inst.inc()) {
321  GUM_SCALAR x = this->get(inst);
322  GUM_SCALAR y = p.get(inst);
323  if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
324  continue;
325 
326  if (static_cast< GUM_SCALAR >(0) == y)
327  // we know that x!=0;
329  "The argument has a 0 at " << inst
330  << " while the potential has not.")
331 
332  res += x * log2(x / y);
333  }
334  return res;
335  }
336 
337  template < typename GUM_SCALAR >
339  this->apply([](GUM_SCALAR x) {
340  if (x >= 0)
341  return x;
342  else
343  return -x;
344  });
345  return *this;
346  }
347 
348  // normalisation of this
349  // do nothing is sum is 0
350  template < typename GUM_SCALAR >
351  INLINE const Potential< GUM_SCALAR >&
353  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
354  if (this->_empty_value != static_cast< GUM_SCALAR >(0))
355  this->_empty_value = static_cast< GUM_SCALAR >(1.0);
356  } else {
357  GUM_SCALAR s = sum();
358 
359  if (s != (GUM_SCALAR)0) {
360  this->apply([s](GUM_SCALAR x) { return x / s; });
361  }
362  }
363  return *this;
364  }
365 
366  template < typename GUM_SCALAR >
368  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
369  if (this->_empty_value != static_cast< GUM_SCALAR >(0)) {
370  this->_empty_value = static_cast< GUM_SCALAR >(1.0);
371  } else {
373  "Normalization for a potential that sum to 0 in " << *this);
374  }
375  } else {
376  Instantiation inst(*this);
377  const auto& v = this->variable(0);
378 
379  for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
380  GUM_SCALAR s = (GUM_SCALAR)0.0;
381  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
382  s += this->get(inst);
383  if (s == (GUM_SCALAR)0.0) {
385  "Normalization for a potential that sum to 0 in " << *this);
386  }
387  if (s != (GUM_SCALAR)1.0) {
388  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
389  this->set(inst, this->get(inst) / s);
390  }
391  inst.setFirstVar(v); // to remove inst.end()
392  }
393  }
394  }
395 
396  template < typename GUM_SCALAR >
397  INLINE const Potential< GUM_SCALAR >&
398  Potential< GUM_SCALAR >::scale(GUM_SCALAR v) const {
399  this->apply([v](GUM_SCALAR x) { return x * v; });
400  return *this;
401  }
402 
403  template < typename GUM_SCALAR >
404  INLINE const Potential< GUM_SCALAR >&
406  this->apply([v](GUM_SCALAR x) { return x + v; });
407  return *this;
408  }
409 
410  template < typename GUM_SCALAR >
412  const Set< const DiscreteVariable* >& del_vars) const {
413  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
415  }
416  return Potential< GUM_SCALAR >(gum::projectSum(*this->content(), del_vars));
417  }
418 
419  template < typename GUM_SCALAR >
421  const Set< const DiscreteVariable* >& del_vars) const {
422  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
424  }
426  gum::projectProduct(*this->content(), del_vars));
427  }
428 
429  template < typename GUM_SCALAR >
431  const Set< const DiscreteVariable* >& del_vars) const {
432  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
434  }
435  return Potential< GUM_SCALAR >(gum::projectMin(*this->content(), del_vars));
436  }
437 
438  template < typename GUM_SCALAR >
440  const Set< const DiscreteVariable* >& del_vars) const {
441  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
443  }
444  return Potential< GUM_SCALAR >(gum::projectMax(*this->content(), del_vars));
445  }
446  template < typename GUM_SCALAR >
448  const Set< const DiscreteVariable* >& kept_vars) const {
449  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
451  }
453  gum::projectSum(*this->content(), _complementVars(kept_vars)));
454  }
455 
456  template < typename GUM_SCALAR >
458  const Set< const DiscreteVariable* >& kept_vars) const {
459  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
461  }
463  gum::projectProduct(*this->content(), _complementVars(kept_vars)));
464  }
465 
466  template < typename GUM_SCALAR >
468  const Set< const DiscreteVariable* >& kept_vars) const {
469  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
471  }
473  gum::projectMin(*this->content(), _complementVars(kept_vars)));
474  }
475 
476  template < typename GUM_SCALAR >
478  const Set< const DiscreteVariable* >& kept_vars) const {
479  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
481  }
483  gum::projectMax(*this->content(), _complementVars(kept_vars)));
484  }
485 
486  template < typename GUM_SCALAR >
488  auto p = Potential< GUM_SCALAR >(*this);
489  p.apply([](GUM_SCALAR x) {
490  if (x != static_cast< GUM_SCALAR >(0))
491  return static_cast< GUM_SCALAR >(1);
492  else
493  return static_cast< GUM_SCALAR >(0);
494  });
495  return p;
496  }
497 
498  template < typename GUM_SCALAR >
500  const Set< const DiscreteVariable* >& vars) const {
502 
503  for (const auto x : this->variablesSequence())
504  if (!vars.contains(x)) cplt.insert(x);
505 
506  return cplt;
507  }
508 
509  template < typename GUM_SCALAR >
511  const std::vector< const DiscreteVariable* >& vars) const {
512  if (vars.size() != this->nbrDim())
514  "The vector contains " << vars.size() << " variables instead of "
515  << this->nbrDim() << ".");
516  for (const auto var : vars) {
517  if (!this->contains(*var))
519  "A variable in the vector does not belong to the potential.");
520  }
521 
524  for (const auto var : vars)
525  p.add(*var);
526  p.endMultipleChanges();
527  p.copyFrom(*this, nullptr); // copy *this in p using the same order
528 
529  return p;
530  }
531 
532  template < typename GUM_SCALAR >
535  if (!this->contains(*var)) {
537  "The variable to put first does not belong to the potential");
538  }
539 
540  std::vector< const DiscreteVariable* > vars;
541  vars.push_back(var);
542  for (Idx i = 0; i < this->nbrDim(); i++)
543  if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
544 
545  return this->reorganize(vars);
546  }
547 
548  template < typename GUM_SCALAR >
552  p.extractFrom(*this, inst);
553 
554  return p;
555  }
556 
557  template < typename GUM_SCALAR >
559  if (this->nbrDim() != 1) {
560  GUM_ERROR(FatalError, "To draw from a potential, the dimension must be 1")
561  }
562 
563  GUM_SCALAR r = static_cast< GUM_SCALAR >(randomProba());
564  Instantiation Ip(*this);
565  for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
566  r -= this->get(Ip);
567  if (r <= 0) return Ip.val(0);
568  }
569  return this->variable(0).domainSize() - 1;
570  }
571 
572  template < typename GUM_SCALAR >
573  std::ostream& operator<<(std::ostream& out,
574  const Potential< GUM_SCALAR >& array) {
575  out << array.toString();
576  return out;
577  }
578 
579  // argmax of all elements in this
580  template < typename GUM_SCALAR >
582  Instantiation I(*this);
584 
585  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->_content)->empty()) {
586  return res;
587  }
588  for (I.setFirst(); !I.end(); ++I) {
589  if (this->get(I) == v) res.insert(I);
590  }
591  return res;
592  }
593  // argmax of all elements in this
594  template < typename GUM_SCALAR >
596  return findAll(max());
597  }
598  // argmin of all elements in this
599  template < typename GUM_SCALAR >
601  return findAll(min());
602  }
603 
604 } /* namespace gum */
Useful macros for maths.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimImplementation< GUM_SCALAR > * _content
The true container.
virtual bool empty() const =0
Returns true if no var is in *this.
virtual Size domainSize() const final
Returns the product of the variables domain size.
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
GUM_SCALAR min() const
min of all elements in the Potential
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
double randomProba()
Returns a random double between 0 and 1 included (i.e.
STL namespace.
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
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Base class for discrete random variable.
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) ...
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
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
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
virtual bool empty() const final
Returns true if no var is in *this.
Idx val(Idx i) const
Returns the current value of the variable at position i.
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
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
GUM_SCALAR product() const
product of all elements in the Potential
virtual Size domainSize() const =0
Potential< GUM_SCALAR > margProdIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
Header of the Potential class.
const Potential< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
void inc()
Operator increment.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
Idx draw() const
get a value at random from a 1-D distribution
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
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
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:51
GUM_SCALAR max() const
max of all elements in the Potential
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
const Potential< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using max as operation (and implementation-optimized operations)
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
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)
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 projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
GUM_SCALAR sum() const
sum of all elements in the Potential
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
virtual const std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:418
void setFirst()
Assign the first values to the tuple of the Instantiation.
<agrum/multidim/multiDimImplementation.h>
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
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:50
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)
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
create a new potential added with v from *this
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
Potential()
Default constructor.
Definition: potential_tpl.h:35
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
Potential< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Potential with a certain variable in first
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
bool end() const
Returns true if the Instantiation reached the end.
virtual void extractFrom(const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)