libDAI
include/dai/util.h
Go to the documentation of this file.
00001 /*  This file is part of libDAI - http://www.libdai.org/
00002  *
00003  *  Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
00004  *
00005  *  Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
00006  */
00007 
00008 
00011 
00012 
00013 #ifndef __defined_libdai_util_h
00014 #define __defined_libdai_util_h
00015 
00016 
00017 #include <string>
00018 #include <vector>
00019 #include <set>
00020 #include <map>
00021 #include <iostream>
00022 #include <boost/foreach.hpp>
00023 #include <boost/functional/hash.hpp>
00024 #include <boost/lexical_cast.hpp>
00025 #include <algorithm>
00026 #include <cerrno>
00027 #include <gmpxx.h>
00028 
00029 #include <dai/exceptions.h>
00030 
00031 
00032 #if defined(WINDOWS)
00033     #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
00034 #elif defined(CYGWIN)
00035     #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
00036 #elif defined(MACOSX)
00037     #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
00038 #else
00039     #include <tr1/unordered_map> // only present in modern GCC distributions
00040 #endif
00041 
00042 
00044 #define foreach BOOST_FOREACH
00045 
00046 #ifdef DAI_DEBUG
00047 
00048 
00053 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
00054 
00055 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
00056 #else
00057 #define DAI_PV(x) do {} while(0)
00058 #define DAI_DMSG(str) do {} while(0)
00059 #endif
00060 
00062 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
00063 
00064 
00065 #ifdef WINDOWS
00066 
00067     double atanh( double x );
00068 
00070     double log1p( double x );
00071 
00073     #define INFINITY (std::numeric_limits<Real>::infinity())
00074 #endif
00075 
00076 
00077 namespace dai {
00078 
00079 
00081 typedef double Real;
00082 
00084 typedef mpz_class BigInt;
00085 
00087 inline size_t BigInt_size_t( const BigInt &N ) {
00088     DAI_ASSERT( N <= std::numeric_limits<std::size_t>::max() );
00089     return N.get_ui();
00090 }
00091 
00093 bool isnan( Real x );
00094 
00096 inline Real log( Real x ) {
00097     return std::log(x);
00098 }
00099 
00101 inline Real log0( Real x ) {
00102     return x ? std::log(x) : 0;
00103 }
00104 
00106 inline Real exp( Real x ) {
00107     return std::exp(x);
00108 }
00109 
00111 inline Real pow( Real x, Real y ) {
00112     errno = 0;
00113     Real result = std::pow(x, y);
00114     DAI_DEBASSERT( errno == 0 );
00115     return result;
00116 }
00117 
00118 
00120 
00122 template <typename T, typename U, typename H = boost::hash<T> >
00123     class hash_map : public std::tr1::unordered_map<T,U,H> {};
00124 
00125 
00127 double toc();
00128 
00129 
00131 template<class T>
00132 inline T abs( const T &t ) {
00133     return (t < 0) ? (-t) : t;
00134 }
00135 
00136 
00138 void rnd_seed( size_t seed );
00139 
00141 Real rnd_uniform();
00142 
00144 Real rnd_stdnormal();
00145 
00147 int rnd_int( int min, int max );
00148 
00150 inline int rnd( int n ) {
00151     return rnd_int( 0, n-1 );
00152 }
00153 
00154 
00156 template<class T>
00157 std::string toString( const T& x ) {
00158     return boost::lexical_cast<std::string>(x);
00159 }
00160 
00161 
00163 template<class T>
00164 T fromString( const std::string& x ) {
00165     return boost::lexical_cast<T>(x);
00166 }
00167 
00168 
00170 template<class T>
00171 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
00172     os << "(";
00173     for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00174         os << (it != x.begin() ? ", " : "") << *it;
00175     os << ")";
00176     return os;
00177 }
00178 
00180 template<class T>
00181 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
00182     os << "{";
00183     for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00184         os << (it != x.begin() ? ", " : "") << *it;
00185     os << "}";
00186     return os;
00187 }
00188 
00190 template<class T1, class T2>
00191 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
00192     os << "{";
00193     for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
00194         os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
00195     os << "}";
00196     return os;
00197 }
00198 
00200 template<class T1, class T2>
00201 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
00202     os << "(" << x.first << ", " << x.second << ")";
00203     return os;
00204 }
00205 
00207 template<class T>
00208 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
00209     std::vector<T> w;
00210     w.reserve( u.size() + v.size() );
00211     for( size_t i = 0; i < u.size(); i++ )
00212         w.push_back( u[i] );
00213     for( size_t i = 0; i < v.size(); i++ )
00214         w.push_back( v[i] );
00215     return w;
00216 }
00217 
00219 
00224 std::vector<std::string> tokenizeString( const std::string& s, bool singleDelim, const std::string& delim="\t\n" );
00225 
00226 
00228 
00232 typedef enum { NORMPROB, NORMLINF } ProbNormType;
00233 
00235 
00242 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } ProbDistType;
00243 
00244 
00245 } // end of namespace dai
00246 
00247 
00248 #endif