13 #ifndef __defined_libdai_prob_h
14 #define __defined_libdai_prob_h
31 template<
typename T>
struct fo_id :
public std::unary_function<T, T> {
40 template<
typename T>
struct fo_abs :
public std::unary_function<T, T> {
52 template<
typename T>
struct fo_exp :
public std::unary_function<T, T> {
61 template<
typename T>
struct fo_log :
public std::unary_function<T, T> {
70 template<
typename T>
struct fo_log0 :
public std::unary_function<T, T> {
82 template<
typename T>
struct fo_inv :
public std::unary_function<T, T> {
91 template<
typename T>
struct fo_inv0 :
public std::unary_function<T, T> {
103 template<
typename T>
struct fo_plog0p :
public std::unary_function<T, T> {
112 template<
typename T>
struct fo_divides0 :
public std::binary_function<T, T, T> {
124 template<
typename T>
struct fo_KL :
public std::binary_function<T, T, T> {
130 return p * (
log(p) -
log(q));
136 template<
typename T>
struct fo_Hellinger :
public std::binary_function<T, T, T> {
139 T x = sqrt(p) - sqrt(q);
146 template<
typename T>
struct fo_pow :
public std::binary_function<T, T, T> {
158 template<
typename T>
struct fo_max :
public std::binary_function<T, T, T> {
161 return (x > y) ? x : y;
167 template<
typename T>
struct fo_min :
public std::binary_function<T, T, T> {
170 return (x > y) ? y : x;
176 template<
typename T>
struct fo_absdiff :
public std::binary_function<T, T, T> {
193 template <
typename T>
213 explicit TProb(
size_t n ) : _p( n, (T)1 / n ) {}
216 explicit TProb(
size_t n, T
p ) : _p( n, p ) {}
225 template <
typename TIterator>
227 _p.reserve( sizeHint );
228 _p.insert( _p.begin(),
begin,
end );
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() );
245 typedef typename container_type::iterator
iterator;
253 iterator
begin() {
return _p.begin(); }
256 const_iterator
begin()
const {
return _p.begin(); }
259 iterator
end() {
return _p.end(); }
261 const_iterator
end()
const {
return _p.end(); }
264 reverse_iterator
rbegin() {
return _p.rbegin(); }
266 const_reverse_iterator
rbegin()
const {
return _p.rbegin(); }
269 reverse_iterator
rend() {
return _p.rend(); }
271 const_reverse_iterator
rend()
const {
return _p.rend(); }
276 void resize(
size_t sz ) {
283 T
get(
size_t i )
const {
293 void set(
size_t i, T val ) {
301 const container_type&
p()
const {
return _p; }
305 container_type&
p() {
return _p; }
311 size_t size()
const {
return _p.size(); }
324 for( const_iterator it =
begin(); it !=
end(); it++ )
338 template<
typename unOp> T
accumulateMax( T init, unOp op,
bool minimize )
const {
341 for( const_iterator it =
begin(); it !=
end(); it++ )
342 t = std::min( t, op(*it) );
344 for( const_iterator it =
begin(); it !=
end(); it++ )
345 t = std::max( t, op(*it) );
370 bool foundnan =
false;
371 for( const_iterator x = _p.begin(); x != _p.end(); x++ )
381 return (std::find_if( _p.begin(), _p.end(), std::bind2nd( std::less<T>(), (T)0 ) ) != _p.end());
388 for(
size_t i = 1; i <
size(); i++ ) {
394 return std::make_pair( arg, max );
401 for(
size_t i = 0; i <
size(); i++ ) {
406 return(
size() - 1 );
426 std::stringstream ss;
434 template<
typename unaryOp> this_type
pwUnaryTr( unaryOp op )
const {
438 std::transform( _p.begin(), _p.end(), back_inserter( r._p ), op );
454 this_type
log(
bool zero=
false)
const {
476 if( norm == dai::NORMPROB )
478 else if( norm == dai::NORMLINF )
481 DAI_THROW(NOT_NORMALIZABLE);
484 return pwUnaryTr( std::bind2nd( std::divides<T>(), Z ) );
490 template<
typename unaryOp> this_type&
pwUnaryOp( unaryOp op ) {
492 std::transform( _p.begin(), _p.end(), _p.begin(), op );
498 std::generate( _p.begin(), _p.end(),
rnd_uniform );
529 if( norm == dai::NORMPROB )
531 else if( norm == dai::NORMLINF )
534 DAI_THROW(NOT_NORMALIZABLE);
543 this_type&
fill( T x ) {
545 std::fill( _p.begin(), _p.end(), x );
552 return pwUnaryOp( std::bind2nd( std::plus<T>(), x ) );
560 return pwUnaryOp( std::bind2nd( std::minus<T>(), x ) );
568 return pwUnaryOp( std::bind2nd( std::multiplies<T>(), x ) );
592 this_type
operator+ (T x)
const {
return pwUnaryTr( std::bind2nd( std::plus<T>(), x ) ); }
615 template<
typename binaryOp> this_type&
pwBinaryOp(
const this_type &q, binaryOp op ) {
617 std::transform( _p.begin(), _p.end(), q.
_p.begin(), _p.begin(), op );
661 template<
typename binaryOp> this_type
pwBinaryTr(
const this_type &q, binaryOp op )
const {
665 std::transform( _p.begin(), _p.end(), q.
_p.begin(), back_inserter( r._p ), op );
705 template<
typename binOp1,
typename binOp2> T
innerProduct(
const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2 )
const {
707 return std::inner_product(
begin(),
end(), q.
begin(), init, binaryOp1, binaryOp2 );
729 DAI_THROW(UNKNOWN_ENUM_VALUE);
738 template<
typename T> std::ostream& operator<< (std::ostream& os, const TProb<T>& p) {
740 for(
size_t i = 0; i < p.size(); i++ )
741 os << ((i != 0) ?
", " :
"") << p.get(i);
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