1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 */
12 /// \file
13 /// \brief Defines TProb<> and Prob classes which represent (probability) vectors (e.g., probability distributions of discrete random variables)
16 #ifndef __defined_libdai_prob_h
17 #define __defined_libdai_prob_h
20 #include <cmath>
21 #include <vector>
22 #include <ostream>
23 #include <algorithm>
24 #include <numeric>
25 #include <functional>
26 #include <dai/util.h>
27 #include <dai/exceptions.h>
30 namespace dai {
33 /// Function object that returns the value itself
34 template<typename T> struct fo_id : public std::unary_function<T, T> {
35 /// Returns \a x
36 T operator()( const T &x ) const {
37 return x;
38 }
39 };
42 /// Function object that takes the absolute value
43 template<typename T> struct fo_abs : public std::unary_function<T, T> {
44 /// Returns abs(\a x)
45 T operator()( const T &x ) const {
46 if( x < (T)0 )
47 return -x;
48 else
49 return x;
50 }
51 };
54 /// Function object that takes the exponent
55 template<typename T> struct fo_exp : public std::unary_function<T, T> {
56 /// Returns exp(\a x)
57 T operator()( const T &x ) const {
58 return exp( x );
59 }
60 };
63 /// Function object that takes the logarithm
64 template<typename T> struct fo_log : public std::unary_function<T, T> {
65 /// Returns log(\a x)
66 T operator()( const T &x ) const {
67 return log( x );
68 }
69 };
72 /// Function object that takes the logarithm, except that log(0) is defined to be 0
73 template<typename T> struct fo_log0 : public std::unary_function<T, T> {
74 /// Returns (\a x == 0 ? 0 : log(\a x))
75 T operator()( const T &x ) const {
76 if( x )
77 return log( x );
78 else
79 return 0;
80 }
81 };
84 /// Function object that takes the inverse
85 template<typename T> struct fo_inv : public std::unary_function<T, T> {
86 /// Returns 1 / \a x
87 T operator()( const T &x ) const {
88 return 1 / x;
89 }
90 };
93 /// Function object that takes the inverse, except that 1/0 is defined to be 0
94 template<typename T> struct fo_inv0 : public std::unary_function<T, T> {
95 /// Returns (\a x == 0 ? 0 : (1 / \a x))
96 T operator()( const T &x ) const {
97 if( x )
98 return 1 / x;
99 else
100 return 0;
101 }
102 };
105 /// Function object that returns p*log0(p)
106 template<typename T> struct fo_plog0p : public std::unary_function<T, T> {
107 /// Returns \a p * log0(\a p)
108 T operator()( const T &p ) const {
109 return p * dai::log0(p);
110 }
111 };
114 /// Function object similar to std::divides(), but different in that dividing by zero results in zero
115 template<typename T> struct fo_divides0 : public std::binary_function<T, T, T> {
116 /// Returns (\a y == 0 ? 0 : (\a x / \a y))
117 T operator()( const T &x, const T &y ) const {
118 if( y == (T)0 )
119 return (T)0;
120 else
121 return x / y;
122 }
123 };
126 /// Function object useful for calculating the KL distance
127 template<typename T> struct fo_KL : public std::binary_function<T, T, T> {
128 /// Returns (\a p == 0 ? 0 : (\a p * (log(\a p) - log(\a q))))
129 T operator()( const T &p, const T &q ) const {
130 if( p == (T)0 )
131 return (T)0;
132 else
133 return p * (log(p) - log(q));
134 }
135 };
138 /// Function object useful for calculating the Hellinger distance
139 template<typename T> struct fo_Hellinger : public std::binary_function<T, T, T> {
140 /// Returns (sqrt(\a p) - sqrt(\a q))^2
141 T operator()( const T &p, const T &q ) const {
142 T x = sqrt(p) - sqrt(q);
143 return x * x;
144 }
145 };
148 /// Function object that returns x to the power y
149 template<typename T> struct fo_pow : public std::binary_function<T, T, T> {
150 /// Returns (\a x ^ \a y)
151 T operator()( const T &x, const T &y ) const {
152 if( y != 1 )
153 return std::pow( x, y );
154 else
155 return x;
156 }
157 };
160 /// Function object that returns the maximum of two values
161 template<typename T> struct fo_max : public std::binary_function<T, T, T> {
162 /// Returns (\a x > y ? x : y)
163 T operator()( const T &x, const T &y ) const {
164 return (x > y) ? x : y;
165 }
166 };
169 /// Function object that returns the minimum of two values
170 template<typename T> struct fo_min : public std::binary_function<T, T, T> {
171 /// Returns (\a x > y ? y : x)
172 T operator()( const T &x, const T &y ) const {
173 return (x > y) ? y : x;
174 }
175 };
178 /// Function object that returns the absolute difference of x and y
179 template<typename T> struct fo_absdiff : public std::binary_function<T, T, T> {
180 /// Returns abs( \a x - \a y )
181 T operator()( const T &x, const T &y ) const {
182 return dai::abs( x - y );
183 }
184 };
187 /// Represents a vector with entries of type \a T.
188 /** It is simply a <tt>std::vector</tt><<em>T</em>> with an interface designed for dealing with probability mass functions.
189 *
190 * It is mainly used for representing measures on a finite outcome space, for example, the probability
191 * distribution of a discrete random variable. However, entries are not necessarily non-negative; it is also used to
192 * represent logarithms of probability mass functions.
193 *
194 * \tparam T Should be a scalar that is castable from and to dai::Real and should support elementary arithmetic operations.
195 */
196 template <typename T>
197 class TProb {
198 public:
199 /// Type of data structure used for storing the values
200 typedef std::vector<T> container_type;
202 private:
203 /// The data structure that stores the values
204 container_type _p;
206 public:
207 /// Enumerates different ways of normalizing a probability measure.
208 /**
209 * - NORMPROB means that the sum of all entries should be 1;
210 * - NORMLINF means that the maximum absolute value of all entries should be 1.
211 */
212 typedef enum { NORMPROB, NORMLINF } NormType;
213 /// Enumerates different distance measures between probability measures.
214 /**
215 * - DISTL1 is the \f$\ell_1\f$ distance (sum of absolute values of pointwise difference);
216 * - DISTLINF is the \f$\ell_\infty\f$ distance (maximum absolute value of pointwise difference);
217 * - DISTTV is the total variation distance (half of the \f$\ell_1\f$ distance);
218 * - DISTKL is the Kullback-Leibler distance (\f$\sum_i p_i (\log p_i - \log q_i)\f$).
219 * - DISTHEL is the Hellinger distance (\f$\frac{1}{2}\sum_i (\sqrt{p_i}-\sqrt{q_i})^2\f$).
220 */
221 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } DistType;
223 /// \name Constructors and destructors
224 //@{
225 /// Default constructor (constructs empty vector)
226 TProb() : _p() {}
228 /// Construct uniform probability distribution over \a n outcomes (i.e., a vector of length \a n with each entry set to \f$1/n\f$)
229 explicit TProb( size_t n ) : _p( n, (T)1 / n ) {}
231 /// Construct vector of length \a n with each entry set to \a p
232 explicit TProb( size_t n, T p ) : _p( n, p ) {}
234 /// Construct vector from a range
235 /** \tparam TIterator Iterates over instances that can be cast to \a T
236 * \param begin Points to first instance to be added.
237 * \param end Points just beyond last instance to be added.
238 * \param sizeHint For efficiency, the number of entries can be speficied by \a sizeHint.
239 */
240 template <typename TIterator>
241 TProb( TIterator begin, TIterator end, size_t sizeHint=0 ) : _p() {
242 _p.reserve( sizeHint );
243 _p.insert( _p.begin(), begin, end );
244 }
246 /// Construct vector from another vector
247 /** \tparam S type of elements in \a v (should be castable to type \a T)
248 * \param v vector used for initialization.
249 */
250 template <typename S>
251 TProb( const std::vector<S> &v ) : _p() {
252 _p.reserve( v.size() );
253 _p.insert( _p.begin(), v.begin(), v.end() );
254 }
255 //@}
257 /// Constant iterator over the elements
258 typedef typename container_type::const_iterator const_iterator;
259 /// Iterator over the elements
260 typedef typename container_type::iterator iterator;
261 /// Constant reverse iterator over the elements
262 typedef typename container_type::const_reverse_iterator const_reverse_iterator;
263 /// Reverse iterator over the elements
264 typedef typename container_type::reverse_iterator reverse_iterator;
266 /// \name Iterator interface
267 //@{
268 /// Returns iterator that points to the first element
269 iterator begin() { return _p.begin(); }
270 /// Returns constant iterator that points to the first element
271 const_iterator begin() const { return _p.begin(); }
273 /// Returns iterator that points beyond the last element
274 iterator end() { return _p.end(); }
275 /// Returns constant iterator that points beyond the last element
276 const_iterator end() const { return _p.end(); }
278 /// Returns reverse iterator that points to the last element
279 reverse_iterator rbegin() { return _p.rbegin(); }
280 /// Returns constant reverse iterator that points to the last element
281 const_reverse_iterator rbegin() const { return _p.rbegin(); }
283 /// Returns reverse iterator that points beyond the first element
284 reverse_iterator rend() { return _p.rend(); }
285 /// Returns constant reverse iterator that points beyond the first element
286 const_reverse_iterator rend() const { return _p.rend(); }
287 //@}
289 /// \name Queries
290 //@{
291 /// Gets \a i 'th entry
292 T get( size_t i ) const {
293 #ifdef DAI_DEBUG
294 return _p.at(i);
295 #else
296 return _p[i];
297 #endif
298 }
300 /// Sets \a i 'th entry to \a val
301 void set( size_t i, T val ) {
302 DAI_DEBASSERT( i < _p.size() );
303 _p[i] = val;
304 }
306 /// Returns a const reference to the wrapped vector
307 const container_type& p() const { return _p; }
309 /// Returns a reference to the wrapped vector
310 container_type& p() { return _p; }
312 /// Returns a copy of the \a i 'th entry
313 T operator[]( size_t i ) const { return get(i); }
315 /// Returns reference to the \a i 'th entry
317 */
318 T& operator[]( size_t i ) { return _p[i]; }
320 /// Returns length of the vector (i.e., the number of entries)
321 size_t size() const { return _p.size(); }
323 /// Accumulate over all values, similar to std::accumulate
324 /** The following calculation is done:
325 * \code
326 * T t = op2(init);
327 * for( const_iterator it = begin(); it != end(); it++ )
328 * t = op1( t, op2(*it) );
329 * return t;
330 * \endcode
331 */
332 template<typename binOp, typename unOp> T accumulate( T init, binOp op1, unOp op2 ) const {
333 T t = op2(init);
334 for( const_iterator it = begin(); it != end(); it++ )
335 t = op1( t, op2(*it) );
336 return t;
337 }
339 /// Returns the Shannon entropy of \c *this, \f$-\sum_i p_i \log p_i\f$
340 T entropy() const { return -accumulate( (T)0, std::plus<T>(), fo_plog0p<T>() ); }
342 /// Returns maximum value of all entries
343 T max() const { return accumulate( (T)(-INFINITY), fo_max<T>(), fo_id<T>() ); }
345 /// Returns minimum value of all entries
346 T min() const { return accumulate( (T)INFINITY, fo_min<T>(), fo_id<T>() ); }
348 /// Returns sum of all entries
349 T sum() const { return accumulate( (T)0, std::plus<T>(), fo_id<T>() ); }
351 /// Return sum of absolute value of all entries
352 T sumAbs() const { return accumulate( (T)0, std::plus<T>(), fo_abs<T>() ); }
354 /// Returns maximum absolute value of all entries
355 T maxAbs() const { return accumulate( (T)0, fo_max<T>(), fo_abs<T>() ); }
357 /// Returns \c true if one or more entries are NaN
358 bool hasNaNs() const {
359 bool foundnan = false;
360 for( const_iterator x = _p.begin(); x != _p.end(); x++ )
361 if( isnan( *x ) ) {
362 foundnan = true;
363 break;
364 }
365 return foundnan;
366 }
368 /// Returns \c true if one or more entries are negative
369 bool hasNegatives() const {
370 return (std::find_if( _p.begin(), _p.end(), std::bind2nd( std::less<T>(), (T)0 ) ) != _p.end());
371 }
373 /// Returns a pair consisting of the index of the maximum value and the maximum value itself
374 std::pair<size_t,T> argmax() const {
375 T max = _p[0];
376 size_t arg = 0;
377 for( size_t i = 1; i < size(); i++ ) {
378 if( _p[i] > max ) {
379 max = _p[i];
380 arg = i;
381 }
382 }
383 return std::make_pair( arg, max );
384 }
386 /// Returns a random index, according to the (normalized) distribution described by *this
387 size_t draw() {
388 Real x = rnd_uniform() * sum();
389 T s = 0;
390 for( size_t i = 0; i < size(); i++ ) {
391 s += get(i);
392 if( s > x )
393 return i;
394 }
395 return( size() - 1 );
396 }
398 /// Lexicographical comparison
399 /** \pre <tt>this->size() == q.size()</tt>
400 */
401 bool operator<( const TProb<T>& q ) const {
402 DAI_DEBASSERT( size() == q.size() );
403 return lexicographical_compare( begin(), end(), q.begin(), q.end() );
404 }
406 /// Comparison
407 bool operator==( const TProb<T>& q ) const {
408 if( size() != q.size() )
409 return false;
410 return p() == q.p();
411 }
412 //@}
414 /// \name Unary transformations
415 //@{
416 /// Returns the result of applying operation \a op pointwise on \c *this
417 template<typename unaryOp> TProb<T> pwUnaryTr( unaryOp op ) const {
418 TProb<T> r;
419 r._p.reserve( size() );
420 std::transform( _p.begin(), _p.end(), back_inserter( r._p ), op );
421 return r;
422 }
424 /// Returns negative of \c *this
425 TProb<T> operator- () const { return pwUnaryTr( std::negate<T>() ); }
427 /// Returns pointwise absolute value
428 TProb<T> abs() const { return pwUnaryTr( fo_abs<T>() ); }
430 /// Returns pointwise exponent
431 TProb<T> exp() const { return pwUnaryTr( fo_exp<T>() ); }
433 /// Returns pointwise logarithm
434 /** If \a zero == \c true, uses <tt>log(0)==0</tt>; otherwise, <tt>log(0)==-Inf</tt>.
435 */
436 TProb<T> log(bool zero=false) const {
437 if( zero )
438 return pwUnaryTr( fo_log0<T>() );
439 else
440 return pwUnaryTr( fo_log<T>() );
441 }
443 /// Returns pointwise inverse
444 /** If \a zero == \c true, uses <tt>1/0==0</tt>; otherwise, <tt>1/0==Inf</tt>.
445 */
446 TProb<T> inverse(bool zero=true) const {
447 if( zero )
448 return pwUnaryTr( fo_inv0<T>() );
449 else
450 return pwUnaryTr( fo_inv<T>() );
451 }
453 /// Returns normalized copy of \c *this, using the specified norm
454 /** \throw NOT_NORMALIZABLE if the norm is zero
455 */
456 TProb<T> normalized( NormType norm = NORMPROB ) const {
457 T Z = 0;
458 if( norm == NORMPROB )
459 Z = sum();
460 else if( norm == NORMLINF )
461 Z = maxAbs();
462 if( Z == (T)0 ) {
463 DAI_THROW(NOT_NORMALIZABLE);
464 return *this;
465 } else
466 return pwUnaryTr( std::bind2nd( std::divides<T>(), Z ) );
467 }
468 //@}
470 /// \name Unary operations
471 //@{
472 /// Applies unary operation \a op pointwise
473 template<typename unaryOp> TProb<T>& pwUnaryOp( unaryOp op ) {
474 std::transform( _p.begin(), _p.end(), _p.begin(), op );
475 return *this;
476 }
478 /// Draws all entries i.i.d. from a uniform distribution on [0,1)
479 TProb<T>& randomize() {
480 std::generate( _p.begin(), _p.end(), rnd_uniform );
481 return *this;
482 }
484 /// Sets all entries to \f$1/n\f$ where \a n is the length of the vector
485 TProb<T>& setUniform () {
486 fill( (T)1 / size() );
487 return *this;
488 }
490 /// Applies absolute value pointwise
491 const TProb<T>& takeAbs() { return pwUnaryOp( fo_abs<T>() ); }
493 /// Applies exponent pointwise
494 const TProb<T>& takeExp() { return pwUnaryOp( fo_exp<T>() ); }
496 /// Applies logarithm pointwise
497 /** If \a zero == \c true, uses <tt>log(0)==0</tt>; otherwise, <tt>log(0)==-Inf</tt>.
498 */
499 const TProb<T>& takeLog(bool zero=false) {
500 if( zero ) {
501 return pwUnaryOp( fo_log0<T>() );
502 } else
503 return pwUnaryOp( fo_log<T>() );
504 }
506 /// Normalizes vector using the specified norm
507 /** \throw NOT_NORMALIZABLE if the norm is zero
508 */
509 T normalize( NormType norm=NORMPROB ) {
510 T Z = 0;
511 if( norm == NORMPROB )
512 Z = sum();
513 else if( norm == NORMLINF )
514 Z = maxAbs();
515 if( Z == (T)0 )
516 DAI_THROW(NOT_NORMALIZABLE);
517 else
518 *this /= Z;
519 return Z;
520 }
521 //@}
523 /// \name Operations with scalars
524 //@{
525 /// Sets all entries to \a x
526 TProb<T> & fill( T x ) {
527 std::fill( _p.begin(), _p.end(), x );
528 return *this;
529 }
531 /// Adds scalar \a x to each entry
532 TProb<T>& operator+= (T x) {
533 if( x != 0 )
534 return pwUnaryOp( std::bind2nd( std::plus<T>(), x ) );
535 else
536 return *this;
537 }
539 /// Subtracts scalar \a x from each entry
540 TProb<T>& operator-= (T x) {
541 if( x != 0 )
542 return pwUnaryOp( std::bind2nd( std::minus<T>(), x ) );
543 else
544 return *this;
545 }
547 /// Multiplies each entry with scalar \a x
548 TProb<T>& operator*= (T x) {
549 if( x != 1 )
550 return pwUnaryOp( std::bind2nd( std::multiplies<T>(), x ) );
551 else
552 return *this;
553 }
555 /// Divides each entry by scalar \a x, where division by 0 yields 0
556 TProb<T>& operator/= (T x) {
557 if( x != 1 )
558 return pwUnaryOp( std::bind2nd( fo_divides0<T>(), x ) );
559 else
560 return *this;
561 }
563 /// Raises entries to the power \a x
564 TProb<T>& operator^= (T x) {
565 if( x != (T)1 )
566 return pwUnaryOp( std::bind2nd( fo_pow<T>(), x) );
567 else
568 return *this;
569 }
570 //@}
572 /// \name Transformations with scalars
573 //@{
574 /// Returns sum of \c *this and scalar \a x
575 TProb<T> operator+ (T x) const { return pwUnaryTr( std::bind2nd( std::plus<T>(), x ) ); }
577 /// Returns difference of \c *this and scalar \a x
578 TProb<T> operator- (T x) const { return pwUnaryTr( std::bind2nd( std::minus<T>(), x ) ); }
580 /// Returns product of \c *this with scalar \a x
581 TProb<T> operator* (T x) const { return pwUnaryTr( std::bind2nd( std::multiplies<T>(), x ) ); }
583 /// Returns quotient of \c *this and scalar \a x, where division by 0 yields 0
584 TProb<T> operator/ (T x) const { return pwUnaryTr( std::bind2nd( fo_divides0<T>(), x ) ); }
586 /// Returns \c *this raised to the power \a x
587 TProb<T> operator^ (T x) const { return pwUnaryTr( std::bind2nd( fo_pow<T>(), x ) ); }
588 //@}
590 /// \name Operations with other equally-sized vectors
591 //@{
592 /// Applies binary operation pointwise on two vectors
593 /** \tparam binaryOp Type of function object that accepts two arguments of type \a T and outputs a type \a T
594 * \param q Right operand
595 * \param op Operation of type \a binaryOp
596 */
597 template<typename binaryOp> TProb<T>& pwBinaryOp( const TProb<T> &q, binaryOp op ) {
598 DAI_DEBASSERT( size() == q.size() );
599 std::transform( _p.begin(), _p.end(), q._p.begin(), _p.begin(), op );
600 return *this;
601 }
603 /// Pointwise addition with \a q
604 /** \pre <tt>this->size() == q.size()</tt>
605 */
606 TProb<T>& operator+= (const TProb<T> & q) { return pwBinaryOp( q, std::plus<T>() ); }
608 /// Pointwise subtraction of \a q
609 /** \pre <tt>this->size() == q.size()</tt>
610 */
611 TProb<T>& operator-= (const TProb<T> & q) { return pwBinaryOp( q, std::minus<T>() ); }
613 /// Pointwise multiplication with \a q
614 /** \pre <tt>this->size() == q.size()</tt>
615 */
616 TProb<T>& operator*= (const TProb<T> & q) { return pwBinaryOp( q, std::multiplies<T>() ); }
618 /// Pointwise division by \a q, where division by 0 yields 0
619 /** \pre <tt>this->size() == q.size()</tt>
620 * \see divide(const TProb<T> &)
621 */
622 TProb<T>& operator/= (const TProb<T> & q) { return pwBinaryOp( q, fo_divides0<T>() ); }
624 /// Pointwise division by \a q, where division by 0 yields +Inf
625 /** \pre <tt>this->size() == q.size()</tt>
626 * \see operator/=(const TProb<T> &)
627 */
628 TProb<T>& divide (const TProb<T> & q) { return pwBinaryOp( q, std::divides<T>() ); }
630 /// Pointwise power
631 /** \pre <tt>this->size() == q.size()</tt>
632 */
633 TProb<T>& operator^= (const TProb<T> & q) { return pwBinaryOp( q, fo_pow<T>() ); }
634 //@}
636 /// \name Transformations with other equally-sized vectors
637 //@{
638 /// Returns the result of applying binary operation \a op pointwise on \c *this and \a q
639 /** \tparam binaryOp Type of function object that accepts two arguments of type \a T and outputs a type \a T
640 * \param q Right operand
641 * \param op Operation of type \a binaryOp
642 */
643 template<typename binaryOp> TProb<T> pwBinaryTr( const TProb<T> &q, binaryOp op ) const {
644 DAI_DEBASSERT( size() == q.size() );
645 TProb<T> r;
646 r._p.reserve( size() );
647 std::transform( _p.begin(), _p.end(), q._p.begin(), back_inserter( r._p ), op );
648 return r;
649 }
651 /// Returns sum of \c *this and \a q
652 /** \pre <tt>this->size() == q.size()</tt>
653 */
654 TProb<T> operator+ ( const TProb<T>& q ) const { return pwBinaryTr( q, std::plus<T>() ); }
656 /// Return \c *this minus \a q
657 /** \pre <tt>this->size() == q.size()</tt>
658 */
659 TProb<T> operator- ( const TProb<T>& q ) const { return pwBinaryTr( q, std::minus<T>() ); }
661 /// Return product of \c *this with \a q
662 /** \pre <tt>this->size() == q.size()</tt>
663 */
664 TProb<T> operator* ( const TProb<T> &q ) const { return pwBinaryTr( q, std::multiplies<T>() ); }
666 /// Returns quotient of \c *this with \a q, where division by 0 yields 0
667 /** \pre <tt>this->size() == q.size()</tt>
668 * \see divided_by(const TProb<T> &)
669 */
670 TProb<T> operator/ ( const TProb<T> &q ) const { return pwBinaryTr( q, fo_divides0<T>() ); }
672 /// Pointwise division by \a q, where division by 0 yields +Inf
673 /** \pre <tt>this->size() == q.size()</tt>
674 * \see operator/(const TProb<T> &)
675 */
676 TProb<T> divided_by( const TProb<T> &q ) const { return pwBinaryTr( q, std::divides<T>() ); }
678 /// Returns \c *this to the power \a q
679 /** \pre <tt>this->size() == q.size()</tt>
680 */
681 TProb<T> operator^ ( const TProb<T> &q ) const { return pwBinaryTr( q, fo_pow<T>() ); }
682 //@}
684 /// Performs a generalized inner product, similar to std::inner_product
685 /** \pre <tt>this->size() == q.size()</tt>
686 */
687 template<typename binOp1, typename binOp2> T innerProduct( const TProb<T> &q, T init, binOp1 binaryOp1, binOp2 binaryOp2 ) const {
688 DAI_DEBASSERT( size() == q.size() );
689 return std::inner_product( begin(), end(), q.begin(), init, binaryOp1, binaryOp2 );
690 }
691 };
694 /// Returns distance between \a p and \a q, measured using distance measure \a dt
695 /** \relates TProb
696 * \pre <tt>this->size() == q.size()</tt>
697 */
698 template<typename T> T dist( const TProb<T> &p, const TProb<T> &q, typename TProb<T>::DistType dt ) {
699 switch( dt ) {
700 case TProb<T>::DISTL1:
701 return p.innerProduct( q, (T)0, std::plus<T>(), fo_absdiff<T>() );
702 case TProb<T>::DISTLINF:
703 return p.innerProduct( q, (T)0, fo_max<T>(), fo_absdiff<T>() );
704 case TProb<T>::DISTTV:
705 return p.innerProduct( q, (T)0, std::plus<T>(), fo_absdiff<T>() ) / 2;
706 case TProb<T>::DISTKL:
707 return p.innerProduct( q, (T)0, std::plus<T>(), fo_KL<T>() );
708 case TProb<T>::DISTHEL:
709 return p.innerProduct( q, (T)0, std::plus<T>(), fo_Hellinger<T>() ) / 2;
710 default:
711 DAI_THROW(UNKNOWN_ENUM_VALUE);
712 return INFINITY;
713 }
714 }
717 /// Writes a TProb<T> to an output stream
718 /** \relates TProb
719 */
720 template<typename T> std::ostream& operator<< (std::ostream& os, const TProb<T>& p) {
721 os << "(";
722 for( typename TProb<T>::const_iterator it = p.begin(); it != p.end(); it++ )
723 os << (it != p.begin() ? ", " : "") << *it;
724 os << ")";
725 return os;
726 }
729 /// Returns the pointwise minimum of \a a and \a b
730 /** \relates TProb
731 * \pre <tt>this->size() == q.size()</tt>
732 */
733 template<typename T> TProb<T> min( const TProb<T> &a, const TProb<T> &b ) {
734 return a.pwBinaryTr( b, fo_min<T>() );
735 }
738 /// Returns the pointwise maximum of \a a and \a b
739 /** \relates TProb
740 * \pre <tt>this->size() == q.size()</tt>
741 */
742 template<typename T> TProb<T> max( const TProb<T> &a, const TProb<T> &b ) {
743 return a.pwBinaryTr( b, fo_max<T>() );
744 }
747 /// Represents a vector with entries of type dai::Real.
748 typedef TProb<Real> Prob;
751 } // end of namespace dai
754 #endif