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

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
GUM_SCALAR mean () const
 compute the mean of a numerical discrete random variable @raise
GUM_SCALAR variance () const
 compute the variance of a numerical discrete random variable @raise
GUM_SCALAR stdDev () const
 compute the stdDev of a numerical discrete random variable
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 (std::string_view 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
Idx nbrDim () const final
 Returns the number of vars in the multidimensional container.
Size domainSize () const final
 Returns the product of the variables domain size.
void add (const DiscreteVariable &v) final
 Adds a new var to the variables of the multidimensional matrix.
void erase (const DiscreteVariable &var) final
 Removes a var from the variables of the multidimensional matrix.
const Sequence< const DiscreteVariable * > & variablesSequence () const final
 Returns a const ref to the sequence of DiscreteVariable*.
const DiscreteVariablevariable (Idx) const final
 Returns a const ref to the ith var.
Idx pos (const DiscreteVariable &var) const final
 Returns the index of a variable.
bool contains (const DiscreteVariable &var) const final
 Returns true if var is in *this.
bool empty () const final
 Returns true if no var is in *this.
Accessors / Modifiers
virtual const DiscreteVariablevariable (std::string_view name) const =0
 Returns the variable with the name.
virtual const DiscreteVariablevariable (std::string_view name) const=0
 Returns the variable with the name.
void replace (const DiscreteVariable &x, const DiscreteVariable &y)
 Replace variables in this multidim.
MultiDimAddressable implementation
bool unregisterSlave (Instantiation &i) final
 Unregister i as a slave of this MultiDimAdressable.
bool registerSlave (Instantiation &i) final
 Register i as a slave of this MultiDimAdressable.
void changeNotification (const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
 Listen to changes in a given Instantiation.
void setChangeNotification (const Instantiation &i) final
 Listen to an assignment of a value in a Instantiation.
void setFirstNotification (const Instantiation &i) final
 Listen to setFirst in a given Instantiation.
void setLastNotification (const Instantiation &i) final
 Listen to setLast in a given Instantiation.
void setIncNotification (const Instantiation &i) final
 Listen to increment in a given Instantiation.
void setDecNotification (const Instantiation &i) final
 Listen to increment in each recorded Instantiation.
virtual void notifyChange () const final
MultiDimContainer implementation
void set (const Instantiation &i, const GUM_SCALAR &value) const final
 Default implementation of MultiDimContainer::set().
GUM_SCALAR get (const Instantiation &i) const final
 Default implementation of MultiDimContainer::get().
void fill (const GUM_SCALAR &d) const final
 Default implementation of MultiDimContainer::set().
void populate (const std::vector< GUM_SCALAR > &v) const final
 Automatically fills this MultiDimContainer with the values in v.
void apply (std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
 Apply a function on every element of the container.
GUM_SCALAR reduce (std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
 compute lfold for this container
void beginMultipleChanges () final
 Default implementation of MultiDimContainer::set().
void endMultipleChanges () final
 Default implementation of MultiDimContainer::set().
std::string toString (const Instantiation *i) const override
 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.
const MultiDimImplementation< GUM_SCALAR > * content () const 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.
MultiDimAdressablegetMasterRef () override
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer.
const MultiDimAdressablegetMasterRef () const override
 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.

Static Public Member Functions

static Tensor< GUM_SCALAR > deterministicTensor (const DiscreteVariable &var, Idx value)
static Tensor< GUM_SCALAR > deterministicTensor (const DiscreteVariable &var, std::string_view 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

void replace_ (const DiscreteVariable *x, const DiscreteVariable *y) override
 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.

Private Member Functions

gum::VariableSet _complementVars_ (const gum::VariableSet &del_vars) const

Detailed Description

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
gum::Tensor< GUM_SCALAR >::Tensor ( )

Default constructor.

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

Definition at line 62 of file tensor_tpl.h.

62 :
65 }
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Tensor()
Default constructor.
Definition tensor_tpl.h:62

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

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

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

◆ Tensor() [2/6]

template<GUM_Numeric GUM_SCALAR>
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 70 of file tensor_tpl.h.

70 : Tensor() {
71 for (const auto& var: vars) {
72 this->add(*var);
73 }
74 }
virtual void add(const DiscreteVariable &v)=0
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<GUM_Numeric GUM_SCALAR>
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<GUM_Numeric 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
const MultiDimImplementation< GUM_ELEMENT > * content() const final
Returns the implementation for this object (may be *this).
bool empty() const final
Returns true if no var is in *this.
const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.

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

Here is the call graph for this function:

◆ Tensor() [5/6]

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

Copy constructor & assignment.

Definition at line 86 of file tensor_tpl.h.

86 :
89 *(src.content())) {
91 // GUM_CONS_CPY not here because in called Tensor
92 // GUM_CONS_CPY( Tensor );
93 }
MultiDimContainer< GUM_ELEMENT > * newFactory() const override=0
Creates an empty clone of this MultiDimContainer.

◆ Tensor() [6/6]

template<GUM_Numeric GUM_SCALAR>
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<GUM_Numeric GUM_SCALAR>
gum::Tensor< GUM_SCALAR >::~Tensor ( )
final

Destructor.

Definition at line 144 of file tensor_tpl.h.

144 {
145 // for debugging purposes
147 }

Member Function Documentation

◆ _complementVars_()

template<GUM_Numeric GUM_SCALAR>
gum::VariableSet gum::Tensor< GUM_SCALAR >::_complementVars_ ( const gum::VariableSet & del_vars) const
nodiscardprivate

Definition at line 647 of file tensor_tpl.h.

647 {
649
650 for (const auto x: this->variablesSequence())
651 if (!vars.contains(x)) cplt.insert(x);
652
653 return cplt;
654 }
bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.

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:

◆ abs()

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

Apply abs on every element of the container.

Definition at line 421 of file tensor_tpl.h.

421 {
422 this->apply([](GUM_SCALAR x) {
423 if (x >= 0) return x;
424 else return -x;
425 });
426 return *this;
427 }
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const

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

Referenced by abs().

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

◆ add()

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

190 {
191 if (v.domainSize() < 1) {
192 GUM_ERROR(InvalidArgument, "Empty variable " << v << " cannot be added in a Tensor")
193 }
195 }
Decorator design pattern in order to separate implementations from multidimensional matrix concepts.
void add(const DiscreteVariable &v) final
Size domainSize() const final
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

Referenced by gum::Tensor< GUM_SCALAR >::Tensor(), gum::Tensor< GUM_SCALAR >::Tensor(), and gum::Counterfactual< GUM_ELEMENT >::_adaptToOriginalVariables_().

Here is the caller graph for this function:

◆ apply()

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

339 {
340 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
342 } else {
343 content_->apply(f);
344 }
345 }
void apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const final
Apply a function on every element of the container.

Referenced by gum::Tensor< GUM_SCALAR >::abs(), gum::Tensor< GUM_SCALAR >::log2(), and gum::Tensor< GUM_SCALAR >::sgn().

Here is the caller graph for this function:

◆ argmax()

template<GUM_Numeric GUM_SCALAR>
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 772 of file tensor_tpl.h.

772 {
773 auto m = max();
774 return std::pair(findAll(m), m);
775 }
GUM_SCALAR max() const
max of all elements in the Tensor
Definition tensor_tpl.h:175
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Tensor
Definition tensor_tpl.h:758

References findAll(), and max().

Here is the call graph for this function:

◆ argmin()

template<GUM_Numeric GUM_SCALAR>
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 779 of file tensor_tpl.h.

779 {
780 auto m = min();
781 return std::pair(findAll(m), m);
782 }
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()

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

Referenced by gum::Tensor< GUM_SCALAR >::Tensor().

Here is the caller graph for this function:

◆ changeNotification()

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

203 {
205 var,
206 oldval,
207 newval);
208 }
void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final

◆ contains()

bool gum::MultiDimDecorator< GUM_SCALAR >::contains ( const DiscreteVariable & var) 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 141 of file multiDimDecorator_tpl.h.

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

◆ content()

◆ copy()

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.

Definition at line 314 of file multiDimContainer_tpl.h.

284 {
285 this->beginMultipleChanges();
286
287 Size nbr = this->nbrDim();
288
289 for (Idx i = 0; i < nbr; i++) {
290 this->erase(this->variable(0));
291 }
292
293 for (Idx i = 0; i < src.nbrDim(); i++) {
294 this->add(src.variable(i));
295 }
296
297 this->endMultipleChanges();
298 this->copyFrom(src);
299 }
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
void erase(const DiscreteVariable &var) final
const DiscreteVariable & variable(Idx) const final

◆ copyFrom() [1/2]

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.

Definition at line 250 of file multiDimContainer_tpl.h.

267 {
268 if (src.domainSize() != domainSize()) {
270 "Domain sizes do not fit : " << src.domainSize() << "!=" << domainSize());
271 }
272
273 Instantiation i_dest(*this);
275
276 for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
277 set(i_dest, src[i_src]);
278 }
279 }

◆ copyFrom() [2/2]

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.

Definition at line 266 of file multiDimContainer_tpl.h.

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

◆ deterministicTensor() [1/2]

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

Definition at line 1206 of file tensor_tpl.h.

1207 {
1209
1211 pot << var;
1213
1215 I.chgVal(var, value);
1216 pot.set(I, GUM_SCALAR(1.0));
1217
1218 return pot;
1219 }
void set(const Instantiation &i, const GUM_ELEMENT &value) const final
Default implementation of MultiDimContainer::set().

References gum::Instantiation::chgVal().

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

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

◆ deterministicTensor() [2/2]

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

Definition at line 1222 of file tensor_tpl.h.

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

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

Here is the call graph for this function:

◆ domainSize()

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

183 {
184 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->domainSize();
185 }

Referenced by gum::Tensor< GUM_SCALAR >::random(), gum::Tensor< GUM_SCALAR >::randomCPT(), and gum::Tensor< GUM_SCALAR >::randomDistribution().

Here is the caller graph for this function:

◆ draw()

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

get a value at random from a 1-D distribution

Definition at line 736 of file tensor_tpl.h.

736 {
737 if (this->nbrDim() != 1) {
738 GUM_ERROR(FatalError, "To draw from a tensor, the dimension must be 1")
739 }
740
741 auto r = static_cast< GUM_SCALAR >(randomProba());
742 Instantiation Ip(*this);
743 for (Ip.setFirst(); !Ip.end(); Ip.inc()) {
744 r -= this->get(Ip);
745 if (r <= 0) return Ip.val(0);
746 }
747 return this->variable(0).domainSize() - 1;
748 }
virtual GUM_SCALAR get(const Instantiation &i) const
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.

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()

◆ endMultipleChanges()

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

376 {
378 }

Referenced by gum::Tensor< GUM_SCALAR >::Tensor().

Here is the caller graph for this function:

◆ entropy()

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::Tensor< GUM_SCALAR >::entropy ( ) const

entropy of the Tensor

Definition at line 283 of file tensor_tpl.h.

283 {
284 return -this->expectedValue([this](const gum::Instantiation& i) -> GUM_SCALAR {
285 return GUM_SCALAR(GUM_LOG2_OR_0(this->get(i)));
286 });
287 }
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().

Here is the call graph for this function:

◆ erase()

void gum::MultiDimDecorator< GUM_SCALAR >::erase ( const DiscreteVariable & var)
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 132 of file multiDimDecorator_tpl.h.

253 {
255 }

References MultiDimDecorator().

Here is the call graph for this function:

◆ evEq()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evEq ( 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
1139 p.add(v);
1140 p.fillWith(0);
1141 Instantiation I(p);
1142 I.chgVal(0, i);
1143 p.set(I, 1);
1144 return p;
1145 }
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:304

References gum::MultiDimDecorator< GUM_ELEMENT >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), fillWith(), and gum::MultiDimDecorator< GUM_ELEMENT >::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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evGt ( const DiscreteVariable & v,
double val )
static

numerical evidence generator

Definition at line 1169 of file tensor_tpl.h.

1169 {
1170 const auto i = v.closestIndex(val);
1171 if (i == v.domainSize() - 1) { return evEq(v, val); }
1172
1174 p.add(v);
1175 p.fillWith(0);
1176 Instantiation I(p);
1177 for (Idx i2 = i + 1; i2 < v.domainSize(); i2++) {
1178 I.chgVal(0, i2);
1179 p.set(I, 1);
1180 }
1181 return p;
1182 }
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator

References gum::MultiDimDecorator< GUM_ELEMENT >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), gum::DiscreteVariable::domainSize(), evEq(), fillWith(), and gum::MultiDimDecorator< GUM_ELEMENT >::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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evIn ( const DiscreteVariable & v,
double val1,
double val2 )
static

numerical evidence generator

Definition at line 1149 of file tensor_tpl.h.

1149 {
1150 if (val2 < val1) {
1152 "val2 (" << val2 << ") must be greater than val1 (" << val1 << ").")
1153 }
1154 const auto i1 = v.closestIndex(val1);
1155 const auto i2 = v.closestIndex(val2);
1156
1158 p.add(v);
1159 p.fillWith(0);
1160 Instantiation I(p);
1161 for (Idx i = i1; i <= i2; i++) {
1162 I.chgVal(0, i);
1163 p.set(I, 1);
1164 }
1165 return p;
1166 }

References gum::MultiDimDecorator< GUM_ELEMENT >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), fillWith(), GUM_ERROR, and gum::MultiDimDecorator< GUM_ELEMENT >::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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::evLt ( const DiscreteVariable & v,
double val )
static

numerical evidence generator

Definition at line 1185 of file tensor_tpl.h.

1185 {
1186 const auto i = v.closestIndex(val);
1187 if (i == 0) { return evEq(v, val); }
1188
1190 p.add(v);
1191 p.fillWith(0);
1192 Instantiation I(p);
1193 for (Idx i2 = 0; i2 < i; i2++) {
1194 I.chgVal(0, i2);
1195 p.set(I, 1);
1196 }
1197 return p;
1198 }

References gum::MultiDimDecorator< GUM_ELEMENT >::add(), gum::Instantiation::chgVal(), gum::DiscreteVariable::closestIndex(), evEq(), fillWith(), and gum::MultiDimDecorator< GUM_ELEMENT >::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<GUM_Numeric 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 }
MultiDimImplementation< GUM_SCALAR > * content_
virtual bool empty() const =0
Returns true if no var is in *this.

Referenced by entropy().

Here is the caller graph for this function:

◆ extract()

template<GUM_Numeric 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 728 of file tensor_tpl.h.

728 {
730 p.extractFrom(*this, inst);
731
732 return p;
733 }
virtual void extractFrom(const MultiDimContainer< GUM_ELEMENT > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.

◆ extractFrom()

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

243 {
244 this->beginMultipleChanges();
245
246 Size nbr = this->nbrDim();
247 for (Idx i = 0; i < nbr; i++) {
248 this->erase(this->variable(0));
249 }
250
251 for (Idx i = 0; i < src.nbrDim(); i++) {
252 if (!imask.contains(src.variable(i))) this->add(src.variable(i));
253 }
254
255 this->endMultipleChanges();
256
257 if (this->nbrDim() == 0) { GUM_ERROR(FatalError, "Empty tensor") }
258
260 inst.setVals(imask);
261 for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
262 set(inst, src[inst]);
263 }

◆ fill()

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

294 {
295 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
296 empty_value_ = d;
297 } else {
299 }
300 }
void fill(const GUM_SCALAR &d) const final

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

Here is the caller graph for this function:

◆ fillWith() [1/4]

template<GUM_Numeric GUM_SCALAR>
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 297 of file tensor_tpl.h.

297 {
298 this->fill(val);
299 return *this;
300 }
virtual void fill(const GUM_SCALAR &d) const=0

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

Here is the call graph for this function:

◆ fillWith() [2/4]

template<GUM_Numeric GUM_SCALAR>
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 291 of file tensor_tpl.h.

291 {
292 this->populate(data);
293 return *this;
294 }
virtual void populate(const std::vector< GUM_SCALAR > &v) const

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

Here is the call graph for this function:

◆ fillWith() [3/4]

template<GUM_Numeric GUM_SCALAR>
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 304 of file tensor_tpl.h.

304 {
305 if (src.domainSize() != this->domainSize()) {
306 GUM_ERROR(InvalidArgument, "Tensor to copy has not the same domainSize.")
307 }
308 gum::Set< std::string > son; // set of names
309 for (const auto& v: src.variablesSequence()) {
310 son.insert(v->name());
311 }
312 for (const auto& v: this->variablesSequence()) {
313 if (!son.contains(v->name())) {
315 "Variable <" << v->name() << "> not present in src (" << son << ").")
316 }
317 // we check size, labels and order of labels in the same time
318 if (v->toString() != src.variable(v->name()).toString()) {
319 GUM_ERROR(InvalidArgument, "Variables <" << v->name() << "> are not identical.")
320 }
321 }
322
324 Instantiation Idst(*this);
325
326 // pre-compute index mapping from src variable positions to dst positions (O(N))
327 // avoids repeated name lookups (O(N) each) inside the hot loop
328 const Idx ndim = src.nbrDim();
330 for (Idx i = 0; i < ndim; i++) {
332 }
333
334 for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
335 for (Idx i = 0; i < ndim; i++) {
336 Idst.chgVal(src_to_dst[i], Isrc.val(i));
337 }
338 this->set(Idst, src.get(Isrc));
339 }
340
341 return *this;
342 }
std::string toString() const
string version of *this
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
GUM_ELEMENT 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
const std::string & name() const
returns the name of the variable

References GUM_ERROR, and gum::Set< Key >::insert().

Referenced by gum::Counterfactual< GUM_ELEMENT >::_adaptToOriginalVariables_(), 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<GUM_Numeric GUM_SCALAR>
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 346 of file tensor_tpl.h.

347 {
348 if (src.nbrDim() != this->nbrDim()) {
349 GUM_ERROR(InvalidArgument, "Tensor to copy has not the same size.")
350 }
351 if (src.nbrDim() != mapSrc.size()) {
352 GUM_ERROR(InvalidArgument, "Tensor and vector have not the same size.")
353 }
355 for (Idx i = 0; i < src.nbrDim(); i++) {
358 "Variables " << mapSrc[i] << " (in the argument) and " << this->variable(i).name()
359 << " have not the same dimension.")
360 } else {
362 }
363 }
364 Instantiation Idst(*this);
365 for (Isrc.setFirst(); !Isrc.end(); ++Isrc, ++Idst) {
366 this->set(Idst, src.get(Isrc));
367 }
368
369 return *this;
370 }
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<GUM_Numeric 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 758 of file tensor_tpl.h.

758 {
759 Instantiation I(*this);
761
762 // if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) { return
763 // res; }
764 for (I.setFirst(); !I.end(); ++I) {
765 if (this->get(I) == v) res.insert(Instantiation(I, false));
766 }
767 return res;
768 }

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()

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

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

Referenced by gum::Tensor< GUM_SCALAR >::draw(), gum::Tensor< GUM_SCALAR >::findAll(), setIncNotification(), and gum::Tensor< GUM_SCALAR >::toString().

Here is the caller graph for this function:

◆ get_()

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

157 {
158 GUM_ERROR(OperationNotAllowed, "_get in the implementation !")
159 }

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

Here is the call graph for this function:

◆ getMasterRef() [1/2]

const MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( ) const
overrideinherited

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

Returns
Returns the master of this MultiDimAdressable.

Definition at line 305 of file multiDimContainer_tpl.h.

