aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::Tensor< GUM_SCALAR > Class Template Referencefinal

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

#include <agrum/base/multidim/tensor.h>

Inheritance diagram for gum::Tensor< GUM_SCALAR >:
Collaboration diagram for gum::Tensor< GUM_SCALAR >:

Public Member Functions

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

Static Public Member Functions

static Tensor< GUM_SCALAR > deterministicTensor (const DiscreteVariable &var, Idx value)
static Tensor< GUM_SCALAR > deterministicTensor (const DiscreteVariable &var, const std::string &label)
static Tensor< GUM_SCALAR > uniformTensor (const DiscreteVariable &var)
static Tensor< GUM_SCALAR > evEq (const DiscreteVariable &v, double val)
 numerical evidence generator
static Tensor< GUM_SCALAR > evIn (const DiscreteVariable &v, double val1, double val2)
 numerical evidence generator
static Tensor< GUM_SCALAR > evGt (const DiscreteVariable &v, double val)
 numerical evidence generator
static Tensor< GUM_SCALAR > evLt (const DiscreteVariable &v, double val)
 numerical evidence generator

Protected Member Functions

gum::VariableSet complementVars_ (const gum::VariableSet &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.
void swapContent_ (MultiDimImplementation< GUM_SCALAR > *aContent) const
 protected method to swap the implementation behind the Tensor
GUM_SCALAR & get_ (const Instantiation &i) const final
 Return a data, given a Instantiation - final method.

Protected Attributes

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

Detailed Description

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

aGrUM's Tensor 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.
Warning
The Tensor class is not thread-safe. If you want to use it in a threaded context, you have to use a mutex to protect the access to the class.
The Tensor class does not contain its variables. It only contains (maybe dangling) pointers to the variables.

Definition at line 85 of file tensor.h.

Constructor & Destructor Documentation

◆ Tensor() [1/6]

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

Default constructor.

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

Definition at line 61 of file tensor_tpl.h.

61 :
64 }
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Class constructor.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Tensor()
Default constructor.
Definition tensor_tpl.h:61

References gum::MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(), and Tensor().

Referenced by Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), ~Tensor(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Tensor() [2/6]

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

Default constructor.

Creates an empty null dimensional matrix with a MultiDimArray as its implementation and add the vars

Definition at line 69 of file tensor_tpl.h.

69 :
70 Tensor() {
71 for (const auto& var: vars) {
72 this->add(*var);
73 }
74 }
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.

References Tensor(), and gum::MultiDimDecorator< GUM_SCALAR >::add().

Here is the call graph for this function:

◆ Tensor() [3/6]

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

Creates an tensor around aContent.

Parameters
aContentThe implementation of this Tensor.

Definition at line 78 of file tensor_tpl.h.

78 :
80 // for debugging purposes
82 }

References gum::MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(), and Tensor().

Here is the call graph for this function:

◆ Tensor() [4/6]

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

Copy constructor.

The newly created Tensor 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 Tensor.
srcThe MultiDimContainer to copy.

Definition at line 104 of file tensor_tpl.h.

105 :
107 // for debugging purposes
109
110 if (!src.empty()) {
111 this->beginMultipleChanges();
112
113 for (Idx i = 0; i < src.variablesSequence().size(); i++) {
114 this->add(*(src.variablesSequence()[i]));
115 }
116
117 this->endMultipleChanges();
118 this->content()->copyFrom(*src.content());
119 }
120 }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
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).

References gum::MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(), Tensor(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::MultiDimDecorator< GUM_SCALAR >::beginMultipleChanges(), gum::MultiDimContainer< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimInterface::empty(), gum::MultiDimDecorator< GUM_SCALAR >::endMultipleChanges(), and gum::MultiDimInterface::variablesSequence().

Here is the call graph for this function:

◆ Tensor() [5/6]

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

Copy constructor & assignment.

Definition at line 86 of file tensor_tpl.h.

86 :
88 static_cast< MultiDimImplementation< GUM_SCALAR >* >(src.content()->newFactory()),
89 *(src.content())) {
91 // GUM_CONS_CPY not here because in called Tensor
92 // GUM_CONS_CPY( Tensor );
93 }
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.

References Tensor(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and newFactory().

Here is the call graph for this function:

◆ Tensor() [6/6]

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

move constructor & assignement

move constructor

Definition at line 97 of file tensor_tpl.h.

References gum::MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(), and Tensor().

Here is the call graph for this function:

◆ ~Tensor()

template<typename GUM_SCALAR>
gum::Tensor< GUM_SCALAR >::~Tensor ( )
final

Destructor.

Definition at line 144 of file tensor_tpl.h.

144 {
145 // for debugging purposes
147 }

References Tensor().

Here is the call graph for this function:

Member Function Documentation

◆ abs()

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

Apply abs on every element of the container.

Definition at line 379 of file tensor_tpl.h.

379 {
380 this->apply([](GUM_SCALAR x) {
381 if (x >= 0) return x;
382 else return -x;
383 });
384 return *this;
385 }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const
Apply a function on every element of the container.

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

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 188 of file multiDimDecorator_tpl.h.

188 {
189 if (v.domainSize() < 1) {
190 GUM_ERROR(InvalidArgument, "Empty variable " << v << " cannot be added in a Tensor")
191 }
193 }
MultiDimContainer()
Default constructor.
Decorator design pattern in order to separate implementations from multidimensional matrix concepts.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual Size domainSize() const final
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), add(), content_, gum::DiscreteVariable::domainSize(), and GUM_ERROR.

Referenced by gum::Tensor< GUM_SCALAR >::Tensor(), gum::Tensor< GUM_SCALAR >::Tensor(), add(), gum::Tensor< GUM_SCALAR >::evEq(), gum::Tensor< GUM_SCALAR >::evGt(), gum::Tensor< GUM_SCALAR >::evIn(), and gum::Tensor< GUM_SCALAR >::evLt().

Here is the call graph for this function:
Here is the caller 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 338 of file multiDimDecorator_tpl.h.

338 {
339 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
341 } else {
342 content_->apply(f);
343 }
344 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, empty(), and empty_value_.

Referenced by gum::Tensor< GUM_SCALAR >::abs(), gum::Tensor< GUM_SCALAR >::inverse(), gum::Tensor< GUM_SCALAR >::log2(), gum::Tensor< GUM_SCALAR >::normalize(), gum::Tensor< GUM_SCALAR >::scale(), gum::Tensor< GUM_SCALAR >::sgn(), gum::Tensor< GUM_SCALAR >::sq(), and gum::Tensor< GUM_SCALAR >::translate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ argmax()

template<typename GUM_SCALAR>
INLINE std::pair< Set< Instantiation >, GUM_SCALAR > gum::Tensor< GUM_SCALAR >::argmax ( ) const

Pair of the set of instantiation corresponding to the max and this max in the Tensor.

Definition at line 732 of file tensor_tpl.h.

732 {
733 auto m = max();
734 return std::pair(findAll(m), m);
735 }
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Tensor
Definition tensor_tpl.h:718
GUM_SCALAR max() const
max of all elements in the Tensor
Definition tensor_tpl.h:175

References findAll(), and max().

Here is the call graph for this function:

◆ argmin()

template<typename GUM_SCALAR>
INLINE std::pair< Set< Instantiation >, GUM_SCALAR > gum::Tensor< GUM_SCALAR >::argmin ( ) const

Pair of the set of instantiation corresponding to the min and this min in the Tensor.

Definition at line 739 of file tensor_tpl.h.

739 {
740 auto m = min();
741 return std::pair(findAll(m), m);
742 }
GUM_SCALAR min() const
min of all elements in the Tensor
Definition tensor_tpl.h:184

References findAll(), and min().

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 371 of file multiDimDecorator_tpl.h.

371 {
373 }
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), beginMultipleChanges(), and content_.

Referenced by gum::Tensor< GUM_SCALAR >::Tensor(), and beginMultipleChanges().

Here is the call graph for this function:
Here is the caller 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 198 of file multiDimDecorator_tpl.h.

201 {
203 var,
204 oldval,
205 newval);
206 }
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), changeNotification(), and content_.

Referenced by changeNotification().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ complementVars_()

template<typename GUM_SCALAR>
gum::VariableSet gum::Tensor< GUM_SCALAR >::complementVars_ ( const gum::VariableSet & del_vars) const
nodiscardprotected

Definition at line 607 of file tensor_tpl.h.

607 {
609
610 for (const auto x: this->variablesSequence())
611 if (!vars.contains(x)) cplt.insert(x);
612
613 return cplt;
614 }
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.

