aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
multiDimValueAggregator_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
50
51#include <algorithm>
52#include <cmath>
53
54// to ease parser in IDEs
56
57namespace gum {
58 namespace aggregator {
59
60 template < typename GUM_SCALAR >
65
66 template < typename GUM_SCALAR >
69 MultiDimAggregator< GUM_SCALAR >(from) {
70 GUM_CONS_CPY(MultiDimValueAggregator);
71 }
72
73 template < typename GUM_SCALAR >
77
78 template < typename GUM_SCALAR >
80 const DiscreteVariable& v) const {
81 if (!v.isNumerical()) {
83 "The aggregator " << this->aggregatorName() << " needs numerical variables, "
84 << v.name() << " is labelized")
85 }
86 }
87
88 template < typename GUM_SCALAR >
90 const DiscreteVariable& v) const {
91 if (this->nbrDim() > 0 && v.isNumerical() != this->variable(0).isNumerical()) {
93 "The aggregator " << this->aggregatorName()
94 << " cannot mix labelized and numerical variables ("
95 << this->variable(0).name() << " and " << v.name() << ")")
96 }
97 }
98
99 template < typename GUM_SCALAR >
101 Idx j) const {
102 const auto& v = this->variable(j);
103 // fast path (no lookup of v in i) when i has the variables of the aggregator in its order
104 if (j < i.nbrDim() && &i.variable(j) == &v) return v.numerical(i.val(j));
105 return v.numerical(i.val(v));
106 }
107
108 template < typename GUM_SCALAR >
110 const auto& agg = this->variable((Idx)0);
111 if (agg.isNumerical()) return agg.closestIndex(value);
112
113 // rounded half down (as RangeVariable::closestIndex()), saturated at both bounds
114 const auto last = static_cast< double >(agg.domainSize() - 1);
115 return static_cast< Idx >(std::ceil(std::clamp(value, 0.0, last) - 0.5));
116 }
117
118 } /* namespace aggregator */
119} /* namespace gum */
Base class for discrete random variable.
virtual double numerical(Idx indice) const =0
get a numerical representation of the indice-th value.
bool isNumerical() const
Class for assigning/browsing values to tuples of discrete variables.
Idx val(Idx i) const
Returns the current value of the variable at position i.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
const DiscreteVariable & variable(Idx i) const override
Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Exception : wrong type for this operation.
const std::string & name() const
returns the name of the variable
<agrum/base/multidim/aggregators/multiDimAggregator.h>
virtual std::optional< Idx > value() const
Returns the scalar parameter of this aggregator, if it has one.
double parentValue_(const gum::Instantiation &i, Idx j) const
Idx valueToIndex_(double value) const
Maps a value onto an index of the aggregator.
void checkNumericalVariable_(const DiscreteVariable &v) const
void checkHomogeneousVariable_(const DiscreteVariable &v) const
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size Idx
Type for indexes.
Definition types.h:79
MultiDimValueAggregator: aggregators computed on the values of the parents.
Aggregators are functional description of CPTs.
Definition agrum.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46