307 {
308 return static_cast< const MultiDimAdressable& >(*content());
309 }

◆ getMasterRef() [2/2]

MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( )
overrideinherited

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&

Definition at line 298 of file multiDimContainer_tpl.h.

302 {
303 return static_cast< MultiDimAdressable& >(*content());
304 }

◆ inverse()

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

the function to inverse (each value of) *this

Definition at line 491 of file tensor_tpl.h.

491 {
492 this->apply([](GUM_SCALAR x) { return 1 / x; });
493 return *this;
494 }

◆ isEvidence()

template<GUM_Numeric 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 879 of file tensor_tpl.h.

879 {
880 if (this->nbrDim() != 1) return false;
881 if (this->sum() <= 0.0) return false;
882 return (this->min() >= 0.0) && (this->max() <= 1.0);
883 }
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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::isNonZeroMap ( ) const

create a boolean-like tensor using the predicate isNonZero

Definition at line 637 of file tensor_tpl.h.

637 {
638 auto p = Tensor< GUM_SCALAR >(*this);
639 p.apply([](GUM_SCALAR x) {
640 if (x != static_cast< GUM_SCALAR >(0)) return static_cast< GUM_SCALAR >(1);
641 else return static_cast< GUM_SCALAR >(0);
642 });
643 return p;
644 }

◆ KL()

template<GUM_Numeric GUM_SCALAR>
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 391 of file tensor_tpl.h.

391 {
392 if (this->nbrDim() != p.nbrDim())
393 GUM_ERROR(InvalidArgument, "BNdistance between tensors with different numbers of dimensions")
395 if (!this->contains(*var))
396 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
397 }
398 for (const auto var: this->variablesSequence()) {
399 if (!p.contains(*var))
400 GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.")
401 }
402
403 Instantiation inst(*this);
404 auto res = static_cast< GUM_SCALAR >(0);
405 for (inst.setFirst(); !inst.end(); inst.inc()) {
406 GUM_SCALAR x = this->get(inst);
407 GUM_SCALAR y = p.get(inst);
408 if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
409 continue;
410
411 if (static_cast< GUM_SCALAR >(0) == y)
412 // we know that x!=0;
413 GUM_ERROR(FatalError, "The argument has a 0 at " << inst << " while the tensor has not.")
414
415 res += x * std::log2(x / y);
416 }
417 return res;
418 }
virtual bool contains(const DiscreteVariable &v) const =0
Returns true if var is in *this.
const Tensor< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Definition tensor_tpl.h:379

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

Here is the call graph for this function:

◆ log2()

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

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

Definition at line 379 of file tensor_tpl.h.

379 {
380 this->apply([](GUM_SCALAR x) { return std::log2(x); });
381 return *this;
382 }

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

Here is the call graph for this function:

◆ max()

template<GUM_Numeric GUM_SCALAR>
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_ELEMENT projectMax(const MultiDimImplementation< GUM_ELEMENT > &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<GUM_Numeric GUM_SCALAR>
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 624 of file tensor_tpl.h.

624 {
625 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
627 }
628
629 // if kept_var is empty, create an empty tensor
630 // TODO: remove this test when operations will be able to handle empty tensors
631 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->max()); }
632
634 }
gum::VariableSet _complementVars_(const gum::VariableSet &del_vars) const
Definition tensor_tpl.h:647

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<GUM_Numeric 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

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<GUM_Numeric GUM_SCALAR>
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 563 of file tensor_tpl.h.

563 {
564 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
566 }
567
568 // if we remove all the variables, create an empty tensor
569 // TODO: remove this test when operations will be able to handle empty tensors
570 if (this->variablesSequence().size() <= del_vars.size()) {
571 bool equal = true;
572 for (const auto var: this->variablesSequence()) {
573 if (!del_vars.exists(var)) {
574 equal = false;
575 break;
576 }
577 }
578 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->max()); }
579 }
580
582 }

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:

◆ mean()

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::Tensor< GUM_SCALAR >::mean ( ) const

compute the mean of a numerical discrete random variable @raise

Definition at line 249 of file tensor_tpl.h.

249 {
250 if (this->nbrDim() != 1) { GUM_ERROR(ArgumentError, "The tensor is not a marginal"); }
251 if (!gum::isCloseToOne(sum())) { GUM_ERROR(ArgumentError, "The tensor is not a distribution"); }
252 if (this->variable(0).isNumerical()) {
253 return expectedValue([this](const gum::Instantiation& i) -> GUM_SCALAR {
254 return GUM_SCALAR(this->variable(0).numerical(i.val(0)));
255 });
256 } else {
257 GUM_ERROR(ArgumentError, "The variable is not numerical");
258 }
259 }

References GUM_ERROR, gum::isCloseToOne(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), and sum().

Here is the call graph for this function:

◆ memoryFootprint()

template<GUM_Numeric 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 1201 of file tensor_tpl.h.

1201 {
1202 return this->content()->realSize() * sizeof(GUM_SCALAR);
1203 }

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

Here is the call graph for this function:

◆ min()

template<GUM_Numeric GUM_SCALAR>
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_ELEMENT projectMin(const MultiDimImplementation< GUM_ELEMENT > &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 >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

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<GUM_Numeric GUM_SCALAR>
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 611 of file tensor_tpl.h.

611 {
612 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
614 }
615
616 // if kept_var is empty, create an empty tensor
617 // TODO: remove this test when operations will be able to handle empty tensors
618 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->min()); }
619
621 }

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<GUM_Numeric GUM_SCALAR>
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 }

◆ minOut()

template<GUM_Numeric GUM_SCALAR>
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 541 of file tensor_tpl.h.

541 {
542 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
544 }
545
546 // if we remove all the variables, create an empty tensor
547 // TODO: remove this test when operations will be able to handle empty tensors
548 if (this->variablesSequence().size() <= del_vars.size()) {
549 bool equal = true;
550 for (const auto var: this->variablesSequence()) {
551 if (!del_vars.exists(var)) {
552 equal = false;
553 break;
554 }
555 }
556 if (equal) { return Tensor< GUM_SCALAR >().fillWith(this->min()); }
557 }
558
560 }

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()

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

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

References content_, and empty_value_.

