aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
numericalDiscreteVariable.cpp
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
42
#include <
agrum/base/variables/numericalDiscreteVariable.h
>
43
44
#include <
agrum/base/core/math/math_utils.h
>
45
46
#ifdef GUM_NO_INLINE
47
# include <
agrum/base/variables/numericalDiscreteVariable_inl.h
>
48
#endif
/* GUM_NO_INLINE */
49
50
namespace
gum
{
51
53
NumericalDiscreteVariable::NumericalDiscreteVariable
(std::string_view aName,
54
std::string_view aDesc,
55
const
std::vector< double >&
domain
) :
56
DiscreteVariable
(aName, aDesc) {
57
// get the values in increasing order
58
_domain_
.reserve(
domain
.size());
59
for (
const
auto
value:
domain
) {
60
if
(!
gum::isfinite< double >
(value)) {
61
GUM_ERROR
(
DefaultInLabel
,
"Value '"
<< value <<
"' is not allowed for variable "
<< aName)
62
}
63
if
(!
isValue
(value)) {
_domain_
.push_back(value); }
64
}
65
std::sort(
_domain_
.begin(),
_domain_
.end());
66
67
// for debugging purposes
68
GUM_CONSTRUCTOR(
NumericalDiscreteVariable
)
69
}
70
72
NumericalDiscreteVariable::NumericalDiscreteVariable
(std::string_view aName,
73
std::string_view aDesc,
74
double
first,
75
double
last,
76
Size
nb) :
DiscreteVariable
(aName, aDesc) {
77
// store the sorted values into a sequence
78
if
(nb < 2)
GUM_ERROR
(
ArgumentError
,
"The size of the domain must be >2 (here :"
<< nb <<
")."
)
79
if
(first >= last)
80
GUM_ERROR
(
ArgumentError
,
"first (here :"
<< first <<
" must be <last (here :"
<< last <<
")."
)
81
82
if
(!
gum::isfinite< double >
(first)) {
83
GUM_ERROR
(
DefaultInLabel
,
"Tick '"
<< first <<
"' is not allowed for variable "
<<
name
())
84
}
85
if
(!
gum::isfinite< double >
(last)) {
86
GUM_ERROR
(
DefaultInLabel
,
"Tick '"
<< last <<
"' is not allowed for variable "
<<
name
())
87
}
88
89
_domain_
.resize(nb);
90
_domain_
.clear();
91
92
const
double
step = (last - first) / (
double
(nb) - 1);
93
const
double
mask = std::pow(10, std::max(4,
int
(2 + std::abs(-std::log10(step)))));
94
double
current = first;
95
96
_domain_
.push_back(first);
97
for
(
Idx
i = 1; i < nb - 1; i++) {
98
current += step;
99
_domain_
.push_back((std::round(current * mask) / mask));
100
}
101
_domain_
.push_back(last);
102
103
std::sort(
_domain_
.begin(),
_domain_
.end());
104
105
// for debugging purposes
106
GUM_CONSTRUCTOR(
NumericalDiscreteVariable
);
107
}
108
110
std::string
NumericalDiscreteVariable::domain
()
const
{
111
const
Size
size =
domainSize
();
112
std::string s =
"{"
;
113
if
(size > 0) {
114
s += std::format(
"{}"
,
_domain_
[0]);
115
for
(
Idx
i = 1; i < size; ++i)
116
s += std::format(
"|{}"
,
_domain_
[i]);
117
}
118
s +=
"}"
;
119
return
s;
120
}
121
}
// namespace gum
double
ArgumentError
Exception base for argument error.
DefaultInLabel
Exception : default in label.
gum::DiscreteVariable::DiscreteVariable
DiscreteVariable(std::string_view aName, std::string_view aDesc)
Default constructor.
gum::NumericalDiscreteVariable::domainSize
Size domainSize() const final
returns the domain size of the discrete random variable
gum::NumericalDiscreteVariable::domain
std::string domain() const final
Returns the domain as a string.
Definition
numericalDiscreteVariable.cpp:110
gum::NumericalDiscreteVariable::NumericalDiscreteVariable
NumericalDiscreteVariable(std::string_view aName, std::string_view aDesc="")
constructor
gum::NumericalDiscreteVariable::isValue
bool isValue(double value) const
does this value exist in the domain ?
gum::NumericalDiscreteVariable::_domain_
std::vector< double > _domain_
the domain of the variable
Definition
numericalDiscreteVariable.h:202
gum::Variable::name
const std::string & name() const
returns the name of the variable
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::isfinite
bool isfinite(T arg)
Definition
math_utils_tpl.h:57
numericalDiscreteVariable.h
Base class for numerical discrete random variables.
numericalDiscreteVariable_inl.h
Base class for NumericalDiscrete discrete random variables.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0