13 #ifndef __defined_libdai_util_h
14 #define __defined_libdai_util_h
22 #include <boost/foreach.hpp>
23 #include <boost/functional/hash.hpp>
24 #include <boost/lexical_cast.hpp>
37 #include <boost/tr1/unordered_map.hpp>
39 #include <boost/tr1/unordered_map.hpp>
41 #include <boost/tr1/unordered_map.hpp>
43 #include <tr1/unordered_map>
48 #define bforeach BOOST_FOREACH
57 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
58 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
61 #define DAI_PV(x) do {} while(0)
62 #define DAI_DMSG(str) do {} while(0)
66 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
70 double atanh(
double x );
74 double log1p(
double x );
77 #define INFINITY (std::numeric_limits<Real>::infinity())
80 #define NAN (std::numeric_limits<Real>::quiet_NaN())
85 #pragma warning( disable : 4996 )
87 #if (_MSC_VER >= 1600)
88 #define __STDC_UTF_16__
105 DAI_ASSERT( N <= (BigInt)std::numeric_limits<size_t>::max() );
110 bool isnan( Real x );
113 inline Real
log( Real x ) {
123 inline Real
exp( Real x ) {
132 inline Real
pow( Real x, Real y ) {
134 if( x == 0.0 && y < 0.0 )
145 template <
typename T,
typename U,
typename H = boost::hash<T> >
146 class hash_map :
public std::tr1::unordered_map<T,U,H> {};
155 inline T
abs(
const T &t ) {
156 return (t < 0) ? (-t) : t;
170 int rnd_int(
int min,
int max );
181 return boost::lexical_cast<std::string>(x);
188 return boost::lexical_cast<T>(x);
194 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
196 for(
typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
197 os << (it != x.begin() ?
", " :
"") << *it;
204 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
206 for(
typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
207 os << (it != x.begin() ?
", " :
"") << *it;
213 template<
class T1,
class T2>
214 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
216 for(
typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
217 os << (it != x.begin() ?
", " :
"") << it->first <<
"->" << it->second;
223 template<
class T1,
class T2>
224 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
225 os <<
"(" << x.first <<
", " << x.second <<
")";
231 std::vector<T>
concat(
const std::vector<T>& u,
const std::vector<T>& v ) {
233 w.reserve( u.size() + v.size() );
234 for(
size_t i = 0; i < u.size(); i++ )
236 for(
size_t i = 0; i < v.size(); i++ )
247 std::vector<std::string>
tokenizeString(
const std::string& s,
bool singleDelim,
const std::string& delim=
"\t\n" );
265 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL }
ProbDistType;
size_t BigInt_size_t(const BigInt &N)
Safe down-cast of big integer to size_t.
Definition: util.h:104
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
ProbDistType
Enumerates different distance measures between probability measures.
Definition: util.h:265
Real log(Real x)
Returns logarithm of x.
Definition: util.h:113
Real rnd_stdnormal()
Returns a real number from a standard-normal distribution.
Definition: util.cpp:91
std::vector< std::string > tokenizeString(const std::string &s, bool singleDelim, const std::string &delim)
Split a string into tokens delimited by one of the characters in delim.
Definition: util.cpp:99
int rnd_int(int min, int max)
Returns a random integer in interval [min, max].
Definition: util.cpp:95
std::string toString(const T &x)
Converts a variable of type T to a std::string by using a boost::lexical_cast.
Definition: util.h:180
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
T abs(const T &t)
Returns absolute value of t.
Definition: util.h:155
void rnd_seed(size_t seed)
Sets the random seed.
Definition: util.cpp:82
mpz_class BigInt
Arbitrary precision integer number.
Definition: util.h:101
bool isnan(Real x)
Returns true if argument is NAN (Not A Number)
Definition: util.cpp:44
int rnd(int n)
Returns a random integer in the half-open interval [0, n)
Definition: util.h:173
Defines the Exception class and macros for throwing exceptions and doing assertions.
T fromString(const std::string &x)
Converts a variable of type std::string to T by using a boost::lexical_cast.
Definition: util.h:187
Real log0(Real x)
Returns logarithm of x, or 0 if x == 0.
Definition: util.h:118
std::vector< T > concat(const std::vector< T > &u, const std::vector< T > &v)
Concatenates two vectors.
Definition: util.h:231
double toc()
Returns wall clock time in seconds.
Definition: util.cpp:50
hash_map is an alias for std::tr1::unordered_map.
Definition: util.h:146
Namespace for libDAI.
Definition: alldai.cpp:16
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
Real rnd_uniform()
Returns a real number, distributed uniformly on [0,1)
Definition: util.cpp:87
Real exp(Real x)
Returns exponent of x.
Definition: util.h:123