Referenced by gum::Tensor< GUM_SCALAR >::draw(), gum::Tensor< GUM_SCALAR >::isEvidence(), gum::Tensor< GUM_SCALAR >::KL(), gum::Tensor< GUM_SCALAR >::mean(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::putFirst(), gum::Tensor< GUM_SCALAR >::reorganize(), and gum::Tensor< GUM_SCALAR >::variance().

Here is the caller graph for this function:

◆ new_abs()

template<GUM_Numeric 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 837 of file tensor_tpl.h.

837 {
838 return Tensor< GUM_SCALAR >(*this).abs();
839 }
const Tensor< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Definition tensor_tpl.h:421

◆ new_log2()

template<GUM_Numeric 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 847 of file tensor_tpl.h.

847 {
848 return Tensor< GUM_SCALAR >(*this).log2();
849 }

◆ new_sgn()

template<GUM_Numeric 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 852 of file tensor_tpl.h.

852 {
853 return Tensor< GUM_SCALAR >(*this).sgn();
854 }
const Tensor< GUM_SCALAR > & sgn() const
apply sgn(x)$ on every element of the container
Definition tensor_tpl.h:385

◆ new_sq()

template<GUM_Numeric 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 842 of file tensor_tpl.h.

842 {
843 return Tensor< GUM_SCALAR >(*this).sq();
844 }
const Tensor< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
Definition tensor_tpl.h:373

◆ newFactory()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > * gum::Tensor< GUM_SCALAR >::newFactory ( ) const
nodiscardfinalvirtual

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 newFactory().

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

◆ noising()

template<GUM_Numeric 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 828 of file tensor_tpl.h.

828 {
829 if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
830 GUM_ERROR(InvalidArgument, "alpha must be in [0,1]")
831 }
833 return fillWith(scale(1 - alpha) + noise.randomCPT().scale(alpha)).normalizeAsCPT();
834 }
const Tensor< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
Definition tensor_tpl.h:479
const Tensor< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Tensor
Definition tensor_tpl.h:810

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

Here is the call graph for this function:

◆ normalize()

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

normalisation of this do nothing if sum is 0

Definition at line 432 of file tensor_tpl.h.

432 {
433 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
434 if (this->empty_value_ != static_cast< GUM_SCALAR >(0))
435 this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
436 } else {
437 GUM_SCALAR s = sum();
438
439 if (s != (GUM_SCALAR)0) {
440 this->apply([s](GUM_SCALAR x) { return x / s; });
441 }
442 }
443 return *this;
444 }

References 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<GUM_Numeric GUM_SCALAR>
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 447 of file tensor_tpl.h.

447 {
448 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
449 if (this->empty_value_ != static_cast< GUM_SCALAR >(0)) {
450 this->empty_value_ = static_cast< GUM_SCALAR >(1.0);
451 } else {
452 GUM_ERROR(FatalError, "Normalization for a tensor that sum to 0 in " << *this)
453 }
454 } else {
455 if (varId >= this->nbrDim()) {
456 GUM_ERROR(FatalError, varId << " is not a position for " << *this)
457 }
458 Instantiation inst(*this);
459 const auto& v = this->variable(varId);
460
461 for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
462 auto s = (GUM_SCALAR)0.0;
463 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
464 s += this->get(inst);
465 if (s == (GUM_SCALAR)0.0) {
466 GUM_ERROR(FatalError, "Normalization for a tensor that sum to 0 in " << *this)
467 }
468 if (s != (GUM_SCALAR)1.0) {
469 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
470 this->set(inst, this->get(inst) / s);
471 }
472 inst.setFirstVar(v); // to remove inst.end()
473 }
474 }
475 return *this;
476 }

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:

◆ notifyChange()

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

Definition at line 170 of file multiDimDecorator_tpl.h.

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

◆ operator&()

template<GUM_Numeric 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 902 of file tensor_tpl.h.

902 {
903 if (!isEvidence() || !p2.isEvidence())
904 GUM_ERROR(InvalidArgument, "The tensors are not evidences.")
905 if (this->variable(0) != p2.variable(0))
906 GUM_ERROR(InvalidArgument, "The evidence are not on the same variable.")
909 for (I.setFirst(); !I.end(); ++I) {
910 res.set(I, std::min(res.get(I), p2.get(I)));
911 }
912 return res;
913 }
bool isEvidence() const
is an evidence ? (marginal-like but has not to sum to 1)
Definition tensor_tpl.h:879

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<GUM_Numeric 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 957 of file tensor_tpl.h.

957 {
958 return Tensor< GUM_SCALAR >(*this).scale(v);
959 }

◆ operator*() [2/2]

template<GUM_Numeric 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 948 of file tensor_tpl.h.

948 {
949 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).scale(p2.empty_value_);
950 if (this->empty()) return Tensor< GUM_SCALAR >(p2).scale(this->empty_value_);
951
952 return Tensor< GUM_SCALAR >(*this->content() * *p2.content());
953 }

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<GUM_Numeric 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 998 of file tensor_tpl.h.

998 {
999 this->scale(v);
1000 return *this;
1001 }

References scale().

Here is the call graph for this function:

◆ operator*=() [2/2]

template<GUM_Numeric 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 992 of file tensor_tpl.h.

992 {
993 *this = *this * r;
994 return *this;
995 }

◆ operator+() [1/2]

template<GUM_Numeric 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 873 of file tensor_tpl.h.

873 {
874 return Tensor< GUM_SCALAR >(*this).translate(v);
875 }
const Tensor< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
Definition tensor_tpl.h:485

◆ operator+() [2/2]

template<GUM_Numeric 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 864 of file tensor_tpl.h.

864 {
866 if (this->empty()) return Tensor< GUM_SCALAR >(p2).translate(this->empty_value_);
867
868 return Tensor< GUM_SCALAR >(*this->content() + *p2.content());
869 }

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<GUM_Numeric 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 986 of file tensor_tpl.h.

986 {
987 this->translate(v);
988 return *this;
989 }

References translate().

Here is the call graph for this function:

◆ operator+=() [2/2]

template<GUM_Numeric 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 980 of file tensor_tpl.h.

980 {
981 *this = *this + r;
982 return *this;
983 }

◆ operator-() [1/2]

template<GUM_Numeric 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 942 of file tensor_tpl.h.

942 {
943 return Tensor< GUM_SCALAR >(*this).translate(-v);
944 }

◆ operator-() [2/2]

template<GUM_Numeric 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 930 of file tensor_tpl.h.

930 {
931 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).translate(-p2.empty_value_);
932 if (this->empty()) {
933 auto p = Tensor< GUM_SCALAR >(p2);
934 p.apply([this](GUM_SCALAR x) { return this->empty_value_ - x; });
935 return p;
936 }
937 return Tensor< GUM_SCALAR >(*this->content() - *p2.content());
938 }

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<GUM_Numeric 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 1010 of file tensor_tpl.h.

1010 {
1011 this->translate(-v);
1012 return *this;
1013 }

References translate().

Here is the call graph for this function:

◆ operator-=() [2/2]

