aGrUM  0.16.0
multiDimAggregator_tpl.h
Go to the documentation of this file.
1 
30 // to ease parser in IDEs
32 
33 namespace gum {
34  namespace aggregator {
35 
36  // Default constructor
37  template < typename GUM_SCALAR >
39  MultiDimReadOnly< GUM_SCALAR >() {
40  GUM_CONSTRUCTOR(MultiDimAggregator);
41  }
42 
43  // Default constructor
44  template < typename GUM_SCALAR >
47  MultiDimReadOnly< GUM_SCALAR >(from) {
48  GUM_CONS_CPY(MultiDimAggregator);
49  }
50 
51  // destructor
52  template < typename GUM_SCALAR >
54  GUM_DESTRUCTOR(MultiDimAggregator);
55  }
56 
57  template < typename GUM_SCALAR >
59  const Instantiation& i) const {
60  if (this->nbrDim() == 1) return _neutralElt();
61 
62  // is i equal to f(f(f(f...(j_,neutral_elt))))
63  Idx current = _neutralElt();
64 
65  bool stop_iteration = false;
66 
67  for (Idx j = 1; j < this->nbrDim(); j++) {
68  current = _fold(
69  this->variable(j), i.val(this->variable(j)), current, stop_iteration);
70 
71  if (stop_iteration) break;
72  }
73 
74  return current;
75  }
76 
77  template < typename GUM_SCALAR >
78  GUM_SCALAR
80  if (this->nbrDim() < 1) {
82  "Not enough variable for an aggregator : " << *this);
83  }
84 
85  const DiscreteVariable& agg = this->variable((Idx)0);
86  auto current = _buildValue(i);
87 
88  // truncate to fit in aggreegator domain size
89  if (current >= agg.domainSize()) current = agg.domainSize() - 1;
90 
91  return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
92  }
93 
94  template < typename GUM_SCALAR >
95  const std::string MultiDimAggregator< GUM_SCALAR >::toString() const {
96  std::stringstream s;
97  s << this->variable(0) << "=" << aggregatorName() << "(";
98 
99  for (Idx i = 1; i < this->nbrDim(); i++) {
100  if (i > 1) s << ",";
101 
102  s << this->variable(i);
103  }
104 
105  s << ")";
106 
107  return s.str();
108  }
109  template < typename GUM_SCALAR >
111  const MultiDimContainer< GUM_SCALAR >& src) const {
112  auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src);
113  if (p == nullptr) {
115  } else {
116  if (p->name() != this->name()) {
118  "Can not copy from a " << p->name() << " to a "
119  << this->name());
120  }
121  }
122  // it the types aree consistant, nothing to do...
123  }
124 
125  // returns the name of the implementation
126  template < typename GUM_SCALAR >
127  const std::string& MultiDimAggregator< GUM_SCALAR >::name() const {
128  static const std::string str = "MultiDimAggregator";
129  return str;
130  }
131 
132  // For friendly displaying the content of the variable.
133  template < typename GUM_SCALAR >
134  INLINE std::ostream& operator<<(std::ostream& s,
136  return s << ag.toString();
137  }
138  } /* namespace aggregator */
139 } /* namespace gum */
virtual ~MultiDimAggregator()
Class destructor.
const std::string toString() const override
const std::string & name() const override
Returns the real name of the multiDimArray.
virtual Idx _fold(const DiscreteVariable &v, Idx i1, Idx i2, bool &stop_iteration) const =0
_fold is applied on value i1 for variable v. the actual result for precedent applications is i2...
virtual std::string aggregatorName() const =0
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const override
Copy of a multiDimICIModel.
Abstract base class for all multi dimensionnal read only structure.
<agrum/multidim/aggregators/multiDimAggregator.h>
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual Size domainSize() const =0
virtual Idx _neutralElt() const =0
_neutralElt() is the result value for the first application of _fold
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
virtual Idx _buildValue(const gum::Instantiation &i) const
by default, _buildValue uses a "fold" scheme and the user has to implement _neutralElt and _fold but ...
Size Idx
Type for indexes.
Definition: types.h:53
virtual GUM_SCALAR get(const Instantiation &i) const override
std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_SCALAR > &ag)
For friendly displaying the content of the array.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55