aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimProjection_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-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#pragma once
41
42
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53# include <limits>
54
55# include <agrum/agrum.h>
56
57namespace gum {
58
60 template < class TABLE >
61 MultiDimProjection< TABLE >::MultiDimProjection(TABLE (*proj)(const TABLE&,
62 const gum::VariableSet&)) :
63 proj_(proj) {
64 // for debugging purposes
65 GUM_CONSTRUCTOR(MultiDimProjection);
66 }
67
69 template < class TABLE >
70 MultiDimProjection< TABLE >::MultiDimProjection(const MultiDimProjection< TABLE >& from) :
71 proj_(from.proj_) {
72 // for debugging purposes
73 GUM_CONS_CPY(MultiDimProjection);
74 }
75
77 template < class TABLE >
78 MultiDimProjection< TABLE >::~MultiDimProjection() {
79 // for debugging purposes
80 GUM_DESTRUCTOR(MultiDimProjection);
81 }
82
84 template < class TABLE >
85 MultiDimProjection< TABLE >* MultiDimProjection< TABLE >::clone() const {
86 return new MultiDimProjection< TABLE >(*this);
87 }
88
90 template < class TABLE >
91 INLINE TABLE* MultiDimProjection< TABLE >::execute(const TABLE& table,
92 const gum::VariableSet& del_vars) const {
93 // projected constants are equal to the same constant
94 if (table.variablesSequence().empty()) return new TABLE(table);
95 else return new TABLE(proj_(table, del_vars));
96 }
97
99 template < class TABLE >
100 INLINE void MultiDimProjection< TABLE >::execute(TABLE& container,
101 const TABLE& table,
102 const gum::VariableSet& del_vars) const {
103 if (table.variablesSequence().empty()) container = table;
104 else container = proj_(table, del_vars);
105 }
106
108 template < class TABLE >
109 std::pair< ScheduleOperator*, const IScheduleMultiDim* >
110 MultiDimProjection< TABLE >::operations(const IScheduleMultiDim* table,
111 const gum::VariableSet& del_vars,
112 const bool is_result_persistent) const {
113 auto op
114 = new ScheduleProjection< TABLE >(dynamic_cast< const ScheduleMultiDim< TABLE >& >(*table),
115 del_vars,
116 proj_,
117 is_result_persistent);
118 return {op, &op->result()};
119 }
120
122 template < class TABLE >
123 INLINE const IScheduleMultiDim*
124 MultiDimProjection< TABLE >::schedule(Schedule& schedule,
125 const IScheduleMultiDim* table,
126 const gum::VariableSet& del_vars,
127 const bool is_result_persistent) const {
128 const auto& xtable = dynamic_cast< const ScheduleMultiDim< TABLE >& >(*table);
129 const auto& op = schedule.emplaceProjection(xtable, del_vars, proj_, is_result_persistent);
130 return op.results()[0];
131 }
132
134 template < class TABLE >
135 void MultiDimProjection< TABLE >::setProjectionFunction(TABLE (*proj)(const TABLE&,
136 const gum::VariableSet&)) {
137 proj_ = proj;
138 }
139
141 template < class TABLE >
142 INLINE TABLE (*MultiDimProjection< TABLE >::projectionFunction())(const TABLE&,
143 const gum::VariableSet&) {
144 return proj_;
145 }
146
149 template < class TABLE >
150 INLINE double MultiDimProjection< TABLE >::nbOperations(const TABLE& table,
151 const gum::VariableSet& del_vars) const {
152 return double(table.domainSize());
153 }
154
157 template < class TABLE >
158 INLINE double
159 MultiDimProjection< TABLE >::nbOperations(const Sequence< const DiscreteVariable* >& vars,
160 const gum::VariableSet& del_vars) const {
161 double res = 1.0;
162 for (const auto var: vars) {
163 res *= double(var->domainSize());
164 }
165 return res;
166 }
167
169 template < class TABLE >
170 INLINE std::pair< double, double >
171 MultiDimProjection< TABLE >::memoryUsage(const Sequence< const DiscreteVariable* >& vars,
172 const gum::VariableSet& del_vars) const {
173 auto res = double(sizeof(value_type));
174
175 for (const auto var: vars) {
176 if (!del_vars.contains(var)) res *= double(var->domainSize());
177 }
178
179 res += double(sizeof(TABLE));
180 return {res, res};
181 }
182
184 template < class TABLE >
185 INLINE std::pair< double, double >
186 MultiDimProjection< TABLE >::memoryUsage(const TABLE& table,
187 const gum::VariableSet& del_vars) const {
188 return memoryUsage(table.variablesSequence(), del_vars);
189 }
190
191} /* namespace gum */
192
193#endif /* DOXYGEN_SHOULD_SKIP_THIS */
MultiDimProjection(TABLE(*proj)(const TABLE &, const gum::VariableSet &))
Default constructor.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet