aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
operatorPattern4BaseName.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 the pattern used by all "basename" binary operators
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 // check if we allowed these patterns to be used
30 #ifndef GUM_OPERATOR_PATTERN_ALLOWED
31 
32 // #warning To use operatorPattern4MultiDimBase.h, you must define
33 // GUM_OPERATOR_PATTERN_ALLOWED
34 
35 #else
36 
37 namespace gum {
38 
39  // a specialized function for combining two multiDimImplementations whose
40  // real data type is unknown to us
41 
42 # ifdef GUM_MULTI_DIM_OPERATOR_NAME
43 # define GUM_MULTI_DIM_OPERATOR_TYPE T
44  template < typename T >
45  MultiDimImplementation< T >* GUM_MULTI_DIM_OPERATOR_NAME(const MultiDimImplementation< T >* t1,
46  const MultiDimImplementation< T >* t2)
47 # endif
48 
49  // clang-format off
50 
51 #ifdef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
52 #define GUM_MULTI_DIM_OPERATOR_TYPE T *
53  template <typename T>
54  MultiDimImplementation<T*>* GUM_MULTI_DIM_OPERATOR_POINTER_NAME(
55  const MultiDimImplementation<T*>* t1,
56  const MultiDimImplementation<T*>* t2 )
57 #endif
58 
59 #ifdef GUM_MULTI_DIM_OPERATOR_NAME_F
60 #define GUM_MULTI_DIM_OPERATOR_TYPE T
61  template <typename T>
62  MultiDimImplementation<T>* GUM_MULTI_DIM_OPERATOR_NAME_F(
63  const MultiDimImplementation<T>* t1,
64  const MultiDimImplementation<T>* t2,
65  const T ( *f )( const T&, const T& ) )
66 #endif
67 
68 #ifdef GUM_MULTI_DIM_OPERATOR_POINTER_NAME_F
69 #define GUM_MULTI_DIM_OPERATOR_TYPE T *
70  template <typename T>
71  MultiDimImplementation<T*>* GUM_MULTI_DIM_OPERATOR_POINTER_NAME_F(
72  const MultiDimImplementation<T*>* t1,
73  const MultiDimImplementation<T*>* t2,
74  const T ( *f )( const T*, const T* ) )
75 #endif
76 
77  // clang-format on
78 
79  {
80 
81  // get the variables of the tables
82  const Sequence< const DiscreteVariable* >& t1_vars = t1->variablesSequence();
83  const Sequence< const DiscreteVariable* >& t2_vars = t2->variablesSequence();
84 
85  // get the domain size of the tables' variables
86  HashTable< const DiscreteVariable*, Idx > t1_offsets;
87  {
88  Idx current_offset = 1;
89 
90  for (const auto var: t1_vars) {
91  t1_offsets.insert(var, current_offset);
92  current_offset *= var->domainSize();
93  }
94  }
95  HashTable< const DiscreteVariable*, Idx > t2_offsets;
96  {
97  Idx current_offset = 1;
98 
99  for (const auto var: t2_vars) {
100  t2_offsets.insert(var, current_offset);
101  current_offset *= var->domainSize();
102  }
103  }
104 
105  // we divide the variables of t1 and t2 into 3 separate sets: those that
106  // belong only to t1 (variables t1_alone_xxx), those that belong only to t2
107  // (variables t2_alone_xxx) and those that belong to both tables (variables
108  // t1_and_t2_xxx). For each set, we get the variables of the table
109  // (txxx_var) and the domain size of the variable (txxx_domain). In
110  // addition, we compute the domain size of the Cartesian product of the
111  // variables in each of the 3 sets. Given these data, we will be able to
112  // parse both t1, t2 and the result table t1+t2.
113  std::vector< const DiscreteVariable* > t1_alone_var;
114  std::vector< Idx > t1_alone_domain;
115  Idx t1_alone_domain_size = 1;
116 
117  std::vector< const DiscreteVariable* > t2_alone_var;
118  std::vector< Idx > t2_alone_domain;
119  Idx t2_alone_domain_size = 1;
120 
121  std::vector< const DiscreteVariable* > t1_and_t2_var;
122  std::vector< Idx > t1_and_t2_domain;
123  Idx t1_and_t2_domain_size = 1;
124 
125  {
126  for (const auto var: t1_vars)
127  if (t2_vars.exists(var)) {
128  t1_and_t2_domain.push_back(var->domainSize());
129  t1_and_t2_var.push_back(var);
130  t1_and_t2_domain_size *= var->domainSize();
131  } else {
132  t1_alone_domain.push_back(var->domainSize());
133  t1_alone_var.push_back(var);
134  t1_alone_domain_size *= var->domainSize();
135  }
136 
137  for (const auto var: t2_vars)
138  if (!t1_vars.exists(var)) {
139  t2_alone_domain.push_back(var->domainSize());
140  t2_alone_var.push_back(var);
141  t2_alone_domain_size *= var->domainSize();
142  }
143  }
144 
145  // a Boolean indicating whether the variables that t1 and t2 have in common
146  // are the first variables and are in the same order. When this is true,
147  // computations can be performed faster
148  bool t1_and_t2_begin_vars = false;
149 
150  if (t1_and_t2_var.size()) {
151  unsigned int nb_t1_t2_vars = 0;
152 
153  for (const auto var: t1_vars) {
154  if (var != t1_and_t2_var[nb_t1_t2_vars]) break;
155  nb_t1_t2_vars += 1;
156  }
157 
158  if (nb_t1_t2_vars == t1_and_t2_var.size()) {
159  nb_t1_t2_vars = 0;
160 
161  for (auto iter = t2_vars.begin(); nb_t1_t2_vars != t1_and_t2_var.size();
162  ++iter, ++nb_t1_t2_vars)
163  if (*iter != t1_and_t2_var[nb_t1_t2_vars]) break;
164 
165  if (nb_t1_t2_vars == t1_and_t2_var.size()) t1_and_t2_begin_vars = true;
166  }
167  }
168 
169  // when we will parse t1 and t2 to fill the result table t1+t2, we will use
170  // variables txxx_value : at the beginning they are initialized to the
171  // domain size of the variables (which are, themselves initialized to 0).
172  // Each time we increment a variable, its corresponding txxx_value is
173  // decreased by 1. When the latter is equal to 0, this means that the
174  // variable itself should be reinitialized to 0 as well and that the next
175  // variable of the table should be increased (that is, this is similar to
176  // increasing 9 to 10).
177  std::vector< Idx > t1_and_t2_value = t1_and_t2_domain;
178  std::vector< Idx > t1_alone_value = t1_alone_domain;
179  std::vector< Idx > t2_alone_value = t2_alone_domain;
180 
181  // create a table "result" containing all the variables: the first
182  // variables are those that belong to both t1 and t2. The next variables
183  // are those that belong to t2 but not to t1. Finally, the last variables
184  // are those that belong to t1 but not t2. This order will be used in the
185  // next for loops.
186  MultiDimArray< GUM_MULTI_DIM_OPERATOR_TYPE >* result
187  = new MultiDimArray< GUM_MULTI_DIM_OPERATOR_TYPE >;
188  result->beginMultipleChanges();
189 
190  for (const auto var: t1_vars)
191  if (t2_vars.exists(var)) *result << *var;
192 
193  for (const auto var: t2_vars)
194  if (!t1_vars.exists(var)) *result << *var;
195 
196  for (const auto var: t1_vars)
197  if (!t2_vars.exists(var)) *result << *var;
198 
199  result->endMultipleChanges();
200 
201  // here we fill result. The idea is to use 3 loops. The innermost loop
202  // corresponds to the variables that belongs both to t1 and t2. The middle
203  // loop to the variables that belong to t2 but not to t1. Finally, the
204  // outer loop corresponds to the variables that belong to t1 but not t2.
205  Idx result_offset = 0;
206  Instantiation t2_inst(t2);
207  Instantiation t1_inst(t1);
208  Instantiation t1_alone_begin_inst(t1);
209 
210  // test if all the variables in common in t1 and t2 are the first variables
211  // and are in the same order. In this case, we can speed-up the
212  // incrementation processes
213  if (t1_and_t2_begin_vars) {
214  for (Idx i = 0; i < t1_alone_domain_size; ++i) {
215  t2_inst.setFirst();
216  t1_alone_begin_inst = t1_inst;
217 
218  for (Idx j = 0; j < t2_alone_domain_size; ++j) {
219  t1_inst = t1_alone_begin_inst;
220 
221  for (Idx z = 0; z < t1_and_t2_domain_size; ++z) {
222  result->unsafeSet(result_offset,
223  GUM_MULTI_DIM_OPERATOR(t1->get(t1_inst), t2->get(t2_inst)));
224 
225  ++result_offset;
226 
227  // update the offset of both t1 and t2
228  ++t1_inst;
229  ++t2_inst;
230  }
231  }
232  }
233  } else {
234  for (Idx i = 0; i < t1_alone_domain_size; ++i) {
235  t2_inst.setFirst();
236  t1_alone_begin_inst = t1_inst;
237 
238  for (Idx j = 0; j < t2_alone_domain_size; ++j) {
239  t1_inst = t1_alone_begin_inst;
240 
241  for (Idx z = 0; z < t1_and_t2_domain_size; ++z) {
242  result->unsafeSet(result_offset,
243  GUM_MULTI_DIM_OPERATOR(t1->get(t1_inst), t2->get(t2_inst)));
244 
245  ++result_offset;
246 
247  // update the offset of both t1 and t2
248  for (unsigned int k = 0; k < t1_and_t2_value.size(); ++k) {
249  --t1_and_t2_value[k];
250 
251  if (t1_and_t2_value[k]) {
252  t1_inst.incVar(*(t1_and_t2_var[k]));
253  t2_inst.incVar(*(t1_and_t2_var[k]));
254  break;
255  }
256 
257  t1_and_t2_value[k] = t1_and_t2_domain[k];
258  t1_inst.setFirstVar(*(t1_and_t2_var[k]));
259  t2_inst.setFirstVar(*(t1_and_t2_var[k]));
260  }
261  }
262 
263  // update the offset of t2 alone
264  for (unsigned int k = 0; k < t2_alone_value.size(); ++k) {
265  --t2_alone_value[k];
266 
267  if (t2_alone_value[k]) {
268  t2_inst.incVar(*(t2_alone_var[k]));
269  break;
270  }
271 
272  t2_alone_value[k] = t2_alone_domain[k];
273  t2_inst.setFirstVar(*(t2_alone_var[k]));
274  }
275  }
276 
277  // update the offset of t1 alone
278  for (unsigned int k = 0; k < t1_alone_value.size(); ++k) {
279  --t1_alone_value[k];
280 
281  if (t1_alone_value[k]) {
282  t1_inst.incVar(*(t1_alone_var[k]));
283  break;
284  }
285 
286  t1_alone_value[k] = t1_alone_domain[k];
287  t1_inst.setFirstVar(*(t1_alone_var[k]));
288  }
289  }
290  }
291 
292  return result;
293  }
294 
295 # undef GUM_MULTI_DIM_OPERATOR_TYPE
296 } // namespace gum
297 #endif /* GUM_OPERATOR_PATTERN_ALLOWED */