aGrUM 3.1.1
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-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
51
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
55# include <limits>
56
57# include <agrum/agrum.h>
58
59namespace gum {
60
62 template < class TABLE >
63 MultiDimProjection< TABLE >::MultiDimProjection(TABLE (*proj)(const TABLE&,
64 const gum::VariableSet&)) :
65 proj_(proj) {
66 // for debugging purposes
67 GUM_CONSTRUCTOR(MultiDimProjection);
68 }
69
71 template < class TABLE >
72 MultiDimProjection< TABLE >::MultiDimProjection(const MultiDimProjection< TABLE >& from) :
73 proj_(from.proj_) {
74 // for debugging purposes
75 GUM_CONS_CPY(MultiDimProjection);
76 }
77
79 template < class TABLE >
80 MultiDimProjection< TABLE >::~MultiDimProjection() {
81 // for debugging purposes
82 GUM_DESTRUCTOR(MultiDimProjection);
83 }
84
86 template < class TABLE >
87 MultiDimProjection< TABLE >* MultiDimProjection< TABLE >::clone() const {
88 return new MultiDimProjection< TABLE >(*this);
89 }
90
92 template < class TABLE >
93 TABLE* MultiDimProjection< TABLE >::execute(const TABLE& table,
94 const gum::VariableSet& del_vars) const {
95 // projected constants are equal to the same constant
96 if (table.variablesSequence().empty()) return new TABLE(table);
97 else return new TABLE(proj_(table, del_vars));
98 }
99
101 template < class TABLE >
102 void MultiDimProjection< TABLE >::execute(TABLE& container,
103 const TABLE& table,
104 const gum::VariableSet& del_vars) const {
105 if (table.variablesSequence().empty()) container = table;
106 else container = proj_(table, del_vars);
107 }
108
110 template < class TABLE >
111 std::pair< ScheduleOperator*, const IScheduleMultiDim* >
112 MultiDimProjection< TABLE >::operations(const IScheduleMultiDim* table,
113 const gum::VariableSet& del_vars,
114 const bool is_result_persistent) const {
115 auto op
116 = new ScheduleProjection< TABLE >(dynamic_cast< const ScheduleMultiDim< TABLE >& >(*table),
117 del_vars,
118 proj_,
119 is_result_persistent);
120 return {op, &op->result()};
121 }
122
124 template < class TABLE >
125 const IScheduleMultiDim*
126 MultiDimProjection< TABLE >::schedule(Schedule& schedule,
127 const IScheduleMultiDim* table,
128 const gum::VariableSet& del_vars,
129 const bool is_result_persistent) const {
130 const auto& xtable = dynamic_cast< const ScheduleMultiDim< TABLE >& >(*table);
131 const auto& op = schedule.emplaceProjection(xtable, del_vars, proj_, is_result_persistent);
132 return op.results()[0];
133 }
134
136 template < class TABLE >
137 void MultiDimProjection< TABLE >::setProjectionFunction(TABLE (*proj)(const TABLE&,
138 const gum::VariableSet&)) {
139 proj_ = proj;
140 }
141
143 template < class TABLE >
144 TABLE (*MultiDimProjection< TABLE >::projectionFunction())(const TABLE&,
145 const gum::VariableSet&) {
146 return proj_;
147 }
148
151 template < class TABLE >
152 double MultiDimProjection< TABLE >::nbOperations(const TABLE& table,
153 const gum::VariableSet& del_vars) const {
154 return double(table.domainSize());
155 }
156
159 template < class TABLE >
160 double MultiDimProjection< TABLE >::nbOperations(const Sequence< const DiscreteVariable* >& vars,
161 const gum::VariableSet& del_vars) const {
162 double res = 1.0;
163 for (const auto var: vars) {
164 res *= double(var->domainSize());
165 }
166 return res;
167 }
168
170 template < class TABLE >
171 std::pair< double, double >
172 MultiDimProjection< TABLE >::memoryUsage(const Sequence< const DiscreteVariable* >& vars,
173 const gum::VariableSet& del_vars) const {
174 auto res = double(sizeof(value_type));
175
176 for (const auto var: vars) {
177 if (!del_vars.contains(var)) res *= double(var->domainSize());
178 }
179
180 res += double(sizeof(TABLE));
181 return {res, res};
182 }
183
185 template < class TABLE >
186 std::pair< double, double >
187 MultiDimProjection< TABLE >::memoryUsage(const TABLE& table,
188 const gum::VariableSet& del_vars) const {
189 return memoryUsage(table.variablesSequence(), del_vars);
190 }
191
192} /* namespace gum */
193
194#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:468
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet