libDAI
prob.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_prob_h
14 #define __defined_libdai_prob_h
15 
16 
17 #include <cmath>
18 #include <vector>
19 #include <ostream>
20 #include <algorithm>
21 #include <numeric>
22 #include <functional>
23 #include <dai/util.h>
24 #include <dai/exceptions.h>
25 
26 
27 namespace dai {
28 
29 
31 template<typename T> struct fo_id : public std::unary_function<T, T> {
33  T operator()( const T &x ) const {
34  return x;
35  }
36 };
37 
38 
40 template<typename T> struct fo_abs : public std::unary_function<T, T> {
42  T operator()( const T &x ) const {
43  if( x < (T)0 )
44  return -x;
45  else
46  return x;
47  }
48 };
49 
50 
52 template<typename T> struct fo_exp : public std::unary_function<T, T> {
54  T operator()( const T &x ) const {
55  return exp( x );
56  }
57 };
58 
59 
61 template<typename T> struct fo_log : public std::unary_function<T, T> {
63  T operator()( const T &x ) const {
64  return log( x );
65  }
66 };
67 
68 
70 template<typename T> struct fo_log0 : public std::unary_function<T, T> {
72  T operator()( const T &x ) const {
73  if( x )
74  return log( x );
75  else
76  return 0;
77  }
78 };
79 
80 
82 template<typename T> struct fo_inv : public std::unary_function<T, T> {
84  T operator()( const T &x ) const {
85  return 1 / x;
86  }
87 };
88 
89 
91 template<typename T> struct fo_inv0 : public std::unary_function<T, T> {
93  T operator()( const T &x ) const {
94  if( x )
95  return 1 / x;
96  else
97  return 0;
98  }
99 };
100 
101 
103 template<typename T> struct fo_plog0p : public std::unary_function<T, T> {
105  T operator()( const T &p ) const {
106  return p * dai::log0(p);
107  }
108 };
109 
110 
112 template<typename T> struct fo_divides0 : public std::binary_function<T, T, T> {
114  T operator()( const T &x, const T &y ) const {
115  if( y == (T)0 )
116  return (T)0;
117  else
118  return x / y;
119  }
120 };
121 
122 
124 template<typename T> struct fo_KL : public std::binary_function<T, T, T> {
126  T operator()( const T &p, const T &q ) const {
127  if( p == (T)0 )
128  return (T)0;
129  else
130  return p * (log(p) - log(q));
131  }
132 };
133 
134 
136 template<typename T> struct fo_Hellinger : public std::binary_function<T, T, T> {
138  T operator()( const T &p, const T &q ) const {
139  T x = sqrt(p) - sqrt(q);
140  return x * x;
141  }
142 };
143 
144 
146 template<typename T> struct fo_pow : public std::binary_function<T, T, T> {
148  T operator()( const T &x, const T &y ) const {
149  if( y != 1 )
150  return pow( x, y );
151  else
152  return x;
153  }
154 };
155 
156 
158 template<typename T> struct fo_max : public std::binary_function<T, T, T> {
160  T operator()( const T &x, const T &y ) const {
161  return (x > y) ? x : y;
162  }
163 };
164 
165 
167 template<typename T> struct fo_min : public std::binary_function<T, T, T> {
169  T operator()( const T &x, const T &y ) const {
170  return (x > y) ? y : x;
171  }
172 };
173 
174 
176 template<typename T> struct fo_absdiff : public std::binary_function<T, T, T> {
178  T operator()( const T &x, const T &y ) const {
179  return dai::abs( x - y );
180  }
181 };
182 
183 
185 
193 template <typename T>
194 class TProb {
195  public:
197  typedef std::vector<T> container_type;
198 
201 
202  private:
204  container_type _p;
205 
206  public:
208 
209  TProb() : _p() {}
211 
213  explicit TProb( size_t n ) : _p( n, (T)1 / n ) {}
214 
216  explicit TProb( size_t n, T p ) : _p( n, p ) {}
217 
219 
225  template <typename TIterator>
226  TProb( TIterator begin, TIterator end, size_t sizeHint ) : _p() {
227  _p.reserve( sizeHint );
228  _p.insert( _p.begin(), begin, end );
229  }
230 
232 
235  template <typename S>
236  TProb( const std::vector<S> &v ) : _p() {
237  _p.reserve( v.size() );
238  _p.insert( _p.begin(), v.begin(), v.end() );
239  }
241 
243  typedef typename container_type::const_iterator const_iterator;
245  typedef typename container_type::iterator iterator;
247  typedef typename container_type::const_reverse_iterator const_reverse_iterator;
249  typedef typename container_type::reverse_iterator reverse_iterator;
250 
252 
253  iterator begin() { return _p.begin(); }
256  const_iterator begin() const { return _p.begin(); }
257 
259  iterator end() { return _p.end(); }
261  const_iterator end() const { return _p.end(); }
262 
264  reverse_iterator rbegin() { return _p.rbegin(); }
266  const_reverse_iterator rbegin() const { return _p.rbegin(); }
267 
269  reverse_iterator rend() { return _p.rend(); }
271  const_reverse_iterator rend() const { return _p.rend(); }
273 
275 
276  void resize( size_t sz ) {
277  _p.resize( sz );
278  }
280 
282 
283  T get( size_t i ) const {
285 #ifdef DAI_DEBUG
286  return _p.at(i);
287 #else
288  return _p[i];
289 #endif
290  }
291 
293  void set( size_t i, T val ) {
294  DAI_DEBASSERT( i < _p.size() );
295  _p[i] = val;
296  }
298 
300 
301  const container_type& p() const { return _p; }
303 
305  container_type& p() { return _p; }
306 
308  T operator[]( size_t i ) const { return get(i); }
309 
311  size_t size() const { return _p.size(); }
312 
314 
322  template<typename unOp> T accumulateSum( T init, unOp op ) const {
323  T t = op(init);
324  for( const_iterator it = begin(); it != end(); it++ )
325  t += op(*it);
326  return t;
327  }
328 
330 
338  template<typename unOp> T accumulateMax( T init, unOp op, bool minimize ) const {
339  T t = op(init);
340  if( minimize ) {
341  for( const_iterator it = begin(); it != end(); it++ )
342  t = std::min( t, op(*it) );
343  } else {
344  for( const_iterator it = begin(); it != end(); it++ )
345  t = std::max( t, op(*it) );
346  }
347  return t;
348  }
349 
351  T entropy() const { return -accumulateSum( (T)0, fo_plog0p<T>() ); }
352 
354  T max() const { return accumulateMax( (T)(-INFINITY), fo_id<T>(), false ); }
355 
357  T min() const { return accumulateMax( (T)INFINITY, fo_id<T>(), true ); }
358 
360  T sum() const { return accumulateSum( (T)0, fo_id<T>() ); }
361 
363  T sumAbs() const { return accumulateSum( (T)0, fo_abs<T>() ); }
364 
366  T maxAbs() const { return accumulateMax( (T)0, fo_abs<T>(), false ); }
367 
369  bool hasNaNs() const {
370  bool foundnan = false;
371  for( const_iterator x = _p.begin(); x != _p.end(); x++ )
372  if( dai::isnan( *x ) ) {
373  foundnan = true;
374  break;
375  }
376  return foundnan;
377  }
378 
380  bool hasNegatives() const {
381  return (std::find_if( _p.begin(), _p.end(), std::bind2nd( std::less<T>(), (T)0 ) ) != _p.end());
382  }
383 
385  std::pair<size_t,T> argmax() const {
386  T max = _p[0];
387  size_t arg = 0;
388  for( size_t i = 1; i < size(); i++ ) {
389  if( _p[i] > max ) {
390  max = _p[i];
391  arg = i;
392  }
393  }
394  return std::make_pair( arg, max );
395  }
396 
398  size_t draw() {
399  Real x = rnd_uniform() * sum();
400  T s = 0;
401  for( size_t i = 0; i < size(); i++ ) {
402  s += get(i);
403  if( s > x )
404  return i;
405  }
406  return( size() - 1 );
407  }
408 
410 
412  bool operator<( const this_type& q ) const {
413  DAI_DEBASSERT( size() == q.size() );
414  return lexicographical_compare( begin(), end(), q.begin(), q.end() );
415  }
416 
418  bool operator==( const this_type& q ) const {
419  if( size() != q.size() )
420  return false;
421  return p() == q.p();
422  }
423 
425  std::string toString() const {
426  std::stringstream ss;
427  ss << *this;
428  return ss.str();
429  }
431 
433 
434  template<typename unaryOp> this_type pwUnaryTr( unaryOp op ) const {
436  this_type r;
437  r._p.reserve( size() );
438  std::transform( _p.begin(), _p.end(), back_inserter( r._p ), op );
439  return r;
440  }
441 
443  this_type operator- () const { return pwUnaryTr( std::negate<T>() ); }
444 
446  this_type abs() const { return pwUnaryTr( fo_abs<T>() ); }
447 
449  this_type exp() const { return pwUnaryTr( fo_exp<T>() ); }
450 
452 
454  this_type log(bool zero=false) const {
455  if( zero )
456  return pwUnaryTr( fo_log0<T>() );
457  else
458  return pwUnaryTr( fo_log<T>() );
459  }
460 
462 
464  this_type inverse(bool zero=true) const {
465  if( zero )
466  return pwUnaryTr( fo_inv0<T>() );
467  else
468  return pwUnaryTr( fo_inv<T>() );
469  }
470 
472 
474  this_type normalized( ProbNormType norm = dai::NORMPROB ) const {
475  T Z = 0;
476  if( norm == dai::NORMPROB )
477  Z = sum();
478  else if( norm == dai::NORMLINF )
479  Z = maxAbs();
480  if( Z == (T)0 ) {
481  DAI_THROW(NOT_NORMALIZABLE);
482  return *this;
483  } else
484  return pwUnaryTr( std::bind2nd( std::divides<T>(), Z ) );
485  }
487 
489 
490  template<typename unaryOp> this_type& pwUnaryOp( unaryOp op ) {
492  std::transform( _p.begin(), _p.end(), _p.begin(), op );
493  return *this;
494  }
495 
497  this_type& randomize() {
498  std::generate( _p.begin(), _p.end(), rnd_uniform );
499  return *this;
500  }
501 
503  this_type& setUniform () {
504  fill( (T)1 / size() );
505  return *this;
506  }
507 
509  this_type& takeAbs() { return pwUnaryOp( fo_abs<T>() ); }
510 
512  this_type& takeExp() { return pwUnaryOp( fo_exp<T>() ); }
513 
515 
517  this_type& takeLog(bool zero=false) {
518  if( zero ) {
519  return pwUnaryOp( fo_log0<T>() );
520  } else
521  return pwUnaryOp( fo_log<T>() );
522  }
523 
525 
527  T normalize( ProbNormType norm=dai::NORMPROB ) {
528  T Z = 0;
529  if( norm == dai::NORMPROB )
530  Z = sum();
531  else if( norm == dai::NORMLINF )
532  Z = maxAbs();
533  if( Z == (T)0 )
534  DAI_THROW(NOT_NORMALIZABLE);
535  else
536  *this /= Z;
537  return Z;
538  }
540 
542 
543  this_type& fill( T x ) {
545  std::fill( _p.begin(), _p.end(), x );
546  return *this;
547  }
548 
550  this_type& operator+= (T x) {
551  if( x != 0 )
552  return pwUnaryOp( std::bind2nd( std::plus<T>(), x ) );
553  else
554  return *this;
555  }
556 
558  this_type& operator-= (T x) {
559  if( x != 0 )
560  return pwUnaryOp( std::bind2nd( std::minus<T>(), x ) );
561  else
562  return *this;
563  }
564 
566  this_type& operator*= (T x) {
567  if( x != 1 )
568  return pwUnaryOp( std::bind2nd( std::multiplies<T>(), x ) );
569  else
570  return *this;
571  }
572 
574  this_type& operator/= (T x) {
575  if( x != 1 )
576  return pwUnaryOp( std::bind2nd( fo_divides0<T>(), x ) );
577  else
578  return *this;
579  }
580 
582  this_type& operator^= (T x) {
583  if( x != (T)1 )
584  return pwUnaryOp( std::bind2nd( fo_pow<T>(), x) );
585  else
586  return *this;
587  }
589 
591 
592  this_type operator+ (T x) const { return pwUnaryTr( std::bind2nd( std::plus<T>(), x ) ); }
594 
596  this_type operator- (T x) const { return pwUnaryTr( std::bind2nd( std::minus<T>(), x ) ); }
597 
599  this_type operator* (T x) const { return pwUnaryTr( std::bind2nd( std::multiplies<T>(), x ) ); }
600 
602  this_type operator/ (T x) const { return pwUnaryTr( std::bind2nd( fo_divides0<T>(), x ) ); }
603 
605  this_type operator^ (T x) const { return pwUnaryTr( std::bind2nd( fo_pow<T>(), x ) ); }
607 
609 
610 
615  template<typename binaryOp> this_type& pwBinaryOp( const this_type &q, binaryOp op ) {
616  DAI_DEBASSERT( size() == q.size() );
617  std::transform( _p.begin(), _p.end(), q._p.begin(), _p.begin(), op );
618  return *this;
619  }
620 
622 
624  this_type& operator+= (const this_type & q) { return pwBinaryOp( q, std::plus<T>() ); }
625 
627 
629  this_type& operator-= (const this_type & q) { return pwBinaryOp( q, std::minus<T>() ); }
630 
632 
634  this_type& operator*= (const this_type & q) { return pwBinaryOp( q, std::multiplies<T>() ); }
635 
637 
640  this_type& operator/= (const this_type & q) { return pwBinaryOp( q, fo_divides0<T>() ); }
641 
643 
646  this_type& divide (const this_type & q) { return pwBinaryOp( q, std::divides<T>() ); }
647 
649 
651  this_type& operator^= (const this_type & q) { return pwBinaryOp( q, fo_pow<T>() ); }
653 
655 
656 
661  template<typename binaryOp> this_type pwBinaryTr( const this_type &q, binaryOp op ) const {
662  DAI_DEBASSERT( size() == q.size() );
663  TProb<T> r;
664  r._p.reserve( size() );
665  std::transform( _p.begin(), _p.end(), q._p.begin(), back_inserter( r._p ), op );
666  return r;
667  }
668 
670 
672  this_type operator+ ( const this_type& q ) const { return pwBinaryTr( q, std::plus<T>() ); }
673 
675 
677  this_type operator- ( const this_type& q ) const { return pwBinaryTr( q, std::minus<T>() ); }
678 
680 
682  this_type operator* ( const this_type &q ) const { return pwBinaryTr( q, std::multiplies<T>() ); }
683 
685 
688  this_type operator/ ( const this_type &q ) const { return pwBinaryTr( q, fo_divides0<T>() ); }
689 
691 
694  this_type divided_by( const this_type &q ) const { return pwBinaryTr( q, std::divides<T>() ); }
695 
697 
699  this_type operator^ ( const this_type &q ) const { return pwBinaryTr( q, fo_pow<T>() ); }
701 
703 
705  template<typename binOp1, typename binOp2> T innerProduct( const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2 ) const {
706  DAI_DEBASSERT( size() == q.size() );
707  return std::inner_product( begin(), end(), q.begin(), init, binaryOp1, binaryOp2 );
708  }
709 };
710 
711 
713 
716 template<typename T> T dist( const TProb<T> &p, const TProb<T> &q, ProbDistType dt ) {
717  switch( dt ) {
718  case DISTL1:
719  return p.innerProduct( q, (T)0, std::plus<T>(), fo_absdiff<T>() );
720  case DISTLINF:
721  return p.innerProduct( q, (T)0, fo_max<T>(), fo_absdiff<T>() );
722  case DISTTV:
723  return p.innerProduct( q, (T)0, std::plus<T>(), fo_absdiff<T>() ) / 2;
724  case DISTKL:
725  return p.innerProduct( q, (T)0, std::plus<T>(), fo_KL<T>() );
726  case DISTHEL:
727  return p.innerProduct( q, (T)0, std::plus<T>(), fo_Hellinger<T>() ) / 2;
728  default:
729  DAI_THROW(UNKNOWN_ENUM_VALUE);
730  return INFINITY;
731  }
732 }
733 
734 
736 
738 template<typename T> std::ostream& operator<< (std::ostream& os, const TProb<T>& p) {
739  os << "(";
740  for( size_t i = 0; i < p.size(); i++ )
741  os << ((i != 0) ? ", " : "") << p.get(i);
742  os << ")";
743  return os;
744 }
745 
746 
748 
751 template<typename T> TProb<T> min( const TProb<T> &a, const TProb<T> &b ) {
752  return a.pwBinaryTr( b, fo_min<T>() );
753 }
754 
755 
757 
760 template<typename T> TProb<T> max( const TProb<T> &a, const TProb<T> &b ) {
761  return a.pwBinaryTr( b, fo_max<T>() );
762 }
763 
764 
767 
768 
769 } // end of namespace dai
770 
771 
772 #endif
Function object that takes the logarithm.
Definition: prob.h:61
T operator()(const T &x) const
Returns x.
Definition: prob.h:33
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
Function object that returns p*log0(p)
Definition: prob.h:103
TProb()
Default constructor (constructs empty vector)
Definition: prob.h:210
TProb(size_t n, T p)
Construct vector of length n with each entry set to p.
Definition: prob.h:216
T operator()(const T &x, const T &y) const
Returns (x > y ? x : y)
Definition: prob.h:160
this_type & fill(T x)
Sets all entries to x.
Definition: prob.h:544
ProbNormType
Enumerates different ways of normalizing a probability measure.
Definition: util.h:255
Real pow(Real x, Real y)
Returns x to the power y.
Definition: util.h:132
TProb(const std::vector< S > &v)
Construct vector from another vector.
Definition: prob.h:236
ProbDistType
Enumerates different distance measures between probability measures.
Definition: util.h:265
T accumulateMax(T init, unOp op, bool minimize) const
Accumulate all values (similar to std::accumulate) by maximization/minimization.
Definition: prob.h:338
this_type normalized(ProbNormType norm=dai::NORMPROB) const
Returns normalized copy of *this, using the specified norm.
Definition: prob.h:474
std::vector< T > container_type
Type of data structure used for storing the values.
Definition: prob.h:197
Real log(Real x)
Returns logarithm of x.
Definition: util.h:113
this_type exp() const
Returns pointwise exponent.
Definition: prob.h:449
TProb(TIterator begin, TIterator end, size_t sizeHint)
Construct vector from a range.
Definition: prob.h:226
TProb< Real > Prob
Represents a vector with entries of type dai::Real.
Definition: prob.h:766
Function object that takes the exponent.
Definition: prob.h:52
Function object useful for calculating the KL distance.
Definition: prob.h:124
Function object that returns the minimum of two values.
Definition: prob.h:167
this_type divided_by(const this_type &q) const
Pointwise division by q, where division by 0 yields +Inf.
Definition: prob.h:694
this_type & takeAbs()
Applies absolute value pointwise.
Definition: prob.h:509
this_type & pwUnaryOp(unaryOp op)
Applies unary operation op pointwise.
Definition: prob.h:491
container_type::iterator iterator
Iterator over the elements.
Definition: prob.h:245
T operator()(const T &x) const
Returns (x == 0 ? 0 : (1 / x))
Definition: prob.h:93
this_type pwUnaryTr(unaryOp op) const
Returns the result of applying operation op pointwise on *this.
Definition: prob.h:435
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
T operator()(const T &x) const
Returns 1 / x.
Definition: prob.h:84
bool hasNaNs() const
Returns true if one or more entries are NaN.
Definition: prob.h:369
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
T abs(const T &t)
Returns absolute value of t.
Definition: util.h:155
this_type abs() const
Returns pointwise absolute value.
Definition: prob.h:446
this_type operator^(T x) const
Returns *this raised to the power x.
Definition: prob.h:605
T max() const
Returns maximum value of all entries.
Definition: prob.h:354
this_type operator+(T x) const
Returns sum of *this and scalar x.
Definition: prob.h:593
std::pair< size_t, T > argmax() const
Returns a pair consisting of the index of the maximum value and the maximum value itself...
Definition: prob.h:385
this_type & pwBinaryOp(const this_type &q, binaryOp op)
Applies binary operation pointwise on two vectors.
Definition: prob.h:615
this_type & randomize()
Draws all entries i.i.d. from a uniform distribution on [0,1)
Definition: prob.h:497
container_type::const_reverse_iterator const_reverse_iterator
Constant reverse iterator over the elements.
Definition: prob.h:247
Represents a vector with entries of type T.
Definition: prob.h:194
Function object that returns the maximum of two values.
Definition: prob.h:158
reverse_iterator rend()
Returns reverse iterator that points beyond the first element.
Definition: prob.h:269
T operator()(const T &x, const T &y) const
Returns (x > y ? y : x)
Definition: prob.h:169
T sum() const
Returns sum of all entries.
Definition: prob.h:360
T dist(const TProb< T > &p, const TProb< T > &q, ProbDistType dt)
Returns distance between p and q, measured using distance measure dt.
Definition: prob.h:716
Function object that returns x to the power y.
Definition: prob.h:146
bool isnan(Real x)
Returns true if argument is NAN (Not A Number)
Definition: util.cpp:44
T operator()(const T &x) const
Returns log(x)
Definition: prob.h:63
Function object that takes the absolute value.
Definition: prob.h:40
T operator()(const T &x) const
Returns exp(x)
Definition: prob.h:54
std::string toString() const
Formats a TProb as a string.
Definition: prob.h:425
const_iterator end() const
Returns constant iterator that points beyond the last element.
Definition: prob.h:261
const container_type & p() const
Returns a const reference to the wrapped container.
Definition: prob.h:302
T operator()(const T &x) const
Returns abs(x)
Definition: prob.h:42
this_type & operator^=(T x)
Raises entries to the power x.
Definition: prob.h:582
Function object similar to std::divides(), but different in that dividing by zero results in zero...
Definition: prob.h:112
bool operator<(const this_type &q) const
Lexicographical comparison.
Definition: prob.h:412
T innerProduct(const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2) const
Performs a generalized inner product, similar to std::inner_product.
Definition: prob.h:705
T operator()(const T &x, const T &y) const
Returns (x ^ y)
Definition: prob.h:148
T operator()(const T &x, const T &y) const
Returns (y == 0 ? 0 : (x / y))
Definition: prob.h:114
this_type & operator/=(T x)
Divides each entry by scalar x, where division by 0 yields 0.
Definition: prob.h:574
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
container_type::reverse_iterator reverse_iterator
Reverse iterator over the elements.
Definition: prob.h:249
this_type & operator+=(T x)
Adds scalar x to each entry.
Definition: prob.h:550
TProb(size_t n)
Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entr...
Definition: prob.h:213
Defines the Exception class and macros for throwing exceptions and doing assertions.
container_type::const_iterator const_iterator
Constant iterator over the elements.
Definition: prob.h:243
Function object useful for calculating the Hellinger distance.
Definition: prob.h:136
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
TProb< T > min(const TProb< T > &a, const TProb< T > &b)
Returns the pointwise minimum of a and b.
Definition: prob.h:751
Function object that returns the absolute difference of x and y.
Definition: prob.h:176
T accumulateSum(T init, unOp op) const
Accumulate all values (similar to std::accumulate) by summing.
Definition: prob.h:322
Function object that takes the inverse.
Definition: prob.h:82
Real log0(Real x)
Returns logarithm of x, or 0 if x == 0.
Definition: util.h:118
this_type operator*(T x) const
Returns product of *this with scalar x.
Definition: prob.h:599
const_iterator begin() const
Returns constant iterator that points to the first element.
Definition: prob.h:256
Function object that takes the inverse, except that 1/0 is defined to be 0.
Definition: prob.h:91
T operator()(const T &p, const T &q) const
Returns (sqrt(p) - sqrt(q))^2.
Definition: prob.h:138
T operator()(const T &x, const T &y) const
Returns abs( x - y )
Definition: prob.h:178
T operator[](size_t i) const
Returns a copy of the i 'th entry.
Definition: prob.h:308
T sumAbs() const
Return sum of absolute value of all entries.
Definition: prob.h:363
container_type _p
The data structure that stores the values.
Definition: prob.h:204
size_t size() const
Returns length of the vector (i.e., the number of entries)
Definition: prob.h:311
this_type & divide(const this_type &q)
Pointwise division by q, where division by 0 yields +Inf.
Definition: prob.h:646
this_type & takeLog(bool zero=false)
Applies logarithm pointwise.
Definition: prob.h:517
bool operator==(const this_type &q) const
Comparison.
Definition: prob.h:418
Function object that takes the logarithm, except that log(0) is defined to be 0.
Definition: prob.h:70
T operator()(const T &p) const
Returns p * log0(p)
Definition: prob.h:105
iterator end()
Returns iterator that points beyond the last element.
Definition: prob.h:259
TProb< T > max(const TProb< T > &a, const TProb< T > &b)
Returns the pointwise maximum of a and b.
Definition: prob.h:760
T min() const
Returns minimum value of all entries.
Definition: prob.h:357
this_type operator-() const
Returns negative of *this.
Definition: prob.h:443
this_type operator/(T x) const
Returns quotient of *this and scalar x, where division by 0 yields 0.
Definition: prob.h:602
this_type & operator-=(T x)
Subtracts scalar x from each entry.
Definition: prob.h:558
Namespace for libDAI.
Definition: alldai.cpp:16
this_type inverse(bool zero=true) const
Returns pointwise inverse.
Definition: prob.h:464
reverse_iterator rbegin()
Returns reverse iterator that points to the last element.
Definition: prob.h:264
size_t draw()
Returns a random index, according to the (normalized) distribution described by *this.
Definition: prob.h:398
const_reverse_iterator rend() const
Returns constant reverse iterator that points beyond the first element.
Definition: prob.h:271
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
T operator()(const T &x) const
Returns (x == 0 ? 0 : log(x))
Definition: prob.h:72
this_type & operator*=(T x)
Multiplies each entry with scalar x.
Definition: prob.h:566
const_reverse_iterator rbegin() const
Returns constant reverse iterator that points to the last element.
Definition: prob.h:266
Function object that returns the value itself.
Definition: prob.h:31
Real rnd_uniform()
Returns a real number, distributed uniformly on [0,1)
Definition: util.cpp:87
container_type & p()
Returns a reference to the wrapped container.
Definition: prob.h:305
T operator()(const T &p, const T &q) const
Returns (p == 0 ? 0 : (p * (log(p) - log(q))))
Definition: prob.h:126
iterator begin()
Returns iterator that points to the first element.
Definition: prob.h:254
TProb< T > this_type
Shorthand.
Definition: prob.h:200
Real exp(Real x)
Returns exponent of x.
Definition: util.h:123
T entropy() const
Returns the Shannon entropy of *this, .
Definition: prob.h:351
this_type & takeExp()
Applies exponent pointwise.
Definition: prob.h:512