References gum::Set< Key >::contains(), gum::Set< Key >::insert(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Referenced by maxIn(), minIn(), prodIn(), and sumIn().

Here is the call graph for this function:
Here is the caller 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 277 of file multiDimDecorator_tpl.h.

277 {
278 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->contains(var);
279 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), contains(), and content_.

Referenced by contains(), gum::Tensor< GUM_SCALAR >::KL(), gum::Tensor< GUM_SCALAR >::putFirst(), and gum::Tensor< GUM_SCALAR >::reorganize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ content() [1/2]

◆ 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 359 of file multiDimDecorator_tpl.h.

359 {
360 return content_;
361 }

References content_.

◆ 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< bool >, gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >, gum::MultiDimFunctionGraph< double >, gum::MultiDimFunctionGraph< double, ExactTerminalNodePolicy >, gum::MultiDimFunctionGraph< gum::ActionSet, gum::SetTerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >, gum::MultiDimFunctionGraph< Size >, and gum::MultiDimFunctionGraph< Size, ExactTerminalNodePolicy >.

Definition at line 288 of file multiDimContainer_tpl.h.

288 {
289 this->beginMultipleChanges();
290
291 Size nbr = this->nbrDim();
292
293 for (Idx i = 0; i < nbr; i++) {
294 this->erase(this->variable(0));
295 }
296
297 for (Idx i = 0; i < src.nbrDim(); i++) {
298 this->add(src.variable(i));
299 }
300
301 this->endMultipleChanges();
302 this->copyFrom(src);
303 }
Abstract base class for all multi dimensionnal containers.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a 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 erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.

References MultiDimContainer(), beginMultipleChanges(), gum::MultiDimInterface::erase(), gum::MultiDimInterface::nbrDim(), and gum::MultiDimInterface::variable().

Referenced by gum::TreeRegress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::compute().

Here is the call graph for this function:
Here is the caller 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::MultiDimArray< GUM_SCALAR >, and gum::MultiDimICIModel< GUM_SCALAR >.

Definition at line 271 of file multiDimContainer_tpl.h.

271 {
272 if (src.domainSize() != domainSize()) {
274 "Domain sizes do not fit : " << src.domainSize() << "!=" << domainSize());
275 }
276
277 Instantiation i_dest(*this);
279
280 for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
281 set(i_dest, src[i_src]);
282 }
283 }
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Changes the value pointed by i.
virtual Size domainSize() const =0
Returns the product of the variables domain size.

References MultiDimContainer(), gum::MultiDimInterface::domainSize(), and GUM_ERROR.

Referenced by gum::aggregator::MultiDimAggregator< GUM_SCALAR >::copyFrom(), gum::MultiDimArray< GUM_SCALAR >::copyFrom(), and gum::MultiDimICIModel< GUM_SCALAR >::copyFrom().

Here is the call graph for this function:
Here is the caller 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 >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 225 of file multiDimContainer_tpl.h.

226 {
227 if (src.domainSize() != domainSize()) {
229 "Domain sizes do not fit : " << src.domainSize() << "!=" << domainSize());
230 }
231
232 if (p_i == nullptr) { // if null, we just follow the same order
234 for (i.setFirst(); !i.end(); ++i) {
235 set(i, src[i]);
236 }
237 } else {
238 Instantiation i_dest(*this);
240 for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); i_dest.incIn(*p_i), ++i_src) {
241 set(i_dest, src[i_src]);
242 }
243 }
244 }

References MultiDimContainer(), gum::MultiDimInterface::domainSize(), and GUM_ERROR.

Here is the call graph for this function:

◆ deterministicTensor() [1/2]

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::deterministicTensor ( const DiscreteVariable & var,
const std::string & label )
static

Definition at line 1172 of file tensor_tpl.h.

1173 {
1175 }
static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable &var, Idx value)

References deterministicTensor(), and gum::DiscreteVariable::index().

Here is the call graph for this function:

◆ deterministicTensor() [2/2]

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::deterministicTensor ( const DiscreteVariable & var,
Idx value )
static

Definition at line 1156 of file tensor_tpl.h.

1157 {
1159
1161 pot << var;
1163
1165 I.chgVal(var, value);
1166 pot.set(I, GUM_SCALAR(1.0));
1167
1168 return pot;
1169 }
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().

References gum::Instantiation::chgVal().

Referenced by gum::BayesNet< double >::_clearTensors_(), gum::GraphicalModelInference< GUM_SCALAR >::_createHardEvidence_(), and deterministicTensor().

Here is the call graph for this function:
Here is the caller 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 181 of file multiDimDecorator_tpl.h.

181 {
182 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->domainSize();
183 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and domainSize().

Referenced by domainSize(), gum::SimpleUTGenerator::generateUT(), gum::SimpleUTGenerator::generateUT(), gum::InfluenceDiagram< GUM_SCALAR >::operator==(), gum::Tensor< GUM_SCALAR >::random(), gum::Tensor< GUM_SCALAR >::randomCPT(), and gum::Tensor< GUM_SCALAR >::randomDistribution().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ draw()

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

get a value at random from a 1-D distribution

Definition at line 696 of file tensor_tpl.h.

696 {
697 if (this->nbrDim() != 1) {
698 GUM_ERROR(FatalError, "To draw from a tensor, the dimension must be 1")
699 }
700
701 auto r = static_cast< GUM_SCALAR >(randomProba());
702 Instantiation Ip(*this);
703 for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
704 r -= this->get(Ip);
705 if (r <= 0) return Ip.val(0);
706 }
707 return this->variable(0).domainSize() - 1;
708 }
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
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.

