aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::NumericalDiscreteVariable Class Reference

class NumericalDiscreteVariable More...

#include <numericalDiscreteVariable.h>

Inheritance diagram for gum::NumericalDiscreteVariable:
Collaboration diagram for gum::NumericalDiscreteVariable:

Public Member Functions

Idx operator[] (const std::string &label) const
 from the label to its index in var.
std::string toString () const
 string version of *this
std::string toStringWithDescription () const
 string version of *this using description attribute instead of name.
Constructors / Destructors
 NumericalDiscreteVariable (const std::string &aName, const std::string &aDesc="")
 constructor
 NumericalDiscreteVariable (const std::string &aName, const std::string &aDesc, const std::vector< double > &domain)
 constructor assigning a domain to the variable
 NumericalDiscreteVariable (const std::string &aName, const std::string &aDesc, double first, double last, Size nb)
 constructor assigning a domain to the variable
 NumericalDiscreteVariable (const NumericalDiscreteVariable &from)
 Copy constructor.
 NumericalDiscreteVariable (NumericalDiscreteVariable &&from)
 move constructor
NumericalDiscreteVariableclone () const final
 virtual copy constructor
virtual ~NumericalDiscreteVariable ()
 destructor
Operators
NumericalDiscreteVariableoperator= (const NumericalDiscreteVariable &from)
 copy operator
NumericalDiscreteVariableoperator= (NumericalDiscreteVariable &&from)
 move operator
Accessors / Modifiers
Size domainSize () const final
 returns the domain size of the discrete random variable
VarType varType () const final
 returns the type of variable
std::string toFast () const final
 returns the domain size of the discrete random variable
Idx index (const std::string &label) const final
 returns the index of a given label
Idx closestIndex (double val) const final
 gives the index of the value closest to val
std::string label (Idx index) const final
 returns a string corresponding to the ith value of the domain
double numerical (Idx index) const final
 get a numerical representation of the value at a given index
std::string domain () const final
 Returns the domain as a string.
std::string stype () const final
 string represent the type of the variable
const std::vector< double > & numericalDomain () const
 returns the domain as a sequence of values
void addValue (double value)
 add a new value to the domain size
bool isValue (double value) const
 does this value exist in the domain ?
void changeValue (double old_value, double new_value)
 substitute a value by another one
void eraseValue (double value)
 erase a value from the domain of the variable
void eraseValues ()
 clear the domain of the variable
std::string closestLabel (double val) const
 gives the value closets to val
Accessors / Modifiers
bool empty () const
std::vector< std::string > labels () const
 vector of labels
virtual bool isEmpirical () const
Operators
bool operator== (const Variable &aRV) const
 equality operator
Accessors / Modifiers
void setName (const std::string &theValue)
 sets the name of the variable
const std::string & name () const
 returns the name of the variable
void setDescription (const std::string &theValue) const
 sets the description of the variable
const std::string & description () const
 returns the description of the variable

Protected Member Functions

void copy_ (const Variable &aRV)
 protected copy

Private Member Functions

bool _checkSameDomain_ (const Variable &aRV) const final
 check the domain
std::string _generateLabel_ (double f) const

Private Attributes

std::vector< double_domain_
 the domain of the variable
std::string _name_
 the name of the variable
std::string _description_
 the description of the variable since description is not a characteristic of a variable, we allow the description to be changed even in a const reference.

Detailed Description

class NumericalDiscreteVariable

The class representing discrete numerical random variables

Definition at line 62 of file numericalDiscreteVariable.h.

Constructor & Destructor Documentation

◆ NumericalDiscreteVariable() [1/5]

gum::NumericalDiscreteVariable::NumericalDiscreteVariable ( const std::string & aName,
const std::string & aDesc = "" )

constructor

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any

References domain().

Referenced by NumericalDiscreteVariable(), NumericalDiscreteVariable(), NumericalDiscreteVariable(), NumericalDiscreteVariable(), ~NumericalDiscreteVariable(), clone(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NumericalDiscreteVariable() [2/5]

gum::NumericalDiscreteVariable::NumericalDiscreteVariable ( const std::string & aName,
const std::string & aDesc,
const std::vector< double > & domain )

constructor assigning a domain to the variable

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any
domainthe domain (set of values) of the variable

Definition at line 53 of file numericalDiscreteVariable.cpp.

55 :
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 }
DiscreteVariable()
(protected) Default constructor
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
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
bool isfinite(T arg)
Definition math_utils.h:75

References gum::DiscreteVariable::DiscreteVariable(), NumericalDiscreteVariable(), _domain_, domain(), GUM_ERROR, gum::isfinite(), and isValue().

Here is the call graph for this function:

◆ NumericalDiscreteVariable() [3/5]

gum::NumericalDiscreteVariable::NumericalDiscreteVariable ( const std::string & aName,
const std::string & aDesc,
double first,
double last,
Size nb )

constructor assigning a domain to the variable

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any
firstthe first value
lastthe last value
nbthe number of values

