aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
operators4MultiDim_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
50
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54// allow operatorsPatterns to be used
55# define GUM_OPERATOR_PATTERN_ALLOWED 1
56
58
59// ==========================================================================
60// MultiDimArrays functions
61// ==========================================================================
62
63// a specialized function for summing two multiDimArrays
64
65// addition taking in argument 2 multiDimArrays
66# define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimArrays
67# define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
69# undef GUM_MULTI_DIM_OPERATOR_NAME
70# undef GUM_MULTI_DIM_OPERATOR
71
72# define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME add2MultiDimArrays
73# define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
75# undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
76# undef GUM_MULTI_DIM_OPERATOR
77
78# define GUM_MULTI_DIM_OPERATOR_POINTER_NAME add2MultiDimArrays4Pointers
79# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) + (*y))
81# undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
82# undef GUM_MULTI_DIM_OPERATOR
83
84# define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME add2MultiDimArrays4Pointers
85# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) + (*y))
87# undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
88# undef GUM_MULTI_DIM_OPERATOR
89
90// a specialized function for subtracting two multiDimArrays
91
92# define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimArrays
93# define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
95# undef GUM_MULTI_DIM_OPERATOR_NAME
96# undef GUM_MULTI_DIM_OPERATOR
97
98# define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME subtract2MultiDimArrays
99# define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
101# undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
102# undef GUM_MULTI_DIM_OPERATOR
103
104# define GUM_MULTI_DIM_OPERATOR_POINTER_NAME subtract2MultiDimArrays4Pointers
105# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) - (*y))
107# undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
108# undef GUM_MULTI_DIM_OPERATOR
109
110# define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME subtract2MultiDimArrays4Pointers
111# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) - (*y))
113# undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
114# undef GUM_MULTI_DIM_OPERATOR
115
116// a specialized function for multiplying two multiDimArrays
117
118# define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimArrays
119# define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
121# undef GUM_MULTI_DIM_OPERATOR_NAME
122# undef GUM_MULTI_DIM_OPERATOR
123
124# define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME multiply2MultiDimArrays
125# define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
127# undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
128# undef GUM_MULTI_DIM_OPERATOR
129
130# define GUM_MULTI_DIM_OPERATOR_POINTER_NAME multiply2MultiDimArrays4Pointers
131# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) * (*y))
133# undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
134# undef GUM_MULTI_DIM_OPERATOR
135
136# define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME multiply2MultiDimArrays4Pointers
137# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) * (*y))
139# undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
140# undef GUM_MULTI_DIM_OPERATOR
141
142// a specialized function for dividing two multiDimArrays
143
144# define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimArrays
145# define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
147# undef GUM_MULTI_DIM_OPERATOR_NAME
148# undef GUM_MULTI_DIM_OPERATOR
149
150# define GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME divide2MultiDimArrays
151# define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
153# undef GUM_MULTI_DIM_OPERATOR_IMPL2ARRAY_NAME
154# undef GUM_MULTI_DIM_OPERATOR
155
156# define GUM_MULTI_DIM_OPERATOR_POINTER_NAME divide2MultiDimArrays4Pointers
157# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) / (*y))
159# undef GUM_MULTI_DIM_OPERATOR_POINTER_NAME
160# undef GUM_MULTI_DIM_OPERATOR
161
162# define GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME divide2MultiDimArrays4Pointers
163# define GUM_MULTI_DIM_OPERATOR(x, y) new T((*x) / (*y))
165# undef GUM_MULTI_DIM_OPERATOR_POINTER_IMPL2ARRAY_NAME
166# undef GUM_MULTI_DIM_OPERATOR
167
168// ==========================================================================
169// MultiDimFunctionGraphs functions
170// ==========================================================================
171
172// ///////////////////////////////////////////////////////
173// a specialized function for summing two multiDimFunctionGraphs
174// ///////////////////////////////////////////////////////
175
176# define GUM_MULTI_DIM_OPERATOR std::plus
177
178# define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimFunctionGraphs
180# undef GUM_MULTI_DIM_OPERATOR_NAME
181
182# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME add2MultiDimFunctionGraphs
184# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
185
186# undef GUM_MULTI_DIM_OPERATOR
187
188// ///////////////////////////////////////////////////////
189// a specialized function for subtracting two multiDimFunctionGraphs
190// ///////////////////////////////////////////////////////
191
192# define GUM_MULTI_DIM_OPERATOR std::minus
193
194# define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimFunctionGraphs
196# undef GUM_MULTI_DIM_OPERATOR_NAME
197
198# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME subtract2MultiDimFunctionGraphs
200# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
201
202# undef GUM_MULTI_DIM_OPERATOR
203
204// ///////////////////////////////////////////////////////
205// a specialized function for multiplying two multiDimFunctionGraphs
206// ///////////////////////////////////////////////////////
207
208# define GUM_MULTI_DIM_OPERATOR std::multiplies
209
210# define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimFunctionGraphs
212# undef GUM_MULTI_DIM_OPERATOR_NAME
213
214# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME multiply2MultiDimFunctionGraphs
216# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
217
218# undef GUM_MULTI_DIM_OPERATOR
219
220// ///////////////////////////////////////////////////////
221// a specialized function for dividing two multiDimFunctionGraphs
222// ///////////////////////////////////////////////////////
223
224# define GUM_MULTI_DIM_OPERATOR std::divides
225
226# define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimFunctionGraphs
228# undef GUM_MULTI_DIM_OPERATOR_NAME
229
230# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME divide2MultiDimFunctionGraphs
232# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
233
234# undef GUM_MULTI_DIM_OPERATOR
235
236// ///////////////////////////////////////////////////////
237// a specialized function for finding max of two multiDimFunctionGraphs
238// ///////////////////////////////////////////////////////
239
240# define GUM_MULTI_DIM_OPERATOR Maximizes
241
242# define GUM_MULTI_DIM_OPERATOR_NAME maximize2MultiDimFunctionGraphs
244# undef GUM_MULTI_DIM_OPERATOR_NAME
245
246# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME maximize2MultiDimFunctionGraphs
248# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
249
250# undef GUM_MULTI_DIM_OPERATOR
251
252// ///////////////////////////////////////////////////////
253// a specialized function for finding min of two multiDimFunctionGraphs
254// ///////////////////////////////////////////////////////
255
256# define GUM_MULTI_DIM_OPERATOR Minimizes
257
258# define GUM_MULTI_DIM_OPERATOR_NAME minimize2MultiDimFunctionGraphs
260# undef GUM_MULTI_DIM_OPERATOR_NAME
261
262# define GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME minimize2MultiDimFunctionGraphs
264# undef GUM_MULTI_DIM_OPERATOR_IMPL2DECISION_GRAPH_NAME
265
266# undef GUM_MULTI_DIM_OPERATOR
267
268// a specialized function for functionally combining two multiDimArrays
269
270# define GUM_MULTI_DIM_OPERATOR_NAME_F combine2MultiDimArrays
271# define GUM_MULTI_DIM_OPERATOR(x, y) f((x), (y))
273# undef GUM_MULTI_DIM_OPERATOR_NAME_F
274# undef GUM_MULTI_DIM_OPERATOR
275
276// the operators that should be used to select appropriately the functions
277// to combine multiDims
278
279// the function to be used to add two MultiDimImplementations
280# define GUM_MULTI_DIM_OPERATOR_NAME operator+
281# define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "+"
282# define GUM_MULTI_DIM_SYMMETRIC_OPERATOR
284# undef GUM_MULTI_DIM_OPERATOR_NAME
285# undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
286# undef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
287
288// the function to be used to multiply two MultiDimImplementations
289# define GUM_MULTI_DIM_OPERATOR_NAME operator*
290# define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "*"
291# define GUM_MULTI_DIM_SYMMETRIC_OPERATOR
293# undef GUM_MULTI_DIM_OPERATOR_NAME
294# undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
295# undef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
296
297// the function to be used to subtract two MultiDimImplementations
298# define GUM_MULTI_DIM_OPERATOR_NAME operator-
299# define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "-"
301# undef GUM_MULTI_DIM_OPERATOR_NAME
302# undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
303
304// the function to be used to divide two MultiDimImplementations
305# define GUM_MULTI_DIM_OPERATOR_NAME operator/
306# define GUM_MULTI_DIM_OPERATOR_FUNC_NAME "/"
308# undef GUM_MULTI_DIM_OPERATOR_NAME
309# undef GUM_MULTI_DIM_OPERATOR_FUNC_NAME
310
311// default "basename" functions for combining two MultiDimImplementations
312
313# define GUM_MULTI_DIM_OPERATOR_NAME add2MultiDimImplementations
314# define GUM_MULTI_DIM_OPERATOR(x, y) (x) + (y)
316# undef GUM_MULTI_DIM_OPERATOR_NAME
317# undef GUM_MULTI_DIM_OPERATOR
318
319# define GUM_MULTI_DIM_OPERATOR_NAME subtract2MultiDimImplementations
320# define GUM_MULTI_DIM_OPERATOR(x, y) (x) - (y)
322# undef GUM_MULTI_DIM_OPERATOR_NAME
323# undef GUM_MULTI_DIM_OPERATOR
324
325# define GUM_MULTI_DIM_OPERATOR_NAME multiply2MultiDimImplementations
326# define GUM_MULTI_DIM_OPERATOR(x, y) (x) * (y)
328# undef GUM_MULTI_DIM_OPERATOR_NAME
329# undef GUM_MULTI_DIM_OPERATOR
330
331# define GUM_MULTI_DIM_OPERATOR_NAME divide2MultiDimImplementations
332# define GUM_MULTI_DIM_OPERATOR(x, y) (x) / (y)
334# undef GUM_MULTI_DIM_OPERATOR_NAME
335# undef GUM_MULTI_DIM_OPERATOR
336
337//
338// DO NOT FORGET TO REGISTER YOUR BINARY FUNCTIONS
339//
340
341namespace gum {
342
343 // the function used to register all the above functions
344 template < typename GUM_ELEMENT >
346 // ensure that only one thread will register the projections
347 static std::once_flag first;
348 std::call_once(first, []() {
349 std::string MultiDimArrayString("MultiDimArray");
350 std::string MultiDimFunctionGraphString("MultiDimFunctionGraph");
351 std::string BaseNameString("MultiDimImplementation");
352
353 // register base functions for multiDimArrays
355 MultiDimArrayString,
356 MultiDimArrayString,
359 MultiDimArrayString,
360 MultiDimArrayString,
363 MultiDimArrayString,
364 MultiDimArrayString,
367 MultiDimArrayString,
368 MultiDimArrayString,
370
371 // register base functions for multiDimFunctionGraphs
373 MultiDimFunctionGraphString,
374 MultiDimFunctionGraphString,
377 MultiDimFunctionGraphString,
378 MultiDimFunctionGraphString,
381 MultiDimFunctionGraphString,
382 MultiDimFunctionGraphString,
385 MultiDimFunctionGraphString,
386 MultiDimFunctionGraphString,
388
389 // register default basename functions
391 BaseNameString,
392 BaseNameString,
393 &add2MultiDimImplementations);
395 BaseNameString,
396 BaseNameString,
397 &subtract2MultiDimImplementations);
399 BaseNameString,
400 BaseNameString,
401 &multiply2MultiDimImplementations);
403 BaseNameString,
404 BaseNameString,
405 &divide2MultiDimImplementations);
406 });
407 }
408
409 // the function used to register all the above functions
410 template < typename GUM_ELEMENT >
412 // ensure that only one thread will register the projections
413 static std::once_flag first;
414 std::call_once(first, []() {
415 std::string MultiDimArrayString("MultiDimArray");
416 std::string BaseNameString("MultiDimImplementation");
417
418 // register base functions for multiDimArrays
420 MultiDimArrayString,
421 MultiDimArrayString,
424 MultiDimArrayString,
425 MultiDimArrayString,
428 MultiDimArrayString,
429 MultiDimArrayString,
432 MultiDimArrayString,
433 MultiDimArrayString,
435 });
436 }
437
438 template < typename GUM_ELEMENT >
441 }
442
443 template < typename GUM_ELEMENT >
446 }
447
448} /* namespace gum */
449
450// remove permission to use operatorsPatterns
451# undef GUM_OPERATOR_PATTERN_ALLOWED
452
453#endif /* DOXYGEN_SHOULD_SKIP_THIS */
void operators4MultiDimInit()
the function used to register all the operators on multidimImplementations over non-pointers types.
MultiDimArray< T * > * multiply2MultiDimArrays4Pointers(const MultiDimArray< T * > *t1, const MultiDimArray< T * > *t2)
a specialized function for multiplying two multiDimArrays
MultiDimArray< T > * subtract2MultiDimArrays(const MultiDimArray< T > *t1, const MultiDimArray< T > *t2)
a specialized function for subtracting two multiDimArrays
MultiDimArray< T > * divide2MultiDimArrays(const MultiDimArray< T > *t1, const MultiDimArray< T > *t2)
a specialized function for dividing two multiDimArrays
void pointerOperators4MultiDimInit()
the function used to register all the operators on multidimImplementations over pointers types.
MultiDimFunctionGraph< T > * divide2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for dividing two multiDimArrays
MultiDimArray< T * > * divide2MultiDimArrays4Pointers(const MultiDimArray< T * > *t1, const MultiDimArray< T * > *t2)
a specialized function for dividing two multiDimArrays
MultiDimArray< T > * add2MultiDimArrays(const MultiDimArray< T > *t1, const MultiDimArray< T > *t2)
a specialized function for summing two multiDimArrays
MultiDimArray< T * > * subtract2MultiDimArrays4Pointers(const MultiDimArray< T * > *t1, const MultiDimArray< T * > *t2)
a specialized function for subtracting two multiDimArrays
MultiDimFunctionGraph< T > * multiply2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for multiplying two multiDimArrays
MultiDimFunctionGraph< T > * subtract2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for subtracting two multiDimArrays
MultiDimArray< T * > * add2MultiDimArrays4Pointers(const MultiDimArray< T * > *t1, const MultiDimArray< T * > *t2)
a specialized function for summing two multiDimArrays
MultiDimArray< T > * multiply2MultiDimArrays(const MultiDimArray< T > *t1, const MultiDimArray< T > *t2)
a specialized function for multiplying two multiDimArrays
MultiDimFunctionGraph< T > * add2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for summing two multiDimArrays
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void registerOperator(std::string_view operation_name, std::string_view type1, std::string_view type2, typename OperatorRegister4MultiDim< GUM_ELEMENT >::OperatorPtr function)
A function to more easily register new operators in MultiDims.
the pattern used by all "basename" binary operators
the pattern used by all binary MultiDimArrays operators
the pattern used by all binary MultiDimFunctionGraphs operators
the pattern used by all binary MultiDimImplementations operators
A container for registering binary functions on multiDimImplementations.
Efficient functionals for combining multiDims.