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 ) {}
225 template <
typename TIterator>
227 _p.reserve( sizeHint );
235 template <
typename S>
237 _p.reserve( v.size() );
238 _p.insert(
_p.begin(), v.begin(), v.end() );
245 typedef typename container_type::iterator
iterator;
276 void resize(
size_t sz ) {
284 T
get(
size_t i )
const {
293 void set(
size_t i, T val ) {
311 size_t size()
const {
return _p.size(); }
338 template<
typename unOp> T
accumulateMax( T init, unOp op,
bool minimize )
const {
342 t = std::min( t, op(*it) );
345 t = std::max( t, op(*it) );
370 bool foundnan =
false;
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 );
431 std::transform(
_p.begin(),
_p.end(), back_inserter( r.
_p ), op );
469 if( norm == dai::NORMPROB )
471 else if( norm == dai::NORMLINF )
474 DAI_THROW(NOT_NORMALIZABLE);
477 return pwUnaryTr( std::bind2nd( std::divides<T>(), Z ) );
485 std::transform(
_p.begin(),
_p.end(),
_p.begin(), op );
522 if( norm == dai::NORMPROB )
524 else if( norm == dai::NORMLINF )
527 DAI_THROW(NOT_NORMALIZABLE);
545 return pwUnaryOp( std::bind2nd( std::plus<T>(), x ) );
553 return pwUnaryOp( std::bind2nd( std::minus<T>(), x ) );
561 return pwUnaryOp( std::bind2nd( std::multiplies<T>(), x ) );
610 std::transform(
_p.begin(),
_p.end(), q.
_p.begin(),
_p.begin(), op );
658 std::transform(
_p.begin(),
_p.end(), q.
_p.begin(), back_inserter( r._p ), op );
698 template<
typename binOp1,
typename binOp2> T
innerProduct(
const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2 )
const {
700 return std::inner_product(
begin(),
end(), q.
begin(), init, binaryOp1, binaryOp2 );
722 DAI_THROW(UNKNOWN_ENUM_VALUE);
731 template<
typename T> std::ostream& operator<< (std::ostream& os, const TProb<T>& p) {
733 for(
size_t i = 0; i < p.size(); i++ )
734 os << ((i != 0) ?
", " :
"") << p.get(i);