aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::Potential< GUM_SCALAR > Class Template Reference

aGrUM's Potential is a multi-dimensional array with tensor operators. More...

#include <agrum/tools/multidim/potential.h>

+ Inheritance diagram for gum::Potential< GUM_SCALAR >:
+ Collaboration diagram for gum::Potential< GUM_SCALAR >:

Public Member Functions

Constructors, Destructors and Copy
 Potential ()
 Default constructor. More...
 
 Potential (MultiDimImplementation< GUM_SCALAR > *aContent)
 Creates an potential around aContent. More...
 
 Potential (MultiDimImplementation< GUM_SCALAR > *aContent, const MultiDimContainer< GUM_SCALAR > &src)
 Copy constructor. More...
 
 Potential (const Potential< GUM_SCALAR > &src)
 Copy constructor & assignment. More...
 
Potential< GUM_SCALAR > & operator= (const Potential< GUM_SCALAR > &src)
 Default constructor. More...
 
 Potential (Potential< GUM_SCALAR > &&from)
 move constructor & assignement More...
 
Potential< GUM_SCALAR > & operator= (Potential< GUM_SCALAR > &&src)
 Default constructor. More...
 
 ~Potential ()
 Destructor. More...
 
MultiDimContainer implementation
virtual Potential< GUM_SCALAR > * newFactory () const
 Default implementation of MultiDimContainer::set(). More...
 
Class operation for Potential instances
const Potential< GUM_SCALAR > & random () const
 generate a random Potential with each parameter in [0,1] More...
 
const Potential< GUM_SCALAR > & randomDistribution () const
 generate a random Distribution in the Potential More...
 
const Potential< GUM_SCALAR > & randomCPT () const
 generate a random CPT in the Potential More...
 
const Potential< GUM_SCALAR > & noising (GUM_SCALAR alpha) const
 add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT() More...
 
Potential< GUM_SCALAR > margSumOut (const Set< const DiscreteVariable * > &del_vars) const
 Projection using sum as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margSumIn (const Set< const DiscreteVariable * > &kept_vars) const
 Projection using sum as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margProdOut (const Set< const DiscreteVariable * > &del_vars) const
 Projection using multiplication as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margProdIn (const Set< const DiscreteVariable * > &kept_vars) const
 Projection using multiplication as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margMinOut (const Set< const DiscreteVariable * > &del_vars) const
 Projection using min as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margMinIn (const Set< const DiscreteVariable * > &kept_vars) const
 Projection using min as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margMaxOut (const Set< const DiscreteVariable * > &del_vars) const
 Projection using max as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > margMaxIn (const Set< const DiscreteVariable * > &kept_vars) const
 Projection using max as operation (and implementation-optimized operations) More...
 
Potential< GUM_SCALAR > isNonZeroMap () const
 create a boolean-like potential using the predicate isNonZero More...
 
GUM_SCALAR sum () const
 sum of all elements in the Potential More...
 
GUM_SCALAR product () const
 product of all elements in the Potential More...
 
GUM_SCALAR max () const
 max of all elements in the Potential More...
 
GUM_SCALAR min () const
 min of all elements in the Potential More...
 
GUM_SCALAR maxNonOne () const
 max of all non one elements in the Potential More...
 
GUM_SCALAR minNonZero () const
 min of all non zero elements in the Potential More...
 
Set< InstantiationfindAll (GUM_SCALAR v) const
 set of instantiation corresponding to the parameter v in the Potential More...
 
Set< Instantiationargmax () const
 set of instantiation corresponding to the max in the Potential More...
 
Set< Instantiationargmin () const
 set of instantiation corresponding to the min in the Potential More...
 
GUM_SCALAR entropy () const
 entropy of the Potential More...
 
Potential< GUM_SCALAR > reorganize (const std::vector< const DiscreteVariable * > &vars) const
 create a new Potential with another order More...
 
Potential< GUM_SCALAR > reorganize (const std::vector< std::string > &vars) const
 create a new Potential with another order More...
 
Potential< GUM_SCALAR > extract (const Instantiation &inst) const
 create a new Potential extracted from *this given a partial instantiation More...
 
Potential< GUM_SCALAR > putFirst (const DiscreteVariable *var) const
 create a new Potential with a certain variable in first More...
 
Potential< GUM_SCALAR > putFirst (const std::string &varname) const
 create a new Potential with a certain variable in first More...
 
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 same orders) More...
 
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. More...
 
const Potential< GUM_SCALAR > & fillWith (const std::vector< GUM_SCALAR > &data) const
 Automatically fills the potential with the values in v. More...
 
const Potential< GUM_SCALAR > & fillWith (const GUM_SCALAR &val) const
 Automatically fills this MultiDimContainer with the value v. More...
 
const Potential< GUM_SCALAR > & abs () const
 Apply abs on every element of the container. More...
 
const Potential< GUM_SCALAR > & sq () const
 apply $x^2$ on every element of the container More...
 
const Potential< GUM_SCALAR > & log2 () const
 apply $log_2(x)$ on every element of the container More...
 
const Potential< GUM_SCALAR > new_abs () const
 Create a new potential and apply abs on every element of the container. More...
 
const Potential< GUM_SCALAR > new_sq () const
 Create a new potential and apply $x^2$ on every element of the container. More...
 
const Potential< GUM_SCALAR > new_log2 () const
 Create a new potential and apply $log_2(x)$ on every element of the container. More...
 
const Potential< GUM_SCALAR > & normalize () const
 normalisation of this do nothing if sum is 0 More...
 
GUM_SCALAR KL (const Potential< GUM_SCALAR > &p) const
 compute KL divergence between this and p Checks the compatibility and then compute KL divergence More...
 
const Potential< GUM_SCALAR > & normalizeAsCPT (const Idx &varId=0) const
 normalisation of this as a CPT for the variable varId More...
 
const Potential< GUM_SCALAR > & scale (GUM_SCALAR v) const
 multiply (each value of) *this by v More...
 
const Potential< GUM_SCALAR > & translate (GUM_SCALAR v) const
 add v to (each value of) *this More...
 
const Potential< GUM_SCALAR > & inverse (void) const
 the function to inverse (each value of) *this More...
 
Idx draw () const
 get a value at random from a 1-D distribution More...
 
Potential algebra operators
Potential< GUM_SCALAR > operator+ (const Potential< GUM_SCALAR > &p2) const
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > operator+ (const GUM_SCALAR &v) const
 the function to be used to add a GUM_SCALAR to a Potential More...
 
Potential< GUM_SCALAR > operator- (const Potential< GUM_SCALAR > &p2) const
 the function to be used to subtract two Potentials More...
 
Potential< GUM_SCALAR > operator- (const GUM_SCALAR &v) const
 the function to be used to substract a GUM_SCALAR from a Potential More...
 
Potential< GUM_SCALAR > operator* (const Potential< GUM_SCALAR > &p2) const
 the function to be used to multiply two Potentials More...
 
Potential< GUM_SCALAR > operator* (const GUM_SCALAR &v) const
 the function to be used to multiply a Potential and a scalar More...
 
Potential< GUM_SCALAR > operator/ (const Potential< GUM_SCALAR > &p2) const
 the function to be used to divide two Potentials More...
 
Potential< GUM_SCALAR > operator/ (const GUM_SCALAR &v) const
 the function to be used to divide a Potential by a scalar More...
 
Potential< GUM_SCALAR > & operator+= (const Potential< GUM_SCALAR > &r)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator+= (const GUM_SCALAR &v)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator*= (const Potential< GUM_SCALAR > &r)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator*= (const GUM_SCALAR &v)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator-= (const Potential< GUM_SCALAR > &r)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator-= (const GUM_SCALAR &v)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator/= (const Potential< GUM_SCALAR > &r)
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator/= (const GUM_SCALAR &v)
 the function to be used to add two Potentials More...
 
bool operator== (const Potential< GUM_SCALAR > &r) const
 the function to be used to add two Potentials More...
 
bool operator!= (const Potential< GUM_SCALAR > &r) const
 the function to be used to add two Potentials More...
 
Potential< GUM_SCALAR > & operator<< (const DiscreteVariable &v)
 the function to be used to add two Potentials More...
 
virtual std::string toString () const
 the function to be used to add two Potentials More...
 
MultiDimInterface implementation
virtual Idx nbrDim () const final
 Returns the number of vars in the multidimensional container. More...
 
virtual Size domainSize () const final
 Returns the product of the variables domain size. More...
 
virtual void add (const DiscreteVariable &v) final
 Adds a new var to the variables of the multidimensional matrix. More...
 
virtual void erase (const DiscreteVariable &var) final
 Removes a var from the variables of the multidimensional matrix. More...
 
virtual void erase (const std::string &name) final
 
