61 template < GUM_Numeric GUM_SCALAR >
69 template < GUM_Numeric GUM_SCALAR >
71 for (
const auto& var: vars) {
77 template < GUM_Numeric GUM_SCALAR >
85 template < GUM_Numeric GUM_SCALAR >
96 template < GUM_Numeric GUM_SCALAR >
103 template < GUM_Numeric GUM_SCALAR >
123 template < GUM_Numeric GUM_SCALAR >
126 if (&src ==
this)
return *
this;
132 template < GUM_Numeric GUM_SCALAR >
135 if (&src ==
this)
return *
this;
143 template < GUM_Numeric GUM_SCALAR >
149 template < GUM_Numeric GUM_SCALAR >
151 return new Tensor< GUM_SCALAR >(
156 template < GUM_Numeric GUM_SCALAR >
165 template < GUM_Numeric GUM_SCALAR >
174 template < GUM_Numeric GUM_SCALAR >
183 template < GUM_Numeric GUM_SCALAR >
193 template < GUM_Numeric GUM_SCALAR >
201 [](GUM_SCALAR z, GUM_SCALAR p) {
202 return (p ==
static_cast< GUM_SCALAR
>(1)) ? z
203 : (z ==
static_cast< GUM_SCALAR
>(1)) ? p
206 static_cast< GUM_SCALAR
>(1));
214 template < GUM_Numeric GUM_SCALAR >
222 [](GUM_SCALAR z, GUM_SCALAR p) {
223 return (p ==
static_cast< GUM_SCALAR
>(0)) ? z
224 : (z ==
static_cast< GUM_SCALAR
>(0)) ? p
227 static_cast< GUM_SCALAR
>(0));
232 template < GUM_Numeric GUM_SCALAR >
236 return static_cast< GUM_SCALAR
>(0);
241 for (i.setFirst(); !i.end(); i.inc()) {
242 const GUM_SCALAR v_f = f(i);
243 if (v_f != GUM_SCALAR(0.0)) { res += this->
get(i) * v_f; }
248 template < GUM_Numeric GUM_SCALAR >
254 return GUM_SCALAR(this->
variable(0).numerical(i.
val(0)));
261 template < GUM_Numeric GUM_SCALAR >
268 const auto r = GUM_SCALAR(this->
variable(0).numerical(i.
val(0))) - mu;
276 template < GUM_Numeric GUM_SCALAR >
282 template < GUM_Numeric GUM_SCALAR >
289 template < GUM_Numeric GUM_SCALAR >
290 const Tensor< GUM_SCALAR >&
296 template < GUM_Numeric GUM_SCALAR >
302 template < GUM_Numeric GUM_SCALAR >
303 const Tensor< GUM_SCALAR >&
305 if (src.domainSize() != this->domainSize()) {
309 for (
const auto& v: src.variablesSequence()) {
312 for (
const auto& v: this->variablesSequence()) {
315 "Variable <" << v->name() <<
"> not present in src (" << son <<
").")
318 if (v->toString() != src.variable(v->name()).toString()) {
328 const Idx ndim = src.nbrDim();
329 std::vector< Idx > src_to_dst(ndim);
330 for (
Idx i = 0; i < ndim; i++) {
331 src_to_dst[i] = Idst.pos(this->
variable(Isrc.variable(i).name()));
334 for (Isrc.setFirst(); !Isrc.end(); ++Isrc) {
335 for (
Idx i = 0; i < ndim; i++) {
336 Idst.chgVal(src_to_dst[i], Isrc.val(i));
338 this->set(Idst, src.get(Isrc));
344 template < GUM_Numeric GUM_SCALAR >
347 const std::vector< std::string >& mapSrc)
const {
348 if (src.nbrDim() != this->nbrDim()) {
351 if (src.nbrDim() != mapSrc.size()) {
355 for (
Idx i = 0; i < src.nbrDim(); i++) {
356 if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
358 "Variables " << mapSrc[i] <<
" (in the argument) and " << this->
variable(i).name()
359 <<
" have not the same dimension.")
361 Isrc.
add(src.variable(mapSrc[i]));
365 for (Isrc.
setFirst(); !Isrc.
end(); ++Isrc, ++Idst) {
366 this->
set(Idst, src.get(Isrc));
372 template < GUM_Numeric GUM_SCALAR >
374 this->
apply([](GUM_SCALAR x) {
return x * x; });
378 template < GUM_Numeric GUM_SCALAR >
380 this->
apply([](GUM_SCALAR x) {
return std::log2(x); });
384 template < GUM_Numeric GUM_SCALAR >
386 this->
apply([](GUM_SCALAR x) {
return (GUM_SCALAR(0) < x) - (x < GUM_SCALAR(0)); });
390 template < GUM_Numeric GUM_SCALAR >
392 if (this->
nbrDim() != p.nbrDim())
394 for (
const auto var: p.variablesSequence()) {
398 for (
const auto var: this->variablesSequence()) {
399 if (!p.contains(*var))
404 auto res =
static_cast< GUM_SCALAR
>(0);
405 for (inst.setFirst(); !inst.end(); inst.inc()) {
406 GUM_SCALAR x = this->
get(inst);
407 GUM_SCALAR y = p.get(inst);
408 if (
static_cast< GUM_SCALAR
>(0) == x)
411 if (
static_cast< GUM_SCALAR
>(0) == y)
422 this->
apply([](GUM_SCALAR x) {
423 if (x >= 0)
return x;
431 template < GUM_Numeric GUM_SCALAR >
437 GUM_SCALAR s =
sum();
439 if (s != (GUM_SCALAR)0) {
440 this->
apply([s](GUM_SCALAR x) {
return x / s; });
446 template < GUM_Numeric GUM_SCALAR >
449 if (this->
empty_value_ !=
static_cast< GUM_SCALAR
>(0)) {
459 const auto& v = this->variable(varId);
461 for (inst.setFirst(); !inst.end(); inst.incNotVar(v)) {
462 auto s = (GUM_SCALAR)0.0;
463 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
464 s += this->
get(inst);
465 if (s == (GUM_SCALAR)0.0) {
468 if (s != (GUM_SCALAR)1.0) {
469 for (inst.setFirstVar(v); !inst.end(); inst.incVar(v))
470 this->set(inst, this->get(inst) / s);
478 template < GUM_Numeric GUM_SCALAR >
480 this->
apply([v](GUM_SCALAR x) {
return x * v; });
484 template < GUM_Numeric GUM_SCALAR >
486 this->
apply([v](GUM_SCALAR x) {
return x + v; });
490 template < GUM_Numeric GUM_SCALAR >
492 this->
apply([](GUM_SCALAR x) {
return 1 / x; });
496 template < GUM_Numeric GUM_SCALAR >
499 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
512 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->sum()); }
515 return Tensor< GUM_SCALAR >(
gum::projectSum(*this->content(), del_vars));
518 template < GUM_Numeric GUM_SCALAR >
521 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
529 if (!del_vars.
exists(var)) {
534 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
product()); }
540 template < GUM_Numeric GUM_SCALAR >
543 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
551 if (!del_vars.
exists(var)) {
556 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
min()); }
562 template < GUM_Numeric GUM_SCALAR >
565 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
573 if (!del_vars.
exists(var)) {
578 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
max()); }
584 template < GUM_Numeric GUM_SCALAR >
587 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
592 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
sum()); }
597 template < GUM_Numeric GUM_SCALAR >
600 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
605 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
product()); }
610 template < GUM_Numeric GUM_SCALAR >
613 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
618 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
min()); }
623 template < GUM_Numeric GUM_SCALAR >
626 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
631 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
max()); }
636 template < GUM_Numeric GUM_SCALAR >
638 auto p = Tensor< GUM_SCALAR >(*
this);
639 p.apply([](GUM_SCALAR x) {
640 if (x !=
static_cast< GUM_SCALAR
>(0))
return static_cast< GUM_SCALAR
>(1);
641 else return static_cast< GUM_SCALAR
>(0);
646 template < GUM_Numeric GUM_SCALAR >
656 template < GUM_Numeric GUM_SCALAR >
659 if (vars.size() != this->nbrDim())
661 "The argument contains " << vars.size() <<
" variables instead of "
662 << this->nbrDim() <<
".")
663 for (
const auto var: vars) {
668 Tensor< GUM_SCALAR > p;
669 p.beginMultipleChanges();
670 for (
const auto var: vars)
672 p.endMultipleChanges();
673 p.copyFrom(*
this,
nullptr);
678 template < GUM_Numeric GUM_SCALAR >
681 std::vector< const DiscreteVariable* > res;
685 namesToVars.
insert(this->variable(i).name(), &(this->variable(i)));
687 for (
const auto& name: vars) {
688 if (!namesToVars.
exists(name)) {
690 "'" << name <<
"' is a not a name of a variable in this tensor")
692 res.push_back(namesToVars[name]);
697 template < GUM_Numeric GUM_SCALAR >
702 if (&(this->
variable(0)) == var)
return Tensor< GUM_SCALAR >(*
this);
704 std::vector< const DiscreteVariable* > vars;
712 template < GUM_Numeric GUM_SCALAR >
717 if (this->
variable(i).name() == varname) {
723 "The variable '" << varname <<
"' to put first does not belong to the tensor")
727 template < GUM_Numeric GUM_SCALAR >
729 Tensor< GUM_SCALAR > p;
730 p.extractFrom(*
this, inst);
735 template < GUM_Numeric GUM_SCALAR >
737 if (this->
nbrDim() != 1) {
745 if (r <= 0)
return Ip.
val(0);
747 return this->
variable(0).domainSize() - 1;
750 template < GUM_Numeric GUM_SCALAR >
751 std::ostream&
operator<<(std::ostream& out,
const Tensor< GUM_SCALAR >& array) {
752 out << array.toString();
757 template < GUM_Numeric GUM_SCALAR >
771 template < GUM_Numeric GUM_SCALAR >
774 return std::pair(
findAll(m), m);
778 template < GUM_Numeric GUM_SCALAR >
781 return std::pair(
findAll(m), m);
784 template < GUM_Numeric GUM_SCALAR >
788 std::vector< GUM_SCALAR > v;
798 template < GUM_Numeric GUM_SCALAR >
809 template < GUM_Numeric GUM_SCALAR >
819 this->
set(I, distrib[I.
val(0)]);
827 template < GUM_Numeric GUM_SCALAR >
829 if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
832 Tensor< GUM_SCALAR > noise(*
this);
833 return fillWith(
scale(1 - alpha) + noise.randomCPT().scale(alpha)).normalizeAsCPT();
836 template < GUM_Numeric GUM_SCALAR >
838 return Tensor< GUM_SCALAR >(*this).abs();
841 template < GUM_Numeric GUM_SCALAR >
843 return Tensor< GUM_SCALAR >(*this).sq();
846 template < GUM_Numeric GUM_SCALAR >
848 return Tensor< GUM_SCALAR >(*this).log2();
851 template < GUM_Numeric GUM_SCALAR >
853 return Tensor< GUM_SCALAR >(*this).sgn();
856 template < GUM_Numeric GUM_SCALAR >
863 template < GUM_Numeric GUM_SCALAR >
865 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).translate(p2.empty_value_);
866 if (this->
empty())
return Tensor< GUM_SCALAR >(p2).translate(this->
empty_value_);
868 return Tensor< GUM_SCALAR >(*this->
content() + *p2.content());
872 template < GUM_Numeric GUM_SCALAR >
874 return Tensor< GUM_SCALAR >(*this).translate(v);
878 template < GUM_Numeric GUM_SCALAR >
880 if (this->
nbrDim() != 1)
return false;
881 if (this->
sum() <= 0.0)
return false;
882 return (this->
min() >= 0.0) && (this->
max() <= 1.0);
886 template < GUM_Numeric GUM_SCALAR >
890 if (this->
variable(0) != p2.variable(0))
892 Tensor< GUM_SCALAR > res(*
this);
895 res.set(I, std::max(res.get(I), p2.get(I)));
901 template < GUM_Numeric GUM_SCALAR >
905 if (this->
variable(0) != p2.variable(0))
907 Tensor< GUM_SCALAR > res(*
this);
910 res.set(I, std::min(res.get(I), p2.get(I)));
916 template < GUM_Numeric GUM_SCALAR >
920 Tensor< GUM_SCALAR > res(*
this);
923 res.set(I, 1 - res.get(I));
929 template < GUM_Numeric GUM_SCALAR >
931 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).translate(-p2.empty_value_);
933 auto p = Tensor< GUM_SCALAR >(p2);
934 p.apply([
this](GUM_SCALAR x) {
return this->
empty_value_ - x; });
937 return Tensor< GUM_SCALAR >(*this->
content() - *p2.content());
941 template < GUM_Numeric GUM_SCALAR >
943 return Tensor< GUM_SCALAR >(*this).translate(-v);
947 template < GUM_Numeric GUM_SCALAR >
949 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).scale(p2.empty_value_);
952 return Tensor< GUM_SCALAR >(*this->
content() * *p2.content());
956 template < GUM_Numeric GUM_SCALAR >
958 return Tensor< GUM_SCALAR >(*this).scale(v);
962 template < GUM_Numeric GUM_SCALAR >
964 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
966 auto p = Tensor< GUM_SCALAR >(p2);
967 p.apply([
this](GUM_SCALAR x) {
return this->
empty_value_ / x; });
970 return Tensor< GUM_SCALAR >(*this->
content() / *p2.content());
974 template < GUM_Numeric GUM_SCALAR >
976 return Tensor< GUM_SCALAR >(*this).scale(1 / v);
979 template < GUM_Numeric GUM_SCALAR >
985 template < GUM_Numeric GUM_SCALAR >
991 template < GUM_Numeric GUM_SCALAR >
997 template < GUM_Numeric GUM_SCALAR >
1003 template < GUM_Numeric GUM_SCALAR >
1009 template < GUM_Numeric GUM_SCALAR >
1015 template < GUM_Numeric GUM_SCALAR >
1021 template < GUM_Numeric GUM_SCALAR >
1027 template < GUM_Numeric GUM_SCALAR >
1029 if (this->
empty()) {
1030 if (r.empty())
return this->
empty_value_ == r.empty_value_;
1033 if (r.empty())
return false;
1034 else return (*this->
content_) == (*r.content_);
1038 template < GUM_Numeric GUM_SCALAR >
1041 std::stringstream ss;
1043 if (table->nbrDim() == 0) {
1045 ss <<
"[" << this->
get(I) <<
"]";
1048 const Size colwidth = 6;
1049 const Size numberwidth = 9;
1050 const Size nbrLigMax = 6;
1052 ss << std::left << std::fixed << std::endl;
1053 ss.precision(numberwidth - 5);
1055 const auto& var = table->variable(0);
1057 const Size nbparents = table->nbrDim() - 1;
1058 const Size nbcol = var.domainSize();
1061 constexpr const char* vbar =
"│";
1062 constexpr const char* dvbar =
"â•‘";
1063 constexpr const char* hbar =
"─";
1065 const auto repeat_str = [](
const char* s,
Size n) {
1067 const std::string unit(s);
1068 r.reserve(unit.size() * n);
1069 for (
Size i = 0; i < n; i++)
1073 const std::string maskparent = repeat_str(hbar, colwidth);
1074 const std::string masknumber = repeat_str(hbar, numberwidth);
1076 if (nbparents > 0) ss << std::setw(nbparents * (colwidth + 1) - 1) <<
" " << dvbar;
1077 ss <<
" " << std::setw(nbcol * (numberwidth + 1) - 3)
1078 << var.name().substr(0, nbcol * (numberwidth + 1) - 3) << vbar;
1081 if (nbparents > 0) {
1082 for (
Idx i = 1; i <= nbparents; i++)
1083 ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth)
1084 << (i < (
Idx)nbparents ? vbar : dvbar);
1086 for (
Idx i = 0; i < nbcol; i++)
1087 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << vbar;
1091 if (nbparents > 0) {
1092 for (
Idx i = 1; i <= nbparents; i++)
1093 ss << maskparent << (i < (
Idx)nbparents ? vbar : dvbar);
1095 for (
Idx i = 0; i < nbcol; i++)
1096 ss << masknumber << vbar;
1100 const auto drawligne = [&]() {
1101 if (nbparents > 0) {
1102 for (
Idx i = 1; i <= nbparents; i++)
1103 ss << std::setw(colwidth) << table->variable(i).label(I.
val(i)).substr(0, colwidth)
1104 << (i < (
Idx)nbparents ? vbar : dvbar);
1107 ss <<
" " << std::setw(numberwidth - 1) << table->get(I) << vbar;
1112 if (
const Size nbrLig = table->domainSize() / var.domainSize(); nbrLig < nbrLigMax * 2 + 1) {
1119 if (cpt > nbrLigMax)
break;
1122 ss <<
"[..." << nbrLig - nbrLigMax * 2 <<
" more line(s) ...]" << std::endl;
1124 for (
Idx revi = 1; revi < nbrLigMax; revi++)
1134 template < GUM_Numeric GUM_SCALAR >
1147 template < GUM_Numeric GUM_SCALAR >
1148 Tensor< GUM_SCALAR >
1152 "val2 (" << val2 <<
") must be greater than val1 (" << val1 <<
").")
1161 for (
Idx i = i1; i <= i2; i++) {
1168 template < GUM_Numeric GUM_SCALAR >
1184 template < GUM_Numeric GUM_SCALAR >
1187 if (i == 0) {
return evEq(v, val); }
1193 for (
Idx i2 = 0; i2 < i; i2++) {
1200 template < GUM_Numeric GUM_SCALAR >
1202 return this->
content()->realSize() *
sizeof(GUM_SCALAR);
1205 template < GUM_Numeric GUM_SCALAR >
1208 Tensor< GUM_SCALAR > pot;
1210 pot.beginMultipleChanges();
1212 pot.endMultipleChanges(GUM_SCALAR(0.0));
1216 pot.set(I, GUM_SCALAR(1.0));
1221 template < GUM_Numeric GUM_SCALAR >
1223 std::string_view value) {
1227 template < GUM_Numeric GUM_SCALAR >
1229 Tensor< GUM_SCALAR > pot;
1231 pot.beginMultipleChanges();
1233 pot.endMultipleChanges(GUM_SCALAR(1.0));
Exception base for argument error.
Base class for discrete random variable.
virtual Idx closestIndex(double val) const =0
for numerical variables, returns the closest index for the value
virtual Idx index(std::string_view label) const =0
virtual Size domainSize() const =0
Exception : fatal (unknown ?) error.
The class for generic Hash Tables.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
void unsetEnd()
Alias for unsetOverflow().
void decNotVar(const DiscreteVariable &v)
Operator decrement for vars which are not v.
void setLast()
Assign the last values in the Instantiation.
Exception: at least one argument passed to a function is not what was expected.
Multidimensional matrix stored as an array in memory.
virtual void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const
Basic copy of a MultiDimContainer.
virtual const MultiDimImplementation< GUM_ELEMENT > * content() const =0
Returns the implementation for this object (may be *this).
void populate(const std::vector< GUM_SCALAR > &v) const final
const MultiDimImplementation< GUM_SCALAR > * content() const final
void fill(const GUM_SCALAR &d) const final
const Sequence< const DiscreteVariable * > & variablesSequence() const final
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
const DiscreteVariable & variable(Idx) const final
MultiDimDecorator< GUM_ELEMENT > & operator=(const MultiDimDecorator &from) noexcept
copy operator
void beginMultipleChanges() final
GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
void set(const Instantiation &i, const GUM_SCALAR &value) const final
MultiDimImplementation< GUM_SCALAR > * content_
void add(const DiscreteVariable &v) final
Size domainSize() const final
void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
GUM_SCALAR get(const Instantiation &i) const final
void endMultipleChanges() final
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
virtual bool empty() const =0
Returns true if no var is in *this.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
bool empty() const noexcept
Indicates whether the set is the empty set.
void insert(const Key &k)
Inserts a new element into the set.
Size size() const noexcept
Returns the number of elements in the set.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
GUM_SCALAR mean() const
compute the mean of a numerical discrete random variable @raise
Tensor< GUM_SCALAR > & operator-=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
const Tensor< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
Tensor< GUM_SCALAR > & operator/=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Tensor< GUM_SCALAR > sumOut(const gum::VariableSet &del_vars) const
Projection using sum as operation (and implementation-optimized operations).
GUM_SCALAR variance() const
compute the variance of a numerical discrete random variable @raise
Tensor()
Default constructor.
Idx draw() const
get a value at random from a 1-D distribution
GUM_SCALAR entropy() const
entropy of the Tensor
Tensor< GUM_SCALAR > operator|(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
const Tensor< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Tensor< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Tensor extracted from *this given a partial instantiation
const Tensor< GUM_SCALAR > & random() const
generate a random Tensor with each parameter in [0,1]
Tensor< GUM_SCALAR > new_sq() const
Create a new tensor and apply $x^2$ on every element of the container.
Tensor< GUM_SCALAR > isNonZeroMap() const
create a boolean-like tensor using the predicate isNonZero
Tensor< GUM_SCALAR > minOut(const gum::VariableSet &del_vars) const
Projection using min as operation (and implementation-optimized operations).
GUM_SCALAR stdDev() const
compute the stdDev of a numerical discrete random variable
Tensor< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Tensor with a certain variable in first
Tensor< GUM_SCALAR > maxIn(const gum::VariableSet &kept_vars) const
Projection using max as operation (and implementation-optimized operations).
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator
Tensor< GUM_SCALAR > sumIn(const gum::VariableSet &kept_vars) const
Projection using sum as operation (and implementation-optimized operations).
Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Tensor with another order
std::pair< Set< Instantiation >, GUM_SCALAR > argmax() const
Pair of the set of instantiation corresponding to the max and this max in the Tensor.
Tensor< GUM_SCALAR > operator&(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
GUM_SCALAR max() const
max of all elements in the Tensor
Tensor< GUM_SCALAR > * newFactory() const final
const Tensor< GUM_SCALAR > & sgn() const
apply sgn(x)$ on every element of the container
Size memoryFootprint() const
compute the (approximated) footprint in memory of the tensor
Tensor< GUM_SCALAR > new_sgn() const
Create a new tensor and apply sgn(x)$ on every element of the container.
GUM_SCALAR maxNonOne() const
max of all non one elements in the Tensor
Tensor< GUM_SCALAR > prodIn(const gum::VariableSet &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
bool operator==(const Tensor< GUM_SCALAR > &r) const
the function to be used to add two Tensors
static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable &var, Idx value)
Tensor< GUM_SCALAR > new_log2() const
Create a new tensor and apply $log_2(x)$ on every element of the container.
static Tensor< GUM_SCALAR > uniformTensor(const DiscreteVariable &var)
static Tensor< GUM_SCALAR > evGt(const DiscreteVariable &v, double val)
numerical evidence generator
GUM_SCALAR minNonZero() const
min of all non zero elements in the Tensor
~Tensor() final
Destructor.
GUM_SCALAR KL(const Tensor< GUM_SCALAR > &p) const
compute KL divergence between this and p Checks the compatibility and then compute KL divergence
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Tensor
static Tensor< GUM_SCALAR > evIn(const DiscreteVariable &v, double val1, double val2)
numerical evidence generator
Tensor< GUM_SCALAR > prodOut(const gum::VariableSet &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
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...
Tensor< GUM_SCALAR > maxOut(const gum::VariableSet &del_vars) const
Projection using max as operation (and implementation-optimized operations).
const Tensor< GUM_SCALAR > & noising(GUM_SCALAR alpha) const
add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
bool isEvidence() const
is an evidence ? (marginal-like but has not to sum to 1)
Tensor< GUM_SCALAR > & operator=(const Tensor< GUM_SCALAR > &src)
Default constructor.
gum::VariableSet _complementVars_(const gum::VariableSet &del_vars) const
Tensor< GUM_SCALAR > operator/(const Tensor< GUM_SCALAR > &p2) const
the function to be used to divide two Tensors
const Tensor< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
GUM_SCALAR expectedValue(std::function< GUM_SCALAR(const gum::Instantiation &) >) const
ExpectedValue computes the expectation of f over *this.
Tensor< GUM_SCALAR > new_abs() const
Create a new tensor and apply abs on every element of the container.
const Tensor< GUM_SCALAR > & inverse() const
the function to inverse (each value of) *this
Tensor< GUM_SCALAR > & operator<<(const DiscreteVariable &v)
the function to be used to add two Tensors
Tensor< GUM_SCALAR > minIn(const gum::VariableSet &kept_vars) const
Projection using min as operation (and implementation-optimized operations).
Tensor< GUM_SCALAR > operator*(const Tensor< GUM_SCALAR > &p2) const
the function to be used to multiply two Tensors
Tensor< GUM_SCALAR > operator-(const Tensor< GUM_SCALAR > &p2) const
the function to be used to subtract two Tensors
const Tensor< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
GUM_SCALAR min() const
min of all elements in the Tensor
Tensor< GUM_SCALAR > & operator*=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
GUM_SCALAR product() const
product of all elements 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.
const Tensor< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
std::string toString() const final
the function to be used to add two Tensors
const Tensor< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Tensor
GUM_SCALAR sum() const
sum of all elements in the Tensor
static Tensor< GUM_SCALAR > evLt(const DiscreteVariable &v, double val)
numerical evidence generator
const Tensor< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Tensor< GUM_SCALAR > & operator+=(const Tensor< GUM_SCALAR > &r)
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
const Tensor< GUM_SCALAR > & randomDistribution() const
generate a random Distribution in the Tensor
Complete concept for GUM_SCALAR template parameter.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
bool contains(std::string_view s, std::string_view needle)
true if needle in s
bool isNumerical(std::string_view val)
return true is a string contains a numerical (double) value
double randomProba()
Returns a random double between 0 and 1 included (i.e.
std::vector< GUM_SCALAR > randomDistribution(Size n)
Return a random discrete distribution.
gum is the global namespace for all aGrUM entities
Set< const DiscreteVariable * > VariableSet
GUM_ELEMENT projectProduct(const MultiDimImplementation< GUM_ELEMENT > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
GUM_ELEMENT projectSum(const MultiDimImplementation< GUM_ELEMENT > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
GUM_ELEMENT projectMin(const MultiDimImplementation< GUM_ELEMENT > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
bool isCloseToOne(T x, T tol=T(1e-9))
GUM_ELEMENT projectMax(const MultiDimImplementation< GUM_ELEMENT > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
Header of the Tensor class.