aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
operators4MultiDim_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 Efficient functionals for combining multiDims
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 // allow operatorsPatterns to be used
32 # define GUM_OPERATOR_PATTERN_ALLOWED 1
33 
34 # include <agrum/tools/multidim/utils/operators/operatorRegister4MultiDim.h>
35 
36 // ==========================================================================
37 // MultiDimArrays functions
38 // ==========================================================================
39 
40 // a specialized function for summing two multiDimArrays
41 
42 // addition taking in argument 2 multiDimArrays
43 # define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimArrays
44 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
45 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
46 # undef GUM_MULTI_DIM_OPERATOR_NAME
47 # undef GUM_MULTI_DIM_OPERATOR
48 
49 # define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME add2MultiDimArrays
50 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
51 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
52 # undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
53 # undef GUM_MULTI_DIM_OPERATOR
54 
55 # define GUM_MULTI_DIM_OPERATOR_POINTER_NAME add2MultiDimArrays4Pointers
56 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) + (*y))
57 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
58 # undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
59 # undef GUM_MULTI_DIM_OPERATOR
60 
61 # define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
62  add2MultiDimArrays4Pointers
63 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) + (*y))
64 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
65 # undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
66 # undef GUM_MULTI_DIM_OPERATOR
67 
68 // a specialized function for subtracting two multiDimArrays
69 
70 # define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimArrays
71 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
72 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
73 # undef GUM_MULTI_DIM_OPERATOR_NAME
74 # undef GUM_MULTI_DIM_OPERATOR
75 
76 # define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME subtract2MultiDimArrays
77 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
78 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
79 # undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
80 # undef GUM_MULTI_DIM_OPERATOR
81 
82 # define GUM_MULTI_DIM_OPERATOR_POINTER_NAME subtract2MultiDimArrays4Pointers
83 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) - (*y))
84 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
85 # undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
86 # undef GUM_MULTI_DIM_OPERATOR
87 
88 # define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
89  subtract2MultiDimArrays4Pointers
90 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) - (*y))
91 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
92 # undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
93 # undef GUM_MULTI_DIM_OPERATOR
94 
95 // a specialized function for multiplying two multiDimArrays
96 
97 # define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimArrays
98 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
99 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
100 # undef GUM_MULTI_DIM_OPERATOR_NAME
101 # undef GUM_MULTI_DIM_OPERATOR
102 
103 # define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME multiply2MultiDimArrays
104 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
105 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
106 # undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
107 # undef GUM_MULTI_DIM_OPERATOR
108 
109 # define GUM_MULTI_DIM_OPERATOR_POINTER_NAME multiply2MultiDimArrays4Pointers
110 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) * (*y))
111 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
112 # undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
113 # undef GUM_MULTI_DIM_OPERATOR
114 
115 # define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
116  multiply2MultiDimArrays4Pointers
117 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) * (*y))
118 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
119 # undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
120 # undef GUM_MULTI_DIM_OPERATOR
121 
122 // a specialized function for dividing two multiDimArrays
123 
124 # define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimArrays
125 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
126 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
127 # undef GUM_MULTI_DIM_OPERATOR_NAME
128 # undef GUM_MULTI_DIM_OPERATOR
129 
130 # define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME divide2MultiDimArrays
131 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
132 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
133 # undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
134 # undef GUM_MULTI_DIM_OPERATOR
135 
136 # define GUM_MULTI_DIM_OPERATOR_POINTER_NAME divide2MultiDimArrays4Pointers
137 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) / (*y))
138 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
139 # undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
140 # undef GUM_MULTI_DIM_OPERATOR
141 
142 # define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
143  divide2MultiDimArrays4Pointers
144 # define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) / (*y))
145 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
146 # undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
147 # undef GUM_MULTI_DIM_OPERATOR
148 
149 // ==========================================================================
150 // MultiDimFunctionGraphs functions
151 // ==========================================================================
152 
153 // ///////////////////////////////////////////////////////
154 // a specialized function for summing two multiDimFunctionGraphs
155 // ///////////////////////////////////////////////////////
156 
157 # define GUM_MULTI_DIM_OPERATOR std::plus
158 
159 # define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimFunctionGraphs
160 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
161 # undef GUM_MULTI_DIM_OPERATOR_NAME
162 
163 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
164  add2MultiDimFunctionGraphs
165 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
166 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
167 
168 # undef GUM_MULTI_DIM_OPERATOR
169 
170 // ///////////////////////////////////////////////////////
171 // a specialized function for subtracting two multiDimFunctionGraphs
172 // ///////////////////////////////////////////////////////
173 
174 # define GUM_MULTI_DIM_OPERATOR std::minus
175 
176 # define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimFunctionGraphs
177 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
178 # undef GUM_MULTI_DIM_OPERATOR_NAME
179 
180 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
181  subtract2MultiDimFunctionGraphs
182 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
183 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
184 
185 # undef GUM_MULTI_DIM_OPERATOR
186 
187 // ///////////////////////////////////////////////////////
188 // a specialized function for multiplying two multiDimFunctionGraphs
189 // ///////////////////////////////////////////////////////
190 
191 # define GUM_MULTI_DIM_OPERATOR std::multiplies
192 
193 # define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimFunctionGraphs
194 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
195 # undef GUM_MULTI_DIM_OPERATOR_NAME
196 
197 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
198  multiply2MultiDimFunctionGraphs
199 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
200 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
201 
202 # undef GUM_MULTI_DIM_OPERATOR
203 
204 // ///////////////////////////////////////////////////////
205 // a specialized function for dividing two multiDimFunctionGraphs
206 // ///////////////////////////////////////////////////////
207 
208 # define GUM_MULTI_DIM_OPERATOR std::divides
209 
210 # define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimFunctionGraphs
211 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
212 # undef GUM_MULTI_DIM_OPERATOR_NAME
213 
214 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
215  divide2MultiDimFunctionGraphs
216 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
217 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
218 
219 # undef GUM_MULTI_DIM_OPERATOR
220 
221 // ///////////////////////////////////////////////////////
222 // a specialized function for finding max of two multiDimFunctionGraphs
223 // ///////////////////////////////////////////////////////
224 
225 # define GUM_MULTI_DIM_OPERATOR Maximizes
226 
227 # define GUM_MULTI_DIM_OPERATOR_NAME maximize2MultiDimFunctionGraphs
228 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
229 # undef GUM_MULTI_DIM_OPERATOR_NAME
230 
231 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
232  maximize2MultiDimFunctionGraphs
233 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
234 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
235 
236 # undef GUM_MULTI_DIM_OPERATOR
237 
238 // ///////////////////////////////////////////////////////
239 // a specialized function for finding min of two multiDimFunctionGraphs
240 // ///////////////////////////////////////////////////////
241 
242 # define GUM_MULTI_DIM_OPERATOR Minimizes
243 
244 # define GUM_MULTI_DIM_OPERATOR_NAME minimize2MultiDimFunctionGraphs
245 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
246 # undef GUM_MULTI_DIM_OPERATOR_NAME
247 
248 # define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
249  minimize2MultiDimFunctionGraphs
250 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimFunctionGraph.h>
251 # undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
252 
253 # undef GUM_MULTI_DIM_OPERATOR
254 
255 // a specialized function for functionally combining two multiDimArrays
256 
257 # define GUM_MULTI_DIM_OPERATOR_NAME_F combine2MultiDimArrays
258 # define GUM_MULTI_DIM_OPERATOR(x, y) f((x), (y))
259 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimArray.h>
260 # undef GUM_MULTI_DIM_OPERATOR_NAME_F
261 # undef GUM_MULTI_DIM_OPERATOR
262 
263 // the operators that should be used to select appropriately the functions
264 // to combine multiDims
265 
266 // the function to be used to add two MultiDimImplementations
267 # define GUM_MULTI_DIM_OPERATOR_NAME operator+
268 # define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "+"
269 # define GUM_MULTI_DIM_SYMMETRIC_OPERATOR
270 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimImplementation.h>
271 # undef GUM_MULTI_DIM_OPERATOR_NAME
272 # undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
273 # undef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
274 
275 // the function to be used to multiply two MultiDimImplementations
276 # define GUM_MULTI_DIM_OPERATOR_NAME operator*
277 # define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "*"
278 # define GUM_MULTI_DIM_SYMMETRIC_OPERATOR
279 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimImplementation.h>
280 # undef GUM_MULTI_DIM_OPERATOR_NAME
281 # undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
282 # undef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
283 
284 // the function to be used to subtract two MultiDimImplementations
285 # define GUM_MULTI_DIM_OPERATOR_NAME operator-
286 # define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "-"
287 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimImplementation.h>
288 # undef GUM_MULTI_DIM_OPERATOR_NAME
289 # undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
290 
291 // the function to be used to divide two MultiDimImplementations
292 # define GUM_MULTI_DIM_OPERATOR_NAME operator/
293 # define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "/"
294 # include <agrum/tools/multidim/utils/patterns/operatorPattern4MultiDimImplementation.h>
295 # undef GUM_MULTI_DIM_OPERATOR_NAME
296 # undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
297 
298 // default "basename" functions for combining two MultiDimImplementations
299 
300 # define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimImplementations
301 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
302 # include <agrum/tools/multidim/utils/patterns/operatorPattern4BaseName.h>
303 # undef GUM_MULTI_DIM_OPERATOR_NAME
304 # undef GUM_MULTI_DIM_OPERATOR
305 
306 # define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimImplementations
307 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
308 # include <agrum/tools/multidim/utils/patterns/operatorPattern4BaseName.h>
309 # undef GUM_MULTI_DIM_OPERATOR_NAME
310 # undef GUM_MULTI_DIM_OPERATOR
311 
312 # define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimImplementations
313 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
314 # include <agrum/tools/multidim/utils/patterns/operatorPattern4BaseName.h>
315 # undef GUM_MULTI_DIM_OPERATOR_NAME
316 # undef GUM_MULTI_DIM_OPERATOR
317 
318 # define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimImplementations
319 # define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
320 # include <agrum/tools/multidim/utils/patterns/operatorPattern4BaseName.h>
321 # undef GUM_MULTI_DIM_OPERATOR_NAME
322 # undef GUM_MULTI_DIM_OPERATOR
323 
324 //
325 // DO NOT FORGET TO REGISTER YOUR BINARY FUNCTIONS
326 //
327 
328 namespace gum {
329 
330  // the function used to register all the above functions
331  template < typename GUM_SCALAR >
332  void operators4MultiDimInit() {
333  static bool first_init = true;
334 
335  if (first_init) {
336  first_init = false;
337 
338  std::string MultiDimArrayString("MultiDimArray");
339  std::string MultiDimFunctionGraphString("MultiDimFunctionGraph");
340  std::string BaseNameString("MultiDimImplementation");
341 
342  // register base functions for multiDimArrays
343  registerOperator< GUM_SCALAR >("+",
344  MultiDimArrayString,
345  MultiDimArrayString,
346  &add2MultiDimArrays);
347  registerOperator< GUM_SCALAR >("-",
348  MultiDimArrayString,
349  MultiDimArrayString,
350  &subtract2MultiDimArrays);
351  registerOperator< GUM_SCALAR >("*",
352  MultiDimArrayString,
353  MultiDimArrayString,
354  &multiply2MultiDimArrays);
355  registerOperator< GUM_SCALAR >("/",
356  MultiDimArrayString,
357  MultiDimArrayString,
358  &divide2MultiDimArrays);
359 
360  // register base functions for multiDimFunctionGraphs
361  registerOperator< GUM_SCALAR >("+",
362  MultiDimFunctionGraphString,
363  MultiDimFunctionGraphString,
364  &add2MultiDimFunctionGraphs);
365  registerOperator< GUM_SCALAR >("-",
366  MultiDimFunctionGraphString,
367  MultiDimFunctionGraphString,
368  &subtract2MultiDimFunctionGraphs);
369  registerOperator< GUM_SCALAR >("*",
370  MultiDimFunctionGraphString,
371  MultiDimFunctionGraphString,
372  &multiply2MultiDimFunctionGraphs);
373  registerOperator< GUM_SCALAR >("/",
374  MultiDimFunctionGraphString,
375  MultiDimFunctionGraphString,
376  &divide2MultiDimFunctionGraphs);
377 
378  // register default basename functions
379  registerOperator< GUM_SCALAR >("+",
380  BaseNameString,
381  BaseNameString,
382  &add2MultiDimImplementations);
383  registerOperator< GUM_SCALAR >("-",
384  BaseNameString,
385  BaseNameString,
386  &subtract2MultiDimImplementations);
387  registerOperator< GUM_SCALAR >("*",
388  BaseNameString,
389  BaseNameString,
390  &multiply2MultiDimImplementations);
391  registerOperator< GUM_SCALAR >("/",
392  BaseNameString,
393  BaseNameString,
394  &divide2MultiDimImplementations);
395  }
396  }
397 
398  // the function used to register all the above functions
399  template < typename GUM_SCALAR >
401  static bool first_init = true;
402 
403  if (first_init) {
404  first_init = false;
405 
406  std::string MultiDimArrayString("MultiDimArray");
407  std::string BaseNameString("MultiDimImplementation");
408 
409  // register base functions for multiDimArrays
410  registerOperator< GUM_SCALAR* >("+",
411  MultiDimArrayString,
412  MultiDimArrayString,
413  &add2MultiDimArrays4Pointers);
414  registerOperator< GUM_SCALAR* >("-",
415  MultiDimArrayString,
416  MultiDimArrayString,
417  &subtract2MultiDimArrays4Pointers);
418  registerOperator< GUM_SCALAR* >("*",
419  MultiDimArrayString,
420  MultiDimArrayString,
421  &multiply2MultiDimArrays4Pointers);
422  registerOperator< GUM_SCALAR* >("/",
423  MultiDimArrayString,
424  MultiDimArrayString,
425  &divide2MultiDimArrays4Pointers);
426  }
427  }
428 
429 } /* namespace gum */
430 
431 // remove permission to use operatorsPatterns
432 # undef GUM_OPERATOR_PATTERN_ALLOWED
433 
434 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
void operators4MultiDimInit()
the function used to register all the operators on multidimImplementations over non-pointers types...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void pointerOperators4MultiDimInit()
the function used to register all the operators on multidimImplementations over pointers types...