aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
completeProjectionPattern4BaseName.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 multiDimImplementation over a subset
60 // of its variables
61
62# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME
63# define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
64
65 template < typename GUM_SCALAR >
66 GUM_SCALAR
67 GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME(const MultiDimImplementation< GUM_SCALAR >* table,
68 Instantiation* instantiation = 0)
69# endif
70
71 // clang-format off
72
73#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME
74#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
75#define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
76 template <typename GUM_SCALAR>
77 GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME(
79 Instantiation* instantiation = 0 )
80#endif
81
82#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F
83#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
84 template <typename GUM_SCALAR>
85 GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F(
87 GUM_SCALAR ( *f )( const GUM_SCALAR&, const GUM_SCALAR& ),
88 Instantiation* instantiation = 0 )
89#endif
90
91#ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
92#define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
93#define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
94 template <typename GUM_SCALAR>
95 GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F(
97 GUM_SCALAR* ( *f )(const GUM_SCALAR const*,
98 const GUM_SCALAR const*))
99#endif
100
101 // clang-format on
102
103 {
104
105# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
106 Instantiation* instantiation = 0;
107# endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
108
109 // first, compute the domain size of the table:
110 Size nb_elt = table->domainSize();
111
112 // now, parse the table and compute the projection. Start with the
113 // neutral element
114 // TODO: change into Instantiation table_inst(table); when Tensors will support
115 // thread-safe creations of Instantiations
116 Instantiation table_inst;
117 for (const auto var: table->variablesSequence())
118 table_inst.add(*var);
119 GUM_SCALAR current_val = GUM_MULTI_DIM_COMPLETE_PROJECTION_NEUTRAL;
120
121 if (instantiation) {
122 GUM_SCALAR old_val = current_val;
123 Size current_ptr = 0;
124
125 for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
126# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
127 GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
128# else
129 GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
130# endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
131
132 if (current_val != old_val) {
133 old_val = current_val;
134 current_ptr = i;
135 }
136 }
137
138 // put the table's variables into the instantiation
139 instantiation->forgetMaster();
140 instantiation->clear();
141 const Sequence< const DiscreteVariable* >& table_vars = table->variablesSequence();
142
143 for (const auto var: table_vars)
144 instantiation->add(*var);
145
146 // return the result
147 instantiation->operator+=(current_ptr);
148 return table->get(*instantiation);
149 } else {
150# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
151 GUM_SCALAR old_val = current_val;
152 Size current_ptr = 0;
153
154 for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
155 GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
156
157 if (current_val != old_val) {
158 old_val = current_val;
159 current_ptr = i;
160 }
161 }
162
163 table_inst.setFirst();
164 table_inst += current_ptr return table->get(table_inst);
165# else
166
167 for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
168 GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
169 }
170
171 return current_val;
172# endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
173 }
174 }
175
176# undef GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE
177
178# ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
179# undef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
180# endif
181
182} /* End of namespace gum */
183
184#endif /* GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED */
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
<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