aGrUM 3.3.2
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 >:
[legend]
Collaboration diagram for gum::Tensor< GUM_SCALAR >:
[legend]

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< Instantiation > findAll (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 DiscreteVariable & variable (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 DiscreteVariable & variable (std::string_view name) const =0
 Returns the variable with the name.
virtual const DiscreteVariable & variable (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.
MultiDimAdressable & getMasterRef () override
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer.
const MultiDimAdressable & getMasterRef () 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 63 of file tensor_tpl.h.

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

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

Referenced by Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), abs(), operator=(), 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 71 of file tensor_tpl.h.

71 : Tensor() {
72 for (const auto& var: vars) {
73 this->add(*var);
74 }
75 }
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 79 of file tensor_tpl.h.

79 :
81 // for debugging purposes
83 }

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

106 :
108 // for debugging purposes
110
111 if (!src.empty()) {
112 this->beginMultipleChanges();
113
114 for (Idx i = 0; i < src.variablesSequence().size(); i++) {
115 this->add(*(src.variablesSequence()[i]));
116 }
117
118 this->endMultipleChanges();
119 this->content()->copyFrom(*src.content());
120 }
121 }
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 87 of file tensor_tpl.h.

87 :
90 *(src.content())) {
92 // GUM_CONS_CPY not here because in called Tensor
93 // GUM_CONS_CPY( Tensor );
94 }
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 98 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 145 of file tensor_tpl.h.

145 {
146 // for debugging purposes
148 }

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

648 {
650
651 for (const auto x: this->variablesSequence())
652 if (!vars.contains(x)) cplt.insert(x);
653
654 return cplt;
655 }
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 422 of file tensor_tpl.h.

422 {
423 this->apply([](GUM_SCALAR x) {
424 if (x >= 0) return x;
425 else return -x;
426 });
427 return *this;
428 }
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.
Size domainSize() const final
Returns the product of the variables domain size.

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(), gum::Tensor< GUM_SCALAR >::sgn(), and gum::Tensor< GUM_SCALAR >::sq().

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

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

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

780 {
781 auto m = min();
782 return std::pair(findAll(m), m);
783 }
GUM_SCALAR min() const
min of all elements in the Tensor
Definition tensor_tpl.h:185

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.

371 {
373 }

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 }

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

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

Here is the caller graph for this function:

◆ 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 }
const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Idx nbrDim() const final
Returns the number of vars in the multidimensional container.

◆ 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 1218 of file tensor_tpl.h.

1219 {
1221
1223 pot << var;
1225
1227 I.chgVal(var, value);
1228 pot.set(I, GUM_SCALAR(1.0));
1229
1230 return pot;
1231 }
void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
void set(const Instantiation &i, const GUM_ELEMENT &value) const final
Default implementation of MultiDimContainer::set().
void endMultipleChanges() final
Default implementation of MultiDimContainer::set().

References gum::Instantiation::chgVal().

Referenced by gum::GraphicalModelInference< GUM_SCALAR >::_createHardEvidence_(), gum::KTBNInference< GUM_SCALAR >::_fillWindow_(), 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 1234 of file tensor_tpl.h.

1235 {
1237 }
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 737 of file tensor_tpl.h.

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

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

284 {
285 return -this->expectedValue([this](const gum::Instantiation& i) -> GUM_SCALAR {
286 return GUM_SCALAR(GUM_LOG2_OR_0(this->get(i)));
287 });
288 }
GUM_SCALAR expectedValue(std::function< GUM_SCALAR(const gum::Instantiation &) >) const
ExpectedValue computes the expectation of f over *this.
Definition tensor_tpl.h:234

◆ 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 1147 of file tensor_tpl.h.

1147 {
1148 const auto i = v.closestIndex(val);
1149
1151 p.add(v);
1152 p.fillWith(0);
1153 Instantiation I(p);
1154 I.chgVal(0, i);
1155 p.set(I, 1);
1156 return p;
1157 }
void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
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:305

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

1181 {
1182 const auto i = v.closestIndex(val);
1183 if (i == v.domainSize() - 1) { return evEq(v, val); }
1184
1186 p.add(v);
1187 p.fillWith(0);
1188 Instantiation I(p);
1189 for (Idx i2 = i + 1; i2 < v.domainSize(); i2++) {
1190 I.chgVal(0, i2);
1191 p.set(I, 1);
1192 }
1193 return p;
1194 }
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 1161 of file tensor_tpl.h.

