00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00014
00015
00016 #ifndef __defined_libdai_util_h
00017 #define __defined_libdai_util_h
00018
00019
00020 #include <string>
00021 #include <vector>
00022 #include <set>
00023 #include <map>
00024 #include <iostream>
00025 #include <boost/foreach.hpp>
00026 #include <boost/functional/hash.hpp>
00027 #include <boost/lexical_cast.hpp>
00028 #include <algorithm>
00029 #include <cerrno>
00030
00031 #include <dai/exceptions.h>
00032
00033
00034 #if defined(WINDOWS)
00035 #include <boost/tr1/unordered_map.hpp>
00036 #elif defined(CYGWIN)
00037 #include <boost/tr1/unordered_map.hpp>
00038 #elif defined(MACOSX)
00039 #include <boost/tr1/unordered_map.hpp>
00040 #else
00041 #include <tr1/unordered_map>
00042 #endif
00043
00044
00046 #define foreach BOOST_FOREACH
00047
00048 #ifdef DAI_DEBUG
00049
00050
00055 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
00056
00057 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
00058 #else
00059 #define DAI_PV(x) do {} while(0)
00060 #define DAI_DMSG(str) do {} while(0)
00061 #endif
00062
00064 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
00065
00066
00067 #ifdef CYGWIN
00068
00069 bool isnan( double x );
00070 #endif
00071
00072
00073 #ifdef WINDOWS
00074
00075 bool isnan( double x );
00076
00078 double atanh( double x );
00079
00081 double log1p( double x );
00082
00084 #define INFINITY (std::numeric_limits<Real>::infinity())
00085 #endif
00086
00087
00088 namespace dai {
00089
00090
00092 typedef double Real;
00093
00095 inline Real log( Real x ) {
00096 return std::log(x);
00097 }
00098
00100 inline Real log0( Real x ) {
00101 return x ? std::log(x) : 0;
00102 }
00103
00105 inline Real exp( Real x ) {
00106 return std::exp(x);
00107 }
00108
00110 inline Real pow( Real x, Real y ) {
00111 errno = 0;
00112 Real result = std::pow(x, y);
00113 DAI_DEBASSERT( errno == 0 );
00114 return result;
00115 }
00116
00117
00119
00121 template <typename T, typename U, typename H = boost::hash<T> >
00122 class hash_map : public std::tr1::unordered_map<T,U,H> {};
00123
00124
00126 double toc();
00127
00128
00130 template<class T>
00131 inline T abs( const T &t ) {
00132 return (t < 0) ? (-t) : t;
00133 }
00134
00135
00137 void rnd_seed( size_t seed );
00138
00140 Real rnd_uniform();
00141
00143 Real rnd_stdnormal();
00144
00146 int rnd_int( int min, int max );
00147
00149 inline int rnd( int n ) {
00150 return rnd_int( 0, n-1 );
00151 }
00152
00153
00155 template<class T>
00156 std::string toString( const T& x ) {
00157 return boost::lexical_cast<std::string>(x);
00158 }
00159
00160
00162 template<class T>
00163 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
00164 os << "(";
00165 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00166 os << (it != x.begin() ? ", " : "") << *it;
00167 os << ")";
00168 return os;
00169 }
00170
00172 template<class T>
00173 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
00174 os << "{";
00175 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00176 os << (it != x.begin() ? ", " : "") << *it;
00177 os << "}";
00178 return os;
00179 }
00180
00182 template<class T1, class T2>
00183 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
00184 os << "{";
00185 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
00186 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
00187 os << "}";
00188 return os;
00189 }
00190
00192 template<class T1, class T2>
00193 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
00194 os << "(" << x.first << ", " << x.second << ")";
00195 return os;
00196 }
00197
00199 template<class T>
00200 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
00201 std::vector<T> w;
00202 w.reserve( u.size() + v.size() );
00203 for( size_t i = 0; i < u.size(); i++ )
00204 w.push_back( u[i] );
00205 for( size_t i = 0; i < v.size(); i++ )
00206 w.push_back( v[i] );
00207 return w;
00208 }
00209
00211 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
00212
00213
00215
00219 typedef enum { NORMPROB, NORMLINF } ProbNormType;
00220
00222
00229 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } ProbDistType;
00230
00231
00232 }
00233
00234
00235 #endif