template<GUM_Numeric 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 1004 of file tensor_tpl.h.

1004 {
1005 *this = *this - r;
1006 return *this;
1007 }

◆ operator/() [1/2]

template<GUM_Numeric 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 975 of file tensor_tpl.h.

975 {
976 return Tensor< GUM_SCALAR >(*this).scale(1 / v);
977 }

◆ operator/() [2/2]

template<GUM_Numeric 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 963 of file tensor_tpl.h.

963 {
964 if (p2.empty()) return Tensor< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
965 if (this->empty()) {
966 auto p = Tensor< GUM_SCALAR >(p2);
967 p.apply([this](GUM_SCALAR x) { return this->empty_value_ / x; });
968 return p;
969 }
970 return Tensor< GUM_SCALAR >(*this->content() / *p2.content());
971 }

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<GUM_Numeric 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 1022 of file tensor_tpl.h.

1022 {
1023 this->scale(1 / v);
1024 return *this;
1025 }

References scale().

Here is the call graph for this function:

◆ operator/=() [2/2]

template<GUM_Numeric 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 1016 of file tensor_tpl.h.

1016 {
1017 *this = *this / r;
1018 return *this;
1019 }

◆ operator<<()

template<GUM_Numeric 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 852 of file tensor_tpl.h.

857 {
858 this->add(v);
859 return *this;
860 }

◆ operator=() [1/2]

template<GUM_Numeric 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_ELEMENT > & operator=(const MultiDimDecorator &from) noexcept
copy operator

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

Here is the call graph for this function:

◆ operator=() [2/2]

template<GUM_Numeric 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 }

◆ operator==() [1/2]

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

149 {
150 if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
151 if (nbrDim() == 0) return true;
152
153 for (auto iter = variablesSequence().beginSafe(); iter != variablesSequence().endSafe();
154 ++iter) {
155 if (!p.variablesSequence().exists(*iter)) { return false; }
156 }
157 } else {
158 return false;
159 }
160
161 Instantiation i(*this);
163 for (i.setFirst(); !i.end(); ++i) {
164 if (cmp(get(i), p.get(i))) { return false; }
165 }
166
167 return true;
168 }

◆ operator==() [2/2]

template<GUM_Numeric 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 1028 of file tensor_tpl.h.

1028 {
1029 if (this->empty()) {
1030 if (r.empty()) return this->empty_value_ == r.empty_value_;
1031 else return false;
1032 } else {
1033 if (r.empty()) return false;
1034 else return (*this->content_) == (*r.content_);
1035 }
1036 }

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[]()

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_ELEMENT) value.

Definition at line 184 of file multiDimContainer_tpl.h.

103 {
104 return get(i);
105 }

◆ operator|()

template<GUM_Numeric 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 887 of file tensor_tpl.h.

887 {
888 if (!isEvidence() || !p2.isEvidence())
889 GUM_ERROR(InvalidArgument, "The tensors are not evidences.")
890 if (this->variable(0) != p2.variable(0))
891 GUM_ERROR(InvalidArgument, "The evidence are not on the same variable.")
894 for (I.setFirst(); !I.end(); ++I) {
895 res.set(I, std::max(res.get(I), p2.get(I)));
896 }
897 return res;
898 }

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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::operator~ ( ) const

the function to be used to add two Tensors

Definition at line 917 of file tensor_tpl.h.

917 {
918 if (!isEvidence()) GUM_ERROR(InvalidArgument, "The tensor is not an evidence.")
919
922 for (I.setFirst(); !I.end(); ++I) {
923 res.set(I, 1 - res.get(I));
924 }
925 return res;
926 }

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

Here is the call graph for this function:

◆ populate() [1/2]

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]);
}
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
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 212 of file multiDimDecorator_tpl.h.

326 {
327 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
328 if (v.size() == 1) {
329 empty_value_ = v[0];
330 } else {
331 GUM_ERROR(SizeError, "Size do not match in populate")
332 }
333 } else {
335 }
336 }
void populate(const std::vector< GUM_ELEMENT > &v) const final
Automatically fills this MultiDimContainer with the values in v.

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

Here is the caller graph for this function:

◆ populate() [2/2]

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

Automatically fills this MultiDimContainer with the values in l.

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

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

Definition at line 230 of file multiDimContainer_tpl.h.

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

◆ pos()

Idx gum::MultiDimDecorator< GUM_SCALAR >::pos ( const DiscreteVariable & var) 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 139 of file multiDimDecorator_tpl.h.

273 {
274 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->pos(var);
275 }

References content_, and newFactory().

Here is the call graph for this function:

◆ prodIn()

template<GUM_Numeric GUM_SCALAR>
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 598 of file tensor_tpl.h.

598 {
599 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
601 }
602
603 // if kept_var is empty, create an empty tensor
604 // TODO: remove this test when operations will be able to handle empty tensors
605 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->product()); }
606
608 }
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<GUM_Numeric GUM_SCALAR>
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 519 of file tensor_tpl.h.

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

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<GUM_Numeric GUM_SCALAR>
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_ELEMENT projectProduct(const MultiDimImplementation< GUM_ELEMENT > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product

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

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<GUM_Numeric 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 698 of file tensor_tpl.h.

698 {
699 if (!this->contains(*var)) {
700 GUM_ERROR(InvalidArgument, "The variable to put first does not belong to the tensor")
701 }
702 if (&(this->variable(0)) == var) return Tensor< GUM_SCALAR >(*this);
703
705 vars.push_back(var);
706 for (Idx i = 0; i < this->nbrDim(); i++)
707 if (&(this->variable(i)) != var) vars.push_back(&(this->variable(i)));
708
709 return this->reorganize(vars);
710 }
Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Tensor with another order
Definition tensor_tpl.h:658

References gum::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<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::Tensor< GUM_SCALAR >::putFirst ( std::string_view varname) const

create a new Tensor with a certain variable in first

Exceptions
InvalidArgumentif the var is not in the tensor

Definition at line 713 of file tensor_tpl.h.

713 {
714 const DiscreteVariable* var = nullptr;
715
716 for (gum::Idx i = 0; i < this->nbrDim(); i++)
717 if (this->variable(i).name() == varname) {
718 var = &(this->variable(i));
719 break;
720 }
721 if (var == nullptr)
723 "The variable '" << varname << "' to put first does not belong to the tensor")
725 }
Tensor< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Tensor with a certain variable in first
Definition tensor_tpl.h:698

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<GUM_Numeric 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 785 of file tensor_tpl.h.

785 {
786 if (this->domainSize() == 0) return *this;
787
789 v.reserve(this->domainSize());
790 for (Size i = 0; i < this->domainSize(); ++i) {
791 auto r = (GUM_SCALAR)randomProba();
792 v.push_back(r);
793 }
794 this->fillWith(v);
795 return *this;
796 }
virtual Size domainSize() const =0
Returns the product of the variables domain size.

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

Here is the call graph for this function:

◆ randomCPT()

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

generate a random CPT in the Tensor

Definition at line 810 of file tensor_tpl.h.

810 {
811 if (this->domainSize() == 0) {
812 this->fillWith((GUM_SCALAR)1.0);
813 } else {
814 gum::Instantiation I(*this);
815 const auto& v = this->variable(0);
816 for (I.setFirstNotVar(v); !I.end(); I.incNotVar(v)) {
818 for (I.setFirstVar(v); !I.end(); I.incVar(v)) {
819 this->set(I, distrib[I.val(0)]);
820 }
821 I.unsetEnd();
822 }
823 }
824 return *this;
825 }
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<GUM_Numeric GUM_SCALAR>
const Tensor< GUM_SCALAR > & gum::Tensor< GUM_SCALAR >::randomDistribution ( ) const

generate a random Distribution in the Tensor

Definition at line 799 of file tensor_tpl.h.

799 {
800 if (this->domainSize() == 0) {
801 this->fillWith((GUM_SCALAR)1.0);
802 } else {
804 }
805
806 return *this;
807 }

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

Here is the call graph for this function:

◆ reduce()

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

350 {
351 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
352 return base;
353 } else {
354 return content_->reduce(f, base);
355 }
356 }
GUM_ELEMENT reduce(std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f, GUM_ELEMENT base) const final
compute lfold for this container

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

Here is the caller graph for this function:

◆ registerSlave()

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

248 {
250 }
bool registerSlave(Instantiation &i) final

◆ reorganize() [1/2]

template<GUM_Numeric 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 658 of file tensor_tpl.h.

658 {
659 if (vars.size() != this->nbrDim())
661 "The argument contains " << vars.size() << " variables instead of "
662 << this->nbrDim() << ".")
663 for (const auto var: vars) {
664 if (!this->contains(*var))
665 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
666 }
667
670 for (const auto var: vars)
671 p.add(*var);
673 p.copyFrom(*this, nullptr); // copy *this in p using the same order
674
675 return p;
676 }

References gum::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<GUM_Numeric 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 680 of file tensor_tpl.h.

680 {
682
684 for (gum::Idx i = 0; i < this->nbrDim(); i++)
685 namesToVars.insert(this->variable(i).name(), &(this->variable(i)));
686
687 for (const auto& name: vars) {
688 if (!namesToVars.exists(name)) {
690 "'" << name << "' is a not a name of a variable in this tensor")
691 }
692 res.push_back(namesToVars[name]);
693 }
694 return reorganize(res);
695 }

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

57 {
58 if (!contains(x)) { GUM_ERROR(NotFound, "could not find the variable") }
59
60 if (contains(y)) { GUM_ERROR(DuplicateElement, "variable " << y << " already in MultiDim") }
61
62 if (x.domainSize() != y.domainSize()) {
63 GUM_ERROR(OperationNotAllowed, "incompatible variables")
64 }
65
66 replace_(&x, &y);
67 }
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override

◆ replace_()

void gum::MultiDimDecorator< GUM_SCALAR >::replace_ ( const DiscreteVariable * x,
const DiscreteVariable * y )
overrideprotectedvirtualinherited

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

406 {
407 this->content()->replace(*x, *y);
408 }
void replace(const DiscreteVariable &x, const DiscreteVariable &y)
Replace variables in this multidim.

◆ scale()

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

multiply (each value of) *this by v

Definition at line 479 of file tensor_tpl.h.

479 {
480 this->apply([v](GUM_SCALAR x) { return x * v; });
481 return *this;
482 }

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

Here is the caller graph for this function:

◆ set()

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

172 {
173 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->nbrDim() == 0) {
175 } else {
176 static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->set(i, value);
177 }
178 }

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

Here is the caller graph for this function:

◆ setChangeNotification()

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

213 {
215 }
void setChangeNotification(const Instantiation &i) final

◆ setDecNotification()

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

241 {
243 }
void setDecNotification(const Instantiation &i) final

◆ setFirstNotification()

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

220 {
222 }
void setFirstNotification(const Instantiation &i) final

◆ setIncNotification()

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

234 {
236 }
void setIncNotification(const Instantiation &i) final

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

Here is the call graph for this function:

◆ setLastNotification()

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

227 {
229 }
void setLastNotification(const Instantiation &i) final

References empty_value_.

◆ sgn()

template<GUM_Numeric GUM_SCALAR>
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 385 of file tensor_tpl.h.

385 {
386 this->apply([](GUM_SCALAR x) { return (GUM_SCALAR(0) < x) - (x < GUM_SCALAR(0)); });
387 return *this;
388 }

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

Here is the call graph for this function:

◆ sq()

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

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

Definition at line 373 of file tensor_tpl.h.

373 {
374 this->apply([](GUM_SCALAR x) { return x * x; });
375 return *this;
376 }

◆ stdDev()

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::Tensor< GUM_SCALAR >::stdDev ( ) const

compute the stdDev of a numerical discrete random variable

@raise

Definition at line 277 of file tensor_tpl.h.

277 {
278 return std::sqrt(variance());
279 }
GUM_SCALAR variance() const
compute the variance of a numerical discrete random variable @raise
Definition tensor_tpl.h:262

◆ sum()

template<GUM_Numeric GUM_SCALAR>
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_ELEMENT projectSum(const MultiDimImplementation< GUM_ELEMENT > &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(), mean(), normalize(), sumIn(), and variance().

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

◆ sumIn()

template<GUM_Numeric GUM_SCALAR>
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 585 of file tensor_tpl.h.

585 {
586 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
588 }
589
590 // if kept_var is empty, create an empty tensor
591 // TODO: remove this test when operations will be able to handle empty tensors
592 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->sum()); }
593
595 }

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<GUM_Numeric GUM_SCALAR>
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 497 of file tensor_tpl.h.

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

◆ swapContent_()

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 266 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 }

◆ toString() [1/2]

std::string gum::MultiDimDecorator< GUM_SCALAR >::toString ( const Instantiation * i) const
overridevirtualinherited

Default implementation of MultiDimContainer::set().

Calls get_ as a r-value.

Implements gum::MultiDimAdressable.

Definition at line 236 of file multiDimDecorator_tpl.h.

400 {
401 return content_->toString(i);
402 }
std::string toString(const Instantiation *i) const override
Default implementation of MultiDimContainer::set().