References gum::Instantiation::end(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::Instantiation::inc(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::randomProba(), gum::Instantiation::setFirst(), gum::Instantiation::val(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Referenced by gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_().

Here is the call graph for this function:
Here is the caller 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 282 of file multiDimDecorator_tpl.h.

282 {
283 if (content_ == nullptr) return true;
284 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty();
285 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and empty().

Referenced by apply(), empty(), gum::Tensor< GUM_SCALAR >::expectedValue(), fill(), get(), gum::Tensor< GUM_SCALAR >::max(), gum::Tensor< GUM_SCALAR >::maxIn(), gum::Tensor< GUM_SCALAR >::maxNonOne(), gum::Tensor< GUM_SCALAR >::maxOut(), gum::Tensor< GUM_SCALAR >::min(), gum::Tensor< GUM_SCALAR >::minIn(), gum::Tensor< GUM_SCALAR >::minNonZero(), gum::Tensor< GUM_SCALAR >::minOut(), gum::Tensor< GUM_SCALAR >::normalize(), gum::Tensor< GUM_SCALAR >::normalizeAsCPT(), gum::Tensor< GUM_SCALAR >::operator*(), gum::Tensor< GUM_SCALAR >::operator+(), gum::Tensor< GUM_SCALAR >::operator-(), gum::Tensor< GUM_SCALAR >::operator/(), gum::Tensor< GUM_SCALAR >::operator==(), populate(), gum::Tensor< GUM_SCALAR >::prodIn(), gum::Tensor< GUM_SCALAR >::prodOut(), gum::Tensor< GUM_SCALAR >::product(), reduce(), gum::Tensor< GUM_SCALAR >::sum(), gum::Tensor< GUM_SCALAR >::sumIn(), gum::Tensor< GUM_SCALAR >::sumOut(), and toString().

Here is the call graph for this function:
Here is the caller 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 376 of file multiDimDecorator_tpl.h.

376 {
378 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and endMultipleChanges().

Referenced by gum::Tensor< GUM_SCALAR >::Tensor(), endMultipleChanges(), endMultipleChanges(), and newFactory().

Here is the call graph for this function:
Here is the caller 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 381 of file multiDimDecorator_tpl.h.

381 {
383 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and endMultipleChanges().

Here is the call graph for this function:

◆ entropy()

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

entropy of the Tensor

Definition at line 250 of file tensor_tpl.h.

250 {
251 return -this->expectedValue([this](const gum::Instantiation& i) -> GUM_SCALAR {
252 return GUM_SCALAR(GUM_LOG2_OR_0(this->get(i)));
253 });
254 }
GUM_SCALAR expectedValue(std::function< GUM_SCALAR(const gum::Instantiation &) >) const
ExpectedValue computes the expectation of f over *this.
Definition tensor_tpl.h:233
#define GUM_LOG2_OR_0(x)
Definition math_utils.h:70

References expectedValue(), gum::MultiDimDecorator< GUM_SCALAR >::get(), and GUM_LOG2_OR_0.

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 251 of file multiDimDecorator_tpl.h.

251 {
253 }
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and erase().

Referenced by erase(), and erase().

Here is the call graph for this function:
Here is the caller 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 256 of file multiDimDecorator_tpl.h.

256 {
258 }

References erase(), and variable().

Here is the call graph for this function:

◆ evEq()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evEq ( const DiscreteVariable & v,
double val )
static

numerical evidence generator

Definition at line 1085 of file tensor_tpl.h.

1085 {
1086 const auto i = v.closestIndex(val);
1087
1089 p.add(v);
1090 p.fillWith(0);
1091 Instantiation I(p);
1092 I.chgVal(0, i);
1093 p.set(I, 1);
1094 return p;
1095 }
const Tensor< GUM_SCALAR > & fillWith(const Tensor< GUM_SCALAR > &src) const
copy a Tensor data using name of variables and labels (not necessarily the same variables in the same...
Definition tensor_tpl.h:271

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), fillWith(), and gum::MultiDimDecorator< GUM_SCALAR >::set().

Referenced by gum::IBayesNet< GUM_SCALAR >::evEq(), evGt(), and evLt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evGt()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evGt ( const DiscreteVariable & v,
double val )
static

numerical evidence generator

Definition at line 1119 of file tensor_tpl.h.

1119 {
1120 const auto i = v.closestIndex(val);
1121 if (i == v.domainSize() - 1) { return evEq(v, val); }
1122
1124 p.add(v);
1125 p.fillWith(0);
1126 Instantiation I(p);
1127 for (Idx i2 = i + 1; i2 < v.domainSize(); i2++) {
1128 I.chgVal(0, i2);
1129 p.set(I, 1);
1130 }
1131 return p;
1132 }
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), gum::DiscreteVariable::domainSize(), evEq(), fillWith(), and gum::MultiDimDecorator< GUM_SCALAR >::set().

Referenced by gum::IBayesNet< GUM_SCALAR >::evGt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evIn()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evIn ( const DiscreteVariable & v,
double val1,
double val2 )
static

numerical evidence generator

Definition at line 1099 of file tensor_tpl.h.

1099 {
1100 if (val2 < val1) {
1102 "val2 (" << val2 << ") must be greater than val1 (" << val1 << ").")
1103 }
1104 const auto i1 = v.closestIndex(val1);
1105 const auto i2 = v.closestIndex(val2);
1106
1108 p.add(v);
1109 p.fillWith(0);
1110 Instantiation I(p);
1111 for (Idx i = i1; i <= i2; i++) {
1112 I.chgVal(0, i);
1113 p.set(I, 1);
1114 }
1115 return p;
1116 }

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), fillWith(), GUM_ERROR, and gum::MultiDimDecorator< GUM_SCALAR >::set().

Referenced by gum::IBayesNet< GUM_SCALAR >::evIn().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evLt()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evLt ( const DiscreteVariable & v,
double val )
static

numerical evidence generator

Definition at line 1135 of file tensor_tpl.h.

1135 {
1136 const auto i = v.closestIndex(val);
1137 if (i == 0) { return evEq(v, val); }
1138
1140 p.add(v);
1141 p.fillWith(0);
1142 Instantiation I(p);
1143 for (Idx i2 = 0; i2 < i; i2++) {
1144 I.chgVal(0, i2);
1145 p.set(I, 1);
1146 }
1147 return p;
1148 }

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), evEq(), fillWith(), and gum::MultiDimDecorator< GUM_SCALAR >::set().

Referenced by gum::IBayesNet< GUM_SCALAR >::evLt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expectedValue()

template<typename GUM_SCALAR>
GUM_SCALAR gum::Tensor< GUM_SCALAR >::expectedValue ( std::function< GUM_SCALAR(const gum::Instantiation &) > f) const

ExpectedValue computes the expectation of f over *this.

Warnings

ExpectedValue assumes (and does not check) that the Tensor contains a joint distribution.

Definition at line 233 of file tensor_tpl.h.

234 {
235 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
236 return static_cast< GUM_SCALAR >(0);
237 }
238
239 GUM_SCALAR res = 0;
240 auto i = Instantiation(*this);
241 for (i.setFirst(); !i.end(); i.inc()) {
242 const GUM_SCALAR v_f = f(i);
243 if (v_f != GUM_SCALAR(0.0)) res += this->get(i) * v_f;
244 }
245 return res;
246 }

References gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::get().

Referenced by entropy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract()

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

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

Definition at line 688 of file tensor_tpl.h.

688 {
690 p.extractFrom(*this, inst);
691
692 return p;
693 }
virtual void extractFrom(const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.

◆ 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 Tensor : 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 247 of file multiDimContainer_tpl.h.

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 tensor") }
263
265 inst.setVals(imask);
266 for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
267 set(inst, src[inst]);
268 }
virtual bool contains(const DiscreteVariable &v) const =0
Returns true if var is in *this.

References MultiDimContainer(), and beginMultipleChanges().

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 293 of file multiDimDecorator_tpl.h.

293 {
294 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
295 empty_value_ = d;
296 } else {
298 }
299 }
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, empty(), empty_value_, and fill().

Referenced by fill(), and gum::Tensor< GUM_SCALAR >::fillWith().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fillWith() [1/4]

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

Automatically fills this MultiDimContainer with the value v.

Parameters
vcontains the data.

Definition at line 264 of file tensor_tpl.h.

264 {
265 this->fill(val);
266 return *this;
267 }
virtual void fill(const GUM_SCALAR &d) const =0
Fill the table with d.

References gum::MultiDimDecorator< GUM_SCALAR >::fill().

Here is the call graph for this function:

◆ fillWith() [2/4]

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

Automatically fills the tensor 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 258 of file tensor_tpl.h.

258 {
259 this->populate(data);
260 return *this;
261 }
virtual void populate(const std::vector< GUM_SCALAR > &v) const
Automatically fills this MultiDimContainer with the values in v.

References gum::MultiDimDecorator< GUM_SCALAR >::populate().

Here is the call graph for this function:

◆ fillWith() [3/4]

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

copy a Tensor 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 Tensor is not compatible with this

Definition at line 271 of file tensor_tpl.h.

271 {
272 if (src.domainSize() != this->domainSize()) {
273 GUM_ERROR(InvalidArgument, "Tensor to copy has not the same domainSize.")
274 }
275 gum::Set< std::string > son; // set of names
276 for (const auto& v: src.variablesSequence()) {
277 son.insert(v->name());
278 }
279 for (const auto& v: this->variablesSequence()) {
280 if (!son.contains(v->name())) {
282 "Variable <" << v->name() << "> not present in src (" << son << ").")
283 }
284 // we check size, labels and order of labels in the same time
285 if (v->toString() != src.variable(v->name()).toString()) {
286 GUM_ERROR(InvalidArgument, "Variables <" << v->name() << "> are not identical.")
287 }
288 }
289
291 Instantiation Idst(*this);
292 for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
293 for (Idx i = 0; i < this->nbrDim(); i++) {
294 Idst.chgVal(Isrc.variable(i).name(), Isrc.val(i));
295 }
296 this->set(Idst, src.get(Isrc));
297 }
298
299 return *this;
300 }
std::string toString() const
string version of *this
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
std::string toString() const final
the function to be used to add two Tensors
Definition tensor_tpl.h:999
const std::string & name() const
returns the name of the variable

References gum::Instantiation::chgVal(), gum::Set< Key >::contains(), gum::Instantiation::end(), GUM_ERROR, gum::Set< Key >::insert(), gum::Variable::name(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), gum::Instantiation::val(), gum::Instantiation::variable(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Referenced by gum::DecisionTensor< GUM_SCALAR >::clear(), evEq(), evGt(), evIn(), evLt(), noising(), random(), randomCPT(), and randomDistribution().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fillWith() [4/4]

template<typename GUM_SCALAR>
INLINE const Tensor< GUM_SCALAR > & gum::Tensor< GUM_SCALAR >::fillWith ( const Tensor< GUM_SCALAR > & src,
const std::vector< std::string > & mapSrc ) const

copy a Tensor data using the sequence of names in mapSrc to find the corresponding variables.

For instance, to copy the tensor 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 Tensor is not compatible with this

Definition at line 304 of file tensor_tpl.h.

305 {
306 if (src.nbrDim() != this->nbrDim()) {
307 GUM_ERROR(InvalidArgument, "Tensor to copy has not the same size.")
308 }
309 if (src.nbrDim() != mapSrc.size()) {
310 GUM_ERROR(InvalidArgument, "Tensor and vector have not the same size.")
311 }
313 for (Idx i = 0; i < src.nbrDim(); i++) {
316 "Variables " << mapSrc[i] << " (in the argument) and " << this->variable(i).name()
317 << " have not the same dimension.")
318 } else {
320 }
321 }
322 Instantiation Idst(*this);
323 for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
324 this->set(Idst, src.get(Isrc));
325 }
326
327 return *this;
328 }
virtual Size domainSize() const =0