virtual const Sequence< const DiscreteVariable *> & variablesSequence () const final
 Returns a const ref to the sequence of DiscreteVariable*. More...
 
virtual const DiscreteVariablevariable (Idx) const final
 Returns a const ref to the ith var. More...
 
virtual const DiscreteVariablevariable (const std::string &name) const final
 Returns the variable with the name. More...
 
virtual Idx pos (const DiscreteVariable &var) const final
 Returns the index of a variable. More...
 
virtual bool contains (const DiscreteVariable &var) const final
 Returns true if var is in *this. More...
 
virtual bool empty () const final
 Returns true if no var is in *this. More...
 
MultiDimAddressable implementation
virtual bool unregisterSlave (Instantiation &i) final
 Unregister i as a slave of this MultiDimAdressable. More...
 
virtual bool registerSlave (Instantiation &i) final
 Register i as a slave of this MultiDimAdressable. More...
 
virtual void changeNotification (const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
 Listen to changes in a given Instantiation. More...
 
virtual void setChangeNotification (const Instantiation &i) final
 Listen to an assignment of a value in a Instantiation. More...
 
virtual void setFirstNotification (const Instantiation &i) final
 Listen to setFirst in a given Instantiation. More...
 
virtual void setLastNotification (const Instantiation &i) final
 Listen to setLast in a given Instantiation. More...
 
virtual void setIncNotification (const Instantiation &i) final
 Listen to increment in a given Instantiation. More...
 
virtual void setDecNotification (const Instantiation &i) final
 Listen to increment in each recorded Instantiation. More...
 
virtual void notifyChange () const final
 
MultiDimContainer implementation
virtual void set (const Instantiation &i, const GUM_SCALAR &value) const final
 Default implementation of MultiDimContainer::set(). More...
 
virtual GUM_SCALAR get (const Instantiation &i) const final
 Default implementation of MultiDimContainer::get(). More...
 
virtual void fill (const GUM_SCALAR &d) const final
 Default implementation of MultiDimContainer::set(). More...
 
virtual void populate (const std::vector< GUM_SCALAR > &v) const final
 Automatically fills this MultiDimContainer with the values in v. More...
 
virtual void apply (std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
 Apply a function on every element of the container. More...
 
virtual GUM_SCALAR reduce (std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
 compute lfold for this container More...
 
virtual void beginMultipleChanges () final
 Default implementation of MultiDimContainer::set(). More...
 
virtual void endMultipleChanges () final
 Default implementation of MultiDimContainer::set(). More...
 
virtual void endMultipleChanges (const GUM_SCALAR &) final
 Default implementation of MultiDimContainer::set(). More...
 
virtual std::string toString (const Instantiation *i) const
 Default implementation of MultiDimContainer::set(). More...
 
Accessors / Modifiers
virtual void populate (std::initializer_list< GUM_SCALAR > l) const
 Automatically fills this MultiDimContainer with the values in l. More...
 
GUM_SCALAR operator[] (const Instantiation &i) const
 An [] operator using a Instantiation as argument. More...
 
Various methods.
virtual const MultiDimImplementation< GUM_SCALAR > * content () const final
 Returns the implementation for this object (may be *this). More...
 
virtual MultiDimImplementation< GUM_SCALAR > * content () final
 Returns the implementation for this object (may be *this). More...
 
Copy methods.
virtual void copyFrom (const MultiDimContainer< GUM_SCALAR > &src) const
 Basic copy of a MultiDimContainer. More...
 
virtual void copyFrom (const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i) const
 Basic copy of a MultiDimContainer. More...
 
virtual void extractFrom (const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
 Basic extraction of a MultiDimContainer. More...
 
virtual MultiDimAdressablegetMasterRef ()
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer. More...
 
virtual const MultiDimAdressablegetMasterRef () const
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer. More...
 
virtual void copy (const MultiDimContainer< GUM_SCALAR > &src)
 Removes all variables in this MultiDimContainer and copy the content of src, variables included. More...
 
Various methods.
bool operator== (const MultiDimContainer< GUM_SCALAR > &p) const
 Test if this MultiDimContainer is equal to p. More...
 
bool operator!= (const MultiDimContainer< GUM_SCALAR > &p) const
 Test if this MultiDimContainer is different of p. More...
 
Accessors / Modifiers
void replace (const DiscreteVariable &x, const DiscreteVariable &y)
 Replace variables in this multidim. More...
 

Protected Attributes

MultiDimImplementation< GUM_SCALAR > * content_
 The true container. More...
 
GUM_SCALAR empty_value_
 value of the MultiDimDecorator if no dimension. More...
 

Protected Member Functions

Set< const DiscreteVariable *> complementVars_ (const Set< const DiscreteVariable * > &del_vars) const
 
virtual void replace_ (const DiscreteVariable *x, const DiscreteVariable *y)
 This is called by MultiDimContainer::replace() to proceed with the replacing between x and y. More...
 
void swapContent_ (MultiDimImplementation< GUM_SCALAR > *aContent) const
 protecte method to swap the implementation behind the Potential More...
 
GUM_SCALAR & get_ (const Instantiation &i) const final
 Return a data, given a Insantiation - final method. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::Potential< GUM_SCALAR >

aGrUM's Potential is a multi-dimensional array with tensor operators.

It is used to represent probabilities and utilities in aGrUMs' multidimensional (graphical) models.

Using the decorator pattern, this representation is independent from the implementation of the multidimensional matrix.

Template Parameters
GUM_SCALARThe type of the scalar stored in this multidimensional matrix.

Definition at line 59 of file potential.h.

Constructor & Destructor Documentation

◆ Potential() [1/5]

template<typename GUM_SCALAR >
INLINE gum::Potential< GUM_SCALAR >::Potential ( )

Default constructor.

Creates an empty null dimensional matrix with a MultiDimArray as its implementation.

Definition at line 37 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

37  :
38  MultiDimDecorator< GUM_SCALAR >(new MultiDimArray< GUM_SCALAR >(),
39  GUM_SCALAR(1)) {
40  GUM_CONSTRUCTOR(Potential);
41  }
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ Potential() [2/5]

template<typename GUM_SCALAR >
INLINE gum::Potential< GUM_SCALAR >::Potential ( MultiDimImplementation< GUM_SCALAR > *  aContent)
explicit

Creates an potential around aContent.

Parameters
aContentThe implementation of this Potential.

Definition at line 45 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

46  :
47  MultiDimDecorator< GUM_SCALAR >(aContent, GUM_SCALAR(1)) {
48  // for debugging purposes
49  GUM_CONSTRUCTOR(Potential);
50  }
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ Potential() [3/5]

template<typename GUM_SCALAR >
gum::Potential< GUM_SCALAR >::Potential ( MultiDimImplementation< GUM_SCALAR > *  aContent,
const MultiDimContainer< GUM_SCALAR > &  src 
)

Copy constructor.

The newly created Potential share the variables and the values from src, but no instantiation is associated to it. It allows to force the chosen implementation and to copy the data from src.

Parameters
aContentThe implementation to use in this Potential.
srcThe MultiDimContainer to copy.

Definition at line 72 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

74  :
75  MultiDimDecorator< GUM_SCALAR >(aContent) {
76  // for debugging purposes
77  GUM_CONSTRUCTOR(Potential);
78 
79  if (!src.empty()) {
80  this->beginMultipleChanges();
81 
82  for (Idx i = 0; i < src.variablesSequence().size(); i++) {
83  this->add(*(src.variablesSequence()[i]));
84  }
85 
86  this->endMultipleChanges();
87  this->content()->copyFrom(*src.content());
88  }
89  }
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ Potential() [4/5]

template<typename GUM_SCALAR >
INLINE gum::Potential< GUM_SCALAR >::Potential ( const Potential< GUM_SCALAR > &  src)

Copy constructor & assignment.

Definition at line 53 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

53  :
54  Potential< GUM_SCALAR >(static_cast< MultiDimImplementation< GUM_SCALAR >* >(
55  src.content()->newFactory()),
56  *(src.content())) {
57  this->empty_value_ = src.empty_value_;
58  // GUM_CONS_CPY not here because in called Potential
59  // GUM_CONS_CPY( Potential );
60  }
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ Potential() [5/5]

template<typename GUM_SCALAR >
INLINE gum::Potential< GUM_SCALAR >::Potential ( Potential< GUM_SCALAR > &&  from)

move constructor & assignement

move constructor

Definition at line 64 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

64  :
65  MultiDimDecorator< GUM_SCALAR >(
66  std::forward< MultiDimDecorator< GUM_SCALAR > >(from)) {
67  GUM_CONS_MOV(Potential);
68  }
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ ~Potential()

template<typename GUM_SCALAR >
gum::Potential< GUM_SCALAR >::~Potential ( )

Destructor.

Definition at line 115 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

115  {
116  // for debugging purposes
117  GUM_DESTRUCTOR(Potential);
118  }
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

Member Function Documentation

◆ abs()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::abs ( ) const

Apply abs on every element of the container.

Definition at line 343 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

343  {
344  this->apply([](GUM_SCALAR x) {
345  if (x >= 0)
346  return x;
347  else
348  return -x;
349  });
350  return *this;
351  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
+ Here is the call graph for this function:

◆ add()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::add ( const DiscreteVariable v)
finalvirtualinherited

Adds a new var to the variables of the multidimensional matrix.

See also
operator<<(MultiDimInterface& c, const DiscreteVariable& v)
Warning
Note that the variable passed in argument is not duplicated, that is, only a pointer toward the variable is kept by the MultiDimInterface.
Parameters
vThe new var.
Exceptions
DuplicateElementRaised if the variable already belongs to the sequence of variables.
OperationNotAllowedRaised if this object is non mutable.

Implements gum::MultiDimInterface.

Definition at line 181 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

181  {
182  if (v.domainSize() < 1) {
183  GUM_ERROR(InvalidArgument,
184  "Empty variable " << v << " cannot be added in a Potential");
185  }
186  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->add(v);
187  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ apply()

template<typename GUM_SCALAR >
void gum::MultiDimDecorator< GUM_SCALAR >::apply ( std::function< GUM_SCALAR(GUM_SCALAR) >  f) const
finalvirtualinherited

Apply a function on every element of the container.

Parameters
fthe function to apply

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 350 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

351  {
352  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
354  } else {
355  content_->apply(f);
356  }
357  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ argmax()

template<typename GUM_SCALAR >
INLINE Set< Instantiation > gum::Potential< GUM_SCALAR >::argmax ( ) const

set of instantiation corresponding to the max in the Potential

Definition at line 650 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

650  {
651  return findAll(max());
652  }
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
GUM_SCALAR max() const
max of all elements in the Potential
+ Here is the call graph for this function:

◆ argmin()

template<typename GUM_SCALAR >
INLINE Set< Instantiation > gum::Potential< GUM_SCALAR >::argmin ( ) const

set of instantiation corresponding to the min in the Potential

Definition at line 655 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

655  {
656  return findAll(min());
657  }
GUM_SCALAR min() const
min of all elements in the Potential
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
+ Here is the call graph for this function:

◆ beginMultipleChanges()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::beginMultipleChanges ( )
finalvirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 385 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

385  {
386  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
388  }
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ changeNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::changeNotification ( const Instantiation i,
const DiscreteVariable *const  var,
Idx  oldval,
Idx  newval 
)
finalvirtualinherited

Listen to changes in a given Instantiation.

Parameters
iThe Instantiation to listen.
varThe changed dim.
oldvalThe old value.
newvalThe changed value.

Implements gum::MultiDimAdressable.

Definition at line 192 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

196  {
197  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
198  ->changeNotification(i, var, oldval, newval);
199  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.
+ Here is the call graph for this function:

◆ complementVars_()

template<typename GUM_SCALAR >
Set< const DiscreteVariable *> gum::Potential< GUM_SCALAR >::complementVars_ ( const Set< const DiscreteVariable * > &  del_vars) const
protected

Definition at line 515 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

516  {
517  Set< const DiscreteVariable* > cplt;
518 
519  for (const auto x: this->variablesSequence())
520  if (!vars.contains(x)) cplt.insert(x);
521 
522  return cplt;
523  }
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
+ Here is the call graph for this function:

◆ contains()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimDecorator< GUM_SCALAR >::contains ( const DiscreteVariable v) const
finalvirtualinherited

Returns true if var is in *this.

Parameters
vA DiscreteVariable.
Returns
Returns true if var is in *this.

Implements gum::MultiDimInterface.

Definition at line 285 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

285  {
286  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->contains(
287  var);
288  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
+ Here is the call graph for this function:

◆ content() [1/2]

template<typename GUM_SCALAR >
INLINE const MultiDimImplementation< GUM_SCALAR > * gum::MultiDimDecorator< GUM_SCALAR >::content ( ) const
finalvirtualinherited

Returns the implementation for this object (may be *this).

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 380 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

380  {
381  return content_;
382  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ content() [2/2]

template<typename GUM_SCALAR >
INLINE MultiDimImplementation< GUM_SCALAR > * gum::MultiDimDecorator< GUM_SCALAR >::content ( )
finalvirtualinherited

Returns the implementation for this object (may be *this).

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 373 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

373  {
374  return content_;
375  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ copy()

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copy ( const MultiDimContainer< GUM_SCALAR > &  src)
virtualinherited

Removes all variables in this MultiDimContainer and copy the content of src, variables included.

Parameters
srcThe MultiDimContainer to copy.

Reimplemented in gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 290 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

291  {
292  this->beginMultipleChanges();
293 
294  Size nbr = this->nbrDim();
295 
296  for (Idx i = 0; i < nbr; i++) {
297  this->erase(this->variable(0));
298  }
299 
300  for (Idx i = 0; i < src.nbrDim(); i++) {
301  this->add(src.variable(i));
302  }
303 
304  this->endMultipleChanges();
305  this->copyFrom(src);
306  }
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ copyFrom() [1/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copyFrom ( const MultiDimContainer< GUM_SCALAR > &  src) const
virtualinherited

Basic copy of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which values are copied. This is a full copy with no verification of dimensions.
Exceptions
OperationNotAllowedRaised if src does not have the same domain size than this MultiDimContainer.

Reimplemented in gum::aggregator::MultiDimAggregator< GUM_SCALAR >, gum::MultiDimICIModel< GUM_SCALAR >, and gum::MultiDimArray< GUM_SCALAR >.

Definition at line 271 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

272  {
273  if (src.domainSize() != domainSize()) {
274  GUM_ERROR(OperationNotAllowed,
275  "Domain sizes do not fit : " << src.domainSize()
276  << "!=" << domainSize());
277  }
278 
279  Instantiation i_dest(*this);
280  Instantiation i_src(src);
281 
282  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
283  set(i_dest, src[i_src]);
284  }
285  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ copyFrom() [2/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copyFrom ( const MultiDimContainer< GUM_SCALAR > &  src,
Instantiation p_i 
) const
virtualinherited

Basic copy of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which values are copied.
p_iGive the order to iterate in this MultiDimContainer during the copy (nullptr will correctly copy if this is a reorganization of src).
Exceptions
OperationNotAllowedRaised if src does not have the same domain size than this MultiDimContainer.

Reimplemented in gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 221 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

223  {
224  if (src.domainSize() != domainSize()) {
225  GUM_ERROR(OperationNotAllowed,
226  "Domain sizes do not fit : " << src.domainSize()
227  << "!=" << domainSize());
228  }
229 
230  if (p_i == nullptr) { // if null, we just follow the same order
231  Instantiation i(src);
232  for (i.setFirst(); !i.end(); ++i) {
233  set(i, src[i]);
234  }
235  } else {
236  Instantiation i_dest(*this);
237  Instantiation i_src(src);
238  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end();
239  i_dest.incIn(*p_i), ++i_src) {
240  set(i_dest, src[i_src]);
241  }
242  }
243  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ domainSize()

template<typename GUM_SCALAR >
INLINE Size gum::MultiDimDecorator< GUM_SCALAR >::domainSize ( ) const
finalvirtualinherited

Returns the product of the variables domain size.

Returns
Returns the product of the variables domain size.

Implements gum::MultiDimInterface.

Definition at line 174 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

174  {
175  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->domainSize();
176  }
virtual Size domainSize() const final
Returns the product of the variables domain size.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ draw()

template<typename GUM_SCALAR >
Idx gum::Potential< GUM_SCALAR >::draw ( ) const

get a value at random from a 1-D distribution

Definition at line 613 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

613  {
614  if (this->nbrDim() != 1) {
615  GUM_ERROR(FatalError, "To draw from a potential, the dimension must be 1")
616  }
617 
618  GUM_SCALAR r = static_cast< GUM_SCALAR >(randomProba());
619  Instantiation Ip(*this);
620  for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
621  r -= this->get(Ip);
622  if (r <= 0) return Ip.val(0);
623  }
624  return this->variable(0).domainSize() - 1;
625  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ empty()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimDecorator< GUM_SCALAR >::empty ( ) const
finalvirtualinherited

Returns true if no var is in *this.

Returns
Returns true if no var is in *this.

Implements gum::MultiDimInterface.

Definition at line 291 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

291  {
292  if (content_ == nullptr) return true;
293  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty();
294  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
+ Here is the call graph for this function:

◆ endMultipleChanges() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::endMultipleChanges ( )
finalvirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 391 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

391  {
392  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
393  ->endMultipleChanges();
394  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
+ Here is the call graph for this function:

◆ endMultipleChanges() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::endMultipleChanges ( const GUM_SCALAR &  x)
finalvirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 398 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

398  {
399  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->endMultipleChanges(
400  x);
401  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
+ Here is the call graph for this function:

◆ entropy()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::entropy ( ) const

entropy of the Potential

Definition at line 203 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

203  {
204  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
205  return static_cast< GUM_SCALAR >(0);
206  }
207 
208  return this->reduce(
209  [](GUM_SCALAR z, GUM_SCALAR p) {
210  return (p == 0.0) ? z : (z - p * std::log2(p));
211  },
212  0.0);
213  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:612
+ Here is the call graph for this function:

◆ erase() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::erase ( const DiscreteVariable v)
finalvirtualinherited

Removes a var from the variables of the multidimensional matrix.

See also
operator>>(MultiDimInterface& c, const DiscreteVariable& v)
Exceptions
OperationNotAllowedRaised if this object is non mutable.
NotFoundRaised if v does not belong to this.

Implements gum::MultiDimInterface.

Definition at line 255 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

255  {
256  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->erase(var);
257  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
+ Here is the call graph for this function:

◆ erase() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::erase ( const std::string &  name)
finalvirtualinherited

Definition at line 260 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

260  {
261  erase(variable(name));
262  }
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
+ Here is the call graph for this function:

◆ extract()

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::extract ( const Instantiation inst) const

create a new Potential extracted from *this given a partial instantiation

Definition at line 605 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

605  {
606  Potential< GUM_SCALAR > p;
607  p.extractFrom(*this, inst);
608 
609  return p;
610  }
+ Here is the call graph for this function:

◆ extractFrom()

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::extractFrom ( const MultiDimContainer< GUM_SCALAR > &  src,
const Instantiation mask 
)
virtualinherited

Basic extraction of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which datas are copied.
maskpartial instantiation of variables of the Potential : the

extraction will concern every variable not in the instantiation and the copy of data will use the (relevant) values in this instantiation.

Definition at line 246 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

248  {
249  this->beginMultipleChanges();
250 
251  Size nbr = this->nbrDim();
252  for (Idx i = 0; i < nbr; i++) {
253  this->erase(this->variable(0));
254  }
255 
256  for (Idx i = 0; i < src.nbrDim(); i++) {
257  if (!imask.contains(src.variable(i))) this->add(src.variable(i));
258  }
259 
260  this->endMultipleChanges();
261 
262  if (this->nbrDim() == 0) { GUM_ERROR(FatalError, "Empty potential"); }
263 
264  Instantiation inst(src);
265  inst.setVals(imask);
266  for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
267  set(inst, src[inst]);
268  }
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ fill()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::fill ( const GUM_SCALAR &  d) const
finalvirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 303 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

303  {
304  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
305  empty_value_ = d;
306  } else {
307  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->fill(d);
308  }
309  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
+ Here is the call graph for this function:

◆ fillWith() [1/4]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::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 same orders)

Warning
a strict control on names of variables and labels are made
Exceptions
InvalidArgumentif the Potential is not compatible with this

Definition at line 231 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

231  {
232  if (src.domainSize() != this->domainSize()) {
233  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
234  }
235  gum::Set< std::string > son; // set of names
236  for (const auto& v: src.variablesSequence()) {
237  son.insert(v->name());
238  }
239  for (const auto& v: this->variablesSequence()) {
240  if (!son.contains(v->name())) {
241  GUM_ERROR(InvalidArgument,
242  "Variable <" << v->name() << "> not present in src (" << son
243  << ").");
244  }
245  // we check size, labels and order of labels in the same time
246  if (v->toString() != src.variable(v->name()).toString()) {
247  GUM_ERROR(InvalidArgument,
248  "Variables <" << v->name() << "> are not identical.");
249  }
250  }
251 
252  Instantiation Isrc(src);
253  Instantiation Idst(*this);
254  for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
255  for (Idx i = 0; i < this->nbrDim(); i++) {
256  Idst.chgVal(Isrc.variable(i).name(), Isrc.val(i));
257  }
258  this->set(Idst, src.get(Isrc));
259  }
260 
261  return *this;
262  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const final
Returns the product of the variables domain size.
virtual std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:517
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ fillWith() [2/4]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::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.

For instance, to copy the potential P(A,B,C) in Q(D,E,A) with the mapping P.A<->Q.E, P.B<->Q.A, P.C<->Q.D (assuming that the corresponding variables have the same domain size and the order of labels):

Q.fillWith(P,{"C","A","B"});
Warning
a strict control on names of variables and labels are made
Exceptions
InvalidArgumentif the Potential is not compatible with this

Definition at line 265 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

267  {
268  if (src.nbrDim() != this->nbrDim()) {
269  GUM_ERROR(InvalidArgument, "Potential to copy has not the same dimension.");
270  }
271  if (src.nbrDim() != mapSrc.size()) {
272  GUM_ERROR(InvalidArgument,
273  "Potential and vector have not the same dimension.");
274  }
275  Instantiation Isrc;
276  for (Idx i = 0; i < src.nbrDim(); i++) {
277  if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
278  GUM_ERROR(InvalidArgument,
279  "Variables " << mapSrc[i] << " (in the argument) and "
280  << this->variable(i).name()
281  << " have not the same dimension.");
282  } else {
283  Isrc.add(src.variable(mapSrc[i]));
284  }
285  }
286  Instantiation Idst(*this);
287  for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
288  this->set(Idst, src.get(Isrc));
289  }
290 
291  return *this;
292  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ fillWith() [3/4]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::fillWith ( const std::vector< GUM_SCALAR > &  data) const

Automatically fills the potential with the values in v.

Parameters
vVector of values.
Exceptions
SizeErrorRaised if v size's does not matches this MultiDimContainer domain size.

Definition at line 216 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

217  {
218  this->populate(data);
219  return *this;
220  }
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
+ Here is the call graph for this function:

◆ fillWith() [4/4]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::fillWith ( const GUM_SCALAR &  val) const

Automatically fills this MultiDimContainer with the value v.

Parameters
vcontains the data.

Definition at line 224 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

224  {
225  this->fill(val);
226  return *this;
227  }
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
+ Here is the call graph for this function:

◆ findAll()

template<typename GUM_SCALAR >
Set< Instantiation > gum::Potential< GUM_SCALAR >::findAll ( GUM_SCALAR  v) const

set of instantiation corresponding to the parameter v in the Potential

Definition at line 636 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

636  {
637  Instantiation I(*this);
638  Set< Instantiation > res;
639 
640  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
641  return res;
642  }
643  for (I.setFirst(); !I.end(); ++I) {
644  if (this->get(I) == v) res.insert(I);
645  }
646  return res;
647  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
+ Here is the call graph for this function:

◆ get()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MultiDimDecorator< GUM_SCALAR >::get ( const Instantiation i) const
finalvirtualinherited

Default implementation of MultiDimContainer::get().

Calls get_ as a l-value.

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 153 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

153  {
154  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
155  return empty_value_;
156  } else {
157  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->get(i);
158  }
159  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ get_()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR & gum::MultiDimDecorator< GUM_SCALAR >::get_ ( const Instantiation i) const
finalprotectedvirtualinherited

Return a data, given a Insantiation - final method.

Parameters
iThe instantiation.
Exceptions
NullElement
NotFound

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 147 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

147  {
148  GUM_ERROR(OperationNotAllowed, "_get in the implementation !");
149  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ getMasterRef() [1/2]

template<typename GUM_SCALAR >
INLINE MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( )
virtualinherited

In order to insure the dereference for decorators, we need to virtualize the access to master pointer.

Returns
Returns the ref to content as MultiDimAdressable&

Implements gum::MultiDimAdressable.

Reimplemented in gum::MultiDimBucket< GUM_SCALAR >.

Definition at line 309 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

309  {
310  return static_cast< MultiDimAdressable& >(*content());
311  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
MultiDimAdressable()
Default constructor.
+ Here is the call graph for this function:

◆ getMasterRef() [2/2]

template<typename GUM_SCALAR >
INLINE const MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( ) const
virtualinherited

In order to insure the dereference for decorators, we need to virtualize the access to master pointer.

Returns
Returns the master of this MultiDimAdressable.

Implements gum::MultiDimAdressable.

Reimplemented in gum::MultiDimBucket< GUM_SCALAR >.

Definition at line 315 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

315  {
316  return static_cast< const MultiDimAdressable& >(*content());
317  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
MultiDimAdressable()
Default constructor.
+ Here is the call graph for this function:

◆ inverse()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::inverse ( void  ) const

the function to inverse (each value of) *this

Definition at line 421 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

421  {
422  this->apply([](GUM_SCALAR x) { return 1 / x; });
423  return *this;
424  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
+ Here is the call graph for this function:

◆ isNonZeroMap()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::isNonZeroMap ( ) const

create a boolean-like potential using the predicate isNonZero

Definition at line 503 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

503  {
504  auto p = Potential< GUM_SCALAR >(*this);
505  p.apply([](GUM_SCALAR x) {
506  if (x != static_cast< GUM_SCALAR >(0))
507  return static_cast< GUM_SCALAR >(1);
508  else
509  return static_cast< GUM_SCALAR >(0);
510  });
511  return p;
512  }
+ Here is the call graph for this function:

◆ KL()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::KL ( const Potential< GUM_SCALAR > &  p) const

compute KL divergence between this and p Checks the compatibility and then compute KL divergence

Exceptions
gum::InvalidArgumentif p is not compatible with $this (dimension, variables)
gum::FatalErrorif a zero is found in p or this and not in the other.

Definition at line 308 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

308  {
309  if (this->nbrDim() != p.nbrDim())
310  GUM_ERROR(
311  InvalidArgument,
312  "BNdistance between potentials with different numbers of dimensions");
313  for (const auto var: p.variablesSequence()) {
314  if (!this->contains(*var))
315  GUM_ERROR(InvalidArgument,
316  "A variable in the argument does not belong to the potential.");
317  }
318  for (const auto var: this->variablesSequence()) {
319  if (!p.contains(*var))
320  GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.");
321  }
322 
323  Instantiation inst(*this);
324  GUM_SCALAR res = static_cast< GUM_SCALAR >(0);
325  for (inst.setFirst(); !inst.end(); inst.inc()) {
326  GUM_SCALAR x = this->get(inst);
327  GUM_SCALAR y = p.get(inst);
328  if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
329  continue;
330 
331  if (static_cast< GUM_SCALAR >(0) == y)
332  // we know that x!=0;
333  GUM_ERROR(FatalError,
334  "The argument has a 0 at " << inst
335  << " while the potential has not.")
336 
337  res += x * std::log2(x / y);
338  }
339  return res;
340  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
STL namespace.
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ log2()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::log2 ( ) const

apply $log_2(x)$ on every element of the container

Definition at line 301 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

301  {
302  this->apply([](GUM_SCALAR x) { return std::log2(x); });
303  return *this;
304  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:612
+ Here is the call graph for this function:

◆ margMaxIn()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margMaxIn ( const Set< const DiscreteVariable * > &  kept_vars) const

Projection using max as operation (and implementation-optimized operations)

Parameters
kept_varsis the set of vars to keep

Definition at line 493 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

494  {
495  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
496  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
497  }
498  return Potential< GUM_SCALAR >(
499  gum::projectMax(*this->content(), complementVars_(kept_vars)));
500  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
+ Here is the call graph for this function:

◆ margMaxOut()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margMaxOut ( const Set< const DiscreteVariable * > &  del_vars) const

Projection using max as operation (and implementation-optimized operations)

Parameters
del_varsis the set of vars to eliminate

Definition at line 455 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

456  {
457  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
458  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
459  }
460  return Potential< GUM_SCALAR >(gum::projectMax(*this->content(), del_vars));
461  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
+ Here is the call graph for this function:

◆ margMinIn()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margMinIn ( const Set< const DiscreteVariable * > &  kept_vars) const

Projection using min as operation (and implementation-optimized operations)

Parameters
kept_varsis the set of vars to keep

Definition at line 483 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

484  {
485  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
486  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
487  }
488  return Potential< GUM_SCALAR >(
489  gum::projectMin(*this->content(), complementVars_(kept_vars)));
490  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
+ Here is the call graph for this function:

◆ margMinOut()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margMinOut ( const Set< const DiscreteVariable * > &  del_vars) const

Projection using min as operation (and implementation-optimized operations)

Parameters
del_varsis the set of vars to eliminate

Definition at line 446 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

447  {
448  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
449  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
450  }
451  return Potential< GUM_SCALAR >(gum::projectMin(*this->content(), del_vars));
452  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
+ Here is the call graph for this function:

◆ margProdIn()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margProdIn ( const Set< const DiscreteVariable * > &  kept_vars) const

Projection using multiplication as operation (and implementation-optimized operations)

Parameters
kept_varsis the set of vars to keep

Definition at line 473 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

474  {
475  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
476  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
477  }
478  return Potential< GUM_SCALAR >(
479  gum::projectProduct(*this->content(), complementVars_(kept_vars)));
480  }
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ margProdOut()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margProdOut ( const Set< const DiscreteVariable * > &  del_vars) const

Projection using multiplication as operation (and implementation-optimized operations)

Parameters
del_varsis the set of vars to eliminate

Definition at line 436 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

437  {
438  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
439  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
440  }
441  return Potential< GUM_SCALAR >(
442  gum::projectProduct(*this->content(), del_vars));
443  }
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ margSumIn()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margSumIn ( const Set< const DiscreteVariable * > &  kept_vars) const

Projection using sum as operation (and implementation-optimized operations)

Parameters
kept_varsis the set of vars to keep

Definition at line 463 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

464  {
465  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
466  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
467  }
468  return Potential< GUM_SCALAR >(
469  gum::projectSum(*this->content(), complementVars_(kept_vars)));
470  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ margSumOut()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::margSumOut ( const Set< const DiscreteVariable * > &  del_vars) const

Projection using sum as operation (and implementation-optimized operations)

Parameters
del_varsis the set of vars to eliminate

Definition at line 427 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

428  {
429  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
430  return Potential< GUM_SCALAR >().fillWith(this->empty_value_);
431  }
432  return Potential< GUM_SCALAR >(gum::projectSum(*this->content(), del_vars));
433  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
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...
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ max()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::max ( ) const

max of all elements in the Potential

Definition at line 145 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

145  {
146  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
147  return this->empty_value_;
148  }
149  return gum::projectMax(*this->content());
150  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
+ Here is the call graph for this function:

◆ maxNonOne()

template<typename GUM_SCALAR >
GUM_SCALAR gum::Potential< GUM_SCALAR >::maxNonOne ( ) const

max of all non one elements in the Potential

Warning
can return 1 if no other value than 1 ...

Definition at line 163 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

163  {
164  GUM_SCALAR res;
165 
166  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
167  res = this->empty_value_;
168  } else {
169  res = this->reduce(
170  [](GUM_SCALAR z, GUM_SCALAR p) {
171  return (p == static_cast< GUM_SCALAR >(1)) ? z
172  : (z == static_cast< GUM_SCALAR >(1)) ? p
173  : (p > z ? p : z);
174  },
175  static_cast< GUM_SCALAR >(1));
176  }
177 
178  return res;
179  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ min()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::min ( ) const

min of all elements in the Potential

Definition at line 153 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

153  {
154  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
155  return this->empty_value_;
156  }
157  return gum::projectMin(*this->content());
158  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
+ Here is the call graph for this function:

◆ minNonZero()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::minNonZero ( ) const

min of all non zero elements in the Potential

Warning
can return 0 if no other value than 0 ...

Definition at line 184 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

184  {
185  GUM_SCALAR res;
186 
187  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
188  res = this->empty_value_;
189  } else {
190  res = this->reduce(
191  [](GUM_SCALAR z, GUM_SCALAR p) {
192  return (p == static_cast< GUM_SCALAR >(0)) ? z
193  : (z == static_cast< GUM_SCALAR >(0)) ? p
194  : (p < z ? p : z);
195  },
196  static_cast< GUM_SCALAR >(0));
197  }
198  return res;
199  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ nbrDim()

template<typename GUM_SCALAR >
INLINE Idx gum::MultiDimDecorator< GUM_SCALAR >::nbrDim ( ) const
finalvirtualinherited

Returns the number of vars in the multidimensional container.

Returns
Returns the number of vars in the multidimensional container.

Implements gum::MultiDimInterface.

Definition at line 331 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

331  {
332  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim();
333  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ new_abs()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::new_abs ( ) const

Create a new potential and apply abs on every element of the container.

Definition at line 703 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

703  {
704  return Potential< GUM_SCALAR >(*this).abs();
705  }
+ Here is the call graph for this function:

◆ new_log2()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::new_log2 ( ) const

Create a new potential and apply $log_2(x)$ on every element of the container.

Definition at line 713 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

713  {
714  return Potential< GUM_SCALAR >(*this).log2();
715  }
+ Here is the call graph for this function:

◆ new_sq()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::new_sq ( ) const

Create a new potential and apply $x^2$ on every element of the container.

Definition at line 708 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

708  {
709  return Potential< GUM_SCALAR >(*this).sq();
710  }
+ Here is the call graph for this function:

◆ newFactory()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > * gum::Potential< GUM_SCALAR >::newFactory ( ) const
virtual

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimDecorator< GUM_SCALAR >.

Definition at line 121 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

121  {
122  return new Potential< GUM_SCALAR >(
123  static_cast< MultiDimImplementation< GUM_SCALAR >* >(
124  this->content()->newFactory()));
125  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
+ Here is the call graph for this function:

◆ noising()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::noising ( GUM_SCALAR  alpha) const

add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()

Warning
alpha in [0,1]

Definition at line 693 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

693  {
694  if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
695  GUM_ERROR(InvalidArgument, "alpha must be in [0,1]");
696  }
697  Potential< GUM_SCALAR > noise(*this);
698  return fillWith(scale(1 - alpha) + noise.randomCPT().scale(alpha))
699  .normalizeAsCPT();
700  }
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
const Potential< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
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...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ normalize()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::normalize ( ) const

normalisation of this do nothing if sum is 0

Definition at line 357 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

357  {
358  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
359  if (this->empty_value_ != static_cast< GUM_SCALAR >(0))
360  this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
361  } else {
362  GUM_SCALAR s = sum();
363 
364  if (s != (GUM_SCALAR)0) {
365  this->apply([s](GUM_SCALAR x) { return x / s; });
366  }
367  }
368  return *this;
369  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
GUM_SCALAR sum() const
sum of all elements in the Potential
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ normalizeAsCPT()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::normalizeAsCPT ( const Idx varId = 0) const

normalisation of this as a CPT for the variable varId

If the Potential is empty, the argument is not used.

Exceptions
FatalErrorit some distribution sums to 0, or if varId>=nbrDim()

Definition at line 373 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

373  {
374  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
375  if (this->empty_value_ != static_cast< GUM_SCALAR >(0)) {
376  this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
377  } else {
378  GUM_ERROR(FatalError,
379  "Normalization for a potential that sum to 0 in " << *this);
380  }
381  } else {
382  if (varId >= this->nbrDim()) {
383  GUM_ERROR(FatalError, varId << " is not a position for " << *this);
384  }
385  Instantiation inst(*this);
386  const auto& v = this->variable(varId);
387 
388  for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
389  GUM_SCALAR s = (GUM_SCALAR)0.0;
390  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
391  s += this->get(inst);
392  if (s == (GUM_SCALAR)0.0) {
393  GUM_ERROR(FatalError,
394  "Normalization for a potential that sum to 0 in " << *this);
395  }
396  if (s != (GUM_SCALAR)1.0) {
397  for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
398  this->set(inst, this->get(inst) / s);
399  }
400  inst.setFirstVar(v); // to remove inst.end()
401  }
402  }
403  return *this;
404  }
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 final
Returns true if no var is in *this.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ notifyChange()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::notifyChange ( ) const
finalvirtualinherited

Definition at line 314 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

314  {
315  /*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
316  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
317  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ operator!=() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimContainer< GUM_SCALAR >::operator!= ( const MultiDimContainer< GUM_SCALAR > &  p) const
inherited

Test if this MultiDimContainer is different of p.

Parameters
pThe MultiDimContainer to test for inequality.
Returns
Returns true if this MultiDimContainer is different of p.

Definition at line 160 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

161  {
162  return !operator==(p);
163  }
bool operator==(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is equal to p.
+ Here is the call graph for this function:

◆ operator!=() [2/2]

template<typename GUM_SCALAR>
bool gum::Potential< GUM_SCALAR >::operator!= ( const Potential< GUM_SCALAR > &  r) const
inline

the function to be used to add two Potentials

Definition at line 508 of file potential.h.

References gum::Potential< GUM_SCALAR >::operator==().

508  {
509  return !operator==(r);
510  }
bool operator==(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:494
+ Here is the call graph for this function:

◆ operator*() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator* ( const Potential< GUM_SCALAR > &  p2) const
inline

the function to be used to multiply two Potentials

Definition at line 429 of file potential.h.

429  {
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  }
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.

◆ operator*() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator* ( const GUM_SCALAR &  v) const
inline

the function to be used to multiply a Potential and a scalar

Definition at line 438 of file potential.h.

438  {
439  return Potential< GUM_SCALAR >(*this).scale(v);
440  }

◆ operator*=() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator*= ( const Potential< GUM_SCALAR > &  r)
inline

the function to be used to add two Potentials

Definition at line 467 of file potential.h.

467  {
468  *this = *this * r;
469  return *this;
470  }

◆ operator*=() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator*= ( const GUM_SCALAR &  v)
inline

the function to be used to add two Potentials

Definition at line 471 of file potential.h.

471  {
472  this->scale(v);
473  return *this;
474  }
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v

◆ operator+() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator+ ( const Potential< GUM_SCALAR > &  p2) const
inline

the function to be used to add two Potentials

Definition at line 397 of file potential.h.

397  {
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  }
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this

◆ operator+() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator+ ( const GUM_SCALAR &  v) const
inline

the function to be used to add a GUM_SCALAR to a Potential

Definition at line 407 of file potential.h.

407  {
408  return Potential< GUM_SCALAR >(*this).translate(v);
409  }

◆ operator+=() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator+= ( const Potential< GUM_SCALAR > &  r)
inline

the function to be used to add two Potentials

Definition at line 458 of file potential.h.

458  {
459  *this = *this + r;
460  return *this;
461  }

◆ operator+=() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator+= ( const GUM_SCALAR &  v)
inline

the function to be used to add two Potentials

Definition at line 462 of file potential.h.

462  {
463  this->translate(v);
464  return *this;
465  }
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this

◆ operator-() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator- ( const Potential< GUM_SCALAR > &  p2) const
inline

the function to be used to subtract two Potentials

Definition at line 412 of file potential.h.

412  {
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  }
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.

◆ operator-() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator- ( const GUM_SCALAR &  v) const
inline

the function to be used to substract a GUM_SCALAR from a Potential

Definition at line 424 of file potential.h.

424  {
425  return Potential< GUM_SCALAR >(*this).translate(-v);
426  }

◆ operator-=() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator-= ( const Potential< GUM_SCALAR > &  r)
inline

the function to be used to add two Potentials

Definition at line 476 of file potential.h.

476  {
477  *this = *this - r;
478  return *this;
479  }

◆ operator-=() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator-= ( const GUM_SCALAR &  v)
inline

the function to be used to add two Potentials

Definition at line 480 of file potential.h.

480  {
481  this->translate(-v);
482  return *this;
483  }
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this

◆ operator/() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator/ ( const Potential< GUM_SCALAR > &  p2) const
inline

the function to be used to divide two Potentials

Definition at line 443 of file potential.h.

443  {
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  }
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.

◆ operator/() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::operator/ ( const GUM_SCALAR &  v) const
inline

the function to be used to divide a Potential by a scalar

Definition at line 454 of file potential.h.

454  {
455  return Potential< GUM_SCALAR >(*this).scale(1 / v);
456  }

◆ operator/=() [1/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator/= ( const Potential< GUM_SCALAR > &  r)
inline

the function to be used to add two Potentials

Definition at line 485 of file potential.h.

485  {
486  *this = *this / r;
487  return *this;
488  }

◆ operator/=() [2/2]

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator/= ( const GUM_SCALAR &  v)
inline

the function to be used to add two Potentials

Definition at line 489 of file potential.h.

489  {
490  this->scale(1 / v);
491  return *this;
492  }
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v

◆ operator<<()

template<typename GUM_SCALAR>
Potential< GUM_SCALAR >& gum::Potential< GUM_SCALAR >::operator<< ( const DiscreteVariable v)
inline

the function to be used to add two Potentials

Definition at line 512 of file potential.h.

512  {
513  this->add(v);
514  return *this;
515  }
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.

◆ operator=() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::operator= ( const Potential< GUM_SCALAR > &  src)

Default constructor.

Creates an empty null dimensional matrix with a MultiDimArray as its implementation.

Definition at line 94 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

94  {
95  GUM_OP_CPY(Potential);
96  if (&src == this) return *this;
98  return *this;
99  }
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ operator=() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::operator= ( Potential< GUM_SCALAR > &&  src)

Default constructor.

Creates an empty null dimensional matrix with a MultiDimArray as its implementation.

Definition at line 104 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

104  {
105  GUM_OP_MOV(Potential);
106  if (&src == this) return *this;
108  std::forward< MultiDimDecorator< GUM_SCALAR > >(src));
109  return *this;
110  }
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Potential()
Default constructor.
Definition: potential_tpl.h:37
+ Here is the call graph for this function:

◆ operator==() [1/2]

template<typename GUM_SCALAR >
bool gum::MultiDimContainer< GUM_SCALAR >::operator== ( const MultiDimContainer< GUM_SCALAR > &  p) const
inherited

Test if this MultiDimContainer is equal to p.

Parameters
pThe MultiDimContainer to test for equality.
Returns
Returns true if this MultiDimContainer is equal to p.

Definition at line 129 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

130  {
131  if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
132  if (nbrDim() == 0) return true;
133 
135  var_iterator;
136 
137  for (var_iterator iter = variablesSequence().beginSafe();
138  iter != variablesSequence().endSafe();
139  ++iter) {
140  if (!p.variablesSequence().exists(*iter)) { return false; }
141  }
142  } else {
143  return false;
144  }
145 
146  Instantiation i(*this);
147 
148  AlmostDifferent< GUM_SCALAR > cmp;
149 
150  for (i.setFirst(); !i.end(); ++i) {
151  if (cmp(get(i), p.get(i))) { return false; }
152  }
153 
154  return true;
155  }
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const =0
Returns the product of the variables domain size.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition: sequence.h:1037
+ Here is the call graph for this function:

◆ operator==() [2/2]

template<typename GUM_SCALAR>
bool gum::Potential< GUM_SCALAR >::operator== ( const Potential< GUM_SCALAR > &  r) const
inline

the function to be used to add two Potentials

Definition at line 494 of file potential.h.

Referenced by gum::Potential< GUM_SCALAR >::operator!=().

494  {
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  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the caller graph for this function:

◆ operator[]()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MultiDimContainer< GUM_SCALAR >::operator[] ( const Instantiation i) const
inherited

An [] operator using a Instantiation as argument.

Warning
If i variables set is disjoint with this MultiDimContainer then 0 is assumed for dimensions (i.e. variables) not prensent in the instantiation.
Parameters
iAn Instantiation.
Returns
Returns the adressed (GUM_SCALAR) value.

Definition at line 83 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

83  {
84  return get(i);
85  }
+ Here is the call graph for this function:

◆ populate() [1/2]

template<typename GUM_SCALAR >
void gum::MultiDimDecorator< GUM_SCALAR >::populate ( const std::vector< GUM_SCALAR > &  v) const
finalvirtualinherited

Automatically fills this MultiDimContainer with the values in v.

The order used to fill this MultiDimContainer is the same as with an instantiation over it.

Size cpt = 0;
Instantiation i( *this );
for (i.setFirst(); !i.end(); ++i, ++cpt) {
set(i, v[cpt]);
}
Parameters
vVector of values.
Exceptions
SizeErrorRaised if v size's does not matches this MultiDimContainer domain size.

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 336 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

337  {
338  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
339  if (v.size() == 1) {
340  empty_value_ = v[0];
341  } else {
342  GUM_ERROR(SizeError, "Size do not match in populate")
343  }
344  } else {
345  content_->populate(v);
346  }
347  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ populate() [2/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::populate ( std::initializer_list< GUM_SCALAR >  l) const
virtualinherited

Automatically fills this MultiDimContainer with the values in l.

The order used to fill this MultiDimContainer is the same as with an instantiation over it.

Size cpt = 0;
Instantiation i( *this );
for (i.setFirst(); !i.end(); ++i, ++cpt) {
set(i, v[cpt]);
}
Parameters
lcontains the data.
Exceptions
SizeErrorRaised if l size's does not matches this MultiDimContainer domain size.

Reimplemented in gum::MultiDimBijArray< GUM_SCALAR >, gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 183 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

184  {
185  if (domainSize() != l.size()) {
186  GUM_ERROR(SizeError,
187  "Sizes do not match : " << domainSize() << "!=" << l.size());
188  }
189 
190  Instantiation i(*this);
191  // insert all the elements
192  for (const auto& elt: l) {
193  set(i, elt);
194  ++i;
195  }
196  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ pos()

template<typename GUM_SCALAR >
INLINE Idx gum::MultiDimDecorator< GUM_SCALAR >::pos ( const DiscreteVariable v) const
finalvirtualinherited

Returns the index of a variable.

Parameters
vThe variable for which the index is returned.
Returns
Returns the index of a variable.
Exceptions
NotFoundRaised if v is not in this multidimensional matrix.

Implements gum::MultiDimInterface.

Definition at line 279 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

279  {
280  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->pos(var);
281  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
+ Here is the call graph for this function:

◆ product()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::product ( ) const

product of all elements in the Potential

Definition at line 137 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

137  {
138  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
139  return this->empty_value_;
140  }
141  return gum::projectProduct(*this->content());
142  }
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ putFirst() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::putFirst ( const DiscreteVariable var) const

create a new Potential with a certain variable in first

Exceptions
InvalidArgumentif the var is not in the potential

Definition at line 570 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

570  {
571  if (!this->contains(*var)) {
572  GUM_ERROR(InvalidArgument,
573  "The variable to put first does not belong to the potential");
574  }
575  if (&(this->variable(0)) == var) return Potential< GUM_SCALAR >(*this);
576 
577  std::vector< const DiscreteVariable* > vars;
578  vars.push_back(var);
579  for (Idx i = 0; i < this->nbrDim(); i++)
580  if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
581 
582  return this->reorganize(vars);
583  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ putFirst() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::putFirst ( const std::string &  varname) const

create a new Potential with a certain variable in first

Exceptions
InvalidArgumentif the var is not in the potential

Definition at line 587 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

587  {
588  const DiscreteVariable* var = nullptr;
589 
590  for (gum::Idx i = 0; i < this->nbrDim(); i++)
591  if (this->variable(i).name() == varname) {
592  var = &(this->variable(i));
593  break;
594  }
595  if (var == nullptr)
596  GUM_ERROR(InvalidArgument,
597  "The variable '"
598  << varname
599  << "' to put first does not belong to the potential");
600  return this->putFirst(var);
601  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Size Idx
Type for indexes.
Definition: types.h:52
const std::string & name() const
returns the name of the variable
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:54
+ Here is the call graph for this function:

◆ random()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::random ( ) const

generate a random Potential with each parameter in [0,1]

Definition at line 660 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

660  {
661  if (this->domainSize() == 0) return *this;
662 
663  std::vector< GUM_SCALAR > v;
664  GUM_SCALAR sum;
665  v.reserve(this->domainSize());
666  sum = 0.0;
667  for (Size i = 0; i < this->domainSize(); ++i) {
668  auto r = (GUM_SCALAR)randomProba();
669  v.push_back(r);
670  sum += r;
671  }
672  if (sum == 0.0)
673  v[gum::randomValue(this->domainSize())] = 1.0; // a 1 somewhere
674 
675  this->fillWith(v);
676  return *this;
677  }
virtual Size domainSize() const final
Returns the product of the variables domain size.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
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...
GUM_SCALAR sum() const
sum of all elements in the Potential
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ randomCPT()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::randomCPT ( ) const

generate a random CPT in the Potential

Definition at line 687 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

687  {
688  return this->random().normalizeAsCPT();
689  }
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
+ Here is the call graph for this function:

◆ randomDistribution()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::randomDistribution ( ) const

generate a random Distribution in the Potential

Definition at line 681 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

681  {
682  return this->random().normalize();
683  }
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
+ Here is the call graph for this function:

◆ reduce()

template<typename GUM_SCALAR >
GUM_SCALAR gum::MultiDimDecorator< GUM_SCALAR >::reduce ( std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) >  f,
GUM_SCALAR  base 
) const
finalvirtualinherited

compute lfold for this container

Parameters
fthe function to apply
basethe initial value

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 360 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

362  {
363  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
364  return base;
365  } else {
366  return content_->reduce(f, base);
367  }
368  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
+ Here is the call graph for this function:

◆ registerSlave()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimDecorator< GUM_SCALAR >::registerSlave ( Instantiation i)
finalvirtualinherited

Register i as a slave of this MultiDimAdressable.

Parameters
iThe Instantiation to enslave.
Returns
Returns true if i becomes a slave of this MultiDimAdressable.

Implements gum::MultiDimAdressable.

Definition at line 249 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

249  {
250  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
251  ->registerSlave(i);
252  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.
+ Here is the call graph for this function:

◆ reorganize() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::reorganize ( const std::vector< const DiscreteVariable * > &  vars) const

create a new Potential with another order

Exceptions
InvalidArgumentif not all and only the vars of the potential are in vars

Definition at line 526 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

527  {
528  if (vars.size() != this->nbrDim())
529  GUM_ERROR(InvalidArgument,
530  "The vector contains " << vars.size() << " variables instead of "
531  << this->nbrDim() << ".");
532  for (const auto var: vars) {
533  if (!this->contains(*var))
534  GUM_ERROR(InvalidArgument,
535  "A variable in the vector does not belong to the potential.");
536  }
537 
538  Potential< GUM_SCALAR > p;
539  p.beginMultipleChanges();
540  for (const auto var: vars)
541  p.add(*var);
542  p.endMultipleChanges();
543  p.copyFrom(*this, nullptr); // copy *this in p using the same order
544 
545  return p;
546  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ reorganize() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::Potential< GUM_SCALAR >::reorganize ( const std::vector< std::string > &  vars) const

create a new Potential with another order

Exceptions
InvalidArgumentif not all and only the vars of the potential are in vars

Definition at line 549 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

550  {
551  std::vector< const DiscreteVariable* > res;
552 
554  for (gum::Idx i = 0; i < this->nbrDim(); i++)
555  namesToVars.insert(this->variable(i).name(), &(this->variable(i)));
556 
557  for (const auto& name: vars) {
558  if (!namesToVars.exists(name)) {
560  "'" << name
561  << "' is a not a name of a variable in this potential");
562  }
563  res.push_back(namesToVars[name]);
564  }
565  return reorganize(res);
566  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
Size Idx
Type for indexes.
Definition: types.h:52
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ replace()

INLINE void gum::MultiDimInterface::replace ( const DiscreteVariable x,
const DiscreteVariable y 
)
inherited

Replace variables in this multidim.

If x is in this MultiDim and y has the same domain size, then x will be replace by y in this MultiDim.

Parameters
xThe variable in this which will be replaced.
yThe variable replacing y.
Exceptions
NotFoundRaised if x does not belong to this MultiDim.
OperationNotAllowedIf y and x are not interchangeable.
DuplicateElementIf y is already in this MultiDim.

Definition at line 35 of file multiDimInterface_inl.h.

References gum::Set< Key, Alloc >::emplace().

36  {
37  if (!contains(x)) { GUM_ERROR(NotFound, "could not find the variable"); }
38 
39  if (contains(y)) {
40  GUM_ERROR(DuplicateElement, "variable " << y << " already in MultiDim");
41  }
42 
43  if (x.domainSize() != y.domainSize()) {
44  GUM_ERROR(OperationNotAllowed, "incompatible variables");
45  }
46 
47  replace_(&x, &y);
48  }
virtual bool contains(const DiscreteVariable &v) const =0
Returns true if var is in *this.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)=0
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ replace_()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::replace_ ( const DiscreteVariable x,
const DiscreteVariable y 
)
protectedvirtualinherited

This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.

This is called only when everything have been checked.

Parameters
xThe variable to replace in
yThe second variable to swap.

Implements gum::MultiDimInterface.

Definition at line 427 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

428  {
429  this->content()->replace(*x, *y);
430  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
+ Here is the call graph for this function:

◆ scale()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::scale ( GUM_SCALAR  v) const

multiply (each value of) *this by v

Definition at line 408 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

408  {
409  this->apply([v](GUM_SCALAR x) { return x * v; });
410  return *this;
411  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
+ Here is the call graph for this function:

◆ set()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::set ( const Instantiation i,
const GUM_SCALAR &  value 
) const
finalvirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 162 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

163  {
164  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim() == 0) {
165  empty_value_ = value;
166  } else {
167  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->set(i, value);
168  }
169  }
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ setChangeNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::setChangeNotification ( const Instantiation i)
finalvirtualinherited

Listen to an assignment of a value in a Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 204 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

205  {
206  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
208  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
+ Here is the call graph for this function:

◆ setDecNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::setDecNotification ( const Instantiation i)
finalvirtualinherited

Listen to increment in each recorded Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 241 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

241  {
242  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->setDecNotification(
243  i);
244  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.
+ Here is the call graph for this function:

◆ setFirstNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::setFirstNotification ( const Instantiation i)
finalvirtualinherited

Listen to setFirst in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 213 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

214  {
215  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
217  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.
+ Here is the call graph for this function:

◆ setIncNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::setIncNotification ( const Instantiation i)
finalvirtualinherited

Listen to increment in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 232 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

232  {
233  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->setIncNotification(
234  i);
235  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.
+ Here is the call graph for this function:

◆ setLastNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::setLastNotification ( const Instantiation i)
finalvirtualinherited

Listen to setLast in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 223 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

223  {
224  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->setLastNotification(
225  i);
226  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.
+ Here is the call graph for this function:

◆ sq()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::sq ( ) const

apply $x^2$ on every element of the container

Definition at line 295 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

295  {
296  this->apply([](GUM_SCALAR x) { return x * x; });
297  return *this;
298  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
+ Here is the call graph for this function:

◆ sum()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::Potential< GUM_SCALAR >::sum ( ) const

sum of all elements in the Potential

Definition at line 129 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

129  {
130  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
131  return this->empty_value_;
132  }
133  return gum::projectSum(*this->content());
134  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
+ Here is the call graph for this function:

◆ swapContent_()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimDecorator< GUM_SCALAR >::swapContent_ ( MultiDimImplementation< GUM_SCALAR > *  aContent) const
protectedinherited

protecte method to swap the implementation behind the Potential

Warning
unsafe method for slave Instantiations !

Definition at line 404 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

405  {
406  if (aContent != nullptr) {
407  // TODO : frees all slave instantiations
408  // TODO : control the dimensions ?
409  MultiDimImplementation< GUM_SCALAR >* tmp = content_;
410  content_ = aContent;
411  // registers all instantiations
412  delete (tmp);
413  }
414  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ toString() [1/2]

template<typename GUM_SCALAR >
INLINE std::string gum::MultiDimDecorator< GUM_SCALAR >::toString ( const Instantiation i) const
virtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 420 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

420  {
421  return content_->toString(i);
422  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ toString() [2/2]

template<typename GUM_SCALAR>
virtual std::string gum::Potential< GUM_SCALAR >::toString ( ) const
inlinevirtual

the function to be used to add two Potentials

Reimplemented from gum::MultiDimDecorator< GUM_SCALAR >.

Definition at line 517 of file potential.h.

517  {
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  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ translate()

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::Potential< GUM_SCALAR >::translate ( GUM_SCALAR  v) const

add v to (each value of) *this

Definition at line 415 of file potential_tpl.h.

References gum::Set< Key, Alloc >::emplace().

415  {
416  this->apply([v](GUM_SCALAR x) { return x + v; });
417  return *this;
418  }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
+ Here is the call graph for this function:

◆ unregisterSlave()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimDecorator< GUM_SCALAR >::unregisterSlave ( Instantiation i)
finalvirtualinherited

Unregister i as a slave of this MultiDimAdressable.

Parameters
iThe Instantiation to free.
Returns
Returns true, whatever happens.

Implements gum::MultiDimAdressable.

Definition at line 297 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

297  {
298  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
299  ->unregisterSlave(i);
300  }
virtual bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
+ Here is the call graph for this function:

◆ variable() [1/2]

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::MultiDimDecorator< GUM_SCALAR >::variable ( Idx  i) const
finalvirtualinherited

Returns a const ref to the ith var.

param i An index of this multidimensional matrix.

Returns
Returns a const ref to the ith var.
Exceptions
NotFoundRaised if i does not reference a variable in this multidimensional matrix.

Implements gum::MultiDimInterface.

Definition at line 266 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

266  {
267  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(i);
268  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
+ Here is the call graph for this function:

◆ variable() [2/2]

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::MultiDimDecorator< GUM_SCALAR >::variable ( const std::string &  name) const
finalvirtualinherited

Returns the variable with the name.

Parameters
nameThe index of the variable
Returns
Returns the variable qith the name in the tuple. This function is not O(1)
Exceptions
NotFoundRaised if the element cannot be found.

Implements gum::MultiDimInterface.

Definition at line 272 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

272  {
273  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(
274  name);
275  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
+ Here is the call graph for this function:

◆ variablesSequence()

template<typename GUM_SCALAR >
INLINE const Sequence< const DiscreteVariable *> & gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence ( ) const
finalvirtualinherited

Returns a const ref to the sequence of DiscreteVariable*.

Returns
Returns a const ref to the sequence of DiscreteVariable*.

Implements gum::MultiDimInterface.

Definition at line 323 of file multiDimDecorator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

323  {
324  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
325  ->variablesSequence();
326  }
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
+ Here is the call graph for this function:

Member Data Documentation

◆ content_

template<typename GUM_SCALAR >
MultiDimImplementation< GUM_SCALAR >* gum::MultiDimDecorator< GUM_SCALAR >::content_
mutableprotectedinherited

The true container.

Definition at line 250 of file multiDimDecorator.h.

◆ empty_value_

template<typename GUM_SCALAR >
GUM_SCALAR gum::MultiDimDecorator< GUM_SCALAR >::empty_value_
mutableprotectedinherited

value of the MultiDimDecorator if no dimension.

Definition at line 264 of file multiDimDecorator.h.


The documentation for this class was generated from the following files: