aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
completeProjectionPattern4MultiDimArray.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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
49
50// check if we allowed these patterns to be used
51#ifndef GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
52
53// #warning To use completeProjectionPattern, you must define
54// GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
55
56#else
57namespace gum {
58
59 // a specialized function for projecting a multiDimArray over all its variables
60
61# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME
62# define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
63
64 template < typename GUM_SCALAR >
65 GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME(const MultiDimArray< GUM_SCALAR >* table,
66 Instantiation* instantiation)
67# endif
68
69 // clang-format off
70
71#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME
72#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
73#define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
74 template <typename GUM_SCALAR>
75 GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME(
76 const MultiDimArray<GUM_SCALAR*>* table,
77 Instantiation* instantiation )
78#endif
79
80#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F
81#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
82 template <typename GUM_SCALAR>
83 GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F(
84 const MultiDimArray<GUM_SCALAR>* table,
85 GUM_SCALAR ( *f )( const GUM_SCALAR&,
86 const GUM_SCALAR& ),
87 Instantiation* instantiation )
88#endif
89
90#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
91#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
92#define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
93 template <typename GUM_SCALAR>
94 GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F(
95 const MultiDimArray<GUM_SCALAR*>* table,
96 GUM_SCALAR* ( *f )( const GUM_SCALAR const*,
97 const GUM_SCALAR const* ),
98 Instantiation* instantiation )
99#endif
100
101#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME
102#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
103 template <typename GUM_SCALAR>
104 GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME(
106 Instantiation* instantiation )
107#endif
108
109#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME
110#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
111#define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
112 template <typename GUM_SCALAR>
113 GUM_SCALAR*
114 GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME(
116 Instantiation* instantiation )
117#endif
118
119 // clang-format on
120
121 {
122
123# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME
124 const auto table = reinterpret_cast< const MultiDimArray< GUM_SCALAR >* >(ttable);
125# endif
126
127# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME
128 const auto table = reinterpret_cast< const MultiDimArray< GUM_SCALAR* >* >(ttable);
129# endif
130
131 // first, compute the domain size of the table:
132 Size nb_elt = table->domainSize();
133
134 // now, parse the table and compute the projection. Start with the
135 // neutral element
136 GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* ptable
137 = const_cast< GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* >(&(table->unsafeGet(0)));
138 GUM_SCALAR current_val = GUM_MULTI_DIM_COMPLETE_PROJECTION_NEUTRAL;
139
140 if (instantiation) {
141 GUM_SCALAR old_val = current_val;
142 GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* current_ptr = ptable;
143 GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* deb_ptr = ptable;
144
145 for (Size i = 0; i < nb_elt; ++i, ++ptable) {
146# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
147 GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, *ptable);
148# else
149 GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, *ptable);
150# endif
151
152 if (current_val != old_val) {
153 old_val = current_val;
154 current_ptr = ptable;
155 }
156 }
157
158 // put the table's variables into the instantiation
159 instantiation->forgetMaster();
160 instantiation->clear();
161 const Sequence< const DiscreteVariable* >& table_vars = table->variablesSequence();
162
163 for (const auto var: table_vars)
164 instantiation->add(*var);
165
166 // return the result
167 table->fromOffset(*instantiation, Size(current_ptr - deb_ptr));
168# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
169 return *current_ptr;
170# else
171 return current_val;
172# endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
173 } else {
174# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
175 GUM_SCALAR old_val = current_val;
176 GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* current_ptr = ptable;
177
178 for (Size i = 0; i < nb_elt; ++i, ++ptable) {
179 GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, *ptable);
180
181 if (current_val != old_val) {
182 old_val = current_val;
183 current_ptr = ptable;
184 }
185 }
186
187 return *current_ptr;
188# else
189
190 for (Size i = 0; i < nb_elt; ++i, ++ptable) {
191 GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, *ptable);
192 }
193
194 return current_val;
195# endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
196 }
197 }
198
199# undef GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE
200
201# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
202# undef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
203# endif
204
205} /* End of namespace gum */
206
207#endif /* GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED */
Class for assigning/browsing values to tuples of discrete variables.
Multidimensional matrix stored as an array in memory.
<agrum/base/multidim/multiDimImplementation.h>
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46