aGrUM  0.14.2
multiDimAggregator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // to ease parser in IDEs
29 
30 namespace gum {
31  namespace aggregator {
32 
33  // Default constructor
34  template < typename GUM_SCALAR >
36  MultiDimReadOnly< GUM_SCALAR >() {
37  GUM_CONSTRUCTOR(MultiDimAggregator);
38  }
39 
40  // Default constructor
41  template < typename GUM_SCALAR >
44  MultiDimReadOnly< GUM_SCALAR >(from) {
45  GUM_CONS_CPY(MultiDimAggregator);
46  }
47 
48  // destructor
49  template < typename GUM_SCALAR >
51  GUM_DESTRUCTOR(MultiDimAggregator);
52  }
53 
54  template < typename GUM_SCALAR >
56  const Instantiation& i) const {
57  if (this->nbrDim() == 1) return _neutralElt();
58 
59  // is i equal to f(f(f(f...(j_,neutral_elt))))
60  Idx current = _neutralElt();
61 
62  bool stop_iteration = false;
63 
64  for (Idx j = 1; j < this->nbrDim(); j++) {
65  current = _fold(
66  this->variable(j), i.val(this->variable(j)), current, stop_iteration);
67 
68  if (stop_iteration) break;
69  }
70 
71  return current;
72  }
73 
74  template < typename GUM_SCALAR >
75  GUM_SCALAR
77  if (this->nbrDim() < 1) {
79  "Not enough variable for an aggregator : " << *this);
80  }
81 
82  const DiscreteVariable& agg = this->variable((Idx)0);
83  auto current = _buildValue(i);
84 
85  // truncate to fit in aggreegator domain size
86  if (current >= agg.domainSize()) current = agg.domainSize() - 1;
87 
88  return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
89  }
90 
91  template < typename GUM_SCALAR >
92  const std::string MultiDimAggregator< GUM_SCALAR >::toString() const {
93  std::stringstream s;
94  s << this->variable(0) << "=" << aggregatorName() << "(";
95 
96  for (Idx i = 1; i < this->nbrDim(); i++) {
97  if (i > 1) s << ",";
98 
99  s << this->variable(i);
100  }
101 
102  s << ")";
103 
104  return s.str();
105  }
106  template < typename GUM_SCALAR >
108  const MultiDimContainer< GUM_SCALAR >& src) const {
109  auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src);
110  if (p == nullptr) {
112  } else {
113  if (p->name() != this->name()) {
115  "Can not copy from a " << p->name() << " to a "
116  << this->name());
117  }
118  }
119  // it the types aree consistant, nothing to do...
120  }
121 
122  // returns the name of the implementation
123  template < typename GUM_SCALAR >
124  const std::string& MultiDimAggregator< GUM_SCALAR >::name() const {
125  static const std::string str = "MultiDimAggregator";
126  return str;
127  }
128 
129  // For friendly displaying the content of the variable.
130  template < typename GUM_SCALAR >
131  INLINE std::ostream& operator<<(std::ostream& s,
133  return s << ag.toString();
134  }
135  } /* namespace aggregator */
136 } /* 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.
gum is the global namespace for all aGrUM entities
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
MultiDimAggregator.
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:80
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:50
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:52