+/// Returns distance of p and q (sizes should be identical), measured using distance measure dt
+/** \relates TProb
+ */
+template<typename T> Real dist( const TProb<T> &p, const TProb<T> &q, typename TProb<T>::DistType dt ) {
+#ifdef DAI_DEBUG
+ assert( p.size() == q.size() );
+#endif
+ Real result = 0.0;
+ switch( dt ) {
+ case TProb<T>::DISTL1:
+ for( size_t i = 0; i < p.size(); i++ )
+ result += fabs((Real)p[i] - (Real)q[i]);
+ break;
+
+ case TProb<T>::DISTLINF:
+ for( size_t i = 0; i < p.size(); i++ ) {
+ Real z = fabs((Real)p[i] - (Real)q[i]);
+ if( z > result )
+ result = z;
+ }
+ break;
+
+ case TProb<T>::DISTTV:
+ for( size_t i = 0; i < p.size(); i++ )
+ result += fabs((Real)p[i] - (Real)q[i]);
+ result *= 0.5;
+ break;
+
+ case TProb<T>::DISTKL:
+ for( size_t i = 0; i < p.size(); i++ ) {
+ if( p[i] != 0.0 )
+ result += p[i] * (std::log(p[i]) - std::log(q[i]));
+ }
+ }
+ return result;
+}
+
+
+/// Writes a TProb<T> to an output stream
+/** \relates TProb
+ */
+template<typename T> std::ostream& operator<< (std::ostream& os, const TProb<T>& P) {
+ os << "[";
+ std::copy( P.p().begin(), P.p().end(), std::ostream_iterator<T>(os, " ") );
+ os << "]";
+ return os;
+}
+
+
+/// Returns the TProb<T> containing the pointwise minimum of a and b (which should have equal size)
+/** \relates TProb
+ */
+template<typename T> TProb<T> min( const TProb<T> &a, const TProb<T> &b ) {
+ assert( a.size() == b.size() );
+ TProb<T> result( a.size() );
+ for( size_t i = 0; i < a.size(); i++ )
+ if( a[i] < b[i] )
+ result[i] = a[i];
+ else
+ result[i] = b[i];
+ return result;
+}
+
+
+/// Returns the TProb<T> containing the pointwise maximum of a and b (which should have equal size)
+/** \relates TProb
+ */
+template<typename T> TProb<T> max( const TProb<T> &a, const TProb<T> &b ) {
+ assert( a.size() == b.size() );
+ TProb<T> result( a.size() );
+ for( size_t i = 0; i < a.size(); i++ )
+ if( a[i] > b[i] )
+ result[i] = a[i];
+ else
+ result[i] = b[i];
+ return result;
+}
+
+
+/// Represents a vector with entries of type Real.
+typedef TProb<Real> Prob;
+
+