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