aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
multiDimAggregator_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/**
23
* @file
24
* @brief MultiDimAggregator
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27
*/
28
29
// to ease parser in IDEs
30
#
include
<
agrum
/
tools
/
multidim
/
aggregators
/
multiDimAggregator
.
h
>
31
32
namespace
gum
{
33
namespace
aggregator
{
34
35
// Default constructor
36
template
<
typename
GUM_SCALAR >
37
INLINE MultiDimAggregator<
GUM_SCALAR
>::
MultiDimAggregator
() :
38
MultiDimReadOnly
<
GUM_SCALAR
>(),
decomposable_
(
false
) {
39
GUM_CONSTRUCTOR
(
MultiDimAggregator
);
40
}
41
42
// Default constructor
43
template
<
typename
GUM_SCALAR
>
44
INLINE
MultiDimAggregator
<
GUM_SCALAR
>::
MultiDimAggregator
(
45
const
MultiDimAggregator
<
GUM_SCALAR
>&
from
) :
46
MultiDimReadOnly
<
GUM_SCALAR
>(
from
),
47
decomposable_
(
from
.
decomposable_
) {
48
GUM_CONS_CPY
(
MultiDimAggregator
);
49
}
50
51
// destructor
52
template
<
typename
GUM_SCALAR
>
53
INLINE
MultiDimAggregator
<
GUM_SCALAR
>::~
MultiDimAggregator
() {
54
GUM_DESTRUCTOR
(
MultiDimAggregator
);
55
}
56
57
template
<
typename
GUM_SCALAR
>
58
Idx
MultiDimAggregator
<
GUM_SCALAR
>::
buildValue_
(
const
Instantiation
&
i
)
const
{
59
if
(
this
->
nbrDim
() == 1)
return
neutralElt_
();
60
61
// is i equal to f(f(f(f...(j_,neutral_elt))))
62
Idx
current
=
neutralElt_
();
63
64
bool
stop_iteration
=
false
;
65
66
for
(
Idx
j
= 1;
j
<
this
->
nbrDim
();
j
++) {
67
current
=
fold_
(
this
->
variable
(
j
),
i
.
val
(
this
->
variable
(
j
)),
current
,
stop_iteration
);
68
69
if
(
stop_iteration
)
break
;
70
}
71
72
return
current
;
73
}
74
75
template
<
typename
GUM_SCALAR
>
76
GUM_SCALAR
MultiDimAggregator
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
77
if
(
this
->
nbrDim
() < 1) {
78
GUM_ERROR
(
OperationNotAllowed
,
"Not enough variable for an aggregator : "
<< *
this
)
79
}
80
81
const
DiscreteVariable
&
agg
=
this
->
variable
((
Idx
)0);
82
auto
current
=
buildValue_
(
i
);
83
84
// truncate to fit in aggreegator domain size
85
if
(
current
>=
agg
.
domainSize
())
current
=
agg
.
domainSize
() - 1;
86
87
return
(
i
.
val
(
agg
) ==
current
) ? (
GUM_SCALAR
)1.0 : (
GUM_SCALAR
)0.0;
88
}
89
90
template
<
typename
GUM_SCALAR
>
91
std
::
string
MultiDimAggregator
<
GUM_SCALAR
>::
toString
()
const
{
92
std
::
stringstream
s
;
93
s
<<
this
->
variable
(0) <<
"="
<<
aggregatorName
() <<
"("
;
94
95
for
(
Idx
i
= 1;
i
<
this
->
nbrDim
();
i
++) {
96
if
(
i
> 1)
s
<<
","
;
97
98
s
<<
this
->
variable
(
i
);
99
}
100
101
s
<<
")"
;
102
103
return
s
.
str
();
104
}
105
template
<
typename
GUM_SCALAR
>
106
void
MultiDimAggregator
<
GUM_SCALAR
>::
copyFrom
(
107
const
MultiDimContainer
<
GUM_SCALAR
>&
src
)
const
{
108
auto
p
=
dynamic_cast
<
const
MultiDimAggregator
<
GUM_SCALAR
>* >(&
src
);
109
if
(
p
==
nullptr
) {
110
MultiDimReadOnly
<
GUM_SCALAR
>::
copyFrom
(
src
);
111
}
else
{
112
if
(
p
->
name
() !=
this
->
name
()) {
113
GUM_ERROR
(
OperationNotAllowed
,
114
"Can not copy from a "
<<
p
->
name
() <<
" to a "
<<
this
->
name
());
115
}
116
}
117
// it the types aree consistant, nothing to do...
118
}
119
120
template
<
typename
GUM_SCALAR
>
121
INLINE
bool
MultiDimAggregator
<
GUM_SCALAR
>::
isDecomposable
()
const
{
122
return
decomposable_
;
123
}
124
125
126
// returns the name of the implementation
127
template
<
typename
GUM_SCALAR
>
128
const
std
::
string
&
MultiDimAggregator
<
GUM_SCALAR
>::
name
()
const
{
129
static
const
std
::
string
str
=
"MultiDimAggregator"
;
130
return
str
;
131
}
132
133
// For friendly displaying the content of the variable.
134
template
<
typename
GUM_SCALAR
>
135
INLINE
std
::
ostream
&
operator
<<(
std
::
ostream
&
s
,
const
MultiDimAggregator
<
GUM_SCALAR
>&
ag
) {
136
return
s
<<
ag
.
toString
();
137
}
138
}
/* namespace aggregator */
139
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::aggregator::operator<<
INLINE std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_SCALAR > &ag)
For friendly displaying the content of the array.
Definition:
multiDimAggregator_tpl.h:135