13 #ifndef __defined_libdai_factor_h
14 #define __defined_libdai_factor_h
89 _p =
TProb<T>( x.begin(), x.end(), x.size() );
107 TFactor(
const std::vector<Var> &
vars,
const std::vector<T> &
p ) : _vs(vars.begin(), vars.end(), vars.size()), _p(p.size()) {
109 for(
size_t i = 0; i < vars.size(); i++ )
110 nrStates *= vars[i].states();
113 for(
size_t li = 0; li < p.size(); ++li )
114 _p.
set( permindex.convertLinearIndex(li), p[li] );
120 void set(
size_t i, T val ) { _p.
set( i, val ); }
124 T
get(
size_t i )
const {
return _p[i]; }
178 return (_vs == y.
_vs) && (_p == y.
_p);
183 std::stringstream ss;
362 for(
size_t i = 0; i < N; i++, ++i_f, ++i_g )
363 _p.
p().push_back( op( f.
_p[i_f], g.
_p[i_g] ) );
418 result.
_p.p().clear();
419 result.
_p.p().reserve( N );
420 for(
size_t i = 0; i < N; i++, ++i_f, ++i_g )
421 result.
_p.p().push_back( op( _p[i_f], g[i_g] ) );
450 return binaryTr(g,std::multiplies<T>());
504 VarSet varsrem = _vs / vars;
510 for(
size_t i = 0; i < nrStates(); i++, ++i_vars, ++i_varsrem )
511 if( (
size_t)i_vars == varsState )
512 result.
set( i_varsrem, _p[i] );
519 VarSet res_vars = vars & _vs;
524 for(
size_t i = 0; i < _p.
size(); i++, ++i_res )
525 res.
set( i_res, res[i_res] + _p[i] );
535 VarSet res_vars = vars & _vs;
540 for(
size_t i = 0; i < _p.
size(); i++, ++i_res )
541 if( _p[i] > res.
_p[i_res] )
542 res.
set( i_res, _p[i] );
558 for(
size_t alpha1 = 0; alpha1 < i.
states(); alpha1++ )
559 for(
size_t alpha2 = 0; alpha2 < i.
states(); alpha2++ )
560 if( alpha2 != alpha1 )
561 for(
size_t beta1 = 0; beta1 < j.
states(); beta1++ )
562 for(
size_t beta2 = 0; beta2 < j.
states(); beta2++ )
563 if( beta2 != beta1 ) {
564 size_t as = 1, bs = 1;
569 T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).max();
570 T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).max();
576 return std::tanh( 0.25 *
std::log( max ) );
583 template<
typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& f) {
584 os <<
"(" << f.vars() <<
", (";
585 for(
size_t i = 0; i < f.nrStates(); i++ )
586 os << (i == 0 ?
"" :
", ") << f[i];
601 return dist( f.
p(), g.
p(), dt );
633 Var i = *it; it++;
Var j = *it;
this_type pwBinaryTr(const this_type &q, binaryOp op) const
Returns the result of applying binary operation op pointwise on *this and q.
Definition: prob.h:661
TFactor(const VarSet &vars, const T *p)
Constructs factor depending on variables in vars, copying the values from an array.
Definition: factor.h:96
TFactor< Real > Factor
Represents a factor with values of type dai::Real.
Definition: factor.h:640
size_t BigInt_size_t(const BigInt &N)
Safe down-cast of big integer to size_t.
Definition: util.h:104
const VarSet & vars() const
Returns constant reference to variable set (i.e., the variables on which the factor depends) ...
Definition: factor.h:139
T entropy() const
Returns the Shannon entropy of *this, .
Definition: factor.h:150
this_type & fill(T x)
Sets all entries to x.
Definition: prob.h:544
TFactor< T > operator^(T x) const
Returns *this raised to the power x.
Definition: factor.h:334
BigInt nrStates() const
Calculates the number of states of this VarSet, which is simply the number of possible joint states o...
Definition: varset.h:130
TFactor(const VarSet &vars)
Constructs factor depending on variables in vars with uniform distribution.
Definition: factor.h:72
TFactor< T > & operator-=(T x)
Subtracts scalar x from each value.
Definition: factor.h:283
ProbNormType
Enumerates different ways of normalizing a probability measure.
Definition: util.h:255
Defines the IndexFor, multifor, Permute and State classes, which all deal with indexing multi-dimensi...
ProbDistType
Enumerates different distance measures between probability measures.
Definition: util.h:265
TFactor< T > abs() const
Returns pointwise absolute value.
Definition: factor.h:203
this_type normalized(ProbNormType norm=dai::NORMPROB) const
Returns normalized copy of *this, using the specified norm.
Definition: prob.h:474
Real log(Real x)
Returns logarithm of x.
Definition: util.h:113
this_type exp() const
Returns pointwise exponent.
Definition: prob.h:449
TFactor< T > & binaryOp(const TFactor< T > &g, binOp op)
Applies binary operation op on two factors, *this and g.
Definition: factor.h:349
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.
Definition: factor.h:518
bool hasNegatives() const
Returns true if one or more values are negative.
Definition: factor.h:171
std::vector< T >::size_type size() const
Returns number of elements.
Definition: smallset.h:169
iterator begin()
Returns iterator that points to the first element.
Definition: smallset.h:193
Defines the VarSet class, which represents a set of random variables.
TFactor< T > & takeLog(bool zero=false)
Applies logarithm pointwise.
Definition: factor.h:266
this_type & takeAbs()
Applies absolute value pointwise.
Definition: prob.h:509
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.
Definition: factor.h:596
this_type log(bool zero=false) const
Returns pointwise logarithm.
Definition: prob.h:454
void set(size_t i, T val)
Sets i 'th entry to val.
Definition: prob.h:293
T maxAbs() const
Returns maximum absolute value of all entries.
Definition: prob.h:366
std::string toString() const
Formats a factor as a string.
Definition: factor.h:182
TFactor< T > embed(const VarSet &vars) const
Embeds this factor in a larger VarSet.
Definition: factor.h:485
bool hasNaNs() const
Returns true if one or more entries are NaN.
Definition: prob.h:369
TFactor< T > min(const TFactor< T > &f, const TFactor< T > &g)
Returns the pointwise minimum of two factors.
Definition: factor.h:620
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
bool hasNegatives() const
Returns true if one or more entries are negative.
Definition: prob.h:380
TFactor< T > & fill(T x)
Sets all values to x.
Definition: factor.h:277
Factor createFactorDelta(const Var &v, size_t state)
Returns a Kronecker delta point mass.
Definition: factor.cpp:55
this_type abs() const
Returns pointwise absolute value.
Definition: prob.h:446
TFactor< T > & setUniform()
Sets all values to where n is the number of states.
Definition: factor.h:255
T min() const
Returns minimum of all values.
Definition: factor.h:156
TFactor(const VarSet &vars, T p)
Constructs factor depending on variables in vars with all values set to p.
Definition: factor.h:77
T max() const
Returns maximum value of all entries.
Definition: prob.h:354
Represents a (probability) factor.
Definition: factor.h:55
this_type & pwBinaryOp(const this_type &q, binaryOp op)
Applies binary operation pointwise on two vectors.
Definition: prob.h:615
Defines TProb<> and Prob classes which represent (probability) vectors (e.g., probability distributio...
TFactor(const VarSet &vars, const std::vector< S > &x)
Constructs factor depending on variables in vars, copying the values from a std::vector<> ...
Definition: factor.h:87
T operator[](size_t i) const
Returns a copy of the i 'th entry of the value vector.
Definition: factor.h:136
this_type & randomize()
Draws all entries i.i.d. from a uniform distribution on [0,1)
Definition: prob.h:497
Represents a vector with entries of type T.
Definition: prob.h:194
Tool for looping over the states of several variables.
Definition: index.h:48
T max() const
Returns maximum of all values.
Definition: factor.h:153
TFactor< T > & randomize()
Draws all values i.i.d. from a uniform distribution on [0,1)
Definition: factor.h:252
Factor createFactorPotts(const Var &n1, const Var &n2, Real J)
Returns a pairwise Potts factor .
Definition: factor.cpp:46
void set(size_t i, T val)
Sets i 'th entry to val.
Definition: factor.h:121
T sum() const
Returns sum of all entries.
Definition: prob.h:360
mpz_class BigInt
Arbitrary precision integer number.
Definition: util.h:101
VarSet & vars()
Returns reference to variable set (i.e., the variables on which the factor depends) ...
Definition: factor.h:142
bool contains(const T &t) const
Returns true if *this contains the element t.
Definition: smallset.h:164
TProb< T > _p
Stores the factor values.
Definition: factor.h:60
std::vector< Var >::const_iterator const_iterator
Constant iterator over the elements.
Definition: smallset.h:182
const container_type & p() const
Returns a const reference to the wrapped container.
Definition: prob.h:302
TFactor< T > binaryTr(const TFactor< T > &g, binOp op) const
Returns result of applying binary operation op on two factors, *this and g.
Definition: factor.h:404
Function object similar to std::divides(), but different in that dividing by zero results in zero...
Definition: prob.h:112
Real dist(const vector< Factor > &b1, const vector< Factor > &b2, size_t nv)
Compute sum of pairwise L-infinity distances of the first nv factors in each vector.
Definition: cbp.cpp:68
Represents a set of variables.
Definition: varset.h:94
T normalize(ProbNormType norm=dai::NORMPROB)
Normalizes vector using the specified norm.
Definition: prob.h:527
this_type & setUniform()
Sets all entries to where n is the length of the vector.
Definition: prob.h:503
Factor createFactorExpGauss(const VarSet &ns, Real beta)
Returns a random factor on the variables vs with strength beta.
Definition: factor.cpp:38
TFactor< T > inverse(bool zero=true) const
Returns pointwise inverse.
Definition: factor.h:231
TFactor< T > & operator/=(T x)
Divides each entry by scalar x.
Definition: factor.h:289
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
TFactor< T > & operator*=(T x)
Multiplies each value with scalar x.
Definition: factor.h:286
TFactor< T > max(const TFactor< T > &f, const TFactor< T > &g)
Returns the pointwise maximum of two factors.
Definition: factor.h:610
T MutualInfo(const TFactor< T > &f)
Calculates the mutual information between the two variables that f depends on, under the distribution...
Definition: factor.h:630
Factor createFactorIsing(const Var &n, Real h)
Returns a binary unnormalized single-variable factor where .
Definition: factor.cpp:18
size_t nrStates() const
Returns the number of possible joint states of the variables on which the factor depends, .
Definition: factor.h:147
TFactor(T p=1)
Constructs factor depending on no variables with value p.
Definition: factor.h:66
TFactor< T > exp() const
Returns pointwise exponent.
Definition: factor.h:211
TFactor< T > operator/(T x) const
Returns quotient of *this with scalar x.
Definition: factor.h:326
TFactor< T > slice(const VarSet &vars, size_t varsState) const
Returns a slice of *this, where the subset vars is in state varsState.
Definition: factor.h:502
T sumAbs() const
Return sum of absolute value of all entries.
Definition: prob.h:363
size_t size() const
Returns length of the vector (i.e., the number of entries)
Definition: prob.h:311
TFactor< T > maxMarginal(const VarSet &vars, bool normed=true) const
Returns max-marginal on vars, obtained by maximizing all variables except those in vars...
Definition: factor.h:534
this_type & takeLog(bool zero=false)
Applies logarithm pointwise.
Definition: prob.h:517
T maxAbs() const
Returns maximum absolute value of all values.
Definition: factor.h:165
TProb< T > & p()
Returns reference to value vector.
Definition: factor.h:133
size_t states() const
Returns the number of states.
Definition: var.h:57
TFactor< T > normalized(ProbNormType norm=NORMPROB) const
Returns normalized copy of *this, using the specified norm.
Definition: factor.h:241
T min() const
Returns minimum value of all entries.
Definition: prob.h:357
bool operator==(const TFactor< T > &y) const
Comparison.
Definition: factor.h:177
T sum() const
Returns sum of all values.
Definition: factor.h:159
T sumAbs() const
Returns sum of absolute values.
Definition: factor.h:162
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
Tool for calculating permutations of linear indices of multi-dimensional arrays.
Definition: index.h:137
this_type inverse(bool zero=true) const
Returns pointwise inverse.
Definition: prob.h:464
TFactor< T > operator+(T x) const
Returns sum of *this and scalar x.
Definition: factor.h:298
TFactor< T > & takeAbs()
Applies absolute value pointwise.
Definition: factor.h:258
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
VarSet _vs
Stores the variables on which the factor depends.
Definition: factor.h:58
TFactor< T > & operator+=(T x)
Adds scalar x to each value.
Definition: factor.h:280
const TProb< T > & p() const
Returns constant reference to value vector.
Definition: factor.h:130
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
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...
Definition: factor.h:107
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]...
Definition: factor.h:551
TFactor< T > & operator^=(T x)
Raises values to the power x.
Definition: factor.h:292
bool hasNaNs() const
Returns true if one or more values are NaN.
Definition: factor.h:168
TFactor< T > log(bool zero=false) const
Returns pointwise logarithm.
Definition: factor.h:221
TFactor(const Var &v)
Constructs factor depending on the variable v with uniform distribution.
Definition: factor.h:69
TFactor< T > & takeExp()
Applies exponent pointwise.
Definition: factor.h:261
TFactor(const VarSet &vars, const TProb< T > &p)
Constructs factor depending on variables in vars, copying the values from p.
Definition: factor.h:102
TFactor< T > operator-() const
Returns negative of *this.
Definition: factor.h:192
T normalize(ProbNormType norm=NORMPROB)
Normalizes factor using the specified norm.
Definition: factor.h:271
bool empty() const
Returns whether *this is empty.
Definition: smallset.h:172
T entropy() const
Returns the Shannon entropy of *this, .
Definition: prob.h:351
TFactor< T > operator*(T x) const
Returns product of *this with scalar x.
Definition: factor.h:318
this_type & takeExp()
Applies exponent pointwise.
Definition: prob.h:512