aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
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-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
41
43
45
46#ifdef GUM_NO_INLINE
48#endif /* GUM_NO_INLINE */
49
50namespace gum {
51
54 const std::string& 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
73 const std::string& 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
111 std::stringstream s;
112 const Size size = domainSize();
113
114 s << "{";
115 if (size > 0) {
116 s << _domain_[0];
117 for (Idx i = 1; i < size; ++i)
118 s << '|' << _domain_[i];
119 }
120 s << "}";
121 return s.str();
122 }
123} // namespace gum
Exception base for argument error.
Exception : default in label.
DiscreteVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
Size domainSize() const final
returns the domain size of the discrete random variable
std::string domain() const final
Returns the domain as a string.
bool isValue(double value) const
does this value exist in the domain ?
NumericalDiscreteVariable(const std::string &aName, const std::string &aDesc="")
constructor
std::vector< double > _domain_
the domain of the variable
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
bool isfinite(T arg)
Definition math_utils.h:75
Base class for numerical discrete random variables.
Base class for NumericalDiscrete discrete random variables.