#include <dai/factor.h>
Public Member Functions | |
Constructors and destructors | |
TFactor (T p=1) | |
Constructs factor depending on no variables with value p. | |
TFactor (const Var &v) | |
Constructs factor depending on the variable v with uniform distribution. | |
TFactor (const VarSet &vars) | |
Constructs factor depending on variables in vars with uniform distribution. | |
TFactor (const VarSet &vars, T p) | |
Constructs factor depending on variables in vars with all values set to p. | |
template<typename S> | |
TFactor (const VarSet &vars, const std::vector< S > &x) | |
Constructs factor depending on variables in vars, copying the values from a std::vector<>. | |
TFactor (const VarSet &vars, const T *p) | |
Constructs factor depending on variables in vars, copying the values from an array. | |
TFactor (const VarSet &vars, const TProb< T > &p) | |
Constructs factor depending on variables in vars, copying the values from p. | |
TFactor (const std::vector< Var > &vars, const std::vector< T > &p) | |
Constructs factor depending on variables in vars, permuting the values given in p accordingly. | |
Get/set individual entries | |
void | set (size_t i, T val) |
Sets i 'th entry to val. | |
T | get (size_t i) const |
Gets i 'th entry. | |
Queries | |
const TProb< T > & | p () const |
Returns constant reference to value vector. | |
TProb< T > & | p () |
Returns reference to value vector. | |
T | operator[] (size_t i) const |
Returns a copy of the i 'th entry of the value vector. | |
const VarSet & | vars () const |
Returns constant reference to variable set (i.e., the variables on which the factor depends). | |
VarSet & | vars () |
Returns reference to variable set (i.e., the variables on which the factor depends). | |
size_t | nrStates () const |
Returns the number of possible joint states of the variables on which the factor depends, ![]() | |
T | entropy () const |
Returns the Shannon entropy of *this , ![]() | |
T | max () const |
Returns maximum of all values. | |
T | min () const |
Returns minimum of all values. | |
T | sum () const |
Returns sum of all values. | |
T | sumAbs () const |
Returns sum of absolute values. | |
T | maxAbs () const |
Returns maximum absolute value of all values. | |
bool | hasNaNs () const |
Returns true if one or more values are NaN. | |
bool | hasNegatives () const |
Returns true if one or more values are negative. | |
T | strength (const Var &i, const Var &j) const |
Returns strength of this factor (between variables i and j), as defined in eq. (52) of [MoK07b]. | |
bool | operator== (const TFactor< T > &y) const |
Comparison. | |
Unary transformations | |
TFactor< T > | operator- () const |
Returns negative of *this . | |
TFactor< T > | abs () const |
Returns pointwise absolute value. | |
TFactor< T > | exp () const |
Returns pointwise exponent. | |
TFactor< T > | log (bool zero=false) const |
Returns pointwise logarithm. | |
TFactor< T > | inverse (bool zero=true) const |
Returns pointwise inverse. | |
TFactor< T > | normalized (ProbNormType norm=NORMPROB) const |
Returns normalized copy of *this , using the specified norm. | |
Unary operations | |
TFactor< T > & | randomize () |
Draws all values i.i.d. from a uniform distribution on [0,1). | |
TFactor< T > & | setUniform () |
Sets all values to ![]() | |
TFactor< T > & | takeAbs () |
Applies absolute value pointwise. | |
TFactor< T > & | takeExp () |
Applies exponent pointwise. | |
TFactor< T > & | takeLog (bool zero=false) |
Applies logarithm pointwise. | |
T | normalize (ProbNormType norm=NORMPROB) |
Normalizes factor using the specified norm. | |
Operations with scalars | |
TFactor< T > & | fill (T x) |
Sets all values to x. | |
TFactor< T > & | operator+= (T x) |
Adds scalar x to each value. | |
TFactor< T > & | operator-= (T x) |
Subtracts scalar x from each value. | |
TFactor< T > & | operator*= (T x) |
Multiplies each value with scalar x. | |
TFactor< T > & | operator/= (T x) |
Divides each entry by scalar x. | |
TFactor< T > & | operator^= (T x) |
Raises values to the power x. | |
Transformations with scalars | |
TFactor< T > | operator+ (T x) const |
Returns sum of *this and scalar x. | |
TFactor< T > | operator- (T x) const |
Returns difference of *this and scalar x. | |
TFactor< T > | operator* (T x) const |
Returns product of *this with scalar x. | |
TFactor< T > | operator/ (T x) const |
Returns quotient of *this with scalar x. | |
TFactor< T > | operator^ (T x) const |
Returns *this raised to the power x. | |
Operations with other factors | |
template<typename binOp> | |
TFactor< T > & | binaryOp (const TFactor< T > &g, binOp op) |
Applies binary operation op on two factors, *this and g. | |
TFactor< T > & | operator+= (const TFactor< T > &g) |
Adds g to *this . | |
TFactor< T > & | operator-= (const TFactor< T > &g) |
Subtracts g from *this . | |
TFactor< T > & | operator*= (const TFactor< T > &g) |
Multiplies *this with g. | |
TFactor< T > & | operator/= (const TFactor< T > &g) |
Divides *this by g (where division by zero yields zero). | |
Transformations with other factors | |
template<typename binOp> | |
TFactor< T > | binaryTr (const TFactor< T > &g, binOp op) const |
Returns result of applying binary operation op on two factors, *this and g. | |
TFactor< T > | operator+ (const TFactor< T > &g) const |
Returns sum of *this and g. | |
TFactor< T > | operator- (const TFactor< T > &g) const |
Returns *this minus g. | |
TFactor< T > | operator* (const TFactor< T > &g) const |
Returns product of *this with g. | |
TFactor< T > | operator/ (const TFactor< T > &g) const |
Returns quotient of *this by f (where division by zero yields zero). | |
Miscellaneous operations | |
TFactor< T > | slice (const VarSet &vars, size_t varsState) const |
Returns a slice of *this , where the subset vars is in state varsState. | |
TFactor< T > | embed (const VarSet &vars) const |
Embeds this factor in a larger VarSet. | |
TFactor< T > | marginal (const VarSet &vars, bool normed=true) const |
Returns marginal on vars, obtained by summing out all variables except those in vars, and normalizing the result if normed == true . | |
TFactor< T > | maxMarginal (const VarSet &vars, bool normed=true) const |
Returns max-marginal on vars, obtained by maximizing all variables except those in vars, and normalizing the result if normed == true . | |
Private Attributes | |
VarSet | _vs |
Stores the variables on which the factor depends. | |
TProb< T > | _p |
Stores the factor values. | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T> | |
std::ostream & | operator<< (std::ostream &os, const TFactor< T > &f) |
Writes a factor to an output stream. | |
template<typename T> | |
T | dist (const TFactor< T > &f, const TFactor< T > &g, ProbDistType dt) |
Returns distance between two factors f and g, according to the distance measure dt. | |
template<typename T> | |
TFactor< T > | max (const TFactor< T > &f, const TFactor< T > &g) |
Returns the pointwise maximum of two factors. | |
template<typename T> | |
TFactor< T > | min (const TFactor< T > &f, const TFactor< T > &g) |
Returns the pointwise minimum of two factors. | |
template<typename T> | |
T | MutualInfo (const TFactor< T > &f) |
Calculates the mutual information between the two variables that f depends on, under the distribution given by f. |
Mathematically, a factor is a function mapping joint states of some variables to the nonnegative real numbers. More formally, denoting a discrete variable with label by
and its state space by
, a factor depending on the variables
is a function
.
In libDAI, a factor is represented by a TFactor<T> object, which has two components:
T | Should be a scalar that is castable from and to double and should support elementary arithmetic operations. |
dai::TFactor< T >::TFactor | ( | T | p = 1 |
) | [inline] |
Constructs factor depending on no variables with value p.
dai::TFactor< T >::TFactor | ( | const Var & | v | ) | [inline] |
Constructs factor depending on the variable v with uniform distribution.
dai::TFactor< T >::TFactor | ( | const VarSet & | vars | ) | [inline] |
Constructs factor depending on variables in vars with uniform distribution.
dai::TFactor< T >::TFactor | ( | const VarSet & | vars, | |
T | p | |||
) | [inline] |
Constructs factor depending on variables in vars with all values set to p.
dai::TFactor< T >::TFactor | ( | const VarSet & | vars, | |
const std::vector< S > & | x | |||
) | [inline] |
Constructs factor depending on variables in vars, copying the values from a std::vector<>.
S | Type of values of x |
vars | contains the variables that the new factor should depend on. | |
x | Vector with values to be copied. |
dai::TFactor< T >::TFactor | ( | const VarSet & | vars, | |
const T * | p | |||
) | [inline] |
Constructs factor depending on variables in vars, copying the values from an array.
vars | contains the variables that the new factor should depend on. | |
p | Points to array of values to be added. |
dai::TFactor< T >::TFactor | ( | const VarSet & | vars, | |
const TProb< T > & | p | |||
) | [inline] |
Constructs factor depending on variables in vars, copying the values from p.
dai::TFactor< T >::TFactor | ( | const std::vector< Var > & | vars, | |
const std::vector< T > & | p | |||
) | [inline] |
Constructs factor depending on variables in vars, permuting the values given in p accordingly.
void dai::TFactor< T >::set | ( | size_t | i, | |
T | val | |||
) | [inline] |
Sets i 'th entry to val.
T dai::TFactor< T >::get | ( | size_t | i | ) | const [inline] |
Gets i 'th entry.
const TProb<T>& dai::TFactor< T >::p | ( | ) | const [inline] |
Returns constant reference to value vector.
TProb<T>& dai::TFactor< T >::p | ( | ) | [inline] |
Returns reference to value vector.
T dai::TFactor< T >::operator[] | ( | size_t | i | ) | const [inline] |
Returns a copy of the i 'th entry of the value vector.
const VarSet& dai::TFactor< T >::vars | ( | ) | const [inline] |
Returns constant reference to variable set (i.e., the variables on which the factor depends).
VarSet& dai::TFactor< T >::vars | ( | ) | [inline] |
Returns reference to variable set (i.e., the variables on which the factor depends).
size_t dai::TFactor< T >::nrStates | ( | ) | const [inline] |
Returns the number of possible joint states of the variables on which the factor depends, .
T dai::TFactor< T >::entropy | ( | ) | const [inline] |
Returns the Shannon entropy of *this
, .
T dai::TFactor< T >::max | ( | ) | const [inline] |
Returns maximum of all values.
T dai::TFactor< T >::min | ( | ) | const [inline] |
Returns minimum of all values.
T dai::TFactor< T >::sum | ( | ) | const [inline] |
Returns sum of all values.
T dai::TFactor< T >::sumAbs | ( | ) | const [inline] |
Returns sum of absolute values.
T dai::TFactor< T >::maxAbs | ( | ) | const [inline] |
Returns maximum absolute value of all values.
bool dai::TFactor< T >::hasNaNs | ( | ) | const [inline] |
Returns true
if one or more values are NaN.
bool dai::TFactor< T >::hasNegatives | ( | ) | const [inline] |
Returns true
if one or more values are negative.
T dai::TFactor< T >::strength | ( | const Var & | i, | |
const Var & | j | |||
) | const [inline] |
Returns strength of this factor (between variables i and j), as defined in eq. (52) of [MoK07b].
bool dai::TFactor< T >::operator== | ( | const TFactor< T > & | y | ) | const [inline] |
Comparison.
TFactor<T> dai::TFactor< T >::operator- | ( | ) | const [inline] |
Returns negative of *this
.
TFactor<T> dai::TFactor< T >::abs | ( | ) | const [inline] |
Returns pointwise absolute value.
TFactor<T> dai::TFactor< T >::exp | ( | ) | const [inline] |
Returns pointwise exponent.
TFactor<T> dai::TFactor< T >::log | ( | bool | zero = false |
) | const [inline] |
Returns pointwise logarithm.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
TFactor<T> dai::TFactor< T >::inverse | ( | bool | zero = true |
) | const [inline] |
Returns pointwise inverse.
If zero == true
, uses 1/0==0
; otherwise, 1/0==Inf
.
TFactor<T> dai::TFactor< T >::normalized | ( | ProbNormType | norm = NORMPROB |
) | const [inline] |
Returns normalized copy of *this
, using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
TFactor<T>& dai::TFactor< T >::randomize | ( | ) | [inline] |
Draws all values i.i.d. from a uniform distribution on [0,1).
TFactor<T>& dai::TFactor< T >::setUniform | ( | ) | [inline] |
Sets all values to where n is the number of states.
TFactor<T>& dai::TFactor< T >::takeAbs | ( | ) | [inline] |
Applies absolute value pointwise.
TFactor<T>& dai::TFactor< T >::takeExp | ( | ) | [inline] |
Applies exponent pointwise.
TFactor<T>& dai::TFactor< T >::takeLog | ( | bool | zero = false |
) | [inline] |
Applies logarithm pointwise.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
T dai::TFactor< T >::normalize | ( | ProbNormType | norm = NORMPROB |
) | [inline] |
Normalizes factor using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
TFactor<T>& dai::TFactor< T >::fill | ( | T | x | ) | [inline] |
Sets all values to x.
TFactor<T>& dai::TFactor< T >::operator+= | ( | T | x | ) | [inline] |
Adds scalar x to each value.
TFactor<T>& dai::TFactor< T >::operator-= | ( | T | x | ) | [inline] |
Subtracts scalar x from each value.
TFactor<T>& dai::TFactor< T >::operator*= | ( | T | x | ) | [inline] |
Multiplies each value with scalar x.
TFactor<T>& dai::TFactor< T >::operator/= | ( | T | x | ) | [inline] |
Divides each entry by scalar x.
TFactor<T>& dai::TFactor< T >::operator^= | ( | T | x | ) | [inline] |
Raises values to the power x.
TFactor<T> dai::TFactor< T >::operator+ | ( | T | x | ) | const [inline] |
Returns sum of *this
and scalar x.
TFactor<T> dai::TFactor< T >::operator- | ( | T | x | ) | const [inline] |
Returns difference of *this
and scalar x.
TFactor<T> dai::TFactor< T >::operator* | ( | T | x | ) | const [inline] |
Returns product of *this
with scalar x.
TFactor<T> dai::TFactor< T >::operator/ | ( | T | x | ) | const [inline] |
Returns quotient of *this
with scalar x.
TFactor<T> dai::TFactor< T >::operator^ | ( | T | x | ) | const [inline] |
Returns *this
raised to the power x.
TFactor<T>& dai::TFactor< T >::binaryOp | ( | const TFactor< T > & | g, | |
binOp | op | |||
) | [inline] |
Applies binary operation op on two factors, *this
and g.
binOp | Type of function object that accepts two arguments of type T and outputs a type T |
g | Right operand | |
op | Operation of type binOp |
TFactor<T>& dai::TFactor< T >::operator+= | ( | const TFactor< T > & | g | ) | [inline] |
Adds g to *this
.
The sum of two factors is defined as follows: if and
, then
TFactor<T>& dai::TFactor< T >::operator-= | ( | const TFactor< T > & | g | ) | [inline] |
Subtracts g from *this
.
The difference of two factors is defined as follows: if and
, then
TFactor<T>& dai::TFactor< T >::operator*= | ( | const TFactor< T > & | g | ) | [inline] |
Multiplies *this
with g.
The product of two factors is defined as follows: if and
, then
TFactor<T>& dai::TFactor< T >::operator/= | ( | const TFactor< T > & | g | ) | [inline] |
Divides *this
by g (where division by zero yields zero).
The quotient of two factors is defined as follows: if and
, then
TFactor<T> dai::TFactor< T >::binaryTr | ( | const TFactor< T > & | g, | |
binOp | op | |||
) | const [inline] |
Returns result of applying binary operation op on two factors, *this
and g.
binOp | Type of function object that accepts two arguments of type T and outputs a type T |
g | Right operand | |
op | Operation of type binOp |
TFactor<T> dai::TFactor< T >::operator+ | ( | const TFactor< T > & | g | ) | const [inline] |
Returns sum of *this
and g.
The sum of two factors is defined as follows: if and
, then
TFactor<T> dai::TFactor< T >::operator- | ( | const TFactor< T > & | g | ) | const [inline] |
Returns *this
minus g.
The difference of two factors is defined as follows: if and
, then
TFactor<T> dai::TFactor< T >::operator* | ( | const TFactor< T > & | g | ) | const [inline] |
Returns product of *this
with g.
The product of two factors is defined as follows: if and
, then
TFactor<T> dai::TFactor< T >::operator/ | ( | const TFactor< T > & | g | ) | const [inline] |
Returns quotient of *this
by f (where division by zero yields zero).
The quotient of two factors is defined as follows: if and
, then
TFactor< T > dai::TFactor< T >::slice | ( | const VarSet & | vars, | |
size_t | varsState | |||
) | const [inline] |
Returns a slice of *this
, where the subset vars is in state varsState.
varsState < vars.nrStates()
*this
corresponds with the factor TFactor<T> dai::TFactor< T >::embed | ( | const VarSet & | vars | ) | const [inline] |
TFactor< T > dai::TFactor< T >::marginal | ( | const VarSet & | vars, | |
bool | normed = true | |||
) | const [inline] |
Returns marginal on vars, obtained by summing out all variables except those in vars, and normalizing the result if normed == true
.
TFactor< T > dai::TFactor< T >::maxMarginal | ( | const VarSet & | vars, | |
bool | normed = true | |||
) | const [inline] |
Returns max-marginal on vars, obtained by maximizing all variables except those in vars, and normalizing the result if normed == true
.
std::ostream & operator<< | ( | std::ostream & | os, | |
const TFactor< T > & | f | |||
) | [related] |
Writes a factor to an output stream.
T dist | ( | const TFactor< T > & | f, | |
const TFactor< T > & | g, | |||
ProbDistType | dt | |||
) | [related] |
Returns distance between two factors f and g, according to the distance measure dt.
Returns the pointwise maximum of two factors.
Returns the pointwise minimum of two factors.
T MutualInfo | ( | const TFactor< T > & | f | ) | [related] |
Calculates the mutual information between the two variables that f depends on, under the distribution given by f.
VarSet dai::TFactor< T >::_vs [private] |
Stores the variables on which the factor depends.
TProb<T> dai::TFactor< T >::_p [private] |
Stores the factor values.