References gum::Instantiation::add(), gum::Instantiation::end(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ findAll()

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

set of instantiation corresponding to the parameter v in the Tensor

Definition at line 718 of file tensor_tpl.h.

718 {
719 Instantiation I(*this);
721
722 // if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) { return
723 // res; }
724 for (I.setFirst(); !I.end(); ++I) {
725 if (this->get(I) == v) res.insert(Instantiation(I, false));
726 }
727 return res;
728 }

References gum::Instantiation::end(), gum::MultiDimDecorator< GUM_SCALAR >::get(), gum::Set< Key >::insert(), and gum::Instantiation::setFirst().

Referenced by argmax(), and argmin().

Here is the call graph for this function:
Here is the caller 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 160 of file multiDimDecorator_tpl.h.

160 {
161 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
162 return empty_value_;
163 } else {
164 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->get(i);
165 }
166 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, empty(), empty_value_, and get().

Referenced by gum::Tensor< GUM_SCALAR >::draw(), gum::Tensor< GUM_SCALAR >::entropy(), gum::Tensor< GUM_SCALAR >::expectedValue(), gum::Tensor< GUM_SCALAR >::findAll(), get(), gum::Tensor< GUM_SCALAR >::KL(), gum::Tensor< GUM_SCALAR >::normalizeAsCPT(), gum::InfluenceDiagram< GUM_SCALAR >::operator==(), gum::prm::o3prmr::O3prmrInterpreter::query(), and gum::Tensor< GUM_SCALAR >::toString().

Here is the call graph for this function:
Here is the caller 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 Instantiation - final method.

Parameters
iThe instantiation.
Exceptions
NullElement
NotFound

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 155 of file multiDimDecorator_tpl.h.

155 {
156 GUM_ERROR(OperationNotAllowed, "_get in the implementation !")
157 }

References GUM_ERROR.

◆ 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 306 of file multiDimContainer_tpl.h.

306 {
307 return static_cast< MultiDimAdressable& >(*content());
308 }
MultiDimAdressable()
Default constructor.

References gum::MultiDimAdressable::MultiDimAdressable(), and content().

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 311 of file multiDimContainer_tpl.h.

311 {
312 return static_cast< const MultiDimAdressable& >(*content());
313 }

References gum::MultiDimAdressable::MultiDimAdressable(), and content().

Here is the call graph for this function:

◆ inverse()

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

the function to inverse (each value of) *this

Definition at line 449 of file tensor_tpl.h.

449 {
450 this->apply([](GUM_SCALAR x) { return 1 / x; });
451 return *this;
452 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Here is the call graph for this function:

◆ isEvidence()

template<typename GUM_SCALAR>
bool gum::Tensor< GUM_SCALAR >::isEvidence ( ) const
nodiscard

is an evidence ? (marginal-like but has not to sum to 1)

Definition at line 839 of file tensor_tpl.h.

839 {
840 if (this->nbrDim() != 1) return false;
841 if (this->sum() <= 0.0) return false;
842 return (this->min() >= 0.0) && (this->max() <= 1.0);
843 }
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:157

References max(), min(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), and sum().

Referenced by operator&(), operator|(), and operator~().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isNonZeroMap()

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

create a boolean-like tensor using the predicate isNonZero

Definition at line 597 of file tensor_tpl.h.

597 {
598 auto p = Tensor< GUM_SCALAR >(*this);
599 p.apply([](GUM_SCALAR x) {
600 if (x != static_cast< GUM_SCALAR >(0)) return static_cast< GUM_SCALAR >(1);
601 else return static_cast< GUM_SCALAR >(0);
602 });
603 return p;
604 }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.

◆ KL()

template<typename GUM_SCALAR>
INLINE GUM_SCALAR gum::Tensor< GUM_SCALAR >::KL ( const Tensor< 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 349 of file tensor_tpl.h.

349 {
350 if (this->nbrDim() != p.nbrDim())
351 GUM_ERROR(InvalidArgument, "BNdistance between tensors with different numbers of dimensions")
353 if (!this->contains(*var))
354 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
355 }
356 for (const auto var: this->variablesSequence()) {
357 if (!p.contains(*var))
358 GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.")
359 }
360
361 Instantiation inst(*this);
362 auto res = static_cast< GUM_SCALAR >(0);
363 for (inst.setFirst(); !inst.end(); inst.inc()) {
364 GUM_SCALAR x = this->get(inst);
365 GUM_SCALAR y = p.get(inst);
366 if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
367 continue;
368
369 if (static_cast< GUM_SCALAR >(0) == y)
370 // we know that x!=0;
371 GUM_ERROR(FatalError, "The argument has a 0 at " << inst << " while the tensor has not.")
372
373 res += x * std::log2(x / y);
374 }
375 return res;
376 }
const Tensor< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Definition tensor_tpl.h:337

References gum::MultiDimDecorator< GUM_SCALAR >::contains(), gum::Instantiation::end(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::Instantiation::inc(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Here is the call graph for this function:

◆ log2()

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

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

Definition at line 337 of file tensor_tpl.h.

337 {
338 this->apply([](GUM_SCALAR x) { return std::log2(x); });
339 return *this;
340 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Here is the call graph for this function:

◆ max()

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

max of all elements in the Tensor

Definition at line 175 of file tensor_tpl.h.

175 {
176 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
177 return this->empty_value_;
178 }
179 return gum::projectMax(*this->content());
180 }
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::projectMax().

Referenced by argmax(), isEvidence(), maxIn(), and maxOut().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ maxIn()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::maxIn ( const gum::VariableSet & kept_vars) const

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

Parameters
kept_varsis the set of vars to keep

Definition at line 584 of file tensor_tpl.h.

584 {
585 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
587 }
588
589 // if kept_var is empty, create an empty tensor
590 // TODO: remove this test when operations will be able to handle empty tensors
591 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->max()); }
592
594 }
gum::VariableSet complementVars_(const gum::VariableSet &del_vars) const
Definition tensor_tpl.h:607

References complementVars_(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::Set< Key >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, max(), and gum::projectMax().

Here is the call graph for this function:

◆ maxNonOne()

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

max of all non one elements in the Tensor

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

Definition at line 194 of file tensor_tpl.h.

194 {
196
197 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
198 res = this->empty_value_;
199 } else {
200 res = this->reduce(
201 [](GUM_SCALAR z, GUM_SCALAR p) {
202 return (p == static_cast< GUM_SCALAR >(1)) ? z
203 : (z == static_cast< GUM_SCALAR >(1)) ? p
204 : (p > z ? p : z);
205 },
206 static_cast< GUM_SCALAR >(1));
207 }
208
209 return res;
210 }
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
compute lfold for this container

References gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::MultiDimDecorator< GUM_SCALAR >::reduce().

Here is the call graph for this function:

◆ maxOut()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::maxOut ( const gum::VariableSet & del_vars) const

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

Parameters
del_varsis the set of vars to eliminate

Definition at line 522 of file tensor_tpl.h.

522 {
523 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
525 }
526
527 // if we remove all the variables, create an empty tensor
528 // TODO: remove this test when operations will be able to handle empty tensors
529 if (this->variablesSequence().size() <= del_vars.size()) {
530 bool equal = true;
531 for (const auto var: this->variablesSequence()) {
532 if (!del_vars.exists(var)) {
533 equal = false;
534 break;
535 }
536 }
537 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->max()); }
538 }
539
541 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::Set< Key >::exists(), max(), gum::projectMax(), gum::Set< Key >::size(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Here is the call graph for this function:

◆ memoryFootprint()

template<typename GUM_SCALAR>
Size gum::Tensor< GUM_SCALAR >::memoryFootprint ( ) const

compute the (approximated) footprint in memory of the tensor

Returns
the size in bytes

Definition at line 1151 of file tensor_tpl.h.

1151 {
1152 return this->content()->realSize() * sizeof(GUM_SCALAR);
1153 }

References gum::MultiDimDecorator< GUM_SCALAR >::content().

Here is the call graph for this function:

◆ min()

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

min of all elements in the Tensor

Definition at line 184 of file tensor_tpl.h.

184 {
185 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
186 return this->empty_value_;
187 }
188 return gum::projectMin(*this->content());
189 }
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::projectMin().

Referenced by argmin(), isEvidence(), minIn(), and minOut().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ minIn()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::minIn ( const gum::VariableSet & kept_vars) const

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

