34 template <
typename GUM_SCALAR >
42 template <
typename GUM_SCALAR >
50 template <
typename GUM_SCALAR >
62 template <
typename GUM_SCALAR >
70 template <
typename GUM_SCALAR >
91 template <
typename GUM_SCALAR >
100 template <
typename GUM_SCALAR >
111 template <
typename GUM_SCALAR >
117 template <
typename GUM_SCALAR >
121 this->
content()->newFactory()));
125 template <
typename GUM_SCALAR >
133 template <
typename GUM_SCALAR >
141 template <
typename GUM_SCALAR >
149 template <
typename GUM_SCALAR >
158 template <
typename GUM_SCALAR >
166 [](GUM_SCALAR z, GUM_SCALAR p) {
167 return (p == static_cast< GUM_SCALAR >(1))
169 : (z == static_cast< GUM_SCALAR >(1)) ? p : (p > z ? p : z);
171 static_cast< GUM_SCALAR
>(1));
174 if (res == static_cast< GUM_SCALAR >(1))
181 template <
typename GUM_SCALAR >
189 [](GUM_SCALAR z, GUM_SCALAR p) {
190 return (p == static_cast< GUM_SCALAR >(0))
192 : (z == static_cast< GUM_SCALAR >(0)) ? p : (p < z ? p : z);
194 static_cast< GUM_SCALAR
>(0));
197 if (res == static_cast< GUM_SCALAR >(0))
204 template <
typename GUM_SCALAR >
207 return static_cast< GUM_SCALAR
>(0);
211 [](GUM_SCALAR z, GUM_SCALAR p) {
212 return (p == 0.0) ? z : (z - p * log2(p));
217 template <
typename GUM_SCALAR >
224 template <
typename GUM_SCALAR >
231 template <
typename GUM_SCALAR >
244 "Variable <" << v->name() <<
"> not present in src.");
249 "Variables <" << v->name() <<
"> are not identical.");
255 for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
256 for (
Idx i = 0; i < this->
nbrDim(); i++) {
257 Idst.
chgVal(Isrc.variable(i).name(), Isrc.val(i));
259 this->
set(Idst, src.
get(Isrc));
265 template <
typename GUM_SCALAR >
268 const std::vector< std::string >& mapSrc)
const {
272 if (src.
nbrDim() != mapSrc.size()) {
274 "Potential and vector have not the same dimension.");
280 "Variables " << mapSrc[i] <<
" (in the argument) and " 282 <<
" have not the same dimension.");
288 for (Isrc.
setFirst(); !Isrc.
end(); ++Isrc, ++Idst) {
289 this->
set(Idst, src.
get(Isrc));
295 template <
typename GUM_SCALAR >
297 this->
apply([](GUM_SCALAR x) {
return x * x; });
301 template <
typename GUM_SCALAR >
307 "BNdistance between potentials with different numbers of dimensions");
311 "A variable in the argument does not belong to the potential.");
319 GUM_SCALAR res =
static_cast< GUM_SCALAR
>(0);
321 GUM_SCALAR x = this->
get(inst);
322 GUM_SCALAR y = p.
get(inst);
323 if (static_cast< GUM_SCALAR >(0) == x)
326 if (static_cast< GUM_SCALAR >(0) == y)
329 "The argument has a 0 at " << inst
330 <<
" while the potential has not.")
332 res += x * log2(x / y);
337 template <
typename GUM_SCALAR >
339 this->
apply([](GUM_SCALAR x) {
350 template <
typename GUM_SCALAR >
357 GUM_SCALAR s =
sum();
359 if (s != (GUM_SCALAR)0) {
360 this->
apply([s](GUM_SCALAR x) {
return x / s; });
366 template <
typename GUM_SCALAR >
369 if (this->
_empty_value != static_cast< GUM_SCALAR >(0)) {
373 "Normalization for a potential that sum to 0 in " << *
this);
380 GUM_SCALAR s = (GUM_SCALAR)0.0;
382 s += this->
get(inst);
383 if (s == (GUM_SCALAR)0.0) {
385 "Normalization for a potential that sum to 0 in " << *
this);
387 if (s != (GUM_SCALAR)1.0) {
389 this->
set(inst, this->
get(inst) / s);
396 template <
typename GUM_SCALAR >
399 this->
apply([v](GUM_SCALAR x) {
return x * v; });
403 template <
typename GUM_SCALAR >
406 this->
apply([v](GUM_SCALAR x) {
return x + v; });
410 template <
typename GUM_SCALAR >
419 template <
typename GUM_SCALAR >
429 template <
typename GUM_SCALAR >
438 template <
typename GUM_SCALAR >
446 template <
typename GUM_SCALAR >
456 template <
typename GUM_SCALAR >
466 template <
typename GUM_SCALAR >
476 template <
typename GUM_SCALAR >
486 template <
typename GUM_SCALAR >
489 p.
apply([](GUM_SCALAR x) {
490 if (x != static_cast< GUM_SCALAR >(0))
491 return static_cast< GUM_SCALAR >(1);
493 return static_cast< GUM_SCALAR
>(0);
498 template <
typename GUM_SCALAR >
509 template <
typename GUM_SCALAR >
511 const std::vector< const DiscreteVariable* >& vars)
const {
512 if (vars.size() != this->
nbrDim())
514 "The vector contains " << vars.size() <<
" variables instead of " 515 << this->
nbrDim() <<
".");
516 for (
const auto var : vars) {
519 "A variable in the vector does not belong to the potential.");
524 for (
const auto var : vars)
532 template <
typename GUM_SCALAR >
537 "The variable to put first does not belong to the potential");
540 std::vector< const DiscreteVariable* > vars;
543 if (&(this->
variable(i)) != var) vars.push_back(&(this->variable(i)));
548 template <
typename GUM_SCALAR >
557 template <
typename GUM_SCALAR >
559 if (this->
nbrDim() != 1) {
563 GUM_SCALAR r =
static_cast< GUM_SCALAR
>(
randomProba());
567 if (r <= 0)
return Ip.
val(0);
572 template <
typename GUM_SCALAR >
580 template <
typename GUM_SCALAR >
589 if (this->
get(I) == v) res.
insert(I);
594 template <
typename GUM_SCALAR >
599 template <
typename GUM_SCALAR >
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
aGrUM's Potential is a multi-dimensional array with tensor operators.
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimImplementation< GUM_SCALAR > * _content
The true container.
virtual bool empty() const =0
Returns true if no var is in *this.
virtual Size domainSize() const final
Returns the product of the variables domain size.
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
GUM_SCALAR min() const
min of all elements in the Potential
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
double randomProba()
Returns a random double between 0 and 1 included (i.e.
GUM_SCALAR _empty_value
value of the MultiDimDecorator if no dimension.
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
create a new potential multiplied by v from *this
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Base class for discrete random variable.
GUM_SCALAR KL(const Potential< GUM_SCALAR > &p) const
compute KL divergence between this and p Checks the compatibility and then compute KL divergence ...
Potential< GUM_SCALAR > margProdOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
gum is the global namespace for all aGrUM entities
Abstract base class for all multi dimensionnal containers.
Set< Instantiation > argmin() const
set of instantiation corresponding to the min in the Potential
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
virtual bool empty() const final
Returns true if no var is in *this.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Set< const DiscreteVariable *> _complementVars(const Set< const DiscreteVariable * > &del_vars) const
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
GUM_SCALAR product() const
product of all elements in the Potential
virtual Size domainSize() const =0
Potential< GUM_SCALAR > margProdIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
Header of the Potential class.
const Potential< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map's DAG in output using the Graphviz-dot format.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
void inc()
Operator increment.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
Idx draw() const
get a value at random from a 1-D distribution
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
Potential< GUM_SCALAR > margSumIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using sum as operation (and implementation-optimized operations)
Set< Instantiation > argmax() const
set of instantiation corresponding to the max in the Potential
Potential< GUM_SCALAR > & operator=(const Potential< GUM_SCALAR > &src)
Default constructor.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Multidimensional matrix stored as an array in memory.
GUM_SCALAR max() const
max of all elements in the Potential
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
const Potential< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using max as operation (and implementation-optimized operations)
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
GUM_SCALAR entropy() const
entropy of the Potential
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
Class for assigning/browsing values to tuples of discrete variables.
void normalizeAsCPT() const
normalisation of this as a CPT
Decorator design pattern in order to separate implementations from multidimensional matrix concepts...
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
GUM_SCALAR sum() const
sum of all elements in the Potential
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
virtual const std::string toString() const
the function to be used to add two Potentials
void setFirst()
Assign the first values to the tuple of the Instantiation.
<agrum/multidim/multiDimImplementation.h>
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using max as operation (and implementation-optimized operations)
Size Idx
Type for indexes.
GUM_SCALAR maxNonOne() const
max of all non one elements in the Potential
Potential< GUM_SCALAR > margMinOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using min as operation (and implementation-optimized operations)
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
create a new potential added with v from *this
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
Potential()
Default constructor.
void insert(const Key &k)
Inserts a new element into the set.
Potential< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Potential with a certain variable in first
#define GUM_ERROR(type, msg)
bool end() const
Returns true if the Instantiation reached the end.
virtual void extractFrom(const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)