◆ toString() [2/2]

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

the function to be used to add two Tensors

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 1039 of file tensor_tpl.h.

1039 {
1040 auto table = this->content();
1042
1043 if (table->nbrDim() == 0) {
1044 Instantiation I(this);
1045 ss << "[" << this->get(I) << "]";
1046 return ss.str();
1047 }
1048 const Size colwidth = 6;
1049 const Size numberwidth = 9;
1050 const Size nbrLigMax = 6;
1051
1053 ss.precision(numberwidth - 5);
1054
1055 const auto& var = table->variable(0);
1056
1057 const Size nbparents = table->nbrDim() - 1;
1058 const Size nbcol = var.domainSize();
1059
1060 // box-drawing characters
1061 constexpr const char* vbar = "│"; // U+2502 single vertical
1062 constexpr const char* dvbar = "║"; // U+2551 double vertical
1063 constexpr const char* hbar = "─"; // U+2500 single horizontal
1064
1065 const auto repeat_str = [](const char* s, Size n) {
1066 std::string r;
1067 const std::string unit(s);
1068 r.reserve(unit.size() * n);
1069 for (Size i = 0; i < n; i++)
1070 r += unit;
1071 return r;
1072 };
1075
1076 if (nbparents > 0) ss << std::setw(nbparents * (colwidth + 1) - 1) << " " << dvbar;
1077 ss << " " << std::setw(nbcol * (numberwidth + 1) - 3)
1078 << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << vbar;
1079 ss << std::endl;
1080
1081 if (nbparents > 0) {
1082 for (Idx i = 1; i <= nbparents; i++)
1083 ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth)
1084 << (i < (Idx)nbparents ? vbar : dvbar);
1085 }
1086 for (Idx i = 0; i < nbcol; i++)
1087 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << vbar;
1088 ss << std::endl;
1089
1090
1091 if (nbparents > 0) {
1092 for (Idx i = 1; i <= nbparents; i++)
1093 ss << maskparent << (i < (Idx)nbparents ? vbar : dvbar);
1094 }
1095 for (Idx i = 0; i < nbcol; i++)
1096 ss << masknumber << vbar;
1097 ss << std::endl;
1099
1100 const auto drawligne = [&]() {
1101 if (nbparents > 0) {
1102 for (Idx i = 1; i <= nbparents; i++)
1103 ss << std::setw(colwidth) << table->variable(i).label(I.val(i)).substr(0, colwidth)
1104 << (i < (Idx)nbparents ? vbar : dvbar);
1105 }
1106 for (I.setFirstVar(var); !I.end(); I.incVar(var))
1107 ss << " " << std::setw(numberwidth - 1) << table->get(I) << vbar;
1108 I.setFirstVar(var);
1109 ss << std::endl;
1110 };
1111
1112 if (const Size nbrLig = table->domainSize() / var.domainSize(); nbrLig < nbrLigMax * 2 + 1) {
1113 for (I.setFirst(); !I.end(); I.incNotVar(var))
1114 drawligne();
1115 } else {
1116 Size cpt = 0;
1117 for (I.setFirst(); !I.end(); I.incNotVar(var)) {
1118 cpt++;
1119 if (cpt > nbrLigMax) break;
1120 drawligne();
1121 }
1122 ss << "[..." << nbrLig - nbrLigMax * 2 << " more line(s) ...]" << std::endl;
1123 I.setLast();
1124 for (Idx revi = 1; revi < nbrLigMax; revi++)
1125 I.decNotVar(var);
1126 for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
1127 drawligne();
1128 }
1129 }
1130
1131 return ss.str();
1132 }
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<GUM_Numeric GUM_SCALAR>
const Tensor< GUM_SCALAR > & gum::Tensor< GUM_SCALAR >::translate ( GUM_SCALAR v) const

add v to (each value of) *this

Definition at line 485 of file tensor_tpl.h.

485 {
486 this->apply([v](GUM_SCALAR x) { return x + v; });
487 return *this;
488 }

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

Here is the caller graph for this function:

◆ uniformTensor()

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

Definition at line 1228 of file tensor_tpl.h.

1228 {
1230
1232 pot << var;
1234
1235 pot.normalize();
1236
1237 return pot;
1238 }
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:432

◆ unregisterSlave()

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

289 {
291 }
bool unregisterSlave(Instantiation &i) final

References MultiDimDecorator().

Here is the call graph for this function:

◆ variable() [1/3]

virtual const DiscreteVariable & gum::MultiDimInterface::variable ( std::string_view name) const
pure virtualinherited

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.

◆ variable() [2/3]

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

263 {
264 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->variable(i);
265 }

References content_, and empty_value_.

Referenced by gum::Tensor< GUM_SCALAR >::draw(), gum::Tensor< GUM_SCALAR >::fillWith(), 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(), and gum::Tensor< GUM_SCALAR >::variance().

Here is the caller graph for this function:

◆ variable() [3/3]

virtual const DiscreteVariable & gum::MultiDimInterface::variable ( std::string_view name) const
pure virtualinherited

◆ variablesSequence()

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

314 {
316 }

Referenced by gum::Counterfactual< GUM_ELEMENT >::_adaptToOriginalVariables_(), gum::Tensor< GUM_SCALAR >::_complementVars_(), gum::Tensor< GUM_SCALAR >::maxOut(), gum::Tensor< GUM_SCALAR >::minOut(), and gum::Tensor< GUM_SCALAR >::prodOut().

Here is the caller graph for this function:

◆ variance()

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::Tensor< GUM_SCALAR >::variance ( ) const

compute the variance of a numerical discrete random variable @raise

Definition at line 262 of file tensor_tpl.h.

262 {
263 if (this->nbrDim() != 1) { GUM_ERROR(ArgumentError, "The tensor is not a marginal"); }
264 if (!gum::isCloseToOne(sum())) { GUM_ERROR(ArgumentError, "The tensor is not a distribution"); }
265 if (this->variable(0).isNumerical()) {
266 const auto mu = mean();
267 return expectedValue([this, mu](const gum::Instantiation& i) -> GUM_SCALAR {
268 const auto r = GUM_SCALAR(this->variable(0).numerical(i.val(0))) - mu;
269 return r * r;
270 });
271 } else {
272 GUM_ERROR(ArgumentError, "The variable is not numerical");
273 }
274 }
GUM_SCALAR mean() const
compute the mean of a numerical discrete random variable @raise
Definition tensor_tpl.h:249

References GUM_ERROR, gum::isCloseToOne(), gum::isNumerical(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), sum(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Here is the call graph for this function:

Member Data Documentation

◆ content_

◆ empty_value_


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