62 template < GUM_Numeric GUM_SCALAR >
70 template < GUM_Numeric GUM_SCALAR >
72 for (
const auto& var: vars) {
78 template < GUM_Numeric GUM_SCALAR >
86 template < GUM_Numeric GUM_SCALAR >
97 template < GUM_Numeric GUM_SCALAR >
104 template < GUM_Numeric GUM_SCALAR >
124 template < GUM_Numeric GUM_SCALAR >
127 if (&src ==
this)
return *
this;
133 template < GUM_Numeric GUM_SCALAR >
136 if (&src ==
this)
return *
this;
144 template < GUM_Numeric GUM_SCALAR >
150 template < GUM_Numeric GUM_SCALAR >
152 return new Tensor< GUM_SCALAR >(
157 template < GUM_Numeric GUM_SCALAR >
166 template < GUM_Numeric GUM_SCALAR >
175 template < GUM_Numeric GUM_SCALAR >
184 template < GUM_Numeric GUM_SCALAR >
194 template < GUM_Numeric GUM_SCALAR >
202 [](GUM_SCALAR z, GUM_SCALAR p) {
203 return (p ==
static_cast< GUM_SCALAR
>(1)) ? z
204 : (z ==
static_cast< GUM_SCALAR
>(1)) ? p
207 static_cast< GUM_SCALAR
>(1));
215 template < GUM_Numeric GUM_SCALAR >
223 [](GUM_SCALAR z, GUM_SCALAR p) {
224 return (p ==
static_cast< GUM_SCALAR
>(0)) ? z
225 : (z ==
static_cast< GUM_SCALAR
>(0)) ? p
228 static_cast< GUM_SCALAR
>(0));
233 template < GUM_Numeric GUM_SCALAR >
237 return static_cast< GUM_SCALAR
>(0);
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; }
249 template < GUM_Numeric GUM_SCALAR >
262 template < GUM_Numeric GUM_SCALAR >
267 const auto mu =
mean();
269 const auto r = GUM_SCALAR(this->
variable(0).numerical(i.
val(0))) - mu;
277 template < GUM_Numeric GUM_SCALAR >
283 template < GUM_Numeric GUM_SCALAR >
290 template < GUM_Numeric GUM_SCALAR >
291 const Tensor< GUM_SCALAR >&
297 template < GUM_Numeric GUM_SCALAR >
303 template < GUM_Numeric GUM_SCALAR >
304 const Tensor< GUM_SCALAR >&
306 if (src.domainSize() != this->domainSize()) {
310 for (
const auto& v: src.variablesSequence()) {
313 for (
const auto& v: this->variablesSequence()) {
316 "Variable <" << v->name() <<
"> not present in src (" << son <<
").")
319 if (v->toString() != src.variable(v->name()).toString()) {
329 const Idx ndim = src.nbrDim();
330 std::vector< Idx > src_to_dst(ndim);
331 for (
Idx i = 0; i < ndim; i++) {
332 src_to_dst[i] = Idst.pos(this->
variable(Isrc.variable(i).name()));
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));
339 this->set(Idst, src.get(Isrc));
345 template < GUM_Numeric GUM_SCALAR >
348 const std::vector< std::string >& mapSrc)
const {
349 if (src.nbrDim() != this->nbrDim()) {
352 if (src.nbrDim() != mapSrc.size()) {
356 for (
Idx i = 0; i < src.nbrDim(); i++) {
357 if (src.variable(mapSrc[i]).domainSize() != this->variable(i).domainSize()) {
359 "Variables " << mapSrc[i] <<
" (in the argument) and " << this->
variable(i).name()
360 <<
" have not the same dimension.")
362 Isrc.
add(src.variable(mapSrc[i]));
366 for (Isrc.
setFirst(); !Isrc.
end(); ++Isrc, ++Idst) {
367 this->
set(Idst, src.get(Isrc));
373 template < GUM_Numeric GUM_SCALAR >
375 this->
apply([](GUM_SCALAR x) {
return x * x; });
379 template < GUM_Numeric GUM_SCALAR >
381 this->
apply([](GUM_SCALAR x) {
return std::log2(x); });
385 template < GUM_Numeric GUM_SCALAR >
387 this->
apply([](GUM_SCALAR x) {
return (GUM_SCALAR(0) < x) - (x < GUM_SCALAR(0)); });
391 template < GUM_Numeric GUM_SCALAR >
393 if (this->
nbrDim() != p.nbrDim())
395 for (
const auto var: p.variablesSequence()) {
399 for (
const auto var: this->variablesSequence()) {
400 if (!p.contains(*var))
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)
412 if (
static_cast< GUM_SCALAR
>(0) == y)
423 this->
apply([](GUM_SCALAR x) {
424 if (x >= 0)
return x;
432 template < GUM_Numeric GUM_SCALAR >
438 GUM_SCALAR s =
sum();
440 if (s != (GUM_SCALAR)0) {
441 this->
apply([s](GUM_SCALAR x) {
return x / s; });
447 template < GUM_Numeric GUM_SCALAR >
456 if (varId >= this->nbrDim()) {
459 Instantiation inst(*
this);
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) {
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);
479 template < GUM_Numeric GUM_SCALAR >
481 this->
apply([v](GUM_SCALAR x) {
return x * v; });
485 template < GUM_Numeric GUM_SCALAR >
487 this->
apply([v](GUM_SCALAR x) {
return x + v; });
491 template < GUM_Numeric GUM_SCALAR >
493 this->
apply([](GUM_SCALAR x) {
return 1 / x; });
497 template < GUM_Numeric GUM_SCALAR >
500 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
505 if (this->variablesSequence().size() <= del_vars.
size()) {
513 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
sum()); }
516 return Tensor< GUM_SCALAR >(
gum::projectSum(*this->content(), del_vars));
519 template < GUM_Numeric GUM_SCALAR >
522 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
530 if (!del_vars.
exists(var)) {
535 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
product()); }
541 template < GUM_Numeric GUM_SCALAR >
544 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
552 if (!del_vars.
exists(var)) {
557 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
min()); }
563 template < GUM_Numeric GUM_SCALAR >
566 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
574 if (!del_vars.
exists(var)) {
579 if (equal) {
return Tensor< GUM_SCALAR >().fillWith(this->
max()); }
585 template < GUM_Numeric GUM_SCALAR >
588 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
593 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
sum()); }
598 template < GUM_Numeric GUM_SCALAR >
601 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
606 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
product()); }
611 template < GUM_Numeric GUM_SCALAR >
614 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
619 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
min()); }
624 template < GUM_Numeric GUM_SCALAR >
627 return Tensor< GUM_SCALAR >().fillWith(this->
empty_value_);
632 if (kept_vars.
empty()) {
return Tensor< GUM_SCALAR >().fillWith(this->
max()); }
637 template < GUM_Numeric GUM_SCALAR >
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);
647 template < GUM_Numeric GUM_SCALAR >
657 template < GUM_Numeric GUM_SCALAR >
660 if (vars.size() != this->nbrDim())
662 "The argument contains " << vars.size() <<
" variables instead of "
663 << this->nbrDim() <<
".")
664 for (
const auto var: vars) {
669 Tensor< GUM_SCALAR > p;
670 p.beginMultipleChanges();
671 for (
const auto var: vars)
673 p.endMultipleChanges();
674 p.copyFrom(*
this,
nullptr);
679 template < GUM_Numeric GUM_SCALAR >
682 std::vector< const DiscreteVariable* > res;
686 namesToVars.
insert(this->variable(i).name(), &(this->variable(i)));
688 for (
const auto& name: vars) {
689 if (!namesToVars.
exists(name)) {
691 "'" << name <<
"' is a not a name of a variable in this tensor")
693 res.push_back(namesToVars[name]);
698 template < GUM_Numeric GUM_SCALAR >
703 if (&(this->
variable(0)) == var)
return Tensor< GUM_SCALAR >(*
this);
705 std::vector< const DiscreteVariable* > vars;
713 template < GUM_Numeric GUM_SCALAR >
718 if (this->
variable(i).name() == varname) {
724 "The variable '" << varname <<
"' to put first does not belong to the tensor")
728 template < GUM_Numeric GUM_SCALAR >
730 Tensor< GUM_SCALAR > p;
731 p.extractFrom(*
this, inst);
736 template < GUM_Numeric GUM_SCALAR >
738 if (this->
nbrDim() != 1) {
746 if (r <= 0)
return Ip.
val(0);
748 return this->
variable(0).domainSize() - 1;
751 template < GUM_Numeric GUM_SCALAR >
752 std::ostream&
operator<<(std::ostream& out,
const Tensor< GUM_SCALAR >& array) {
753 out << array.toString();
758 template < GUM_Numeric GUM_SCALAR >
772 template < GUM_Numeric GUM_SCALAR >
775 return std::pair(
findAll(m), m);
779 template < GUM_Numeric GUM_SCALAR >
782 return std::pair(
findAll(m), m);
785 template < GUM_Numeric GUM_SCALAR >
789 std::vector< GUM_SCALAR > v;
799 template < GUM_Numeric GUM_SCALAR >
810 template < GUM_Numeric GUM_SCALAR >
820 this->
set(I, distrib[I.
val(0)]);
828 template < GUM_Numeric GUM_SCALAR >
830 if ((alpha < GUM_SCALAR(0.0)) || (alpha > GUM_SCALAR(1.0))) {
833 Tensor< GUM_SCALAR > noise(*
this);
834 return fillWith(
scale(1 - alpha) + noise.randomCPT().scale(alpha)).normalizeAsCPT();
837 template < GUM_Numeric GUM_SCALAR >
839 return Tensor< GUM_SCALAR >(*this).abs();
842 template < GUM_Numeric GUM_SCALAR >
844 return Tensor< GUM_SCALAR >(*this).sq();
847 template < GUM_Numeric GUM_SCALAR >
849 return Tensor< GUM_SCALAR >(*this).log2();
852 template < GUM_Numeric GUM_SCALAR >
854 return Tensor< GUM_SCALAR >(*this).sgn();
857 template < GUM_Numeric GUM_SCALAR >
864 template < GUM_Numeric GUM_SCALAR >
866 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).translate(p2.empty_value_);
867 if (this->
empty())
return Tensor< GUM_SCALAR >(p2).translate(this->
empty_value_);
869 return Tensor< GUM_SCALAR >(*this->
content() + *p2.content());
873 template < GUM_Numeric GUM_SCALAR >
875 return Tensor< GUM_SCALAR >(*this).translate(v);
879 template < GUM_Numeric GUM_SCALAR >
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);
887 template < GUM_Numeric GUM_SCALAR >
891 if (this->
variable(0) != p2.variable(0))
893 Tensor< GUM_SCALAR > res(*
this);
896 res.set(I, std::max(res.get(I), p2.get(I)));
902 template < GUM_Numeric GUM_SCALAR >
906 if (this->
variable(0) != p2.variable(0))
908 Tensor< GUM_SCALAR > res(*
this);
911 res.set(I, std::min(res.get(I), p2.get(I)));
917 template < GUM_Numeric GUM_SCALAR >
921 Tensor< GUM_SCALAR > res(*
this);
924 res.set(I, 1 - res.get(I));
930 template < GUM_Numeric GUM_SCALAR >
932 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).translate(-p2.empty_value_);
934 auto p = Tensor< GUM_SCALAR >(p2);
935 p.apply([
this](GUM_SCALAR x) {
return this->
empty_value_ - x; });
938 return Tensor< GUM_SCALAR >(*this->
content() - *p2.content());
942 template < GUM_Numeric GUM_SCALAR >
944 return Tensor< GUM_SCALAR >(*this).translate(-v);
948 template < GUM_Numeric GUM_SCALAR >
950 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).scale(p2.empty_value_);
953 return Tensor< GUM_SCALAR >(*this->
content() * *p2.content());
957 template < GUM_Numeric GUM_SCALAR >
959 return Tensor< GUM_SCALAR >(*this).scale(v);
963 template < GUM_Numeric GUM_SCALAR >
965 if (p2.empty())
return Tensor< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
967 auto p = Tensor< GUM_SCALAR >(p2);
968 p.apply([
this](GUM_SCALAR x) {
return this->
empty_value_ / x; });
971 return Tensor< GUM_SCALAR >(*this->
content() / *p2.content());
975 template < GUM_Numeric GUM_SCALAR >
977 return Tensor< GUM_SCALAR >(*this).scale(1 / v);
980 template < GUM_Numeric GUM_SCALAR >
986 template < GUM_Numeric GUM_SCALAR >
992 template < GUM_Numeric GUM_SCALAR >
998 template < GUM_Numeric GUM_SCALAR >
1004 template < GUM_Numeric GUM_SCALAR >
1010 template < GUM_Numeric GUM_SCALAR >
1016 template < GUM_Numeric GUM_SCALAR >
1022 template < GUM_Numeric GUM_SCALAR >
1028 template < GUM_Numeric GUM_SCALAR >
1030 if (this->
empty()) {
1031 if (r.empty())
return this->
empty_value_ == r.empty_value_;
1034 if (r.empty())
return false;
1035 else return (*this->
content_) == (*r.content_);
1039 template < GUM_Numeric GUM_SCALAR >
1042 std::stringstream ss;
1044 if (table->nbrDim() == 0) {
1046 ss <<
"[" << this->
get(I) <<
"]";
1050 const auto sizeFromConfiguration = [](
const std::string& key, std::int64_t minimum) {
1052 if (value < minimum) {
1054 "The key tensor." << key <<
" of the configuration must be at least " << minimum
1057 return static_cast< Size >(value);
1060 const Size colwidth = 6;
1061 const Size numberwidth = sizeFromConfiguration(
"columnWidthConsole", 6);
1062 const Size nbrLigMax = sizeFromConfiguration(
"maxRowsConsole", 1);
1064 ss << std::left << std::fixed << std::endl;
1065 ss.precision(numberwidth - 5);
1067 const auto& var = table->variable(0);
1069 const Size nbparents = table->nbrDim() - 1;
1070 const Size nbcol = var.domainSize();
1073 constexpr const char* vbar =
"│";
1074 constexpr const char* dvbar =
"â•‘";
1075 constexpr const char* hbar =
"─";
1077 const auto repeat_str = [](
const char* s,
Size n) {
1079 const std::string unit(s);
1080 r.reserve(unit.size() * n);
1081 for (
Size i = 0; i < n; i++)
1085 const std::string maskparent = repeat_str(hbar, colwidth);
1086 const std::string masknumber = repeat_str(hbar, numberwidth);
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;
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);
1098 for (
Idx i = 0; i < nbcol; i++)
1099 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) << vbar;
1103 if (nbparents > 0) {
1104 for (
Idx i = 1; i <= nbparents; i++)
1105 ss << maskparent << (i < (
Idx)nbparents ? vbar : dvbar);
1107 for (
Idx i = 0; i < nbcol; i++)
1108 ss << masknumber << vbar;
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);
1119 ss <<
" " << std::setw(numberwidth - 1) << table->get(I) << vbar;
1124 if (
const Size nbrLig = table->domainSize() / var.domainSize(); nbrLig < nbrLigMax * 2 + 1) {
1131 if (cpt > nbrLigMax)
break;
1134 ss <<
"[..." << nbrLig - nbrLigMax * 2 <<
" more line(s) ...]" << std::endl;
1136 for (
Idx revi = 1; revi < nbrLigMax; revi++)
1146 template < GUM_Numeric GUM_SCALAR >
1159 template < GUM_Numeric GUM_SCALAR >
1160 Tensor< GUM_SCALAR >
1164 "val2 (" << val2 <<
") must be greater than val1 (" << val1 <<
").")
1173 for (
Idx i = i1; i <= i2; i++) {
1180 template < GUM_Numeric GUM_SCALAR >
1196 template < GUM_Numeric GUM_SCALAR >
1199 if (i == 0) {
return evEq(v, val); }
1205 for (
Idx i2 = 0; i2 < i; i2++) {
1212 template < GUM_Numeric GUM_SCALAR >
1214 return this->
content()->realSize() *
sizeof(GUM_SCALAR);
1217 template < GUM_Numeric GUM_SCALAR >
1220 Tensor< GUM_SCALAR > pot;
1222 pot.beginMultipleChanges();
1224 pot.endMultipleChanges(GUM_SCALAR(0.0));
1228 pot.set(I, GUM_SCALAR(1.0));
1233 template < GUM_Numeric GUM_SCALAR >
1235 std::string_view value) {
1239 template < GUM_Numeric GUM_SCALAR >
1241 Tensor< GUM_SCALAR > pot;
1243 pot.beginMultipleChanges();
1245 pot.endMultipleChanges(GUM_SCALAR(1.0));
Exception base for argument error.
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.
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.
Instantiation()
Default constructor: creates an empty tuple.
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
bool contains(const DiscreteVariable &var) const 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.
Exception : out of bound.
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.
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
GUM_SHARED_PUBLIC bool isNumerical(std::string_view val)
return true is a string contains a numerical (double) value
GUM_SHARED_PUBLIC 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
bool isCloseToOne(T x, T tol=static_cast< T >(config().epsilonFine()))
Is |x - 1| <= tol ? By default, tol is the key core.epsilonFine of the configuration.
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
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.