aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
operatorPattern4MultiDimArray.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 binary MultiDimArrays 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 operatorPattern4MultiDimArray.h, you must define
33 // GUM_OPERATOR_PATTERN_ALLOWED
34 
35 #else
36 
37 namespace gum {
38 
39  // a specialized function for combining two multiDimArrays
40 
41 # ifdef GUM_MULTI_DIM_OPERATOR_NAME
42 # define GUM_MULTI_DIM_OPERATOR_TYPE T
43  template < typename T >
44  MultiDimArray< T >* GUM_MULTI_DIM_OPERATOR_NAME(const MultiDimArray< T >* t1,
45  const MultiDimArray< T >* t2)
46 # endif
47 
48  // clang-format off
49 
50 #ifdef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
51 #define GUM_MULTI_DIM_OPERATOR_TYPE T *
52  template <typename T>
53  MultiDimArray<T*>* GUM_MULTI_DIM_OPERATOR_POINTER_NAME(
54  const MultiDimArray<T*>* t1, const MultiDimArray<T*>* t2 )
55 #endif
56 
57 #ifdef GUM_MULTI_DIM_OPERATOR_NAME_F
58 #define GUM_MULTI_DIM_OPERATOR_TYPE T
59  template <typename T>
60  MultiDimArray<T>* GUM_MULTI_DIM_OPERATOR_NAME_F(
61  const MultiDimArray<T>* t1,
62  const MultiDimArray<T>* t2,
63  const T ( *f )( const T&, const T& ) )
64 #endif
65 
66 #ifdef GUM_MULTI_DIM_OPERATOR_POINTER_NAME_F
67 #define GUM_MULTI_DIM_OPERATOR_TYPE T *
68  template <typename T>
69  MultiDimArray<T*>* GUM_MULTI_DIM_OPERATOR_POINTER_NAME_F(
70  const MultiDimArray<T*>* t1,
71  const MultiDimArray<T*>* t2,
72  const T* ( *f )( const T*, const T* ) )
73 #endif
74 
75 #ifdef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
76 #define GUM_MULTI_DIM_OPERATOR_TYPE T
77  template <typename T>
78  MultiDimImplementation<T>* GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME(
79  const MultiDimImplementation<T>* tt1,
80  const MultiDimImplementation<T>* tt2 )
81 #endif
82 
83 #ifdef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
84 #define GUM_MULTI_DIM_OPERATOR_TYPE T *
85  template <typename T>
86  MultiDimImplementation<T*>*
87  GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME(
88  const MultiDimImplementation<T*>* tt1,
89  const MultiDimImplementation<T*>* tt2 )
90 #endif
91 
92  // clang-format on
93 
94  {
95 
96 # ifdef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
97  const MultiDimArray< T >* t1 = reinterpret_cast< const MultiDimArray< T >* >(tt1);
98  const MultiDimArray< T >* t2 = reinterpret_cast< const MultiDimArray< T >* >(tt2);
99 # endif
100 
101 # ifdef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
102  const MultiDimArray< T* >* t1 = reinterpret_cast< const MultiDimArray< T* >* >(tt1);
103  const MultiDimArray< T* >* t2 = reinterpret_cast< const MultiDimArray< T* >* >(tt2);
104 # endif
105 
106  // get the variables of the tables
107  const Sequence< const DiscreteVariable* >& t1_vars = t1->variablesSequence();
108  const Sequence< const DiscreteVariable* >& t2_vars = t2->variablesSequence();
109 
110  // get the domain size of the tables' variables
111  HashTable< const DiscreteVariable*, Idx > t1_offsets;
112  {
113  Idx current_offset = 1;
114 
115  for (const auto var: t1_vars) {
116  t1_offsets.insert(var, current_offset);
117  current_offset *= var->domainSize();
118  }
119  }
120  HashTable< const DiscreteVariable*, Idx > t2_offsets;
121  {
122  Idx current_offset = 1;
123 
124  for (const auto var: t2_vars) {
125  t2_offsets.insert(var, current_offset);
126  current_offset *= var->domainSize();
127  }
128  }
129 
130  // we divide the variables of t1 and t2 into 3 separate sets: those that
131  // belong only to t1 (variables t1_alone_xxx), those that belong only to t2
132  // (variables t2_alone_xxx) and those that belong to both tables (variables
133  // t1_and_t2_xxx). For each set, we define how an increment of a given
134  // variable of the set changes the offset in the corresponding table
135  // (variable txxx_offset) and what is the domain size of the variable
136  // (txxx_domain). In addition, we compute the domain size of the Cartesian
137  // product of the variables in each of the 3 sets. Given these data, we
138  // will be able to parse both t1, t2 and the result table t1+t2 without
139  // resorting to instantiations.
140  std::vector< Idx > t1_alone_offset;
141  std::vector< Idx > t1_alone_domain;
142  Idx t1_alone_domain_size = 1;
143 
144  std::vector< Idx > t2_alone_offset;
145  std::vector< Idx > t2_alone_domain;
146  Idx t2_alone_domain_size = 1;
147 
148  std::vector< Idx > t1_and_t2_1_offset;
149  std::vector< Idx > t1_and_t2_2_offset;
150  std::vector< Idx > t1_and_t2_domain;
151  std::vector< const DiscreteVariable* > t1_and_t2_vars;
152  Idx t1_and_t2_domain_size = 1;
153 
154  {
155  for (const auto var: t1_vars)
156  if (t2_vars.exists(var)) {
157  t1_and_t2_domain.push_back(var->domainSize());
158  t1_and_t2_1_offset.push_back(t1_offsets[var]);
159  t1_and_t2_2_offset.push_back(t2_offsets[var]);
160  t1_and_t2_vars.push_back(var);
161  t1_and_t2_domain_size *= var->domainSize();
162  } else {
163  t1_alone_domain.push_back(var->domainSize());
164  t1_alone_offset.push_back(t1_offsets[var]);
165  t1_alone_domain_size *= var->domainSize();
166  }
167 
168  for (const auto var: t2_vars)
169  if (!t1_vars.exists(var)) {
170  t2_alone_domain.push_back(var->domainSize());
171  t2_alone_offset.push_back(t2_offsets[var]);
172  t2_alone_domain_size *= var->domainSize();
173  }
174  }
175 
176  // a Boolean indicating whether the variables that t1 and t2 have in common
177  // are the first variables and are in the same order. When this is true,
178  // computations can be performed faster
179  bool t1_and_t2_begin_vars = false;
180 
181  if (t1_and_t2_vars.size()) {
182  unsigned int nb_t1_t2_vars = 0;
183 
184  for (auto iter = t1_vars.begin(); nb_t1_t2_vars != t1_and_t2_vars.size();
185  ++iter, ++nb_t1_t2_vars)
186  if (*iter != t1_and_t2_vars[nb_t1_t2_vars]) break;
187 
188  if (nb_t1_t2_vars == t1_and_t2_vars.size()) {
189  nb_t1_t2_vars = 0;
190 
191  for (auto iter = t2_vars.begin(); nb_t1_t2_vars != t1_and_t2_vars.size();
192  ++iter, ++nb_t1_t2_vars)
193  if (*iter != t1_and_t2_vars[nb_t1_t2_vars]) break;
194 
195  if (nb_t1_t2_vars == t1_and_t2_vars.size()) { t1_and_t2_begin_vars = true; }
196  }
197  }
198 
199  // when we will parse t1 and t2 to fill the result table t1+t2, we will use
200  // variables txxx_value : at the beginning they are initialized to the
201  // domain size of the variables (which are, themselves initialized to 0).
202  // Each time we increment a variable (that is, we increase the offset of
203  // the table by txxx_offset), its corresponding txxx_value is decreased by
204  // 1. When the latter is equal to 0, this means that the variable itself
205  // should be reinitialized to 0 as well and that the next variable of the
206  // table should be increased (that is, this is similar to increasing 9 to
207  // 10). As such the offset of txxx should be decreased by txxx_offset * the
208  // domain size of the variable. This quantity is precisely what is stored
209  // into variables txxx_down.
210  std::vector< Idx > t1_and_t2_value = t1_and_t2_domain;
211  std::vector< Idx > t1_and_t2_1_down = t1_and_t2_1_offset;
212  std::vector< Idx > t1_and_t2_2_down = t1_and_t2_2_offset;
213 
214  for (unsigned int i = 0; i < t1_and_t2_domain.size(); ++i) {
215  t1_and_t2_1_down[i] *= (t1_and_t2_domain[i] - 1);
216  t1_and_t2_2_down[i] *= (t1_and_t2_domain[i] - 1);
217  }
218 
219  std::vector< Idx > t1_alone_value = t1_alone_domain;
220  std::vector< Idx > t1_alone_down = t1_alone_offset;
221 
222  for (unsigned int i = 0; i < t1_alone_domain.size(); ++i) {
223  t1_alone_down[i] *= (t1_alone_domain[i] - 1);
224  }
225 
226  std::vector< Idx > t2_alone_value = t2_alone_domain;
227  std::vector< Idx > t2_alone_down = t2_alone_offset;
228 
229  for (unsigned int i = 0; i < t2_alone_domain.size(); ++i) {
230  t2_alone_down[i] *= (t2_alone_domain[i] - 1);
231  }
232 
233  // create a table "result" containing all the variables: the first
234  // variables are those that belong to both t1 and t2. The next variables
235  // are those that belong to t2 but not to t1. Finally, the last variables
236  // are those that belong to t1 but not t2. This order will be used in the
237  // next for loops.
238  MultiDimArray< GUM_MULTI_DIM_OPERATOR_TYPE >* result
239  = new MultiDimArray< GUM_MULTI_DIM_OPERATOR_TYPE >;
240  result->beginMultipleChanges();
241 
242  for (const auto var: t1_vars)
243  if (t2_vars.exists(var)) *result << *var;
244 
245  for (const auto var: t2_vars)
246  if (!t1_vars.exists(var)) *result << *var;
247 
248  for (const auto var: t1_vars)
249  if (!t2_vars.exists(var)) *result << *var;
250 
251  result->endMultipleChanges();
252 
253  // here we fill result. The idea is to use 3 loops. The innermost loop
254  // corresponds to the variables that belongs both to t1 and t2. The middle
255  // loop to the variables that belong to t2 but not to t1. Finally, the
256  // outer loop corresponds to the variables that belong to t1 but not t2.
257  GUM_MULTI_DIM_OPERATOR_TYPE* pt1
258  = const_cast< GUM_MULTI_DIM_OPERATOR_TYPE* >(&(t1->unsafeGet(0)));
259  GUM_MULTI_DIM_OPERATOR_TYPE* pt2
260  = const_cast< GUM_MULTI_DIM_OPERATOR_TYPE* >(&(t2->unsafeGet(0)));
261  GUM_MULTI_DIM_OPERATOR_TYPE* pres
262  = const_cast< GUM_MULTI_DIM_OPERATOR_TYPE* >(&(result->unsafeGet(0)));
263  GUM_MULTI_DIM_OPERATOR_TYPE* pt2_deb = pt2;
264  GUM_MULTI_DIM_OPERATOR_TYPE* pt1_alone_begin;
265 
266  // test if all the variables in common in t1 and t2 are the first variables
267  // and are in the same order. In this case, we can speed-up the
268  // incrementation processes
269  if (t1_and_t2_begin_vars) {
270  for (Idx i = 0; i < t1_alone_domain_size; ++i) {
271  pt2 = pt2_deb;
272  pt1_alone_begin = pt1;
273 
274  for (Idx j = 0; j < t2_alone_domain_size; ++j) {
275  pt1 = pt1_alone_begin;
276 
277  for (Idx z = 0; z < t1_and_t2_domain_size; ++z) {
278  *pres = GUM_MULTI_DIM_OPERATOR(*pt1, *pt2);
279  ++pres;
280 
281  // update the offset of both t1 and t2
282  ++pt1;
283  ++pt2;
284  }
285  }
286  }
287  } else {
288  Idx t1_offset = 0;
289  Idx t2_offset = 0;
290  Idx t1_alone_begin_offset = 0;
291 
292  for (Idx i = 0; i < t1_alone_domain_size; ++i) {
293  t2_offset = 0;
294  t1_alone_begin_offset = t1_offset;
295 
296  for (Idx j = 0; j < t2_alone_domain_size; ++j) {
297  t1_offset = t1_alone_begin_offset;
298 
299  for (Idx z = 0; z < t1_and_t2_domain_size; ++z) {
300  *pres = GUM_MULTI_DIM_OPERATOR(pt1[t1_offset], pt2[t2_offset]);
301  ++pres;
302 
303  // update the offset of both t1 and t2
304  for (unsigned int k = 0; k < t1_and_t2_value.size(); ++k) {
305  if (--t1_and_t2_value[k]) {
306  t1_offset += t1_and_t2_1_offset[k];
307  t2_offset += t1_and_t2_2_offset[k];
308  break;
309  }
310 
311  t1_and_t2_value[k] = t1_and_t2_domain[k];
312  t1_offset -= t1_and_t2_1_down[k];
313  t2_offset -= t1_and_t2_2_down[k];
314  }
315  }
316 
317  // update the offset of t2 alone
318  for (unsigned int k = 0; k < t2_alone_value.size(); ++k) {
319  if (--t2_alone_value[k]) {
320  t2_offset += t2_alone_offset[k];
321  break;
322  }
323 
324  t2_alone_value[k] = t2_alone_domain[k];
325  t2_offset -= t2_alone_down[k];
326  }
327  }
328 
329  // update the offset of t1 alone
330  for (unsigned int k = 0; k < t1_alone_value.size(); ++k) {
331  if (--t1_alone_value[k]) {
332  t1_offset += t1_alone_offset[k];
333  break;
334  }
335 
336  t1_alone_value[k] = t1_alone_domain[k];
337  t1_offset -= t1_alone_down[k];
338  }
339  }
340  }
341 
342  return result;
343  }
344 
345 # undef GUM_MULTI_DIM_OPERATOR_TYPE
346 } // namespace gum
347 
348 #endif /* GUM_OPERATOR_PATTERN_ALLOWED */