aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimAggregator_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief MultiDimAggregator
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 // to ease parser in IDEs
30 #include <agrum/tools/multidim/aggregators/multiDimAggregator.h>
31 
32 namespace gum {
33  namespace aggregator {
34 
35  // Default constructor
36  template < typename GUM_SCALAR >
37  INLINE MultiDimAggregator< GUM_SCALAR >::MultiDimAggregator() :
40  }
41 
42  // Default constructor
43  template < typename GUM_SCALAR >
49  }
50 
51  // destructor
52  template < typename GUM_SCALAR >
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))))
64 
65  bool stop_iteration = false;
66 
67  for (Idx j = 1; j < this->nbrDim(); j++) {
68  current = fold_(this->variable(j),
69  i.val(this->variable(j)),
70  current,
72 
73  if (stop_iteration) break;
74  }
75 
76  return current;
77  }
78 
79  template < typename GUM_SCALAR >
82  if (this->nbrDim() < 1) {
84  "Not enough variable for an aggregator : " << *this);
85  }
86 
87  const DiscreteVariable& agg = this->variable((Idx)0);
88  auto current = buildValue_(i);
89 
90  // truncate to fit in aggreegator domain size
91  if (current >= agg.domainSize()) current = agg.domainSize() - 1;
92 
93  return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
94  }
95 
96  template < typename GUM_SCALAR >
99  s << this->variable(0) << "=" << aggregatorName() << "(";
100 
101  for (Idx i = 1; i < this->nbrDim(); i++) {
102  if (i > 1) s << ",";
103 
104  s << this->variable(i);
105  }
106 
107  s << ")";
108 
109  return s.str();
110  }
111  template < typename GUM_SCALAR >
113  const MultiDimContainer< GUM_SCALAR >& src) const {
114  auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src);
115  if (p == nullptr) {
117  } else {
118  if (p->name() != this->name()) {
120  "Can not copy from a " << p->name() << " to a "
121  << this->name());
122  }
123  }
124  // it the types aree consistant, nothing to do...
125  }
126 
127  template < typename GUM_SCALAR >
129  return decomposable_;
130  }
131 
132 
133  // returns the name of the implementation
134  template < typename GUM_SCALAR >
136  static const std::string str = "MultiDimAggregator";
137  return str;
138  }
139 
140  // For friendly displaying the content of the variable.
141  template < typename GUM_SCALAR >
143  const MultiDimAggregator< GUM_SCALAR >& ag) {
144  return s << ag.toString();
145  }
146  } /* namespace aggregator */
147 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
INLINE std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_SCALAR > &ag)
For friendly displaying the content of the array.