libDAI
factor.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_factor_h
14 #define __defined_libdai_factor_h
15 
16 
17 #include <iostream>
18 #include <functional>
19 #include <cmath>
20 #include <dai/prob.h>
21 #include <dai/varset.h>
22 #include <dai/index.h>
23 #include <dai/util.h>
24 
25 
26 namespace dai {
27 
28 
30 
54 template <typename T>
55 class TFactor {
56  private:
61 
62  public:
64 
65  TFactor ( T p = 1 ) : _vs(), _p(1,p) {}
67 
69  TFactor( const Var &v ) : _vs(v), _p(v.states()) {}
70 
72  TFactor( const VarSet& vars ) : _vs(vars), _p() {
73  _p = TProb<T>( BigInt_size_t( _vs.nrStates() ) );
74  }
75 
77  TFactor( const VarSet& vars, T p ) : _vs(vars), _p() {
78  _p = TProb<T>( BigInt_size_t( _vs.nrStates() ), p );
79  }
80 
82 
86  template<typename S>
87  TFactor( const VarSet& vars, const std::vector<S> &x ) : _vs(vars), _p() {
88  DAI_ASSERT( (BigInt)x.size() == vars.nrStates() );
89  _p = TProb<T>( x.begin(), x.end(), x.size() );
90  }
91 
93 
96  TFactor( const VarSet& vars, const T* p ) : _vs(vars), _p() {
97  size_t N = BigInt_size_t( _vs.nrStates() );
98  _p = TProb<T>( p, p + N, N );
99  }
100 
102  TFactor( const VarSet& vars, const TProb<T> &p ) : _vs(vars), _p(p) {
103  DAI_ASSERT( _vs.nrStates() == (BigInt)_p.size() );
104  }
105 
107  TFactor( const std::vector<Var> &vars, const std::vector<T> &p ) : _vs(vars.begin(), vars.end(), vars.size()), _p(p.size()) {
108  BigInt nrStates = 1;
109  for( size_t i = 0; i < vars.size(); i++ )
110  nrStates *= vars[i].states();
111  DAI_ASSERT( nrStates == p.size() );
112  Permute permindex(vars);
113  for( size_t li = 0; li < p.size(); ++li )
114  _p.set( permindex.convertLinearIndex(li), p[li] );
115  }
117 
119 
120  void set( size_t i, T val ) { _p.set( i, val ); }
122 
124  T get( size_t i ) const { return _p[i]; }
126 
128 
129  const TProb<T>& p() const { return _p; }
131 
133  TProb<T>& p() { return _p; }
134 
136  T operator[] (size_t i) const { return _p[i]; }
137 
139  const VarSet& vars() const { return _vs; }
140 
142  VarSet& vars() { return _vs; }
143 
145 
147  size_t nrStates() const { return _p.size(); }
148 
150  T entropy() const { return _p.entropy(); }
151 
153  T max() const { return _p.max(); }
154 
156  T min() const { return _p.min(); }
157 
159  T sum() const { return _p.sum(); }
160 
162  T sumAbs() const { return _p.sumAbs(); }
163 
165  T maxAbs() const { return _p.maxAbs(); }
166 
168  bool hasNaNs() const { return _p.hasNaNs(); }
169 
171  bool hasNegatives() const { return _p.hasNegatives(); }
172 
174  T strength( const Var &i, const Var &j ) const;
175 
177  bool operator==( const TFactor<T>& y ) const {
178  return (_vs == y._vs) && (_p == y._p);
179  }
180 
182  std::string toString() const {
183  std::stringstream ss;
184  ss << *this;
185  return ss.str();
186  }
188 
190 
191  TFactor<T> operator- () const {
193  // Note: the alternative (shorter) way of implementing this,
194  // return TFactor<T>( _vs, _p.abs() );
195  // is slower because it invokes the copy constructor of TProb<T>
196  TFactor<T> x;
197  x._vs = _vs;
198  x._p = -_p;
199  return x;
200  }
201 
203  TFactor<T> abs() const {
204  TFactor<T> x;
205  x._vs = _vs;
206  x._p = _p.abs();
207  return x;
208  }
209 
211  TFactor<T> exp() const {
212  TFactor<T> x;
213  x._vs = _vs;
214  x._p = _p.exp();
215  return x;
216  }
217 
219 
221  TFactor<T> log(bool zero=false) const {
222  TFactor<T> x;
223  x._vs = _vs;
224  x._p = _p.log(zero);
225  return x;
226  }
227 
229 
231  TFactor<T> inverse(bool zero=true) const {
232  TFactor<T> x;
233  x._vs = _vs;
234  x._p = _p.inverse(zero);
235  return x;
236  }
237 
239 
241  TFactor<T> normalized( ProbNormType norm=NORMPROB ) const {
242  TFactor<T> x;
243  x._vs = _vs;
244  x._p = _p.normalized( norm );
245  return x;
246  }
248 
250 
251  TFactor<T>& randomize() { _p.randomize(); return *this; }
253 
255  TFactor<T>& setUniform() { _p.setUniform(); return *this; }
256 
258  TFactor<T>& takeAbs() { _p.takeAbs(); return *this; }
259 
261  TFactor<T>& takeExp() { _p.takeExp(); return *this; }
262 
264 
266  TFactor<T>& takeLog( bool zero = false ) { _p.takeLog(zero); return *this; }
267 
269 
271  T normalize( ProbNormType norm=NORMPROB ) { return _p.normalize( norm ); }
273 
275 
276  TFactor<T>& fill (T x) { _p.fill( x ); return *this; }
278 
280  TFactor<T>& operator+= (T x) { _p += x; return *this; }
281 
283  TFactor<T>& operator-= (T x) { _p -= x; return *this; }
284 
286  TFactor<T>& operator*= (T x) { _p *= x; return *this; }
287 
289  TFactor<T>& operator/= (T x) { _p /= x; return *this; }
290 
292  TFactor<T>& operator^= (T x) { _p ^= x; return *this; }
294 
296 
297  TFactor<T> operator+ (T x) const {
299  // Note: the alternative (shorter) way of implementing this,
300  // TFactor<T> result(*this);
301  // result._p += x;
302  // is slower because it invokes the copy constructor of TFactor<T>
303  TFactor<T> result;
304  result._vs = _vs;
305  result._p = p() + x;
306  return result;
307  }
308 
310  TFactor<T> operator- (T x) const {
311  TFactor<T> result;
312  result._vs = _vs;
313  result._p = p() - x;
314  return result;
315  }
316 
318  TFactor<T> operator* (T x) const {
319  TFactor<T> result;
320  result._vs = _vs;
321  result._p = p() * x;
322  return result;
323  }
324 
326  TFactor<T> operator/ (T x) const {
327  TFactor<T> result;
328  result._vs = _vs;
329  result._p = p() / x;
330  return result;
331  }
332 
334  TFactor<T> operator^ (T x) const {
335  TFactor<T> result;
336  result._vs = _vs;
337  result._p = p() ^ x;
338  return result;
339  }
341 
343 
344 
349  template<typename binOp> TFactor<T>& binaryOp( const TFactor<T> &g, binOp op ) {
350  if( _vs == g._vs ) // optimize special case
351  _p.pwBinaryOp( g._p, op );
352  else {
353  TFactor<T> f(*this); // make a copy
354  _vs |= g._vs;
355  size_t N = BigInt_size_t( _vs.nrStates() );
356 
357  IndexFor i_f( f._vs, _vs );
358  IndexFor i_g( g._vs, _vs );
359 
360  _p.p().clear();
361  _p.p().reserve( N );
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] ) );
364  }
365  return *this;
366  }
367 
369 
373  TFactor<T>& operator+= (const TFactor<T>& g) { return binaryOp( g, std::plus<T>() ); }
374 
376 
380  TFactor<T>& operator-= (const TFactor<T>& g) { return binaryOp( g, std::minus<T>() ); }
381 
383 
387  TFactor<T>& operator*= (const TFactor<T>& g) { return binaryOp( g, std::multiplies<T>() ); }
388 
390 
394  TFactor<T>& operator/= (const TFactor<T>& g) { return binaryOp( g, fo_divides0<T>() ); }
396 
398 
399 
404  template<typename binOp> TFactor<T> binaryTr( const TFactor<T> &g, binOp op ) const {
405  // Note that to prevent a copy to be made, it is crucial
406  // that the result is declared outside the if-else construct.
407  TFactor<T> result;
408  if( _vs == g._vs ) { // optimize special case
409  result._vs = _vs;
410  result._p = _p.pwBinaryTr( g._p, op );
411  } else {
412  result._vs = _vs | g._vs;
413  size_t N = BigInt_size_t( result._vs.nrStates() );
414 
415  IndexFor i_f( _vs, result.vars() );
416  IndexFor i_g( g._vs, result.vars() );
417 
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] ) );
422  }
423  return result;
424  }
425 
427 
431  TFactor<T> operator+ (const TFactor<T>& g) const {
432  return binaryTr(g,std::plus<T>());
433  }
434 
436 
440  TFactor<T> operator- (const TFactor<T>& g) const {
441  return binaryTr(g,std::minus<T>());
442  }
443 
445 
449  TFactor<T> operator* (const TFactor<T>& g) const {
450  return binaryTr(g,std::multiplies<T>());
451  }
452 
454 
458  TFactor<T> operator/ (const TFactor<T>& g) const {
459  return binaryTr(g,fo_divides0<T>());
460  }
462 
464 
465 
477  TFactor<T> slice( const VarSet& vars, size_t varsState ) const;
478 
480 
485  TFactor<T> embed(const VarSet & vars) const {
486  DAI_ASSERT( vars >> _vs );
487  if( _vs == vars )
488  return *this;
489  else
490  return (*this) * TFactor<T>(vars / _vs, (T)1);
491  }
492 
494  TFactor<T> marginal(const VarSet &vars, bool normed=true) const;
495 
497  TFactor<T> maxMarginal(const VarSet &vars, bool normed=true) const;
499 };
500 
501 
502 template<typename T> TFactor<T> TFactor<T>::slice( const VarSet& vars, size_t varsState ) const {
503  DAI_ASSERT( vars << _vs );
504  VarSet varsrem = _vs / vars;
505  TFactor<T> result( varsrem, T(0) );
506 
507  // OPTIMIZE ME
508  IndexFor i_vars (vars, _vs);
509  IndexFor i_varsrem (varsrem, _vs);
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] );
513 
514  return result;
515 }
516 
517 
518 template<typename T> TFactor<T> TFactor<T>::marginal(const VarSet &vars, bool normed) const {
519  VarSet res_vars = vars & _vs;
520 
521  TFactor<T> res( res_vars, 0.0 );
522 
523  IndexFor i_res( res_vars, _vs );
524  for( size_t i = 0; i < _p.size(); i++, ++i_res )
525  res.set( i_res, res[i_res] + _p[i] );
526 
527  if( normed )
528  res.normalize( NORMPROB );
529 
530  return res;
531 }
532 
533 
534 template<typename T> TFactor<T> TFactor<T>::maxMarginal(const VarSet &vars, bool normed) const {
535  VarSet res_vars = vars & _vs;
536 
537  TFactor<T> res( res_vars, 0.0 );
538 
539  IndexFor i_res( res_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] );
543 
544  if( normed )
545  res.normalize( NORMPROB );
546 
547  return res;
548 }
549 
550 
551 template<typename T> T TFactor<T>::strength( const Var &i, const Var &j ) const {
552  DAI_DEBASSERT( _vs.contains( i ) );
553  DAI_DEBASSERT( _vs.contains( j ) );
554  DAI_DEBASSERT( i != j );
555  VarSet ij(i, j);
556 
557  T max = 0.0;
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;
565  if( i < j )
566  bs = i.states();
567  else
568  as = j.states();
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();
571  T f = f1 * f2;
572  if( f > max )
573  max = f;
574  }
575 
576  return std::tanh( 0.25 * std::log( max ) );
577 }
578 
579 
581 
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];
587  os << "))";
588  return os;
589 }
590 
591 
593 
596 template<typename T> T dist( const TFactor<T> &f, const TFactor<T> &g, ProbDistType dt ) {
597  if( f.vars().empty() || g.vars().empty() )
598  return -1;
599  else {
600  DAI_DEBASSERT( f.vars() == g.vars() );
601  return dist( f.p(), g.p(), dt );
602  }
603 }
604 
605 
607 
610 template<typename T> TFactor<T> max( const TFactor<T> &f, const TFactor<T> &g ) {
611  DAI_ASSERT( f.vars() == g.vars() );
612  return TFactor<T>( f.vars(), max( f.p(), g.p() ) );
613 }
614 
615 
617 
620 template<typename T> TFactor<T> min( const TFactor<T> &f, const TFactor<T> &g ) {
621  DAI_ASSERT( f.vars() == g.vars() );
622  return TFactor<T>( f.vars(), min( f.p(), g.p() ) );
623 }
624 
625 
627 
630 template<typename T> T MutualInfo(const TFactor<T> &f) {
631  DAI_ASSERT( f.vars().size() == 2 );
632  VarSet::const_iterator it = f.vars().begin();
633  Var i = *it; it++; Var j = *it;
634  TFactor<T> projection = f.marginal(i) * f.marginal(j);
635  return dist( f.normalized(), projection, DISTKL );
636 }
637 
638 
641 
642 
644 
647 Factor createFactorIsing( const Var &x, Real h );
648 
649 
651 
655 Factor createFactorIsing( const Var &x1, const Var &x2, Real J );
656 
657 
659 
664 Factor createFactorExpGauss( const VarSet &vs, Real beta );
665 
666 
668 
672 Factor createFactorPotts( const Var &x1, const Var &x2, Real J );
673 
674 
676 
679 Factor createFactorDelta( const Var &v, size_t state );
680 
681 
683 
686 Factor createFactorDelta( const VarSet& vs, size_t state );
687 
688 
689 } // end of namespace dai
690 
691 
692 #endif
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