16 #ifndef __defined_libdai_probsp_h
17 #define __defined_libdai_probsp_h
43 template <
typename T,
typename spvector_type>
74 template <
typename TIterator>
89 typedef typename container_type::iterator
iterator;
120 void resize(
size_t sz ) {
128 T
get(
size_t i )
const {
return _p[i]; }
131 void set(
size_t i, T val ) {
_p.set( i, val ); }
146 size_t size()
const {
return _p.size(); }
186 template<
typename unOp> T
accumulateMax( T init, unOp op,
bool minimize )
const {
190 t = std::min( t, op(it->second) );
192 t = std::min( t, op(
def()) );
195 t = std::max( t, op(it->second) );
197 t = std::max( t, op(
def()) );
225 bool foundnan =
false;
227 if(
isnan( it->second ) ) {
240 bool foundnegative =
false;
242 if( it->second < 0 ) {
243 foundnegative =
true;
246 return foundnegative;
258 }
else if(
nrDef() > 0 ) {
259 max =
begin()->second;
260 arg =
begin()->first;
264 if( it->second > max ) {
277 max =
begin()->second;
278 arg =
begin()->first;
280 if( it->second > max ) {
285 return std::make_pair( arg, max );
292 for(
size_t i = 0; i <
size(); i++ ) {
297 return(
size() - 1 );
305 for(
size_t i = 0; i <
size(); i++ ) {
332 T new_val = op( it->second );
333 if( new_val != r.
def() )
334 r.
_p.push_back( it->first, new_val );
373 if( norm == dai::NORMPROB )
375 else if( norm == dai::NORMLINF )
378 DAI_THROW(NOT_NORMALIZABLE);
381 return pwUnaryTr( std::bind2nd( std::divides<T>(), Z ) );
391 T new_val = op( it->second );
392 if( new_val !=
def() ) {
393 it->second = new_val;
404 for(
size_t i = 0; i <
size(); i++ )
437 if( norm == dai::NORMPROB )
439 else if( norm == dai::NORMLINF )
442 DAI_THROW(NOT_NORMALIZABLE);
461 return pwUnaryOp( std::bind2nd( std::plus<T>(), x ) );
469 return pwUnaryOp( std::bind2nd( std::minus<T>(), x ) );
477 return pwUnaryOp( std::bind2nd( std::multiplies<T>(), x ) );
529 T new_val = op( it->second, q[it->first] );
530 set( it->first, new_val );
533 T new_val = op( p[it->first], it->second );
534 set( it->first, new_val );
583 result._p.resize(
size() );
585 T new_val = op( it->second, q[it->first] );
586 if( new_val != result.def() )
587 result._p.push_back(it->first, new_val);
590 T new_val = op(
get(it->first), it->second );
591 if( new_val != result.def() )
592 result.set( it->first, new_val );
633 template<
typename binOp1,
typename binOp2> T
innerProduct(
const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2 )
const {
637 for(
size_t i = 0; i <
size(); i++ )
638 result = binaryOp1( result, binaryOp2(
get(i), q.
get(i) ) );
661 DAI_THROW(UNKNOWN_ENUM_VALUE);
670 template<
typename T,
typename spvector_type> std::ostream& operator<< (std::ostream& os, const TProbSp<T,spvector_type>& p) {