1161 {
1162 if (val2 < val1) {
1164 "val2 (" << val2 << ") must be greater than val1 (" << val1 << ").")
1165 }
1166 const auto i1 = v.closestIndex(val1);
1167 const auto i2 = v.closestIndex(val2);
1168
1170 p.add(v);
1171 p.fillWith(0);
1172 Instantiation I(p);
1173 for (Idx i = i1; i <= i2; i++) {
1174 I.chgVal(0, i);
1175 p.set(I, 1);
1176 }
1177 return p;
1178 }

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

1197 {
1198 const auto i = v.closestIndex(val);
1199 if (i == 0) { return evEq(v, val); }
1200
1202 p.add(v);
1203 p.fillWith(0);
1204 Instantiation I(p);
1205 for (Idx i2 = 0; i2 < i; i2++) {
1206 I.chgVal(0, i2);
1207 p.set(I, 1);
1208 }
1209 return p;
1210 }

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

235 {
236 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
237 return static_cast< GUM_SCALAR >(0);
238 }
239
240 GUM_SCALAR res = 0;
241 auto i = Instantiation(*this);
242 for (i.setFirst(); !i.end(); i.inc()) {
243 const GUM_SCALAR v_f = f(i);
244 if (v_f != GUM_SCALAR(0.0)) { res += this->get(i) * v_f; }
245 }
246 return res;
247 }
Instantiation()
Default constructor: creates an empty tuple.
MultiDimImplementation< GUM_SCALAR > * content_
virtual bool empty() const =0
Returns true if no var is in *this.

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

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

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

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

Here is the caller graph for this function:

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

References GUM_ERROR.

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

298 {
299 this->fill(val);
300 return *this;
301 }
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 292 of file tensor_tpl.h.

292 {
293 this->populate(data);
294 return *this;
295 }
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 305 of file tensor_tpl.h.

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

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

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

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

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

References gum::Instantiation::Instantiation(), 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(), contains(), 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 492 of file tensor_tpl.h.

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

◆ 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 880 of file tensor_tpl.h.

880 {
881 if (this->nbrDim() != 1) return false;
882 if (this->sum() <= 0.0) return false;
883 return (this->min() >= 0.0) && (this->max() <= 1.0);
884 }
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:158

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

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

◆ 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 392 of file tensor_tpl.h.

392 {
393 if (this->nbrDim() != p.nbrDim())
394 GUM_ERROR(InvalidArgument, "BNdistance between tensors with different numbers of dimensions")
396 if (!this->contains(*var))
397 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
398 }
399 for (const auto var: this->variablesSequence()) {
400 if (!p.contains(*var))
401 GUM_ERROR(InvalidArgument, "A variable does not belong to the argument.")
402 }
403
404 Instantiation inst(*this);
405 auto res = static_cast< GUM_SCALAR >(0);
406 for (inst.setFirst(); !inst.end(); inst.inc()) {
407 GUM_SCALAR x = this->get(inst);
408 GUM_SCALAR y = p.get(inst);
409 if (static_cast< GUM_SCALAR >(0) == x) // 0*log(0/y)=0
410 continue;
411
412 if (static_cast< GUM_SCALAR >(0) == y)
413 // we know that x!=0;
414 GUM_ERROR(FatalError, "The argument has a 0 at " << inst << " while the tensor has not.")
415
416 res += x * std::log2(x / y);
417 }
418 return res;
419 }
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:380

References 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 380 of file tensor_tpl.h.

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

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

176 {
177 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
178 return this->empty_value_;
179 }
180 return gum::projectMax(*this->content());
181 }
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 >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

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

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

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

195 {
197
198 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
199 res = this->empty_value_;
200 } else {
201 res = this->reduce(
202 [](GUM_SCALAR z, GUM_SCALAR p) {
203 return (p == static_cast< GUM_SCALAR >(1)) ? z
204 : (z == static_cast< GUM_SCALAR >(1)) ? p
205 : (p > z ? p : z);
206 },
207 static_cast< GUM_SCALAR >(1));
208 }
209
210 return res;
211 }
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(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

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

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

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

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

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

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

1213 {
1214 return this->content()->realSize() * sizeof(GUM_SCALAR);
1215 }

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

185 {
186 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
187 return this->empty_value_;
188 }
189 return gum::projectMin(*this->content());
190 }
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_, and gum::MultiDimDecorator< GUM_SCALAR >::empty().

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

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

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

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

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:

◆ 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 542 of file tensor_tpl.h.

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

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

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

◆ 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 848 of file tensor_tpl.h.

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

◆ 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 853 of file tensor_tpl.h.

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

◆ 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 843 of file tensor_tpl.h.

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

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

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

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

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

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

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

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_(), and gum::GibbsOperator< GUM_SCALAR >::_smoothedGibbsSample_().

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

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

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

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

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

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

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

◆ 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 949 of file tensor_tpl.h.

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

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

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

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

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

◆ 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 874 of file tensor_tpl.h.

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

◆ 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 865 of file tensor_tpl.h.

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

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

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

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

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

◆ 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 943 of file tensor_tpl.h.

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

◆ 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 931 of file tensor_tpl.h.

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

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

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

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

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

◆ 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 976 of file tensor_tpl.h.

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

◆ 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 964 of file tensor_tpl.h.

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

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

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

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

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

◆ 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 853 of file tensor_tpl.h.

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

◆ 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 125 of file tensor_tpl.h.

125 {
127 if (&src == this) return *this;
129 return *this;
130 }
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 134 of file tensor_tpl.h.

134 {
136 if (&src == this) return *this;
139 return *this;
140 }

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

Here is the call graph for this function:

◆ 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 1029 of file tensor_tpl.h.

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

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

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

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

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

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

599 {
600 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
602 }
603
604 // if kept_var is empty, create an empty tensor
605 // TODO: remove this test when operations will be able to handle empty tensors
606 if (kept_vars.empty()) { return Tensor< GUM_SCALAR >().fillWith(this->product()); }
607
609 }
GUM_SCALAR product() const
product of all elements in the Tensor
Definition tensor_tpl.h:167

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

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

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

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

Referenced by prodIn(), and prodOut().

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

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

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

Referenced by putFirst().

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

◆ putFirst() [2/2]

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

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

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

786 {
787 if (this->domainSize() == 0) return *this;
788
790 v.reserve(this->domainSize());
791 for (Size i = 0; i < this->domainSize(); ++i) {
792 auto r = (GUM_SCALAR)randomProba();
793 v.push_back(r);
794 }
795 this->fillWith(v);
796 return *this;
797 }
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 811 of file tensor_tpl.h.

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

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

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

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

◆ 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 659 of file tensor_tpl.h.

659 {
660 if (vars.size() != this->nbrDim())
662 "The argument contains " << vars.size() << " variables instead of "
663 << this->nbrDim() << ".")
664 for (const auto var: vars) {
665 if (!this->contains(*var))
666 GUM_ERROR(InvalidArgument, "A variable in the argument does not belong to the tensor.")
667 }
668
671 for (const auto var: vars)
672 p.add(*var);
674 p.copyFrom(*this, nullptr); // copy *this in p using the same order
675
676 return p;
677 }
virtual void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const
Basic copy of a MultiDimContainer.

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

Referenced by putFirst(), and reorganize().

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

◆ reorganize() [2/2]

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

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

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 }

◆ 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 480 of file tensor_tpl.h.

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

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.

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

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

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

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 }

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

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

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

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

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

Here is the call graph for this function:

◆ 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 278 of file tensor_tpl.h.

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

References variance().

Here is the call graph for this function:

◆ sum()

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

sum of all elements in the Tensor

Definition at line 158 of file tensor_tpl.h.

158 {
159 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(this->content_)->empty()) {
160 return this->empty_value_;
161 }
162 return gum::projectSum(*this->content());
163 }
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 >::empty(), and gum::MultiDimDecorator< GUM_SCALAR >::empty_value_.

Referenced by gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_(), isEvidence(), 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 586 of file tensor_tpl.h.

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

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

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

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:

◆ 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 1040 of file tensor_tpl.h.

