aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
completeProjections4MultiDim.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 projecting multidim tables over all their
25  * variables
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #ifndef GUM_COMPLETE_PROJECTIONS_4_MULTI_DIM_H
31 #define GUM_COMPLETE_PROJECTIONS_4_MULTI_DIM_H
32 
33 #include <agrum/tools/multidim/implementations/multiDimArray.h>
34 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
35 #include <agrum/tools/multidim/instantiation.h>
36 
37 namespace gum {
38 
39  // ==========================================================================
40  /// @name Project Max Operators
41  // ==========================================================================
42  /// @{
43 
44  /**
45  * @brief A specialized function for projecting a multiDimArray using a Max
46  * operator.
47  * @ingroup multidim_op_group
48  *
49  * The function projects a table over all its variables and, thus, returns
50  * the max value stored in the table.
51  *
52  * @param table a pointer on the multidim table to be projected
53  *
54  * @param instantiation if instantiation is different from 0, then the
55  * projection function stores in the instantation the values of the variables
56  * that led to the max (hence, the argmax).
57  *
58  * @return the max value stored in the multiDimArray
59  *
60  * @warning In practice, do not use this function but rather
61  * operator/function projectMax on MultiDimImplementation. This operator will
62  * take care to select this function if it is appropriate for your
63  * projection.
64  */
65  template < typename GUM_SCALAR >
66  GUM_SCALAR projectMaxMultiDimArray(const MultiDimArray< GUM_SCALAR >* table,
67  Instantiation* instantiation = 0);
68 
69  /**
70  * @brief A specialized function for projecting a multiDimArray using a Max
71  * operator.
72  * @ingroup multidim_op_group
73  *
74  * The function projects a table over all its variables and, thus, returns
75  * the max value stored in the table.
76  *
77  * @param table a pointer on the multidim table to be projected
78  *
79  * @param instantiation if instantiation is different from 0, then the
80  * projection function stores in the instantation the values of the variables
81  * that led to the max (hence, the argmax).
82  *
83  * @return the max value stored in the multiDimArray
84  *
85  * @warning In practice, do not use this function but rather
86  * operator/function projectMax on MultiDimImplementation. This operator will
87  * take care to select this function if it is appropriate for your
88  * projection.
89  */
90  template < typename GUM_SCALAR >
91  GUM_SCALAR
92  projectMaxMultiDimArray(const MultiDimImplementation< GUM_SCALAR >* table,
93  Instantiation* instantiation = 0);
94 
95  /**
96  * @brief A specialized function for projecting a multiDimArray using a Max
97  * operator.
98  * @ingroup multidim_op_group
99  *
100  * The function projects a table over all its variables and, thus, returns
101  * the max value stored in the table.
102  *
103  * @param table a pointer on the multidim table to be projected
104  *
105  * @param instantiation if instantiation is different from 0, then the
106  * projection function stores in the instantation the values of the variables
107  * that led to the max (hence, the argmax).
108  *
109  * @return the max value stored in the multiDimArray
110  *
111  * @warning In practice, do not use this function but rather
112  * operator/function projectMax on MultiDimImplementation. This operator will
113  * take care to select this function if it is appropriate for your
114  * projection.
115  */
116  template < typename GUM_SCALAR >
117  GUM_SCALAR*
118  projectMaxMultiDimArray4Pointers(const MultiDimArray< GUM_SCALAR* >* table,
119  Instantiation* instantiation = 0);
120 
121  /**
122  * @brief A specialized function for projecting a multiDimArray using a Max
123  * operator.
124  * @ingroup multidim_op_group
125  *
126  * The function projects a table over all its variables and, thus, returns
127  * the max value stored in the table.
128  *
129  * @param table a pointer on the multidim table to be projected
130  *
131  * @param instantiation if instantiation is different from 0, then the
132  * projection function stores in the instantation the values of the variables
133  * that led to the max (hence, the argmax).
134  *
135  * @return the max value stored in the multiDimArray
136  *
137  * @warning In practice, do not use this function but rather
138  * operator/function projectMax on MultiDimImplementation. This operator will
139  * take care to select this function if it is appropriate for your
140  * projection.
141  */
142  template < typename GUM_SCALAR >
144  const MultiDimImplementation< GUM_SCALAR* >* table,
145  Instantiation* instantiation = 0);
146 
147  /// @}
148  // ==========================================================================
149  /// @name Project Min Operators
150  // ==========================================================================
151  /// @{
152 
153  /**
154  * @brief A specialized function for projecting a multiDimArray using a Min
155  * operator.
156  * @ingroup multidim_op_group
157  *
158  * The function projects a table over all its variables and, thus, returns
159  * the min value stored in the table
160  *
161  * @return the min value stored in the multiDimArray
162  *
163  * @param table a pointer on the multidim table to be projected
164  *
165  * @param instantiation if instantiation is different from 0, then the
166  * projection function stores in the instantation the values of the variables
167  * that led to the min (hence, the argmin).
168  *
169  * @warning In practice, do not use this function but rather
170  * operator/function projectMin on MultiDimImplementation. This operator will
171  * take care to select this function if it is appropriate for your
172  * projection.
173  */
174  template < typename GUM_SCALAR >
175  GUM_SCALAR projectMinMultiDimArray(const MultiDimArray< GUM_SCALAR >* table,
176  Instantiation* instantiation = 0);
177 
178  /**
179  * @brief A specialized function for projecting a multiDimArray using a Min
180  * operator.
181  * @ingroup multidim_op_group
182  *
183  * The function projects a table over all its variables and, thus, returns
184  * the min value stored in the table
185  *
186  * @return the min value stored in the multiDimArray
187  *
188  * @param table a pointer on the multidim table to be projected
189  *
190  * @param instantiation if instantiation is different from 0, then the
191  * projection function stores in the instantation the values of the variables
192  * that led to the min (hence, the argmin).
193  *
194  * @warning In practice, do not use this function but rather
195  * operator/function projectMin on MultiDimImplementation. This operator will
196  * take care to select this function if it is appropriate for your
197  * projection.
198  */
199  template < typename GUM_SCALAR >
200  GUM_SCALAR
201  projectMinMultiDimArray(const MultiDimImplementation< GUM_SCALAR >* table,
202  Instantiation* instantiation = 0);
203 
204  /**
205  * @brief A specialized function for projecting a multiDimArray using a Min
206  * operator.
207  * @ingroup multidim_op_group
208  *
209  * The function projects a table over all its variables and, thus, returns
210  * the min value stored in the table
211  *
212  * @return the min value stored in the multiDimArray
213  *
214  * @param table a pointer on the multidim table to be projected
215  *
216  * @param instantiation if instantiation is different from 0, then the
217  * projection function stores in the instantation the values of the variables
218  * that led to the min (hence, the argmin).
219  *
220  * @warning In practice, do not use this function but rather
221  * operator/function projectMin on MultiDimImplementation. This operator will
222  * take care to select this function if it is appropriate for your
223  * projection.
224  */
225  template < typename GUM_SCALAR >
226  GUM_SCALAR*
227  projectMinMultiDimArray4Pointers(const MultiDimArray< GUM_SCALAR* >* table,
228  Instantiation* instantiation = 0);
229 
230  /**
231  * @brief A specialized function for projecting a multiDimArray using a Min
232  * operator.
233  * @ingroup multidim_op_group
234  *
235  * The function projects a table over all its variables and, thus, returns
236  * the min value stored in the table
237  *
238  * @return the min value stored in the multiDimArray
239  *
240  * @param table a pointer on the multidim table to be projected
241  *
242  * @param instantiation if instantiation is different from 0, then the
243  * projection function stores in the instantation the values of the variables
244  * that led to the min (hence, the argmin).
245  *
246  * @warning In practice, do not use this function but rather
247  * operator/function projectMin on MultiDimImplementation. This operator will
248  * take care to select this function if it is appropriate for your
249  * projection.
250  */
251  template < typename GUM_SCALAR >
253  const MultiDimImplementation< GUM_SCALAR* >* table,
254  Instantiation* instantiation = 0);
255 
256  /// @}
257  // ==========================================================================
258  /// @name Project Sum Operators
259  // ==========================================================================
260  /// @{
261 
262  /**
263  * @brief a specialized function for projecting a multiDimArray using a Sum
264  * operator
265  * @ingroup multidim_op_group
266  *
267  * The function projects a table over all its variables and, thus, returns
268  * the sum of the values stored in the table
269  *
270  * @return the sum of the values stored in the multiDimArray
271  *
272  * @param table a pointer on the multidim table to be projected
273  *
274  * @param instantiation It shall always be 0 else instantiation is updated
275  * with a meaningless value. In addition, setting instantiation to a value
276  * different than 0 will induce more computations and thus will slow down the
277  * projection.
278  *
279  * @warning In practice, do not use this function but rather
280  * operator/function projectSum on MultiDimImplementation. This operator will
281  * take care to select this function if it is appropriate for your
282  * projection.
283  */
284  template < typename GUM_SCALAR >
285  GUM_SCALAR projectSumMultiDimArray(const MultiDimArray< GUM_SCALAR >* table,
286  Instantiation* instantiation = 0);
287 
288  /**
289  * @brief a specialized function for projecting a multiDimArray using a Sum
290  * operator
291  * @ingroup multidim_op_group
292  *
293  * The function projects a table over all its variables and, thus, returns
294  * the sum of the values stored in the table
295  *
296  * @return the sum of the values stored in the multiDimArray
297  *
298  * @param table a pointer on the multidim table to be projected
299  *
300  * @param instantiation It shall always be 0 else instantiation is updated
301  * with a meaningless value. In addition, setting instantiation to a value
302  * different than 0 will induce more computations and thus will slow down the
303  * projection.
304  *
305  * @warning In practice, do not use this function but rather
306  * operator/function projectSum on MultiDimImplementation. This operator will
307  * take care to select this function if it is appropriate for your
308  * projection.
309  */
310  template < typename GUM_SCALAR >
311  GUM_SCALAR
312  projectSumMultiDimArray(const MultiDimImplementation< GUM_SCALAR >* table,
313  Instantiation* instantiation = 0);
314 
315  /**
316  * @brief a specialized function for projecting a multiDimArray using a Sum
317  * operator
318  * @ingroup multidim_op_group
319  *
320  * The function projects a table over all its variables and, thus, returns
321  * the sum of the values stored in the table
322  *
323  * @return the sum of the values stored in the multiDimArray
324  *
325  * @param table a pointer on the multidim table to be projected
326  *
327  * @param instantiation It shall always be 0 else instantiation is updated
328  * with a meaningless value. In addition, setting instantiation to a value
329  * different than 0 will induce more computations and thus will slow down the
330  * projection.
331  *
332  * @warning In practice, do not use this function but rather
333  * operator/function projectSum on MultiDimImplementation. This operator will
334  * take care to select this function if it is appropriate for your
335  * projection.
336  */
337  template < typename GUM_SCALAR >
338  GUM_SCALAR*
339  projectSumMultiDimArray4Pointers(const MultiDimArray< GUM_SCALAR* >* table,
340  Instantiation* instantiation = 0);
341 
342  /**
343  * @brief a specialized function for projecting a multiDimArray using a Sum
344  * operator
345  * @ingroup multidim_op_group
346  *
347  * The function projects a table over all its variables and, thus, returns
348  * the sum of the values stored in the table
349  *
350  * @return the sum of the values stored in the multiDimArray
351  *
352  * @param table a pointer on the multidim table to be projected
353  *
354  * @param instantiation It shall always be 0 else instantiation is updated
355  * with a meaningless value. In addition, setting instantiation to a value
356  * different than 0 will induce more computations and thus will slow down the
357  * projection.
358  *
359  * @warning In practice, do not use this function but rather
360  * operator/function projectSum on MultiDimImplementation. This operator will
361  * take care to select this function if it is appropriate for your
362  * projection.
363  */
364  template < typename GUM_SCALAR >
366  const MultiDimImplementation< GUM_SCALAR* >* table,
367  Instantiation* instantiation = 0);
368 
369  /// @}
370  // ==========================================================================
371  /// @name Project Multiply Operators
372  // ==========================================================================
373  /// @{
374 
375  /**
376  * @brief a function for projecting a multiDimArray using a Product operator
377  * @ingroup multidim_op_group
378  *
379  * The function projects a table over all its variables and, thus, returns
380  * the product of the values stored in the table
381  *
382  * @return the product of the values stored in the multiDimArray
383  *
384  * @param table a pointer on the multidim table to be projected
385  *
386  * @param instantiation It shall always be 0 else instantiation is updated
387  * with a meaningless value. In addition, setting instantiation to a value
388  * different than 0 will induce more computations and thus will slow down the
389  * projection.
390  *
391  * @warning In practice, do not use this function but rather
392  * operator/function projectProduct on MultiDimImplementation. This operator
393  * will take care to select this function if it is appropriate for your
394  * projection.
395  */
396  template < typename GUM_SCALAR >
397  GUM_SCALAR projectProductMultiDimArray(const MultiDimArray< GUM_SCALAR >* table,
398  Instantiation* instantiation = 0);
399 
400  /**
401  * @brief a function for projecting a multiDimArray using a Product operator
402  * @ingroup multidim_op_group
403  *
404  * The function projects a table over all its variables and, thus, returns
405  * the product of the values stored in the table
406  *
407  * @return the product of the values stored in the multiDimArray
408  *
409  * @param table a pointer on the multidim table to be projected
410  *
411  * @param instantiation It shall always be 0 else instantiation is updated
412  * with a meaningless value. In addition, setting instantiation to a value
413  * different than 0 will induce more computations and thus will slow down the
414  * projection.
415  *
416  * @warning In practice, do not use this function but rather
417  * operator/function projectProduct on MultiDimImplementation. This operator
418  * will take care to select this function if it is appropriate for your
419  * projection.
420  */
421  template < typename GUM_SCALAR >
422  GUM_SCALAR
423  projectProductMultiDimArray(const MultiDimImplementation< GUM_SCALAR >* table,
424  Instantiation* instantiation = 0);
425 
426  /**
427  * @brief a function for projecting a multiDimArray using a Product operator
428  * @ingroup multidim_op_group
429  *
430  * The function projects a table over all its variables and, thus, returns
431  * the product of the values stored in the table
432  *
433  * @return the product of the values stored in the multiDimArray
434  *
435  * @param table a pointer on the multidim table to be projected
436  *
437  * @param instantiation It shall always be 0 else instantiation is updated
438  * with a meaningless value. In addition, setting instantiation to a value
439  * different than 0 will induce more computations and thus will slow down the
440  * projection.
441  *
442  * @warning In practice, do not use this function but rather
443  * operator/function projectProduct on MultiDimImplementation. This operator
444  * will take care to select this function if it is appropriate for your
445  * projection.
446  */
447  template < typename GUM_SCALAR >
449  const MultiDimArray< GUM_SCALAR* >* table,
450  Instantiation* instantiation = 0);
451 
452  /**
453  * @brief a function for projecting a multiDimArray using a Product operator
454  * @ingroup multidim_op_group
455  *
456  * The function projects a table over all its variables and, thus, returns
457  * the product of the values stored in the table
458  *
459  * @return the product of the values stored in the multiDimArray
460  *
461  * @param table a pointer on the multidim table to be projected
462  *
463  * @param instantiation It shall always be 0 else instantiation is updated
464  * with a meaningless value. In addition, setting instantiation to a value
465  * different than 0 will induce more computations and thus will slow down the
466  * projection.
467  *
468  * @warning In practice, do not use this function but rather
469  * operator/function projectProduct on MultiDimImplementation. This operator
470  * will take care to select this function if it is appropriate for your
471  * projection.
472  */
473  template < typename GUM_SCALAR >
475  const MultiDimImplementation< GUM_SCALAR* >* table,
476  Instantiation* instantiation = 0);
477 
478  /// @}
479 
480  /// the function to be used to project a MultiDimImplementation using a Max
481  template < typename GUM_SCALAR >
482  GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR >& table,
483  Instantiation* instantiation = 0);
484 
485  /// the function to be used to project a MultiDimImplementation using a Min
486  template < typename GUM_SCALAR >
487  GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR >& table,
488  Instantiation* instantiation = 0);
489 
490  /// the function to be used to project a MultiDimImplementation using a sum
491  template < typename GUM_SCALAR >
492  GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR >& table,
493  Instantiation* instantiation = 0);
494 
495  /// the function to be used to project a MultiDimImplementation using a
496  /// Product
497  template < typename GUM_SCALAR >
498  GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR >& table,
499  Instantiation* instantiation = 0);
500 
501  // ==========================================================================
502  // DO NOT FORGET TO REGISTER YOUR PROJECTION FUNCTIONS AT THE END OF FILE
503  // completeProjections4MultiDim_tpl.h
504  // ==========================================================================
505 
506  /**
507  * @brief the function used to register all the complete projection operators
508  * on multidimImplementations over non-pointers types.
509  * @ingroup multidim_op_group
510  */
511  template < typename GUM_SCALAR >
513 
514  /**
515  * @brief The function used to register all the complete projections on
516  * multidimImplementations over pointers types.
517  * @ingroup multidim_op_group
518  */
519  template < typename GUM_SCALAR >
521 
522  /**
523  * @brief a class used to register complete projections over non-pointers
524  * types
525  * @ingroup multidim_op_group
526  *
527  * This class is of course completely redundant with function
528  * completeProjections4MultiDimInit. Its aim is to enable specialization of
529  * function completeProjections4MultiDimInit for pointer types: C++ allows
530  * partial specialization of templated classes (e.g., create different
531  * implementations for C<T> and C<T*>) but it does not allows partial
532  * specialization for functions. Hence, by creating a class the primary
533  * purpose of which is to run function completeProjections4MultiDimInit, we
534  * allow this partial specialization. This is most effective to produce
535  * different codes for pointer types and non-pointer types.
536  */
537  template < typename GUM_SCALAR >
539  void init() { completeProjections4MultiDimInit< GUM_SCALAR >(); };
540  };
541 
542  /**
543  * @brief a class used to register complete projections over pointers types
544  * @ingroup multidim_op_group
545  *
546  * This class is of course completely redundant with function
547  * pointerCompleteProjections4MultiDimInit. Its aim is to enable different
548  * implementations of the projections for multidims depending in whether
549  * these multidim contain pointers or not. Actually, C++ allows partial
550  * specialization of templated classes (e.g., create different
551  * implementations for C<T> and C<T*>) but it does not allows partial
552  * specialization for functions. Hence, by creating a class the primary
553  * purpose of which is to run function completeProjections4MultiDimInit or
554  * pointerCompleteProjections4MultiDimInit, we allow this partial
555  * specialization to obtain.
556  * */
557  template < typename GUM_SCALAR >
559  void init() { pointerCompleteProjections4MultiDimInit< GUM_SCALAR >(); };
560  };
561 
562 } /* namespace gum */
563 
564 // always include the templatized implementations
565 #include <agrum/tools/multidim/utils/operators/completeProjections4MultiDim_tpl.h>
566 
567 #endif /* GUM_COMPLETE_PROJECTIONS_4_MULTI_DIM_H */
GUM_SCALAR projectProduct(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Product
GUM_SCALAR projectSumMultiDimArray(const MultiDimImplementation< GUM_SCALAR > *table, Instantiation *instantiation=0)
a specialized function for projecting a multiDimArray using a Sum operatorThe function projects a tab...
GUM_SCALAR projectProductMultiDimArray(const MultiDimImplementation< GUM_SCALAR > *table, Instantiation *instantiation=0)
a function for projecting a multiDimArray using a Product operatorThe function projects a table over ...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
GUM_SCALAR * projectMaxMultiDimArray4Pointers(const MultiDimImplementation< GUM_SCALAR * > *table, Instantiation *instantiation=0)
A specialized function for projecting a multiDimArray using a Max operator.
GUM_SCALAR * projectMinMultiDimArray4Pointers(const MultiDimImplementation< GUM_SCALAR * > *table, Instantiation *instantiation=0)
A specialized function for projecting a multiDimArray using a Min operator.
GUM_SCALAR projectMaxMultiDimArray(const MultiDimImplementation< GUM_SCALAR > *table, Instantiation *instantiation=0)
A specialized function for projecting a multiDimArray using a Max operator.
void completeProjections4MultiDimInit()
the function used to register all the complete projection operators on multidimImplementations over n...
GUM_SCALAR * projectSumMultiDimArray4Pointers(const MultiDimImplementation< GUM_SCALAR * > *table, Instantiation *instantiation=0)
a specialized function for projecting a multiDimArray using a Sum operatorThe function projects a tab...
GUM_SCALAR projectSum(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a sum
void pointerCompleteProjections4MultiDimInit()
The function used to register all the complete projections on multidimImplementations over pointers t...
GUM_SCALAR projectMinMultiDimArray(const MultiDimImplementation< GUM_SCALAR > *table, Instantiation *instantiation=0)
A specialized function for projecting a multiDimArray using a Min operator.
GUM_SCALAR projectMin(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Min
GUM_SCALAR projectMax(const MultiDimImplementation< GUM_SCALAR > &table, Instantiation *instantiation=0)
the function to be used to project a MultiDimImplementation using a Max
GUM_SCALAR * projectProductMultiDimArray4Pointers(const MultiDimImplementation< GUM_SCALAR * > *table, Instantiation *instantiation=0)
a function for projecting a multiDimArray using a Product operatorThe function projects a table over ...
a class used to register complete projections over non-pointers typesThis class is of course complete...