Definition at line 72 of file numericalDiscreteVariable.cpp.

76 : 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 }
const std::string & name() const
returns the name of the variable
Size Idx
Type for indexes.
Definition types.h:79

References gum::DiscreteVariable::DiscreteVariable(), NumericalDiscreteVariable(), _domain_, GUM_ERROR, gum::isfinite(), and gum::Variable::name().

Here is the call graph for this function:

◆ NumericalDiscreteVariable() [4/5]

gum::NumericalDiscreteVariable::NumericalDiscreteVariable ( const NumericalDiscreteVariable & from)

Copy constructor.

Parameters
fromthe variable we copy

References NumericalDiscreteVariable().

Here is the call graph for this function:

◆ NumericalDiscreteVariable() [5/5]

gum::NumericalDiscreteVariable::NumericalDiscreteVariable ( NumericalDiscreteVariable && from)

move constructor

References NumericalDiscreteVariable().

Here is the call graph for this function:

◆ ~NumericalDiscreteVariable()

virtual gum::NumericalDiscreteVariable::~NumericalDiscreteVariable ( )
virtual

destructor

References NumericalDiscreteVariable().

Here is the call graph for this function:

Member Function Documentation

◆ _checkSameDomain_()

bool gum::NumericalDiscreteVariable::_checkSameDomain_ ( const Variable & aRV) const
finalprivatevirtual

check the domain

this function use the assumption that the concrete type of the variable is the same as *this

Implements gum::Variable.

◆ _generateLabel_()

std::string gum::NumericalDiscreteVariable::_generateLabel_ ( double f) const
private

◆ addValue()

void gum::NumericalDiscreteVariable::addValue ( double value)

add a new value to the domain size

Exceptions
DuplicateElementis raised if the variable already contains the value

◆ changeValue()

void gum::NumericalDiscreteVariable::changeValue ( double old_value,
double new_value )

substitute a value by another one

◆ clone()

NumericalDiscreteVariable * gum::NumericalDiscreteVariable::clone ( ) const
finalvirtual

virtual copy constructor

Implements gum::DiscreteVariable.

References NumericalDiscreteVariable().

Here is the call graph for this function:

◆ closestIndex()

Idx gum::NumericalDiscreteVariable::closestIndex ( double val) const
finalvirtual

gives the index of the value closest to val

Parameters
valthe desired double
Returns
the index

Implements gum::DiscreteVariable.

References closestIndex().

Referenced by closestIndex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ closestLabel()

std::string gum::NumericalDiscreteVariable::closestLabel ( double val) const

gives the value closets to val

Parameters
valthe desired value
Returns
the value

◆ copy_()

void gum::Variable::copy_ ( const Variable & aRV)
protectedinherited

protected copy

Parameters
aRVto be copied

References Variable().

Here is the call graph for this function:

◆ description()

const std::string & gum::Variable::description ( ) const
inherited

returns the description of the variable

◆ domain()

std::string gum::NumericalDiscreteVariable::domain ( ) const
finalvirtual

Returns the domain as a string.

Implements gum::DiscreteVariable.

Definition at line 110 of file numericalDiscreteVariable.cpp.

110 {
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 }
Size domainSize() const final
returns the domain size of the discrete random variable
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _domain_, and domainSize().

Referenced by NumericalDiscreteVariable(), NumericalDiscreteVariable(), and numerical().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ domainSize()

Size gum::NumericalDiscreteVariable::domainSize ( ) const
finalvirtual

returns the domain size of the discrete random variable

Implements gum::DiscreteVariable.

References domainSize().

Referenced by domain(), and domainSize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ empty()

bool gum::DiscreteVariable::empty ( ) const
inherited
Returns
true if the domainSize() < 2;

◆ eraseValue()

void gum::NumericalDiscreteVariable::eraseValue ( double value)

erase a value from the domain of the variable

◆ eraseValues()

void gum::NumericalDiscreteVariable::eraseValues ( )

clear the domain of the variable

◆ index()

Idx gum::NumericalDiscreteVariable::index ( const std::string & label) const
finalvirtual

returns the index of a given label

Parameters
labelsearched label
Returns
the index of this label
Exceptions
NotFound

Implements gum::DiscreteVariable.

References index(), and label().

Referenced by index(), label(), and numerical().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isEmpirical()

virtual bool gum::DiscreteVariable::isEmpirical ( ) const
inlinevirtualinherited
Returns
true if the domainSize() < 2;

Reimplemented in gum::IDiscretizedVariable.

Definition at line 124 of file discreteVariable.h.

124{ return false; };

◆ isValue()

bool gum::NumericalDiscreteVariable::isValue ( double value) const

does this value exist in the domain ?

Referenced by NumericalDiscreteVariable().

Here is the caller graph for this function:

◆ label()

std::string gum::NumericalDiscreteVariable::label ( Idx index) const
finalvirtual

returns a string corresponding to the ith value of the domain

Implements gum::DiscreteVariable.

References index(), and label().

Referenced by index(), and label().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ labels()