1040 {
1041 auto table = this->content();
1043
1044 if (table->nbrDim() == 0) {
1045 Instantiation I(this);
1046 ss << "[" << this->get(I) << "]";
1047 return ss.str();
1048 }
1049 // size of the display from the configuration (tensor.columnWidthConsole, tensor.maxRowsConsole)
1050 const auto sizeFromConfiguration = [](const std::string& key, std::int64_t minimum) {
1051 const auto value = Configuration::instance().typed< std::int64_t >("tensor", key);
1052 if (value < minimum) {
1054 "The key tensor." << key << " of the configuration must be at least " << minimum
1055 << ".")
1056 }
1057 return static_cast< Size >(value);
1058 };
1059
1060 const Size colwidth = 6;
1061 const Size numberwidth = sizeFromConfiguration("columnWidthConsole", 6);
1062 const Size nbrLigMax = sizeFromConfiguration("maxRowsConsole", 1);
1063
1065 ss.precision(numberwidth - 5);
1066
1067 const auto& var = table->variable(0);
1068
1069 const Size nbparents = table->nbrDim() - 1;
1070 const Size nbcol = var.domainSize();
1071
1072 // box-drawing characters
1073 constexpr const char* vbar = "│"; // U+2502 single vertical
1074 constexpr const char* dvbar = "║"; // U+2551 double vertical
1075 constexpr const char* hbar = "─"; // U+2500 single horizontal
1076
1077 const auto repeat_str = [](const char* s, Size n) {
1078 std::string r;
1079 const std::string unit(s);
1080 r.reserve(unit.size() * n);
1081 for (Size i = 0; i < n; i++)
1082 r += unit;
1083 return r;
1084 };
1087
1088 if (nbparents > 0) ss << std::setw(nbparents * (colwidth + 1) - 1) << " " << dvbar;
1089 ss << " " << std::setw(nbcol * (numberwidth + 1) - 3)
1090 << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << vbar;
1091 ss << std::endl;
1092
1093 if (nbparents > 0) {
1094 for (Idx i = 1; i <= nbparents; i++)
1095 ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth)
1096 << (i < (Idx)nbparents ? vbar : dvbar);
1097 }
1098 for (Idx i = 0; i < nbcol; i++)
1099 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << vbar;
1100 ss << std::endl;
1101
1102
1103 if (nbparents > 0) {
1104 for (Idx i = 1; i <= nbparents; i++)
1105 ss << maskparent << (i < (Idx)nbparents ? vbar : dvbar);
1106 }
1107 for (Idx i = 0; i < nbcol; i++)
1108 ss << masknumber << vbar;
1109 ss << std::endl;
1111
1112 const auto drawligne = [&]() {
1113 if (nbparents > 0) {
1114 for (Idx i = 1; i <= nbparents; i++)
1115 ss << std::setw(colwidth) << table->variable(i).label(I.val(i)).substr(0, colwidth)
1116 << (i < (Idx)nbparents ? vbar : dvbar);
1117 }
1118 for (I.setFirstVar(var); !I.end(); I.incVar(var))
1119 ss << " " << std::setw(numberwidth - 1) << table->get(I) << vbar;
1120 I.setFirstVar(var);
1121 ss << std::endl;
1122 };
1123
1124 if (const Size nbrLig = table->domainSize() / var.domainSize(); nbrLig < nbrLigMax * 2 + 1) {
1125 for (I.setFirst(); !I.end(); I.incNotVar(var))
1126 drawligne();
1127 } else {
1128 Size cpt = 0;
1129 for (I.setFirst(); !I.end(); I.incNotVar(var)) {
1130 cpt++;
1131 if (cpt > nbrLigMax) break;
1132 drawligne();
1133 }
1134 ss << "[..." << nbrLig - nbrLigMax * 2 << " more line(s) ...]" << std::endl;
1135 I.setLast();
1136 for (Idx revi = 1; revi < nbrLigMax; revi++)
1137 I.decNotVar(var);
1138 for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
1139 drawligne();
1140 }
1141 }
1142
1143 return ss.str();
1144 }
static Configuration & instance()
The global configuration.
T typed(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type.
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_ERROR, gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::Configuration::instance(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstVar(), gum::Instantiation::setLast(), gum::Configuration::typed(), 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 486 of file tensor_tpl.h.

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

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

1240 {
1242
1244 pot << var;
1246
1247 pot.normalize();
1248
1249 return pot;
1250 }
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:433

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

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(), and gum::Tensor< GUM_SCALAR >::randomCPT().

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 }

References gum::_initTensorOperators__().

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 call graph for this function:
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 263 of file tensor_tpl.h.

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

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

Referenced by stdDev().

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

Member Data Documentation

◆ content_

◆ empty_value_


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