Parameters
kept_varsis the set of vars to keep

Definition at line 571 of file tensor_tpl.h.

571 {
572 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
574 }
575
576 // if kept_var is empty, create an empty tensor
577 // TODO: remove this test when operations will be able to handle empty tensors
578 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->min()); }
579
581 }

References complementVars_(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::Set< Key >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, min(), and gum::projectMin().

Here is the call graph for this function:

◆ minNonZero()

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

min of all non zero elements in the Tensor

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

Definition at line 215 of file tensor_tpl.h.

215 {
217
218 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
219 res = this->empty_value_;
220 } else {
221 res = this->reduce(
222 [](GUM_SCALAR z, GUM_SCALAR p) {
223 return (p == static_cast< GUM_SCALAR >(0)) ? z
224 : (z == static_cast< GUM_SCALAR >(0)) ? p
225 : (p < z ? p : z);
226 },
227 static_cast< GUM_SCALAR >(0));
228 }
229 return res;
230 }

References gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::MultiDimDecorator< GUM_SCALAR >::reduce().

Here is the call graph for this function:

◆ minOut()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::minOut ( const gum::VariableSet & del_vars) const

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

Parameters
del_varsis the set of vars to eliminate

Definition at line 500 of file tensor_tpl.h.

500 {
501 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
503 }
504
505 // if we remove all the variables, create an empty tensor
506 // TODO: remove this test when operations will be able to handle empty tensors
507 if (this->variablesSequence().size() <= del_vars.size()) {
508 bool equal = true;
509 for (const auto var: this->variablesSequence()) {
510 if (!del_vars.exists(var)) {
511 equal = false;
512 break;
513 }
514 }
515 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->min()); }
516 }
517
519 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::Set< Key >::exists(), min(), gum::projectMin(), gum::Set< Key >::size(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

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 320 of file multiDimDecorator_tpl.h.

320 {
321 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim();
322 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and nbrDim().

Referenced by gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_(), gum::Tensor< GUM_SCALAR >::draw(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::Tensor< GUM_SCALAR >::isEvidence(), gum::Tensor< GUM_SCALAR >::KL(), nbrDim(), gum::Tensor< GUM_SCALAR >::normalizeAsCPT(), gum::InfluenceDiagram< GUM_SCALAR >::operator==(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::reorganize(), and set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_abs()

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

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

Definition at line 797 of file tensor_tpl.h.

797 {
798 return Tensor< GUM_SCALAR >(*this).abs();
799 }
const Tensor< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Definition tensor_tpl.h:379

◆ new_log2()

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

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

Definition at line 807 of file tensor_tpl.h.

807 {
808 return Tensor< GUM_SCALAR >(*this).log2();
809 }

◆ new_sgn()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::new_sgn ( ) const

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

fill the tensor with -1 for a GUM_SCALAR<0,1 if >0 and 0 if ==0

Definition at line 812 of file tensor_tpl.h.

812 {
813 return Tensor< GUM_SCALAR >(*this).sgn();
814 }
const Tensor< GUM_SCALAR > & sgn() const
apply sgn(x)$ on every element of the container
Definition tensor_tpl.h:343

◆ new_sq()

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

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

Definition at line 802 of file tensor_tpl.h.

802 {
803 return Tensor< GUM_SCALAR >(*this).sq();
804 }
const Tensor< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
Definition tensor_tpl.h:331

◆ newFactory()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > * gum::Tensor< GUM_SCALAR >::newFactory ( ) const
finalvirtual

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimDecorator< GUM_SCALAR >.

Definition at line 150 of file tensor_tpl.h.

150 {
151 return new Tensor< GUM_SCALAR >(
152 static_cast< MultiDimImplementation< GUM_SCALAR >* >(this->content()->newFactory()));
153 }
Tensor< GUM_SCALAR > * newFactory() const final
Default implementation of MultiDimContainer::set().
Definition tensor_tpl.h:150

References gum::MultiDimDecorator< GUM_SCALAR >::content(), and newFactory().

Referenced by Tensor(), and newFactory().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ noising()

template<typename GUM_SCALAR>
const Tensor< GUM_SCALAR > & gum::Tensor< 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 788 of file tensor_tpl.h.

788 {
789 if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
790 GUM_ERROR(InvalidArgument, "alpha must be in [0,1]")
791 }
793 return fillWith(scale(1 - alpha) + noise.randomCPT().scale(alpha)).normalizeAsCPT();
794 }
const Tensor< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
Definition tensor_tpl.h:437
const Tensor< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Tensor
Definition tensor_tpl.h:770

References fillWith(), GUM_ERROR, and scale().

Here is the call graph for this function:

◆ normalize()

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

normalisation of this do nothing if sum is 0

Definition at line 390 of file tensor_tpl.h.

390 {
391 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
392 if (this->empty_value_ != static_cast< GUM_SCALAR >(0))
393 this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
394 } else {
395 GUM_SCALAR s = sum();
396
397 if (s != (GUM_SCALAR)0) {
398 this->apply([s](GUM_SCALAR x) { return x / s; });
399 }
400 }
401 return *this;
402 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and sum().

Referenced by gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ normalizeAsCPT()

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

normalisation of this as a CPT for the variable varId

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

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

Definition at line 405 of file tensor_tpl.h.

405 {
406 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
407 if (this->empty_value_ != static_cast< GUM_SCALAR >(0)) {
408 this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
409 } else {
410 GUM_ERROR(FatalError, "Normalization for a tensor that sum to 0 in " << *this)
411 }
412 } else {
413 if (varId >= this->nbrDim()) {
414 GUM_ERROR(FatalError, varId << " is not a position for " << *this)
415 }
416 Instantiation inst(*this);
417 const auto& v = this->variable(varId);
418
419 for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
420 auto s = (GUM_SCALAR)0.0;
421 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
422 s += this->get(inst);
423 if (s == (GUM_SCALAR)0.0) {
424 GUM_ERROR(FatalError, "Normalization for a tensor that sum to 0 in " << *this)
425 }
426 if (s != (GUM_SCALAR)1.0) {
427 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
428 this->set(inst, this->get(inst) / s);
429 }
430 inst.setFirstVar(v); // to remove inst.end()
431 }
432 }
433 return *this;
434 }

References gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::Instantiation::end(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstVar(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

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 304 of file multiDimDecorator_tpl.h.

304 {
305 /*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
306 GUM_ERROR(OperationNotAllowed, "Not implemented yet")
307 }

References GUM_ERROR.

◆ operator!=()

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 171 of file multiDimContainer_tpl.h.

171 {
172 return !operator==(p);
173 }
bool operator==(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is equal to p.

References MultiDimContainer().

Here is the call graph for this function:

◆ operator&()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::operator& ( const Tensor< GUM_SCALAR > & p2) const

the function to be used to add two Tensors

Definition at line 862 of file tensor_tpl.h.

862 {
863 if (!isEvidence() || !p2.isEvidence())
864 GUM_ERROR(InvalidArgument, "The tensors are not evidences.")
865 if (this->variable(0) != p2.variable(0))
866 GUM_ERROR(InvalidArgument, "The evidence are not on the same variable.")
869 for (I.setFirst(); !I.end(); ++I) {
870 res.set(I, std::min(res.get(I), p2.get(I)));
871 }
872 return res;
873 }
bool isEvidence() const
is an evidence ? (marginal-like but has not to sum to 1)
Definition tensor_tpl.h:839

References gum::Instantiation::end(), GUM_ERROR, isEvidence(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ operator*() [1/2]

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

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

Definition at line 917 of file tensor_tpl.h.

917 {
918 return Tensor< GUM_SCALAR >(*this).scale(v);
919 }

◆ operator*() [2/2]

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

the function to be used to multiply two Tensors

Definition at line 908 of file tensor_tpl.h.

908 {
909 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).scale(p2.empty_value_);
910 if (this->empty()) return Tensor< GUM_SCALAR >(p2).scale(this->empty_value_);
911
912 return Tensor< GUM_SCALAR >(*this->content() * *p2.content());
913 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Here is the call graph for this function:

◆ operator*=() [1/2]

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

the function to be used to add two Tensors

Definition at line 958 of file tensor_tpl.h.

958 {
959 this->scale(v);
960 return *this;
961 }

References scale().

Here is the call graph for this function:

◆ operator*=() [2/2]

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

the function to be used to add two Tensors

Definition at line 952 of file tensor_tpl.h.

952 {
953 *this = *this * r;
954 return *this;
955 }

◆ operator+() [1/2]

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

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

Definition at line 833 of file tensor_tpl.h.

833 {
834 return Tensor< GUM_SCALAR >(*this).translate(v);
835 }
const Tensor< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
Definition tensor_tpl.h:443

◆ operator+() [2/2]

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

the function to be used to add two Tensors

Definition at line 824 of file tensor_tpl.h.

824 {
826 if (this->empty()) return Tensor< GUM_SCALAR >(p2).translate(this->empty_value_);
827
828 return Tensor< GUM_SCALAR >(*this->content() + *p2.content());
829 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Here is the call graph for this function:

◆ operator+=() [1/2]

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

the function to be used to add two Tensors

Definition at line 946 of file tensor_tpl.h.

946 {
947 this->translate(v);
948 return *this;
949 }

References translate().

Here is the call graph for this function:

◆ operator+=() [2/2]

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

the function to be used to add two Tensors

Definition at line 940 of file tensor_tpl.h.

940 {
941 *this = *this + r;
942 return *this;
943 }

◆ operator-() [1/2]

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

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

Definition at line 902 of file tensor_tpl.h.

902 {
903 return Tensor< GUM_SCALAR >(*this).translate(-v);
904 }

◆ operator-() [2/2]

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

the function to be used to subtract two Tensors

Definition at line 890 of file tensor_tpl.h.

890 {
891 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).translate(-p2.empty_value_);
892 if (this->empty()) {
893 auto p = Tensor< GUM_SCALAR >(p2);
894 p.apply([this](GUM_SCALAR x) { return this->empty_value_ - x; });
895 return p;
896 }
897 return Tensor< GUM_SCALAR >(*this->content() - *p2.content());
898 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Here is the call graph for this function:

◆ operator-=() [1/2]

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

the function to be used to add two Tensors

Definition at line 970 of file tensor_tpl.h.

970 {
971 this->translate(-v);
972 return *this;
973 }

References translate().

Here is the call graph for this function:

◆ operator-=() [2/2]

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

the function to be used to add two Tensors

Definition at line 964 of file tensor_tpl.h.

964 {
965 *this = *this - r;
966 return *this;
967 }

◆ operator/() [1/2]

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

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

Definition at line 935 of file tensor_tpl.h.

935 {
936 return Tensor< GUM_SCALAR >(*this).scale(1 / v);
937 }

◆ operator/() [2/2]

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

the function to be used to divide two Tensors

Definition at line 923 of file tensor_tpl.h.

923 {
924 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
925 if (this->empty()) {
926 auto p = Tensor< GUM_SCALAR >(p2);
927 p.apply([this](GUM_SCALAR x) { return this->empty_value_ / x; });
928 return p;
929 }
930 return Tensor< GUM_SCALAR >(*this->content() / *p2.content());
931 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Here is the call graph for this function:

◆ operator/=() [1/2]

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

the function to be used to add two Tensors

Definition at line 982 of file tensor_tpl.h.

982 {
983 this->scale(1 / v);
984 return *this;
985 }

References scale().

Here is the call graph for this function:

◆ operator/=() [2/2]

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

the function to be used to add two Tensors

Definition at line 976 of file tensor_tpl.h.

976 {
977 *this = *this / r;
978 return *this;
979 }

◆ operator<<()

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

the function to be used to add two Tensors

Definition at line 812 of file tensor_tpl.h.

817 {
818 this->add(v);
819 return *this;
820 }

◆ operator=() [1/2]

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

Default constructor.

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

Definition at line 124 of file tensor_tpl.h.

124 {
126 if (&src == this) return *this;
128 return *this;
129 }
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator

References Tensor(), and gum::MultiDimDecorator< GUM_SCALAR >::operator=().

Here is the call graph for this function:

◆ operator=() [2/2]

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

Default constructor.

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

Definition at line 133 of file tensor_tpl.h.

133 {
135 if (&src == this) return *this;
138 return *this;
139 }

References gum::MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(), Tensor(), and gum::MultiDimDecorator< GUM_SCALAR >::operator=().

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 146 of file multiDimContainer_tpl.h.

146 {
147 if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
148 if (nbrDim() == 0) return true;
149
150 for (auto iter = variablesSequence().beginSafe(); iter != variablesSequence().endSafe();
151 ++iter) {
152 if (!p.variablesSequence().exists(*iter)) { return false; }
153 }
154 } else {
155 return false;
156 }
157
158 Instantiation i(*this);
160 for (i.setFirst(); !i.end(); ++i) {
161 if (cmp(get(i), p.get(i))) { return false; }
162 }
163
164 return true;
165 }
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.

References MultiDimContainer(), gum::MultiDimInterface::domainSize(), gum::MultiDimInterface::nbrDim(), and gum::MultiDimInterface::variablesSequence().

Here is the call graph for this function:

◆ operator==() [2/2]

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

the function to be used to add two Tensors

Definition at line 988 of file tensor_tpl.h.

988 {
989 if (this->empty()) {
990 if (r.empty()) return this->empty_value_ == r.empty_value_;
991 else return false;
992 } else {
993 if (r.empty()) return false;
994 else return (*this->content_) == (*r.content_);
995 }
996 }

References gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Here is the call 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 101 of file multiDimContainer_tpl.h.

101 {
102 return get(i);
103 }

References get().

Here is the call graph for this function:

◆ operator|()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::operator| ( const Tensor< GUM_SCALAR > & p2) const

the function to be used to add two Tensors

Definition at line 847 of file tensor_tpl.h.

847 {
848 if (!isEvidence() || !p2.isEvidence())
849 GUM_ERROR(InvalidArgument, "The tensors are not evidences.")
850 if (this->variable(0) != p2.variable(0))
851 GUM_ERROR(InvalidArgument, "The evidence are not on the same variable.")
854 for (I.setFirst(); !I.end(); ++I) {
855 res.set(I, std::max(res.get(I), p2.get(I)));
856 }
857 return res;
858 }

References gum::Instantiation::end(), GUM_ERROR, isEvidence(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ operator~()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::operator~ ( ) const

the function to be used to add two Tensors

Definition at line 877 of file tensor_tpl.h.

877 {
878 if (!isEvidence()) GUM_ERROR(InvalidArgument, "The tensor is not an evidence.")
879
882 for (I.setFirst(); !I.end(); ++I) {
883 res.set(I, 1 - res.get(I));
884 }
885 return res;
886 }

References gum::Instantiation::end(), GUM_ERROR, isEvidence(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ populate() [1/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]);
}
Class for assigning/browsing values to tuples of discrete variables.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Parameters
lcontains the data.
Exceptions
SizeErrorRaised if l size's does not matches this MultiDimContainer domain size.

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

Definition at line 191 of file multiDimContainer_tpl.h.

191 {
192 if (domainSize() != l.size()) {
193 GUM_ERROR(SizeError, "Sizes do not match : " << domainSize() << "!=" << l.size())
194 }
195
196 Instantiation i(*this);
197 // insert all the elements
198 for (const auto& elt: l) {
199 set(i, elt);
200 ++i;
201 }
202 }

References gum::MultiDimInterface::domainSize(), GUM_ERROR, and set().

Here is the call graph for this function:

◆ populate() [2/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 325 of file multiDimDecorator_tpl.h.

325 {
326 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
327 if (v.size() == 1) {
328 empty_value_ = v[0];
329 } else {
330 GUM_ERROR(SizeError, "Size do not match in populate")
331 }
332 } else {
333 content_->populate(v);
334 }
335 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, empty(), empty_value_, and GUM_ERROR.

Referenced by gum::Tensor< GUM_SCALAR >::fillWith(), gum::SimpleUTGenerator::generateUT(), and gum::SimpleUTGenerator::generateUT().

Here is the call graph for this function:
Here is the caller 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 272 of file multiDimDecorator_tpl.h.

272 {
273 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->pos(var);
274 }
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and pos().

Referenced by pos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ prodIn()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::prodIn ( const gum::VariableSet & kept_vars) const

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

Parameters
kept_varsis the set of vars to keep

Definition at line 558 of file tensor_tpl.h.

558 {
559 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
561 }
562
563 // if kept_var is empty, create an empty tensor
564 // TODO: remove this test when operations will be able to handle empty tensors
565 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->product()); }
566
568 }
GUM_SCALAR product() const
product of all elements in the Tensor
Definition tensor_tpl.h:166

References complementVars_(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::Set< Key >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, product(), and gum::projectProduct().

Here is the call graph for this function:

◆ prodOut()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::prodOut ( const gum::VariableSet & del_vars) const

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

Parameters
del_varsis the set of vars to eliminate

Definition at line 478 of file tensor_tpl.h.

478 {
479 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
481 }
482
483 // if we remove all the variables, create an empty tensor
484 // TODO: remove this test when operations will be able to handle empty tensors
485 if (this->variablesSequence().size() <= del_vars.size()) {
486 bool equal = true;
487 for (const auto var: this->variablesSequence()) {
488 if (!del_vars.exists(var)) {
489 equal = false;
490 break;
491 }
492 }
493 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->product()); }
494 }
495
497 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::Set< Key >::exists(), product(), gum::projectProduct(), gum::Set< Key >::size(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Here is the call graph for this function:

◆ product()

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

product of all elements in the Tensor

Definition at line 166 of file tensor_tpl.h.

166 {
167 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
168 return this->empty_value_;
169 }
170 return gum::projectProduct(*this->content());
171 }
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::projectProduct().

Referenced by prodIn(), and prodOut().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ putFirst() [1/2]

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

create a new Tensor with a certain variable in first

Exceptions
InvalidArgumentif the var is not in the tensor

Definition at line 658 of file tensor_tpl.h.

658 {
659 if (!this->contains(*var)) {
660 GUM_ERROR(InvalidArgument, "The variable to put first does not belong to the tensor")
661 }
662 if (&(this->variable(0)) == var) return Tensor< GUM_SCALAR >(*this);
663
665 vars.push_back(var);
666 for (Idx i = 0; i < this->nbrDim(); i++)
667 if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
668
669 return this->reorganize(vars);
670 }
Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Tensor with another order
Definition tensor_tpl.h:618

References gum::MultiDimDecorator< GUM_SCALAR >::contains(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), reorganize(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Referenced by putFirst().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ putFirst() [2/2]

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

create a new Tensor with a certain variable in first

Exceptions
InvalidArgumentif the var is not in the tensor

Definition at line 673 of file tensor_tpl.h.

673 {
674 const DiscreteVariable* var = nullptr;
675
676 for (gum::Idx i = 0; i < this->nbrDim(); i++)
677 if (this->variable(i).name() == varname) {
678 var = &(this->variable(i));
679 break;
680 }
681 if (var == nullptr)
683 "The variable '" << varname << "' to put first does not belong to the tensor")
685 }
Tensor< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Tensor with a certain variable in first
Definition tensor_tpl.h:658

References GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), putFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ random()

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

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

Definition at line 745 of file tensor_tpl.h.

745 {
746 if (this->domainSize() == 0) return *this;
747
749 v.reserve(this->domainSize());
750 for (Size i = 0; i < this->domainSize(); ++i) {
751 auto r = (GUM_SCALAR)randomProba();
752 v.push_back(r);
753 }
754 this->fillWith(v);
755 return *this;
756 }

References gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), fillWith(), and gum::randomProba().

Here is the call graph for this function:

◆ randomCPT()

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

generate a random CPT in the Tensor

Definition at line 770 of file tensor_tpl.h.

770 {
771 if (this->domainSize() == 0) {
772 this->fillWith((GUM_SCALAR)1.0);
773 } else {
774 gum::Instantiation I(*this);
775 const auto& v = this->variable(0);
776 for (I.setFirstNotVar(v); !I.end(); I.incNotVar(v)) {
778 for (I.setFirstVar(v); !I.end(); I.incVar(v)) {
779 this->set(I, distrib[I.val(0)]);
780 }
781 I.unsetEnd();
782 }
783 }
784 return *this;
785 }
std::vector< GUM_SCALAR > randomDistribution(Size n)
Return a random discrete distribution.

References gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), gum::Instantiation::end(), fillWith(), gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::randomDistribution(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirstNotVar(), gum::Instantiation::setFirstVar(), gum::Instantiation::unsetEnd(), gum::Instantiation::val(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ randomDistribution()

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

generate a random Distribution in the Tensor

Definition at line 759 of file tensor_tpl.h.

759 {
760 if (this->domainSize() == 0) {
761 this->fillWith((GUM_SCALAR)1.0);
762 } else {
764 }
765
766 return *this;
767 }

References gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), fillWith(), and gum::randomDistribution().

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 348 of file multiDimDecorator_tpl.h.

349 {
350 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
351 return base;
352 } else {
353 return content_->reduce(f, base);
354 }
355 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and empty().

Referenced by gum::Tensor< GUM_SCALAR >::maxNonOne(), and gum::Tensor< GUM_SCALAR >::minNonZero().

Here is the call graph for this function:
Here is the caller 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 246 of file multiDimDecorator_tpl.h.

246 {
248 }
virtual bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and registerSlave().

Referenced by registerSlave().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reorganize() [1/2]

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

create a new Tensor with another order

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

Definition at line 618 of file tensor_tpl.h.

618 {
619 if (vars.size() != this->nbrDim())
621 "The argument contains " << vars.size() << " variables instead of "
622 << this->nbrDim() << ".")
623 for (const auto var: vars) {
624 if (!this->contains(*var))
625 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
626 }
627
630 for (const auto var: vars)
631 p.add(*var);
633 p.copyFrom(*this, nullptr); // copy *this in p using the same order
634
635 return p;
636 }

References gum::MultiDimDecorator< GUM_SCALAR >::contains(), and GUM_ERROR.

Referenced by putFirst(), and reorganize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reorganize() [2/2]

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

create a new Tensor with another order

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

Definition at line 640 of file tensor_tpl.h.

640 {
642
644 for (gum::Idx i = 0; i < this->nbrDim(); i++)
645 namesToVars.insert(this->variable(i).name(), &(this->variable(i)));
646
647 for (const auto& name: vars) {
648 if (!namesToVars.exists(name)) {
650 "'" << name << "' is a not a name of a variable in this tensor")
651 }
652 res.push_back(namesToVars[name]);
653 }
654 return reorganize(res);
655 }

References gum::HashTable< Key, Val >::exists(), GUM_ERROR, gum::HashTable< Key, Val >::insert(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), and reorganize().

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 56 of file multiDimInterface_inl.h.

56 {
57 if (!contains(x)) { GUM_ERROR(NotFound, "could not find the variable") }
58
59 if (contains(y)) { GUM_ERROR(DuplicateElement, "variable " << y << " already in MultiDim") }
60
61 if (x.domainSize() != y.domainSize()) {
62 GUM_ERROR(OperationNotAllowed, "incompatible variables")
63 }
64
65 replace_(&x, &y);
66 }
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.

References contains(), gum::DiscreteVariable::domainSize(), GUM_ERROR, and replace_().

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 405 of file multiDimDecorator_tpl.h.

406 {
407 this->content()->replace(*x, *y);
408 }

References content().

Referenced by newFactory().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scale()

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

multiply (each value of) *this by v

Definition at line 437 of file tensor_tpl.h.

437 {
438 this->apply([v](GUM_SCALAR x) { return x * v; });
439 return *this;
440 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Referenced by noising(), operator*=(), and operator/=().

Here is the call graph for this function:
Here is the caller 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 169 of file multiDimDecorator_tpl.h.

170 {
171 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim() == 0) {
173 } else {
175 }
176 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, empty_value_, nbrDim(), and set().

Referenced by gum::Tensor< GUM_SCALAR >::evEq(), gum::Tensor< GUM_SCALAR >::evGt(), gum::Tensor< GUM_SCALAR >::evIn(), gum::Tensor< GUM_SCALAR >::evLt(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::Tensor< GUM_SCALAR >::normalizeAsCPT(), gum::Tensor< GUM_SCALAR >::randomCPT(), and set().

Here is the call graph for this function:
Here is the caller 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 211 of file multiDimDecorator_tpl.h.

211 {
213 }
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and setChangeNotification().

Referenced by setChangeNotification().

Here is the call graph for this function:
Here is the caller 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 239 of file multiDimDecorator_tpl.h.

239 {
241 }
virtual void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and setDecNotification().

Referenced by setDecNotification().

Here is the call graph for this function:
Here is the caller 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 218 of file multiDimDecorator_tpl.h.

218 {
220 }
virtual void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and setFirstNotification().

Referenced by setFirstNotification().

Here is the call graph for this function:
Here is the caller 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.

232 {
234 }
virtual void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and setIncNotification().

Referenced by setIncNotification().

Here is the call graph for this function:
Here is the caller 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 225 of file multiDimDecorator_tpl.h.

225 {
227 }
virtual void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and setLastNotification().

Referenced by setLastNotification().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sgn()

template<typename GUM_SCALAR>
INLINE const Tensor< GUM_SCALAR > & gum::Tensor< GUM_SCALAR >::sgn ( ) const

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

fill the tensor with -1 for a GUM_SCALAR<0,1 if >0 and 0 if ==0

Definition at line 343 of file tensor_tpl.h.

343 {
344 this->apply([](GUM_SCALAR x) { return (GUM_SCALAR(0) < x) - (x < GUM_SCALAR(0)); });
345 return *this;
346 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Here is the call graph for this function:

◆ sq()

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

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

Definition at line 331 of file tensor_tpl.h.

331 {
332 this->apply([](GUM_SCALAR x) { return x * x; });
333 return *this;
334 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Here is the call graph for this function:

◆ sum()

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

sum of all elements in the Tensor

Definition at line 157 of file tensor_tpl.h.

157 {
158 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
159 return this->empty_value_;
160 }
161 return gum::projectSum(*this->content());
162 }
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, and gum::projectSum().

Referenced by gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_(), isEvidence(), normalize(), sumIn(), and sumOut().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sumIn()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::sumIn ( const gum::VariableSet & kept_vars) const

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

Parameters
kept_varsis the set of vars to keep

Definition at line 544 of file tensor_tpl.h.

544 {
545 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
547 }
548
549 // if kept_var is empty, create an empty tensor
550 // TODO: remove this test when operations will be able to handle empty tensors
551 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->sum()); }
552
554 }

References complementVars_(), gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::Set< Key >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::projectSum(), and sum().

Here is the call graph for this function:

◆ sumOut()

template<typename GUM_SCALAR>
INLINE Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::sumOut ( const gum::VariableSet & del_vars) const

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

Parameters
del_varsis the set of vars to eliminate

Definition at line 455 of file tensor_tpl.h.

455 {
456 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
458 }
459
460 // if we remove all the variables, create an empty tensor
461 // TODO: remove this test when operations will be able to handle empty tensors
462 if (this->variablesSequence().size() <= del_vars.size()) {
463 bool equal = true;
464 for (const auto var: this->variablesSequence()) {
465 if (!del_vars.exists(var)) {
466 equal = false;
467 break;
468 }
469 }
470 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->sum()); }
471 }
472
474 }

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::MultiDimDecorator< GUM_SCALAR >::content_, gum::MultiDimDecorator< GUM_SCALAR >::empty(), gum::MultiDimDecorator< GUM_SCALAR >::empty_value_, gum::Set< Key >::exists(), gum::projectSum(), gum::Set< Key >::size(), sum(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

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

protected method to swap the implementation behind the Tensor

Warning
unsafe method for slave Instantiations !

Definition at line 386 of file multiDimDecorator_tpl.h.

387 {
388 if (aContent != nullptr) {
389 // TODO : frees all slave instantiations
390 // TODO : control the dimensions ?
393 // registers all instantiations
394 delete (tmp);
395 }
396 }

References content_.

Referenced by newFactory().

Here is the caller 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 400 of file multiDimDecorator_tpl.h.

400 {
401 return content_->toString(i);
402 }

References content_.

Referenced by newFactory().

Here is the caller graph for this function:

◆ toString() [2/2]

template<typename GUM_SCALAR>
std::string gum::Tensor< GUM_SCALAR >::toString ( ) const
nodiscardfinalvirtual

the function to be used to add two Tensors

Reimplemented from gum::MultiDimDecorator< GUM_SCALAR >.

Definition at line 999 of file tensor_tpl.h.

999 {
1000 auto table = this->content();
1002
1003 if (table->nbrDim() == 0) {
1004 Instantiation I(this);
1005 ss << "[" << this->get(I) << "]";
1006 return ss.str();
1007 }
1008 const Size colwidth = 6;
1009 const Size numberwidth = 9;
1010 const Size nbrLigMax = 6;
1011
1013 ss.precision(numberwidth - 5);
1014
1015 const auto& var = table->variable(0);
1016
1017 const Size nbparents = table->nbrDim() - 1;
1018 const Size nbcol = var.domainSize();
1019 const std::string maskparent(colwidth, '-');
1021
1022 if (nbparents > 0)
1023 ss << std::setw(nbparents * (colwidth + 1) - 1) << " "
1024 << "||";
1025 ss << " " << std::setw(nbcol * (numberwidth + 1) - 3)
1026 << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << "|";
1027 ss << std::endl;
1028
1029 if (nbparents > 0) {
1030 for (Idx i = 1; i <= nbparents; i++)
1031 ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth) << "|";
1032 ss << "|";
1033 }
1034 for (Idx i = 0; i < nbcol; i++)
1035 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << "|";
1036 ss << std::endl;
1037
1038
1039 if (nbparents > 0) {
1040 for (Idx i = 1; i <= nbparents; i++)
1041 ss << maskparent << "|";
1042 ss << "|";
1043 }
1044 for (Idx i = 0; i < nbcol; i++)
1045 ss << masknumber << "|";
1046 ss << std::endl;
1048
1049 const auto drawligne = [&]() {
1050 if (nbparents > 0) {
1051 for (Idx i = 1; i <= nbparents; i++)
1052 ss << std::setw(colwidth) << table->variable(i).label(I.val(i)).substr(0, colwidth)
1053 << "|";
1054 ss << "|";
1055 }
1056 for (I.setFirstVar(var); !I.end(); I.incVar(var))
1057 ss << " " << std::setw(numberwidth - 1) << table->get(I) << "|";
1058 I.setFirstVar(var);
1059 ss << std::endl;
1060 };
1061
1062 if (const Size nbrLig = table->domainSize() / var.domainSize(); nbrLig < nbrLigMax * 2 + 1) {
1063 for (I.setFirst(); !I.end(); I.incNotVar(var))
1064 drawligne();
1065 } else {
1066 Size cpt = 0;
1067 for (I.setFirst(); !I.end(); I.incNotVar(var)) {
1068 cpt++;
1069 if (cpt > nbrLigMax) break;
1070 drawligne();
1071 }
1072 ss << "[..." << nbrLig - nbrLigMax * 2 << " more line(s) ...]" << std::endl;
1073 I.setLast();
1074 for (Idx revi = 1; revi < nbrLigMax; revi++)
1075 I.decNotVar(var);
1076 for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
1077 drawligne();
1078 }
1079 }
1080
1081 return ss.str();
1082 }
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.

References gum::MultiDimDecorator< GUM_SCALAR >::content(), gum::Instantiation::decNotVar(), gum::Instantiation::end(), gum::MultiDimDecorator< GUM_SCALAR >::get(), gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstVar(), gum::Instantiation::setLast(), and gum::Instantiation::val().

Here is the call graph for this function:

◆ translate()

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

add v to (each value of) *this

Definition at line 443 of file tensor_tpl.h.

443 {
444 this->apply([v](GUM_SCALAR x) { return x + v; });
445 return *this;
446 }

References gum::MultiDimDecorator< GUM_SCALAR >::apply().

Referenced by operator+=(), and operator-=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uniformTensor()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::uniformTensor ( const DiscreteVariable & var)
static

Definition at line 1178 of file tensor_tpl.h.

1178 {
1180
1182 pot << var;
1184
1185 pot.normalize();
1186
1187 return pot;
1188 }
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:390

◆ 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 288 of file multiDimDecorator_tpl.h.

288 {
290 }
virtual bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and unregisterSlave().

Referenced by unregisterSlave().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ variable() [1/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. @warging This function is not O(1)
Exceptions
NotFoundRaised if the element cannot be found.

Implements gum::MultiDimInterface.

Definition at line 267 of file multiDimDecorator_tpl.h.

267 {
268 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(name);
269 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and variable().

Here is the call graph for this function:

◆ variable() [2/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 261 of file multiDimDecorator_tpl.h.

261 {
262 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(i);
263 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and variable().

Referenced by gum::Tensor< GUM_SCALAR >::draw(), erase(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::Tensor< GUM_SCALAR >::normalizeAsCPT(), gum::Tensor< GUM_SCALAR >::operator&(), gum::Tensor< GUM_SCALAR >::operator|(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::randomCPT(), variable(), and variable().

Here is the call graph for this function:
Here is the caller 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 313 of file multiDimDecorator_tpl.h.

313 {
315 }

References gum::MultiDimContainer< GUM_SCALAR >::MultiDimContainer(), content_, and variablesSequence().

Referenced by gum::Tensor< GUM_SCALAR >::complementVars_(), gum::learning::DAG2BNLearner::createBNwithEM(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::Tensor< GUM_SCALAR >::KL(), gum::Tensor< GUM_SCALAR >::maxOut(), gum::Tensor< GUM_SCALAR >::minOut(), gum::Tensor< GUM_SCALAR >::prodOut(), gum::Tensor< GUM_SCALAR >::sumOut(), and variablesSequence().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ content_

◆ empty_value_


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