std::vector< std::string > gum::DiscreteVariable::labels ( ) const
inherited

vector of labels

Referenced by gum::LabelizedVariable::LabelizedVariable().

Here is the caller graph for this function:

◆ name()

const std::string & gum::Variable::name ( ) const
inherited

returns the name of the variable

Referenced by gum::learning::IBNLearner::Database::Database(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::MultiDimImplementation< double >::MultiDimImplementation(), gum::NumericalDiscreteVariable::NumericalDiscreteVariable(), gum::BNdistance< GUM_SCALAR >::_checkCompatibility_(), gum::BayesNet< double >::_copyTensors_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInputs_(), gum::BayesNetFactory< GUM_SCALAR >::_setCPTAndParents_(), gum::Instantiation::add(), gum::FMDP< double >::addCostForAction(), gum::FMDP< GUM_SCALAR >::addVariable(), gum::MultiDimICIModel< GUM_SCALAR >::causalWeight(), gum::Instantiation::chgVal(), gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscreteType(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscretizedType(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::FMDPFactory< GUM_SCALAR >::endVariableDeclaration(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::BayesNetFragment< GUM_SCALAR >::nodeId(), gum::Estimator< GUM_SCALAR >::posterior(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::Instantiation::setValsFrom(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), gum::RangeVariable::toFast(), gum::Estimator< GUM_SCALAR >::update(), and gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::varNodeListe().

◆ numerical()

double gum::NumericalDiscreteVariable::numerical ( Idx index) const
finalvirtual

get a numerical representation of the value at a given index

Implements gum::DiscreteVariable.

References domain(), index(), and numerical().

Referenced by numerical().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ numericalDomain()

const std::vector< double > & gum::NumericalDiscreteVariable::numericalDomain ( ) const

returns the domain as a sequence of values

◆ operator=() [1/2]

NumericalDiscreteVariable & gum::NumericalDiscreteVariable::operator= ( const NumericalDiscreteVariable & from)

copy operator

Parameters
fromthe numerical discrete random variable we copy

References NumericalDiscreteVariable().

Here is the call graph for this function:

◆ operator=() [2/2]

NumericalDiscreteVariable & gum::NumericalDiscreteVariable::operator= ( NumericalDiscreteVariable && from)

move operator

Parameters
fromthe numerical discrete random variable we copy

References NumericalDiscreteVariable().

Here is the call graph for this function:

◆ operator==()

bool gum::Variable::operator== ( const Variable & aRV) const
inherited

equality operator

References Variable().

Here is the call graph for this function:

◆ operator[]()

Idx gum::DiscreteVariable::operator[] ( const std::string & label) const
inlineinherited

from the label to its index in var.

Warning
This operation may have different complexity in different subclasses.
Exceptions
NotFound

Definition at line 156 of file discreteVariable.h.

156{ return index(label); };
virtual Idx index(const std::string &label) const =0
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.

References index(), and label().

Here is the call graph for this function:

◆ setDescription()

void gum::Variable::setDescription ( const std::string & theValue) const
inherited

sets the description of the variable

Warning
since description is mutable, setDescription() is const
Parameters
theValue

◆ setName()

void gum::Variable::setName ( const std::string & theValue)
inherited

sets the name of the variable

Parameters
theValue

◆ stype()

std::string gum::NumericalDiscreteVariable::stype ( ) const
inlinefinalvirtual

string represent the type of the variable

Implements gum::DiscreteVariable.

Definition at line 168 of file numericalDiscreteVariable.h.

168{ return "NumericalDiscrete"; };

References stype().

Referenced by stype().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toFast()

std::string gum::NumericalDiscreteVariable::toFast ( ) const
finalvirtual

returns the domain size of the discrete random variable

Implements gum::DiscreteVariable.

References toFast().

Referenced by toFast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

std::string gum::DiscreteVariable::toString ( ) const
inherited

string version of *this

Referenced by gum::IntegerVariable::IntegerVariable(), and gum::BNdistance< GUM_SCALAR >::_checkCompatibility_().

Here is the caller graph for this function:

◆ toStringWithDescription()

std::string gum::DiscreteVariable::toStringWithDescription ( ) const
inherited

string version of *this using description attribute instead of name.

◆ varType()

VarType gum::NumericalDiscreteVariable::varType ( ) const
finalvirtual

returns the type of variable

Implements gum::DiscreteVariable.

References varType().

Referenced by varType().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _description_

std::string gum::Variable::_description_
mutableprivateinherited

the description of the variable since description is not a characteristic of a variable, we allow the description to be changed even in a const reference.

Definition at line 165 of file variable.h.

◆ _domain_

std::vector< double > gum::NumericalDiscreteVariable::_domain_
private

the domain of the variable

Definition at line 202 of file numericalDiscreteVariable.h.

Referenced by NumericalDiscreteVariable(), NumericalDiscreteVariable(), and domain().

◆ _name_

std::string gum::Variable::_name_
privateinherited

the name of the variable

Definition at line 160 of file variable.h.


The documentation for this class was generated from the following files: